Re: Can't register an adapter for an "old style" python class - Mailing list psycopg

From Daniele Varrazzo
Subject Re: Can't register an adapter for an "old style" python class
Date
Msg-id CA+mi_8b1gTC6z_15E7CFCkfPo1Rv36sOsgQF4cu_e6VJTkxNCQ@mail.gmail.com
Whole thread Raw
In response to Re: Can't register an adapter for an "old style" python class  (Ghislain LEVEQUE <ghislain.leveque@clarisys.fr>)
List psycopg
On Wed, Nov 2, 2011 at 1:28 PM, Ghislain LEVEQUE
<ghislain.leveque@clarisys.fr> wrote:
> Le 02/11/2011 13:00, Daniele Varrazzo a écrit :
>>
>> On Wed, Nov 2, 2011 at 10:56 AM, Ghislain LEVEQUE
>> <ghislain.leveque@clarisys.fr>  wrote:
>>>
>>> I guess psycopg2 uses type instead of isinstance and this does not work
>>> with
>>> old-style python class :
>>
>> Yes, as far as I can see it has always worked this way: roughly there
>> is a mapping type ->  adapter in psycopg2.extensions.adapters and the
>> adapter is looked up by adapters[type(obj)]. If we had to use
>> isinstance instead, it would take a linear scan of the adapters map
>> for every adapted object. So It seems psycopg has worked with new
>> style classes since they were really new, and never bothered with
>> old-style ones.
>
> Is it possible to (re)define 'adapt' so that it first check if type(obj) =
> 'instance' and then perform a scan on the mapping. Else, get the adapter the
> usual way ?

It is definitely possible, but I don't feel it urgent: I don't see any
reason to still be using old-style classes, new ones offer only
advantages (such as having a type, dateutil objects could use
__slots__ etc.), they have been around since about 2002 and never
since adapters for old style classes have been requested. Old style
classes have been phased out since Python 3.0.

Said this, the check for an object being an old-style class instance
is probably very lightweight (not much more than a pointer comparison
in C), so if a patch was submitted to introduce it I wouldn't be
against including it. The code is in microprotocols.c.


>> Also consider filing a request to the dateutil
>> author to make it a new-style class. Oh, it looks like you are a
>> contributor:<https://launchpad.net/dateutil>. Why don't you make it a
>> new-style class?
>
> Well I think I'll have to do this. Thanks

I honestly think it would be globally a better gain than making
psycopg2 old-style classes aware.


-- Daniele

psycopg by date:

Previous
From: Ghislain LEVEQUE
Date:
Subject: Re: Can't register an adapter for an "old style" python class
Next
From: Fred Cox
Date:
Subject: ETA for 2.4.3 release? And stability of git repository branch?