Use codecs.open in textfile_open() for Py2.7 so it can work more like open() in Py3, auto-converting to/from utf-8 and unicode. Use textfile_open in parts of the sphinx command that were having encoding errors and also in writeIfChanged().

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxPython/Phoenix/trunk@71564 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Robin Dunn
2012-05-25 20:43:48 +00:00
parent b41df0b779
commit 2011586fa0
4 changed files with 34 additions and 35 deletions

View File

@@ -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')

View File

@@ -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):
def write(self, text):
if isinstance(text, unicode):
text = text.encode('utf-8')
return StringIO.write(self, text)
# 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, str):
text = text.decode('utf-8')
return io.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
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')

View File

@@ -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()
#-----------------------------------------------------------------------

View File

@@ -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)'))