gh-90868: Adjust the Generated Objects (gh-99223)

We do the following:

* move the generated _PyUnicode_InitStaticStrings() to its own file
* move the generated _PyStaticObjects_CheckRefcnt() to its own file
* include pycore_global_objects.h in extension modules instead of pycore_runtime_init.h

These changes help us avoid including things that aren't needed.

https://github.com/python/cpython/issues/90868
This commit is contained in:
Eric Snow 2022-11-08 10:03:03 -07:00 committed by GitHub
parent d45cc80452
commit 52f91c642b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 7193 additions and 7098 deletions

View file

@ -330,7 +330,29 @@ def generate_runtime_init(identifiers, strings):
with printer.block('.tuple_empty =', ','):
printer.write('.ob_base = _PyVarObject_IMMORTAL_INIT(&PyTuple_Type, 0)')
immortal_objects.append(f'(PyObject *)&_Py_SINGLETON(tuple_empty)')
printer.write('')
printer.write(END)
printer.write(after)
return immortal_objects
def generate_static_strings_initializer(identifiers, strings):
# Target the runtime initializer.
filename = os.path.join(INTERNAL, 'pycore_unicodeobject_generated.h')
# Read the non-generated part of the file.
with open(filename) as infile:
orig = infile.read()
lines = iter(orig.rstrip().splitlines())
before = '\n'.join(iter_to_marker(lines, START))
for _ in iter_to_marker(lines, END):
pass
after = '\n'.join(lines)
# Generate the file.
with open_for_changes(filename, orig) as outfile:
printer = Printer(outfile)
printer.write(before)
printer.write(START)
printer.write("static inline void")
with printer.block("_PyUnicode_InitStaticStrings(void)"):
printer.write(f'PyObject *string;')
@ -339,7 +361,29 @@ def generate_runtime_init(identifiers, strings):
# since iter_files() ignores .h files.
printer.write(f'string = &_Py_ID({i});')
printer.write(f'PyUnicode_InternInPlace(&string);')
printer.write('')
# XXX What about "strings"?
printer.write(END)
printer.write(after)
def generate_global_object_finalizers(immortal_objects):
# Target the runtime initializer.
filename = os.path.join(INTERNAL, 'pycore_global_objects_fini_generated.h')
# Read the non-generated part of the file.
with open(filename) as infile:
orig = infile.read()
lines = iter(orig.rstrip().splitlines())
before = '\n'.join(iter_to_marker(lines, START))
for _ in iter_to_marker(lines, END):
pass
after = '\n'.join(lines)
# Generate the file.
with open_for_changes(filename, orig) as outfile:
printer = Printer(outfile)
printer.write(before)
printer.write(START)
printer.write('#ifdef Py_DEBUG')
printer.write("static inline void")
with printer.block("_PyStaticObjects_CheckRefcnt(void)"):
@ -375,7 +419,9 @@ def main() -> None:
identifiers, strings = get_identifiers_and_strings()
generate_global_strings(identifiers, strings)
generate_runtime_init(identifiers, strings)
immortal_objects = generate_runtime_init(identifiers, strings)
generate_static_strings_initializer(identifiers, strings)
generate_global_object_finalizers(immortal_objects)
if __name__ == '__main__':