Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python: Why does SQLObject fail in conn.autocommit(1)?

In my server code, there is a call to _SO_fetchAlternateID (nested in some value call), which ultimately calls makeConnection in pgconnection.py.

This call fails on conn.autocommit(1), with the error

TypeError: 'bool' object is not callable

Here is SQLObject's (0.8.7) code:

def makeConnection(self):
try:
    if self.use_dsn:
        conn = self.module.connect(self.dsn)
    else:
        conn = self.module.connect(**self.dsn_dict)
except self.module.OperationalError, e:
    raise self.module.OperationalError("%s; used connection string %r" % (e, self.dsn))
if self.autoCommit:
    # psycopg2 does not have an autocommit method.
    if hasattr(conn, 'autocommit'):
        conn.autocommit(1)
return conn

Debugging shows that conn indeed holds a connection object, but autocommit is not a method but instead a boolean (False).

self.module is module 'psycopg2' (2.4.2).

Is this a configuration issue? mismatching versions?

UPDATE:

The cause turns out to be an incompatibility issue in psycopg2-2.4.2. Looking at the C source code, psycopg/connection.h has an integer variable unfortunately named autocommit. Version 2-2.4 works ok.

like image 911
Ovesh Avatar asked Jun 16 '11 15:06

Ovesh


1 Answers

You have just spotted a bug. Take a look at this code:

def _setAutoCommit(self, conn, auto):
    # psycopg2 does not have an autocommit method.
    if hasattr(conn, 'autocommit'):
        conn.autocommit(auto)

It assumes that conn (type: psycopg2.connection) may not have an autocommit property, but when it has one, it has to be a function. It is wrong in context of psycopg2.connection, where psycopg2.connection.autocommit is a bool.

The same assumption is taken in makeConnection as you mentioned, and few other functions in there.

This could be fixed by changing every call like conn.autocommit(val) to conn.autocommit = val, which should be easy, even with sed.

like image 159
Dr McKay Avatar answered Sep 21 '22 05:09

Dr McKay