[Checkins] SVN: ZConfig/trunk/ZConfig/loader.py Shut up ResourceWarnings about an unclosed io.BufferedReader on Python 3.
Marius Gedminas
cvs-admin at zope.org
Thu Feb 14 10:51:59 UTC 2013
Log message for revision 129382:
Shut up ResourceWarnings about an unclosed io.BufferedReader on Python 3.
Also avoid the StringIO workaround on Python 2 where it's not necessary.
Changed:
U ZConfig/trunk/ZConfig/loader.py
-=-
Modified: ZConfig/trunk/ZConfig/loader.py
===================================================================
--- ZConfig/trunk/ZConfig/loader.py 2013-02-14 10:10:37 UTC (rev 129381)
+++ ZConfig/trunk/ZConfig/loader.py 2013-02-14 10:51:59 UTC (rev 129382)
@@ -122,9 +122,15 @@
# Python 2.1 raises a different error from Python 2.2+,
# so we catch both to make sure we detect the situation.
self._raise_open_error(url, str(e))
- # Python 3 support: file.read() returns bytes, so we convert it to an
- # StringIO.
- file = StringIO.StringIO(file.read().decode())
+ if sys.version_info[0] >= 3:
+ # Python 3 support: file.read() returns bytes, so we convert it
+ # to an StringIO. (Can't use io.TextIOWrapper because of
+ # http://bugs.python.org/issue16723 and probably other bugs)
+ try:
+ data = file.read().decode()
+ finally:
+ file.close()
+ file = StringIO.StringIO(data)
return self.createResource(file, url)
def _raise_open_error(self, url, message):
More information about the checkins
mailing list