Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for issue #50, to ignore errors on shiftjis encoding #51

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions pyqrcode/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,11 @@ def next_byte(b):
#characters. Hence, the resulting mode should not be kanji.
#This is not an error.
pass
except LookupError:
#This occurs if the host Python does not support Shift JIS kanji
#encoding. Hence, the resulting mode should not be kanji.
#This is not an error.
pass

#All of the other attempts failed. The content can only be binary.
return 'binary', encoding
Expand Down
31 changes: 31 additions & 0 deletions tests/test_issue50.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# -*- coding: utf-8 -*-
"""\
Tests against <https://github.com/mnooner256/pyqrcode/issues/50>
"""
from __future__ import unicode_literals
from nose.tools import eq_
import pyqrcode


class FakeString(str):
'''
Create a mock class that *acts* like a string as far as needed for the
QRCode constructor, but raises an exception in case shiftjis encoding is
used on its value.

This mimics the behaviour of Python on an environment where this codec is
not installed.
'''
def __new__(cls, *more):
return str.__new__(cls, *more)

def encode(self, encoding=None, errors=None):
if encoding == 'shiftjis':
raise LookupError("unknown encoding: shiftjis")
return self.encode(encoding, errors)


def test_constructing_without_shiftjis_encoding_available():
content = FakeString("t123456789")
code = pyqrcode.create(content, error="Q")
eq_(code.mode, 'binary')