Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preliminary PEP 573 (module state access) implementation #3

Closed
wants to merge 19 commits into from

Conversation

encukou
Copy link
Owner

@encukou encukou commented Apr 14, 2020

WIP PEP 573 implementation. Not yet ready to send as a PR to CPython.
@dormouse759 @vstinner, do you want to comment?

Look at the diff rather than individual commits. Lots of changes have been reverted/rebased. Marcel started the implementation in 2017.

There's still lots to do:

  • Make sure this matches all of the text in the PEP
    • Argument Clinic: Check the new argument doesn not appear in __text_signature__.
  • Try not subclassing PyCMethod from PyCFunction
  • Add documentation
  • Move tests to a better place
  • Add more tests?
  • Convert a stdlib module or two
    etc.
Not Found
@encukou encukou changed the title Preliminary PEP 573 implementation Preliminary PEP 573 (module state access) implementation Apr 14, 2020
@vstinner
Copy link

Convert a stdlib module or two

Maybe reintroducing _json assertions would be a good start? See https://bugs.python.org/issue40137

@encukou encukou changed the base branch from master to 3.6 April 21, 2020 16:36
@encukou encukou changed the base branch from 3.6 to master April 21, 2020 16:36
@encukou encukou changed the base branch from master to 3.6 April 21, 2020 16:36
@encukou encukou changed the base branch from 3.6 to master April 21, 2020 16:36
@encukou
Copy link
Owner Author

encukou commented Apr 21, 2020

I've rebased to make the commits show individual pieces, rather than history.
@vstinner, could you please check if this matches the newest C-API conventions?

encukou added 2 commits April 28, 2020 17:39
Also adds the signatures for PyCFunction*: these were not
summarized in the docs; instead and they were defined in prose
with references to simpler signatures. That is hard to read.
Instead, PyCFunction_Type is made variable, so it can hold PyCMethodObject
(or any future extensions) directly.
(This is similar to how PyType_Type handles the need for extra storage.)
Copy link

@vstinner vstinner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @encukou for working on that! It's a great addition to Python 3.9 and I will ensure that it lands before beta1 :-)

As promised, here is a first review. The overall change looks good, but I didn't dig into the details yet. My main concern is about the conversion to PyObject_VAR_HEAD.

((PyCMethodObject *)func) -> mm_class : NULL)

typedef struct {
PyObject_VAR_HEAD

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you convert PyCFunctionObject from PyObject_HEAD to PyObject_VAR_HEAD?

The code below suprised me:

        PyCMethodObject *om = PyObject_GC_NewVar(
            PyCMethodObject,
            &PyCFunction_Type,
            sizeof(PyCMethodObject) - sizeof(PyCFunctionObject));

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It needs space to store the new mm_class, i.e. be backed by PyCMethodObject rather than PyCFunctionObject.
What's the best way to do this?
Marcel's original work made PyCMethod a subclass of PyCFunction, which works -- but PyCFunction is not Py_TPFLAGS_BASETYPE so it can't be subclassed. It only worked because static types bypass the Py_TPFLAGS_BASETYPE check.

Maybe it would be best to add mm_class to PyCFunctionObject, i.e. to all built-in methods. But that isn't what the PEP says we'll do.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reverted and fixed up. PyCMethod_Type (builtin_method) now inherits from PyCFunction_Type (builtin_function_or_method), as in Marcel's branch.

Copy link

@vstinner vstinner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. I just left a few minor coding style remarks.

@encukou
Copy link
Owner Author

encukou commented May 5, 2020

CPython's C code style is still foreign to me :(

@encukou encukou closed this May 5, 2020
@encukou
Copy link
Owner Author

encukou commented May 5, 2020

This is now python#19936

encukou pushed a commit that referenced this pull request Apr 21, 2022
…python#91466)

Fix an uninitialized bool in exception print context.
    
`struct exception_print_context.need_close` was uninitialized.
    
Found by oss-fuzz in a test case running under the undefined behavior sanitizer.
    
https://oss-fuzz.com/testcase-detail/6217746058182656
    
```
Python/pythonrun.c:1241:28: runtime error: load of value 253, which is not a valid value for type 'bool'
    #0 0xbf2203 in print_chained cpython3/Python/pythonrun.c:1241:28
    #1 0xbea4bb in print_exception_cause_and_context cpython3/Python/pythonrun.c:1320:19
    #2 0xbea4bb in print_exception_recursive cpython3/Python/pythonrun.c:1470:13
    #3 0xbe9e39 in _PyErr_Display cpython3/Python/pythonrun.c:1517:9
```
    
Pretty obvious what the ommission was upon code inspection.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants