2016-06-27 48 views
0

我与pyelliptic打,但我不能使用文件ecc.py中定义的方法raw_get_ecdh_key不受约束的方法必须用X例如被称为第一个参数(STR了实例,而不是)

这里是我的示例代码:

>>> import pyelliptic 
>>> pubkey1="0453eb8248f42b00d057bc1adc724c4c841ec75851d6cd86f56f9bae5223219c7d3c7aff832d2383dfec167327ef38e9bf066690a8f94c055b336a607cebc2dddf".decode('hex') 
>>> pubkey2="04678b95e902b04817ba258ecd3dbb2150d83850848a3b8523c11d51b6a9f89b93ea82db74ba82ba44fadb050b35eae8b96f3738e88c7c117227303a528c8df985".decode('hex') 
>>> pyelliptic.ECC.raw_get_ecdh_key(pubkey1, pubkey2) 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
TypeError: unbound method raw_get_ecdh_key() must be called with ECC instance as first argument (got str instance instead) 

我搜索,发现这里的主题的许多问题。我知道我需要调用ECC(),而不是ECC,但它并没有任何更好的工作:

>>> pyelliptic.ECC().raw_get_ecdh_key(pubkey1, pubkey2) 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "/usr/local/lib/python2.7/dist-packages/pyelliptic/ecc.py", line 324, in raw_get_ecdh_key 
    OpenSSL.EC_KEY_free(own_key) 
UnboundLocalError: local variable 'own_key' referenced before assignment 

我想不出有什么不对。我检查了,应该引用变量own_key

+0

请修复您的标题 - 您的问题不是尝试从类中调用实例方法。 –

回答

0

这是一个非常明显的pyelliptic错误 - 如果你看代码,own_key被定义的部分(ecc.py行301)被封装在巨大的try/finally块中。如果在第301行(很明显发生了什么)之前引发(而不是管理)异常,则执行流程跳转到finally块,以尝试引用own_key,此时尚未定义该块。

您可以通过克隆git的回购和编辑raw_get_ecdh_key函数的代码,这样1.相关的局部变量固定在功能开始之前try块(它们设置为None)和2的定义终于阻止这些变量对None测试,试图使用它们释放资源,即(未经测试)之前:

def raw_get_ecdh_key(self, pubkey_x, pubkey_y): 
    other_key = None 
    other_pub_key_x = None 
    other_pub_key_y = None 
    other_pub_key = None 
    own_key = None 
    own_priv_key = None 

    try: 
     # (snip code) 
    finally: 
     if other_key is not None: 
      OpenSSL.EC_KEY_free(other_key) 
     if other_pub_key_x is not None: 
      OpenSSL.BN_free(other_pub_key_x) 
     if other_pub_key_y is not None: 
      OpenSSL.BN_free(other_pub_key_y) 
     if other_pub_key is not None: 
      OpenSSL.EC_POINT_free(other_pub_key) 
     if own_key is not None: 
      OpenSSL.EC_KEY_free(own_key) 
     if own_priv_key is not None: 
      OpenSSL.BN_free(own_priv_key) 

再运行的单元测试(最终增加了对这种情况下一些测试),并提交pull请求作者。

请注意,您在已应用此(未经测试)解决您应该另一个错误 - 那送你finally块起初的一个 - 但至少你应该再有关于调用失败的原因更多的相关信息。

相关问题