Python six.moves.builtins() Examples

The following are 21 code examples of six.moves.builtins(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module six.moves , or try the search function .
Example #1
Source File: notebook_formatters.py    From tfx with Apache License 2.0 6 votes vote down vote up
def register_formatters():
  """Register HTML notebook formatters for TFX classes.

  This method registers HTML formatters for TFX classes for display in
  IPython / Jupyter / Colab notebooks. No action will be performed if called
  outside a notebook environment.
  """
  if getattr(builtins, '__IPYTHON__', None):
    # Skip registration if (1) IPython is not installed or (2) if IPython is
    # installed but we are not running in the notebook context (in this case,
    # get_ipython() returns None).
    try:
      ipython = __import__('IPython.core.getipython').get_ipython()
      if not ipython:
        return
    except ImportError:
      return
    html_formatter = ipython.display_formatter.formatters['text/html']
    for cls, formatter in FORMATTER_REGISTRY.items():
      html_formatter.for_type(cls, formatter.render) 
Example #2
Source File: reprconf.py    From Tautulli with GNU General Public License v3.0 6 votes vote down vote up
def build_Name(self, o):
        name = o.id
        if name == 'None':
            return None
        if name == 'True':
            return True
        if name == 'False':
            return False

        # See if the Name is a package or module. If it is, import it.
        try:
            return modules(name)
        except ImportError:
            pass

        # See if the Name is in builtins.
        try:
            import builtins
            return getattr(builtins, name)
        except AttributeError:
            pass

        raise TypeError('unrepr could not resolve the name %s' % repr(name)) 
Example #3
Source File: reprconf.py    From Tautulli with GNU General Public License v3.0 6 votes vote down vote up
def build_Name(self, o):
        name = o.name
        if name == 'None':
            return None
        if name == 'True':
            return True
        if name == 'False':
            return False

        # See if the Name is a package or module. If it is, import it.
        try:
            return modules(name)
        except ImportError:
            pass

        # See if the Name is in builtins.
        try:
            return getattr(builtins, name)
        except AttributeError:
            pass

        raise TypeError('unrepr could not resolve the name %s' % repr(name)) 
Example #4
Source File: _cpchecker.py    From Tautulli with GNU General Public License v3.0 6 votes vote down vote up
def _populate_known_types(self):
        b = [x for x in vars(builtins).values()
             if type(x) is type(str)]

        def traverse(obj, namespace):
            for name in dir(obj):
                # Hack for 3.2's warning about body_params
                if name == 'body_params':
                    continue
                vtype = type(getattr(obj, name, None))
                if vtype in b:
                    self.known_config_types[namespace + '.' + name] = vtype

        traverse(cherrypy.request, 'request')
        traverse(cherrypy.response, 'response')
        traverse(cherrypy.server, 'server')
        traverse(cherrypy.engine, 'engine')
        traverse(cherrypy.log, 'log') 
Example #5
Source File: test_base.py    From os-win with Apache License 2.0 6 votes vote down vote up
def setUp(self):
        super(OsWinBaseTestCase, self).setUp()

        self._mock_wmi = mock.MagicMock()
        baseutils.BaseUtilsVirt._old_wmi = self._mock_wmi

        mock_os = mock.MagicMock(Version='6.3.0')
        self._mock_wmi.WMI.return_value.Win32_OperatingSystem.return_value = (
            [mock_os])

        if os.name == 'nt':
            # The wmi module is expected to exist and by the time this runs,
            # the tested module will have imported it already.
            wmi_patcher = mock.patch('wmi.WMI', new=self._mock_wmi.WMI)
        else:
            # The wmi module doesn't exist, we'll have to "create" it.
            wmi_patcher = mock.patch.object(builtins, 'wmi', create=True,
                                            new=self._mock_wmi)
        wmi_patcher.start() 
Example #6
Source File: api_cache_test.py    From threat_intel with MIT License 6 votes vote down vote up
def _open_cache(self, initial_contents=None, update_cache=True):
        """Creates an ApiCache object, mocking the contents of the cache on disk.

        Args:
                initial_contents: A dict containing the initial contents of the cache
                update_cache: Specifies whether ApiCache should write out the
                              cache file when closing it
        Returns:
                ApiCache
        """
        if not initial_contents:
            initial_contents = {}

        file_contents = simplejson.dumps(initial_contents)
        mock_read = mock_open(read_data=file_contents)
        with patch.object(builtins, 'open', mock_read, create=True):
            api_cache = ApiCache(self._file_name, update_cache=update_cache)
            return api_cache 
Example #7
Source File: interactive_context.py    From tfx with Apache License 2.0 6 votes vote down vote up
def requires_ipython(fn):
  """Decorator for methods that can only be run in IPython."""

  @functools.wraps(fn)
  def run_if_ipython(*args, **kwargs):
    """Invokes `fn` if called from IPython, otherwise just emits a warning."""
    if getattr(builtins, '__IPYTHON__', None):
      # __IPYTHON__ variable is set by IPython, see
      # https://ipython.org/ipython-doc/rel-0.10.2/html/interactive/reference.html#embedding-ipython.
      return fn(*args, **kwargs)
    else:
      absl.logging.warning(
          'Method "%s" is a no-op when invoked outside of IPython.',
          fn.__name__)

  return run_if_ipython 
Example #8
Source File: test_multistore_filesystem.py    From glance_store with Apache License 2.0 6 votes vote down vote up
def _do_test_add_write_failure(self, errno, exception):
        filesystem.ChunkedFile.CHUNKSIZE = units.Ki
        image_id = str(uuid.uuid4())
        file_size = 5 * units.Ki  # 5K
        file_contents = b"*" * file_size
        path = os.path.join(self.test_dir, image_id)
        image_file = six.BytesIO(file_contents)

        with mock.patch.object(builtins, 'open') as popen:
            e = IOError()
            e.errno = errno
            popen.side_effect = e

            self.assertRaises(exception,
                              self.store.add,
                              image_id, image_file, 0)
            self.assertFalse(os.path.exists(path)) 
Example #9
Source File: test_filesystem_store.py    From glance_store with Apache License 2.0 6 votes vote down vote up
def _do_test_add_write_failure(self, errno, exception):
        filesystem.ChunkedFile.CHUNKSIZE = units.Ki
        image_id = str(uuid.uuid4())
        file_size = 5 * units.Ki  # 5K
        file_contents = b"*" * file_size
        path = os.path.join(self.test_dir, image_id)
        image_file = six.BytesIO(file_contents)

        with mock.patch.object(builtins, 'open') as popen:
            e = IOError()
            e.errno = errno
            popen.side_effect = e

            self.assertRaises(exception,
                              self.store.add,
                              image_id, image_file, 0, self.hash_algo)
            self.assertFalse(os.path.exists(path)) 
Example #10
Source File: raw_building.py    From linter-pylama with MIT License 6 votes vote down vote up
def _astroid_bootstrapping(astroid_builtin=None):
    """astroid boot strapping the builtins module"""
    # this boot strapping is necessary since we need the Const nodes to
    # inspect_build builtins, and then we can proxy Const
    if astroid_builtin is None:
        from six.moves import builtins
        astroid_builtin = Astroid_BUILDER.inspect_build(builtins)

    # pylint: disable=redefined-outer-name
    for cls, node_cls in node_classes.CONST_CLS.items():
        if cls is type(None):
            proxy = build_class('NoneType')
            proxy.parent = astroid_builtin
        elif cls is type(NotImplemented):
            proxy = build_class('NotImplementedType')
            proxy.parent = astroid_builtin
        else:
            proxy = astroid_builtin.getattr(cls.__name__)[0]
        if cls in (dict, list, set, tuple):
            node_cls._proxied = proxy
        else:
            _CONST_PROXY[cls] = proxy 
Example #11
Source File: api_cache_test.py    From threat_intel with MIT License 6 votes vote down vote up
def _close_cache(self, api_cache, cache_written=True):
        """Closes an ApiCache and reads the final contents that were written to disk.

        Args:
                api_cache: An ApiCache instance
                cache_written: Specifies whether it should test that the cache
                               was written out to the cache file or whether to
                               test that it was not written out
        Returns:
                A dict representing the contents of the cache that was written
                out to the cache file or `None` in case cache was not expected
                to be written out
        """
        mock_write = mock_open()
        with patch.object(builtins, 'open', mock_write, create=True) as patched_open:
            api_cache.close()

            if cache_written:
                return assert_cache_written(mock_write, patched_open)

            return assert_cache_not_written(mock_write) 
Example #12
Source File: test_pathutils.py    From compute-hyperv with Apache License 2.0 5 votes vote down vote up
def test_check_dir_exc(self, mock_check_create_dir):

        class FakeWindowsError(Exception):
            def __init__(self, winerror=None):
                self.winerror = winerror

        mock_check_create_dir.side_effect = FakeWindowsError(
            pathutils.ERROR_INVALID_NAME)
        with mock.patch.object(builtins, 'WindowsError',
                               FakeWindowsError, create=True):
            self.assertRaises(exception.AdminRequired,
                              self._pathutils.check_dir,
                              mock.sentinel.dir_name,
                              create_dir=True) 
Example #13
Source File: test_storage.py    From pypowervm with Apache License 2.0 5 votes vote down vote up
def test_upload_vopt_by_filepath(self, mock_create_file, mock_log_warn):
        """Tests the uploads of the virtual disks with an upload retry."""

        fake_file = self._fake_meta()
        fake_file.adapter.helpers = [vb.vios_busy_retry_helper]

        mock_create_file.return_value = fake_file
        self.adpt.upload_file.side_effect = [exc.Error("error"),
                                             object()]
        m = mock.mock_open()
        with mock.patch.object(builtins, 'open', m):
            v_opt, f_wrap = ts.upload_vopt(
                self.adpt, self.v_uuid, 'fake-path', 'test2', f_size=50)

        # Test that vopt was 'uploaded'
        self.adpt.upload_file.assert_called_with(mock.ANY, m(), helpers=[])
        self.assertIsNone(f_wrap)
        self.assertIsNotNone(v_opt)
        self.assertIsInstance(v_opt, stor.VOptMedia)
        self.assertEqual('test2', v_opt.media_name)

        # Validate that there was a warning log call and multiple executions
        # of the upload
        mock_log_warn.assert_called_once()
        self.assertEqual(2, self.adpt.upload_file.call_count)

        # Ensure cleanup was called twice since the first uploads fails.
        self.adpt.delete.assert_has_calls([mock.call(
            'File', service='web',
            root_id='6233b070-31cc-4b57-99bd-37f80e845de9')]*2) 
Example #14
Source File: test_helpers.py    From moler with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_instance_id_returns_id_in_hex_form_without_0x():
    from moler.helpers import instance_id
    from six.moves import builtins
    # 0xf0a1 == 61601 decimal
    with mock.patch.object(builtins, "id", return_value=61601):
        instance = "moler object"
        assert "0x" not in instance_id(instance)
        assert instance_id(instance) == "f0a1" 
Example #15
Source File: test_patroni.py    From patroni with MIT License 5 votes vote down vote up
def test_check_psycopg2(self):
        with patch.object(builtins, '__import__', Mock(side_effect=ImportError)):
            self.assertRaises(SystemExit, check_psycopg2)
        with patch('psycopg2.__version__', '2.5.3.dev1 a b c'):
            self.assertRaises(SystemExit, check_psycopg2) 
Example #16
Source File: test_wale_restore.py    From patroni with MIT License 5 votes vote down vote up
def test_get_major_version(self):
        with patch.object(builtins, 'open', mock_open(read_data='9.4')):
            self.assertEqual(get_major_version("data"), 9.4)
        with patch.object(builtins, 'open', side_effect=OSError):
            self.assertEqual(get_major_version("data"), 0.0) 
Example #17
Source File: test_postmaster.py    From patroni with MIT License 5 votes vote down vote up
def test_read_postmaster_pidfile(self):
        with patch.object(builtins, 'open', Mock(side_effect=IOError)):
            self.assertIsNone(PostmasterProcess.from_pidfile(''))
        with patch.object(builtins, 'open', mock_open(read_data='123\n')):
            self.assertIsNone(PostmasterProcess.from_pidfile('')) 
Example #18
Source File: test_rewind.py    From patroni with MIT License 5 votes vote down vote up
def test_read_postmaster_opts(self):
        m = mock_open(read_data='/usr/lib/postgres/9.6/bin/postgres "-D" "data/postgresql0" \
"--listen_addresses=127.0.0.1" "--port=5432" "--hot_standby=on" "--wal_level=hot_standby" \
"--wal_log_hints=on" "--max_wal_senders=5" "--max_replication_slots=5"\n')
        with patch.object(builtins, 'open', m):
            data = self.r.read_postmaster_opts()
            self.assertEqual(data['wal_level'], 'hot_standby')
            self.assertEqual(int(data['max_replication_slots']), 5)
            self.assertEqual(data.get('D'), None)

            m.side_effect = IOError
            data = self.r.read_postmaster_opts()
            self.assertEqual(data, dict()) 
Example #19
Source File: test_postgresql.py    From patroni with MIT License 5 votes vote down vote up
def test_write_postgresql_and_sanitize_auto_conf(self):
        read_data = 'primary_conninfo = foo\nfoo = bar\n'
        with open(os.path.join(self.p.data_dir, 'postgresql.auto.conf'), 'w') as f:
            f.write(read_data)

        mock_read_auto = mock_open(read_data=read_data)
        mock_read_auto.return_value.__iter__ = lambda o: iter(o.readline, '')
        with patch.object(builtins, 'open', Mock(side_effect=[mock_open()(), mock_read_auto(), IOError])),\
                patch('os.chmod', Mock()):
            self.p.config.write_postgresql_conf()

        with patch.object(builtins, 'open', Mock(side_effect=[mock_open()(), IOError])), patch('os.chmod', Mock()):
            self.p.config.write_postgresql_conf()
        self.p.config.write_recovery_conf({'foo': 'bar'})
        self.p.config.write_postgresql_conf() 
Example #20
Source File: raw_building.py    From linter-pylama with MIT License 5 votes vote down vote up
def imported_member(self, node, member, name):
        """verify this is not an imported class or handle it"""
        # /!\ some classes like ExtensionClass doesn't have a __module__
        # attribute ! Also, this may trigger an exception on badly built module
        # (see http://www.logilab.org/ticket/57299 for instance)
        try:
            modname = getattr(member, '__module__', None)
        except: # pylint: disable=bare-except
            _LOG.exception('unexpected error while building '
                           'astroid from living object')
            modname = None
        if modname is None:
            if (name in ('__new__', '__subclasshook__')
                    or (name in _BUILTINS and _JYTHON)):
                # Python 2.5.1 (r251:54863, Sep  1 2010, 22:03:14)
                # >>> print object.__new__.__module__
                # None
                modname = six.moves.builtins.__name__
            else:
                attach_dummy_node(node, name, member)
                return True

        real_name = {
            'gtk': 'gtk_gtk',
            '_io': 'io',
        }.get(modname, modname)

        if real_name != self._module.__name__:
            # check if it sounds valid and then add an import node, else use a
            # dummy node
            try:
                getattr(sys.modules[modname], name)
            except (KeyError, AttributeError):
                attach_dummy_node(node, name, member)
            else:
                attach_import_node(node, modname, name)
            return True
        return False


### astroid bootstrapping ###################################################### 
Example #21
Source File: test_cli.py    From profiling with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
def test_config(monkeypatch):
    @click.command()
    @profiler_options
    def f(profiler_factory, **kwargs):
        profiler = profiler_factory()
        return profiler, kwargs
    # no config.
    def io_error(*args, **kwargs):
        raise IOError
    monkeypatch.setattr(builtins, 'open', io_error)
    profiler, kwargs = f([], standalone_mode=False)
    assert isinstance(profiler, TracingProfiler)
    # config to use SamplingProfiler.
    monkeypatch.setattr(builtins, 'open', lambda *a, **k: mock_file(u'''
    [profiling]
    profiler = sampling
    sampler = tracing
    '''))
    profiler, kwargs = f([], standalone_mode=False)
    assert isinstance(profiler, SamplingProfiler)
    assert isinstance(profiler.sampler, TracingSampler)
    # set both of setup.cfg and .profiling.
    @valuedispatch
    def mock_open(path, *args, **kwargs):
        raise IOError
    @mock_open.register('setup.cfg')
    def open_setup_cfg(*_, **__):
        return mock_file(u'''
        [profiling]
        profiler = sampling
        pickle-protocol = 3
        ''')
    @mock_open.register('.profiling')
    def open_profiling(*_, **__):
        return mock_file(u'''
        [profiling]
        pickle-protocol = 0
        ''')
    monkeypatch.setattr(builtins, 'open', mock_open)
    profiler, kwargs = f([], standalone_mode=False)
    assert isinstance(profiler, SamplingProfiler)  # from setup.cfg
    assert kwargs['pickle_protocol'] == 0  # from .profiling