11b108b8bSchristos 21b108b8bSchristos#------------------------------------------------------------------------------ 3*a77ebd86Schristos# $File: python,v 1.45 2022/07/24 23:59:37 christos Exp $ 41b108b8bSchristos# python: file(1) magic for python 51b108b8bSchristos# 6f50962bcSchristos# Outlook puts """ too for urgent messages 71b108b8bSchristos# From: David Necas <yeti@physics.muni.cz> 81b108b8bSchristos# often the module starts with a multiline string 9f50962bcSchristos0 string/t """ Python script text executable 10eff51ed2Schristos# MAGIC as specified in Python/import.c (1.0 to 3.7) 11*a77ebd86Schristos# and in Lib/importlib/_bootstrap_external.py (3.5+) 12ae9cfef6Schristos# two bytes of magic followed by "\r\n" in little endian order 13eff51ed2Schristos0 belong 0x02099900 python 1.0 byte-compiled 14*a77ebd86Schristos!:mime application/x-bytecode.python 15eff51ed2Schristos0 belong 0x03099900 python 1.1/1.2 byte-compiled 16*a77ebd86Schristos!:mime application/x-bytecode.python 17eff51ed2Schristos0 belong 0x892e0d0a python 1.3 byte-compiled 18*a77ebd86Schristos!:mime application/x-bytecode.python 19eff51ed2Schristos0 belong 0x04170d0a python 1.4 byte-compiled 20*a77ebd86Schristos!:mime application/x-bytecode.python 21eff51ed2Schristos0 belong 0x994e0d0a python 1.5 byte-compiled 22*a77ebd86Schristos!:mime application/x-bytecode.python 23eff51ed2Schristos0 belong 0xfcc40d0a python 1.6 byte-compiled 24*a77ebd86Schristos!:mime application/x-bytecode.python 25eff51ed2Schristos0 belong 0xfdc40d0a python 1.6 byte-compiled 26*a77ebd86Schristos!:mime application/x-bytecode.python 271b108b8bSchristos0 belong 0x87c60d0a python 2.0 byte-compiled 28*a77ebd86Schristos!:mime application/x-bytecode.python 29eff51ed2Schristos0 belong 0x88c60d0a python 2.0 byte-compiled 30*a77ebd86Schristos!:mime application/x-bytecode.python 311b108b8bSchristos0 belong 0x2aeb0d0a python 2.1 byte-compiled 32*a77ebd86Schristos!:mime application/x-bytecode.python 33eff51ed2Schristos0 belong 0x2beb0d0a python 2.1 byte-compiled 34*a77ebd86Schristos!:mime application/x-bytecode.python 351b108b8bSchristos0 belong 0x2ded0d0a python 2.2 byte-compiled 36*a77ebd86Schristos!:mime application/x-bytecode.python 37eff51ed2Schristos0 belong 0x2eed0d0a python 2.2 byte-compiled 38*a77ebd86Schristos!:mime application/x-bytecode.python 391b108b8bSchristos0 belong 0x3bf20d0a python 2.3 byte-compiled 40*a77ebd86Schristos!:mime application/x-bytecode.python 41eff51ed2Schristos0 belong 0x3cf20d0a python 2.3 byte-compiled 42*a77ebd86Schristos!:mime application/x-bytecode.python 4303c288bbSchristos0 belong 0x45f20d0a python 2.3 byte-compiled 44*a77ebd86Schristos!:mime application/x-bytecode.python 4503c288bbSchristos0 belong 0x59f20d0a python 2.4 byte-compiled 46*a77ebd86Schristos!:mime application/x-bytecode.python 4703c288bbSchristos0 belong 0x63f20d0a python 2.4 byte-compiled 48*a77ebd86Schristos!:mime application/x-bytecode.python 491b108b8bSchristos0 belong 0x6df20d0a python 2.4 byte-compiled 50*a77ebd86Schristos!:mime application/x-bytecode.python 51eff51ed2Schristos0 belong 0x6ef20d0a python 2.4 byte-compiled 52*a77ebd86Schristos!:mime application/x-bytecode.python 5303c288bbSchristos0 belong 0x77f20d0a python 2.5 byte-compiled 54*a77ebd86Schristos!:mime application/x-bytecode.python 5503c288bbSchristos0 belong 0x81f20d0a python 2.5 byte-compiled 56*a77ebd86Schristos!:mime application/x-bytecode.python 5703c288bbSchristos0 belong 0x8bf20d0a python 2.5 byte-compiled 58*a77ebd86Schristos!:mime application/x-bytecode.python 5903c288bbSchristos0 belong 0x8cf20d0a python 2.5 byte-compiled 60*a77ebd86Schristos!:mime application/x-bytecode.python 6103c288bbSchristos0 belong 0x95f20d0a python 2.5 byte-compiled 62*a77ebd86Schristos!:mime application/x-bytecode.python 6303c288bbSchristos0 belong 0x9ff20d0a python 2.5 byte-compiled 64*a77ebd86Schristos!:mime application/x-bytecode.python 6503c288bbSchristos0 belong 0xa9f20d0a python 2.5 byte-compiled 66*a77ebd86Schristos!:mime application/x-bytecode.python 671b108b8bSchristos0 belong 0xb3f20d0a python 2.5 byte-compiled 68*a77ebd86Schristos!:mime application/x-bytecode.python 69eff51ed2Schristos0 belong 0xb4f20d0a python 2.5 byte-compiled 70*a77ebd86Schristos!:mime application/x-bytecode.python 7103c288bbSchristos0 belong 0xc7f20d0a python 2.6 byte-compiled 72*a77ebd86Schristos!:mime application/x-bytecode.python 731b108b8bSchristos0 belong 0xd1f20d0a python 2.6 byte-compiled 74*a77ebd86Schristos!:mime application/x-bytecode.python 75eff51ed2Schristos0 belong 0xd2f20d0a python 2.6 byte-compiled 76*a77ebd86Schristos!:mime application/x-bytecode.python 7703c288bbSchristos0 belong 0xdbf20d0a python 2.7 byte-compiled 78*a77ebd86Schristos!:mime application/x-bytecode.python 7903c288bbSchristos0 belong 0xe5f20d0a python 2.7 byte-compiled 80*a77ebd86Schristos!:mime application/x-bytecode.python 8103c288bbSchristos0 belong 0xeff20d0a python 2.7 byte-compiled 82*a77ebd86Schristos!:mime application/x-bytecode.python 8303c288bbSchristos0 belong 0xf9f20d0a python 2.7 byte-compiled 84*a77ebd86Schristos!:mime application/x-bytecode.python 855ccaa8c0Schristos0 belong 0x03f30d0a python 2.7 byte-compiled 86*a77ebd86Schristos!:mime application/x-bytecode.python 87eff51ed2Schristos0 belong 0x04f30d0a python 2.7 byte-compiled 88*a77ebd86Schristos!:mime application/x-bytecode.python 89*a77ebd86Schristos0 belong 0x0af30d0a PyPy2.7 byte-compiled 90*a77ebd86Schristos!:mime application/x-bytecode.python 9103c288bbSchristos0 belong 0xb80b0d0a python 3.0 byte-compiled 92*a77ebd86Schristos!:mime application/x-bytecode.python 9303c288bbSchristos0 belong 0xc20b0d0a python 3.0 byte-compiled 94*a77ebd86Schristos!:mime application/x-bytecode.python 9503c288bbSchristos0 belong 0xcc0b0d0a python 3.0 byte-compiled 96*a77ebd86Schristos!:mime application/x-bytecode.python 9703c288bbSchristos0 belong 0xd60b0d0a python 3.0 byte-compiled 98*a77ebd86Schristos!:mime application/x-bytecode.python 9903c288bbSchristos0 belong 0xe00b0d0a python 3.0 byte-compiled 100*a77ebd86Schristos!:mime application/x-bytecode.python 10103c288bbSchristos0 belong 0xea0b0d0a python 3.0 byte-compiled 102*a77ebd86Schristos!:mime application/x-bytecode.python 10303c288bbSchristos0 belong 0xf40b0d0a python 3.0 byte-compiled 104*a77ebd86Schristos!:mime application/x-bytecode.python 10503c288bbSchristos0 belong 0xf50b0d0a python 3.0 byte-compiled 106*a77ebd86Schristos!:mime application/x-bytecode.python 10703c288bbSchristos0 belong 0xff0b0d0a python 3.0 byte-compiled 108*a77ebd86Schristos!:mime application/x-bytecode.python 10903c288bbSchristos0 belong 0x090c0d0a python 3.0 byte-compiled 110*a77ebd86Schristos!:mime application/x-bytecode.python 11103c288bbSchristos0 belong 0x130c0d0a python 3.0 byte-compiled 112*a77ebd86Schristos!:mime application/x-bytecode.python 11303c288bbSchristos0 belong 0x1d0c0d0a python 3.0 byte-compiled 114*a77ebd86Schristos!:mime application/x-bytecode.python 11503c288bbSchristos0 belong 0x1f0c0d0a python 3.0 byte-compiled 116*a77ebd86Schristos!:mime application/x-bytecode.python 11703c288bbSchristos0 belong 0x270c0d0a python 3.0 byte-compiled 118*a77ebd86Schristos!:mime application/x-bytecode.python 1195ccaa8c0Schristos0 belong 0x3b0c0d0a python 3.0 byte-compiled 120*a77ebd86Schristos!:mime application/x-bytecode.python 12103c288bbSchristos0 belong 0x450c0d0a python 3.1 byte-compiled 122*a77ebd86Schristos!:mime application/x-bytecode.python 1235ccaa8c0Schristos0 belong 0x4f0c0d0a python 3.1 byte-compiled 124*a77ebd86Schristos!:mime application/x-bytecode.python 12503c288bbSchristos0 belong 0x580c0d0a python 3.2 byte-compiled 126*a77ebd86Schristos!:mime application/x-bytecode.python 12703c288bbSchristos0 belong 0x620c0d0a python 3.2 byte-compiled 128*a77ebd86Schristos!:mime application/x-bytecode.python 1295ccaa8c0Schristos0 belong 0x6c0c0d0a python 3.2 byte-compiled 130*a77ebd86Schristos!:mime application/x-bytecode.python 13103c288bbSchristos0 belong 0x760c0d0a python 3.3 byte-compiled 132*a77ebd86Schristos!:mime application/x-bytecode.python 13303c288bbSchristos0 belong 0x800c0d0a python 3.3 byte-compiled 134*a77ebd86Schristos!:mime application/x-bytecode.python 13503c288bbSchristos0 belong 0x8a0c0d0a python 3.3 byte-compiled 136*a77ebd86Schristos!:mime application/x-bytecode.python 13703c288bbSchristos0 belong 0x940c0d0a python 3.3 byte-compiled 138*a77ebd86Schristos!:mime application/x-bytecode.python 13952df37c7Schristos0 belong 0x9e0c0d0a python 3.3 byte-compiled 140*a77ebd86Schristos!:mime application/x-bytecode.python 14103c288bbSchristos0 belong 0xb20c0d0a python 3.4 byte-compiled 142*a77ebd86Schristos!:mime application/x-bytecode.python 14303c288bbSchristos0 belong 0xbc0c0d0a python 3.4 byte-compiled 144*a77ebd86Schristos!:mime application/x-bytecode.python 14503c288bbSchristos0 belong 0xc60c0d0a python 3.4 byte-compiled 146*a77ebd86Schristos!:mime application/x-bytecode.python 14703c288bbSchristos0 belong 0xd00c0d0a python 3.4 byte-compiled 148*a77ebd86Schristos!:mime application/x-bytecode.python 14903c288bbSchristos0 belong 0xda0c0d0a python 3.4 byte-compiled 150*a77ebd86Schristos!:mime application/x-bytecode.python 15103c288bbSchristos0 belong 0xe40c0d0a python 3.4 byte-compiled 152*a77ebd86Schristos!:mime application/x-bytecode.python 153ebfd1121Schristos0 belong 0xee0c0d0a python 3.4 byte-compiled 154*a77ebd86Schristos!:mime application/x-bytecode.python 15503c288bbSchristos0 belong 0xf80c0d0a python 3.5.1- byte-compiled 156*a77ebd86Schristos!:mime application/x-bytecode.python 15703c288bbSchristos0 belong 0x020d0d0a python 3.5.1- byte-compiled 158*a77ebd86Schristos!:mime application/x-bytecode.python 15903c288bbSchristos0 belong 0x0c0d0d0a python 3.5.1- byte-compiled 160*a77ebd86Schristos!:mime application/x-bytecode.python 16103c288bbSchristos0 belong 0x160d0d0a python 3.5.1- byte-compiled 162*a77ebd86Schristos!:mime application/x-bytecode.python 16303c288bbSchristos0 belong 0x170d0d0a python 3.5.2+ byte-compiled 164*a77ebd86Schristos!:mime application/x-bytecode.python 16503c288bbSchristos0 belong 0x200d0d0a python 3.6 byte-compiled 166*a77ebd86Schristos!:mime application/x-bytecode.python 16703c288bbSchristos0 belong 0x210d0d0a python 3.6 byte-compiled 168*a77ebd86Schristos!:mime application/x-bytecode.python 16903c288bbSchristos0 belong 0x2a0d0d0a python 3.6 byte-compiled 170*a77ebd86Schristos!:mime application/x-bytecode.python 17103c288bbSchristos0 belong 0x2b0d0d0a python 3.6 byte-compiled 172*a77ebd86Schristos!:mime application/x-bytecode.python 17303c288bbSchristos0 belong 0x2c0d0d0a python 3.6 byte-compiled 174*a77ebd86Schristos!:mime application/x-bytecode.python 17503c288bbSchristos0 belong 0x2d0d0d0a python 3.6 byte-compiled 176*a77ebd86Schristos!:mime application/x-bytecode.python 17703c288bbSchristos0 belong 0x2f0d0d0a python 3.6 byte-compiled 178*a77ebd86Schristos!:mime application/x-bytecode.python 17903c288bbSchristos0 belong 0x300d0d0a python 3.6 byte-compiled 180*a77ebd86Schristos!:mime application/x-bytecode.python 18103c288bbSchristos0 belong 0x310d0d0a python 3.6 byte-compiled 182*a77ebd86Schristos!:mime application/x-bytecode.python 18303c288bbSchristos0 belong 0x320d0d0a python 3.6 byte-compiled 184*a77ebd86Schristos!:mime application/x-bytecode.python 18541da50d8Schristos0 belong 0x330d0d0a python 3.6 byte-compiled 186*a77ebd86Schristos!:mime application/x-bytecode.python 18703c288bbSchristos0 belong 0x3e0d0d0a python 3.7 byte-compiled 188*a77ebd86Schristos!:mime application/x-bytecode.python 18903c288bbSchristos0 belong 0x3f0d0d0a python 3.7 byte-compiled 190*a77ebd86Schristos!:mime application/x-bytecode.python 191*a77ebd86Schristos 192*a77ebd86Schristos# magic 3392+ implements PEP 552: Deterministic pycs 193*a77ebd86Schristos0 name pyc-pep552 194*a77ebd86Schristos# the flag field determines how .pyc validity is checked 195*a77ebd86Schristos>4 ulelong&1 0 timestamp-based, 196*a77ebd86Schristos>>8 uledate x .py timestamp: %s UTC, 197*a77ebd86Schristos>>12 ulelong x .py size: %d bytes 198*a77ebd86Schristos>4 ulelong&1 !0 hash-based, check-source flag 199*a77ebd86Schristos>>4 ulelong&2 0 unset, 200*a77ebd86Schristos>>4 ulelong&2 !0 set, 201*a77ebd86Schristos>>8 ulequad x hash: 0x%llx 202*a77ebd86Schristos 203*a77ebd86Schristos# uleshort magic followed by \x0d\0xa 204*a77ebd86Schristos2 string \x0d\x0a 205*a77ebd86Schristos# extra check: only two bits of flag field are currently used 206*a77ebd86Schristos>4 ulelong <0x4 207*a77ebd86Schristos# \x0d as part of magic should suffice till Python 3.14 (magic 3600) 208*a77ebd86Schristos>>1 ubyte 0x0d Byte-compiled Python module for 209*a77ebd86Schristos!:mime application/x-bytecode.python 210*a77ebd86Schristos# now look at the magic number to determine the version 211*a77ebd86Schristos>>>0 uleshort <3400 CPython 3.7, 212*a77ebd86Schristos>>>0 default x 213*a77ebd86Schristos>>>>0 uleshort <3420 CPython 3.8, 214*a77ebd86Schristos>>>>0 default x 215*a77ebd86Schristos>>>>>0 uleshort <3430 CPython 3.9, 216*a77ebd86Schristos>>>>>0 default x 217*a77ebd86Schristos>>>>>>0 uleshort <3450 CPython 3.10, 218*a77ebd86Schristos>>>>>>0 default x 219*a77ebd86Schristos>>>>>>>0 uleshort <3500 CPython 3.11, 220*a77ebd86Schristos>>>>>>>0 default x CPython 3.12 or newer, 221*a77ebd86Schristos>>>0 use pyc-pep552 222*a77ebd86Schristos>>0 uleshort 240 Byte-compiled Python module for PyPy3.7, 223*a77ebd86Schristos!:mime application/x-bytecode.python 224*a77ebd86Schristos>>>0 use pyc-pep552 225*a77ebd86Schristos>>0 uleshort 256 Byte-compiled Python module for PyPy3.8, 226*a77ebd86Schristos!:mime application/x-bytecode.python 227*a77ebd86Schristos>>>0 use pyc-pep552 228*a77ebd86Schristos>>0 uleshort 336 Byte-compiled Python module for PyPy3.9, 229*a77ebd86Schristos!:mime application/x-bytecode.python 230*a77ebd86Schristos>>>0 use pyc-pep552 23141da50d8Schristos 23203c288bbSchristos0 search/1/w #!\040/usr/bin/python Python script text executable 233ae9cfef6Schristos!:strength + 15 23403c288bbSchristos!:mime text/x-script.python 23503c288bbSchristos0 search/1/w #!\040/usr/local/bin/python Python script text executable 236ae9cfef6Schristos!:strength + 15 23703c288bbSchristos!:mime text/x-script.python 23803c288bbSchristos0 search/10/w #!\040/usr/bin/env\040python Python script text executable 239ae9cfef6Schristos!:strength + 15 24003c288bbSchristos!:mime text/x-script.python 2411b108b8bSchristos 2421b108b8bSchristos 2435ccaa8c0Schristos# from module.submodule import func1, func2 244dc958920Schristos0 search/8192 import 24503c288bbSchristos>0 regex \^from[\040\t]+([A-Za-z0-9_]|\\.)+[\040\t]+import.*$ Python script text executable 246008b33e8Schristos!:strength + 15 24703c288bbSchristos!:mime text/x-script.python 2481b108b8bSchristos 2495ccaa8c0Schristos# def __init__ (self, ...): 2505ccaa8c0Schristos0 search/4096 def\ __init__ 2515ccaa8c0Schristos>&0 search/64 self Python script text executable 252008b33e8Schristos!:strength + 15 25303c288bbSchristos!:mime text/x-script.python 2545ccaa8c0Schristos 25541da50d8Schristos# if __name__ == "__main__": 25641da50d8Schristos0 search/4096 if\ __name__ 25741da50d8Schristos>&0 search/64 '__main__' Python script text executable 25841da50d8Schristos>&0 search/64 "__main__" Python script text executable 25941da50d8Schristos!:strength + 15 26003c288bbSchristos!:mime text/x-script.python 26141da50d8Schristos 26241da50d8Schristos# import module [as abrev] 263dc958920Schristos0 search/8192 import 264dc958920Schristos>0 regex \^import\ [_[:alpha:]]+\ as\ [[:alpha:]][[:space:]]*$ Python script text executable 26503c288bbSchristos!:mime text/x-script.python 26641da50d8Schristos 2675ccaa8c0Schristos# comments 268012fd133Schristos#0 search/4096 ''' 269012fd133Schristos#>&0 regex .*'''$ Python script text executable 27003c288bbSchristos#!:mime text/x-script.python 2715ccaa8c0Schristos 272012fd133Schristos#0 search/4096 """ 273012fd133Schristos#>&0 regex .*"""$ Python script text executable 27403c288bbSchristos#!:mime text/x-script.python 2755ccaa8c0Schristos 2765ccaa8c0Schristos# try: 2775ccaa8c0Schristos# except: or finally: 2785ccaa8c0Schristos# block 2795ccaa8c0Schristos0 search/4096 try: 28041da50d8Schristos>&0 regex \^[[:space:]]*except.*:$ Python script text executable 281008b33e8Schristos!:strength + 15 28203c288bbSchristos!:mime text/x-script.python 2835ccaa8c0Schristos>&0 search/4096 finally: Python script text executable 28403c288bbSchristos!:mime text/x-script.python 2852ae0d66fSchristos 28641da50d8Schristos# class name[(base classes,)]: [pass] 287dc958920Schristos0 search/8192 class 288dc958920Schristos>0 regex \^class\ [_[:alpha:]]+(\\(.*\\))?(\ )*:([\ \t]+pass)?$ Python script text executable 289008b33e8Schristos!:strength + 15 29003c288bbSchristos!:mime text/x-script.python 29141da50d8Schristos 29241da50d8Schristos# def name(*args, **kwargs): 293dc958920Schristos0 search/8192 def\ 294dc958920Schristos>0 regex \^[[:space:]]{0,50}def\ {1,50}[_a-zA-Z]{1,100} 295dc958920Schristos>>&0 regex \\(([[:alpha:]*_,\ ]){0,255}\\):$ Python script text executable 29641da50d8Schristos!:strength + 15 29703c288bbSchristos!:mime text/x-script.python 29803c288bbSchristos 29903c288bbSchristos# https://numpy.org/devdocs/reference/generated/numpy.lib.format.html 30003c288bbSchristos0 string \223NUMPY NumPy data file 30103c288bbSchristos!:mime application/x-numpy-data 30203c288bbSchristos>6 byte x \b, version %d 30303c288bbSchristos>7 byte x \b.%d 30403c288bbSchristos#>8 leshort x \b, header length=%d 30503c288bbSchristos>10 string x \b, description %s 306