将 python2 项目配适为 python3 时出错误

2017-04-29 16:11:32 +08:00
 2378799

https://github.com/weex/addrgen

这是一个 python2 生成 bitcoin 公钥与私钥的项目

将 python2 项目配适为 python3 时出错误

首先我修改了 print

然后运行,出现了:

Traceback (most recent call last):
  File "addrgen.py", line 191, in <module>
    test(int(options.otherversion))
  File "addrgen.py", line 165, in test
    print(get_addr(gen_eckey()))
  File "addrgen.py", line 147, in get_addr
    secret = k.get_secret()
  File "addrgen.py", line 56, in get_secret
    mb = ctypes.create_string_buffer(bytes)
  File "/usr/lib/python3.5/ctypes/__init__.py", line 63, in create_string_buffer
    raise TypeError(init)
TypeError: 32.875

然后在 stackoverflow 找到了解决方案:

http://stackoverflow.com/questions/7237133/create-string-buffer-throwing-error-typeerror-str-bytes-expected-instead-of-str

将 56 行

mb = ctypes.create_string_buffer(bytes)

改为了

mb = ctypes.create_string_buffer(b'bytes')

然后运行,出现了

段错误 (核心已转储)
3620 次点击
所在节点    问与答
13 条回复
awanabe
2017-04-29 16:20:23 +08:00
bytes 是变量。。你把变量名改成 bytes 有什么用

encode...
2378799
2017-04-29 16:25:05 +08:00
@awanabe 可以具体说说应该如何修改吗?
hosiet
2017-04-29 17:47:34 +08:00
我都不知道该从哪里开始吐槽了……

先看文档,https://docs.python.org/3.6/library/ctypes.html#ctypes.create_string_buffer

ctypes.create_string_buffer(init_or_size, size=None)

This function creates a mutable character buffer. The returned object is a ctypes array of c_char.

init_or_size must be an integer which specifies the size of the array, or a bytes object which will be used to initialize the array items.

于是你应该传入的是整型( int 类型)的数据,或者 bytes 类型的二进制数据。

然后你传进来的是个浮点数?

修改就更奇葩了,名为 bytes 的不知类型的变量,和一个内容为 "bytes" 五个字节的 bytes 类型的字面量根本就是雷锋和雷锋塔的区别,捂脸
2378799
2017-04-29 18:34:48 +08:00
@hosiet 你好,请问为什么在 python2 中可以运行?

https://docs.python.org/2/library/ctypes.html#ctypes.create_string_buffer

ctypes.create_string_buffer ( init_or_size [,size ] )


在 python2 中传入的也应该是浮点数呀
pkuphy
2017-04-29 18:40:07 +08:00
除法的问题,把 bytes 转成 int 类型:

mb = ctypes.create_string_buffer(int(bytes))
2378799
2017-04-29 18:41:49 +08:00
@pkuphy
```
TypeError: rjust() argument 2 must be a byte string of length 1, not str
```
2378799
2017-04-29 18:45:36 +08:00
#6 回复错了,出现的是一个新的问题
pkuphy
2017-04-29 19:04:30 +08:00
里面太多 bytes 相关的问题了,您慢慢调吧……
pkuphy
2017-04-29 19:05:53 +08:00
@2378799 macOS 上还不能直接跑,您自己调吧……
pkuphy
2017-04-29 19:06:49 +08:00
@2378799 为什么不用 pycoin 呢
2378799
2017-04-29 19:11:51 +08:00
@pkuphy 因为不知道有 pycoin,我现在试试
Ahri
2017-04-29 21:50:10 +08:00
Python 自带一个 2to3 自动工具能解决大部分
pkuphy
2017-04-30 12:07:26 +08:00
@2378799 [bit]( https://github.com/ofek/bit) 也是一个好的选择

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/358103

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX