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-120834: Use PyGenObject for generators, coroutines and async generators #120976

Closed
wants to merge 1 commit into from

Conversation

iritkatriel
Copy link
Member

@iritkatriel iritkatriel commented Jun 24, 2024

@@ -35,7 +35,7 @@ PyAPI_FUNC(PyObject *) PyCoro_New(PyFrameObject *,

/* --- Asynchronous Generators -------------------------------------------- */

typedef struct _PyAsyncGenObject PyAsyncGenObject;
typedef PyGenObject PyAsyncGenObject;
Copy link
Member Author

Choose a reason for hiding this comment

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

PyCoroObject and PyAsyncGenObject are mentioned in the docs, so I think we need to leave this even though we're not using these names.

Copy link
Member

@markshannon markshannon left a comment

Choose a reason for hiding this comment

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

A few pedantic type suggestions, otherwise LGTM.

struct _PyCoroObject {
_PyGenObject_HEAD(cr)
/* The gi_ prefix is for generator-iterator. */
PyObject_HEAD \
Copy link
Member

Choose a reason for hiding this comment

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

We don't need the line extension \s any more.

@@ -1399,12 +1399,12 @@ PyErr_WarnExplicitFormat(PyObject *category,
}

void
_PyErr_WarnUnawaitedAgenMethod(PyAsyncGenObject *agen, PyObject *method)
_PyErr_WarnUnawaitedAgenMethod(PyGenObject *agen, PyObject *method)
Copy link
Member

Choose a reason for hiding this comment

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

This seems a less correct type. agen should be an async generator, so PyAsyncGenObject * seems the correct type.

Copy link
Member Author

Choose a reason for hiding this comment

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

Are we going to keep PyAsyncGenObject and PyCoroObject, now that they are just aliases?
It's not like using them in a signature will give compile-time type checks.

I was assuming we will want to stop using them altogether and deprecate them from the API.

@@ -1413,7 +1413,7 @@ _PyErr_WarnUnawaitedAgenMethod(PyAsyncGenObject *agen, PyObject *method)


void
_PyErr_WarnUnawaitedCoroutine(PyObject *coro)
_PyErr_WarnUnawaitedCoroutine(PyGenObject *coro)
Copy link
Member

Choose a reason for hiding this comment

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

This should only be called with a coroutine, so I think PyCoroObject * is correct type.
Maybe add an assert that Py_TYPE(coro) == &PyCoro_Type?

@@ -232,7 +232,7 @@ static inline void _Py_LeaveRecursiveCall(void) {

extern struct _PyInterpreterFrame* _PyEval_GetFrame(void);

PyAPI_FUNC(PyObject *)_Py_MakeCoro(PyFunctionObject *func);
PyAPI_FUNC(PyGenObject *)_Py_MakeCoro(PyFunctionObject *func);
Copy link
Member

Choose a reason for hiding this comment

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

We generally have a convention that there is a correspondence between the C type and the Python class.
e.g. if PyXXX_Check[Exact](obj) is true, then it is safe to cast (PyXXXObject *)obj and vice-versa.
This sort of breaks that for coroutines and async generators.

So, I think it best to leave this as PyObject *. Does that make sense to you?

@bedevere-app
Copy link

bedevere-app bot commented Jun 26, 2024

When you're done making the requested changes, leave the comment: I have made the requested changes; please review again.

@iritkatriel
Copy link
Member Author

We decided not to do this after all.

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.

2 participants