1

Good Afternoon, for several days i try, to getting the Python QR-Code Module from lincolnloop running under Python3. It runs perfect on Python 2.x. - https://github.com/lincolnloop/python-qrcode

In general i'am very new to Python Programming but i think i have done my homework so far.

First Error:

 File "/usr/lib/python3.2/qrcode/util.py", line 274, in __init__

    if not isinstance(data, basestring):
NameError: global name 'basestring' is not defined

So basestring didnt exist in Python3 anymore i fix this with this Code Statement found here. - https://github.com/oxplot/fysom/issues/1

try:
    unicode = unicode
except NameError:
    # 'unicode' is undefined, must be Python 3
    str = str
    unicode = str
    bytes = bytes
    basestring = (str,bytes)
else:
    # 'unicode' exists, must be Python 2
    str = str
    unicode = unicode
    bytes = str
    basestring = basestring

So the next Error comes up.

  File "/usr/lib/python3.2/qrcode/util.py", line 285, in __init__
    elif re.match('^[%s]*$' % re.escape(ALPHA_NUM), data):
  File "/usr/lib/python3.2/re.py", line 153, in match
    return _compile(pattern, flags).match(string)
TypeError: can't use a string pattern on a bytes-like object

So i try the solution found here - Python TypeError on regex and change the following code from:

elif re.match('^[%s]*$' % re.escape(ALPHA_NUM), data):

to:

elif re.match(b'^[%s]*$' % re.escape(ALPHA_NUM), data): 

to Handle the RegEx in Binary Mode. But this throws the next execption in the same line of Code.

    elif re.match(b'^[%s]*$' % re.escape(ALPHA_NUM), data):
TypeError: unsupported operand type(s) for %: 'bytes' and 'str'

I also try to change

ALPHA_NUM = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:'

to

ALPHA_NUM = b'0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:'

But this makes no changes to the Execption.

So this indicates for me the same error as the one before, and anywhere in the code must be this type error wether its bytes or string type. But i cant find it.

I know the whole script is very complex for diving into python, but for my project, i need a working QR Code generator.

Can somebody give me a clue? Thanks in advance!

Community
  • 1
  • 1
Michael
  • 46
  • 5
  • 3
    Best place is probably to start with http://docs.python.org/2/library/2to3.html - and fix errors it can't automatically translate – Jon Clements Dec 29 '12 at 20:28
  • Thnx for your reply. I study the link you provided earlier today day, but the automatic 2to3 tool only fixes the "string, basestring" Problem, which i fixed manual before. The 'TypeError: unsupported operand type(s) for %: bytes and str' didnt even show up. This was the reason i decide to ask on stackoverflow for help. – Michael Dec 29 '12 at 20:39
  • 1
    Can you go the other way and convert `data` to a string? You'd need to do `data.decode(encoding)` where `encoding` is the appropriate encoding (like `'ascii'`). Sorry, I haven't looked through all of the links, so maybe it's not a real solution. – Lev Levitsky Dec 29 '12 at 21:05
  • there is no official `PIL` version (the dependency that `qrcode` relies on to handle images) for Python 3. There is no tests (if you wan't to make it useful to others; don't port without tests). – jfs Dec 29 '12 at 21:43

1 Answers1

0

You can't use the % operator with bytes objects. Is the data you are dealing with now really binary data, or is it text? If it is text, you should really handle it as strings, not as bytes.

Also, talk to the authors of the module. They might have done much of the porting already, or they might be willing to help. Thirdly, read python3porting.com, a free book on the topic.

Handling both Unicode and bytes data is the difficult part of the porting. You will have to make sure you are consistently using one or the other, and it is often quite a pain to get right.

Lennart Regebro
  • 167,292
  • 41
  • 224
  • 251
  • Thank you all for trying to help me. I can't figured out how to fix the errors. I'am really wondering about this error message becouse i can't find further information about what that means. `elif re.match(b'^[%s]*$' % re.escape(ALPHA_NUM), data): TypeError: unsupported operand type(s) for %: 'bytes' and 'bytes'` I found another Version of the QR Generator.It seems to be the original one, which Python-QRCode is based on.But currently there is no working Image Processing implemented.The Codes look really similar but there are some differences. This one dosen't use Regluar Expresions. – Michael Dec 30 '12 at 16:34
  • For the sake of completeness, here is the link to the other one: [link](http://www.d-project.com/qrcode/index.html) Michael – Michael Dec 30 '12 at 16:37
  • Thank you! After many hours studying the code, i finally get a working port of Python-QR for Python3 running. – Michael Dec 30 '12 at 21:40
  • 2
    I take the Original one from d-project.com and implemented a working Image Processing Function with PIL. Currently i need to clean up the code a little bit, but after that i post it, so everyone can use it with Python3. Michael – Michael Dec 30 '12 at 21:40