File: test_warnings.py

package info (click to toggle)
pypy3 7.0.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 111,848 kB
  • sloc: python: 1,291,746; ansic: 74,281; asm: 5,187; cpp: 3,017; sh: 2,533; makefile: 544; xml: 243; lisp: 45; csh: 21; awk: 4
file content (102 lines) | stat: -rw-r--r-- 3,901 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
class AppTestWarnings:
    spaceconfig = dict(usemodules=('_warnings',))

    def test_defaults(self):
        import _warnings
        assert _warnings._onceregistry == {}
        assert _warnings._defaultaction == 'default'
        expected = [('ignore', None, DeprecationWarning, None, 0),
                    ('ignore', None, PendingDeprecationWarning, None, 0),
                    ('ignore', None, ImportWarning, None, 0),
                    ('ignore', None, BytesWarning, None, 0),
                    ('ignore', None, ResourceWarning, None, 0)]
        assert expected == _warnings.filters

    def test_warn(self):
        import _warnings
        _warnings.warn("some message", DeprecationWarning)
        _warnings.warn("some message", Warning)
        _warnings.warn(("some message",1), Warning)

    def test_use_builtin__warnings(self):
        """Check that the stdlib warnings.py module manages to import our
        _warnings module.  If something is missing, it doesn't, and silently
        continues.  Then things don't reliably work: either the
        functionality of the pure Python version is subtly different, or
        more likely we get confusion because of a half-imported _warnings.
        """
        import warnings
        assert not hasattr(warnings, '_filters_version')

    def test_lineno(self):
        import warnings, _warnings, sys
        with warnings.catch_warnings(record=True) as w:
            _warnings.warn("some message", Warning)
            lineno = sys._getframe().f_lineno - 1 # the line above
            assert w[-1].lineno == lineno

    def test_warn_explicit(self):
        import _warnings
        _warnings.warn_explicit("some message", DeprecationWarning,
                                "<string>", 1, module_globals=globals())
        _warnings.warn_explicit("some message", Warning,
                                "<string>", 1, module_globals=globals())

    def test_default_action(self):
        import warnings, _warnings
        warnings.defaultaction = 'ignore'
        warnings.resetwarnings()
        with warnings.catch_warnings(record=True) as w:
            __warningregistry__ = {}
            _warnings.warn_explicit("message", UserWarning, "<test>", 44,
                                    registry={})
            assert len(w) == 0
        warnings.defaultaction = 'default'

    def test_show_source_line(self):
        import warnings
        import sys, io
        try:
            from test.warning_tests import inner
        except ImportError:
            skip('no test, -A on cpython?')
        # With showarning() missing, make sure that output is okay.
        saved = warnings.showwarning
        try:
            del warnings.showwarning

            stderr = sys.stderr
            try:
                sys.stderr = io.StringIO()
                inner('test message')
                result = sys.stderr.getvalue()
            finally:
                sys.stderr = stderr

            assert result.count('\n') == 2
            assert '  warnings.warn(message, ' in result
        finally:
            warnings.showwarning = saved

    def test_filename_none(self):
        import _warnings
        globals()['__file__'] = 'test.pyc'
        _warnings.warn('test', UserWarning)
        globals()['__file__'] = None
        _warnings.warn('test', UserWarning)

    def test_bad_category(self):
        import _warnings
        raises(TypeError, _warnings.warn, "text", 123)
        class Foo:
            pass
        raises(TypeError, _warnings.warn, "text", Foo)

    def test_surrogate_in_filename(self):
        import _warnings, __pypy__
        for filename in ("nonascii\xe9\u20ac", "surrogate\udc80"):
            try:
                __pypy__.fsencode(filename)
            except UnicodeEncodeError:
                continue
            _warnings.warn_explicit("text", UserWarning, filename, 1)