当前位置: 首页>>代码示例>>Python>>正文


Python test_support.swap_attr函数代码示例

本文整理汇总了Python中test.test_support.swap_attr函数的典型用法代码示例。如果您正苦于以下问题:Python swap_attr函数的具体用法?Python swap_attr怎么用?Python swap_attr使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了swap_attr函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_initialization_no_master

 def test_initialization_no_master(self):
     # no master passing
     with swap_attr(tkinter, '_default_root', None), \
          swap_attr(tkinter, '_support_default_root', True):
         try:
             x = ttk.LabeledScale()
             self.assertIsNotNone(tkinter._default_root)
             self.assertEqual(x.master, tkinter._default_root)
             self.assertEqual(x.tk, tkinter._default_root.tk)
             x.destroy()
         finally:
             destroy_default_root()
开发者ID:1187,项目名称:PokemonGo-DesktopMap,代码行数:12,代码来源:test_extensions.py

示例2: test_memoryerror

    def test_memoryerror(self):
        lines = linecache.getlines(FILENAME)
        self.assertTrue(lines)
        def raise_memoryerror(*args, **kwargs):
            raise MemoryError
        with support.swap_attr(linecache, 'updatecache', raise_memoryerror):
            lines2 = linecache.getlines(FILENAME)
        self.assertEqual(lines2, lines)

        linecache.clearcache()
        with support.swap_attr(linecache, 'updatecache', raise_memoryerror):
            lines3 = linecache.getlines(FILENAME)
        self.assertEqual(lines3, [])
        self.assertEqual(linecache.getlines(FILENAME), lines)
开发者ID:abhinavthomas,项目名称:pypy,代码行数:14,代码来源:test_linecache.py

示例3: test_nonexisting_directory

 def test_nonexisting_directory(self):
     with _inside_empty_temp_dir():
         tempdir = os.path.join(tempfile.tempdir, 'nonexistent')
         with support.swap_attr(tempfile, 'tempdir', tempdir):
             with self.assertRaises(OSError) as cm:
                 self.make_temp()
             self.assertEqual(cm.exception.errno, errno.ENOENT)
开发者ID:Kelauni22,项目名称:Meeple,代码行数:7,代码来源:test_tempfile.py

示例4: run_script

    def run_script(self, input="", args=("-",), substfile="xx yy\n"):
        substfilename = test_support.TESTFN + ".subst"
        with open(substfilename, "w") as file:
            file.write(substfile)
        self.addCleanup(test_support.unlink, substfilename)

        argv = ["fixcid.py", "-s", substfilename] + list(args)
        script = os.path.join(scriptsdir, "fixcid.py")
        with test_support.swap_attr(sys, "argv", argv), \
                test_support.swap_attr(sys, "stdin", StringIO(input)), \
                test_support.captured_stdout() as output:
            try:
                runpy.run_path(script, run_name="__main__")
            except SystemExit as exit:
                self.assertEqual(exit.code, 0)
        return output.getvalue()
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:16,代码来源:test_tools.py

示例5: _inside_empty_temp_dir

def _inside_empty_temp_dir():
    dir = tempfile.mkdtemp()
    try:
        with support.swap_attr(tempfile, 'tempdir', dir):
            yield
    finally:
        support.rmtree(dir)
开发者ID:Kelauni22,项目名称:Meeple,代码行数:7,代码来源:test_tempfile.py

示例6: test_find_mac

    def test_find_mac(self):
        data = '''\

fake hwaddr
cscotun0  Link encap:UNSPEC  HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00
eth0      Link encap:Ethernet  HWaddr 12:34:56:78:90:ab
'''
        def mock_popen(cmd):
            return io.BytesIO(data)

        path = os.environ.get("PATH", os.defpath).split(os.pathsep)
        path.extend(('/sbin', '/usr/sbin'))
        for dir in path:
            executable = os.path.join(dir, 'ifconfig')
            if (os.path.exists(executable) and
                os.access(executable, os.F_OK | os.X_OK) and
                not os.path.isdir(executable)):
                break
        else:
            self.skipTest('requires ifconfig')

        with test_support.swap_attr(os, 'popen', mock_popen):
            mac = uuid._find_mac(
                command='ifconfig',
                args='',
                hw_identifiers=['hwaddr'],
                get_index=lambda x: x + 1,
            )
            self.assertEqual(mac, 0x1234567890ab)
开发者ID:dr4ke616,项目名称:custom_python,代码行数:29,代码来源:test_uuid.py

示例7: test_non_directory

 def test_non_directory(self):
     with _inside_empty_temp_dir():
         tempdir = os.path.join(tempfile.tempdir, 'file')
         open(tempdir, 'wb').close()
         with support.swap_attr(tempfile, 'tempdir', tempdir):
             with self.assertRaises(OSError) as cm:
                 self.make_temp()
             self.assertIn(cm.exception.errno, (errno.ENOTDIR, errno.ENOENT))
开发者ID:Kelauni22,项目名称:Meeple,代码行数:8,代码来源:test_tempfile.py

示例8: test_bad_timezone

    def test_bad_timezone(self):
        # Explicitly test possibility of bad timezone;
        # when time.tzname[0] == time.tzname[1] and time.daylight
        tz_name = time.tzname[0]
        if tz_name.upper() in ("UTC", "GMT"):
            self.skipTest('need non-UTC/GMT timezone')

        with support.swap_attr(time, 'tzname', (tz_name, tz_name)), \
             support.swap_attr(time, 'daylight', 1), \
             support.swap_attr(time, 'tzset', lambda: None):
            time.tzname = (tz_name, tz_name)
            time.daylight = 1
            tz_value = _strptime._strptime_time(tz_name, "%Z")[8]
            self.assertEqual(tz_value, -1,
                    "%s lead to a timezone value of %s instead of -1 when "
                    "time.daylight set to %s and passing in %s" %
                    (time.tzname, tz_value, time.daylight, tz_name))
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:17,代码来源:test_strptime.py

示例9: test_modify_builtins

    def test_modify_builtins(self):
        # Modify the __builtin__ module directly.
        def foo():
            return len([1, 2, 3])
        self.configure_func(foo)

        self.assertEqual(foo(), 3)
        with swap_attr(__builtin__, "len", lambda x: 7):
            self.assertEqual(foo(), 7)
开发者ID:andrcmdr,项目名称:unladen-swallow,代码行数:9,代码来源:test_dynamic.py

示例10: test_apop_REDOS

 def test_apop_REDOS(self):
     # Replace welcome with very long evil welcome.
     # NB The upper bound on welcome length is currently 2048.
     # At this length, evil input makes each apop call take
     # on the order of milliseconds instead of microseconds.
     evil_welcome = b'+OK' + (b'<' * 1000000)
     with test_support.swap_attr(self.client, 'welcome', evil_welcome):
         # The evil welcome is invalid, so apop should throw.
         self.assertRaises(poplib.error_proto, self.client.apop, 'a', 'kb')
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:9,代码来源:test_poplib.py

示例11: test_issue31411

 def test_issue31411(self):
     # warn_explicit() shouldn't raise a SystemError in case
     # warnings.onceregistry isn't a dictionary.
     wmod = self.module
     with original_warnings.catch_warnings(module=wmod):
         wmod.filterwarnings('once')
         with test_support.swap_attr(wmod, 'onceregistry', None):
             with self.assertRaises(TypeError):
                 wmod.warn_explicit('foo', Warning, 'bar', 1, registry=None)
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:9,代码来源:test_warnings.py

示例12: test_modify_builtins_while_generator_active

    def test_modify_builtins_while_generator_active(self):
        # Modify the builtins out from under a live generator.
        def foo():
            x = range(3)
            yield len(x)
            yield len(x)
        self.configure_func(foo)

        g = foo()
        self.assertEqual(g.next(), 3)
        with swap_attr(__builtin__, "len", lambda x: 7):
            self.assertEqual(g.next(), 7)
开发者ID:andrcmdr,项目名称:unladen-swallow,代码行数:12,代码来源:test_dynamic.py

示例13: test_no_files_left_behind

    def test_no_files_left_behind(self):
        # use a private empty directory
        our_temp_directory = tempfile.mkdtemp()
        try:
            # force _get_default_tempdir() to consider our empty directory
            def our_candidate_list():
                return [our_temp_directory]

            with support.swap_attr(tempfile, "_candidate_tempdir_list",
                                   our_candidate_list):
                # verify our directory is empty after _get_default_tempdir()
                tempfile._get_default_tempdir()
                self.assertEqual(os.listdir(our_temp_directory), [])

                def raise_OSError(*args, **kwargs):
                    raise OSError(-1)

                with support.swap_attr(io, "open", raise_OSError):
                    # test again with failing io.open()
                    with self.assertRaises(IOError) as cm:
                        tempfile._get_default_tempdir()
                    self.assertEqual(cm.exception.errno, errno.ENOENT)
                    self.assertEqual(os.listdir(our_temp_directory), [])

                open = io.open
                def bad_writer(*args, **kwargs):
                    fp = open(*args, **kwargs)
                    fp.write = raise_OSError
                    return fp

                with support.swap_attr(io, "open", bad_writer):
                    # test again with failing write()
                    with self.assertRaises(IOError) as cm:
                        tempfile._get_default_tempdir()
                    self.assertEqual(cm.exception.errno, errno.ENOENT)
                    self.assertEqual(os.listdir(our_temp_directory), [])
        finally:
            shutil.rmtree(our_temp_directory)
开发者ID:Kelauni22,项目名称:Meeple,代码行数:38,代码来源:test_tempfile.py

示例14: test_parse_close_source

    def test_parse_close_source(self):
        builtin_open = open
        non_local = {'fileobj': None}

        def mock_open(*args):
            fileobj = builtin_open(*args)
            non_local['fileobj'] = fileobj
            return fileobj

        with support.swap_attr(saxutils, 'open', mock_open):
            make_xml_file(self.data, 'iso-8859-1', None)
            with self.assertRaises(SAXException):
                self.check_parse(TESTFN)
            self.assertTrue(non_local['fileobj'].closed)
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:14,代码来源:test_sax.py

示例15: test_modify_builtins_from_leaf_function

    def test_modify_builtins_from_leaf_function(self):
        # Verify that modifications made by leaf functions percolate up the
        # callstack.
        with swap_attr(__builtin__, "len", len):
            def bar():
                __builtin__.len = lambda x: 4

            def foo(modifier):
                l = []
                l.append(len(range(7)))
                modifier()
                l.append(len(range(7)))
                return l
            self.configure_func(foo, lambda: None)

            self.assertEqual(foo(bar), [7, 4])
开发者ID:andrcmdr,项目名称:unladen-swallow,代码行数:16,代码来源:test_dynamic.py


注:本文中的test.test_support.swap_attr函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。