def testPatch(self): # verify obvious facts of object identity self.assert_(atestmodule.Bar is atestmodule.Sub.__bases__[0]) self.assert_(atestmodule.aFunc is atestmodule.foo[0]) moddict = self.olddict convert(atestmodule, {}) newdict = atestmodule.__dict__ L1 = moddict.keys() L2 = newdict.keys() L1.sort() L2.sort() self.assertEqual(L1, L2) self.assertEqual(atestmodule.__dict__, atestmodule.aFunc.func_globals) # make sure object identity is maintained by patch Bar = newdict["Bar"] Bar_as_base = newdict["Sub"].__bases__[0] self.assert_(Bar is Bar_as_base) self.assert_(newdict["aFunc"] is newdict["foo"][0]) # The patch should not touch modules, functions, etc. that # are imported from other modules. import ZODB.utils for name in dir(ZODB.utils): obj = getattr(ZODB.utils, name) if isinstance(obj, type) or isinstance(obj, function): self.assert_(obj is newdict[name])
def compileModule(module, registry, source): # Try to prevent compilation errors from files without trailing newlines. if source and source[-1] != "\n": source += "\n" module._p_changed = True moddict = module.__dict__ old_names = NameFinder(module) moddict[__persistent_module_registry__] = registry # XXX need to be able to replace sys.std{in,out,err} at this point exec source in moddict # XXX and restore them here. del moddict[__persistent_module_registry__] new_names = NameFinder(module) replacements = new_names.replacements(old_names) convert(module, replacements)