python - 关于 3. 0's "hashlib"模块的问题

标签 python python-3.x python-2to3

我一直在努力将 2.5 模块移植到 3.0,主要是为了我自己的教育,当我遇到困难时。 “Builder”类的初始值是:

def __init__(self, **options):
    self._verifyOptions(options)
    self._options = options
    self._initDigest()
    self._initBuildNames()
    self._methods = []

但是错误发生在:

def _initDigest(self):
    import os, sys, hashlib
    digester = hashlib.md5()
    digester.update(self._options.get('code'))
    self._digest = digester.hexdigest()

它的追溯是:

Traceback (most recent call last):
  File "<pyshell#5>", line 5, in <module>
    """, language="Cee")
  File "C:\Python30\lib\site-packages\PyInline\__init__.py", line 31, in build
    b = m.Builder(**args)
  File "C:\Python30\lib\site-packages\PyInline\Cee.py", line 17, in __init__
    self._initDigest()
  File "C:\Python30\lib\site-packages\PyInline\Cee.py", line 27, in _initDigest
    digester.update(self._options.get('code'))
TypeError: object supporting the buffer API required

我已经通过 2to3 运行它,但它没有接受它。据我所知,更新函数期望参数采用字节/缓冲区的形式,但我尝试了几种不同的方法来转换它,但没有成功。

一如既往,我们将不胜感激任何帮助。 :)

最佳答案

我猜这行:

digester.update(self._options.get('code'))

应该变成:

digester.update(self._options.get('code').encode("utf-8"))

实际所需的编码在您的情况下可能会有所不同,但 UTF-8 将适用于所有情况。

关于python - 关于 3. 0's "hashlib"模块的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/343204/

相关文章:

python - 使用 Django,如何从父类(super class)对象实例构造代理对象实例?

python-3.x - 在 Python 3 中将十进制数转换为二进制数

python - 2to3 说 "No changes needed",然后是 "files that need to be modified"

python - python 2to3 中的 eval(input())

python - 摘要行或数据框

python - Clojure 中的 map 是有序的吗?

python - 创建线程本地的成员变量

python - 使用 beautifulsoup 进行 HTML 解析适用于除我想要的 URL 之外的大多数 URL

arrays - NumPy:用三维数组中的平均值替换第三维的所有元素

python - setup.py 中的 2 到 3 没有涵盖测试?