@@ -501,34 +501,44 @@ _pq_fetch_tuples(cursorObject *curs)
501501
502502 PyTuple_SET_ITEM (curs -> description , i , dtitem );
503503
504- /* fill the right cast function by accessing the global dictionary of
505- casting objects. if we got no defined cast use the default
506- one */
507- if (!(cast = PyDict_GetItem (curs -> casts , type ))) {
508- Dprintf ("_pq_fetch_tuples: cast %d not in per-cursor dict" , ftype );
509- if (!(cast = PyDict_GetItem (psyco_types , type ))) {
510- Dprintf ("_pq_fetch_tuples: cast %d not found, using default" ,
511- PQftype (curs -> pgres ,i ));
512- cast = psyco_default_cast ;
513- }
504+ /* fill the right cast function by accessing three different dictionaries:
505+ - the per-cursor dictionary, if available (can be NULL or None)
506+ - the per-connection dictionary (always exists but can be null)
507+ - the global dictionary (at module level)
508+ if we get no defined cast use the default one */
509+
510+ Dprintf ("_pq_fetch_tuples: looking for cast %d:" , ftype );
511+ if (curs -> string_types != NULL && curs -> string_types != Py_None ) {
512+ cast = PyDict_GetItem (curs -> string_types , type );
513+ Dprintf ("_pq_fetch_tuples: per-cursor dict: %p" , cast );
514+ }
515+ if (cast == NULL ) {
516+ cast = PyDict_GetItem (curs -> conn -> string_types , type );
517+ Dprintf ("_pq_fetch_tuples: per-connection dict: %p" , cast );
518+ }
519+ if (cast == NULL ) {
520+ cast = PyDict_GetItem (psyco_types , type );
521+ Dprintf ("_pq_fetch_tuples: global dict: %p" , cast );
514522 }
523+ if (cast == NULL ) cast = psyco_default_cast ;
524+
515525 /* else if we got binary tuples and if we got a field that
516526 is binary use the default cast
517527 FIXME: what the hell am I trying to do here? This just can't work..
518528 */
519- else if (pgbintuples && cast == psyco_default_binary_cast ) {
529+ if (pgbintuples && cast == psyco_default_binary_cast ) {
520530 Dprintf ("_pq_fetch_tuples: Binary cursor and "
521531 "binary field: %i using default cast" ,
522532 PQftype (curs -> pgres ,i ));
523533 cast = psyco_default_cast ;
524534 }
535+
525536 Dprintf ("_pq_fetch_tuples: using cast at %p (%s) for type %d" ,
526537 cast , PyString_AS_STRING (((typecastObject * )cast )-> name ),
527538 PQftype (curs -> pgres ,i ));
528539 Py_INCREF (cast );
529540 PyTuple_SET_ITEM (curs -> casts , i , cast );
530541
531-
532542 /* 1/ fill the other fields */
533543 PyTuple_SET_ITEM (dtitem , 0 ,
534544 PyString_FromString (PQfname (curs -> pgres , i )));
0 commit comments