Ove*_*esh 5 python psycopg2 sqlobject
在我的服务器代码中,有一个对_SO_fetchAlternateID(嵌套在某个value调用中)的调用,它最终调用makeConnection了pgconnection.py.
此调用失败conn.autocommit(1),并出现错误
类型错误:“bool”对象不可调用
这是 SQLObject 的 (0.8.7) 代码:
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
Run Code Online (Sandbox Code Playgroud)
调试显示 conn 确实持有一个连接对象,但 autocommit 不是一个方法,而是一个布尔值 (False)。
self.module 是模块“psycopg2”(2.4.2)。
这是配置问题吗?版本不匹配?
更新:
结果证明是 psycopg2-2.4.2 中的不兼容问题。查看 C 源代码,psycopg/connection.h 有一个整数变量,不幸的是名为autocommit. 版本 2-2.4 工作正常。
您刚刚发现了一个错误。看看这段代码:
def _setAutoCommit(self, conn, auto):
# psycopg2 does not have an autocommit method.
if hasattr(conn, 'autocommit'):
conn.autocommit(auto)
Run Code Online (Sandbox Code Playgroud)
它假设conn(type :)psycopg2.connection可能没有autocommit属性,但是当它有属性时,它必须是一个函数。在psycopg2.connection, where psycopg2.connection.autocommitis a bool 的上下文中是错误的。
makeConnection正如您所提到的,采用了相同的假设,并且其中几乎没有其他功能。
这可以通过更改像 的每个调用conn.autocommit(val)来解决conn.autocommit = val,这应该很容易,即使使用sed.