Skip to content

Commit cf7701a

Browse files
committed
Little fixes.
1 parent 8d8bfe9 commit cf7701a

File tree

3 files changed

+27
-29
lines changed

3 files changed

+27
-29
lines changed

ChangeLog

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
2006-06-15 Federico Di Gregorio <fog@initd.org>
2+
3+
* psycopg/typecast_basic.c: fixed problem with bogus
4+
conversion when importing gtk (that was crazy, I didn't
5+
understand why it happened but the new code just fixes it.)
6+
7+
* ZPsycopgDA/db.py: better type analisys, using an hash
8+
instead of a series of if (variation on patch from Charlie
9+
Clark.)
10+
111
2006-06-11 Federico Di Gregorio <fog@initd.org>
212

313
* Release 2.0.2.

ZPsycopgDA/db.py

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ def __init__(self, dsn, tilevel, enc='utf-8'):
4242
self.encoding = enc
4343
self.failures = 0
4444
self.calls = 0
45+
self.make_mappings()
4546

4647
def getconn(self, create=True):
4748
conn = pool.getconn(self.dsn)
@@ -89,32 +90,23 @@ def close(self):
8990
def sortKey(self):
9091
return 1
9192

93+
def make_mappings(self):
94+
"""Generate the mappings used later by self.convert_description()."""
95+
self.type_mappings = {}
96+
for t, s in [(INTEGER,'i'), (LONGINTEGER, 'i'), (NUMBER, 'n'),
97+
(BOOLEAN,'n'), (ROWID, 'i'),
98+
(DATETIME, 'd'), (DATE, 'd'), (TIME, 'd')]:
99+
for v in t.values:
100+
self.type_mappings[v] = (t, s)
101+
92102
def convert_description(self, desc, use_psycopg_types=False):
93103
"""Convert DBAPI-2.0 description field to Zope format."""
94104
items = []
95105
for name, typ, width, ds, p, scale, null_ok in desc:
96-
if typ == NUMBER:
97-
if typ == INTEGER or typ == LONGINTEGER:
98-
typs = 'i'
99-
else:
100-
typs = 'n'
101-
typp = NUMBER
102-
elif typ == BOOLEAN:
103-
typs = 'n'
104-
typp = BOOLEAN
105-
elif typ == ROWID:
106-
typs = 'i'
107-
typp = ROWID
108-
# FIXME: shouldn't DATETIME include other types?
109-
elif typ == DATETIME or typ == DATE or typ == TIME:
110-
typs = 'd'
111-
typp = DATETIME
112-
else:
113-
typs = 's'
114-
typp = STRING
106+
m = self.type_mappings.get(typ, (STRING, 's'))
115107
items.append({
116108
'name': name,
117-
'type': use_psycopg_types and typp or typs,
109+
'type': use_psycopg_types and m[0] or m[1],
118110
'width': width,
119111
'precision': p,
120112
'scale': scale,

psycopg/typecast_basic.c

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,11 @@ typecast_LONGINTEGER_cast(char *s, int len, PyObject *curs)
5454
static PyObject *
5555
typecast_FLOAT_cast(char *s, int len, PyObject *curs)
5656
{
57-
/* FIXME: is 64 large enough for any float? */
58-
char buffer[64];
59-
60-
if (s == NULL) {Py_INCREF(Py_None); return Py_None;}
61-
if (s[len] != '\0') {
62-
strncpy(buffer, s, len); buffer[len] = '\0';
63-
s = buffer;
64-
}
65-
return PyFloat_FromDouble(atof(s));
57+
char *pend;
58+
PyObject *str = PyString_FromStringAndSize(s, len);
59+
PyObject *flo = PyFloat_FromString(str, &pend);
60+
Py_DECREF(str);
61+
return flo;
6662
}
6763

6864
/** STRING - cast strings of any type to python string **/

0 commit comments

Comments
 (0)