From 549beeea84a5133a3059619d0fb7fe3abdc6b410 Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Mon, 21 Jan 2019 18:18:41 +0000 Subject: [PATCH] Module constants definition moved in a separate function for clarity --- psycopg/psycopgmodule.c | 53 +++++++++++++++++++++++++++++------------ 1 file changed, 38 insertions(+), 15 deletions(-) diff --git a/psycopg/psycopgmodule.c b/psycopg/psycopgmodule.c index a372b891..3d695b58 100644 --- a/psycopg/psycopgmodule.c +++ b/psycopg/psycopgmodule.c @@ -721,6 +721,40 @@ psyco_errors_set(PyObject *type) } } +RAISES_NEG +static int +add_module_constants(PyObject *module) +{ + if (0 > PyModule_AddStringConstant(module, + "__version__", xstr(PSYCOPG_VERSION))) + { return -1; } + + if (0 > PyModule_AddStringConstant(module, + "__doc__", "psycopg2 PostgreSQL driver")) + { return -1; } + + if (0 > PyModule_AddIntConstant(module, + "__libpq_version__", PG_VERSION_NUM)) + { return -1; } + + if (0 > PyModule_AddObject(module, + "apilevel", Text_FromUTF8(APILEVEL))) + { return -1; } + + if (0 > PyModule_AddObject(module, + "threadsafety", PyInt_FromLong(THREADSAFETY))) + { return -1; } + + if (0 > PyModule_AddObject(module, + "paramstyle", Text_FromUTF8(PARAMSTYLE))) + { return -1; } + + if (0 > PyModule_AddIntMacro(module, REPLICATION_PHYSICAL)) { return -1; } + if (0 > PyModule_AddIntMacro(module, REPLICATION_LOGICAL)) { return -1; } + + return 0; +} + /** method table and module initialization **/ @@ -934,11 +968,6 @@ INIT_MODULE(_psycopg)(void) #endif if (!module) { goto exit; } - dict = PyModule_GetDict(module); - - /* initialize all the module's exported functions */ - /* PyBoxer_API[PyBoxer_Fake_NUM] = (void *)PyBoxer_Fake; */ - /* Create a CObject containing the API pointer array's address */ /* If anybody asks for a PyCapsule we'll deal with it. */ #if PY_VERSION_HEX < 0x03020000 @@ -950,17 +979,9 @@ INIT_MODULE(_psycopg)(void) /* other mixed initializations of module-level variables */ if (!(psycoEncodings = PyDict_New())) { goto exit; } if (0 != psyco_encodings_fill(psycoEncodings)) { goto exit; } - psyco_null = Bytes_FromString("NULL"); + if (!(psyco_null = Bytes_FromString("NULL"))) { goto exit; } - /* set some module's parameters */ - PyModule_AddStringConstant(module, "__version__", xstr(PSYCOPG_VERSION)); - PyModule_AddStringConstant(module, "__doc__", "psycopg PostgreSQL driver"); - PyModule_AddIntConstant(module, "__libpq_version__", PG_VERSION_NUM); - PyModule_AddIntMacro(module, REPLICATION_PHYSICAL); - PyModule_AddIntMacro(module, REPLICATION_LOGICAL); - PyModule_AddObject(module, "apilevel", Text_FromUTF8(APILEVEL)); - PyModule_AddObject(module, "threadsafety", PyInt_FromLong(THREADSAFETY)); - PyModule_AddObject(module, "paramstyle", Text_FromUTF8(PARAMSTYLE)); + if (0 > add_module_constants(module)) { goto exit; } /* put new types in module dictionary */ PyModule_AddObject(module, "connection", (PyObject*)&connectionType); @@ -987,6 +1008,8 @@ INIT_MODULE(_psycopg)(void) /* encodings dictionary in module dictionary */ PyModule_AddObject(module, "encodings", psycoEncodings); + dict = PyModule_GetDict(module); + #ifdef HAVE_MXDATETIME /* If we can't find mx.DateTime objects at runtime, * remove them from the module (and, as consequence, from the adapters). */