开发者

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 pgc开发者_运维知识库onnection.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.


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.


It sound like that somewhere in the code somebody assign autocommit = True to the conn object.

When the interpreter reach the :

 conn.autocommit(1) 

In fact it evaluates:

 True(1) 

Check the content of self.dsn or self.dsn_dict, if there is not a 'autocommit' boolean key.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜