1b6cee71dSXin LI 2b6cee71dSXin LI#------------------------------------------------------------------------------ 3*a2dfb722SXin LI# $File: python,v 1.45 2022/07/24 23:59:37 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 89*a2dfb722SXin LI0 belong 0x0af30d0a PyPy2.7 byte-compiled 90*a2dfb722SXin 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 191*a2dfb722SXin LI 192*a2dfb722SXin LI# magic 3392+ implements PEP 552: Deterministic pycs 193*a2dfb722SXin LI0 name pyc-pep552 194*a2dfb722SXin LI# the flag field determines how .pyc validity is checked 195*a2dfb722SXin LI>4 ulelong&1 0 timestamp-based, 196*a2dfb722SXin LI>>8 uledate x .py timestamp: %s UTC, 197*a2dfb722SXin LI>>12 ulelong x .py size: %d bytes 198*a2dfb722SXin LI>4 ulelong&1 !0 hash-based, check-source flag 199*a2dfb722SXin LI>>4 ulelong&2 0 unset, 200*a2dfb722SXin LI>>4 ulelong&2 !0 set, 201*a2dfb722SXin LI>>8 ulequad x hash: 0x%llx 202*a2dfb722SXin LI 203*a2dfb722SXin LI# uleshort magic followed by \x0d\0xa 204*a2dfb722SXin LI2 string \x0d\x0a 205*a2dfb722SXin LI# extra check: only two bits of flag field are currently used 206*a2dfb722SXin LI>4 ulelong <0x4 207*a2dfb722SXin LI# \x0d as part of magic should suffice till Python 3.14 (magic 3600) 208*a2dfb722SXin LI>>1 ubyte 0x0d Byte-compiled Python module for 20943a5ec4eSXin LI!:mime application/x-bytecode.python 210*a2dfb722SXin LI# now look at the magic number to determine the version 211*a2dfb722SXin LI>>>0 uleshort <3400 CPython 3.7, 212*a2dfb722SXin LI>>>0 default x 213*a2dfb722SXin LI>>>>0 uleshort <3420 CPython 3.8, 214*a2dfb722SXin LI>>>>0 default x 215*a2dfb722SXin LI>>>>>0 uleshort <3430 CPython 3.9, 216*a2dfb722SXin LI>>>>>0 default x 217*a2dfb722SXin LI>>>>>>0 uleshort <3450 CPython 3.10, 218*a2dfb722SXin LI>>>>>>0 default x 219*a2dfb722SXin LI>>>>>>>0 uleshort <3500 CPython 3.11, 220*a2dfb722SXin LI>>>>>>>0 default x CPython 3.12 or newer, 221*a2dfb722SXin LI>>>0 use pyc-pep552 222*a2dfb722SXin LI>>0 uleshort 240 Byte-compiled Python module for PyPy3.7, 22343a5ec4eSXin LI!:mime application/x-bytecode.python 224*a2dfb722SXin LI>>>0 use pyc-pep552 225*a2dfb722SXin LI>>0 uleshort 256 Byte-compiled Python module for PyPy3.8, 22643a5ec4eSXin LI!:mime application/x-bytecode.python 227*a2dfb722SXin LI>>>0 use pyc-pep552 228*a2dfb722SXin LI>>0 uleshort 336 Byte-compiled Python module for PyPy3.9, 22943a5ec4eSXin LI!:mime application/x-bytecode.python 230*a2dfb722SXin LI>>>0 use pyc-pep552 23140427ccaSGordon Tetlow 2322726a701SXin LI0 search/1/w #!\040/usr/bin/python Python script text executable 2333e41d09dSXin LI!:strength + 15 2342726a701SXin LI!:mime text/x-script.python 2352726a701SXin LI0 search/1/w #!\040/usr/local/bin/python Python script text executable 2363e41d09dSXin LI!:strength + 15 2372726a701SXin LI!:mime text/x-script.python 2382726a701SXin LI0 search/10/w #!\040/usr/bin/env\040python Python script text executable 2393e41d09dSXin LI!:strength + 15 2402726a701SXin LI!:mime text/x-script.python 241b6cee71dSXin LI 242b6cee71dSXin LI 243b6cee71dSXin LI# from module.submodule import func1, func2 24448c779cdSXin LI0 search/8192 import 2452726a701SXin LI>0 regex \^from[\040\t]+([A-Za-z0-9_]|\\.)+[\040\t]+import.*$ Python script text executable 24640427ccaSGordon Tetlow!:strength + 15 2472726a701SXin LI!:mime text/x-script.python 248b6cee71dSXin LI 249b6cee71dSXin LI# def __init__ (self, ...): 250b6cee71dSXin LI0 search/4096 def\ __init__ 251b6cee71dSXin LI>&0 search/64 self Python script text executable 25240427ccaSGordon Tetlow!:strength + 15 2532726a701SXin LI!:mime text/x-script.python 25440427ccaSGordon Tetlow 25540427ccaSGordon Tetlow# if __name__ == "__main__": 25640427ccaSGordon Tetlow0 search/4096 if\ __name__ 25740427ccaSGordon Tetlow>&0 search/64 '__main__' Python script text executable 25840427ccaSGordon Tetlow>&0 search/64 "__main__" Python script text executable 25940427ccaSGordon Tetlow!:strength + 15 2602726a701SXin LI!:mime text/x-script.python 26140427ccaSGordon Tetlow 26240427ccaSGordon Tetlow# import module [as abrev] 26348c779cdSXin LI0 search/8192 import 26448c779cdSXin LI>0 regex \^import\ [_[:alpha:]]+\ as\ [[:alpha:]][[:space:]]*$ Python script text executable 2652726a701SXin LI!:mime text/x-script.python 266b6cee71dSXin LI 267b6cee71dSXin LI# comments 268b6cee71dSXin LI#0 search/4096 ''' 269b6cee71dSXin LI#>&0 regex .*'''$ Python script text executable 2702726a701SXin LI#!:mime text/x-script.python 271b6cee71dSXin LI 272b6cee71dSXin LI#0 search/4096 """ 273b6cee71dSXin LI#>&0 regex .*"""$ Python script text executable 2742726a701SXin LI#!:mime text/x-script.python 275b6cee71dSXin LI 276b6cee71dSXin LI# try: 277b6cee71dSXin LI# except: or finally: 278b6cee71dSXin LI# block 279b6cee71dSXin LI0 search/4096 try: 28040427ccaSGordon Tetlow>&0 regex \^[[:space:]]*except.*:$ Python script text executable 28140427ccaSGordon Tetlow!:strength + 15 2822726a701SXin LI!:mime text/x-script.python 283b6cee71dSXin LI>&0 search/4096 finally: Python script text executable 2842726a701SXin LI!:mime text/x-script.python 285b6cee71dSXin LI 28640427ccaSGordon Tetlow# class name[(base classes,)]: [pass] 28748c779cdSXin LI0 search/8192 class 28848c779cdSXin LI>0 regex \^class\ [_[:alpha:]]+(\\(.*\\))?(\ )*:([\ \t]+pass)?$ Python script text executable 28940427ccaSGordon Tetlow!:strength + 15 2902726a701SXin LI!:mime text/x-script.python 29140427ccaSGordon Tetlow 29240427ccaSGordon Tetlow# def name(*args, **kwargs): 29348c779cdSXin LI0 search/8192 def\ 29448c779cdSXin LI>0 regex \^[[:space:]]{0,50}def\ {1,50}[_a-zA-Z]{1,100} 29548c779cdSXin LI>>&0 regex \\(([[:alpha:]*_,\ ]){0,255}\\):$ Python script text executable 29640427ccaSGordon Tetlow!:strength + 15 2972726a701SXin LI!:mime text/x-script.python 2982726a701SXin LI 2992726a701SXin LI# https://numpy.org/devdocs/reference/generated/numpy.lib.format.html 3002726a701SXin LI0 string \223NUMPY NumPy data file 3012726a701SXin LI!:mime application/x-numpy-data 3022726a701SXin LI>6 byte x \b, version %d 3032726a701SXin LI>7 byte x \b.%d 3042726a701SXin LI#>8 leshort x \b, header length=%d 3052726a701SXin LI>10 string x \b, description %s 306