From 442f300e9152aa6c57a50fc797dc0828b5fb979b Mon Sep 17 00:00:00 2001 From: Cyril Jouve Date: Sun, 26 Jul 2020 15:41:53 +0200 Subject: [PATCH] use Py_SET_TYPE for compat with python 3.10 --- psycopg/psycopgmodule.c | 12 ++++++------ psycopg/python.h | 8 ++++++++ 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/psycopg/psycopgmodule.c b/psycopg/psycopgmodule.c index 14e44369..36fd1f9c 100644 --- a/psycopg/psycopgmodule.c +++ b/psycopg/psycopgmodule.c @@ -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); @@ -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; @@ -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()) { @@ -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; } diff --git a/psycopg/python.h b/psycopg/python.h index 2a5f9d83..f7f53962 100644 --- a/psycopg/python.h +++ b/psycopg/python.h @@ -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) +#endif + /* FORMAT_CODE_PY_SSIZE_T is for Py_ssize_t: */ #define FORMAT_CODE_PY_SSIZE_T "%" PY_FORMAT_SIZE_T "d"