Skip to content
Merged
Changes from all 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
49 changes: 6 additions & 43 deletions Modules/overlapped.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,18 +105,6 @@ typedef struct {
};
} OverlappedObject;

typedef struct {
PyTypeObject *overlapped_type;
} OverlappedState;

static inline OverlappedState*
overlapped_get_state(PyObject *module)
{
void *state = PyModule_GetState(module);
assert(state != NULL);
return (OverlappedState *)state;
}


static inline void
steal_buffer(Py_buffer * dst, Py_buffer * src)
Expand Down Expand Up @@ -1996,28 +1984,6 @@ static PyMethodDef overlapped_functions[] = {
{NULL}
};

static int
overlapped_traverse(PyObject *module, visitproc visit, void *arg)
{
OverlappedState *state = overlapped_get_state(module);
Py_VISIT(state->overlapped_type);
return 0;
}

static int
overlapped_clear(PyObject *module)
{
OverlappedState *state = overlapped_get_state(module);
Py_CLEAR(state->overlapped_type);
return 0;
}

static void
overlapped_free(void *module)
{
overlapped_clear((PyObject *)module);
}

#define WINAPI_CONSTANT(fmt, con) \
do { \
PyObject *value = Py_BuildValue(fmt, con); \
Expand Down Expand Up @@ -2045,14 +2011,15 @@ overlapped_exec(PyObject *module)
return -1;
}

OverlappedState *st = overlapped_get_state(module);
st->overlapped_type = (PyTypeObject *)PyType_FromModuleAndSpec(
PyTypeObject *overlapped_type = (PyTypeObject *)PyType_FromModuleAndSpec(
Copy link
Member

Choose a reason for hiding this comment

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

Is PyType_FromModuleAndSpec the correct API to retrieve a type object from a module by spec? It calls PyType_FromMetaclass, which does a lot of extra work.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

All the PyType_From* APIs call PyType_FromMetaclass. The recent changes to the PyType_From* APIs should not result in a lot of extra work. If that is the case, PyType_FromMetaclass should be optimised, if possible.

Copy link
Member

Choose a reason for hiding this comment

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

I was under the assumption that the PyType_From* APIs are used to create a new type object from a spec, not fetch an existing type. It may even leak memory . PyType_FromMetaclass calls PyMem_Malloc and eventually assigns it to type->tp_name eventually without a check.

Copy link
Member

Choose a reason for hiding this comment

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

Each call allocates a whole new type on the heap memory.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

IMO, if there are issues with PEP-489 and the PyType_From* APIs they should be addressed in a separate issue and discussed there. Suggesting to mark this conversation as resolved and continue this discussion in an appropriate issue if needed.

module, &overlapped_type_spec, NULL);
if (st->overlapped_type == NULL) {
if (overlapped_type == NULL) {
return -1;
}

if (PyModule_AddType(module, st->overlapped_type) < 0) {
int rc = PyModule_AddType(module, overlapped_type);
Py_DECREF(overlapped_type);
if (rc < 0) {
return -1;
}

Expand All @@ -2077,14 +2044,10 @@ static PyModuleDef_Slot overlapped_slots[] = {
};

static struct PyModuleDef overlapped_module = {
PyModuleDef_HEAD_INIT,
.m_base = PyModuleDef_HEAD_INIT,
.m_name = "_overlapped",
.m_size = sizeof(OverlappedState),
.m_methods = overlapped_functions,
.m_slots = overlapped_slots,
.m_traverse = overlapped_traverse,
.m_clear = overlapped_clear,
.m_free = overlapped_free
};

PyMODINIT_FUNC
Expand Down