From 865b17d5cf8682cc77889ba26db36f486a6cf6f3 Mon Sep 17 00:00:00 2001 From: Hugo Date: Mon, 7 Oct 2019 15:34:12 +0300 Subject: [PATCH] Remove Python 2-compatibility code --- Tests/test_file_png.py | 4 +--- Tests/test_imagefont.py | 5 +---- src/PIL/ImageQt.py | 6 +----- src/PIL/PdfParser.py | 12 ++---------- src/PIL/TiffImagePlugin.py | 2 +- 5 files changed, 6 insertions(+), 23 deletions(-) diff --git a/Tests/test_file_png.py b/Tests/test_file_png.py index 3f5f003aca3..26c40ab44af 100644 --- a/Tests/test_file_png.py +++ b/Tests/test_file_png.py @@ -448,9 +448,7 @@ def test_nonunicode_text(self): self.assertIsInstance(im.info["Text"], str) def test_unicode_text(self): - # Check preservation of non-ASCII characters on Python 3 - # This cannot really be meaningfully tested on Python 2, - # since it didn't preserve charsets to begin with. + # Check preservation of non-ASCII characters def rt_text(value): im = Image.new("RGB", (32, 32)) diff --git a/Tests/test_imagefont.py b/Tests/test_imagefont.py index 6daac701baf..d2e24836e3f 100644 --- a/Tests/test_imagefont.py +++ b/Tests/test_imagefont.py @@ -463,10 +463,7 @@ def test_unicode_pilfont(self): with self.assertRaises(UnicodeEncodeError): font.getsize("’") - @unittest.skipIf( - sys.version.startswith("2") or hasattr(sys, "pypy_translation_info"), - "requires CPython 3.3+", - ) + @unittest.skipIf(hasattr(sys, "pypy_translation_info"), "requires CPython") def test_unicode_extended(self): # issue #3777 text = "A\u278A\U0001F12B" diff --git a/src/PIL/ImageQt.py b/src/PIL/ImageQt.py index ce34b0a043a..f19ad3df989 100644 --- a/src/PIL/ImageQt.py +++ b/src/PIL/ImageQt.py @@ -65,11 +65,7 @@ def fromqimage(im): im.save(buffer, "ppm") b = BytesIO() - try: - b.write(buffer.data()) - except TypeError: - # workaround for Python 2 - b.write(str(buffer.data())) + b.write(buffer.data()) buffer.close() b.seek(0) diff --git a/src/PIL/PdfParser.py b/src/PIL/PdfParser.py index 6e054af0899..3267ee49152 100644 --- a/src/PIL/PdfParser.py +++ b/src/PIL/PdfParser.py @@ -7,11 +7,6 @@ import time import zlib -try: - from UserDict import UserDict # Python 2.x -except ImportError: - UserDict = collections.UserDict # Python 3.x - def make_bytes(s): return s.encode("us-ascii") @@ -256,13 +251,10 @@ def __bytes__(self): __str__ = __bytes__ -class PdfDict(UserDict): +class PdfDict(collections.UserDict): def __setattr__(self, key, value): if key == "data": - if hasattr(UserDict, "__setattr__"): - UserDict.__setattr__(self, key, value) - else: - self.__dict__[key] = value + collections.UserDict.__setattr__(self, key, value) else: self[key.encode("us-ascii")] = value diff --git a/src/PIL/TiffImagePlugin.py b/src/PIL/TiffImagePlugin.py index 59d323fc26d..1094a6072e2 100644 --- a/src/PIL/TiffImagePlugin.py +++ b/src/PIL/TiffImagePlugin.py @@ -1027,7 +1027,7 @@ def _seek(self, frame): "Seeking to frame %s, on frame %s, __next %s, location: %s" % (frame, self.__frame, self.__next, self.fp.tell()) ) - # reset python3 buffered io handle in case fp + # reset buffered io handle in case fp # was passed to libtiff, invalidating the buffer self.fp.tell() self.fp.seek(self.__next)