将 python2 项目配适为 python3 时出错误 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
2378799
V2EX    问与答

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

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

    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  
    PRO
       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  
    PRO
       2017-04-29 19:04:30 +08:00
    里面太多 bytes 相关的问题了,您慢慢调吧……
    pkuphy
        9
    pkuphy  
    PRO
       2017-04-29 19:05:53 +08:00
    @2378799 macOS 上还不能直接跑,您自己调吧……
    pkuphy
        10
    pkuphy  
    PRO
       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  
    PRO
       2017-04-30 12:07:26 +08:00
    @2378799 [bit]( https://github.com/ofek/bit) 也是一个好的选择
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     2576 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 35ms UTC 02:24 PVG 10:24 LAX 19:24 JFK 22:24
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86