def test_cp15803(self): _testdir = 'cp15803' pickle_file = os.path.join(self.test_dir, "cp15803_%d.pickle" % os.getpid()) module_txt = """ class K%s(object): static_member = 1 def __init__(self): self.member = 2 %s = K%s() """ _testdir_init = os.path.join(self.test_dir, _testdir, '__init__.py') self.write_to_file(_testdir_init, module_txt % ("", "FROM_INIT", "")) _testdir_mod = os.path.join(self.test_dir, _testdir, 'mod.py') self.write_to_file( _testdir_mod, module_txt % ("Mod", "FROM_MOD", "Mod") + """ from cp15803 import K FROM_INIT_IN_MOD = K() """) _testdir_sub = os.path.join(self.test_dir, _testdir, 'sub') _testdir_sub_init = os.path.join(_testdir_sub, '__init__.py') self.write_to_file(_testdir_sub_init) _testdir_sub_submod = os.path.join(_testdir_sub, 'submod.py') self.write_to_file( _testdir_sub_submod, module_txt % ("SubMod", "FROM_SUB_MOD", "SubMod") + """ from cp15803 import mod FROM_MOD_IN_SUBMOD = mod.KMod() """) try: with path_modifier(self.test_dir) as p: import cp15803 import cp15803.mod import cp15803.sub.submod import cp15803.sub.submod as newname for x in [ cp15803.K(), cp15803.FROM_INIT, cp15803.mod.KMod(), cp15803.mod.FROM_MOD, cp15803.mod.K(), cp15803.mod.FROM_INIT_IN_MOD, cp15803.sub.submod.KSubMod(), cp15803.sub.submod.FROM_SUB_MOD, cp15803.sub.submod.FROM_MOD_IN_SUBMOD, cp15803.sub.submod.mod.KMod(), cp15803.sub.submod.mod.FROM_MOD, cp15803.sub.submod.mod.K(), cp15803.sub.submod.mod.FROM_INIT_IN_MOD, newname.KSubMod(), newname.FROM_SUB_MOD, newname.FROM_MOD_IN_SUBMOD, newname.mod.KMod(), newname.mod.FROM_MOD, newname.mod.K(), newname.mod.FROM_INIT_IN_MOD, ]: with open(pickle_file, "wb") as f: cPickle.dump(x, f) with open(pickle_file, "rb") as f: x_unpickled = cPickle.load(f) self.assertEqual(x.__class__.__name__, x_unpickled.__class__.__name__) self.assertEqual(x.static_member, x_unpickled.static_member) self.assertEqual(x.member, x_unpickled.member) finally: try: os.unlink(pickle_file) self.clean_directory(os.path.join(self.test_dir, _testdir)) except: pass
def test_cp15803(): ''' ''' _testdir = 'cp15803' pickle_file = path_combine(testpath.public_testdir, "cp15803.pickle") module_txt = """ class K%s(object): static_member = 1 def __init__(self): self.member = 2 %s = K%s() """ _testdir_init = path_combine(testpath.public_testdir, _testdir, '__init__.py') write_to_file(_testdir_init, module_txt % ("", "FROM_INIT", "")) _testdir_mod = path_combine(testpath.public_testdir, _testdir, 'mod.py') write_to_file(_testdir_mod, module_txt % ("Mod", "FROM_MOD", "Mod") + """ from cp15803 import K FROM_INIT_IN_MOD = K() """ ) _testdir_sub = path_combine(testpath.public_testdir, _testdir, 'sub') _testdir_sub_init = path_combine(_testdir_sub, '__init__.py') write_to_file(_testdir_sub_init) _testdir_sub_submod = path_combine(_testdir_sub, 'submod.py') write_to_file(_testdir_sub_submod, module_txt % ("SubMod", "FROM_SUB_MOD", "SubMod") + """ from cp15803 import mod FROM_MOD_IN_SUBMOD = mod.KMod() """ ) import cp15803 import cp15803.mod import cp15803.sub.submod import cp15803.sub.submod as newname try: for x in [ cp15803.K(), cp15803.FROM_INIT, cp15803.mod.KMod(), cp15803.mod.FROM_MOD, cp15803.mod.K(), cp15803.mod.FROM_INIT_IN_MOD, cp15803.sub.submod.KSubMod(), cp15803.sub.submod.FROM_SUB_MOD, cp15803.sub.submod.FROM_MOD_IN_SUBMOD, cp15803.sub.submod.mod.KMod(), cp15803.sub.submod.mod.FROM_MOD, cp15803.sub.submod.mod.K(), cp15803.sub.submod.mod.FROM_INIT_IN_MOD, newname.KSubMod(), newname.FROM_SUB_MOD, newname.FROM_MOD_IN_SUBMOD, newname.mod.KMod(), newname.mod.FROM_MOD, newname.mod.K(), newname.mod.FROM_INIT_IN_MOD, ]: with open(pickle_file, "w") as f: cPickle.dump(x, f) with open(pickle_file, "r") as f: x_unpickled = cPickle.load(f) AreEqual(x.__class__.__name__, x_unpickled.__class__.__name__) AreEqual(x.static_member, x_unpickled.static_member) AreEqual(x.member, x_unpickled.member) finally: import nt try: nt.unlink(pickle_file) for f_name in [ _testdir_init, _testdir_mod, _testdir_sub_init, _testdir_sub_submod, ]: nt.unlink(f_name) if sys.platform=="win32": nt.unlink(f_name + "c") for dir_name in [ _testdir_sub, _testdir]: nt.rmdir(dir_name) except: pass