报错解决:TypeError: Object type class 'str' cannot be passed to C code

在Linux环境中运行Python代码时遇到TypeError,问题在于类型为str的对象不能传递到C代码。原本在Windows环境下正常运行的代码,在Linux上出现错误。解决方案是适当地调整代码,以兼容C代码对数据类型的处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

此文首发于我的个人博客:报错解决 TypeError Object type class ‘str’ cannot be passed to C code — zhang0peter的个人博客


下午在使用Crypto的时候报错如下:

Traceback (most recent call last):
  File "/usr/lib64/python3.6/multiprocessing/pool.py", line 119, in worker
    result = (True, func(*args, **kwds))
  File "/usr/lib64/python3.6/multiprocessing/pool.py", line 44, in mapstar
    return list(map(*args))
  File "FastFinally.py", line 86, in generate2
    next(yield_32),next(yield_32)
  File "FastFinally.py", line 55, in AES_32_YIELD
    yield int(aes.encrypt(data).hex(),16)
  File "/usr/local/lib64/python3.6/site-packages/pycryptodome-3.9a0-py3.6-linux-x86_64.egg/Crypto/Cipher/_mode_ctr.py", line 201, in encrypt
    c_uint8_ptr(plaintext),
  File "/usr/local/lib64/python3.6/site-packages/pycryptodome-3.9a0-py3.6-linux-x86_64.egg/Crypto/Util/_raw_api.py", line 235, in c_uint8_ptr
    raise TypeError("Object type %s cannot be passed to C code" % type(data))
TypeError: Object type <class 'str'> cannot be passed to C code
"""

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "FastFinally.py", line 97, in <module>
    pool.map(generate2,l)
  File "/usr/lib64/python3.6/multiprocessing/pool.py", line 266, in map
    return self._map_async(func, iterable, mapstar, chunksize).get()
  File "/usr/lib64/python3.6/multiprocessing/pool.py", line 644, in get
    raise self._value
TypeError: Object type <class 'str'> cannot be passed to C code

我的代码如下:

from Crypto.Cipher import AES
from Crypto.Util import Counter
import os
key=os.urandom(16)
aes = AES.new(key, AES.MODE_CTR, counter=Counter.new(128))
data = "0" * 8
temp=aes.encrypt(data)

这部分代码在Windows上跑没问题,但是到Linux上跑就出错了。

把代码修改如下:

temp=aes.encrypt(data.encode('utf-8'))

只能说坑真多

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值