xref: /freebsd/contrib/file/magic/Magdir/python (revision e949ce9dc0e6fff26e83904f1008b76d36ba0a37)
1b6cee71dSXin LI
2b6cee71dSXin LI#------------------------------------------------------------------------------
3*e949ce9dSXin LI# $File: python,v 1.49 2025/06/17 21:14:40 christos Exp $
4b6cee71dSXin LI# python:  file(1) magic for python
5b6cee71dSXin LI#
6b6cee71dSXin LI# Outlook puts """ too for urgent messages
7b6cee71dSXin LI# From: David Necas <yeti@physics.muni.cz>
8b6cee71dSXin LI# often the module starts with a multiline string
9b6cee71dSXin LI0	string/t	"""	Python script text executable
10d38c30c0SXin LI# MAGIC as specified in Python/import.c (1.0 to 3.7)
11a4d6d3b8SXin LI# and in Lib/importlib/_bootstrap_external.py (3.5+)
12a5d223e6SXin LI# two bytes of magic followed by "\r\n" in little endian order
13d38c30c0SXin LI0	belong		0x02099900	python 1.0 byte-compiled
1443a5ec4eSXin LI!:mime application/x-bytecode.python
15d38c30c0SXin LI0	belong		0x03099900	python 1.1/1.2 byte-compiled
1643a5ec4eSXin LI!:mime application/x-bytecode.python
17d38c30c0SXin LI0	belong		0x892e0d0a	python 1.3 byte-compiled
1843a5ec4eSXin LI!:mime application/x-bytecode.python
19d38c30c0SXin LI0	belong		0x04170d0a	python 1.4 byte-compiled
2043a5ec4eSXin LI!:mime application/x-bytecode.python
21d38c30c0SXin LI0	belong		0x994e0d0a	python 1.5 byte-compiled
2243a5ec4eSXin LI!:mime application/x-bytecode.python
23d38c30c0SXin LI0	belong		0xfcc40d0a	python 1.6 byte-compiled
2443a5ec4eSXin LI!:mime application/x-bytecode.python
25d38c30c0SXin LI0	belong		0xfdc40d0a	python 1.6 byte-compiled
2643a5ec4eSXin LI!:mime application/x-bytecode.python
27b6cee71dSXin LI0	belong		0x87c60d0a	python 2.0 byte-compiled
2843a5ec4eSXin LI!:mime application/x-bytecode.python
29d38c30c0SXin LI0	belong		0x88c60d0a	python 2.0 byte-compiled
3043a5ec4eSXin LI!:mime application/x-bytecode.python
31b6cee71dSXin LI0	belong		0x2aeb0d0a	python 2.1 byte-compiled
3243a5ec4eSXin LI!:mime application/x-bytecode.python
33d38c30c0SXin LI0	belong		0x2beb0d0a	python 2.1 byte-compiled
3443a5ec4eSXin LI!:mime application/x-bytecode.python
35b6cee71dSXin LI0	belong		0x2ded0d0a	python 2.2 byte-compiled
3643a5ec4eSXin LI!:mime application/x-bytecode.python
37d38c30c0SXin LI0	belong		0x2eed0d0a	python 2.2 byte-compiled
3843a5ec4eSXin LI!:mime application/x-bytecode.python
39b6cee71dSXin LI0	belong		0x3bf20d0a	python 2.3 byte-compiled
4043a5ec4eSXin LI!:mime application/x-bytecode.python
41d38c30c0SXin LI0	belong		0x3cf20d0a	python 2.3 byte-compiled
4243a5ec4eSXin LI!:mime application/x-bytecode.python
432726a701SXin LI0	belong		0x45f20d0a	python 2.3 byte-compiled
4443a5ec4eSXin LI!:mime application/x-bytecode.python
452726a701SXin LI0	belong		0x59f20d0a	python 2.4 byte-compiled
4643a5ec4eSXin LI!:mime application/x-bytecode.python
472726a701SXin LI0	belong		0x63f20d0a	python 2.4 byte-compiled
4843a5ec4eSXin LI!:mime application/x-bytecode.python
49b6cee71dSXin LI0	belong		0x6df20d0a	python 2.4 byte-compiled
5043a5ec4eSXin LI!:mime application/x-bytecode.python
51d38c30c0SXin LI0	belong		0x6ef20d0a	python 2.4 byte-compiled
5243a5ec4eSXin LI!:mime application/x-bytecode.python
532726a701SXin LI0	belong		0x77f20d0a	python 2.5 byte-compiled
5443a5ec4eSXin LI!:mime application/x-bytecode.python
552726a701SXin LI0	belong		0x81f20d0a	python 2.5 byte-compiled
5643a5ec4eSXin LI!:mime application/x-bytecode.python
572726a701SXin LI0	belong		0x8bf20d0a	python 2.5 byte-compiled
5843a5ec4eSXin LI!:mime application/x-bytecode.python
592726a701SXin LI0	belong		0x8cf20d0a	python 2.5 byte-compiled
6043a5ec4eSXin LI!:mime application/x-bytecode.python
612726a701SXin LI0	belong		0x95f20d0a	python 2.5 byte-compiled
6243a5ec4eSXin LI!:mime application/x-bytecode.python
632726a701SXin LI0	belong		0x9ff20d0a	python 2.5 byte-compiled
6443a5ec4eSXin LI!:mime application/x-bytecode.python
652726a701SXin LI0	belong		0xa9f20d0a	python 2.5 byte-compiled
6643a5ec4eSXin LI!:mime application/x-bytecode.python
67b6cee71dSXin LI0	belong		0xb3f20d0a	python 2.5 byte-compiled
6843a5ec4eSXin LI!:mime application/x-bytecode.python
69d38c30c0SXin LI0	belong		0xb4f20d0a	python 2.5 byte-compiled
7043a5ec4eSXin LI!:mime application/x-bytecode.python
712726a701SXin LI0	belong		0xc7f20d0a	python 2.6 byte-compiled
7243a5ec4eSXin LI!:mime application/x-bytecode.python
73b6cee71dSXin LI0	belong		0xd1f20d0a	python 2.6 byte-compiled
7443a5ec4eSXin LI!:mime application/x-bytecode.python
75d38c30c0SXin LI0	belong		0xd2f20d0a	python 2.6 byte-compiled
7643a5ec4eSXin LI!:mime application/x-bytecode.python
772726a701SXin LI0	belong		0xdbf20d0a	python 2.7 byte-compiled
7843a5ec4eSXin LI!:mime application/x-bytecode.python
792726a701SXin LI0	belong		0xe5f20d0a	python 2.7 byte-compiled
8043a5ec4eSXin LI!:mime application/x-bytecode.python
812726a701SXin LI0	belong		0xeff20d0a	python 2.7 byte-compiled
8243a5ec4eSXin LI!:mime application/x-bytecode.python
832726a701SXin LI0	belong		0xf9f20d0a	python 2.7 byte-compiled
8443a5ec4eSXin LI!:mime application/x-bytecode.python
85b6cee71dSXin LI0	belong		0x03f30d0a	python 2.7 byte-compiled
8643a5ec4eSXin LI!:mime application/x-bytecode.python
87d38c30c0SXin LI0	belong		0x04f30d0a	python 2.7 byte-compiled
8843a5ec4eSXin LI!:mime application/x-bytecode.python
89a2dfb722SXin LI0	belong		0x0af30d0a	PyPy2.7 byte-compiled
90a2dfb722SXin LI!:mime application/x-bytecode.python
912726a701SXin LI0	belong		0xb80b0d0a	python 3.0 byte-compiled
9243a5ec4eSXin LI!:mime application/x-bytecode.python
932726a701SXin LI0	belong		0xc20b0d0a	python 3.0 byte-compiled
9443a5ec4eSXin LI!:mime application/x-bytecode.python
952726a701SXin LI0	belong		0xcc0b0d0a	python 3.0 byte-compiled
9643a5ec4eSXin LI!:mime application/x-bytecode.python
972726a701SXin LI0	belong		0xd60b0d0a	python 3.0 byte-compiled
9843a5ec4eSXin LI!:mime application/x-bytecode.python
992726a701SXin LI0	belong		0xe00b0d0a	python 3.0 byte-compiled
10043a5ec4eSXin LI!:mime application/x-bytecode.python
1012726a701SXin LI0	belong		0xea0b0d0a	python 3.0 byte-compiled
10243a5ec4eSXin LI!:mime application/x-bytecode.python
1032726a701SXin LI0	belong		0xf40b0d0a	python 3.0 byte-compiled
10443a5ec4eSXin LI!:mime application/x-bytecode.python
1052726a701SXin LI0	belong		0xf50b0d0a	python 3.0 byte-compiled
10643a5ec4eSXin LI!:mime application/x-bytecode.python
1072726a701SXin LI0	belong		0xff0b0d0a	python 3.0 byte-compiled
10843a5ec4eSXin LI!:mime application/x-bytecode.python
1092726a701SXin LI0	belong		0x090c0d0a	python 3.0 byte-compiled
11043a5ec4eSXin LI!:mime application/x-bytecode.python
1112726a701SXin LI0	belong		0x130c0d0a	python 3.0 byte-compiled
11243a5ec4eSXin LI!:mime application/x-bytecode.python
1132726a701SXin LI0	belong		0x1d0c0d0a	python 3.0 byte-compiled
11443a5ec4eSXin LI!:mime application/x-bytecode.python
1152726a701SXin LI0	belong		0x1f0c0d0a	python 3.0 byte-compiled
11643a5ec4eSXin LI!:mime application/x-bytecode.python
1172726a701SXin LI0	belong		0x270c0d0a	python 3.0 byte-compiled
11843a5ec4eSXin LI!:mime application/x-bytecode.python
119b6cee71dSXin LI0	belong		0x3b0c0d0a	python 3.0 byte-compiled
12043a5ec4eSXin LI!:mime application/x-bytecode.python
1212726a701SXin LI0	belong		0x450c0d0a	python 3.1 byte-compiled
12243a5ec4eSXin LI!:mime application/x-bytecode.python
123b6cee71dSXin LI0	belong		0x4f0c0d0a	python 3.1 byte-compiled
12443a5ec4eSXin LI!:mime application/x-bytecode.python
1252726a701SXin LI0	belong		0x580c0d0a	python 3.2 byte-compiled
12643a5ec4eSXin LI!:mime application/x-bytecode.python
1272726a701SXin LI0	belong		0x620c0d0a	python 3.2 byte-compiled
12843a5ec4eSXin LI!:mime application/x-bytecode.python
129b6cee71dSXin LI0	belong		0x6c0c0d0a	python 3.2 byte-compiled
13043a5ec4eSXin LI!:mime application/x-bytecode.python
1312726a701SXin LI0	belong		0x760c0d0a	python 3.3 byte-compiled
13243a5ec4eSXin LI!:mime application/x-bytecode.python
1332726a701SXin LI0	belong		0x800c0d0a	python 3.3 byte-compiled
13443a5ec4eSXin LI!:mime application/x-bytecode.python
1352726a701SXin LI0	belong		0x8a0c0d0a	python 3.3 byte-compiled
13643a5ec4eSXin LI!:mime application/x-bytecode.python
1372726a701SXin LI0	belong		0x940c0d0a	python 3.3 byte-compiled
13843a5ec4eSXin LI!:mime application/x-bytecode.python
139b6cee71dSXin LI0	belong		0x9e0c0d0a	python 3.3 byte-compiled
14043a5ec4eSXin LI!:mime application/x-bytecode.python
1412726a701SXin LI0	belong		0xb20c0d0a	python 3.4 byte-compiled
14243a5ec4eSXin LI!:mime application/x-bytecode.python
1432726a701SXin LI0	belong		0xbc0c0d0a	python 3.4 byte-compiled
14443a5ec4eSXin LI!:mime application/x-bytecode.python
1452726a701SXin LI0	belong		0xc60c0d0a	python 3.4 byte-compiled
14643a5ec4eSXin LI!:mime application/x-bytecode.python
1472726a701SXin LI0	belong		0xd00c0d0a	python 3.4 byte-compiled
14843a5ec4eSXin LI!:mime application/x-bytecode.python
1492726a701SXin LI0	belong		0xda0c0d0a	python 3.4 byte-compiled
15043a5ec4eSXin LI!:mime application/x-bytecode.python
1512726a701SXin LI0	belong		0xe40c0d0a	python 3.4 byte-compiled
15243a5ec4eSXin LI!:mime application/x-bytecode.python
153c2931133SXin LI0	belong		0xee0c0d0a	python 3.4 byte-compiled
15443a5ec4eSXin LI!:mime application/x-bytecode.python
1552726a701SXin LI0	belong		0xf80c0d0a	python 3.5.1- byte-compiled
15643a5ec4eSXin LI!:mime application/x-bytecode.python
1572726a701SXin LI0	belong		0x020d0d0a	python 3.5.1- byte-compiled
15843a5ec4eSXin LI!:mime application/x-bytecode.python
1592726a701SXin LI0	belong		0x0c0d0d0a	python 3.5.1- byte-compiled
16043a5ec4eSXin LI!:mime application/x-bytecode.python
1612726a701SXin LI0	belong		0x160d0d0a	python 3.5.1- byte-compiled
16243a5ec4eSXin LI!:mime application/x-bytecode.python
1632726a701SXin LI0	belong		0x170d0d0a	python 3.5.2+ byte-compiled
16443a5ec4eSXin LI!:mime application/x-bytecode.python
1652726a701SXin LI0	belong		0x200d0d0a	python 3.6 byte-compiled
16643a5ec4eSXin LI!:mime application/x-bytecode.python
1672726a701SXin LI0	belong		0x210d0d0a	python 3.6 byte-compiled
16843a5ec4eSXin LI!:mime application/x-bytecode.python
1692726a701SXin LI0	belong		0x2a0d0d0a	python 3.6 byte-compiled
17043a5ec4eSXin LI!:mime application/x-bytecode.python
1712726a701SXin LI0	belong		0x2b0d0d0a	python 3.6 byte-compiled
17243a5ec4eSXin LI!:mime application/x-bytecode.python
1732726a701SXin LI0	belong		0x2c0d0d0a	python 3.6 byte-compiled
17443a5ec4eSXin LI!:mime application/x-bytecode.python
1752726a701SXin LI0	belong		0x2d0d0d0a	python 3.6 byte-compiled
17643a5ec4eSXin LI!:mime application/x-bytecode.python
1772726a701SXin LI0	belong		0x2f0d0d0a	python 3.6 byte-compiled
17843a5ec4eSXin LI!:mime application/x-bytecode.python
1792726a701SXin LI0	belong		0x300d0d0a	python 3.6 byte-compiled
18043a5ec4eSXin LI!:mime application/x-bytecode.python
1812726a701SXin LI0	belong		0x310d0d0a	python 3.6 byte-compiled
18243a5ec4eSXin LI!:mime application/x-bytecode.python
1832726a701SXin LI0	belong		0x320d0d0a	python 3.6 byte-compiled
18443a5ec4eSXin LI!:mime application/x-bytecode.python
18540427ccaSGordon Tetlow0	belong		0x330d0d0a	python 3.6 byte-compiled
18643a5ec4eSXin LI!:mime application/x-bytecode.python
1872726a701SXin LI0	belong		0x3e0d0d0a	python 3.7 byte-compiled
18843a5ec4eSXin LI!:mime application/x-bytecode.python
1892726a701SXin LI0	belong		0x3f0d0d0a	python 3.7 byte-compiled
19043a5ec4eSXin LI!:mime application/x-bytecode.python
191a2dfb722SXin LI
192a2dfb722SXin LI# magic 3392+ implements PEP 552: Deterministic pycs
193a2dfb722SXin LI0	name		pyc-pep552
194ae316d1dSXin LI>0	uleshort	x	(magic: %04d),
195a2dfb722SXin LI# the flag field determines how .pyc validity is checked
196a2dfb722SXin LI>4	ulelong&1	0		timestamp-based,
197a2dfb722SXin LI>>8	uledate		x		.py timestamp: %s UTC,
198a2dfb722SXin LI>>12	ulelong		x		.py size: %d bytes
199a2dfb722SXin LI>4	ulelong&1	!0		hash-based, check-source flag
200a2dfb722SXin LI>>4	ulelong&2	0		unset,
201a2dfb722SXin LI>>4	ulelong&2	!0		set,
202a2dfb722SXin LI>>8	ulequad		x		hash: 0x%llx
203a2dfb722SXin LI
204a2dfb722SXin LI# uleshort magic followed by \x0d\0xa
205a2dfb722SXin LI2		string		\x0d\x0a
206a2dfb722SXin LI# extra check: only two bits of flag field are currently used
207a2dfb722SXin LI>4		ulelong		<0x4
208*e949ce9dSXin LI# \x0d as part of magic suffices up to Python 3.13
209a2dfb722SXin LI>>1		ubyte		0x0d		Byte-compiled Python module for
21043a5ec4eSXin LI!:mime application/x-bytecode.python
211a2dfb722SXin LI# now look at the magic number to determine the version
212ae316d1dSXin LI>>>0		uleshort	<3400		CPython 3.7
213a2dfb722SXin LI>>>0		default		x
214ae316d1dSXin LI>>>>0		uleshort	<3420		CPython 3.8
215a2dfb722SXin LI>>>>0		default		x
216ae316d1dSXin LI>>>>>0		uleshort	<3430		CPython 3.9
217a2dfb722SXin LI>>>>>0		default		x
218ae316d1dSXin LI>>>>>>0		uleshort	<3450		CPython 3.10
219a2dfb722SXin LI>>>>>>0		default		x
220ae316d1dSXin LI>>>>>>>0	uleshort	<3500		CPython 3.11
221ae316d1dSXin LI>>>>>>>0	default		x
222ae316d1dSXin LI>>>>>>>>0	uleshort	<3550		CPython 3.12
223*e949ce9dSXin LI>>>>>>>>0	default		x		CPython 3.13
224a2dfb722SXin LI>>>0		use		pyc-pep552
225*e949ce9dSXin LI# \x0e means Python 3.14 or newer
226*e949ce9dSXin LI>>1		ubyte		0x0e		Byte-compiled Python module for
227*e949ce9dSXin LI!:mime application/x-bytecode.python
228*e949ce9dSXin LI>>>0	uleshort	<3650		CPython 3.14
229*e949ce9dSXin LI>>>0	default		x
230*e949ce9dSXin LI>>>>0	uleshort	<3700		CPython 3.15
231*e949ce9dSXin LI>>>>0	default		x		CPython 3.16 or newer
232*e949ce9dSXin LI>>>0		use		pyc-pep552
233*e949ce9dSXin LI# PyPy magic numbers
234ae316d1dSXin LI>>0		uleshort	240		Byte-compiled Python module for PyPy3.7
23543a5ec4eSXin LI!:mime application/x-bytecode.python
236a2dfb722SXin LI>>>0		use		pyc-pep552
237ae316d1dSXin LI>>0		uleshort	256		Byte-compiled Python module for PyPy3.8
23843a5ec4eSXin LI!:mime application/x-bytecode.python
239a2dfb722SXin LI>>>0		use		pyc-pep552
240ae316d1dSXin LI>>0		uleshort	336		Byte-compiled Python module for PyPy3.9
24143a5ec4eSXin LI!:mime application/x-bytecode.python
242a2dfb722SXin LI>>>0		use		pyc-pep552
243*e949ce9dSXin LI>>0		uleshort	384		Byte-compiled Python module for PyPy3.10
244*e949ce9dSXin LI!:mime application/x-bytecode.python
245*e949ce9dSXin LI>>>0		use		pyc-pep552
246*e949ce9dSXin LI>>0		uleshort	416		Byte-compiled Python module for PyPy3.11
247*e949ce9dSXin LI!:mime application/x-bytecode.python
248*e949ce9dSXin LI>>>0		use		pyc-pep552
24940427ccaSGordon Tetlow
2502726a701SXin LI0	search/1/w	#!\040/usr/bin/python	Python script text executable
251*e949ce9dSXin LI!:strength + 30
2522726a701SXin LI!:mime text/x-script.python
2532726a701SXin LI0	search/1/w	#!\040/usr/local/bin/python	Python script text executable
254*e949ce9dSXin LI!:strength + 30
2552726a701SXin LI!:mime text/x-script.python
2562726a701SXin LI0	search/10/w	#!\040/usr/bin/env\040python	Python script text executable
257*e949ce9dSXin LI!:strength + 30
2582726a701SXin LI!:mime text/x-script.python
259b6cee71dSXin LI
260b6cee71dSXin LI
261b6cee71dSXin LI# from module.submodule import func1, func2
26248c779cdSXin LI0	search/8192	import
2632726a701SXin LI>0	regex		\^from[\040\t]+([A-Za-z0-9_]|\\.)+[\040\t]+import.*$	Python script text executable
26440427ccaSGordon Tetlow!:strength + 15
2652726a701SXin LI!:mime text/x-script.python
266ae316d1dSXin LI>0	regex		\^import\ [_[:alpha:]]+\ as\ [[:alpha:]][[:space:]]*$ Python script text executable
267ae316d1dSXin LI!:mime text/x-script.python
268ae316d1dSXin LI
269b6cee71dSXin LI
270b6cee71dSXin LI# def __init__ (self, ...):
271b6cee71dSXin LI0	search/4096	def\ __init__
272b6cee71dSXin LI>&0	search/64 self	Python script text executable
27340427ccaSGordon Tetlow!:strength + 15
2742726a701SXin LI!:mime text/x-script.python
27540427ccaSGordon Tetlow
27640427ccaSGordon Tetlow# if __name__ == "__main__":
27740427ccaSGordon Tetlow0 search/4096 if\ __name__
27840427ccaSGordon Tetlow>&0 search/64 '__main__'	Python script text executable
27940427ccaSGordon Tetlow>&0 search/64 "__main__"	Python script text executable
28040427ccaSGordon Tetlow!:strength + 15
2812726a701SXin LI!:mime text/x-script.python
28240427ccaSGordon Tetlow
283b6cee71dSXin LI# comments
284b6cee71dSXin LI#0	search/4096	'''
285b6cee71dSXin LI#>&0	regex	.*'''$	Python script text executable
2862726a701SXin LI#!:mime text/x-script.python
287b6cee71dSXin LI
288b6cee71dSXin LI#0	search/4096	"""
289b6cee71dSXin LI#>&0	regex	.*"""$	Python script text executable
2902726a701SXin LI#!:mime text/x-script.python
291b6cee71dSXin LI
292b6cee71dSXin LI# try:
293b6cee71dSXin LI# except: or finally:
294b6cee71dSXin LI# block
295b6cee71dSXin LI0	search/4096	try:
29640427ccaSGordon Tetlow>&0	regex	\^[[:space:]]*except.*:$	Python script text executable
29740427ccaSGordon Tetlow!:strength + 15
2982726a701SXin LI!:mime text/x-script.python
299b6cee71dSXin LI>&0	search/4096	finally:	Python script text executable
3002726a701SXin LI!:mime text/x-script.python
301b6cee71dSXin LI
30240427ccaSGordon Tetlow# class name[(base classes,)]: [pass]
30348c779cdSXin LI0	search/8192	class
30448c779cdSXin LI>0	regex	\^class\ [_[:alpha:]]+(\\(.*\\))?(\ )*:([\ \t]+pass)?$		Python script text executable
30540427ccaSGordon Tetlow!:strength + 15
3062726a701SXin LI!:mime text/x-script.python
30740427ccaSGordon Tetlow
30840427ccaSGordon Tetlow# def name(*args, **kwargs):
30948c779cdSXin LI0	search/8192	def\
31048c779cdSXin LI>0	regex	 \^[[:space:]]{0,50}def\ {1,50}[_a-zA-Z]{1,100}
31148c779cdSXin LI>>&0	regex	 \\(([[:alpha:]*_,\ ]){0,255}\\):$ Python script text executable
31240427ccaSGordon Tetlow!:strength + 15
3132726a701SXin LI!:mime text/x-script.python
3142726a701SXin LI
3152726a701SXin LI# https://numpy.org/devdocs/reference/generated/numpy.lib.format.html
3162726a701SXin LI0	string	\223NUMPY		NumPy data file
3172726a701SXin LI!:mime  application/x-numpy-data
3182726a701SXin LI>6	byte	x			\b, version %d
3192726a701SXin LI>7	byte	x			\b.%d
3202726a701SXin LI#>8	leshort	x			\b, header length=%d
3212726a701SXin LI>10	string	x			\b, description %s
322