V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
2378799
V2EX  ›  问与答

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

  •  
  •   2378799 · 2017-04-29 16:11:32 +08:00 · 3610 次点击
    这是一个创建于 2547 天前的主题,其中的信息可能已经有所发展或是发生改变。

    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')
    

    然后运行,出现了

    段错误 (核心已转储)
    
    13 条回复    2017-04-30 12:07:26 +08:00
    awanabe
        1
    awanabe  
       2017-04-29 16:20:23 +08:00
    bytes 是变量。。你把变量名改成 bytes 有什么用

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

    先看文档,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
        4
    2378799  
    OP
       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
        5
    pkuphy  
       2017-04-29 18:40:07 +08:00
    除法的问题,把 bytes 转成 int 类型:

    mb = ctypes.create_string_buffer(int(bytes))
    2378799
        6
    2378799  
    OP
       2017-04-29 18:41:49 +08:00
    @pkuphy
    ```
    TypeError: rjust() argument 2 must be a byte string of length 1, not str
    ```
    2378799
        7
    2378799  
    OP
       2017-04-29 18:45:36 +08:00
    #6 回复错了,出现的是一个新的问题
    pkuphy
        8
    pkuphy  
       2017-04-29 19:04:30 +08:00
    里面太多 bytes 相关的问题了,您慢慢调吧……
    pkuphy
        9
    pkuphy  
       2017-04-29 19:05:53 +08:00
    @2378799 macOS 上还不能直接跑,您自己调吧……
    pkuphy
        10
    pkuphy  
       2017-04-29 19:06:49 +08:00
    @2378799 为什么不用 pycoin 呢
    2378799
        11
    2378799  
    OP
       2017-04-29 19:11:51 +08:00
    @pkuphy 因为不知道有 pycoin,我现在试试
    Ahri
        12
    Ahri  
       2017-04-29 21:50:10 +08:00 via iPhone
    Python 自带一个 2to3 自动工具能解决大部分
    pkuphy
        13
    pkuphy  
       2017-04-30 12:07:26 +08:00
    @2378799 [bit]( https://github.com/ofek/bit) 也是一个好的选择
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2779 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 13:24 · PVG 21:24 · LAX 06:24 · JFK 09:24
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.