From 959339cefb3a7aa59f9e0c7ccf0450eaff3ef4ba Mon Sep 17 00:00:00 2001 From: Jacob Champion Date: Wed, 19 Jul 2023 13:36:48 -0700 Subject: [PATCH 1/2] Return NULL on failed module initialization Previously, any exceptions raised during initialization were swallowed with a message like SystemError: initialization of _psycopg raised unreported exception Fixes #1598. --- psycopg/psycopgmodule.c | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/psycopg/psycopgmodule.c b/psycopg/psycopgmodule.c index 0d284f8f..1490a92e 100644 --- a/psycopg/psycopgmodule.c +++ b/psycopg/psycopgmodule.c @@ -1001,32 +1001,35 @@ INIT_MODULE(_psycopg)(void) /* initialize types and objects not exposed to the module */ Py_SET_TYPE(&typecastType, &PyType_Type); - if (0 > PyType_Ready(&typecastType)) { goto exit; } + if (0 > PyType_Ready(&typecastType)) { goto error; } Py_SET_TYPE(&chunkType, &PyType_Type); - if (0 > PyType_Ready(&chunkType)) { goto exit; } + if (0 > PyType_Ready(&chunkType)) { goto error; } Py_SET_TYPE(&errorType, &PyType_Type); errorType.tp_base = (PyTypeObject *)PyExc_StandardError; - if (0 > PyType_Ready(&errorType)) { goto exit; } + if (0 > PyType_Ready(&errorType)) { goto error; } - if (!(psyco_null = Bytes_FromString("NULL"))) { goto exit; } + if (!(psyco_null = Bytes_FromString("NULL"))) { goto error; } /* initialize the module */ module = PyModule_Create(&psycopgmodule); - if (!module) { goto exit; } + if (!module) { goto error; } - if (0 > add_module_constants(module)) { goto exit; } - if (0 > add_module_types(module)) { goto exit; } - if (0 > datetime_init()) { goto exit; } - if (0 > encodings_init(module)) { goto exit; } - if (0 > typecast_init(module)) { goto exit; } - if (0 > adapters_init(module)) { goto exit; } - if (0 > basic_errors_init(module)) { goto exit; } - if (0 > sqlstate_errors_init(module)) { goto exit; } + if (0 > add_module_constants(module)) { goto error; } + if (0 > add_module_types(module)) { goto error; } + if (0 > datetime_init()) { goto error; } + if (0 > encodings_init(module)) { goto error; } + if (0 > typecast_init(module)) { goto error; } + if (0 > adapters_init(module)) { goto error; } + if (0 > basic_errors_init(module)) { goto error; } + if (0 > sqlstate_errors_init(module)) { goto error; } Dprintf("psycopgmodule: module initialization complete"); - -exit: return module; + +error: + if (module) + Py_DECREF(module); + return NULL; } From 14e06d8185add825b3787e2f6a5bc97c2654359f Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Fri, 4 Aug 2023 16:57:14 +0100 Subject: [PATCH 2/2] docs: mention module init errors fix in news file --- NEWS | 2 ++ 1 file changed, 2 insertions(+) diff --git a/NEWS b/NEWS index 841e051c..acace75a 100644 --- a/NEWS +++ b/NEWS @@ -4,6 +4,8 @@ Current release What's new in psycopg 2.9.7 (unreleased) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +- Fix propagation of exceptions raised during module initialization + (:ticket:`#1598`). - Fix building when pg_config returns an empty string (:ticket:`#1599`). - Wheel package compiled against OpenSSL 1.1.1v.