简体   繁体   English

UnboundLocalError:调用WinDLL后在分配之前引用了本地变量'cur'

[英]UnboundLocalError: local variable 'cur' referenced before assignment after call WinDLL

Help me please... I can't solve that problem. 请帮助我...我不能解决这个问题。 That message is happen after call DLL function. 该消息是在调用DLL函数后发生的。 Of course DLL's function and return value is all right. 当然DLL的功能和返回值都可以。

Error Message: UnboundLocalError: local variable 'cur' referenced before assignment 错误消息:UnboundLocalError:赋值之前引用了本地变量'cur'

Source Code. 源代码。

daDll = windll.LoadLibrary('C:\DLL\DA_PcPos.dll')
...
rtnValue = daDll.da_PcPos(3, req_char, rep_text)

if rtnValue == -1:
   self.QueryData()


def QueryData(self):
        global gsHOST_DB, gsPORT_DB, gsUSER_DB, gsPSWD_DB, gsSCHEMA_DB
        try:
            connDB = pymysql.connect(host=gsHOST_DB, port=int(gsPORT_DB), user=gsUSER_DB, passwd=gsPSWD_DB, db=gsSCHEMA_DB, charset='utf8', use_unicode=True) <- Assignment Error
            cur = connDB.cursor()
            cur.execute(""" SELECT DEPOSIT_DIV_NM
                                 , DEPOSIT_DIV_CD
                              FROM ADM_DEPOSIT_DIV
                             ORDER BY ORDER_SEQ """,)
            rows = cur.fetchall()
            self.cbxPayMethod.Clear()
            for row in rows:
                self.cbxPayMethod.Append(row[0])
        except:
            exception = sys.exc_info()[1]
            wx.MessageBox(exception.args[1], 'error', wx.OK | wx.ICON_INFORMATION)
        finally:
            cur.close()
            connDB.close()

You just need to look at what will happen if (for example) the pymysql.connect call throws an exception. 您只需要查看(例如) pymysql.connect调用引发异常会发生什么情况。

In that case, cur will never be set to anything and yet you will attempt to close() it in the finally block. 在这种情况下, cur永远不会设置为任何东西,但是您将尝试在finally块中将其close()


As to how to fix it, there's a couple of ways. 关于修复方法,有两种方法。 The first is to make your exception handling a little more fine-grained so that each potentially-excepting statement has its own catch . 首先是使您的异常处理更细粒度,以便每个可能例外的语句都有自己的catch That way, you know what has failed to date and what to clean up. 这样一来,您就知道什么到目前为止还没有解决,还有什么要清理。

However, that may not be desirable since it may increase the size of your code considerably. 但是,这可能并不理想,因为它可能会大大增加代码的大小。

Another method that does not have that drawback relies on setting the variables to a sentinel value before the try block. 没有此缺点的另一种方法依赖于在try之前将变量设置为哨兵值。 That way, you know they will exist and you can check their value. 这样,您知道它们将存在,并且可以检查它们的值。

In other words, something like: 换句话说,类似:

connDb = None
cur = None
try:
    connDb = ...
    cur = ...
    :
catch:
    :
finally:
    if cur is not None: cur.close()
    if connDb is not None: connDb.close()

If the problem is more to do with the fact that an exception only occurs with a call to the DLL beforehand, you'll need to do some more investigation as there's not really enough information in the question. 如果问题更多与仅在事先调用DLL时发生异常有关,那么您将需要进行更多调查,因为问题中没有足够的信息。

For a start, I'd examine all the variables that you use for the pymysql.connect call since they may be affected by the DLL (global variables are quite often the cause of these sorts of problems). 首先,我将检查用于pymysql.connect调用的所有变量,因为它们可能会受到DLL的影响(全局变量通常是导致此类问题的原因)。 The actual text of the exception would also be valuable. 例外的实际文本也很有价值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 UnboundLocalError:分配前已引用局部变量“ cur” - UnboundLocalError: local variable 'cur' referenced before assignment if 语句后的“UnboundLocalError:赋值前引用的局部变量” - "UnboundLocalError: local variable referenced before assignment" after an if statement “UnboundLocalError: local variable referenced before assignment” 使用全局变量后 - "UnboundLocalError: local variable referenced before assignment" After using global variables UnboundLocalError:分配前已引用局部变量“ truebomb” - UnboundLocalError: local variable 'truebomb' referenced before assignment UnboundLocalError:分配前已引用局部变量“ Counter” - UnboundLocalError: local variable 'Counter' referenced before assignment UnBoundLocalError:赋值之前引用的局部变量(Python) - UnBoundLocalError: local variable referenced before assignment (Python) UnboundLocalError:分配前已引用局部变量“ strdate” - UnboundLocalError: local variable 'strdate' referenced before assignment UnboundLocalError:赋值之前引用了局部变量“ key” - UnboundLocalError: local variable 'key' referenced before assignment unboundLocalError:赋值前引用了局部变量“loopback” - unboundLocalError: local variable 'loopback' referenced before assignment UnboundLocalError:分配前已引用局部变量“ endProgram” - UnboundLocalError: local variable 'endProgram' referenced before assignment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM