summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdam Hupp <adam@hupp.org>2017-12-04 10:50:31 -0800
committerAdam Hupp <adam@hupp.org>2017-12-04 10:50:31 -0800
commit10e20995b5f4b8f8131b7e69912882bb81393cb2 (patch)
treec5b2ba87bc64bf58b09b22fc136d635bad7d3ca9
parent9ae12462c15100941435acf4eb9caaf5de5dddf9 (diff)
downloadpython-magic-10e20995b5f4b8f8131b7e69912882bb81393cb2.tar.gz
add from_open_file to match libmagic binding featureset
-rw-r--r--magic/__init__.py11
-rwxr-xr-xtest/test.py6
2 files changed, 17 insertions, 0 deletions
diff --git a/magic/__init__.py b/magic/__init__.py
index 3b351e2..fae6620 100644
--- a/magic/__init__.py
+++ b/magic/__init__.py
@@ -104,6 +104,13 @@ class Magic:
except MagicException as e:
return self._handle509Bug(e)
+ def from_open_file(self, open_file):
+ with self.lock:
+ try:
+ return maybe_decode(magic_descriptor(self.cookie, open_file.fileno()))
+ except MagicException as e:
+ return self._handle509Bug(e)
+
def from_file(self, filename):
# raise FileNotFoundException or IOError if the file does not exist
with open(filename):
@@ -318,6 +325,10 @@ _magic_buffer.errcheck = errorcheck_null
def magic_buffer(cookie, buf):
return _magic_buffer(cookie, buf, len(buf))
+magic_descriptor = libmagic.magic_descriptor
+magic_descriptor.restype = c_char_p
+magic_descriptor.argtypes = [magic_t, c_int]
+magic_descriptor.errcheck = errorcheck_null
_magic_descriptor = libmagic.magic_descriptor
_magic_descriptor.restype = c_char_p
diff --git a/test/test.py b/test/test.py
index 3097e6e..2865a66 100755
--- a/test/test.py
+++ b/test/test.py
@@ -74,6 +74,12 @@ class MagicTest(unittest.TestCase):
in ("text/x-python", "text/x-script.python"))
+
+ def test_open_file(self):
+ m = magic.Magic(mime=True)
+ with open(os.path.join(self.TESTDATA_DIR, "test.pdf")) as f:
+ self.assertEqual("application/pdf", m.from_open_file(f))
+
def test_mime_types(self):
dest = os.path.join(MagicTest.TESTDATA_DIR,
b'\xce\xbb'.decode('utf-8'))