def test_pythontypes(self): # check all types defined in Python/ h = self.header vh = self.vheader size = self.calcsize check = self.check_sizeof # _ast.AST if not test.test_support.due_to_ironpython_bug( "http://ironpython.codeplex.com/WorkItem/View.aspx?WorkItemId=21088" ): import _ast check(_ast.AST(), size(h + '')) # imp.NullImporter import imp check(imp.NullImporter(self.file.name), size(h + '')) try: raise TypeError except TypeError: tb = sys.exc_info()[2] # traceback if tb != None: check(tb, size(h + '2P2i')) # symtable entry # XXX # sys.flags check(sys.flags, size(vh) + self.P * len(sys.flags))
def test_pythontypes(self): # check all types defined in Python/ size = test.test_support.calcobjsize vsize = test.test_support.calcvobjsize check = self.check_sizeof # _ast.AST import _ast check(_ast.AST(), size('')) # imp.NullImporter import imp f = open(test.test_support.TESTFN, 'wb') try: check(imp.NullImporter(f.name), size('')) finally: f.close() test.test_support.unlink(test.test_support.TESTFN) try: raise TypeError except TypeError: tb = sys.exc_info()[2] # traceback if tb != None: check(tb, size('2P2i')) # symtable entry # XXX # sys.flags check(sys.flags, vsize('') + self.P * len(sys.flags))
def _get_importer(path_name): """Python version of PyImport_GetImporter C API function""" cache = sys.path_importer_cache try: importer = cache[path_name] except KeyError: # Not yet cached. Flag as using the # standard machinery until we finish # checking the hooks cache[path_name] = None for hook in sys.path_hooks: try: importer = hook(path_name) break except ImportError: pass else: # The following check looks a bit odd. The trick is that # NullImporter throws ImportError if the supplied path is a # *valid* directory entry (and hence able to be handled # by the standard import machinery) try: importer = imp.NullImporter(path_name) except ImportError: return None cache[path_name] = importer return importer
def test_pickle_nullimporter(): n1 = imp.NullImporter("__test__") s = pf.pickle_object(n1) n2 = pf.unpickle_object(s) assert type(n1) == type(n2) assert n2.find_module() == None print "imp.NullImporter pickling OK!"
def test_pythontypes(self): # check all types defined in Python/ size = test.test_support.calcobjsize vsize = test.test_support.calcvobjsize check = self.check_sizeof # imp.NullImporter import imp check(imp.NullImporter(self.file.name), size('3P')) try: raise TypeError except TypeError: tb = sys.exc_info()[2] # traceback if tb != None: check(tb, size('2P2i'))
def test_pythontypes(self): # check all types defined in Python/ size = test.support.calcobjsize vsize = test.support.calcvobjsize check = self.check_sizeof # _ast.AST import _ast check(_ast.AST(), size('')) # imp.NullImporter import imp check(imp.NullImporter(self.file.name), size('')) try: raise TypeError except TypeError: tb = sys.exc_info()[2] # traceback if tb != None: check(tb, size('2P2i')) # symtable entry # XXX # sys.flags check(sys.flags, vsize('') + self.P * len(sys.flags))
def _get_importer(path_name): cache = sys.path_importer_cache try: importer = cache[path_name] except KeyError: cache[path_name] = None for hook in sys.path_hooks: try: importer = hook(path_name) break except ImportError: pass else: try: importer = imp.NullImporter(path_name) except ImportError: return cache[path_name] = importer return importer
def _get_importer(path_name): """Python version of PyImport_GetImporter C API function""" cache = sys.path_importer_cache try: importer = cache[path_name] except KeyError: cache[path_name] = None for hook in sys.path_hooks: try: importer = hook(path_name) break except ImportError: pass else: try: importer = imp.NullImporter(path_name) except ImportError: return cache[path_name] = importer return importer
def test_method_lacking(self): # There should be no issues if the method is not defined. key = 'gobbledeegook' sys.path_importer_cache[key] = imp.NullImporter('abc') self.addCleanup(lambda: sys.path_importer_cache.__delitem__(key)) importlib.invalidate_caches() # Shouldn't trigger an exception.
def _getimporter(path): import os, imp if os.path.isdir(path): return None else: return imp.NullImporter(path)
def test_nullimporter(self): import os, imp importer = imp.NullImporter("path") assert importer.find_module(1) is None raises(ImportError, imp.NullImporter, os.getcwd())
import imp print len(imp.find_module("os")) e = imp.find_module("encodings") print e[0] m = imp.load_module("myenc", e[0], e[1], e[2]) print m.__name__ # Null Importer tests for a in (1, "", "/proc", "nonexisting_dir"): try: i = imp.NullImporter(a) print i.find_module("foo") except Exception as e: print e print imp.lock_held() imp.acquire_lock() print imp.lock_held() imp.release_lock() import os print "first load_source():" m1 = imp.load_source("import_target", os.path.join(os.path.dirname(__file__), "import_target.py")) print "second load_source():" m2 = imp.load_source("import_target", os.path.join(os.path.dirname(__file__), "import_target.py")) print m1 is m2 m = imp.new_module("My new module") print type(m), m, hasattr(m, "__file__") print imp.is_builtin("sys"), imp.is_frozen("sys") print imp.is_builtin("io"), imp.is_frozen("io")