def _determine_import_context(self, globals): """Returns the context in which a module should be imported. The context could be a loaded (package) module and the imported module will be looked for within that package. The context could also be None, meaning there is no context -- the module should be looked for as a "top-level" module. """ if not globals or not globals.get('__importer__'): # globals does not refer to one of our modules or packages. That # implies there is no relative import context (as far as we are # concerned), and it should just pick it off the standard path. return None # The globals refer to a module or package of ours. It will define # the context of the new import. Get the module/package fqname. parent_fqname = globals['__name__'] # if a package is performing the import, then return itself (imports # refer to pkg contents) if globals['__ispkg__']: parent = sys.modules[parent_fqname] assert globals is parent.__dict__ return parent i = strop.rfind(parent_fqname, '.') # a module outside of a package has no particular import context if i == -1: return None # if a module in a package is performing the import, then return the # package (imports refer to siblings) parent_fqname = parent_fqname[:i] parent = sys.modules[parent_fqname] assert parent.__name__ == parent_fqname return parent
def test_rfind(self): self.assertTrue(strop.rfind("abcdefghiabc", "abc") == 9)
import warnings