Re: (Real)DictConnection is not compatible with the adaptation mechanism

James Henstridge <[email protected]>
Newsgroups gmane.comp.python.db.psycopg.devel
Message-ID <[email protected]>
On Thu, Jan 22, 2009 at 2:20 AM, Lawrence Oluyede <[email protected]> wrote:
> I tried to register an adapter for a SQL type following the examples
> and the documentation and it all worked fine.
> My problem manifested itself when I tried to integrate the code with
> the real application which uses (Real)DictConnection.
>
> When I register the new type with:
>
> psycopg2.extensions.register_type(NEWTYPE, conn)
>
> it breaks with the following error:
>
> TypeError: argument 2 must be a connection, cursor or None
>
> which is located in psycopgmodule.c in function psyco_register_type.
>
> It seems that it checks for "cursorType" and "connectionType"
> preventing inherited objects to work with the adaptation mechanism:
>
> psyco_register_type(PyObject *self, PyObject *args)
> {
>   [...]
>
>    if (obj != NULL) {
>        if (obj->ob_type == &cursorType) {
>            _psyco_register_type_set(&(((cursorObject*)obj)->string_types),
> type);
>        }
>        else if (obj->ob_type == &connectionType) {
>            typecast_add(type, ((connectionObject*)obj)->string_types, 0);
>        }
>        else {
>            PyErr_SetString(PyExc_TypeError,
>                "argument 2 must be a connection, cursor or None");
>            return NULL;
>        }
>    }

You could try replacing the type checks with e.g.

    PyObject_TypeCheck(obj, &cursorType)

That should handle the subclassed connection correctly.  If you try
this, please tell us whether it is successful or not so we can fix it
in future versions.

James.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.