Last active
January 7, 2022 21:38
-
-
Save delimitry/e281132916a2ba4c0418b4532cff4096 to your computer and use it in GitHub Desktop.
A script to get the current list of CPython version's magic numbers from CPython repository
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
""" | |
A script to get the current list of CPython version's magic numbers from CPython repository. | |
It is parses "/Lib/importlib/_bootstrap_external.py" source file to get this list. | |
And this is a helper for my script: https://gist.github.com/delimitry/bad5496b52161449f6de, | |
which allows to get the version of Python by which the file was compiled | |
""" | |
from __future__ import print_function | |
import re | |
import requests | |
from collections import OrderedDict | |
START_TAG = '# Known values:' | |
END_TAG = '# MAGIC must change whenever the bytecode emitted' | |
PYTHON_MAGIC_PATTERN = re.compile(r'(Python .*?):*\s+(\d+)') | |
ONLY_MAGIC_PATTERN = re.compile(r'\s+(\d+)\s') | |
BOOTSTRAP_EXTERNAL_PY_URL = 'https://raw.githubusercontent.com/python/cpython/master/Lib/importlib/_bootstrap_external.py' | |
def main(): | |
"""Get CPython version's magic numbers from official CPython repository""" | |
response = requests.get(BOOTSTRAP_EXTERNAL_PY_URL) | |
data = response.text | |
magics = [] | |
cur_ver, magic = '', '' | |
pos = data.find(START_TAG) | |
if pos < 0: | |
return | |
pos_end = data.find(END_TAG) | |
found_data = data[pos + len(START_TAG): pos_end] | |
for line in found_data.split('\n'): | |
uncomment_line = line[1:] if line.startswith('#') else line | |
res = PYTHON_MAGIC_PATTERN.findall(uncomment_line) | |
if res: | |
cur_ver, magic = res[0] | |
magics.append((magic, cur_ver,)) | |
else: | |
res = ONLY_MAGIC_PATTERN.findall(uncomment_line) | |
if res: | |
magic = res[0] | |
magics.append((magic, cur_ver,)) | |
# make an ordered dict to handle magic numbers' duplicates | |
magic_version = OrderedDict() | |
for item in magics: | |
magic, version = item | |
if magic not in magic_version: | |
magic_version[magic] = [version] | |
else: | |
magic_version[magic].append(version) | |
# write out magics | |
out = '' | |
out += 'MAGICS = {\n' | |
for magic, version in magic_version.items(): | |
out += ' {}: \'{}\',\n'.format(magic, ', '.join(sorted(set(version)))) | |
out += '}\n' | |
print(out) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment