88import stat
99import sys
1010import 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
1615def 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
2624class 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
273284class 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
408418class 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.
0 commit comments