by Python 3.10, and 3.11 features are not available.
"""
import argparse
-import ast
import builtins
import collections
import contextlib
from typing import Dict, FrozenSet, TextIO, Tuple
import umarshal
-from generate_global_objects import get_identifiers_and_strings
+
+ROOT = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
verbose = False
-identifiers, strings = get_identifiers_and_strings()
# This must be kept in sync with opcode.py
RESUME = 151
self.hits, self.misses = 0, 0
self.finis: list[str] = []
self.inits: list[str] = []
+ self.identifiers, self.strings = self.get_identifiers_and_strings()
self.write('#include "Python.h"')
self.write('#include "internal/pycore_gc.h"')
self.write('#include "internal/pycore_code.h"')
self.write('#include "internal/pycore_long.h"')
self.write("")
+ def get_identifiers_and_strings(self) -> tuple[set[str], dict[str, str]]:
+ filename = os.path.join(ROOT, "Include", "internal", "pycore_global_strings.h")
+ with open(filename) as fp:
+ lines = fp.readlines()
+ identifiers: set[str] = set()
+ strings: dict[str, str] = {}
+ for line in lines:
+ if m := re.search(r"STRUCT_FOR_ID\((\w+)\)", line):
+ identifiers.add(m.group(1))
+ if m := re.search(r'STRUCT_FOR_STR\((\w+), "(.*?)"\)', line):
+ strings[m.group(2)] = m.group(1)
+ return identifiers, strings
+
@contextlib.contextmanager
def indent(self) -> None:
save_level = self.level
return f"& {name}.ob_base.ob_base"
def generate_unicode(self, name: str, s: str) -> str:
- if s in strings:
- return f"&_Py_STR({strings[s]})"
- if s in identifiers:
+ if s in self.strings:
+ return f"&_Py_STR({self.strings[s]})"
+ if s in self.identifiers:
return f"&_Py_ID({s})"
if len(s) == 1:
c = ord(s)
def decode_frozen_data(source: str) -> types.CodeType:
- lines = source.splitlines()
- while lines and re.match(FROZEN_DATA_LINE, lines[0]) is None:
- del lines[0]
- while lines and re.match(FROZEN_DATA_LINE, lines[-1]) is None:
- del lines[-1]
- values: Tuple[int, ...] = ast.literal_eval("".join(lines).strip())
+ values: list[int] = []
+ for line in source.splitlines():
+ if re.match(FROZEN_DATA_LINE, line):
+ values.extend([int(x) for x in line.split(",") if x.strip()])
data = bytes(values)
return umarshal.loads(data)