Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8fbddf1

Browse files
committed
Merged revisions 79030-79032 via svnmerge from
svn+ssh://[email protected]/python/trunk ........ r79030 | florent.xicluna | 2010-03-17 20:05:04 +0100 (mer, 17 mar 2010) | 2 lines Cleanup in test_import and test_coding. ........ r79031 | florent.xicluna | 2010-03-17 20:15:56 +0100 (mer, 17 mar 2010) | 2 lines Cleanup some test cases using check_warnings and check_py3k_warnings. ........ r79032 | florent.xicluna | 2010-03-17 21:05:11 +0100 (mer, 17 mar 2010) | 2 lines Fix and check cgi module deprecation warnings. Revert an unwanted rename in test_import. ........
1 parent a85c3bd commit 8fbddf1

6 files changed

Lines changed: 100 additions & 126 deletions

File tree

Lib/test/test_cgi.py

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
from test.support import run_unittest
1+
from test.support import run_unittest, check_warnings
22
import cgi
33
import os
44
import sys
55
import tempfile
66
import unittest
77
from io import StringIO
8-
from warnings import catch_warnings, filterwarnings
98

109
class HackedSysModule:
1110
# The regression test will have real values in sys.argv, which
@@ -15,10 +14,6 @@ class HackedSysModule:
1514

1615
cgi.sys = HackedSysModule()
1716

18-
try:
19-
from io import StringIO
20-
except ImportError:
21-
from io import StringIO
2217

2318
class ComparableException:
2419
def __init__(self, err):
@@ -117,7 +112,7 @@ def gen_result(data, environ):
117112

118113
result = {}
119114
for k, v in dict(form).items():
120-
result[k] = type(v) is list and form.getlist(k) or v.value
115+
result[k] = isinstance(v, list) and form.getlist(k) or v.value
121116

122117
return result
123118

@@ -133,7 +128,7 @@ def test_strict(self):
133128

134129
env = {'QUERY_STRING': orig}
135130
fs = cgi.FieldStorage(environ=env)
136-
if type(expect) == type({}):
131+
if isinstance(expect, dict):
137132
# test dict interface
138133
self.assertEqual(len(expect), len(fs))
139134
self.assertEqual(norm(expect.keys()), norm(fs.keys()))
@@ -308,20 +303,16 @@ def testQSAndFormDataFile(self):
308303
self.assertEqual(result, v)
309304

310305
def test_deprecated_parse_qs(self):
311-
# this func is moved to urlparse, this is just a sanity check
312-
with catch_warnings():
313-
filterwarnings('ignore',
314-
'cgi.parse_qs is deprecated, use urllib.parse.parse_qs instead',
315-
DeprecationWarning)
306+
# this func is moved to urllib.parse, this is just a sanity check
307+
with check_warnings(('cgi.parse_qs is deprecated, use urllib.parse.'
308+
'parse_qs instead', DeprecationWarning)):
316309
self.assertEqual({'a': ['A1'], 'B': ['B3'], 'b': ['B2']},
317310
cgi.parse_qs('a=A1&b=B2&B=B3'))
318311

319312
def test_deprecated_parse_qsl(self):
320-
# this func is moved to urlparse, this is just a sanity check
321-
with catch_warnings():
322-
filterwarnings('ignore',
323-
'cgi.parse_qsl is deprecated, use urllib.parse.parse_qsl instead',
324-
DeprecationWarning)
313+
# this func is moved to urllib.parse, this is just a sanity check
314+
with check_warnings(('cgi.parse_qsl is deprecated, use urllib.parse.'
315+
'parse_qsl instead', DeprecationWarning)):
325316
self.assertEqual([('a', 'A1'), ('b', 'B2'), ('B', 'B3')],
326317
cgi.parse_qsl('a=A1&b=B2&B=B3'))
327318

Lib/test/test_coding.py

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
import test.support, unittest
3-
from test.support import TESTFN, unlink
3+
from test.support import TESTFN, unlink, unload
44
import os, sys
55

66
class CodingTest(unittest.TestCase):
@@ -17,9 +17,8 @@ def verify_bad_module(self, module_name):
1717

1818
path = os.path.dirname(__file__)
1919
filename = os.path.join(path, module_name + '.py')
20-
fp = open(filename, "rb")
21-
bytes = fp.read()
22-
fp.close()
20+
with open(filename, "rb") as fp:
21+
bytes = fp.read()
2322
self.assertRaises(SyntaxError, compile, bytes, filename, 'exec')
2423

2524
def test_exec_valid_coding(self):
@@ -30,9 +29,8 @@ def test_exec_valid_coding(self):
3029
def test_file_parse(self):
3130
# issue1134: all encodings outside latin-1 and utf-8 fail on
3231
# multiline strings and long lines (>512 columns)
33-
if TESTFN in sys.modules:
34-
del sys.modules[TESTFN]
35-
sys.path.insert(0, ".")
32+
unload(TESTFN)
33+
sys.path.insert(0, os.curdir)
3634
filename = TESTFN + ".py"
3735
f = open(filename, "w")
3836
try:
@@ -45,21 +43,20 @@ def test_file_parse(self):
4543
__import__(TESTFN)
4644
finally:
4745
f.close()
48-
unlink(TESTFN+".py")
49-
unlink(TESTFN+".pyc")
50-
sys.path.pop(0)
46+
unlink(filename)
47+
unlink(filename + "c")
48+
unload(TESTFN)
49+
del sys.path[0]
5150

5251
def test_error_from_string(self):
5352
# See http://bugs.python.org/issue6289
5453
input = "# coding: ascii\n\N{SNOWMAN}".encode('utf-8')
55-
try:
54+
with self.assertRaises(SyntaxError) as c:
5655
compile(input, "<string>", "exec")
57-
except SyntaxError as e:
58-
expected = "'ascii' codec can't decode byte 0xe2 in position 16: " \
59-
"ordinal not in range(128)"
60-
self.assertTrue(str(e).startswith(expected))
61-
else:
62-
self.fail("didn't raise")
56+
expected = "'ascii' codec can't decode byte 0xe2 in position 16: " \
57+
"ordinal not in range(128)"
58+
self.assertTrue(c.exception.args[0].startswith(expected))
59+
6360

6461
def test_main():
6562
test.support.run_unittest(CodingTest)

Lib/test/test_import.py

Lines changed: 59 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@
88
import stat
99
import sys
1010
import unittest
11-
import warnings
12-
from test.support import (unlink, TESTFN, unload, run_unittest,
13-
TestFailed, EnvironmentVarGuard, swap_attr, swap_item)
11+
from test.support import (unlink, TESTFN, unload, run_unittest, is_jython,
12+
check_warnings, EnvironmentVarGuard, swap_attr, swap_item)
1413

1514

1615
def remove_files(name):
@@ -19,12 +18,15 @@ def remove_files(name):
1918
name + ".pyo",
2019
name + ".pyw",
2120
name + "$py.class"):
22-
if os.path.exists(f):
23-
os.remove(f)
21+
unlink(f)
2422

2523

2624
class ImportTests(unittest.TestCase):
2725

26+
def tearDown(self):
27+
unload(TESTFN)
28+
setUp = tearDown
29+
2830
def test_case_sensitivity(self):
2931
# Brief digression to test that import is case-sensitive: if we got
3032
# this far, we know for sure that "random" exists.
@@ -45,7 +47,7 @@ def test_with_extension(ext):
4547
# The extension is normally ".py", perhaps ".pyw".
4648
source = TESTFN + ext
4749
pyo = TESTFN + ".pyo"
48-
if sys.platform.startswith('java'):
50+
if is_jython:
4951
pyc = TESTFN + "$py.class"
5052
else:
5153
pyc = TESTFN + ".pyc"
@@ -66,15 +68,15 @@ def test_with_extension(ext):
6668
except ImportError as err:
6769
self.fail("import from %s failed: %s" % (ext, err))
6870

69-
self.assertEquals(mod.a, a,
71+
self.assertEqual(mod.a, a,
7072
"module loaded (%s) but contents invalid" % mod)
71-
self.assertEquals(mod.b, b,
73+
self.assertEqual(mod.b, b,
7274
"module loaded (%s) but contents invalid" % mod)
7375
finally:
7476
unlink(source)
7577
unlink(pyc)
7678
unlink(pyo)
77-
del sys.modules[TESTFN]
79+
unload(TESTFN)
7880

7981
sys.path.insert(0, os.curdir)
8082
try:
@@ -100,21 +102,22 @@ def test_execute_bit_not_copied(self):
100102
fn = fname + 'c'
101103
if not os.path.exists(fn):
102104
fn = fname + 'o'
103-
if not os.path.exists(fn): raise TestFailed("__import__ did "
104-
"not result in creation of either a .pyc or .pyo file")
105+
if not os.path.exists(fn):
106+
self.fail("__import__ did not result in creation of "
107+
"either a .pyc or .pyo file")
105108
s = os.stat(fn)
106-
self.assertEquals(stat.S_IMODE(s.st_mode),
107-
stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH)
109+
self.assertEqual(stat.S_IMODE(s.st_mode),
110+
stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH)
108111
finally:
109112
os.umask(oldmask)
110113
remove_files(TESTFN)
111-
if TESTFN in sys.modules: del sys.modules[TESTFN]
114+
unload(TESTFN)
112115
del sys.path[0]
113116

114117
def test_imp_module(self):
115118
# Verify that the imp module can correctly load and find .py files
116119
import imp, os
117-
# XXX (ncoghlan): It would be nice to use test_support.CleanImport
120+
# XXX (ncoghlan): It would be nice to use support.CleanImport
118121
# here, but that breaks because the os module registers some
119122
# handlers in copy_reg on import. Since CleanImport doesn't
120123
# revert that registration, the module is left in a broken
@@ -144,7 +147,7 @@ def test_module_with_large_stack(self, module='longlist'):
144147
# Compile & remove .py file, we only need .pyc (or .pyo).
145148
with open(filename, 'r') as f:
146149
py_compile.compile(filename)
147-
os.unlink(filename)
150+
unlink(filename)
148151

149152
# Need to be able to load from current dir.
150153
sys.path.append('')
@@ -154,10 +157,8 @@ def test_module_with_large_stack(self, module='longlist'):
154157

155158
# Cleanup.
156159
del sys.path[-1]
157-
for ext in '.pyc', '.pyo':
158-
fname = module + ext
159-
if os.path.exists(fname):
160-
os.unlink(fname)
160+
unlink(filename + 'c')
161+
unlink(filename + 'o')
161162

162163
def test_failing_import_sticks(self):
163164
source = TESTFN + ".py"
@@ -171,15 +172,11 @@ def test_failing_import_sticks(self):
171172
del sys.modules[TESTFN]
172173
try:
173174
for i in [1, 2, 3]:
174-
try:
175-
mod = __import__(TESTFN)
176-
except ZeroDivisionError:
177-
if TESTFN in sys.modules:
178-
self.fail("damaged module in sys.modules on %i. try" % i)
179-
else:
180-
self.fail("was able to import a damaged module on %i. try" % i)
175+
self.assertRaises(ZeroDivisionError, __import__, TESTFN)
176+
self.assertNotIn(TESTFN, sys.modules,
177+
"damaged module in sys.modules on %i try" % i)
181178
finally:
182-
sys.path.pop(0)
179+
del sys.path[0]
183180
remove_files(TESTFN)
184181

185182
def test_import_name_binding(self):
@@ -210,8 +207,8 @@ def test_failing_reload(self):
210207
try:
211208
mod = __import__(TESTFN)
212209
self.assertIn(TESTFN, sys.modules)
213-
self.assertEquals(mod.a, 1, "module has wrong attribute values")
214-
self.assertEquals(mod.b, 2, "module has wrong attribute values")
210+
self.assertEqual(mod.a, 1, "module has wrong attribute values")
211+
self.assertEqual(mod.b, 2, "module has wrong attribute values")
215212

216213
# On WinXP, just replacing the .py file wasn't enough to
217214
# convince reload() to reparse it. Maybe the timestamp didn't
@@ -226,18 +223,17 @@ def test_failing_reload(self):
226223
self.assertRaises(ZeroDivisionError, imp.reload, mod)
227224
# But we still expect the module to be in sys.modules.
228225
mod = sys.modules.get(TESTFN)
229-
self.assertFalse(mod is None, "expected module to be in sys.modules")
226+
self.assertIsNot(mod, None, "expected module to be in sys.modules")
230227

231228
# We should have replaced a w/ 10, but the old b value should
232229
# stick.
233-
self.assertEquals(mod.a, 10, "module has wrong attribute values")
234-
self.assertEquals(mod.b, 2, "module has wrong attribute values")
230+
self.assertEqual(mod.a, 10, "module has wrong attribute values")
231+
self.assertEqual(mod.b, 2, "module has wrong attribute values")
235232

236233
finally:
237-
sys.path.pop(0)
234+
del sys.path[0]
238235
remove_files(TESTFN)
239-
if TESTFN in sys.modules:
240-
del sys.modules[TESTFN]
236+
unload(TESTFN)
241237

242238
def test_file_to_source(self):
243239
# check if __file__ points to the source file where available
@@ -255,19 +251,34 @@ def test_file_to_source(self):
255251
ext = mod.__file__[-4:]
256252
self.assertIn(ext, ('.pyc', '.pyo'))
257253
finally:
258-
sys.path.pop(0)
254+
del sys.path[0]
259255
remove_files(TESTFN)
260256
if TESTFN in sys.modules:
261257
del sys.modules[TESTFN]
262258

259+
def test_import_name_binding(self):
260+
# import x.y.z binds x in the current namespace.
261+
import test as x
262+
import test.support
263+
self.assertIs(x, test, x.__name__)
264+
self.assertTrue(hasattr(test.support, "__file__"))
265+
266+
# import x.y.z as w binds z as w.
267+
import test.support as y
268+
self.assertIs(y, test.support, y.__name__)
269+
270+
def test_import_initless_directory_warning(self):
271+
with check_warnings(('', ImportWarning)):
272+
# Just a random non-package directory we always expect to be
273+
# somewhere in sys.path...
274+
self.assertRaises(ImportError, __import__, "site-packages")
275+
263276
def test_import_by_filename(self):
264277
path = os.path.abspath(TESTFN)
265-
try:
278+
with self.assertRaises(ImportError) as c:
266279
__import__(path)
267-
except ImportError as err:
268-
pass
269-
else:
270-
self.fail("import by path didn't raise an exception")
280+
self.assertEqual("Import by filename is not supported.",
281+
c.exception.args[0])
271282

272283

273284
class PycRewritingTests(unittest.TestCase):
@@ -302,10 +313,9 @@ def tearDown(self):
302313
if self.orig_module is not None:
303314
sys.modules[self.module_name] = self.orig_module
304315
else:
305-
del sys.modules[self.module_name]
306-
for file_name in self.file_name, self.compiled_name:
307-
if os.path.exists(file_name):
308-
os.remove(file_name)
316+
unload(self.module_name)
317+
unlink(self.file_name)
318+
unlink(self.compiled_name)
309319
if os.path.exists(self.dir_name):
310320
shutil.rmtree(self.dir_name)
311321

@@ -406,11 +416,10 @@ def _test_UNC_path(self):
406416

407417

408418
class RelativeImportTests(unittest.TestCase):
419+
409420
def tearDown(self):
410-
try:
411-
del sys.modules["test.relimport"]
412-
except:
413-
pass
421+
unload("test.relimport")
422+
setUp = tearDown
414423

415424
def test_relimport_star(self):
416425
# This will import * from .test_import.

Lib/test/test_io.py

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,14 +1046,9 @@ def test_misbehaved_io(self):
10461046
self.assertRaises(IOError, bufio.write, b"abcdef")
10471047

10481048
def test_max_buffer_size_deprecation(self):
1049-
with support.check_warnings() as w:
1050-
warnings.simplefilter("always", DeprecationWarning)
1049+
with support.check_warnings(("max_buffer_size is deprecated",
1050+
DeprecationWarning)):
10511051
self.tp(self.MockRawIO(), 8, 12)
1052-
self.assertEqual(len(w.warnings), 1)
1053-
warning = w.warnings[0]
1054-
self.assertTrue(warning.category is DeprecationWarning)
1055-
self.assertEqual(str(warning.message),
1056-
"max_buffer_size is deprecated")
10571052

10581053

10591054
class CBufferedWriterTest(BufferedWriterTest):
@@ -1109,14 +1104,9 @@ def test_detach(self):
11091104
self.assertRaises(self.UnsupportedOperation, pair.detach)
11101105

11111106
def test_constructor_max_buffer_size_deprecation(self):
1112-
with support.check_warnings() as w:
1113-
warnings.simplefilter("always", DeprecationWarning)
1107+
with support.check_warnings(("max_buffer_size is deprecated",
1108+
DeprecationWarning)):
11141109
self.tp(self.MockRawIO(), self.MockRawIO(), 8, 12)
1115-
self.assertEqual(len(w.warnings), 1)
1116-
warning = w.warnings[0]
1117-
self.assertTrue(warning.category is DeprecationWarning)
1118-
self.assertEqual(str(warning.message),
1119-
"max_buffer_size is deprecated")
11201110

11211111
def test_constructor_with_not_readable(self):
11221112
class NotReadable(MockRawIO):

0 commit comments

Comments
 (0)