Skip to content

use Py_SET_TYPE for compat with python 3.10 #1131

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

Merged
merged 1 commit into from
Aug 24, 2020
Merged
Show file tree
Hide file tree
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
12 changes: 6 additions & 6 deletions psycopg/psycopgmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -918,7 +918,7 @@ add_module_types(PyObject *module)
for (i = 0; typetable[i].name; i++) {
PyObject *type = (PyObject *)typetable[i].type;

Py_TYPE(typetable[i].type) = &PyType_Type;
Py_SET_TYPE(typetable[i].type, &PyType_Type);
if (0 > PyType_Ready(typetable[i].type)) { return -1; }

Py_INCREF(type);
Expand Down Expand Up @@ -950,7 +950,7 @@ datetime_init(void)
if (0 > repl_curs_datetime_init()) { return -1; }
if (0 > replmsg_datetime_init()) { return -1; }

Py_TYPE(&pydatetimeType) = &PyType_Type;
Py_SET_TYPE(&pydatetimeType, &PyType_Type);
if (0 > PyType_Ready(&pydatetimeType)) { return -1; }

return 0;
Expand All @@ -962,7 +962,7 @@ mxdatetime_init(PyObject *module)
Dprintf("psycopgmodule: initializing mx.DateTime module");

#ifdef HAVE_MXDATETIME
Py_TYPE(&mxdatetimeType) = &PyType_Type;
Py_SET_TYPE(&mxdatetimeType, &PyType_Type);
if (0 > PyType_Ready(&mxdatetimeType)) { return -1; }

if (mxDateTime_ImportModuleAndAPI()) {
Expand Down Expand Up @@ -1082,13 +1082,13 @@ INIT_MODULE(_psycopg)(void)
libcrypto_threads_init();

/* initialize types and objects not exposed to the module */
Py_TYPE(&typecastType) = &PyType_Type;
Py_SET_TYPE(&typecastType, &PyType_Type);
if (0 > PyType_Ready(&typecastType)) { goto exit; }

Py_TYPE(&chunkType) = &PyType_Type;
Py_SET_TYPE(&chunkType, &PyType_Type);
if (0 > PyType_Ready(&chunkType)) { goto exit; }

Py_TYPE(&errorType) = &PyType_Type;
Py_SET_TYPE(&errorType, &PyType_Type);
errorType.tp_base = (PyTypeObject *)PyExc_StandardError;
if (0 > PyType_Ready(&errorType)) { goto exit; }

Expand Down
8 changes: 8 additions & 0 deletions psycopg/python.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ typedef long Py_hash_t;
typedef unsigned long Py_uhash_t;
#endif

/* Since Py_TYPE() is changed to the inline static function,
* Py_TYPE(obj) = new_type must be replaced with Py_SET_TYPE(obj, new_type)
* https://docs.python.org/3.10/whatsnew/3.10.html#id2
*/
#if PY_VERSION_HEX < 0x030900A4
#define Py_SET_TYPE(obj, type) ((Py_TYPE(obj) = (type)), (void)0)
Copy link
Member

Choose a reason for hiding this comment

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

Thank you very much for the contribution!

I wonder why is the (void)0 idiom preferred to the classic do { ... } while (0): I have seen it accepted but not discussed in the ticket.

Copy link
Contributor Author

@jouve jouve Jul 26, 2020

Choose a reason for hiding this comment

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

#endif

/* FORMAT_CODE_PY_SSIZE_T is for Py_ssize_t: */
#define FORMAT_CODE_PY_SSIZE_T "%" PY_FORMAT_SIZE_T "d"

Expand Down