From 6e4c9532cfc43cba6b04d402a2b14306b61e4fd0 Mon Sep 17 00:00:00 2001 From: Martijn van Rheenen Date: Tue, 1 Nov 2016 15:45:02 +0100 Subject: [PATCH 1/2] Fix for issue #50, to ignore errors on shiftjis encoding when trying to detect the type of contents --- pyqrcode/__init__.py | 5 +++++ tests/test_issue50.py | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 tests/test_issue50.py diff --git a/pyqrcode/__init__.py b/pyqrcode/__init__.py index 8301f8b..e54d243 100644 --- a/pyqrcode/__init__.py +++ b/pyqrcode/__init__.py @@ -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 diff --git a/tests/test_issue50.py b/tests/test_issue50.py new file mode 100644 index 0000000..7c17dbd --- /dev/null +++ b/tests/test_issue50.py @@ -0,0 +1,35 @@ +# -*- coding: utf-8 -*- +"""\ +Tests against +""" +from __future__ import unicode_literals +from nose.tools import eq_ +import pyqrcode + + +class FakeString: + ''' + 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 __init__(self, value): + self.value = value + + def __str__(self): + return self.value + + def __len__(self): + return len(self.value) + + def encode(self, encoding): + if encoding == 'shiftjis': + raise LookupError("unknown encoding: shiftjis") + return self.value.encode(encoding) + + +def test_constructing_without_shiftjis_encoding_available(): + content = FakeString("t123456789") + code = pyqrcode.create(content, error="Q") + eq_(code.mode, 'binary') From 810b7ef97aeb397d839de0f8ab9cd50f893b17c9 Mon Sep 17 00:00:00 2001 From: Martijn van Rheenen Date: Thu, 10 Nov 2016 14:33:17 +0100 Subject: [PATCH 2/2] Enhanced test_issue50.py: Changed FakeString class to inherit from str, as suggested by Lars Heur, enhanced to prevent PyLint warnings --- tests/test_issue50.py | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/tests/test_issue50.py b/tests/test_issue50.py index 7c17dbd..dc12092 100644 --- a/tests/test_issue50.py +++ b/tests/test_issue50.py @@ -7,26 +7,22 @@ import pyqrcode -class FakeString: +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. + 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. + This mimics the behaviour of Python on an environment where this codec is + not installed. ''' - def __init__(self, value): - self.value = value + def __new__(cls, *more): + return str.__new__(cls, *more) - def __str__(self): - return self.value - - def __len__(self): - return len(self.value) - - def encode(self, encoding): + def encode(self, encoding=None, errors=None): if encoding == 'shiftjis': raise LookupError("unknown encoding: shiftjis") - return self.value.encode(encoding) + return self.encode(encoding, errors) def test_constructing_without_shiftjis_encoding_available():