Example #1
0
def _unpickle_node(node_name, modulename, is_bound, vardata=None):
    """returns an MDFNode object from the pickled results of _pickle_node"""
    # make sure all the required module is imported
    _log.debug("Unpickling node %s from module %s" % (node_name, modulename))
    if modulename:
        if modulename not in sys.modules:

            # TODO: we have to reference __builtin__ for now due to the way the import hook works
            # in the cluster. Get rid of it once a more stable import hook is implemented.
            __import__(modulename)

    # get the node and return it (there's no state to recover)
    try:
        return _get_node(node_name, is_bound)
    except MissingNodeError:
        if not vardata:
            raise

    # if the above block didn't return or raise then it must be a varnode
    # that hasn't been created (possibly one that was originally dynamically
    # created) so re-create it now.
    _log.debug("Creating new varnode node %s" % node_name)
    return MDFVarNode(name=node_name,
                      fqname=node_name,
                      default=vardata["default"],
                      category=vardata["categories"],
                      modulename=modulename)
Example #2
0
def _unpickle_node(node_name, modulename, is_bound, vardata=None):
    """returns an MDFNode object from the pickled results of _pickle_node"""
    # make sure all the required module is imported
    _log.debug("Unpickling node %s from module %s" % (node_name, modulename))
    if modulename:
        if modulename not in sys.modules:

            # TODO: we have to reference __builtin__ for now due to the way the import hook works
            # in the cluster. Get rid of it once a more stable import hook is implemented.
            __import__(modulename)

    # get the node and return it (there's no state to recover)
    try:
        return _get_node(node_name, is_bound)
    except MissingNodeError:
        if not vardata:
            raise

    # if the above block didn't return or raise then it must be a varnode
    # that hasn't been created (possibly one that was originally dynamically
    # created) so re-create it now.
    _log.debug("Creating new varnode node %s" % node_name)
    return MDFVarNode(name=node_name,
                      fqname=node_name,
                      default=vardata["default"],
                      category=vardata["categories"],
                      modulename=modulename)
Example #3
0
def __import_module_by_name(module_name, file_=sys.stdout, verbose=True):
    try:
        builtins.__import__(module_name)
        return sys.modules.get(module_name, None)
    except Exception as err:
        if verbose:
            file_.write(str(err))
        return
Example #4
0
def __import_module_by_name(module_name, file_=sys.stdout, verbose=True):
    try:
        builtins.__import__(module_name)
        return sys.modules.get(module_name, None)
    except Exception as err:
        if verbose:
            file_.write(str(err))
        return
Example #5
0
def find_class(path, class_name):
    builtins.__import__(path)
    module = sys.modules[path]
    cls = module
    try:
        for attr in class_name.split('.'):
            cls = getattr(cls, attr)
    except AttributeError:
        logger.error('{} object has no attribute {}', cls, attr)
        return
    return cls
Example #6
0
def find_class(path, class_name):
    builtins.__import__(path)
    module = sys.modules[path]
    cls = module
    try:
        for attr in class_name.split('.'):
            cls = getattr(cls, attr)
    except AttributeError:
        logger.error('{} object has no attribute {}', cls, attr)
        return
    return cls
Example #7
0
def import_modules_by_path(_path):
    import builtins
    ignored_modules = set()
    error_count = 0
    for (module_name, module_fqn) in module_names_gen(_path):
        try:
            builtins.__import__(module_fqn)
            ignored_modules.add(module_fqn)
        except Exception:
            logger.exception("  Failure: '{0}' ({1})", module_name, module_fqn)
            error_count += 1
    return error_count
Example #8
0
def import_modules_by_path(_path):
    import builtins
    ignored_modules = set()
    error_count = 0
    for (module_name, module_fqn) in module_names_gen(_path):
        try:
            builtins.__import__(module_fqn)
            ignored_modules.add(module_fqn)
        except Exception:
            logger.exception("  Failure: '{0}' ({1})", module_name, module_fqn)
            error_count += 1
    return error_count
Example #9
0
def union_routes(dir, p=False):
    routes = []
    name_app = dir.split(os.path.sep)
    name_app = name_app[len(name_app) - 1]
    if p:
        builtins.__import__('routes', globals=globals())
    else:
        for name in os.listdir(dir):
            path = os.path.join(dir, name)
            if os.path.isdir ( path ) and os.path.isfile ( os.path.join( path, 'routes.py' )):
                name = name_app+'.'+path[len(dir)+1:]+'.routes'
                builtins.__import__(name, globals=globals())
Example #10
0
def union_routes(dir, p=False):
    routes = []
    name_app = dir.split(os.path.sep)
    name_app = name_app[len(name_app) - 1]
    if p:
        builtins.__import__('routes', globals=globals())
    else:
        for name in os.listdir(dir):
            path = os.path.join(dir, name)
            if os.path.isdir(path) and os.path.isfile(
                    os.path.join(path, 'routes.py')):
                name = name_app + '.' + path[len(dir) + 1:] + '.routes'
                builtins.__import__(name, globals=globals())
Example #11
0
    def test_module_utils_basic_import_syslog(self, mock_import):
        def _mock_import(name, *args, **kwargs):
            if name == 'syslog':
                raise ImportError
            return realimport(name, *args, **kwargs)

        self.clear_modules(['syslog', 'ansible.module_utils.basic'])
        mod = builtins.__import__('ansible.module_utils.basic')
        self.assertTrue(mod.module_utils.basic.HAS_SYSLOG)

        self.clear_modules(['syslog', 'ansible.module_utils.basic'])
        mock_import.side_effect = _mock_import
        mod = builtins.__import__('ansible.module_utils.basic')
        self.assertFalse(mod.module_utils.basic.HAS_SYSLOG)
Example #12
0
    def test_module_utils_basic_import_json(self, mock_import):
        def _mock_import(name, *args, **kwargs):
            if name == 'json':
                raise ImportError
            elif name == 'simplejson':
                return MagicMock()
            return realimport(name, *args, **kwargs)

        self.clear_modules(['json', 'ansible.module_utils.basic'])
        mod = builtins.__import__('ansible.module_utils.basic')

        self.clear_modules(['json', 'ansible.module_utils.basic'])
        mock_import.side_effect = _mock_import
        mod = builtins.__import__('ansible.module_utils.basic')
def run_init(config, ls):
    fsl = ls
    config_objs_dir = config.config_objs_dir
    # loading all modules handling changes from modules dir
    for im in tunnel_ctl_service.modules.__all__:
         if im == "__init__": continue
         builtins.__import__("tunnel_ctl_service.modules.%s" % im)
    # now loading state
    if fsl.isdir(config_objs_dir):
        for fname in fsl.listdir(config_objs_dir):
            if not fname.startswith("."): continue # all obj jsons
            data = fsl.readfile(config_objs_dir + '/' + fname)
            config = read_config_obj(json.loads(data))
            config_objs[fname] = config
    def test_module_utils_basic_import_syslog(self, mock_import):
        def _mock_import(name, *args, **kwargs):
            if name == 'syslog':
                raise ImportError
            return realimport(name, *args, **kwargs)

        self.clear_modules(['syslog', 'ansible.module_utils.basic'])
        mod = builtins.__import__('ansible.module_utils.basic')
        self.assertTrue(mod.module_utils.basic.HAS_SYSLOG)

        self.clear_modules(['syslog', 'ansible.module_utils.basic'])
        mock_import.side_effect = _mock_import
        mod = builtins.__import__('ansible.module_utils.basic')
        self.assertFalse(mod.module_utils.basic.HAS_SYSLOG)
    def test_module_utils_basic_import_json(self, mock_import):
        def _mock_import(name, *args, **kwargs):
            if name == 'json':
                raise ImportError
            elif name == 'simplejson':
                return MagicMock()
            return realimport(name, *args, **kwargs)

        self.clear_modules(['json', 'ansible.module_utils.basic'])
        mod = builtins.__import__('ansible.module_utils.basic')

        self.clear_modules(['json', 'ansible.module_utils.basic'])
        mock_import.side_effect = _mock_import
        mod = builtins.__import__('ansible.module_utils.basic')
Example #16
0
def find_class_by_name(name):
    parts = name.rsplit('.', 1)
    if len(parts) != 2:
        raise ValueError('Name {0} must be fully qualified'.format(name))
    (path, class_name) = parts
    builtins.__import__(path)
    module = sys.modules[path]
    if module is None:
        raise ValueError('Path {0} is not a valid module'.format(path))
    cls = vars(module).get(class_name)
    if cls is None:
        raise ValueError('Type {0} in module {1} does not exist'.format(class_name, path))
    if not isinstance(cls, type):
        raise ValueError('Name {0} is not a type'.format(name))
    return cls
Example #17
0
def test_module_by_name(module_name, modules_to_ignore, file_=sys.stdout, verbose=False):
    try:
        builtins.__import__(module_name)
        ans = test_module(sys.modules.get(module_name, None), modules_to_ignore, file_, verbose)
        if (ans is None or not ans.succeeded) and file_:
            if file_ is sys.stdout:
                ConsoleColor.change_color(ConsoleColor.RED)
            file_.write('ERROR in ' + module_name + '\n')
        return ans
    except:
        result = TestResult()
        result.add_error(module_name, ('Module import error', "Failed to import module '{0}'".format(module_name)))
        if file_:
            file_.write("Failed to import module '{0}'\n".format(module_name))
            file_.write(traceback.format_exc())
        return result
Example #18
0
def trace_import(name, globals=None, locals=None, fromlist=(), level=0):
    name_parts = name.split(".")
    for i in range(1, len(name_parts) + 1):
        parent = ".".join(name_parts[:i])
        if is_plugin(parent):
            deps.add_edge(current_plugin(), parent)
    return builtins.__import__(name, globals, locals, fromlist, level)
Example #19
0
def _vcheck():
    # ================================
    # Access command line arguments
    # ================================
    parser = _argparse.ArgumentParser(description=
            'Returns the hash and version of a git repository.')
    parser.add_argument('-V', action='version', version='%(prog)s v{}'.format(vcheck.__version__))
    parser.add_argument('-v', '--verbose', action='store_true',
            help='Verbose mode.')
    parser.add_argument('module',
            help='Module to check.')

    arg = parser.parse_args()

    mod = _builtins.__import__(arg.module)

    def printerr():
        e = _sys.exc_info()
        print(e[1].args[0])

    try:
        print('Hexsha is: {}'.format(hexsha(mod)))
    except:
        printerr()
        return

    try:
        print(version(mod))
    except:
        printerr()
    def __import__(name, globals={}, locals={}, fromlist=[], level=-1):
        """Compatibility definition for Python 2.4.

        Silently ignore the `level` argument missing in Python < 2.5.
        """
        # we need the level arg because the default changed in Python 3.3
        return builtins.__import__(name, globals, locals, fromlist)
Example #21
0
 def getPackageInstance(self, category, package):
     """return instance of class Package from package file"""
     fileName =  getFilename( category, package )
     pack = None
     mod = None
     if fileName.endswith(".py") and os.path.isfile(fileName):
         if not fileName in self._packageDict:
             EmergeDebug.debug("module to import: %s" % fileName, 2)
             if not os.path.isfile( fileName ):
                 try:
                     mod = builtins.__import__( fileName )
                 except ImportError as e:
                     EmergeDebug.warning('import failed for module %s: %s' % (fileName, str(e)))
                     mod =  None
             else:
                 modulename = os.path.basename( fileName )[:-3].replace('.', '_')
                 loader = importlib.machinery.SourceFileLoader(modulename, fileName)
                 try:
                     mod = loader.load_module()
                 except Exception as e:
                     raise PortageException("Failed to load file %s" % fileName, category, package, e)
             if not mod is None:
                 subpackage, package = getSubPackage( category, package )
                 self._CURRENT_MODULE  = ( fileName, category,subpackage, package, mod )
                 pack = mod.Package( )
                 self._packageDict[ fileName ] = pack
             else:
                 raise PortageException("Failed to find package", category, package)
         else:
             pack = self._packageDict[ fileName ]
         return pack
Example #22
0
def _vcheck():
    # ================================
    # Access command line arguments
    # ================================
    parser = _argparse.ArgumentParser(
        description='Returns the hash and version of a git repository.')
    parser.add_argument('-V',
                        action='version',
                        version='%(prog)s v{}'.format(vcheck.__version__))
    parser.add_argument('-v',
                        '--verbose',
                        action='store_true',
                        help='Verbose mode.')
    parser.add_argument('module', help='Module to check.')

    arg = parser.parse_args()

    mod = _builtins.__import__(arg.module)

    def printerr():
        e = _sys.exc_info()
        print(e[1].args[0])

    try:
        print('Hexsha is: {}'.format(hexsha(mod)))
    except:
        printerr()
        return

    try:
        print(version(mod))
    except:
        printerr()
Example #23
0
 def getPackageInstance(self, category, package):
     """return instance of class Package from package file"""
     fileName =  getFilename( category, package )
     pack = None
     mod = None
     if fileName.endswith(".py") and os.path.isfile(fileName):
         if not fileName in self._packageDict:
             utils.debug( "module to import: %s" % fileName, 2 )
             if not os.path.isfile( fileName ):
                 try:
                     mod = builtins.__import__( fileName )
                 except ImportError as e:
                     utils.warning( 'import failed for module %s: %s' % (fileName, str(e)) )
                     mod =  None
             else:
                 modulename = os.path.basename( fileName )[:-3].replace('.', '_')
                 loader = importlib.machinery.SourceFileLoader(modulename, fileName)
                 try:
                     mod = loader.load_module()
                 except Exception as e:
                     raise PortageException("Failed to load file %s" % fileName, category, package, e)
             if not mod is None:
                 subpackage, package = getSubPackage( category, package )
                 self._CURRENT_MODULE  = ( fileName, category,subpackage, package, mod )
                 pack = mod.Package( )
                 self._packageDict[ fileName ] = pack
             else:
                 raise PortageException("Failed to find package", category, package)
         else:
             pack = self._packageDict[ fileName ]
         return pack
Example #24
0
def run_tests(test_names):
    """Runs tests, returns the number of failures."""
    # unittest's discovery produces very misleading errors in some cases; if it tries to import
    # a module which imports other things that then fail, it reports 'module object has no
    # attribute <test name>' and swallows the original exception. Try to import them all first
    # so we get better error messages.
    for test_name in TEST_NAMES:
        __import__(test_name)
    suite = unittest.defaultTestLoader.loadTestsFromNames(TEST_NAMES)
    if test_names:
        suite = filter_suite(suite, test_names)
        if suite.countTestCases() == 0:
            raise Exception('No matching tests found')
    runner = xmlrunner.XMLTestRunner(output='test.results', outsuffix='')
    results = runner.run(suite)
    return len(results.errors) + len(results.failures)
Example #25
0
def __import__( module ): # pylint: disable=W0622
    utils.debug( "module to import: %s" % module, 2 )
    if not os.path.isfile( module ):
        try:
            return builtins.__import__( module )
        except ImportError as e:
            utils.warning( 'import failed for module %s: %s' % (module, str(e)) )
            return None
    else:
        sys.path.append( os.path.dirname( module ) )
        modulename = os.path.basename( module ).replace('.py', '')

        suff_index = None
        for suff in imp.get_suffixes():
            if suff[0] == ".py":
                suff_index = suff
                break

        if suff_index is None:
            utils.die("no .py suffix found")

        with open( module ) as fileHdl:
            try:
                return imp.load_module(  modulename.replace('.', '_'),
                fileHdl,module , suff_index )
            
            except ImportError as e:
                utils.warning( 'import failed for file %s: %s' % (module, e) )
                return None
Example #26
0
    def __import__(name, globals={}, locals={}, fromlist=[], level=-1):
        """Compatibility definition for Python 2.4.

        Silently ignore the `level` argument missing in Python < 2.5.
        """
        # we need the level arg because the default changed in Python 3.3
        return builtins.__import__(name, globals, locals, fromlist)
Example #27
0
def cython_import(filename, **kwds):
    """
    Compile a file containing Cython code, then import and return the
    module.  Raises an ``ImportError`` if anything goes wrong.

    INPUT:

    - ``filename`` - a string; name of a file that contains Cython
      code

    See the function :func:`sage.misc.cython.cython` for documentation
    for the other inputs.

    OUTPUT:

    - the module that contains the compiled Cython code.
    """
    name, build_dir = cython(filename, **kwds)

    oldpath = sys.path
    try:
        sys.path.append(build_dir)
        return builtins.__import__(name)
    finally:
        sys.path = oldpath
Example #28
0
def run_tests(test_names):
    """Runs tests, returns the number of failures."""
    # unittest's discovery produces very misleading errors in some cases; if it tries to import
    # a module which imports other things that then fail, it reports 'module object has no
    # attribute <test name>' and swallows the original exception. Try to import them all first
    # so we get better error messages.
    for test_name in TEST_NAMES:
        __import__(test_name)
    suite = unittest.defaultTestLoader.loadTestsFromNames(TEST_NAMES)
    if test_names:
        suite = filter_suite(suite, test_names)
        if suite.countTestCases() == 0:
            raise Exception('No matching tests found')
    runner = xmlrunner.XMLTestRunner(output='test.results', outsuffix='')
    results = runner.run(suite)
    return len(results.errors) + len(results.failures)
Example #29
0
def find_class_by_name(name):
    parts = name.rsplit('.', 1)
    if len(parts) != 2:
        raise ValueError('Name {0} must be fully qualified'.format(name))
    (path, class_name) = parts
    builtins.__import__(path)
    module = sys.modules[path]
    if module is None:
        raise ValueError('Path {0} is not a valid module'.format(path))
    cls = vars(module).get(class_name)
    if cls is None:
        raise ValueError('Type {0} in module {1} does not exist'.format(
            class_name, path))
    if not isinstance(cls, type):
        raise ValueError('Name {0} is not a type'.format(name))
    return cls
Example #30
0
    def test_module_utils_basic_import_literal_eval(self, mock_import):
        def _mock_import(name, *args, **kwargs):
            try:
                fromlist = kwargs.get('fromlist', args[2])
            except IndexError:
                fromlist = []
            if name == 'ast' and 'literal_eval' in fromlist:
                raise ImportError
            return realimport(name, *args, **kwargs)

        mock_import.side_effect = _mock_import
        self.clear_modules(['ast', 'ansible.module_utils.basic'])
        mod = builtins.__import__('ansible.module_utils.basic')
        self.assertEqual(mod.module_utils.basic.literal_eval("'1'"), "1")
        self.assertEqual(mod.module_utils.basic.literal_eval("1"), 1)
        self.assertEqual(mod.module_utils.basic.literal_eval("-1"), -1)
        self.assertEqual(mod.module_utils.basic.literal_eval("(1,2,3)"),
                         (1, 2, 3))
        self.assertEqual(mod.module_utils.basic.literal_eval("[1]"), [1])
        self.assertEqual(mod.module_utils.basic.literal_eval("True"), True)
        self.assertEqual(mod.module_utils.basic.literal_eval("False"), False)
        self.assertEqual(mod.module_utils.basic.literal_eval("None"), None)
        #self.assertEqual(mod.module_utils.basic.literal_eval('{"a": 1}'), dict(a=1))
        self.assertRaises(ValueError, mod.module_utils.basic.literal_eval,
                          "asdfasdfasdf")
Example #31
0
def param_objhook(obj):
    '''
    A custom JSON "decoder" which can recognize certain types of serialized python objects
    (django models, function calls, object constructors) and re-create the objects

    Parameters
    ----------
    obj : dict
        The deserialized JSON data in python dictionary form (after calling json.loads)

    Returns
    -------
    object
        If possible, a python object based on the JSON data is created. If not, the original dictionary
        is simply returned.

    '''
    from db.tracker import models
    if '__django_model__' in obj:
        model = getattr(models, obj['__django_model__'])
        return model(pk=obj['pk'])
    elif '__builtin__' in obj:
        func = getattr(builtins, obj['__builtin__'])
        return func(*obj['args'])
    elif '__class__' in obj:
        # look up the module
        mod = builtins.__import__(obj['__module__'],
                                  fromlist=[obj['__class__']])

        # get the class with the 'getattr' and then run the class constructor on the class data
        return getattr(mod, obj['__class__'])(obj['__dict__'])
    else:  # the type of object is unknown, just return the original dictionary
        return obj
Example #32
0
def _named_import(name):
    parts = name.split('.')
    assert(len(parts) >= 2)
    module = builtins.__import__(name)
    for m in parts[1:]:
        module = module.__dict__[m]
    module_name = parts[-1]
    _register_binding_module(module_name, module)
Example #33
0
def _named_import(name):
    parts = name.split('.')
    assert (len(parts) >= 2)
    module = builtins.__import__(name)
    for m in parts[1:]:
        module = module.__dict__[m]
    module_name = parts[-1]
    _register_binding_module(module_name, module)
Example #34
0
def trace_import(name: str, globals: Optional[Dict[str, Any]] = None, locals: Optional[Dict[str, Any]] = None,
    fromlist: Sequence[str] = (), level: int = 0) -> Any:
    name_parts = name.split(".")
    for i in range(1, len(name_parts) + 1):
        parent = ".".join(name_parts[:i])
        if is_plugin(parent):
            deps.add_edge(current_plugin(), parent)
            logger.debug("{} depends on {}".format(current_plugin(), parent))
    return builtins.__import__(name, globals, locals, fromlist, level)
Example #35
0
    def test_module_utils_basic_import_systemd_journal(self, mock_import):
        def _mock_import(name, *args, **kwargs):
            try:
                fromlist = kwargs.get('fromlist', args[2])
            except IndexError:
                fromlist = []
            if name == 'systemd' and 'journal' in fromlist:
                raise ImportError
            return realimport(name, *args, **kwargs)

        self.clear_modules(['systemd', 'ansible.module_utils.basic'])
        mod = builtins.__import__('ansible.module_utils.basic')
        self.assertTrue(mod.module_utils.basic.has_journal)

        self.clear_modules(['systemd', 'ansible.module_utils.basic'])
        mock_import.side_effect = _mock_import
        mod = builtins.__import__('ansible.module_utils.basic')
        self.assertFalse(mod.module_utils.basic.has_journal)
    def test_module_utils_basic_import_selinux(self, mock_import):
        def _mock_import(name, *args, **kwargs):
            if name == 'selinux':
                raise ImportError
            return realimport(name, *args, **kwargs)

        try:
            self.clear_modules(['selinux', 'ansible.module_utils.basic'])
            mod = builtins.__import__('ansible.module_utils.basic')
            self.assertTrue(mod.module_utils.basic.HAVE_SELINUX)
        except ImportError:
            # no selinux on test system, so skip
            pass

        self.clear_modules(['selinux', 'ansible.module_utils.basic'])
        mock_import.side_effect = _mock_import
        mod = builtins.__import__('ansible.module_utils.basic')
        self.assertFalse(mod.module_utils.basic.HAVE_SELINUX)
    def test_module_utils_basic_import_systemd_journal(self, mock_import):
        def _mock_import(name, *args, **kwargs):
            try:
                fromlist = kwargs.get('fromlist', args[2])
            except IndexError:
                fromlist = []
            if name == 'systemd' and 'journal' in fromlist:
                raise ImportError
            return realimport(name, *args, **kwargs)

        self.clear_modules(['systemd', 'ansible.module_utils.basic'])
        mod = builtins.__import__('ansible.module_utils.basic')
        self.assertTrue(mod.module_utils.basic.has_journal)

        self.clear_modules(['systemd', 'ansible.module_utils.basic'])
        mock_import.side_effect = _mock_import
        mod = builtins.__import__('ansible.module_utils.basic')
        self.assertFalse(mod.module_utils.basic.has_journal)
Example #38
0
    def test_module_utils_basic_import_selinux(self, mock_import):
        def _mock_import(name, *args, **kwargs):
            if name == 'selinux':
                raise ImportError
            return realimport(name, *args, **kwargs)

        try:
            self.clear_modules(['selinux', 'ansible.module_utils.basic'])
            mod = builtins.__import__('ansible.module_utils.basic')
            self.assertTrue(mod.module_utils.basic.HAVE_SELINUX)
        except ImportError:
            # no selinux on test system, so skip
            pass

        self.clear_modules(['selinux', 'ansible.module_utils.basic'])
        mock_import.side_effect = _mock_import
        mod = builtins.__import__('ansible.module_utils.basic')
        self.assertFalse(mod.module_utils.basic.HAVE_SELINUX)
Example #39
0
 def _import(self, module_name, *args, **kwargs):
     if any(re.match(m, module_name) for m in self._disallowed_modules):
         raise ImportError('Module not found')
     try:
             
         with add_paths(self._additional_paths):
             module = builtins.__import__(module_name, *args, **kwargs)
         return module
     except KeyError:
         raise ImportError('Module not found')
Example #40
0
def mock_import(name, *args, installed=False):
    """
    Mock mlflow import, installed mocks behaviour if mlflow is supposed to be installed or not
    """
    if name == 'mlflow':
        if not installed:
            raise ImportError
        else:
            return FakeMLFlow()
    else:
        return builtins.__import__(name, *args)
Example #41
0
def patch_module(reloadmodule: Any, mock_modulename: str, mock_module: Any) -> None:
    with mock.patch('builtins.__import__', side_effect=_import_mock(mock_modulename, mock_module)):
        try:
            # Try to load the mock
            module = builtins.__import__(mock_modulename)
            # If it is not the mock it was already loaded so we need a reload
            if mock_module != module:
                importlib.reload(module)
        except (ImportError, TypeError):
            pass
        # The mock is in place. Reload the module which imports the mock so that the mock is actually used
        importlib.reload(reloadmodule)
Example #42
0
def test_module_by_name(module_name,
                        modules_to_ignore,
                        file_=sys.stdout,
                        verbose=False):
    try:
        builtins.__import__(module_name)
        ans = test_module(sys.modules.get(module_name, None),
                          modules_to_ignore, file_, verbose)
        if not (ans is None or not ans.succeeded) and file_:
            if file_ is sys.stdout:
                ConsoleColor.change_color(ConsoleColor.RED)
            file_.write('ERROR in ' + module_name + '\n')
        return ans
    except:
        result = TestResult()
        result.add_error(module_name,
                         ('Module import error',
                          "Failed to import module '{0}'".format(module_name)))
        if file_:
            file_.write("Failed to import module '{0}'\n".format(module_name))
            file_.write(traceback.format_exc())
        return result
Example #43
0
def optional_import(module_name):
    """Import optional module by name.

    Returns a mock object if not found.
    """
    try:
        module = builtins.__import__(module_name)
        success = True
    except ModuleNotFoundError:
        module = NotFoundModule(module_name)
        success = False

    return module, success
Example #44
0
def _import(fn, globals):
    try:
        return __import__(fn, globals=globals)
    except ImportError:
        psilname = fn + ".psil"
        pycname = fn + ".pyc"
        try:
            psilstat = os.stat(psilname)
        except OSError:
            raise ImportError("no such module")
        timestamp = int(psilstat.st_mtime)
        try:
            pycstat = os.stat(pycname)
        except OSError:
            pycstat = None
        if pycstat is None or pycstat.st_mtime < timestamp:
            f = open(psilname)
            code = f.read()
            f.close()
            body = []
            t = interpreter.tokenise(code)
            while True:
                p = interpreter.parse(t)
                if p is None:
                    break
                p = interpreter.macroexpand_r(p)
                if p is None:
                    continue
                tree = psilc(p)
                body.append(tree)
            tree = ast.Module(body)
            ast.fix_missing_locations(tree)
            codeobject = compile(tree, psilname, "exec")

            fc = open(pycname, "wb")
            fc.write(b"\0\0\0\0")
            py_compile.wr_long(fc, timestamp)
            marshal.dump(codeobject, fc)
            fc.flush()
            fc.seek(0, 0)
            fc.write(py_compile.MAGIC)
            fc.close()
            py_compile.set_creator_type(pycname)

            return builtins.__import__(fn, globals=globals)
Example #45
0
def _import(fn, globals):
    try:
        return __import__(fn, globals=globals)
    except ImportError:
        psilname = fn + ".psil"
        pycname = fn + ".pyc"
        try:
            psilstat = os.stat(psilname)
        except OSError:
            raise ImportError("no such module")
        timestamp = int(psilstat.st_mtime)
        try:
            pycstat = os.stat(pycname)
        except OSError:
            pycstat = None
        if pycstat is None or pycstat.st_mtime < timestamp:
            f = open(psilname)
            code = f.read()
            f.close()
            body = []
            t = interpreter.tokenise(code)
            while True:
                p = interpreter.parse(t)
                if p is None:
                    break
                p = interpreter.macroexpand_r(p)
                if p is None:
                    continue
                tree = psilc(p)
                body.append(tree)
            tree = ast.Module(body)
            ast.fix_missing_locations(tree)
            codeobject = compile(tree, psilname, 'exec')

            fc = open(pycname, 'wb')
            fc.write(b'\0\0\0\0')
            py_compile.wr_long(fc, timestamp)
            marshal.dump(codeobject, fc)
            fc.flush()
            fc.seek(0, 0)
            fc.write(py_compile.MAGIC)
            fc.close()
            py_compile.set_creator_type(pycname)

            return builtins.__import__(fn, globals=globals)
Example #46
0
def new_import(name, global_dict=None, local_dict=None, fromlist=(), level=0):
    """
    Import a module. This function is meant for use by the
    Python interpreter.
    """
    if '.' in name:
        if name[0] == '.':
            name = '__main__' + name
        package = name.split('.')[0]
        raise ModuleNotFoundError(f"No module named '{name}'; '{package}' is not a package")
    elif name not in module_whitelist:
        raise ModuleNotFoundError(f"No module named '{name}'")
    module = builtins.__import__(name, global_dict, local_dict, fromlist, level)
    exports = module.__dict__.get('__all__') or module_exports.get(name) or filter(lambda x: x[0] != '_', dir(module))
    for attr in fromlist or ():
        if attr not in exports:
            raise ImportError(f"cannot import name '{attr}' from '{name}'")
    items = {name: getattr(module, name) for name in exports}
    return ModuleWrapper(name, items)
    def test_module_utils_basic_import_literal_eval(self, mock_import):
        def _mock_import(name, *args, **kwargs):
            try:
                fromlist = kwargs.get('fromlist', args[2])
            except IndexError:
                fromlist = []
            if name == 'ast' and 'literal_eval' in fromlist:
                raise ImportError
            return realimport(name, *args, **kwargs)

        mock_import.side_effect = _mock_import
        self.clear_modules(['ast', 'ansible.module_utils.basic'])
        mod = builtins.__import__('ansible.module_utils.basic')
        self.assertEqual(mod.module_utils.basic.literal_eval("'1'"), "1")
        self.assertEqual(mod.module_utils.basic.literal_eval("1"), 1)
        self.assertEqual(mod.module_utils.basic.literal_eval("-1"), -1)
        self.assertEqual(mod.module_utils.basic.literal_eval("(1,2,3)"), (1,2,3))
        self.assertEqual(mod.module_utils.basic.literal_eval("[1]"), [1])
        self.assertEqual(mod.module_utils.basic.literal_eval("True"), True)
        self.assertEqual(mod.module_utils.basic.literal_eval("False"), False)
        self.assertEqual(mod.module_utils.basic.literal_eval("None"), None)
        #self.assertEqual(mod.module_utils.basic.literal_eval('{"a": 1}'), dict(a=1))
        self.assertRaises(ValueError, mod.module_utils.basic.literal_eval, "asdfasdfasdf")
Example #48
0
 def process_action(self, action, opts, return_type, caller):
     """Invoke a single action and return the result.
     """
     if action == 'call_obj':
         obj = opts['obj']
         fnargs = opts['args']
         fnkwds = opts['kwargs']
         
         if len(fnkwds) == 0:  ## need to do this because some functions do not allow keyword arguments.
             try:
                 result = obj(*fnargs)
             except:
                 logger.warn("Failed to call object %s: %d, %s", obj, len(fnargs), fnargs[1:])
                 raise
         else:
             result = obj(*fnargs, **fnkwds)
         #logging.debug("    => call_obj result: %r", result)
     elif action == 'get_obj':
         result = opts['obj']
     elif action == 'delete':
         proxy_ref = self._proxy_refs[opts['obj_id']]
         proxy_ref[1].remove(opts['ref_id'])
         if len(proxy_ref[1]) == 0:
             del self._proxy_refs[opts['obj_id']]
             del self._proxy_id_map[id(proxy_ref[0])]
         result = None
     elif action =='getitem':
         result = self[opts['name']]
     elif action =='setitem':
         self[opts['name']] = opts['obj']
         result = None
     elif action == 'import':
         name = opts['module']
         fromlist = opts.get('fromlist', [])
         mod = builtins.__import__(name, fromlist=fromlist)
         
         if len(fromlist) == 0:
             parts = name.lstrip('.').split('.')
             result = mod
             for part in parts[1:]:
                 result = getattr(result, part)
         else:
             result = map(mod.__getattr__, fromlist)
     elif action == 'ping':
         result = 'pong'
     elif action == 'close':
         self._closed = True
         # Send a disconnect message to all known clients
         data = {}
         for client, ser_type in self._clients.items():
             if client == caller:
                 # We will send an actual return value to confirm closure
                 # to the caller.
                 continue
             
             # Select or generate the disconnect message that was serialized
             # correctly for this client.
             if ser_type not in data:
                 ser = self._serializers[ser_type]
                 data[ser_type] = ser.dumps({'action': 'disconnect'})
             data_str = data[ser_type]
             
             # Send disconnect message.
             logger.debug("RPC server sending disconnect message to %r", client)
             self._socket.send_multipart([client, data_str])
         RPCServer.unregister_server(self)
         result = True
     else:
         raise ValueError("Invalid RPC action '%s'" % action)
     
     return result
Example #49
0
def func4():
    print(dir(__import__('os')))  
Example #50
0
def importer(name, *x, **y):
    rootpkg = name.split(".")[0]
    if rootpkg not in ("runner",):  # TODO: module check
        return builtins.__import__(name, *x, **y)
    raise ImportError("Cannot import banned module")
#!/usr/bin/python3
from builtins import int, float, object, __import__

is_same_class = __import__('2-is_same_class').is_same_class

a = 1
if is_same_class(a, int):
    print("{} is an instance of the class {}".format(a, int.__name__))
if is_same_class(a, float):
    print("{} is an instance of the class {}".format(a, float.__name__))
if is_same_class(a, object):
    print("{} is an instance of the class {}".format(a, object.__name__))
Example #52
0
 def failing_import(name, *args, **kwargs):
     if name in removed_module_names:
         raise ImportError("Could not import {}".format(name))
     return builtins.__import__(name, *args, **kwargs)