From 60161a370b9943af45628663abf552f93ed256d6 Mon Sep 17 00:00:00 2001 From: Cosimo Lupo Date: Mon, 21 Oct 2019 16:17:16 +0100 Subject: [PATCH] [emoji_builder|png] fix bytes vs str issues in py2.py3 --- third_party/color_emoji/emoji_builder.py | 26 ++++++-------- third_party/color_emoji/png.py | 43 ++++++++++-------------- 2 files changed, 27 insertions(+), 42 deletions(-) diff --git a/third_party/color_emoji/emoji_builder.py b/third_party/color_emoji/emoji_builder.py index 7f17c62f..61a3a392 100644 --- a/third_party/color_emoji/emoji_builder.py +++ b/third_party/color_emoji/emoji_builder.py @@ -26,6 +26,12 @@ from os import path from nototools import font_data + +try: + unichr # py2 +except NameError: + unichr = chr # py3 + def get_glyph_name_from_gsub (string, font, cmap_dict): ligatures = font['GSUB'].table.LookupList.Lookup[0].SubTable[0].ligatures first_glyph = cmap_dict[ord (string[0])] @@ -179,10 +185,7 @@ class CBDT: self.write (pixel) offset += stride - png_allowed_chunks = [ - "IHDR", "PLTE", "tRNS", "sRGB", "IDAT", "IEND", # Python2 - b"IHDR", b"PLTE", b"tRNS", b"sRGB", b"IDAT", b"IEND", # Python3 - ] + png_allowed_chunks = [b"IHDR", b"PLTE", b"tRNS", b"sRGB", b"IDAT", b"IEND"] def write_format17 (self, png): self.write_format17or18(png, False) @@ -444,10 +447,7 @@ By default they are dropped. def add_font_table (font, tag, data): tab = ttLib.tables.DefaultTable.DefaultTable (tag) - if sys.version_info >= (3, 0, 0): - tab.data = data - else: - tab.data = str(data) + tab.data = data font[tag] = tab def drop_outline_tables (font): @@ -498,19 +498,13 @@ By default they are dropped. if "_" in codes: pieces = codes.split ("_") cps = [int(code, 16) for code in pieces] - if sys.version_info >= (3, 0, 0): - uchars = "".join ([chr(cp) for cp in cps if not is_vs(cp)]) - else: - uchars = "".join ([unichr(cp) for cp in cps if not is_vs(cp)]) + uchars = "".join (unichr(cp) for cp in cps if not is_vs(cp)) else: cp = int(codes, 16) if is_vs(cp): print("ignoring unexpected vs input %04x" % cp) continue - if sys.version_info >= (3, 0, 0): - uchars = chr(cp) - else: - uchars = unichr(cp) + uchars = unichr(cp) img_files[uchars] = img_file if not img_files: raise Exception ("No image files found in '%s'." % glb) diff --git a/third_party/color_emoji/png.py b/third_party/color_emoji/png.py index f5d4c2d5..6e74500f 100644 --- a/third_party/color_emoji/png.py +++ b/third_party/color_emoji/png.py @@ -19,10 +19,13 @@ import struct import sys -if sys.version_info >= (3,0,0): # Python3 - from io import StringIO -else: - from StringIO import StringIO +from io import BytesIO + + +try: + basestring # py2 +except NameError: + basestring = str # py3 class PNG: @@ -31,7 +34,7 @@ class PNG: def __init__ (self, f): - if (isinstance(f, str) or isinstance(f, type(u''))): + if isinstance(f, basestring): f = open (f, 'rb') self.f = f @@ -48,10 +51,7 @@ class PNG: def data (self): self.seek (0) - if sys.version_info >= (3,0,0): # Python3 - return bytearray (self.f.read (), 'iso-8859-1') - else: - return bytearray (self.f.read ()) + return bytearray (self.f.read ()) class BadSignature (Exception): pass class BadChunk (Exception): pass @@ -76,7 +76,7 @@ class PNG: def read_IHDR (self): (chunk_type, chunk_data, crc) = self.read_chunk () - if chunk_type not in ("IHDR", b"IHDR"): + if chunk_type != b"IHDR": raise PNG.BadChunk # Width: 4 bytes # Height: 4 bytes @@ -102,24 +102,15 @@ class PNG: def filter_chunks (self, chunks): self.seek (0); - out = StringIO () - if sys.version_info >= (3,0,0): # Python3 - out.write (self.read_signature ().decode('iso-8859-1')) - else: - out.write (self.read_signature ()) + out = BytesIO () + out.write (self.read_signature ()) while True: chunk_type, chunk_data, crc = self.read_chunk () if chunk_type in chunks: - if sys.version_info >= (3,0,0): # Python3 - out.write (struct.pack (">I", len (chunk_data)).decode('iso-8859-1')) - out.write (chunk_type.decode('iso-8859-1')) - out.write (chunk_data.decode('iso-8859-1')) - out.write (crc.decode('iso-8859-1')) - else: - out.write (struct.pack (">I", len (chunk_data))) - out.write (chunk_type) - out.write (chunk_data) - out.write (crc) - if chunk_type in ("IEND", b"IEND"): + out.write (struct.pack (">I", len (chunk_data))) + out.write (chunk_type) + out.write (chunk_data) + out.write (crc) + if chunk_type == b"IEND": break return PNG (out)