Skip site navigation (1) Skip section navigation (2)

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

From: Daniele Varrazzo <daniele(dot)varrazzo(at)gmail(dot)com>
To: Ghislain LEVEQUE <ghislain(dot)leveque(at)clarisys(dot)fr>
Cc: psycopg(at)postgresql(dot)org
Subject: Re: Can't register an adapter for an "old style" python class
Date: 2011-11-02 13:51:09
Message-ID: (view raw, whole thread or download thread mbox)
Lists: psycopg
On Wed, Nov 2, 2011 at 1:28 PM, Ghislain LEVEQUE
<ghislain(dot)leveque(at)clarisys(dot)fr> wrote:
> Le 02/11/2011 13:00, Daniele Varrazzo a écrit :
>> On Wed, Nov 2, 2011 at 10:56 AM, Ghislain LEVEQUE
>> <ghislain(dot)leveque(at)clarisys(dot)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:<>. 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

In response to

psycopg by date

Next:From: Fred CoxDate: 2011-11-03 06:39:19
Subject: ETA for 2.4.3 release? And stability of git repository branch?
Previous:From: Ghislain LEVEQUEDate: 2011-11-02 13:28:44
Subject: Re: Can't register an adapter for an "old style" python class

Privacy Policy | About PostgreSQL
Copyright © 1996-2017 The PostgreSQL Global Development Group