Пример #1
0
 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")
Пример #2
0
 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)
Пример #3
0
 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)
Пример #4
0
 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")
Пример #5
0
 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')
Пример #6
0
 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')
Пример #7
0
 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)
Пример #8
0
 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()',
     ])
Пример #9
0
 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)
Пример #10
0
 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_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()',
     ])
Пример #12
0
 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')
Пример #13
0
    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

        with captured_output("stderr") as stderr:
            try:
                raise KeyError()
            except MyException, e:
                self.fail("exception should not be a MyException")
            except KeyError:
                pass
Пример #15
0
    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
Пример #16
0
 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)
Пример #17
0
 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_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())
Пример #19
0
 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)
Пример #20
0
 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)
Пример #21
0
    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")
Пример #23
0
    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")
Пример #24
0
    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)
Пример #25
0
 def test_after_stop_fails(self):
     with captured_output('stdout'):
         self._test_hooks(behavior=ERROR, status='stopped',
                          hook_name='after_stop',
                          call=self._stop)
Пример #26
0
 def test_after_start_fails(self):
     with captured_output('stderr'):
         yield self._test_hooks(behavior=ERROR,
                                status='stopped',
                                hook_name='after_start')
Пример #27
0
 def test_after_stop_fails(self):
     with captured_output("stdout"):
         self._test_hooks(behavior=ERROR, status="stopped", hook_name="after_stop", call=self._stop)
Пример #28
0
 def test_before_spawn_failure(self):
     with captured_output("stdout"):
         self._test_hooks(behavior=ERROR, status="stopped", hook_name="before_spawn", call=self._stop)
Пример #29
0
 def test_after_start_fails(self):
     with captured_output('stderr'):
         yield self._test_hooks(behavior=ERROR, status='stopped',
                                hook_name='after_start')
            __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"""
Пример #31
0
 def test_print_function(self):
     with test_support.captured_output("stderr") as s:
         print("foo", file=sys.stderr)
     self.assertEqual(s.getvalue(), "foo\n")
Пример #32
0
            __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):
Пример #33
0
 def test_after_stop_fails(self):
     with captured_output('stdout'):
         self._test_hooks(behavior=ERROR, status='stopped',
                          hook_name='after_stop',
                          call=self._stop)
Пример #34
0
 def test_before_spawn_failure(self):
     with captured_output('stdout'):
         self._test_hooks(behavior=ERROR,
                          status='stopped',
                          hook_name='before_spawn',
                          call=self._stop)
Пример #35
0
 def test_before_spawn_failure(self):
     with captured_output('stdout'):
         self._test_hooks(behavior=ERROR, status='stopped',
                          hook_name='before_spawn',
                          call=self._stop)
Пример #36
0
 def test_print_function(self):
     with test_support.captured_output("stderr") as s:
         print("foo", file=sys.stderr)
     self.assertEqual(s.getvalue(), "foo\n")