13459dd37SDavid E. O'Brien 23459dd37SDavid E. O'Brien#------------------------------------------------------------------------------ 3995a16e0SXin LI# $File: python,v 1.47 2024/08/27 18:50:57 christos Exp $ 43459dd37SDavid E. O'Brien# python: file(1) magic for python 53459dd37SDavid E. O'Brien# 6b60a4870SDavid E. O'Brien# Outlook puts """ too for urgent messages 73459dd37SDavid E. O'Brien# From: David Necas <yeti@physics.muni.cz> 875a6cdb3SDavid E. O'Brien# often the module starts with a multiline string 9b60a4870SDavid E. O'Brien0 string/t """ Python script text executable 10f210d1e9SXin LI# MAGIC as specified in Python/import.c (1.0 to 3.7) 11ba49e637SXin LI# and in Lib/importlib/_bootstrap_external.py (3.5+) 1246a30e2eSXin LI# two bytes of magic followed by "\r\n" in little endian order 13f210d1e9SXin LI0 belong 0x02099900 python 1.0 byte-compiled 14048bd409SXin LI!:mime application/x-bytecode.python 15f210d1e9SXin LI0 belong 0x03099900 python 1.1/1.2 byte-compiled 16048bd409SXin LI!:mime application/x-bytecode.python 17f210d1e9SXin LI0 belong 0x892e0d0a python 1.3 byte-compiled 18048bd409SXin LI!:mime application/x-bytecode.python 19f210d1e9SXin LI0 belong 0x04170d0a python 1.4 byte-compiled 20048bd409SXin LI!:mime application/x-bytecode.python 21f210d1e9SXin LI0 belong 0x994e0d0a python 1.5 byte-compiled 22048bd409SXin LI!:mime application/x-bytecode.python 23f210d1e9SXin LI0 belong 0xfcc40d0a python 1.6 byte-compiled 24048bd409SXin LI!:mime application/x-bytecode.python 25f210d1e9SXin LI0 belong 0xfdc40d0a python 1.6 byte-compiled 26048bd409SXin LI!:mime application/x-bytecode.python 273459dd37SDavid E. O'Brien0 belong 0x87c60d0a python 2.0 byte-compiled 28048bd409SXin LI!:mime application/x-bytecode.python 29f210d1e9SXin LI0 belong 0x88c60d0a python 2.0 byte-compiled 30048bd409SXin LI!:mime application/x-bytecode.python 313459dd37SDavid E. O'Brien0 belong 0x2aeb0d0a python 2.1 byte-compiled 32048bd409SXin LI!:mime application/x-bytecode.python 33f210d1e9SXin LI0 belong 0x2beb0d0a python 2.1 byte-compiled 34048bd409SXin LI!:mime application/x-bytecode.python 353459dd37SDavid E. O'Brien0 belong 0x2ded0d0a python 2.2 byte-compiled 36048bd409SXin LI!:mime application/x-bytecode.python 37f210d1e9SXin LI0 belong 0x2eed0d0a python 2.2 byte-compiled 38048bd409SXin LI!:mime application/x-bytecode.python 39ea8dc7e0SDavid E. O'Brien0 belong 0x3bf20d0a python 2.3 byte-compiled 40048bd409SXin LI!:mime application/x-bytecode.python 41f210d1e9SXin LI0 belong 0x3cf20d0a python 2.3 byte-compiled 42048bd409SXin LI!:mime application/x-bytecode.python 432d24dbcfSXin LI0 belong 0x45f20d0a python 2.3 byte-compiled 44048bd409SXin LI!:mime application/x-bytecode.python 452d24dbcfSXin LI0 belong 0x59f20d0a python 2.4 byte-compiled 46048bd409SXin LI!:mime application/x-bytecode.python 472d24dbcfSXin LI0 belong 0x63f20d0a python 2.4 byte-compiled 48048bd409SXin LI!:mime application/x-bytecode.python 4988a5e5acSDavid E. O'Brien0 belong 0x6df20d0a python 2.4 byte-compiled 50048bd409SXin LI!:mime application/x-bytecode.python 51f210d1e9SXin LI0 belong 0x6ef20d0a python 2.4 byte-compiled 52048bd409SXin LI!:mime application/x-bytecode.python 532d24dbcfSXin LI0 belong 0x77f20d0a python 2.5 byte-compiled 54048bd409SXin LI!:mime application/x-bytecode.python 552d24dbcfSXin LI0 belong 0x81f20d0a python 2.5 byte-compiled 56048bd409SXin LI!:mime application/x-bytecode.python 572d24dbcfSXin LI0 belong 0x8bf20d0a python 2.5 byte-compiled 58048bd409SXin LI!:mime application/x-bytecode.python 592d24dbcfSXin LI0 belong 0x8cf20d0a python 2.5 byte-compiled 60048bd409SXin LI!:mime application/x-bytecode.python 612d24dbcfSXin LI0 belong 0x95f20d0a python 2.5 byte-compiled 62048bd409SXin LI!:mime application/x-bytecode.python 632d24dbcfSXin LI0 belong 0x9ff20d0a python 2.5 byte-compiled 64048bd409SXin LI!:mime application/x-bytecode.python 652d24dbcfSXin LI0 belong 0xa9f20d0a python 2.5 byte-compiled 66048bd409SXin LI!:mime application/x-bytecode.python 67d1d50c78SDavid E. O'Brien0 belong 0xb3f20d0a python 2.5 byte-compiled 68048bd409SXin LI!:mime application/x-bytecode.python 69f210d1e9SXin LI0 belong 0xb4f20d0a python 2.5 byte-compiled 70048bd409SXin LI!:mime application/x-bytecode.python 712d24dbcfSXin LI0 belong 0xc7f20d0a python 2.6 byte-compiled 72048bd409SXin LI!:mime application/x-bytecode.python 73b873e82fSXin LI0 belong 0xd1f20d0a python 2.6 byte-compiled 74048bd409SXin LI!:mime application/x-bytecode.python 75f210d1e9SXin LI0 belong 0xd2f20d0a python 2.6 byte-compiled 76048bd409SXin LI!:mime application/x-bytecode.python 772d24dbcfSXin LI0 belong 0xdbf20d0a python 2.7 byte-compiled 78048bd409SXin LI!:mime application/x-bytecode.python 792d24dbcfSXin LI0 belong 0xe5f20d0a python 2.7 byte-compiled 80048bd409SXin LI!:mime application/x-bytecode.python 812d24dbcfSXin LI0 belong 0xeff20d0a python 2.7 byte-compiled 82048bd409SXin LI!:mime application/x-bytecode.python 832d24dbcfSXin LI0 belong 0xf9f20d0a python 2.7 byte-compiled 84048bd409SXin LI!:mime application/x-bytecode.python 85dacc26abSDavid E. O'Brien0 belong 0x03f30d0a python 2.7 byte-compiled 86048bd409SXin LI!:mime application/x-bytecode.python 87f210d1e9SXin LI0 belong 0x04f30d0a python 2.7 byte-compiled 88048bd409SXin LI!:mime application/x-bytecode.python 89ac338006SXin LI0 belong 0x0af30d0a PyPy2.7 byte-compiled 90ac338006SXin LI!:mime application/x-bytecode.python 912d24dbcfSXin LI0 belong 0xb80b0d0a python 3.0 byte-compiled 92048bd409SXin LI!:mime application/x-bytecode.python 932d24dbcfSXin LI0 belong 0xc20b0d0a python 3.0 byte-compiled 94048bd409SXin LI!:mime application/x-bytecode.python 952d24dbcfSXin LI0 belong 0xcc0b0d0a python 3.0 byte-compiled 96048bd409SXin LI!:mime application/x-bytecode.python 972d24dbcfSXin LI0 belong 0xd60b0d0a python 3.0 byte-compiled 98048bd409SXin LI!:mime application/x-bytecode.python 992d24dbcfSXin LI0 belong 0xe00b0d0a python 3.0 byte-compiled 100048bd409SXin LI!:mime application/x-bytecode.python 1012d24dbcfSXin LI0 belong 0xea0b0d0a python 3.0 byte-compiled 102048bd409SXin LI!:mime application/x-bytecode.python 1032d24dbcfSXin LI0 belong 0xf40b0d0a python 3.0 byte-compiled 104048bd409SXin LI!:mime application/x-bytecode.python 1052d24dbcfSXin LI0 belong 0xf50b0d0a python 3.0 byte-compiled 106048bd409SXin LI!:mime application/x-bytecode.python 1072d24dbcfSXin LI0 belong 0xff0b0d0a python 3.0 byte-compiled 108048bd409SXin LI!:mime application/x-bytecode.python 1092d24dbcfSXin LI0 belong 0x090c0d0a python 3.0 byte-compiled 110048bd409SXin LI!:mime application/x-bytecode.python 1112d24dbcfSXin LI0 belong 0x130c0d0a python 3.0 byte-compiled 112048bd409SXin LI!:mime application/x-bytecode.python 1132d24dbcfSXin LI0 belong 0x1d0c0d0a python 3.0 byte-compiled 114048bd409SXin LI!:mime application/x-bytecode.python 1152d24dbcfSXin LI0 belong 0x1f0c0d0a python 3.0 byte-compiled 116048bd409SXin LI!:mime application/x-bytecode.python 1172d24dbcfSXin LI0 belong 0x270c0d0a python 3.0 byte-compiled 118048bd409SXin LI!:mime application/x-bytecode.python 119dacc26abSDavid E. O'Brien0 belong 0x3b0c0d0a python 3.0 byte-compiled 120048bd409SXin LI!:mime application/x-bytecode.python 1212d24dbcfSXin LI0 belong 0x450c0d0a python 3.1 byte-compiled 122048bd409SXin LI!:mime application/x-bytecode.python 123dacc26abSDavid E. O'Brien0 belong 0x4f0c0d0a python 3.1 byte-compiled 124048bd409SXin LI!:mime application/x-bytecode.python 1252d24dbcfSXin LI0 belong 0x580c0d0a python 3.2 byte-compiled 126048bd409SXin LI!:mime application/x-bytecode.python 1272d24dbcfSXin LI0 belong 0x620c0d0a python 3.2 byte-compiled 128048bd409SXin LI!:mime application/x-bytecode.python 129dacc26abSDavid E. O'Brien0 belong 0x6c0c0d0a python 3.2 byte-compiled 130048bd409SXin LI!:mime application/x-bytecode.python 1312d24dbcfSXin LI0 belong 0x760c0d0a python 3.3 byte-compiled 132048bd409SXin LI!:mime application/x-bytecode.python 1332d24dbcfSXin LI0 belong 0x800c0d0a python 3.3 byte-compiled 134048bd409SXin LI!:mime application/x-bytecode.python 1352d24dbcfSXin LI0 belong 0x8a0c0d0a python 3.3 byte-compiled 136048bd409SXin LI!:mime application/x-bytecode.python 1372d24dbcfSXin LI0 belong 0x940c0d0a python 3.3 byte-compiled 138048bd409SXin LI!:mime application/x-bytecode.python 13977409849SXin LI0 belong 0x9e0c0d0a python 3.3 byte-compiled 140048bd409SXin LI!:mime application/x-bytecode.python 1412d24dbcfSXin LI0 belong 0xb20c0d0a python 3.4 byte-compiled 142048bd409SXin LI!:mime application/x-bytecode.python 1432d24dbcfSXin LI0 belong 0xbc0c0d0a python 3.4 byte-compiled 144048bd409SXin LI!:mime application/x-bytecode.python 1452d24dbcfSXin LI0 belong 0xc60c0d0a python 3.4 byte-compiled 146048bd409SXin LI!:mime application/x-bytecode.python 1472d24dbcfSXin LI0 belong 0xd00c0d0a python 3.4 byte-compiled 148048bd409SXin LI!:mime application/x-bytecode.python 1492d24dbcfSXin LI0 belong 0xda0c0d0a python 3.4 byte-compiled 150048bd409SXin LI!:mime application/x-bytecode.python 1512d24dbcfSXin LI0 belong 0xe40c0d0a python 3.4 byte-compiled 152048bd409SXin LI!:mime application/x-bytecode.python 153a12836acSXin LI0 belong 0xee0c0d0a python 3.4 byte-compiled 154048bd409SXin LI!:mime application/x-bytecode.python 1552d24dbcfSXin LI0 belong 0xf80c0d0a python 3.5.1- byte-compiled 156048bd409SXin LI!:mime application/x-bytecode.python 1572d24dbcfSXin LI0 belong 0x020d0d0a python 3.5.1- byte-compiled 158048bd409SXin LI!:mime application/x-bytecode.python 1592d24dbcfSXin LI0 belong 0x0c0d0d0a python 3.5.1- byte-compiled 160048bd409SXin LI!:mime application/x-bytecode.python 1612d24dbcfSXin LI0 belong 0x160d0d0a python 3.5.1- byte-compiled 162048bd409SXin LI!:mime application/x-bytecode.python 1632d24dbcfSXin LI0 belong 0x170d0d0a python 3.5.2+ byte-compiled 164048bd409SXin LI!:mime application/x-bytecode.python 1652d24dbcfSXin LI0 belong 0x200d0d0a python 3.6 byte-compiled 166048bd409SXin LI!:mime application/x-bytecode.python 1672d24dbcfSXin LI0 belong 0x210d0d0a python 3.6 byte-compiled 168048bd409SXin LI!:mime application/x-bytecode.python 1692d24dbcfSXin LI0 belong 0x2a0d0d0a python 3.6 byte-compiled 170048bd409SXin LI!:mime application/x-bytecode.python 1712d24dbcfSXin LI0 belong 0x2b0d0d0a python 3.6 byte-compiled 172048bd409SXin LI!:mime application/x-bytecode.python 1732d24dbcfSXin LI0 belong 0x2c0d0d0a python 3.6 byte-compiled 174048bd409SXin LI!:mime application/x-bytecode.python 1752d24dbcfSXin LI0 belong 0x2d0d0d0a python 3.6 byte-compiled 176048bd409SXin LI!:mime application/x-bytecode.python 1772d24dbcfSXin LI0 belong 0x2f0d0d0a python 3.6 byte-compiled 178048bd409SXin LI!:mime application/x-bytecode.python 1792d24dbcfSXin LI0 belong 0x300d0d0a python 3.6 byte-compiled 180048bd409SXin LI!:mime application/x-bytecode.python 1812d24dbcfSXin LI0 belong 0x310d0d0a python 3.6 byte-compiled 182048bd409SXin LI!:mime application/x-bytecode.python 1832d24dbcfSXin LI0 belong 0x320d0d0a python 3.6 byte-compiled 184048bd409SXin LI!:mime application/x-bytecode.python 185388b470cSGordon Tetlow0 belong 0x330d0d0a python 3.6 byte-compiled 186048bd409SXin LI!:mime application/x-bytecode.python 1872d24dbcfSXin LI0 belong 0x3e0d0d0a python 3.7 byte-compiled 188048bd409SXin LI!:mime application/x-bytecode.python 1892d24dbcfSXin LI0 belong 0x3f0d0d0a python 3.7 byte-compiled 190048bd409SXin LI!:mime application/x-bytecode.python 191ac338006SXin LI 192ac338006SXin LI# magic 3392+ implements PEP 552: Deterministic pycs 193ac338006SXin LI0 name pyc-pep552 194995a16e0SXin LI>0 uleshort x (magic: %04d), 195ac338006SXin LI# the flag field determines how .pyc validity is checked 196ac338006SXin LI>4 ulelong&1 0 timestamp-based, 197ac338006SXin LI>>8 uledate x .py timestamp: %s UTC, 198ac338006SXin LI>>12 ulelong x .py size: %d bytes 199ac338006SXin LI>4 ulelong&1 !0 hash-based, check-source flag 200ac338006SXin LI>>4 ulelong&2 0 unset, 201ac338006SXin LI>>4 ulelong&2 !0 set, 202ac338006SXin LI>>8 ulequad x hash: 0x%llx 203ac338006SXin LI 204ac338006SXin LI# uleshort magic followed by \x0d\0xa 205ac338006SXin LI2 string \x0d\x0a 206ac338006SXin LI# extra check: only two bits of flag field are currently used 207ac338006SXin LI>4 ulelong <0x4 208ac338006SXin LI# \x0d as part of magic should suffice till Python 3.14 (magic 3600) 209ac338006SXin LI>>1 ubyte 0x0d Byte-compiled Python module for 210048bd409SXin LI!:mime application/x-bytecode.python 211ac338006SXin LI# now look at the magic number to determine the version 212995a16e0SXin LI>>>0 uleshort <3400 CPython 3.7 213ac338006SXin LI>>>0 default x 214995a16e0SXin LI>>>>0 uleshort <3420 CPython 3.8 215ac338006SXin LI>>>>0 default x 216995a16e0SXin LI>>>>>0 uleshort <3430 CPython 3.9 217ac338006SXin LI>>>>>0 default x 218995a16e0SXin LI>>>>>>0 uleshort <3450 CPython 3.10 219ac338006SXin LI>>>>>>0 default x 220995a16e0SXin LI>>>>>>>0 uleshort <3500 CPython 3.11 221995a16e0SXin LI>>>>>>>0 default x 222995a16e0SXin LI>>>>>>>>0 uleshort <3550 CPython 3.12 223995a16e0SXin LI>>>>>>>>0 default x 224995a16e0SXin LI>>>>>>>>>0 uleshort <3600 CPython 3.13 225995a16e0SXin LI>>>>>>>>>0 default x CPython 3.14 or newer 226ac338006SXin LI>>>0 use pyc-pep552 227995a16e0SXin LI>>0 uleshort 240 Byte-compiled Python module for PyPy3.7 228048bd409SXin LI!:mime application/x-bytecode.python 229ac338006SXin LI>>>0 use pyc-pep552 230995a16e0SXin LI>>0 uleshort 256 Byte-compiled Python module for PyPy3.8 231048bd409SXin LI!:mime application/x-bytecode.python 232ac338006SXin LI>>>0 use pyc-pep552 233995a16e0SXin LI>>0 uleshort 336 Byte-compiled Python module for PyPy3.9 234048bd409SXin LI!:mime application/x-bytecode.python 235ac338006SXin LI>>>0 use pyc-pep552 236388b470cSGordon Tetlow 2372d24dbcfSXin LI0 search/1/w #!\040/usr/bin/python Python script text executable 238196a3809SXin LI!:strength + 15 2392d24dbcfSXin LI!:mime text/x-script.python 2402d24dbcfSXin LI0 search/1/w #!\040/usr/local/bin/python Python script text executable 241196a3809SXin LI!:strength + 15 2422d24dbcfSXin LI!:mime text/x-script.python 2432d24dbcfSXin LI0 search/10/w #!\040/usr/bin/env\040python Python script text executable 244196a3809SXin LI!:strength + 15 2452d24dbcfSXin LI!:mime text/x-script.python 246b873e82fSXin LI 24788a5e5acSDavid E. O'Brien 248dacc26abSDavid E. O'Brien# from module.submodule import func1, func2 2494ef4bf01SXin LI0 search/8192 import 2502d24dbcfSXin LI>0 regex \^from[\040\t]+([A-Za-z0-9_]|\\.)+[\040\t]+import.*$ Python script text executable 251388b470cSGordon Tetlow!:strength + 15 2522d24dbcfSXin LI!:mime text/x-script.python 253995a16e0SXin LI>0 regex \^import\ [_[:alpha:]]+\ as\ [[:alpha:]][[:space:]]*$ Python script text executable 254995a16e0SXin LI!:mime text/x-script.python 255995a16e0SXin LI 25688a5e5acSDavid E. O'Brien 257dacc26abSDavid E. O'Brien# def __init__ (self, ...): 258dacc26abSDavid E. O'Brien0 search/4096 def\ __init__ 259dacc26abSDavid E. O'Brien>&0 search/64 self Python script text executable 260388b470cSGordon Tetlow!:strength + 15 2612d24dbcfSXin LI!:mime text/x-script.python 262388b470cSGordon Tetlow 263388b470cSGordon Tetlow# if __name__ == "__main__": 264388b470cSGordon Tetlow0 search/4096 if\ __name__ 265388b470cSGordon Tetlow>&0 search/64 '__main__' Python script text executable 266388b470cSGordon Tetlow>&0 search/64 "__main__" Python script text executable 267388b470cSGordon Tetlow!:strength + 15 2682d24dbcfSXin LI!:mime text/x-script.python 269388b470cSGordon Tetlow 270dacc26abSDavid E. O'Brien# comments 27177409849SXin LI#0 search/4096 ''' 27277409849SXin LI#>&0 regex .*'''$ Python script text executable 2732d24dbcfSXin LI#!:mime text/x-script.python 274dacc26abSDavid E. O'Brien 27577409849SXin LI#0 search/4096 """ 27677409849SXin LI#>&0 regex .*"""$ Python script text executable 2772d24dbcfSXin LI#!:mime text/x-script.python 278dacc26abSDavid E. O'Brien 279dacc26abSDavid E. O'Brien# try: 280dacc26abSDavid E. O'Brien# except: or finally: 281dacc26abSDavid E. O'Brien# block 282dacc26abSDavid E. O'Brien0 search/4096 try: 283388b470cSGordon Tetlow>&0 regex \^[[:space:]]*except.*:$ Python script text executable 284388b470cSGordon Tetlow!:strength + 15 2852d24dbcfSXin LI!:mime text/x-script.python 286dacc26abSDavid E. O'Brien>&0 search/4096 finally: Python script text executable 2872d24dbcfSXin LI!:mime text/x-script.python 28877409849SXin LI 289388b470cSGordon Tetlow# class name[(base classes,)]: [pass] 2904ef4bf01SXin LI0 search/8192 class 2914ef4bf01SXin LI>0 regex \^class\ [_[:alpha:]]+(\\(.*\\))?(\ )*:([\ \t]+pass)?$ Python script text executable 292388b470cSGordon Tetlow!:strength + 15 2932d24dbcfSXin LI!:mime text/x-script.python 294388b470cSGordon Tetlow 295388b470cSGordon Tetlow# def name(*args, **kwargs): 2964ef4bf01SXin LI0 search/8192 def\ 2974ef4bf01SXin LI>0 regex \^[[:space:]]{0,50}def\ {1,50}[_a-zA-Z]{1,100} 2984ef4bf01SXin LI>>&0 regex \\(([[:alpha:]*_,\ ]){0,255}\\):$ Python script text executable 299388b470cSGordon Tetlow!:strength + 15 3002d24dbcfSXin LI!:mime text/x-script.python 3012d24dbcfSXin LI 3022d24dbcfSXin LI# https://numpy.org/devdocs/reference/generated/numpy.lib.format.html 3032d24dbcfSXin LI0 string \223NUMPY NumPy data file 3042d24dbcfSXin LI!:mime application/x-numpy-data 3052d24dbcfSXin LI>6 byte x \b, version %d 3062d24dbcfSXin LI>7 byte x \b.%d 3072d24dbcfSXin LI#>8 leshort x \b, header length=%d 3082d24dbcfSXin LI>10 string x \b, description %s 309