Can't register an adapter for an "old style" python class

From: Ghislain LEVEQUE <ghislain(dot)leveque(at)clarisys(dot)fr>
To: psycopg(at)postgresql(dot)org
Subject: Can't register an adapter for an "old style" python class
Date: 2011-11-02 10:56:18
Message-ID: 4EB121D2.2010907@clarisys.fr
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: psycopg

I need to register an adapter for the dateutil's relativedelta class.

Here is what I did :

In [1]: import psycopg2

In [2]: psycopg2.__version__
Out[2]: '2.4.2 (dt dec pq3 ext)'

In [3]: import dateutil

In [4]: dateutil.__version__
Out[4]: '1.5'

In [5]: from psycopg2.extensions import adapt, adapters,
register_adapter, AsIs

In [6]: from dateutil.relativedelta import relativedelta

In [7]: register_adapter(relativedelta, lambda v: AsIs(adapt(str(v))))

In [8]: relativedelta in [k[0] for k in adapters.keys()]
Out[8]: True

In [9]: adapt(relativedelta(years=+1))
---------------------------------------------------------------------------
ProgrammingError Traceback (most recent call last)

/home/gl/src/MCA3/git2/<ipython console> in <module>()

ProgrammingError: can't adapt type 'instance'

I guess psycopg2 uses type instead of isinstance and this does not work
with old-style python class :

In [10]: [(isinstance(r, typ), type(r) == typ) for (typ, a), b in
....: adapters.items()]
Out[10]:
[(True, False),
(False, False),
(False, False),
(False, False),

[... SNIP ... ]

What is the "right" way to do this ?

--
Ghislain LEVEQUE - Clarisys Informatique
http://www.clarisys.fr - 09 72 11 43 60

Responses

Browse psycopg by date

  From Date Subject
Next Message Daniele Varrazzo 2011-11-02 12:00:17 Re: Can't register an adapter for an "old style" python class
Previous Message Daniele Varrazzo 2011-11-01 06:31:57 Re: patch: docs cleanup