@@ -294,103 +294,115 @@ psyco_libcrypto_threads_init(void)
294
294
* Return 0 on success, else -1 and set an exception.
295
295
*/
296
296
RAISES_NEG static int
297
- psyco_adapters_init (PyObject * mod )
297
+ adapters_init (PyObject * module )
298
298
{
299
- PyObject * call = NULL ;
299
+ PyObject * dict = NULL , * obj = NULL ;
300
300
int rv = -1 ;
301
301
302
+ if (0 > microprotocols_init (module )) { goto exit ; }
303
+
302
304
Dprintf ("psycopgmodule: configuring adapters" );
303
305
304
- if (0 != microprotocols_add (& PyFloat_Type , NULL , (PyObject * )& pfloatType )) {
306
+ if (0 > microprotocols_add (& PyFloat_Type , NULL , (PyObject * )& pfloatType )) {
305
307
goto exit ;
306
308
}
307
309
#if PY_MAJOR_VERSION < 3
308
- if (0 != microprotocols_add (& PyInt_Type , NULL , (PyObject * )& pintType )) {
310
+ if (0 > microprotocols_add (& PyInt_Type , NULL , (PyObject * )& pintType )) {
309
311
goto exit ;
310
312
}
311
313
#endif
312
- if (0 != microprotocols_add (& PyLong_Type , NULL , (PyObject * )& pintType )) {
314
+ if (0 > microprotocols_add (& PyLong_Type , NULL , (PyObject * )& pintType )) {
313
315
goto exit ;
314
316
}
315
- if (0 != microprotocols_add (& PyBool_Type , NULL , (PyObject * )& pbooleanType )) {
317
+ if (0 > microprotocols_add (& PyBool_Type , NULL , (PyObject * )& pbooleanType )) {
316
318
goto exit ;
317
319
}
318
320
319
321
/* strings */
320
322
#if PY_MAJOR_VERSION < 3
321
- if (0 != microprotocols_add (& PyString_Type , NULL , (PyObject * )& qstringType )) {
323
+ if (0 > microprotocols_add (& PyString_Type , NULL , (PyObject * )& qstringType )) {
322
324
goto exit ;
323
325
}
324
326
#endif
325
- if (0 != microprotocols_add (& PyUnicode_Type , NULL , (PyObject * )& qstringType )) {
327
+ if (0 > microprotocols_add (& PyUnicode_Type , NULL , (PyObject * )& qstringType )) {
326
328
goto exit ;
327
329
}
328
330
329
331
/* binary */
330
332
#if PY_MAJOR_VERSION < 3
331
- if (0 != microprotocols_add (& PyBuffer_Type , NULL , (PyObject * )& binaryType )) {
333
+ if (0 > microprotocols_add (& PyBuffer_Type , NULL , (PyObject * )& binaryType )) {
332
334
goto exit ;
333
335
}
334
336
#else
335
- if (0 != microprotocols_add (& PyBytes_Type , NULL , (PyObject * )& binaryType )) {
337
+ if (0 > microprotocols_add (& PyBytes_Type , NULL , (PyObject * )& binaryType )) {
336
338
goto exit ;
337
339
}
338
340
#endif
339
341
340
342
#if PY_MAJOR_VERSION >= 3 || PY_MINOR_VERSION >= 6
341
- if (0 != microprotocols_add (& PyByteArray_Type , NULL , (PyObject * )& binaryType )) {
343
+ if (0 > microprotocols_add (& PyByteArray_Type , NULL , (PyObject * )& binaryType )) {
342
344
goto exit ;
343
345
}
344
346
#endif
345
347
#if PY_MAJOR_VERSION >= 3 || PY_MINOR_VERSION >= 7
346
- if (0 != microprotocols_add (& PyMemoryView_Type , NULL , (PyObject * )& binaryType )) {
348
+ if (0 > microprotocols_add (& PyMemoryView_Type , NULL , (PyObject * )& binaryType )) {
347
349
goto exit ;
348
350
}
349
351
#endif
350
352
351
- if (0 != microprotocols_add (& PyList_Type , NULL , (PyObject * )& listType )) {
353
+ if (0 > microprotocols_add (& PyList_Type , NULL , (PyObject * )& listType )) {
352
354
goto exit ;
353
355
}
354
356
355
357
/* the module has already been initialized, so we can obtain the callable
356
358
objects directly from its dictionary :) */
357
- if (!(call = PyMapping_GetItemString (mod , "DateFromPy" ))) { goto exit ; }
358
- if (0 != microprotocols_add (PyDateTimeAPI -> DateType , NULL , call )) { goto exit ; }
359
- Py_CLEAR (call );
359
+ if (!(dict = PyModule_GetDict (module ))) { goto exit ; }
360
+
361
+ if (!(obj = PyMapping_GetItemString (dict , "DateFromPy" ))) { goto exit ; }
362
+ if (0 > microprotocols_add (PyDateTimeAPI -> DateType , NULL , obj )) { goto exit ; }
363
+ Py_CLEAR (obj );
360
364
361
- if (!(call = PyMapping_GetItemString (mod , "TimeFromPy" ))) { goto exit ; }
362
- if (0 != microprotocols_add (PyDateTimeAPI -> TimeType , NULL , call )) { goto exit ; }
363
- Py_CLEAR (call );
365
+ if (!(obj = PyMapping_GetItemString (dict , "TimeFromPy" ))) { goto exit ; }
366
+ if (0 > microprotocols_add (PyDateTimeAPI -> TimeType , NULL , obj )) { goto exit ; }
367
+ Py_CLEAR (obj );
364
368
365
- if (!(call = PyMapping_GetItemString (mod , "TimestampFromPy" ))) { goto exit ; }
366
- if (0 != microprotocols_add (PyDateTimeAPI -> DateTimeType , NULL , call )) { goto exit ; }
367
- Py_CLEAR (call );
369
+ if (!(obj = PyMapping_GetItemString (dict , "TimestampFromPy" ))) { goto exit ; }
370
+ if (0 > microprotocols_add (PyDateTimeAPI -> DateTimeType , NULL , obj )) { goto exit ; }
371
+ Py_CLEAR (obj );
368
372
369
- if (!(call = PyMapping_GetItemString (mod , "IntervalFromPy" ))) { goto exit ; }
370
- if (0 != microprotocols_add (PyDateTimeAPI -> DeltaType , NULL , call )) { goto exit ; }
371
- Py_CLEAR (call );
373
+ if (!(obj = PyMapping_GetItemString (dict , "IntervalFromPy" ))) { goto exit ; }
374
+ if (0 > microprotocols_add (PyDateTimeAPI -> DeltaType , NULL , obj )) { goto exit ; }
375
+ Py_CLEAR (obj );
372
376
373
377
#ifdef HAVE_MXDATETIME
374
- /* as above, we use the callable objects from the psycopg module */
375
- if (NULL != (call = PyMapping_GetItemString (mod , "TimestampFromMx" ))) {
376
- if (0 != microprotocols_add (mxDateTime .DateTime_Type , NULL , call )) { goto exit ; }
377
- Py_CLEAR (call );
378
+ /* As above, we use the callable objects from the psycopg module.
379
+ These object are not be available at runtime if mx.DateTime import
380
+ failed (e.g. it was available at build time but not at runtime). */
381
+ if (PyMapping_HasKeyString (dict , "TimestampFromMx" )) {
382
+ if (!(obj = PyMapping_GetItemString (dict , "TimestampFromMx" ))) {
383
+ goto exit ;
384
+ }
385
+ if (0 > microprotocols_add (mxDateTime .DateTime_Type , NULL , obj )) {
386
+ goto exit ;
387
+ }
388
+ Py_CLEAR (obj );
378
389
379
390
/* if we found the above, we have this too. */
380
- if (!(call = PyMapping_GetItemString (mod , "TimeFromMx" ))) { goto exit ; }
381
- if (0 != microprotocols_add (mxDateTime .DateTimeDelta_Type , NULL , call )) { goto exit ; }
382
- Py_CLEAR (call );
383
- }
384
- else {
385
- PyErr_Clear ();
391
+ if (!(obj = PyMapping_GetItemString (dict , "TimeFromMx" ))) {
392
+ goto exit ;
393
+ }
394
+ if (0 > microprotocols_add (mxDateTime .DateTimeDelta_Type , NULL , obj )) {
395
+ goto exit ;
396
+ }
397
+ Py_CLEAR (obj );
386
398
}
387
399
#endif
388
400
389
401
/* Success! */
390
402
rv = 0 ;
391
403
392
404
exit :
393
- Py_XDECREF (call );
405
+ Py_XDECREF (obj );
394
406
395
407
return rv ;
396
408
}
@@ -599,7 +611,10 @@ encodings_init(PyObject *module)
599
611
600
612
if (!(psycoEncodings = PyDict_New ())) { goto exit ; }
601
613
Py_INCREF (psycoEncodings );
602
- PyModule_AddObject (module , "encodings" , psycoEncodings );
614
+ if (0 > PyModule_AddObject (module , "encodings" , psycoEncodings )) {
615
+ Py_DECREF (psycoEncodings );
616
+ goto exit ;
617
+ }
603
618
604
619
for (i = 0 ; enctable [i ].pgenc != NULL ; i ++ ) {
605
620
if (!(value = Text_FromUTF8 (enctable [i ].pyenc ))) { goto exit ; }
@@ -655,8 +670,8 @@ static struct {
655
670
};
656
671
657
672
658
- static int
659
- psyco_errors_init (PyObject * module )
673
+ RAISES_NEG static int
674
+ errors_init (PyObject * module )
660
675
{
661
676
/* the names of the exceptions here reflect the organization of the
662
677
psycopg2 module and not the fact the the original error objects
@@ -702,6 +717,7 @@ psyco_errors_init(PyObject *module)
702
717
703
718
Py_INCREF (* exctable [i ].exc );
704
719
if (0 > PyModule_AddObject (module , name , * exctable [i ].exc )) {
720
+ Py_DECREF (* exctable [i ].exc );
705
721
goto exit ;
706
722
}
707
723
}
@@ -715,8 +731,7 @@ psyco_errors_init(PyObject *module)
715
731
}
716
732
717
733
718
- RAISES_NEG
719
- static int
734
+ RAISES_NEG static int
720
735
add_module_constants (PyObject * module )
721
736
{
722
737
PyObject * tmp ;
@@ -789,8 +804,7 @@ static struct {
789
804
{NULL } /* Sentinel */
790
805
};
791
806
792
- RAISES_NEG
793
- static int
807
+ RAISES_NEG static int
794
808
add_module_types (PyObject * module )
795
809
{
796
810
int i ;
@@ -805,6 +819,7 @@ add_module_types(PyObject *module)
805
819
806
820
Py_INCREF (type );
807
821
if (0 > PyModule_AddObject (module , typetable [i ].name , type )) {
822
+ Py_DECREF (type );
808
823
return -1 ;
809
824
}
810
825
}
@@ -832,7 +847,7 @@ datetime_init(void)
832
847
if (0 > psyco_replmsg_datetime_init ()) { return -1 ; }
833
848
834
849
Py_TYPE (& pydatetimeType ) = & PyType_Type ;
835
- if (PyType_Ready (& pydatetimeType ) == -1 ) { return -1 ; }
850
+ if (0 > PyType_Ready (& pydatetimeType )) { return -1 ; }
836
851
837
852
return 0 ;
838
853
}
@@ -844,7 +859,7 @@ mxdatetime_init(PyObject *module)
844
859
845
860
#ifdef HAVE_MXDATETIME
846
861
Py_TYPE (& mxdatetimeType ) = & PyType_Type ;
847
- if (PyType_Ready (& mxdatetimeType ) == -1 ) { return -1 ; }
862
+ if (0 > PyType_Ready (& mxdatetimeType )) { return -1 ; }
848
863
849
864
if (mxDateTime_ImportModuleAndAPI ()) {
850
865
Dprintf ("psycopgmodule: mx.DateTime module import failed" );
@@ -862,7 +877,7 @@ mxdatetime_init(PyObject *module)
862
877
* remove them from the module (and, as consequence, from the adapters). */
863
878
if (0 != psyco_adapter_mxdatetime_init ()) {
864
879
PyObject * dict ;
865
- dict = PyModule_GetDict (module );
880
+ if (!( dict = PyModule_GetDict (module ))) { return -1 ; }
866
881
if (0 > PyDict_DelItemString (dict , "DateFromMx" )) { return -1 ; }
867
882
if (0 > PyDict_DelItemString (dict , "TimeFromMx" )) { return -1 ; }
868
883
if (0 > PyDict_DelItemString (dict , "TimestampFromMx" )) { return -1 ; }
@@ -957,7 +972,7 @@ static struct PyModuleDef psycopgmodule = {
957
972
PyMODINIT_FUNC
958
973
INIT_MODULE (_psycopg )(void )
959
974
{
960
- PyObject * module = NULL , * dict ;
975
+ PyObject * module = NULL ;
961
976
962
977
#ifdef PSYCOPG_DEBUG
963
978
if (getenv ("PSYCOPG_DEBUG" ))
@@ -966,50 +981,38 @@ INIT_MODULE(_psycopg)(void)
966
981
967
982
Dprintf ("psycopgmodule: initializing psycopg %s" , xstr (PSYCOPG_VERSION ));
968
983
969
- /* initialize the types not exposed to the module */
984
+ /* initialize libcrypto threading callbacks */
985
+ psyco_libcrypto_threads_init ();
986
+
987
+ /* initialize types and objects not exposed to the module */
970
988
Py_TYPE (& typecastType ) = & PyType_Type ;
971
- if (PyType_Ready (& typecastType ) == -1 ) goto exit ;
989
+ if (0 > PyType_Ready (& typecastType )) { goto exit ; }
972
990
973
991
Py_TYPE (& chunkType ) = & PyType_Type ;
974
- if (PyType_Ready (& chunkType ) == -1 ) goto exit ;
992
+ if (0 > PyType_Ready (& chunkType )) { goto exit ; }
975
993
976
994
Py_TYPE (& errorType ) = & PyType_Type ;
977
995
errorType .tp_base = (PyTypeObject * )PyExc_StandardError ;
978
- if (PyType_Ready (& errorType ) == -1 ) goto exit ;
996
+ if (0 > PyType_Ready (& errorType )) { goto exit ; }
979
997
980
- /* initialize libcrypto threading callbacks */
981
- psyco_libcrypto_threads_init ();
998
+ if (!(psyco_null = Bytes_FromString ("NULL" ))) { goto exit ; }
982
999
983
- /* initialize the module and grab module's dictionary */
1000
+ /* initialize the module */
984
1001
#if PY_MAJOR_VERSION < 3
985
1002
module = Py_InitModule ("_psycopg" , psycopgMethods );
986
1003
#else
987
1004
module = PyModule_Create (& psycopgmodule );
988
1005
#endif
989
1006
if (!module ) { goto exit ; }
990
1007
991
- /* other mixed initializations of module-level variables */
992
- if (!(psyco_null = Bytes_FromString ("NULL" ))) { goto exit ; }
993
-
994
1008
if (0 > add_module_constants (module )) { goto exit ; }
995
1009
if (0 > add_module_types (module )) { goto exit ; }
996
1010
if (0 > datetime_init ()) { goto exit ; }
997
1011
if (0 > mxdatetime_init (module )) { goto exit ; }
998
-
999
- /* encodings dictionary in module dictionary */
1000
1012
if (0 > encodings_init (module )) { goto exit ; }
1001
-
1002
- dict = PyModule_GetDict (module );
1003
-
1004
- /* initialize default set of typecasters */
1005
- if (0 != typecast_init (dict )) { goto exit ; }
1006
-
1007
- /* initialize microprotocols layer */
1008
- microprotocols_init (dict );
1009
- if (0 != psyco_adapters_init (dict )) { goto exit ; }
1010
-
1011
- /* create a standard set of exceptions and add them to the module's dict */
1012
- if (0 != psyco_errors_init (module )) { goto exit ; }
1013
+ if (0 > typecast_init (module )) { goto exit ; }
1014
+ if (0 > adapters_init (module )) { goto exit ; }
1015
+ if (0 > errors_init (module )) { goto exit ; }
1013
1016
1014
1017
Dprintf ("psycopgmodule: module initialization complete" );
1015
1018
0 commit comments