Python test.test_support.swap_attr() Examples
The following are 30
code examples of test.test_support.swap_attr().
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
test.test_support
, or try the search function
.
Example #1
Source File: test_util.py From ironpython2 with Apache License 2.0 | 6 votes |
def test_check_environ_getpwuid(self): util._environ_checked = 0 os.environ.pop('HOME', None) import pwd # only set pw_dir field, other fields are not used def mock_getpwuid(uid): return pwd.struct_passwd((None, None, None, None, None, '/home/distutils', None)) with swap_attr(pwd, 'getpwuid', mock_getpwuid): check_environ() self.assertEqual(os.environ['HOME'], '/home/distutils') util._environ_checked = 0 os.environ.pop('HOME', None) # bpo-10496: Catch pwd.getpwuid() error def getpwuid_err(uid): raise KeyError with swap_attr(pwd, 'getpwuid', getpwuid_err): check_environ() self.assertNotIn('HOME', os.environ)
Example #2
Source File: test_tools.py From ironpython2 with Apache License 2.0 | 6 votes |
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()
Example #3
Source File: test_subprocess.py From ironpython2 with Apache License 2.0 | 6 votes |
def test_stopped(self): """Test wait() behavior when waitpid returns WIFSTOPPED; issue29335.""" args = [sys.executable, '-c', 'pass'] proc = subprocess.Popen(args) # Wait until the real process completes to avoid zombie process pid = proc.pid pid, status = os.waitpid(pid, 0) self.assertEqual(status, 0) status = _testcapi.W_STOPCODE(3) def mock_waitpid(pid, flags): return (pid, status) with test_support.swap_attr(os, 'waitpid', mock_waitpid): returncode = proc.wait() self.assertEqual(returncode, -3)
Example #4
Source File: test_httpservers.py From ironpython2 with Apache License 2.0 | 6 votes |
def test_windows_colon(self): import SimpleHTTPServer with test_support.swap_attr(SimpleHTTPServer.os, 'path', ntpath): path = self.handler.translate_path('c:c:c:foo/filename') path = path.replace(ntpath.sep, os.sep) self.assertEqual(path, self.translated) path = self.handler.translate_path('\\c:../filename') path = path.replace(ntpath.sep, os.sep) self.assertEqual(path, self.translated) path = self.handler.translate_path('c:\\c:..\\foo/filename') path = path.replace(ntpath.sep, os.sep) self.assertEqual(path, self.translated) path = self.handler.translate_path('c:c:foo\\c:c:bar/filename') path = path.replace(ntpath.sep, os.sep) self.assertEqual(path, self.translated)
Example #5
Source File: test_tempfile.py From gcblue with BSD 3-Clause "New" or "Revised" License | 5 votes |
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)
Example #6
Source File: test_extensions.py From oss-ftp with MIT License | 5 votes |
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()
Example #7
Source File: test_tempfile.py From oss-ftp with MIT License | 5 votes |
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)
Example #8
Source File: test_tempfile.py From oss-ftp with MIT License | 5 votes |
def _inside_empty_temp_dir(): dir = tempfile.mkdtemp() try: with support.swap_attr(tempfile, 'tempdir', dir): yield finally: support.rmtree(dir)
Example #9
Source File: test_tempfile.py From oss-ftp with MIT License | 5 votes |
def _mock_candidate_names(*names): return support.swap_attr(tempfile, '_get_candidate_names', lambda: iter(names))
Example #10
Source File: test_uuid.py From oss-ftp with MIT License | 5 votes |
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)
Example #11
Source File: test_uuid.py From ironpython2 with Apache License 2.0 | 5 votes |
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): if sys.platform == 'cli': return io.StringIO(data) 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)
Example #12
Source File: test_tempfile.py From gcblue with BSD 3-Clause "New" or "Revised" License | 5 votes |
def _inside_empty_temp_dir(): dir = tempfile.mkdtemp() try: with support.swap_attr(tempfile, 'tempdir', dir): yield finally: support.rmtree(dir)
Example #13
Source File: test_tempfile.py From gcblue with BSD 3-Clause "New" or "Revised" License | 5 votes |
def _mock_candidate_names(*names): return support.swap_attr(tempfile, '_get_candidate_names', lambda: iter(names))
Example #14
Source File: test_uuid.py From gcblue with BSD 3-Clause "New" or "Revised" License | 5 votes |
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)
Example #15
Source File: test_tempfile.py From CTFCrackTools-V2 with GNU General Public License v3.0 | 5 votes |
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)
Example #16
Source File: test_extensions.py From PokemonGo-DesktopMap with MIT License | 5 votes |
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()
Example #17
Source File: test_tempfile.py From CTFCrackTools with GNU General Public License v3.0 | 5 votes |
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)
Example #18
Source File: test_tempfile.py From BinderFilter with MIT License | 5 votes |
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)
Example #19
Source File: test_sax.py From ironpython2 with Apache License 2.0 | 5 votes |
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)
Example #20
Source File: test_xml_etree.py From ironpython2 with Apache License 2.0 | 5 votes |
def test_recursive_repr(self): # Issue #25455 e = ET.Element('foo') with swap_attr(e, 'tag', e): with self.assertRaises(RuntimeError): repr(e) # Should not crash
Example #21
Source File: test_poplib.py From ironpython2 with Apache License 2.0 | 5 votes |
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')
Example #22
Source File: test_imaplib.py From ironpython2 with Apache License 2.0 | 5 votes |
def test_linetoolong(self): maxline = 10 class TooLongHandler(SimpleIMAPHandler): def handle(self): # Send a very long response line self.wfile.write('* OK ' + maxline * 'x' + '\r\n') with self.reaped_server(TooLongHandler) as server, \ support.swap_attr(imaplib, '_MAXLINE', maxline): with self.assertRaisesRegexp(imaplib.IMAP4.error, 'got more than 10 bytes'): self.imap_class(*server.server_address)
Example #23
Source File: test_tempfile.py From ironpython2 with Apache License 2.0 | 5 votes |
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)
Example #24
Source File: test_tempfile.py From ironpython2 with Apache License 2.0 | 5 votes |
def _mock_candidate_names(*names): return support.swap_attr(tempfile, '_get_candidate_names', lambda: iter(names))
Example #25
Source File: test_tempfile.py From ironpython2 with Apache License 2.0 | 5 votes |
def _inside_empty_temp_dir(): dir = tempfile.mkdtemp() try: with support.swap_attr(tempfile, 'tempdir', dir): yield finally: support.rmtree(dir)
Example #26
Source File: test_tempfile.py From ironpython2 with Apache License 2.0 | 5 votes |
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), []) def bad_writer(*args, **kwargs): fp = orig_open(*args, **kwargs) fp.write = raise_OSError return fp with support.swap_attr(io, "open", bad_writer) as orig_open: # 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)
Example #27
Source File: test_posixpath.py From ironpython2 with Apache License 2.0 | 5 votes |
def test_expanduser_pwd(self): pwd = support.import_module('pwd') self.assertIsInstance(posixpath.expanduser("~/"), str) # if home directory == root directory, this test makes no sense if posixpath.expanduser("~") != '/': self.assertEqual( posixpath.expanduser("~") + "/", posixpath.expanduser("~/") ) self.assertIsInstance(posixpath.expanduser("~root/"), str) self.assertIsInstance(posixpath.expanduser("~foo/"), str) with support.EnvironmentVarGuard() as env: # expanduser should fall back to using the password database del env['HOME'] home = pwd.getpwuid(os.getuid()).pw_dir # $HOME can end with a trailing /, so strip it (see #17809) home = home.rstrip("/") or '/' self.assertEqual(posixpath.expanduser("~"), home) # bpo-10496: If the HOME environment variable is not set and the # user (current identifier or name in the path) doesn't exist in # the password database (pwd.getuid() or pwd.getpwnam() fail), # expanduser() must return the path unchanged. def raise_keyerror(*args): raise KeyError with support.swap_attr(pwd, 'getpwuid', raise_keyerror), \ support.swap_attr(pwd, 'getpwnam', raise_keyerror): for path in ('~', '~/.local', '~vstinner/'): self.assertEqual(posixpath.expanduser(path), path)
Example #28
Source File: test_warnings.py From ironpython2 with Apache License 2.0 | 5 votes |
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)
Example #29
Source File: test_io.py From ironpython2 with Apache License 2.0 | 5 votes |
def test_illegal_encoder(self): # bpo-31271: A TypeError should be raised in case the return value of # encoder's encode() is invalid. class BadEncoder: def encode(self, dummy): return u'spam' def get_bad_encoder(dummy): return BadEncoder() rot13 = codecs.lookup("rot13") with support.swap_attr(rot13, '_is_text_encoding', True), \ support.swap_attr(rot13, 'incrementalencoder', get_bad_encoder): t = io.TextIOWrapper(io.BytesIO(b'foo'), encoding="rot13") with self.assertRaises(TypeError): t.write('bar') t.flush()
Example #30
Source File: test_io.py From ironpython2 with Apache License 2.0 | 4 votes |
def test_illegal_decoder(self): # Issue #17106 # Bypass the early encoding check added in issue 20404 def _make_illegal_wrapper(): quopri = codecs.lookup("quopri_codec") quopri._is_text_encoding = True try: t = self.TextIOWrapper(self.BytesIO(b'aaaaaa'), newline='\n', encoding="quopri_codec") finally: quopri._is_text_encoding = False return t # Crash when decoder returns non-string with support.check_py3k_warnings(): t = self.TextIOWrapper(self.BytesIO(b'aaaaaa'), newline='\n', encoding='quopri_codec') with self.maybeRaises(TypeError): t.read(1) with support.check_py3k_warnings(): t = self.TextIOWrapper(self.BytesIO(b'aaaaaa'), newline='\n', encoding='quopri_codec') with self.maybeRaises(TypeError): t.readline() with support.check_py3k_warnings(): t = self.TextIOWrapper(self.BytesIO(b'aaaaaa'), newline='\n', encoding='quopri_codec') with self.maybeRaises(TypeError): t.read() #else: #t = _make_illegal_wrapper() #self.assertRaises(TypeError, t.read, 1) #t = _make_illegal_wrapper() #self.assertRaises(TypeError, t.readline) #t = _make_illegal_wrapper() #self.assertRaises(TypeError, t.read) # Issue 31243: calling read() while the return value of decoder's # getstate() is invalid should neither crash the interpreter nor # raise a SystemError. def _make_very_illegal_wrapper(getstate_ret_val): class BadDecoder: def getstate(self): return getstate_ret_val def _get_bad_decoder(dummy): return BadDecoder() quopri = codecs.lookup("quopri_codec") with support.swap_attr(quopri, 'incrementaldecoder', _get_bad_decoder): return _make_illegal_wrapper() t = _make_very_illegal_wrapper(42) with self.maybeRaises(TypeError): t.read(42) t = _make_very_illegal_wrapper(()) with self.maybeRaises(TypeError): t.read(42)