Commit cac29acf authored by Jason R. Coombs's avatar Jason R. Coombs

Convert quiet to a context manager, so its comment is no longer complaining...

Convert quiet to a context manager, so its comment is no longer complaining and to simplify the code.
parent 8567ca65
...@@ -9,6 +9,7 @@ import tempfile ...@@ -9,6 +9,7 @@ import tempfile
import unittest import unittest
import unicodedata import unicodedata
import re import re
import contextlib
from setuptools.tests import environment, test_svn from setuptools.tests import environment, test_svn
from setuptools.tests.py26compat import skipIf from setuptools.tests.py26compat import skipIf
...@@ -41,13 +42,14 @@ else: ...@@ -41,13 +42,14 @@ else:
# Cannot use context manager because of Python 2.4 # Cannot use context manager because of Python 2.4
@contextlib.contextmanager
def quiet(): def quiet():
global old_stdout, old_stderr
old_stdout, old_stderr = sys.stdout, sys.stderr old_stdout, old_stderr = sys.stdout, sys.stderr
sys.stdout, sys.stderr = StringIO(), StringIO() sys.stdout, sys.stderr = StringIO(), StringIO()
try:
def unquiet(): yield
sys.stdout, sys.stderr = old_stdout, old_stderr finally:
sys.stdout, sys.stderr = old_stdout, old_stderr
# Fake byte literals for Python <= 2.5 # Fake byte literals for Python <= 2.5
...@@ -112,12 +114,8 @@ class TestSdistTest(unittest.TestCase): ...@@ -112,12 +114,8 @@ class TestSdistTest(unittest.TestCase):
cmd = sdist(dist) cmd = sdist(dist)
cmd.ensure_finalized() cmd.ensure_finalized()
# squelch output with quiet():
quiet()
try:
cmd.run() cmd.run()
finally:
unquiet()
manifest = cmd.filelist.files manifest = cmd.filelist.files
self.assertTrue(os.path.join('sdist_test', 'a.txt') in manifest) self.assertTrue(os.path.join('sdist_test', 'a.txt') in manifest)
...@@ -136,13 +134,10 @@ class TestSdistTest(unittest.TestCase): ...@@ -136,13 +134,10 @@ class TestSdistTest(unittest.TestCase):
filename = os.path.join('sdist_test', 'smörbröd.py') filename = os.path.join('sdist_test', 'smörbröd.py')
# Add UTF-8 filename and write manifest # Add UTF-8 filename and write manifest
quiet() with quiet():
try:
mm.run() mm.run()
mm.filelist.files.append(filename) mm.filelist.files.append(filename)
mm.write_manifest() mm.write_manifest()
finally:
unquiet()
manifest = open(mm.manifest, 'rbU') manifest = open(mm.manifest, 'rbU')
contents = manifest.read() contents = manifest.read()
...@@ -177,15 +172,12 @@ class TestSdistTest(unittest.TestCase): ...@@ -177,15 +172,12 @@ class TestSdistTest(unittest.TestCase):
filename = os.path.join(b('sdist_test'), b('smörbröd.py')) filename = os.path.join(b('sdist_test'), b('smörbröd.py'))
# Add filename and write manifest # Add filename and write manifest
quiet() with quiet():
try:
mm.run() mm.run()
u_filename = filename.decode('utf-8') u_filename = filename.decode('utf-8')
mm.filelist.files.append(u_filename) mm.filelist.files.append(u_filename)
# Re-write manifest # Re-write manifest
mm.write_manifest() mm.write_manifest()
finally:
unquiet()
manifest = open(mm.manifest, 'rbU') manifest = open(mm.manifest, 'rbU')
contents = manifest.read() contents = manifest.read()
...@@ -221,15 +213,12 @@ class TestSdistTest(unittest.TestCase): ...@@ -221,15 +213,12 @@ class TestSdistTest(unittest.TestCase):
filename = os.path.join(b('sdist_test'), LATIN1_FILENAME) filename = os.path.join(b('sdist_test'), LATIN1_FILENAME)
# Add filename with surrogates and write manifest # Add filename with surrogates and write manifest
quiet() with quiet():
try:
mm.run() mm.run()
u_filename = filename.decode('utf-8', 'surrogateescape') u_filename = filename.decode('utf-8', 'surrogateescape')
mm.filelist.append(u_filename) mm.filelist.append(u_filename)
# Re-write manifest # Re-write manifest
mm.write_manifest() mm.write_manifest()
finally:
unquiet()
manifest = open(mm.manifest, 'rbU') manifest = open(mm.manifest, 'rbU')
contents = manifest.read() contents = manifest.read()
...@@ -256,11 +245,8 @@ class TestSdistTest(unittest.TestCase): ...@@ -256,11 +245,8 @@ class TestSdistTest(unittest.TestCase):
cmd.ensure_finalized() cmd.ensure_finalized()
# Create manifest # Create manifest
quiet() with quiet():
try:
cmd.run() cmd.run()
finally:
unquiet()
# Add UTF-8 filename to manifest # Add UTF-8 filename to manifest
filename = os.path.join(b('sdist_test'), b('smörbröd.py')) filename = os.path.join(b('sdist_test'), b('smörbröd.py'))
...@@ -274,11 +260,8 @@ class TestSdistTest(unittest.TestCase): ...@@ -274,11 +260,8 @@ class TestSdistTest(unittest.TestCase):
# Re-read manifest # Re-read manifest
cmd.filelist.files = [] cmd.filelist.files = []
quiet() with quiet():
try:
cmd.read_manifest() cmd.read_manifest()
finally:
unquiet()
# The filelist should contain the UTF-8 filename # The filelist should contain the UTF-8 filename
if PY3: if PY3:
...@@ -296,11 +279,8 @@ class TestSdistTest(unittest.TestCase): ...@@ -296,11 +279,8 @@ class TestSdistTest(unittest.TestCase):
cmd.ensure_finalized() cmd.ensure_finalized()
# Create manifest # Create manifest
quiet() with quiet():
try:
cmd.run() cmd.run()
finally:
unquiet()
# Add Latin-1 filename to manifest # Add Latin-1 filename to manifest
filename = os.path.join(b('sdist_test'), LATIN1_FILENAME) filename = os.path.join(b('sdist_test'), LATIN1_FILENAME)
...@@ -314,15 +294,12 @@ class TestSdistTest(unittest.TestCase): ...@@ -314,15 +294,12 @@ class TestSdistTest(unittest.TestCase):
# Re-read manifest # Re-read manifest
cmd.filelist.files = [] cmd.filelist.files = []
quiet() with quiet():
try:
try: try:
cmd.read_manifest() cmd.read_manifest()
except UnicodeDecodeError: except UnicodeDecodeError:
e = sys.exc_info()[1] e = sys.exc_info()[1]
self.fail(e) self.fail(e)
finally:
unquiet()
# The Latin-1 filename should have been skipped # The Latin-1 filename should have been skipped
filename = filename.decode('latin-1') filename = filename.decode('latin-1')
...@@ -341,11 +318,8 @@ class TestSdistTest(unittest.TestCase): ...@@ -341,11 +318,8 @@ class TestSdistTest(unittest.TestCase):
filename = os.path.join(b('sdist_test'), b('smörbröd.py')) filename = os.path.join(b('sdist_test'), b('smörbröd.py'))
open(filename, 'w').close() open(filename, 'w').close()
quiet() with quiet():
try:
cmd.run() cmd.run()
finally:
unquiet()
if sys.platform == 'darwin': if sys.platform == 'darwin':
filename = decompose(filename) filename = decompose(filename)
...@@ -379,11 +353,8 @@ class TestSdistTest(unittest.TestCase): ...@@ -379,11 +353,8 @@ class TestSdistTest(unittest.TestCase):
open(filename, 'w').close() open(filename, 'w').close()
self.assertTrue(os.path.isfile(filename)) self.assertTrue(os.path.isfile(filename))
quiet() with quiet():
try:
cmd.run() cmd.run()
finally:
unquiet()
if PY3: if PY3:
#not all windows systems have a default FS encoding of cp1252 #not all windows systems have a default FS encoding of cp1252
......
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