Skip to content

gh-140009: Replace PyTuple_Pack with PyTuple_FromArray in codeobject.c and _pickle.c#144829

Open
RoryGlenn wants to merge 1 commit intopython:mainfrom
RoryGlenn:gh-140009-pytuple-fromarray
Open

gh-140009: Replace PyTuple_Pack with PyTuple_FromArray in codeobject.c and _pickle.c#144829
RoryGlenn wants to merge 1 commit intopython:mainfrom
RoryGlenn:gh-140009-pytuple-fromarray

Conversation

@RoryGlenn
Copy link

gh-140009: Replace PyTuple_Pack with PyTuple_FromArray

Summary

This PR replaces PyTuple_Pack calls with PyTuple_FromArray using stack-allocated arrays in two files:

  • Objects/codeobject.c: 11 replacements in _PyCode_ConstantKey() — called for every constant in every code object during compilation (handles bool, bytes, float, complex, tuple, frozenset, slice, and fallback branches)
  • Modules/_pickle.c: 3 replacements in fix_imports(), save_global(), and find_class() — dictionary key construction for protocol compatibility mappings

Motivation

PyTuple_FromArray avoids the variadic argument overhead of PyTuple_Pack by taking a C array directly. This follows the pattern established in PR #140010 (merged for Objects/listobject.c).

Pattern

// Before
key = PyTuple_Pack(2, type, op);

// After
PyObject *items[] = {type, op};
key = PyTuple_FromArray(items, 2);

Note: Py_TYPE(op) returns PyTypeObject *, so an explicit (PyObject *) cast is needed in the array initializers for codeobject.c.

Testing

  • All compilation-related tests pass: test_code, test_compile, test_peepholer, test_symtable, test_codecs, test_marshal (743 tests)
  • All pickle tests pass: test_pickle, test_pickletools (1,157 tests)
  • Build completes with 0 warnings/errors
"

@python-cla-bot
Copy link

The following commit authors need to sign the Contributor License Agreement:

CLA not signed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant