diff --git a/ChangeLog b/ChangeLog index 221459b3..fc9a8b11 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,13 @@ +2006-06-15 Federico Di Gregorio + + * psycopg/typecast_basic.c: fixed problem with bogus + conversion when importing gtk (that was crazy, I didn't + understand why it happened but the new code just fixes it.) + + * ZPsycopgDA/db.py: better type analisys, using an hash + instead of a series of if (variation on patch from Charlie + Clark.) + 2006-06-11 Federico Di Gregorio * Release 2.0.2. diff --git a/ZPsycopgDA/db.py b/ZPsycopgDA/db.py index 5b810805..9a0b4b02 100644 --- a/ZPsycopgDA/db.py +++ b/ZPsycopgDA/db.py @@ -42,6 +42,7 @@ class DB(TM, dbi_db.DB): self.encoding = enc self.failures = 0 self.calls = 0 + self.make_mappings() def getconn(self, create=True): conn = pool.getconn(self.dsn) @@ -89,32 +90,23 @@ class DB(TM, dbi_db.DB): def sortKey(self): return 1 + def make_mappings(self): + """Generate the mappings used later by self.convert_description().""" + self.type_mappings = {} + for t, s in [(INTEGER,'i'), (LONGINTEGER, 'i'), (NUMBER, 'n'), + (BOOLEAN,'n'), (ROWID, 'i'), + (DATETIME, 'd'), (DATE, 'd'), (TIME, 'd')]: + for v in t.values: + self.type_mappings[v] = (t, s) + def convert_description(self, desc, use_psycopg_types=False): """Convert DBAPI-2.0 description field to Zope format.""" items = [] for name, typ, width, ds, p, scale, null_ok in desc: - if typ == NUMBER: - if typ == INTEGER or typ == LONGINTEGER: - typs = 'i' - else: - typs = 'n' - typp = NUMBER - elif typ == BOOLEAN: - typs = 'n' - typp = BOOLEAN - elif typ == ROWID: - typs = 'i' - typp = ROWID - # FIXME: shouldn't DATETIME include other types? - elif typ == DATETIME or typ == DATE or typ == TIME: - typs = 'd' - typp = DATETIME - else: - typs = 's' - typp = STRING + m = self.type_mappings.get(typ, (STRING, 's')) items.append({ 'name': name, - 'type': use_psycopg_types and typp or typs, + 'type': use_psycopg_types and m[0] or m[1], 'width': width, 'precision': p, 'scale': scale, diff --git a/psycopg/typecast_basic.c b/psycopg/typecast_basic.c index d3ebb0fc..00f9480a 100644 --- a/psycopg/typecast_basic.c +++ b/psycopg/typecast_basic.c @@ -54,15 +54,11 @@ typecast_LONGINTEGER_cast(char *s, int len, PyObject *curs) static PyObject * typecast_FLOAT_cast(char *s, int len, PyObject *curs) { - /* FIXME: is 64 large enough for any float? */ - char buffer[64]; - - if (s == NULL) {Py_INCREF(Py_None); return Py_None;} - if (s[len] != '\0') { - strncpy(buffer, s, len); buffer[len] = '\0'; - s = buffer; - } - return PyFloat_FromDouble(atof(s)); + char *pend; + PyObject *str = PyString_FromStringAndSize(s, len); + PyObject *flo = PyFloat_FromString(str, &pend); + Py_DECREF(str); + return flo; } /** STRING - cast strings of any type to python string **/