Fixed error related to calling C typecasters from Python ones

This commit is contained in:
Federico Di Gregorio 2010-05-15 19:34:56 +02:00
parent 36cbefdee1
commit 6521fb5a44
4 changed files with 30 additions and 0 deletions

View File

@ -1,3 +1,8 @@
2010-04-05 Federico Di Gregorio <fog@initd.org>
* typecast.c: Fixed problem related to receiving None from Python
when a string was expected.
2010-05-07 Daniele Varrazzo <daniele.varrazzo@gmail.com>
* psycopg/adapter_datetime.c: Fixed TimestampFromTicks for second

View File

@ -435,6 +435,15 @@ typecast_call(PyObject *obj, PyObject *args, PyObject *kwargs)
return NULL;
}
// If the string is not a string but a None value we're being called
// from a Python-defined caster. There is no need to convert, just
// return it.
if (string == Py_None) {
Py_INCREF(string);
return string;
}
return typecast_cast(obj,
PyString_AsString(string), PyString_Size(string),
cursor);

View File

@ -198,6 +198,7 @@
<None Include="sandbox\valgrind-python.supp" />
<None Include="psycopg\green.h" />
<None Include="doc\src\pool.rst" />
<None Include="sandbox\dec2float.py" />
</ItemGroup>
<ItemGroup>
<Compile Include="psycopg\adapter_asis.c" />

15
sandbox/dec2float.py Normal file
View File

@ -0,0 +1,15 @@
import psycopg2
import psycopg2.extensions
DEC2FLOAT = psycopg2.extensions.new_type(
psycopg2._psycopg.DECIMAL.values,
'DEC2FLOAT',
psycopg2.extensions.FLOAT)
psycopg2.extensions.register_type(DEC2FLOAT)
o = psycopg2.connect("dbname=test")
c = o.cursor()
c.execute("SELECT NULL::decimal(10,2)")
n = c.fetchone()[0]
print n, type(n)