Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes | Static Public Attributes | Private Member Functions
test.test_import.PycacheTests Class Reference

List of all members.

Public Member Functions

def setUp
def tearDown
def test_import_pyc_path
def test_unwritable_directory
def test_missing_source
def test_missing_source_legacy
def test___cached__
def test___cached___legacy_pyc
def test_package___cached__
def test_package___cached___from_pyc

Public Attributes

 source

Static Public Attributes

tuple tag = imp.get_tag()

Private Member Functions

def _clean

Detailed Description

Definition at line 508 of file test_import.py.


Member Function Documentation

def test.test_import.PycacheTests._clean (   self) [private]

Definition at line 513 of file test_import.py.

00513 
00514     def _clean(self):
00515         forget(TESTFN)
00516         rmtree('__pycache__')
00517         unlink(self.source)

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 518 of file test_import.py.

00518 
00519     def setUp(self):
00520         self.source = TESTFN + '.py'
00521         self._clean()
00522         with open(self.source, 'w') as fp:
00523             print('# This is a test file written by test_import.py', file=fp)
00524         sys.path.insert(0, os.curdir)

Here is the caller graph for this function:

Definition at line 525 of file test_import.py.

00525 
00526     def tearDown(self):
00527         assert sys.path[0] == os.curdir, 'Unexpected sys.path[0]'
00528         del sys.path[0]
00529         self._clean()

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 573 of file test_import.py.

00573 
00574     def test___cached__(self):
00575         # Modules now also have an __cached__ that points to the pyc file.
00576         m = __import__(TESTFN)
00577         pyc_file = imp.cache_from_source(TESTFN + '.py')
00578         self.assertEqual(m.__cached__, os.path.join(os.curdir, pyc_file))

Here is the call graph for this function:

Definition at line 579 of file test_import.py.

00579 
00580     def test___cached___legacy_pyc(self):
00581         # Like test___cached__() except that for backward compatibility,
00582         # when the pyc file lives where the py file would have been (and named
00583         # without the tag), it is importable.  The __cached__ of the imported
00584         # module is the pyc location.
00585         __import__(TESTFN)
00586         # pyc_file gets removed in _clean() via tearDown().
00587         pyc_file = make_legacy_pyc(self.source)
00588         os.remove(self.source)
00589         unload(TESTFN)
00590         m = __import__(TESTFN)
00591         self.assertEqual(m.__cached__,
00592                          os.path.join(os.curdir, os.path.relpath(pyc_file)))

Here is the call graph for this function:

Definition at line 530 of file test_import.py.

00530 
00531     def test_import_pyc_path(self):
00532         self.assertFalse(os.path.exists('__pycache__'))
00533         __import__(TESTFN)
00534         self.assertTrue(os.path.exists('__pycache__'))
00535         self.assertTrue(os.path.exists(os.path.join(
00536             '__pycache__', '{}.{}.py{}'.format(
00537             TESTFN, self.tag, __debug__ and 'c' or 'o'))))

Here is the call graph for this function:

Definition at line 549 of file test_import.py.

00549 
00550     def test_missing_source(self):
00551         # With PEP 3147 cache layout, removing the source but leaving the pyc
00552         # file does not satisfy the import.
00553         __import__(TESTFN)
00554         pyc_file = imp.cache_from_source(self.source)
00555         self.assertTrue(os.path.exists(pyc_file))
00556         os.remove(self.source)
00557         forget(TESTFN)
00558         self.assertRaises(ImportError, __import__, TESTFN)

Here is the call graph for this function:

Definition at line 559 of file test_import.py.

00559 
00560     def test_missing_source_legacy(self):
00561         # Like test_missing_source() except that for backward compatibility,
00562         # when the pyc file lives where the py file would have been (and named
00563         # without the tag), it is importable.  The __file__ of the imported
00564         # module is the pyc location.
00565         __import__(TESTFN)
00566         # pyc_file gets removed in _clean() via tearDown().
00567         pyc_file = make_legacy_pyc(self.source)
00568         os.remove(self.source)
00569         unload(TESTFN)
00570         m = __import__(TESTFN)
00571         self.assertEqual(m.__file__,
00572                          os.path.join(os.curdir, os.path.relpath(pyc_file)))

Here is the call graph for this function:

Definition at line 593 of file test_import.py.

00593 
00594     def test_package___cached__(self):
00595         # Like test___cached__ but for packages.
00596         def cleanup():
00597             rmtree('pep3147')
00598         os.mkdir('pep3147')
00599         self.addCleanup(cleanup)
00600         # Touch the __init__.py
00601         with open(os.path.join('pep3147', '__init__.py'), 'w'):
00602             pass
00603         with open(os.path.join('pep3147', 'foo.py'), 'w'):
00604             pass
00605         unload('pep3147.foo')
00606         unload('pep3147')
00607         m = __import__('pep3147.foo')
00608         init_pyc = imp.cache_from_source(
00609             os.path.join('pep3147', '__init__.py'))
00610         self.assertEqual(m.__cached__, os.path.join(os.curdir, init_pyc))
00611         foo_pyc = imp.cache_from_source(os.path.join('pep3147', 'foo.py'))
00612         self.assertEqual(sys.modules['pep3147.foo'].__cached__,
00613                          os.path.join(os.curdir, foo_pyc))

Here is the call graph for this function:

Definition at line 614 of file test_import.py.

00614 
00615     def test_package___cached___from_pyc(self):
00616         # Like test___cached__ but ensuring __cached__ when imported from a
00617         # PEP 3147 pyc file.
00618         def cleanup():
00619             rmtree('pep3147')
00620         os.mkdir('pep3147')
00621         self.addCleanup(cleanup)
00622         unload('pep3147.foo')
00623         unload('pep3147')
00624         # Touch the __init__.py
00625         with open(os.path.join('pep3147', '__init__.py'), 'w'):
00626             pass
00627         with open(os.path.join('pep3147', 'foo.py'), 'w'):
00628             pass
00629         m = __import__('pep3147.foo')
00630         unload('pep3147.foo')
00631         unload('pep3147')
00632         m = __import__('pep3147.foo')
00633         init_pyc = imp.cache_from_source(
00634             os.path.join('pep3147', '__init__.py'))
00635         self.assertEqual(m.__cached__, os.path.join(os.curdir, init_pyc))
00636         foo_pyc = imp.cache_from_source(os.path.join('pep3147', 'foo.py'))
00637         self.assertEqual(sys.modules['pep3147.foo'].__cached__,
00638                          os.path.join(os.curdir, foo_pyc))
00639 

Here is the call graph for this function:

Definition at line 540 of file test_import.py.

00540 
00541     def test_unwritable_directory(self):
00542         # When the umask causes the new __pycache__ directory to be
00543         # unwritable, the import still succeeds but no .pyc file is written.
00544         with temp_umask(0o222):
00545             __import__(TESTFN)
00546         self.assertTrue(os.path.exists('__pycache__'))
00547         self.assertFalse(os.path.exists(os.path.join(
00548             '__pycache__', '{}.{}.pyc'.format(TESTFN, self.tag))))

Here is the call graph for this function:


Member Data Documentation

Definition at line 519 of file test_import.py.

tuple test.test_import.PycacheTests.tag = imp.get_tag() [static]

Definition at line 511 of file test_import.py.


The documentation for this class was generated from the following file: