Fix PyCode constructor/replace by youknowone · Pull Request #6193 · RustPython/RustPython
5-22: LGTM! Necessary imports for the new constructor.
The new imports support the Constructor implementation and bytecode parsing functionality. All imports are used appropriately in the code.
373-394: LGTM! Well-structured constructor arguments.
The PyCodeNewArgs struct properly captures all necessary parameters for code object construction with appropriate Rust types matching the Python code object structure.
401-456: LGTM! Thorough validation of constructor arguments.
The constructor properly validates and converts all tuple arguments to interned strings, and correctly validates that nlocals matches varnames.len(). Error handling is appropriate with clear error messages.
460-461: LGTM! Proper bytecode validation.
The constructor correctly validates bytecode before creating the code object, providing clear error messages for invalid bytecode.
523-523: LGTM! Proper exposure of constructor.
The #[pyclass] attribute is correctly updated to include Constructor, enabling the py_new path for code object creation.
556-558: LGTM! Correct derivation of nlocals.
The getter now correctly derives co_nlocals from varnames.len(), which aligns with the validation enforced in both the constructor and replace method.
839-923: LGTM! Clear and maintainable refactoring.
The destructuring and pattern matching for optional arguments makes the replace logic much more readable and maintainable. The validation logic is preserved correctly.
925-932: LGTM! Consistent bytecode validation.
The replace method correctly validates bytecode when co_code is provided, using the same parse_bytecode helper as the constructor for consistency.
950-959: LGTM! Proper nlocals validation in replace.
The validation ensures consistency between co_nlocals and co_varnames length, preventing invalid code object states.