Skip to content

Commit 20e2dc9

Browse files
committed
Merge branch 'master' of https://github.com/ahupp/python-magic into abi3-wheels
* 'master' of https://github.com/ahupp/python-magic: python-magic tests: add test files for elf and json, use to test flags Magic.__init__: add kwargs to enable/disable different types of magic detection typing stubs: add Magic.__init__ extension & follow_symlinks args
2 parents 1adc0a5 + 54d86fd commit 20e2dc9

5 files changed

Lines changed: 137 additions & 6 deletions

File tree

magic/__init__.py

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@ class Magic:
4040

4141
def __init__(self, mime=False, magic_file=None, mime_encoding=False,
4242
keep_going=False, uncompress=False, raw=False, extension=False,
43-
follow_symlinks=False):
43+
follow_symlinks=False, check_tar=True, check_soft=True,
44+
check_apptype=True, check_elf=True, check_text=True,
45+
check_cdf=True, check_csv=True, check_encoding=True,
46+
check_json=True, check_simh=True):
4447
"""
4548
Create a new libmagic wrapper.
4649
@@ -69,6 +72,27 @@ def __init__(self, mime=False, magic_file=None, mime_encoding=False,
6972
if follow_symlinks:
7073
self.flags |= MAGIC_SYMLINK
7174

75+
if not check_tar:
76+
self.flags |= MAGIC_NO_CHECK_TAR
77+
if not check_soft:
78+
self.flags |= MAGIC_NO_CHECK_SOFT
79+
if not check_apptype:
80+
self.flags |= MAGIC_NO_CHECK_APPTYPE
81+
if not check_elf:
82+
self.flags |= MAGIC_NO_CHECK_ELF
83+
if not check_text:
84+
self.flags |= MAGIC_NO_CHECK_TEXT
85+
if not check_cdf:
86+
self.flags |= MAGIC_NO_CHECK_CDF
87+
if not check_csv:
88+
self.flags |= MAGIC_NO_CHECK_CSV
89+
if not check_encoding:
90+
self.flags |= MAGIC_NO_CHECK_ENCODING
91+
if not check_json:
92+
self.flags |= MAGIC_NO_CHECK_JSON
93+
if not check_simh:
94+
self.flags |= MAGIC_NO_CHECK_SIMH
95+
7296
self.cookie = magic_open(self.flags)
7397
self.lock = threading.Lock()
7498

@@ -416,10 +440,16 @@ def version():
416440
MAGIC_NO_CHECK_SOFT = 0x004000 # Don't check magic entries
417441
MAGIC_NO_CHECK_APPTYPE = 0x008000 # Don't check application type
418442
MAGIC_NO_CHECK_ELF = 0x010000 # Don't check for elf details
419-
MAGIC_NO_CHECK_ASCII = 0x020000 # Don't check for ascii files
420-
MAGIC_NO_CHECK_TROFF = 0x040000 # Don't check ascii/troff
421-
MAGIC_NO_CHECK_FORTRAN = 0x080000 # Don't check ascii/fortran
422-
MAGIC_NO_CHECK_TOKENS = 0x100000 # Don't check ascii/tokens
443+
MAGIC_NO_CHECK_TEXT = 0x020000 # Don't check for ascii files
444+
MAGIC_NO_CHECK_ASCII = 0x020000 # Deprecated alias for MAGIC_NO_CHECK_TEXT
445+
MAGIC_NO_CHECK_TROFF = 0x040000 # Don't check ascii/troff (deprecated)
446+
MAGIC_NO_CHECK_FORTRAN = 0x080000 # Don't check ascii/fortran (deprecated)
447+
MAGIC_NO_CHECK_TOKENS = 0x100000 # Don't check ascii/tokens (deprecated)
448+
MAGIC_NO_CHECK_CDF = 0x0040000 # Don't check for CDF files
449+
MAGIC_NO_CHECK_CSV = 0x0080000 # Don't check for CSV files
450+
MAGIC_NO_CHECK_ENCODING = 0x0200000 # Don't check text encodings
451+
MAGIC_NO_CHECK_JSON = 0x0400000 # Don't check for JSON files
452+
MAGIC_NO_CHECK_SIMH = 0x0800000 # Don't check for SIMH tape files
423453

424454
MAGIC_PARAM_INDIR_MAX = 0 # Recursion limit for indirect magic
425455
MAGIC_PARAM_NAME_MAX = 1 # Use count limit for name/use magic

magic/__init__.pyi

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class Magic:
1111
flags: int = ...
1212
cookie: Any = ...
1313
lock: threading.Lock = ...
14-
def __init__(self, mime: bool = ..., magic_file: Optional[Any] = ..., mime_encoding: bool = ..., keep_going: bool = ..., uncompress: bool = ..., raw: bool = ...) -> None: ...
14+
def __init__(self, mime: bool = ..., magic_file: Optional[Any] = ..., mime_encoding: bool = ..., keep_going: bool = ..., uncompress: bool = ..., raw: bool = ..., extension: bool = ..., follow_symlinks: bool = ..., check_tar: bool = ..., check_soft: bool = ..., check_apptype: bool = ..., check_elf: bool = ..., check_text: bool = ..., check_encoding: bool = ..., check_json: bool = ..., check_simh: bool = ...) -> None: ...
1515
def from_buffer(self, buf: Union[bytes, str]) -> Text: ...
1616
def from_file(self, filename: Union[bytes, str, PathLike]) -> Text: ...
1717
def from_descriptor(self, fd: int, mime: bool = ...) -> Text: ...
@@ -74,10 +74,16 @@ MAGIC_NO_CHECK_TAR: int
7474
MAGIC_NO_CHECK_SOFT: int
7575
MAGIC_NO_CHECK_APPTYPE: int
7676
MAGIC_NO_CHECK_ELF: int
77+
MAGIC_NO_CHECK_TEXT: int
7778
MAGIC_NO_CHECK_ASCII: int
7879
MAGIC_NO_CHECK_TROFF: int
7980
MAGIC_NO_CHECK_FORTRAN: int
81+
MAGIC_NO_CHECK_CDF: int
82+
MAGIC_NO_CHECK_CSV: int
8083
MAGIC_NO_CHECK_TOKENS: int
84+
MAGIC_NO_CHECK_ENCODING: int
85+
MAGIC_NO_CHECK_JSON: int
86+
MAGIC_NO_CHECK_SIMH: int
8187
MAGIC_PARAM_INDIR_MAX: int
8288
MAGIC_PARAM_NAME_MAX: int
8389
MAGIC_PARAM_ELF_PHNUM_MAX: int

test/python_magic_test.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ def test_mime_types(self):
9696
self.assert_values(
9797
m,
9898
{
99+
"elf-NetBSD-x86_64-echo": (
100+
"application/x-pie-executable",
101+
"application/x-sharedlib",
102+
),
99103
"magic._pyc_": (
100104
"application/octet-stream",
101105
"text/x-bytecode.python",
@@ -107,7 +111,9 @@ def test_mime_types(self):
107111
"text.txt": "text/plain",
108112
b"\xce\xbb".decode("utf-8"): "text/plain",
109113
b"\xce\xbb": "text/plain",
114+
"test.json": "application/json",
110115
},
116+
buf_equals_file=False,
111117
)
112118
finally:
113119
os.unlink(dest)
@@ -119,6 +125,88 @@ def test_descriptions(self):
119125
self.assert_values(
120126
m,
121127
{
128+
"elf-NetBSD-x86_64-echo": (
129+
"ELF 64-bit LSB shared object, x86-64, version 1 (SYSV)",
130+
"ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /libexec/ld.elf_so, for NetBSD 8.0, not stripped",
131+
),
132+
"magic._pyc_": "python 2.4 byte-compiled",
133+
"test.pdf": (
134+
"PDF document, version 1.2",
135+
"PDF document, version 1.2, 2 pages",
136+
"PDF document, version 1.2, 2 page(s)",
137+
),
138+
"test.gz": (
139+
'gzip compressed data, was "test", from Unix, last '
140+
"modified: Sun Jun 29 01:32:52 2008",
141+
'gzip compressed data, was "test", last modified'
142+
": Sun Jun 29 01:32:52 2008, from Unix",
143+
'gzip compressed data, was "test", last modified'
144+
": Sun Jun 29 01:32:52 2008, from Unix, original size 15",
145+
'gzip compressed data, was "test", '
146+
"last modified: Sun Jun 29 01:32:52 2008, "
147+
"from Unix, original size modulo 2^32 15",
148+
'gzip compressed data, was "test", last modified'
149+
": Sun Jun 29 01:32:52 2008, from Unix, truncated",
150+
),
151+
"text.txt": "ASCII text",
152+
"test.snappy.parquet": ("Apache Parquet", "Par archive data"),
153+
"test.json": "JSON text data",
154+
},
155+
buf_equals_file=False,
156+
)
157+
finally:
158+
del os.environ["TZ"]
159+
160+
def test_descriptions_no_soft(self):
161+
m = magic.Magic(check_soft=False)
162+
self.assert_values(
163+
m,
164+
{
165+
"elf-NetBSD-x86_64-echo": (
166+
"data",
167+
"ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /libexec/ld.elf_so, for NetBSD 8.0, not stripped",
168+
),
169+
"magic._pyc_": "data",
170+
"test.pdf": "ASCII text",
171+
"test.gz": "data",
172+
"text.txt": "ASCII text",
173+
"test.snappy.parquet": "data",
174+
"test.json": "JSON text data",
175+
},
176+
buf_equals_file=False,
177+
)
178+
179+
def test_descriptions_no_elf(self):
180+
m = magic.Magic(check_elf=False)
181+
self.assert_values(
182+
m,
183+
{
184+
"elf-NetBSD-x86_64-echo": "ELF 64-bit LSB shared object, x86-64, version 1 (SYSV)",
185+
},
186+
buf_equals_file=True,
187+
)
188+
189+
def test_descriptions_no_json(self):
190+
m = magic.Magic(check_elf=False)
191+
self.assert_values(
192+
m,
193+
{
194+
"test.json": "data",
195+
},
196+
buf_equals_file=True,
197+
)
198+
199+
def test_descriptions_no_json(self):
200+
m = magic.Magic(check_json=False)
201+
os.environ["TZ"] = "UTC" # To get last modified date of test.gz in UTC
202+
try:
203+
self.assert_values(
204+
m,
205+
{
206+
"elf-NetBSD-x86_64-echo": (
207+
"ELF 64-bit LSB shared object, x86-64, version 1 (SYSV)",
208+
"ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /libexec/ld.elf_so, for NetBSD 8.0, not stripped",
209+
),
122210
"magic._pyc_": "python 2.4 byte-compiled",
123211
"test.pdf": (
124212
"PDF document, version 1.2",
9.33 KB
Binary file not shown.

test/testdata/test.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[
2+
{
3+
"one": 2,
4+
"three": null,
5+
"four": [5, "six", false]
6+
}
7+
]

0 commit comments

Comments
 (0)