Commit 41112f5a authored by Jason R. Coombs's avatar Jason R. Coombs

Use io.open for future compatibility and consistency

parent 1c7e97f9
......@@ -163,7 +163,7 @@ class build_py(orig.build_py, Mixin2to3):
with io.open(init_py, 'rb') as f:
contents = f.read()
if b'declare_namespace' not in f.read():
if b'declare_namespace' not in contents:
raise distutils.errors.DistutilsError(
"Namespace package problem: %s is a namespace package, but "
"its\n__init__.py does not call declare_namespace()! Please "
......
......@@ -3,6 +3,7 @@ from distutils import log
from distutils.errors import DistutilsError, DistutilsOptionError
import os
import glob
import io
from pkg_resources import Distribution, PathMetadata, normalize_path
from setuptools.command.easy_install import easy_install
......@@ -163,9 +164,8 @@ class develop(easy_install):
for script_name in self.distribution.scripts or []:
script_path = os.path.abspath(convert_path(script_name))
script_name = os.path.basename(script_path)
f = open(script_path, 'rU')
script_text = f.read()
f.close()
with io.open(script_path) as strm:
script_text = strm.read()
self.install_script(dist, script_name, script_text, script_path)
def install_wrapper_scripts(self, dist):
......
......@@ -10,6 +10,7 @@ import distutils.filelist
import os
import re
import sys
import io
try:
from setuptools_svn import svn_utils
......@@ -471,10 +472,9 @@ def get_pkg_info_revision():
# a subversion revision
#
if os.path.exists('PKG-INFO'):
f = open('PKG-INFO', 'rU')
with io.open('PKG-INFO') as f:
for line in f:
match = re.match(r"Version:.*-r(\d+)\s*$", line)
if match:
return int(match.group(1))
f.close()
return 0
......@@ -3,6 +3,7 @@ from distutils import log
import distutils.command.sdist as orig
import os
import sys
import io
from setuptools.compat import PY3
from setuptools.utils import cs_path_exists
......@@ -166,11 +167,8 @@ class sdist(orig.sdist):
if not os.path.isfile(self.manifest):
return False
fp = open(self.manifest, 'rbU')
try:
with io.open(self.manifest, 'rb') as fp:
first_line = fp.readline()
finally:
fp.close()
return (first_line !=
'# file GENERATED by distutils, do NOT edit\n'.encode())
......
......@@ -7,6 +7,7 @@ import sys
import tempfile
import unicodedata
import contextlib
import io
import pytest
......@@ -81,6 +82,11 @@ def decompose(path):
return path
def read_all_bytes(filename):
with io.open(filename, 'rb') as fp:
return fp.read()
class TestSdistTest:
def setup_method(self, method):
......@@ -172,9 +178,7 @@ class TestSdistTest:
mm.filelist.append(filename)
mm.write_manifest()
manifest = open(mm.manifest, 'rbU')
contents = manifest.read()
manifest.close()
contents = read_all_bytes(mm.manifest)
# The manifest should be UTF-8 encoded
u_contents = contents.decode('UTF-8')
......@@ -210,9 +214,7 @@ class TestSdistTest:
# Re-write manifest
mm.write_manifest()
manifest = open(mm.manifest, 'rbU')
contents = manifest.read()
manifest.close()
contents = read_all_bytes(mm.manifest)
# The manifest should be UTF-8 encoded
contents.decode('UTF-8')
......@@ -248,9 +250,7 @@ class TestSdistTest:
# Re-write manifest
mm.write_manifest()
manifest = open(mm.manifest, 'rbU')
contents = manifest.read()
manifest.close()
contents = read_all_bytes(mm.manifest)
# The manifest should be UTF-8 encoded
contents.decode('UTF-8')
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment