|
4 | 4 | from sys import version_info
|
5 | 5 |
|
6 | 6 | from pyflakes import messages as m
|
| 7 | +from pyflakes.checker import Checker |
7 | 8 | from pyflakes.test.harness import TestCase, skipIf
|
8 | 9 |
|
| 10 | +try: |
| 11 | + WindowsError |
| 12 | + WIN = True |
| 13 | +except NameError: |
| 14 | + WIN = False |
| 15 | + |
9 | 16 |
|
10 | 17 | class TestBuiltins(TestCase):
|
11 | 18 |
|
@@ -39,3 +46,51 @@ def f():
|
39 | 46 |
|
40 | 47 | f()
|
41 | 48 | ''', m.UndefinedLocal)
|
| 49 | + |
| 50 | + |
| 51 | +class TestLiveBuiltins(TestCase): |
| 52 | + |
| 53 | + def test_exists(self): |
| 54 | + for name in sorted(Checker.builtIns): |
| 55 | + # __file__ does exist in this test harness |
| 56 | + if name == '__file__': |
| 57 | + continue |
| 58 | + |
| 59 | + if name == 'WindowsError' and not WIN: |
| 60 | + continue |
| 61 | + |
| 62 | + source = ''' |
| 63 | + %s |
| 64 | + ''' % name |
| 65 | + e = self.pythonException(source) |
| 66 | + self.assertIsNone(e) |
| 67 | + |
| 68 | + def test_del(self): |
| 69 | + for name in sorted(Checker.builtIns): |
| 70 | + # __file__ does exist in this test harness |
| 71 | + if name == '__file__': |
| 72 | + continue |
| 73 | + |
| 74 | + # __debug__ can be deleted sometimes and not deleted other times. |
| 75 | + # Safest course of action is to assume it can be deleted, in |
| 76 | + # order that no error is reported by pyflakes |
| 77 | + if name == '__debug__': |
| 78 | + continue |
| 79 | + |
| 80 | + source = ''' |
| 81 | + del %s |
| 82 | + ''' % name |
| 83 | + |
| 84 | + e = self.pythonException(source) |
| 85 | + |
| 86 | + if isinstance(e, SyntaxError): |
| 87 | + if version_info < (3,): |
| 88 | + # SyntaxError: invalid syntax |
| 89 | + self.assertIn(name, ('print')) |
| 90 | + else: |
| 91 | + # SyntaxError: can't delete keyword |
| 92 | + self.assertIn(name, ('None', 'True', 'False')) |
| 93 | + elif isinstance(e, NameError): |
| 94 | + self.flakes(source, m.UndefinedName) |
| 95 | + else: |
| 96 | + self.flakes(source) |
0 commit comments