def test_addpackage_import_bad_pth_file(self): # Issue 5258 pth_dir, pth_fn = self.make_pth("abc\x00def\n") with captured_output("stderr") as err_out: site.addpackage(pth_dir, pth_fn, set()) self.assertRegexpMatches(err_out.getvalue(), "line 1") self.assertRegexpMatches(err_out.getvalue(), re.escape(os.path.join(pth_dir, pth_fn))) # XXX: ditto previous XXX comment. self.assertRegexpMatches(err_out.getvalue(), "Traceback") self.assertRegexpMatches(err_out.getvalue(), "TypeError")
def test_showwarning_missing(self): # Test that showwarning() missing is okay. text = 'del showwarning test' with original_warnings.catch_warnings(module=self.module): self.module.filterwarnings("always", category=UserWarning) del self.module.showwarning with test_support.captured_output('stderr') as stream: self.module.warn(text) result = stream.getvalue() self.assertIn(text, result)
def test_addpackage_import_bad_exec(self): # Issue 10642 pth_dir, pth_fn = self.make_pth("randompath\nimport nosuchmodule\n") with captured_output("stderr") as err_out: site.addpackage(pth_dir, pth_fn, set()) self.assertRegexpMatches(err_out.getvalue(), "line 2") self.assertRegexpMatches(err_out.getvalue(), re.escape(os.path.join(pth_dir, pth_fn))) # XXX: ditto previous XXX comment. self.assertRegexpMatches(err_out.getvalue(), "Traceback") self.assertRegexpMatches(err_out.getvalue(), "ImportError")
def test_addpackage_import_bad_pth_file(self): # Issue 5258 pth_dir, pth_fn = self.make_pth("abc\x00def\n") with captured_output("stderr") as err_out: site.addpackage(pth_dir, pth_fn, set()) self.assertRegexpMatches(err_out.getvalue(), "line 1") self.assertRegexpMatches(err_out.getvalue(), re.escape(os.path.join(pth_dir, pth_fn))) # XXX: ditto previous XXX comment. self.assertRegexpMatches(err_out.getvalue(), 'Traceback') self.assertRegexpMatches(err_out.getvalue(), 'TypeError')
def test_addpackage_import_bad_exec(self): # Issue 10642 pth_dir, pth_fn = self.make_pth("randompath\nimport nosuchmodule\n") with captured_output("stderr") as err_out: site.addpackage(pth_dir, pth_fn, set()) self.assertRegexpMatches(err_out.getvalue(), "line 2") self.assertRegexpMatches(err_out.getvalue(), re.escape(os.path.join(pth_dir, pth_fn))) # XXX: ditto previous XXX comment. self.assertRegexpMatches(err_out.getvalue(), 'Traceback') self.assertRegexpMatches(err_out.getvalue(), 'ImportError')
def test_showwarning_missing(self): if test_support.due_to_ironpython_bug("http://ironpython.codeplex.com/workitem/28171"): return # Test that showwarning() missing is okay. text = 'del showwarning test' with original_warnings.catch_warnings(module=self.module): self.module.filterwarnings("always", category=UserWarning) del self.module.showwarning with test_support.captured_output('stderr') as stream: self.module.warn(text) result = stream.getvalue() self.assertIn(text, result)
def test_print_stack(self): def prn(): traceback.print_stack() with captured_output("stderr") as stderr: prn() lineno = prn.__code__.co_firstlineno file = prn.__code__.co_filename self.assertEqual(stderr.getvalue().splitlines()[-4:], [ ' File "%s", line %d, in test_print_stack' % (file, lineno+3), ' prn()', ' File "%s", line %d, in prn' % (file, lineno+1), ' traceback.print_stack()', ])
def test_showwarning_missing(self): if test_support.due_to_ironpython_bug( "http://ironpython.codeplex.com/workitem/28171"): return # Test that showwarning() missing is okay. text = 'del showwarning test' with original_warnings.catch_warnings(module=self.module): self.module.filterwarnings("always", category=UserWarning) del self.module.showwarning with test_support.captured_output('stderr') as stream: self.module.warn(text) result = stream.getvalue() self.assertIn(text, result)
def test_addpackage_import_bad_syntax(self): # Issue 10642 pth_dir, pth_fn = self.make_pth("import bad)syntax\n") with captured_output("stderr") as err_out: site.addpackage(pth_dir, pth_fn, set()) self.assertRegexpMatches(err_out.getvalue(), "line 1") self.assertRegexpMatches(err_out.getvalue(), re.escape(os.path.join(pth_dir, pth_fn))) # XXX: the previous two should be independent checks so that the # order doesn't matter. The next three could be a single check # but my regex foo isn't good enough to write it. self.assertRegexpMatches(err_out.getvalue(), "Traceback") self.assertRegexpMatches(err_out.getvalue(), r"import bad\)syntax") self.assertRegexpMatches(err_out.getvalue(), "SyntaxError")
def test_addpackage_import_bad_syntax(self): # Issue 10642 pth_dir, pth_fn = self.make_pth("import bad)syntax\n") with captured_output("stderr") as err_out: site.addpackage(pth_dir, pth_fn, set()) self.assertRegexpMatches(err_out.getvalue(), "line 1") self.assertRegexpMatches(err_out.getvalue(), re.escape(os.path.join(pth_dir, pth_fn))) # XXX: the previous two should be independent checks so that the # order doesn't matter. The next three could be a single check # but my regex foo isn't good enough to write it. self.assertRegexpMatches(err_out.getvalue(), 'Traceback') self.assertRegexpMatches(err_out.getvalue(), r'import bad\)syntax') self.assertRegexpMatches(err_out.getvalue(), 'SyntaxError')
def test_badisinstance(self): # Bug #2542: if issubclass(e, MyException) raises an exception, # it should be ignored class Meta(type): def __subclasscheck__(cls, subclass): raise ValueError() class MyException(Exception): __metaclass__ = Meta pass with captured_output("stderr") as stderr: try: raise KeyError() except MyException, e: self.fail("exception should not be a MyException") except KeyError: pass
def test_badisinstance(self): # Bug #2542: if issubclass(e, MyException) raises an exception, # it should be ignored class Meta(type): def __subclasscheck__(cls, subclass): raise ValueError() class MyException(Exception): __metaclass__ = Meta pass if due_to_ironpython_bug("http://ironpython.codeplex.com/WorkItem/View.aspx?WorkItemId=21116"): return with captured_output("stderr") as stderr: try: raise KeyError() except MyException, e: self.fail("exception should not be a MyException") except KeyError: pass
def test_show_warning_output(self): # With showarning() missing, make sure that output is okay. text = "test show_warning" with original_warnings.catch_warnings(module=self.module): self.module.filterwarnings("always", category=UserWarning) del self.module.showwarning with test_support.captured_output("stderr") as stream: warning_tests.inner(text) result = stream.getvalue() self.assertEqual(result.count("\n"), 2, "Too many newlines in %r" % result) first_line, second_line = result.split("\n", 1) expected_file = os.path.splitext(warning_tests.__file__)[0] + ".py" first_line_parts = first_line.rsplit(":", 3) path, line, warning_class, message = first_line_parts line = int(line) self.assertEqual(expected_file, path) self.assertEqual(warning_class, " " + UserWarning.__name__) self.assertEqual(message, " " + text) expected_line = " " + linecache.getline(path, line).strip() + "\n" assert expected_line self.assertEqual(second_line, expected_line)
def test_save_exception_state_on_error(self): # See issue #14474 def task(): started.release() raise SyntaxError def mywrite(self, *args): try: raise ValueError except ValueError: pass real_write(self, *args) c = thread._count() started = thread.allocate_lock() with test_support.captured_output("stderr") as stderr: real_write = stderr.write stderr.write = mywrite started.acquire() thread.start_new_thread(task, ()) started.acquire() while thread._count() > c: time.sleep(0.01) self.assertIn("Traceback", stderr.getvalue())
def test_show_warning_output(self): # With showarning() missing, make sure that output is okay. text = 'test show_warning' with original_warnings.catch_warnings(module=self.module): self.module.filterwarnings("always", category=UserWarning) del self.module.showwarning with test_support.captured_output('stderr') as stream: warning_tests.inner(text) result = stream.getvalue() self.assertEqual(result.count('\n'), 2, "Too many newlines in %r" % result) first_line, second_line = result.split('\n', 1) expected_file = os.path.splitext(warning_tests.__file__)[0] + '.py' first_line_parts = first_line.rsplit(':', 3) path, line, warning_class, message = first_line_parts line = int(line) self.assertEqual(expected_file, path) self.assertEqual(warning_class, ' ' + UserWarning.__name__) self.assertEqual(message, ' ' + text) expected_line = ' ' + linecache.getline(path, line).strip() + '\n' assert expected_line self.assertEqual(second_line, expected_line)
def test_show_warning_output(self): # With showarning() missing, make sure that output is okay. text = 'test show_warning' with original_warnings.catch_warnings(module=self.module): self.module.filterwarnings("always", category=UserWarning) del self.module.showwarning with test_support.captured_output('stderr') as stream: warning_tests.inner(text) result = stream.getvalue() self.assertEqual(result.count('\n'), 2, "Too many newlines in %r" % result) first_line, second_line = result.split('\n', 1) expected_file = warning_tests_py first_line_parts = first_line.rsplit(':', 3) path, line, warning_class, message = first_line_parts line = int(line) self.assertEqual(expected_file, path) self.assertEqual(warning_class, ' ' + UserWarning.__name__) self.assertEqual(message, ' ' + text) expected_line = ' ' + linecache.getline(path, line).strip() + '\n' assert expected_line self.assertEqual(second_line, expected_line)
def testInfiniteRecursion(self): if is_cli64: print "Dev10 Bug 409568" return if is_cli: import sys import System _cli_saved_limit = sys.getrecursionlimit() sys.setrecursionlimit(1001) def f(): return f() self.assertRaises(RuntimeError, f) def g(): try: return g() except ValueError: return -1 # The test prints an unraisable recursion error when # doing "except ValueError", this is because subclass # checking has recursion checking too. with captured_output("stderr"): try: g() except RuntimeError: pass except: self.fail("Should have raised KeyError") else: self.fail("Should have raised KeyError") if is_cli: import sys sys.setrecursionlimit(_cli_saved_limit)
def testInfiniteRecursion(self): def f(): return f() self.assertRaises(RuntimeError, f) def g(): try: return g() except ValueError: return -1 # The test prints an unraisable recursion error when # doing "except ValueError", this is because subclass # checking has recursion checking too. with captured_output("stderr"): try: g() except RuntimeError: pass except: self.fail("Should have raised KeyError") else: self.fail("Should have raised KeyError")
def test_after_stop_fails(self): with captured_output('stdout'): self._test_hooks(behavior=ERROR, status='stopped', hook_name='after_stop', call=self._stop)
def test_after_start_fails(self): with captured_output('stderr'): yield self._test_hooks(behavior=ERROR, status='stopped', hook_name='after_start')
def test_after_stop_fails(self): with captured_output("stdout"): self._test_hooks(behavior=ERROR, status="stopped", hook_name="after_stop", call=self._stop)
def test_before_spawn_failure(self): with captured_output("stdout"): self._test_hooks(behavior=ERROR, status="stopped", hook_name="before_spawn", call=self._stop)
__metaclass__ = Meta pass with captured_output("stderr") as stderr: try: raise KeyError() except MyException, e: self.fail("exception should not be a MyException") except KeyError: pass except: self.fail("Should have raised KeyError") else: self.fail("Should have raised KeyError") with captured_output("stderr") as stderr: def g(): try: return g() except RuntimeError: return sys.exc_info() e, v, tb = g() self.assert_(e is RuntimeError, e) self.assert_("maximum recursion depth exceeded" in str(v), v) # Helper class used by TestSameStrAndUnicodeMsg class ExcWithOverriddenStr(Exception): """Subclass of Exception that accepts a keyword 'msg' arg that is returned by __str__. 'msg' won't be included in self.args"""
def test_print_function(self): with test_support.captured_output("stderr") as s: print("foo", file=sys.stderr) self.assertEqual(s.getvalue(), "foo\n")
__metaclass__ = Meta pass with captured_output("stderr") as stderr: try: raise KeyError() except MyException, e: self.fail("exception should not be a MyException") except KeyError: pass except: self.fail("Should have raised KeyError") else: self.fail("Should have raised KeyError") with captured_output("stderr") as stderr: def g(): try: return g() except RuntimeError: return sys.exc_info() e, v, tb = g() self.assertTrue(e is RuntimeError, e) self.assertIn("maximum recursion depth exceeded", str(v)) def test_new_returns_invalid_instance(self): # See issue #11627. class MyException(Exception): def __new__(cls, *args):
def test_before_spawn_failure(self): with captured_output('stdout'): self._test_hooks(behavior=ERROR, status='stopped', hook_name='before_spawn', call=self._stop)