Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes | Static Public Attributes
test.test_import.PycRewritingTests Class Reference
Collaboration diagram for test.test_import.PycRewritingTests:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def setUp
def tearDown
def import_module
def test_basics
def test_incorrect_code_name
def test_module_without_source
def test_foreign_code

Public Attributes

 sys_path
 orig_module

Static Public Attributes

string module_name = "unlikely_module_name"
string module_source
tuple dir_name = os.path.abspath(TESTFN)
tuple file_name = os.path.join(dir_name, module_name)
tuple compiled_name = imp.cache_from_source(file_name)

Detailed Description

Definition at line 304 of file test_import.py.


Member Function Documentation

Definition at line 341 of file test_import.py.

00341 
00342     def import_module(self):
00343         ns = globals()
00344         __import__(self.module_name, ns, ns)
00345         return sys.modules[self.module_name]

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 323 of file test_import.py.

00323 
00324     def setUp(self):
00325         self.sys_path = sys.path[:]
00326         self.orig_module = sys.modules.pop(self.module_name, None)
00327         os.mkdir(self.dir_name)
00328         with open(self.file_name, "w") as f:
00329             f.write(self.module_source)
00330         sys.path.insert(0, self.dir_name)

Here is the caller graph for this function:

Definition at line 331 of file test_import.py.

00331 
00332     def tearDown(self):
00333         sys.path[:] = self.sys_path
00334         if self.orig_module is not None:
00335             sys.modules[self.module_name] = self.orig_module
00336         else:
00337             unload(self.module_name)
00338         unlink(self.file_name)
00339         unlink(self.compiled_name)
00340         rmtree(self.dir_name)

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 346 of file test_import.py.

00346 
00347     def test_basics(self):
00348         mod = self.import_module()
00349         self.assertEqual(mod.module_filename, self.file_name)
00350         self.assertEqual(mod.code_filename, self.file_name)
00351         self.assertEqual(mod.func_filename, self.file_name)
00352         del sys.modules[self.module_name]
00353         mod = self.import_module()
00354         self.assertEqual(mod.module_filename, self.file_name)
00355         self.assertEqual(mod.code_filename, self.file_name)
00356         self.assertEqual(mod.func_filename, self.file_name)

Here is the call graph for this function:

Definition at line 374 of file test_import.py.

00374 
00375     def test_foreign_code(self):
00376         py_compile.compile(self.file_name)
00377         with open(self.compiled_name, "rb") as f:
00378             header = f.read(8)
00379             code = marshal.load(f)
00380         constants = list(code.co_consts)
00381         foreign_code = test_main.__code__
00382         pos = constants.index(1)
00383         constants[pos] = foreign_code
00384         code = type(code)(code.co_argcount, code.co_kwonlyargcount,
00385                           code.co_nlocals, code.co_stacksize,
00386                           code.co_flags, code.co_code, tuple(constants),
00387                           code.co_names, code.co_varnames, code.co_filename,
00388                           code.co_name, code.co_firstlineno, code.co_lnotab,
00389                           code.co_freevars, code.co_cellvars)
00390         with open(self.compiled_name, "wb") as f:
00391             f.write(header)
00392             marshal.dump(code, f)
00393         mod = self.import_module()
00394         self.assertEqual(mod.constant.co_filename, foreign_code.co_filename)
00395 

Here is the call graph for this function:

Definition at line 357 of file test_import.py.

00357 
00358     def test_incorrect_code_name(self):
00359         py_compile.compile(self.file_name, dfile="another_module.py")
00360         mod = self.import_module()
00361         self.assertEqual(mod.module_filename, self.file_name)
00362         self.assertEqual(mod.code_filename, self.file_name)
00363         self.assertEqual(mod.func_filename, self.file_name)

Here is the call graph for this function:

Definition at line 364 of file test_import.py.

00364 
00365     def test_module_without_source(self):
00366         target = "another_module.py"
00367         py_compile.compile(self.file_name, dfile=target)
00368         os.remove(self.file_name)
00369         pyc_file = make_legacy_pyc(self.file_name)
00370         mod = self.import_module()
00371         self.assertEqual(mod.module_filename, pyc_file)
00372         self.assertEqual(mod.code_filename, target)
00373         self.assertEqual(mod.func_filename, target)

Here is the call graph for this function:


Member Data Documentation

tuple test.test_import.PycRewritingTests.compiled_name = imp.cache_from_source(file_name) [static]

Definition at line 321 of file test_import.py.

tuple test.test_import.PycRewritingTests.dir_name = os.path.abspath(TESTFN) [static]

Definition at line 319 of file test_import.py.

Definition at line 320 of file test_import.py.

string test.test_import.PycRewritingTests.module_name = "unlikely_module_name" [static]

Definition at line 309 of file test_import.py.

Initial value:
"""
import sys
code_filename = sys._getframe().f_code.co_filename
module_filename = __file__
constant = 1
def func():
pass
func_filename = func.__code__.co_filename
"""

Definition at line 310 of file test_import.py.

Definition at line 325 of file test_import.py.

Definition at line 324 of file test_import.py.


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