From 14390933f427a02361fd49ecbb3d6f763fafb978 Mon Sep 17 00:00:00 2001 From: Richard Mitchell Date: Wed, 15 Apr 2015 12:08:24 +0100 Subject: [PATCH 1/2] Fix reading README & CHANGES RST files when the system encoding is not UTF-8. By default open in text mdoe reads files in the system encoding (see https://github.com/python/cpython/blob/6f4d604990c4a22bc06fd044ad61fce331ac9f83/Modules/_io/_iomodule.c#L114), however in this case we know that the encoding of the files being read is UTF8, regardless of the platform on which the package is being installed. --- setup.py | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/setup.py b/setup.py index d5f1293..9bf0bb5 100755 --- a/setup.py +++ b/setup.py @@ -16,13 +16,9 @@ def find_version(*file_paths): raise RuntimeError("Unable to find version string.") -if sys.version_info[0] < 3: - def read(*parts): - return codecs.open(os.path.join(os.path.dirname(__file__), *parts)).read() - -else: - def read(*parts): - return open(os.path.join(os.path.dirname(__file__), *parts), 'r').read() +def read(*parts): + return codecs.open(os.path.join(os.path.dirname(__file__), *parts), + encoding='utf8').read() class UltraMagicString(object): From 7c4230dbced4d1725aaadd65f239be620cc0b51d Mon Sep 17 00:00:00 2001 From: Richard Mitchell Date: Wed, 15 Apr 2015 14:59:32 +0100 Subject: [PATCH 2/2] Encode metadata. --- setup.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/setup.py b/setup.py index 9bf0bb5..551ef6b 100755 --- a/setup.py +++ b/setup.py @@ -21,25 +21,38 @@ def read(*parts): encoding='utf8').read() +try: + bytes +except NameError: + bytes = str + + class UltraMagicString(object): ''' Taken from http://stackoverflow.com/questions/1162338/whats-the-right-way-to-use-unicode-metadata-in-setup-py ''' def __init__(self, value): + if not isinstance(value, bytes): + value = value.encode('utf8') self.value = value - def __str__(self): + def __bytes__(self): return self.value def __unicode__(self): return self.value.decode('UTF-8') + if sys.version_info[0] < 3: + __str__ = __bytes__ + else: + __str__ = __unicode__ + def __add__(self, other): - return UltraMagicString(self.value + str(other)) + return UltraMagicString(self.value + bytes(other)) def split(self, *args, **kw): - return self.value.split(*args, **kw) + return str(self).split(*args, **kw) long_description = UltraMagicString('\n\n'.join((