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

gh-115653: Add docs for the PyCode_GetFirstFree and correct return type for the PyCode_GetNumFree #115654

Merged
merged 4 commits into from
Feb 21, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Doc/c-api/code.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,14 @@ bound into a function.
Return true if *co* is a :ref:`code object <code-objects>`.
This function always succeeds.

.. c:function:: int PyCode_GetNumFree(PyCodeObject *co)
.. c:function:: Py_ssize_t PyCode_GetNumFree(PyCodeObject *co)
vstinner marked this conversation as resolved.
Show resolved Hide resolved

Return the number of free variables in *co*.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
Return the number of free variables in *co*.
Return the number of free variables in a code object.


.. c:function:: int PyCode_GetFirstFree(PyCodeObject *co)

Return the number of local + cell variables in *co*.
Copy link
Contributor

Choose a reason for hiding this comment

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

Document what the API does, not its implementation details: PyCode_GetFirstFree returns the position (or offset) of the first free variable in co.

FTR: @markshannon and @iritkatriel introduced this API in gh-100721. It is available via Python.h.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done. But my question above still stands. If I'm right I might make a separate pull request for this.

Copy link
Contributor

Choose a reason for hiding this comment

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

See the PR I linked to, especially #100721 (comment) and 49ca044.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you. If there is anything else here that I could fix, I'll be happy to do it.

Copy link
Contributor

Choose a reason for hiding this comment

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

I would still like to hear from either @markshannon or @iritkatriel; I'm not sure this was intended as a public API.

Copy link
Member

@markshannon markshannon Feb 21, 2024

Choose a reason for hiding this comment

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

It was and it wasn't. We don't really want to expose it, but if we don't then Cython and other C extensions will access the internal fields directly, which is worse.

Now that we have an unstable API, it should probably be part of that.
I'd like all code object C APIs to be unstable, but it's probably too late for that.

Maybe rename it to PyUnstableCode_GetFirstFree?

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe rename it to PyUnstableCode_GetFirstFree?

Sounds like a good idea, but since it is already included in 3.12, we have to deprecate PyCode_GetFirstFree and PyUnstableCode_GetFirstFree in 3.13.

Copy link
Contributor

Choose a reason for hiding this comment

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

Follow-up issue: #115756.


.. c:function:: PyCodeObject* PyUnstable_Code_New(int argcount, int kwonlyargcount, int nlocals, int stacksize, int flags, PyObject *code, PyObject *consts, PyObject *names, PyObject *varnames, PyObject *freevars, PyObject *cellvars, PyObject *filename, PyObject *name, PyObject *qualname, int firstlineno, PyObject *linetable, PyObject *exceptiontable)

Return a new code object. If you need a dummy code object to create a frame,
Expand Down
Loading