xref: /freebsd/contrib/file/magic/Magdir/python (revision ae316d1d1cffd71ab7751f94e10118777a88e027)
1b6cee71dSXin LI
2b6cee71dSXin LI#------------------------------------------------------------------------------
3*ae316d1dSXin LI# $File: python,v 1.47 2024/08/27 18:50:57 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
194*ae316d1dSXin 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
208a2dfb722SXin LI# \x0d as part of magic should suffice till Python 3.14 (magic 3600)
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
212*ae316d1dSXin LI>>>0		uleshort	<3400		CPython 3.7
213a2dfb722SXin LI>>>0		default		x
214*ae316d1dSXin LI>>>>0		uleshort	<3420		CPython 3.8
215a2dfb722SXin LI>>>>0		default		x
216*ae316d1dSXin LI>>>>>0		uleshort	<3430		CPython 3.9
217a2dfb722SXin LI>>>>>0		default		x
218*ae316d1dSXin LI>>>>>>0		uleshort	<3450		CPython 3.10
219a2dfb722SXin LI>>>>>>0		default		x
220*ae316d1dSXin LI>>>>>>>0	uleshort	<3500		CPython 3.11
221*ae316d1dSXin LI>>>>>>>0	default		x
222*ae316d1dSXin LI>>>>>>>>0	uleshort	<3550		CPython 3.12
223*ae316d1dSXin LI>>>>>>>>0	default		x
224*ae316d1dSXin LI>>>>>>>>>0	uleshort	<3600		CPython 3.13
225*ae316d1dSXin LI>>>>>>>>>0	default		x		CPython 3.14 or newer
226a2dfb722SXin LI>>>0		use		pyc-pep552
227*ae316d1dSXin LI>>0		uleshort	240		Byte-compiled Python module for PyPy3.7
22843a5ec4eSXin LI!:mime application/x-bytecode.python
229a2dfb722SXin LI>>>0		use		pyc-pep552
230*ae316d1dSXin LI>>0		uleshort	256		Byte-compiled Python module for PyPy3.8
23143a5ec4eSXin LI!:mime application/x-bytecode.python
232a2dfb722SXin LI>>>0		use		pyc-pep552
233*ae316d1dSXin LI>>0		uleshort	336		Byte-compiled Python module for PyPy3.9
23443a5ec4eSXin LI!:mime application/x-bytecode.python
235a2dfb722SXin LI>>>0		use		pyc-pep552
23640427ccaSGordon Tetlow
2372726a701SXin LI0	search/1/w	#!\040/usr/bin/python	Python script text executable
2383e41d09dSXin LI!:strength + 15
2392726a701SXin LI!:mime text/x-script.python
2402726a701SXin LI0	search/1/w	#!\040/usr/local/bin/python	Python script text executable
2413e41d09dSXin LI!:strength + 15
2422726a701SXin LI!:mime text/x-script.python
2432726a701SXin LI0	search/10/w	#!\040/usr/bin/env\040python	Python script text executable
2443e41d09dSXin LI!:strength + 15
2452726a701SXin LI!:mime text/x-script.python
246b6cee71dSXin LI
247b6cee71dSXin LI
248b6cee71dSXin LI# from module.submodule import func1, func2
24948c779cdSXin LI0	search/8192	import
2502726a701SXin LI>0	regex		\^from[\040\t]+([A-Za-z0-9_]|\\.)+[\040\t]+import.*$	Python script text executable
25140427ccaSGordon Tetlow!:strength + 15
2522726a701SXin LI!:mime text/x-script.python
253*ae316d1dSXin LI>0	regex		\^import\ [_[:alpha:]]+\ as\ [[:alpha:]][[:space:]]*$ Python script text executable
254*ae316d1dSXin LI!:mime text/x-script.python
255*ae316d1dSXin LI
256b6cee71dSXin LI
257b6cee71dSXin LI# def __init__ (self, ...):
258b6cee71dSXin LI0	search/4096	def\ __init__
259b6cee71dSXin LI>&0	search/64 self	Python script text executable
26040427ccaSGordon Tetlow!:strength + 15
2612726a701SXin LI!:mime text/x-script.python
26240427ccaSGordon Tetlow
26340427ccaSGordon Tetlow# if __name__ == "__main__":
26440427ccaSGordon Tetlow0 search/4096 if\ __name__
26540427ccaSGordon Tetlow>&0 search/64 '__main__'	Python script text executable
26640427ccaSGordon Tetlow>&0 search/64 "__main__"	Python script text executable
26740427ccaSGordon Tetlow!:strength + 15
2682726a701SXin LI!:mime text/x-script.python
26940427ccaSGordon Tetlow
270b6cee71dSXin LI# comments
271b6cee71dSXin LI#0	search/4096	'''
272b6cee71dSXin LI#>&0	regex	.*'''$	Python script text executable
2732726a701SXin LI#!:mime text/x-script.python
274b6cee71dSXin LI
275b6cee71dSXin LI#0	search/4096	"""
276b6cee71dSXin LI#>&0	regex	.*"""$	Python script text executable
2772726a701SXin LI#!:mime text/x-script.python
278b6cee71dSXin LI
279b6cee71dSXin LI# try:
280b6cee71dSXin LI# except: or finally:
281b6cee71dSXin LI# block
282b6cee71dSXin LI0	search/4096	try:
28340427ccaSGordon Tetlow>&0	regex	\^[[:space:]]*except.*:$	Python script text executable
28440427ccaSGordon Tetlow!:strength + 15
2852726a701SXin LI!:mime text/x-script.python
286b6cee71dSXin LI>&0	search/4096	finally:	Python script text executable
2872726a701SXin LI!:mime text/x-script.python
288b6cee71dSXin LI
28940427ccaSGordon Tetlow# class name[(base classes,)]: [pass]
29048c779cdSXin LI0	search/8192	class
29148c779cdSXin LI>0	regex	\^class\ [_[:alpha:]]+(\\(.*\\))?(\ )*:([\ \t]+pass)?$		Python script text executable
29240427ccaSGordon Tetlow!:strength + 15
2932726a701SXin LI!:mime text/x-script.python
29440427ccaSGordon Tetlow
29540427ccaSGordon Tetlow# def name(*args, **kwargs):
29648c779cdSXin LI0	search/8192	def\
29748c779cdSXin LI>0	regex	 \^[[:space:]]{0,50}def\ {1,50}[_a-zA-Z]{1,100}
29848c779cdSXin LI>>&0	regex	 \\(([[:alpha:]*_,\ ]){0,255}\\):$ Python script text executable
29940427ccaSGordon Tetlow!:strength + 15
3002726a701SXin LI!:mime text/x-script.python
3012726a701SXin LI
3022726a701SXin LI# https://numpy.org/devdocs/reference/generated/numpy.lib.format.html
3032726a701SXin LI0	string	\223NUMPY		NumPy data file
3042726a701SXin LI!:mime  application/x-numpy-data
3052726a701SXin LI>6	byte	x			\b, version %d
3062726a701SXin LI>7	byte	x			\b.%d
3072726a701SXin LI#>8	leshort	x			\b, header length=%d
3082726a701SXin LI>10	string	x			\b, description %s
309