diff --git a/buildtools/config.py b/buildtools/config.py index 6d3b5074..cb1055d4 100644 --- a/buildtools/config.py +++ b/buildtools/config.py @@ -651,15 +651,15 @@ def writeIfChanged(filename, text): """ if os.path.exists(filename): - fid = codecs.open(filename, 'r', 'utf-8') - current = fid.read() - fid.close() + f = textfile_open(filename, 'rt') + current = f.read() + f.close() if current == text: return - f = codecs.open(filename, 'w', 'utf-8') - f.write(text.encode('utf-8')) + f = textfile_open(filename, 'wt') + f.write(text) f.close() @@ -777,12 +777,13 @@ def myExecfile(filename, ns): def textfile_open(filename, mode='rt'): """ - Simple wrapper around open() that will open normally on Python 2.x and on - Python 2.3 will add the encoding parameter. The mode parameter must - include the 't' to put the stream into text mode. + Simple wrapper around open() that will use codecs.open on Python 2.x and + on Python 2.3 will add the encoding parameter to the normal open(). The + mode parameter must include the 't' to put the stream into text mode. """ assert 't' in mode if sys.version_info < (3,): - return open(filename, mode) + import codecs + return codecs.open(filename, mode, encoding='utf-8') else: return open(filename, mode, encoding='utf-8') diff --git a/etgtools/generators.py b/etgtools/generators.py index 52a88662..6829ede4 100644 --- a/etgtools/generators.py +++ b/etgtools/generators.py @@ -80,35 +80,31 @@ def nci(text, numSpaces=0, stripLeading=True): #--------------------------------------------------------------------------- -if sys.version_info < (3,): - # For Python 2.x we'll convert any unicode text values to strings before - # adding them to the buffer - from StringIO import StringIO - class Utf8EncodingStream(StringIO): +# io.StringIO reads/writes unicode objects for both Python 2.7 and 3.x. For +# 2.7 we'll convert any string values to unicode objects before storing them +# in the StringIO + +import io +class Utf8EncodingStream(io.StringIO): + if sys.version_info < (3,): def write(self, text): - if isinstance(text, unicode): - text = text.encode('utf-8') - return StringIO.write(self, text) - -else: - # For Python 3.x we'll keep it all as str (unicode) objects and let the - # conversion to bytes happen when the text is written to the actual - # file. - from io import StringIO - class Utf8EncodingStream(StringIO): - pass - + if isinstance(text, str): + text = text.decode('utf-8') + return io.StringIO.write(self, text) + + def textfile_open(filename, mode='rt'): """ - Simple wrapper around open() that will open normally on Python 2.x and on - Python 2.3 will add the encoding parameter. The mode parameter must - include the 't' to put the stream into text mode. + Simple wrapper around open() that will use codecs.open on Python 2.x and + on Python 2.3 will add the encoding parameter to the normal open(). The + mode parameter must include the 't' to put the stream into text mode. """ assert 't' in mode if sys.version_info < (3,): - return open(filename, mode) + import codecs + return codecs.open(filename, mode, encoding='utf-8') else: return open(filename, mode, encoding='utf-8') diff --git a/etgtools/sip_generator.py b/etgtools/sip_generator.py index 74e36e2b..a1257f25 100644 --- a/etgtools/sip_generator.py +++ b/etgtools/sip_generator.py @@ -45,7 +45,9 @@ class SipWrapperGenerator(generators.WrapperGeneratorBase): # Write the contents of the stream to the destination file if not destFile: destFile = os.path.join(phoenixRoot, 'sip/gen', module.name + '.sip') - textfile_open(destFile, 'wt').write(stream.getvalue()) + f = textfile_open(destFile, 'wt') + f.write(stream.getvalue()) + f.close() #----------------------------------------------------------------------- diff --git a/sphinxtools/postprocess.py b/sphinxtools/postprocess.py index 418d4419..81b7c106 100644 --- a/sphinxtools/postprocess.py +++ b/sphinxtools/postprocess.py @@ -20,7 +20,7 @@ import subprocess # Phoenix-specific imports import templates -from buildtools.config import copyIfNewer, writeIfChanged, newer, getSvnRev +from buildtools.config import copyIfNewer, writeIfChanged, newer, getSvnRev, textfile_open from utilities import Wx2Sphinx from constants import HTML_REPLACE, TODAY, SPHINXROOT, SECTIONS_EXCLUDE @@ -112,7 +112,7 @@ def BuildEnumsAndMethods(sphinxDir): for input in textfiles: - fid = open(input, 'rt') + fid = textfile_open(input, 'rt') orig_text = text = fid.read() fid.close() @@ -166,7 +166,7 @@ def BuildEnumsAndMethods(sphinxDir): text = TooltipsOnInheritance(text, class_summary) if text != orig_text: - fid = open(input, 'wt') + fid = textfile_open(input, 'wt') fid.write(text) fid.close() @@ -185,7 +185,7 @@ def BuildEnumsAndMethods(sphinxDir): keys = unreferenced_classes.keys() keys.sort() - fid = open(os.path.join(SPHINXROOT, 'unreferenced_classes.inc'), 'wt') + fid = textfile_open(os.path.join(SPHINXROOT, 'unreferenced_classes.inc'), 'wt') fid.write('\n') fid.write('='*50 + ' ' + '='*50 + '\n') fid.write('%-50s %-50s\n'%('Reference', 'File Name(s)'))