[Zope3-dev] Re: Psycopgda adapter, should _conv_str do NULL check?

Derrick Hudson dman at dman13.dyndns.org
Wed Oct 13 08:20:33 EDT 2004


On Wed, Oct 13, 2004 at 07:58:48AM -0400, Carroll Kong wrote:
| In psycopgda module rev 27872, adapter.py ~line 266
| 
| The original code just converts strings passed to _conv_string into unicode.
| 
| def _conv_string(str):
|     return str.decode(PG_ENCODING)
| 
| However, if I have a NULL field in the database, then str.decode fails with
| AttributeError: 'NoneType' object has no attribute 'decode'

Good catch.

| This isn't the same as a '' string (in which case the decode would work), or
| at least my psycopgda driver (1.1.14 ) and postgresl version (7.4.1) didn't
| seem to believe so.  (I had an optional field and it was empty before I put
| on constraints to add default values).
| 
| I suppose you could argue that one should always use strong default values
| into the database.  I am not as familiar with the code, but I added this to
| psycopgda's adapter.py.
| 
| def _conv_string(str):
|     if str:
|         return str.decode(PG_ENCODING)
|     else:
|         return None
| 
| Not sure if that's going to cause problems or not.

That will convert '' to None because the empty string has a truth
value of False in python.

I would suggest something like

    def _conv_string(s):
        if s is not None :
            s = s.decode(PG_ENCODING)
        return s

or

    def _conv_string(s):
        if isinstance(s, str) :
            return s.decode(PG_ENCODING)
        return s

(note: I changed the argument name so it doesn't shadow the built-in
class 'str')

| My prelim testing seems to show it will work, or I could just always
| use default values so there are no NULL optional fields.

Zope can't disallow NULL in the database because the database allows
it.  (IMO, anyways)  This should probably be added to the collector
with a patch.

HTH,
-D

-- 
A)bort, R)etry, B)ang it with a large hammer
 
www: http://dman13.dyndns.org/~dman/            jabber: dman at dman13.dyndns.org
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://mail.zope.org/pipermail/zope3-dev/attachments/20041013/fbb3de6d/attachment-0001.bin


More information about the Zope3-dev mailing list