Esempio n. 1
0
    def test_get_calling_module(self):
        self.assertEqual(_helpers.get_calling_module(), sys.argv[0])
        self.assertEqual(module_foo.get_module_name(),
                         'absl.flags.tests.module_foo')
        self.assertEqual(module_bar.get_module_name(),
                         'absl.flags.tests.module_bar')

        # We execute the following exec statements for their side-effect
        # (i.e., not raising an error).  They emphasize the case that not
        # all code resides in one of the imported modules: Python is a
        # really dynamic language, where we can dynamically construct some
        # code and execute it.
        code = ('from absl.flags import _helpers\n'
                'module_name = _helpers.get_calling_module()')
        exec(code)  # pylint: disable=exec-used

        # Next two exec statements executes code with a global environment
        # that is different from the global environment of any imported
        # module.
        exec(code, {})  # pylint: disable=exec-used
        # vars(self) returns a dictionary corresponding to the symbol
        # table of the self object.  dict(...) makes a distinct copy of
        # this dictionary, such that any new symbol definition by the
        # exec-ed code (e.g., import flags, module_name = ...) does not
        # affect the symbol table of self.
        exec(code, dict(vars(self)))  # pylint: disable=exec-used

        # Next test is actually more involved: it checks not only that
        # get_calling_module does not crash inside exec code, it also checks
        # that it returns the expected value: the code executed via exec
        # code is treated as being executed by the current module.  We
        # check it twice: first time by executing exec from the main
        # module, second time by executing it from module_bar.
        global_dict = {}
        exec(code, global_dict)  # pylint: disable=exec-used
        self.assertEqual(global_dict['module_name'], sys.argv[0])

        global_dict = {}
        module_bar.execute_code(code, global_dict)
        self.assertEqual(global_dict['module_name'],
                         'absl.flags.tests.module_bar')
Esempio n. 2
0
    def test_get_calling_module_with_iteritems_error(self):
        # This test checks that get_calling_module is using
        # sys.modules.items(), instead of .iteritems().
        orig_sys_modules = sys.modules

        # Mock sys.modules: simulates error produced by importing a module
        # in paralel with our iteration over sys.modules.iteritems().
        class SysModulesMock(dict):
            def __init__(self, original_content):
                dict.__init__(self, original_content)

            def iteritems(self):
                # Any dictionary method is fine, but not .iteritems().
                raise RuntimeError('dictionary changed size during iteration')

        sys.modules = SysModulesMock(orig_sys_modules)
        try:
            # _get_calling_module should still work as expected:
            self.assertEqual(_helpers.get_calling_module(), sys.argv[0])
            self.assertEqual(module_foo.get_module_name(),
                             'absl.flags.tests.module_foo')
        finally:
            sys.modules = orig_sys_modules