Skip to content

Commit

Permalink
Merge pull request #48 from isotoma/fix-setup-py3k
Browse files Browse the repository at this point in the history
Fix reading README & CHANGES RST files when the system encoding is not UTF-8
  • Loading branch information
gregmuellegger committed Apr 16, 2015
2 parents eeac4ee + 7c4230d commit 712d4d6
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ 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()
def read(*parts):
return codecs.open(os.path.join(os.path.dirname(__file__), *parts),
encoding='utf8').read()

else:
def read(*parts):
return open(os.path.join(os.path.dirname(__file__), *parts), 'r').read()

try:
bytes
except NameError:
bytes = str


class UltraMagicString(object):
Expand All @@ -31,19 +33,26 @@ class UltraMagicString(object):
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((
Expand Down

0 comments on commit 712d4d6

Please sign in to comment.