Search code examples
pythonpython-3.xpython-2to3

Problem regarding 3.0's "hashlib" module


I've been working on getting a 2.5 module ported to 3.0, mostly for my own education, when I've gotten stuck. The class "Builder" has as its init:

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

But the error occurs at:

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

which has as its traceback:

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

I've run it through 2to3, but it isn't picking up on it. As far as I can tell, the update function is expecting the argument to be in the form of bytes/buffer, but I've tried several different methods to convert it and haven't succeeded.

As always, any assistance would be greatly appreciated. :)


Solution

  • I'm guessing that this line:

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

    should become:

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

    The actual desired encoding could be different in your case, but UTF-8 will work in all cases.