| 1 |
import py |
| 2 |
from pypy.interpreter.pyparser import future, pytokenizer |
| 3 |
from pypy.tool import stdlib___future__ as fut |
| 4 |
|
| 5 |
def run(s, expected_last_future=(0, 0)): |
| 6 |
source_lines = s.splitlines(True) |
| 7 |
tokens = pytokenizer.generate_tokens(source_lines, 0) |
| 8 |
|
| 9 |
flags, last_future_import = future.add_future_flags( |
| 10 |
future.futureFlags_3_5, tokens) |
| 11 |
assert last_future_import == expected_last_future |
| 12 |
return flags |
| 13 |
|
| 14 |
def test_docstring(): |
| 15 |
s = '"Docstring\\" "\nfrom __future__ import division\n' |
| 16 |
f = run(s, (2, 24)) |
| 17 |
assert f == 0 |
| 18 |
|
| 19 |
def test_comment(): |
| 20 |
s = '# A comment about nothing ;\n' |
| 21 |
f = run(s) |
| 22 |
assert f == 0 |
| 23 |
|
| 24 |
def test_tripledocstring(): |
| 25 |
s = '''""" This is a |
| 26 |
docstring with line |
| 27 |
breaks in it. It even has a \n""" |
| 28 |
''' |
| 29 |
f = run(s) |
| 30 |
assert f == 0 |
| 31 |
|
| 32 |
def test_escapedquote_in_tripledocstring(): |
| 33 |
s = '''""" This is a |
| 34 |
docstring with line |
| 35 |
breaks in it. \\"""It even has an escaped quote!""" |
| 36 |
''' |
| 37 |
f = run(s) |
| 38 |
assert f == 0 |
| 39 |
|
| 40 |
def test_empty_line(): |
| 41 |
s = ' \t \f \n \n' |
| 42 |
f = run(s) |
| 43 |
assert f == 0 |
| 44 |
|
| 45 |
def test_from(): |
| 46 |
s = 'from __future__ import division\n' |
| 47 |
f = run(s, (1, 24)) |
| 48 |
assert f == 0 |
| 49 |
|
| 50 |
def test_froms(): |
| 51 |
s = 'from __future__ import division, generators, with_statement\n' |
| 52 |
f = run(s, (1, 24)) |
| 53 |
assert f == 0 |
| 54 |
|
| 55 |
def test_from_as(): |
| 56 |
s = 'from __future__ import division as b\n' |
| 57 |
f = run(s, (1, 24)) |
| 58 |
assert f == 0 |
| 59 |
|
| 60 |
def test_froms_as(): |
| 61 |
s = 'from __future__ import division as b, generators as c\n' |
| 62 |
f = run(s, (1, 24)) |
| 63 |
assert f == 0 |
| 64 |
|
| 65 |
def test_from_paren(): |
| 66 |
s = 'from __future__ import (division)\n' |
| 67 |
f = run(s, (1, 25)) |
| 68 |
assert f == 0 |
| 69 |
|
| 70 |
def test_froms_paren(): |
| 71 |
s = 'from __future__ import (division, generators)\n' |
| 72 |
f = run(s, (1, 25)) |
| 73 |
assert f == 0 |
| 74 |
|
| 75 |
def test_froms_paren_as(): |
| 76 |
s = 'from __future__ import (division as b, generators,)\n' |
| 77 |
f = run(s, (1, 25)) |
| 78 |
assert f == 0 |
| 79 |
|
| 80 |
def test_paren_with_newline(): |
| 81 |
s = 'from __future__ import (division,\nabsolute_import)\n' |
| 82 |
f = run(s, (1, 24)) |
| 83 |
assert f == 0 |
| 84 |
|
| 85 |
def test_paren_with_newline_2(): |
| 86 |
s = 'from __future__ import (\ndivision,\nabsolute_import)\n' |
| 87 |
f = run(s, (2, 0)) |
| 88 |
assert f == 0 |
| 89 |
|
| 90 |
def test_multiline(): |
| 91 |
s = '"abc" #def\n #ghi\nfrom __future__ import (division as b, generators,)\nfrom __future__ import with_statement\n' |
| 92 |
f = run(s, (4, 23)) |
| 93 |
assert f == 0 |
| 94 |
|
| 95 |
def test_windows_style_lineendings(): |
| 96 |
s = '"abc" #def\r\n #ghi\r\nfrom __future__ import (division as b, generators,)\r\nfrom __future__ import with_statement\r\n' |
| 97 |
f = run(s, (4, 23)) |
| 98 |
assert f == 0 |
| 99 |
|
| 100 |
def test_mac_style_lineendings(): |
| 101 |
s = '"abc" #def\r #ghi\rfrom __future__ import (division as b, generators,)\rfrom __future__ import with_statement\r' |
| 102 |
f = run(s, (4, 23)) |
| 103 |
assert f == 0 |
| 104 |
|
| 105 |
def test_semicolon(): |
| 106 |
s = '"abc" #def\n #ghi\nfrom __future__ import (division as b, generators,); from __future__ import with_statement\n' |
| 107 |
f = run(s, (3, 78)) |
| 108 |
assert f == 0 |
| 109 |
|
| 110 |
def test_semicolon_2(): |
| 111 |
s = 'from __future__ import division; from foo import bar' |
| 112 |
f = run(s, expected_last_future=(1, 24)) |
| 113 |
assert f == 0 |
| 114 |
|
| 115 |
def test_full_chain(): |
| 116 |
s = '"abc" #def\n #ghi\nfrom __future__ import (division as b, generators,); from __future__ import with_statement\n' |
| 117 |
f = run(s, (3, 78)) |
| 118 |
assert f == 0 |
| 119 |
|
| 120 |
def test_intervening_code(): |
| 121 |
s = 'from __future__ import (division as b, generators,)\nfrom sys import modules\nfrom __future__ import with_statement\n' |
| 122 |
f = run(s, expected_last_future=(1, 25)) |
| 123 |
assert f == 0 |
| 124 |
|
| 125 |
def test_nonexisting(): |
| 126 |
s = 'from __future__ import non_existing_feature\n' |
| 127 |
f = run(s, (1, 24)) |
| 128 |
assert f == 0 |
| 129 |
|
| 130 |
def test_nonexisting_2(): |
| 131 |
s = 'from __future__ import non_existing_feature, with_statement\n' |
| 132 |
f = run(s, (1, 24)) |
| 133 |
assert f == 0 |
| 134 |
|
| 135 |
def test_from_import_abs_import(): |
| 136 |
s = 'from __future__ import absolute_import\n' |
| 137 |
f = run(s, (1, 24)) |
| 138 |
assert f == 0 |
| 139 |
|
| 140 |
def test_raw_doc(): |
| 141 |
s = 'r"Doc"\nfrom __future__ import with_statement\n' |
| 142 |
f = run(s, (2, 23)) |
| 143 |
assert f == 0 |
| 144 |
|
| 145 |
def test_unicode_doc(): |
| 146 |
s = 'u"Doc"\nfrom __future__ import with_statement\n' |
| 147 |
f = run(s, (2, 23)) |
| 148 |
assert f == 0 |
| 149 |
|
| 150 |
def test_raw_unicode_doc(): |
| 151 |
s = 'ru"Doc"\nfrom __future__ import with_statement\n' |
| 152 |
f = run(s, (2, 23)) |
| 153 |
assert f == 0 |
| 154 |
|
| 155 |
def test_continuation_line(): |
| 156 |
s = "\\\nfrom __future__ import with_statement\n" |
| 157 |
f = run(s, (2, 23)) |
| 158 |
assert f == 0 |
| 159 |
|
| 160 |
def test_continuation_lines(): |
| 161 |
s = "\\\n \t\\\nfrom __future__ import with_statement\n" |
| 162 |
f = run(s, (0, 0)) |
| 163 |
assert f == 0 |
| 164 |
|
| 165 |
def test_lots_of_continuation_lines(): |
| 166 |
s = "\\\n\\\n\\\n\\\n\\\n\\\n\nfrom __future__ import with_statement\n" |
| 167 |
f = run(s, (8, 23)) |
| 168 |
assert f == 0 |
| 169 |
|
| 170 |
def test_continuation_lines_in_docstring_single_quoted(): |
| 171 |
s = '"\\\n\\\n\\\n\\\n\\\n\\\n"\nfrom __future__ import division\n' |
| 172 |
f = run(s, (8, 24)) |
| 173 |
assert f == 0 |
| 174 |
|
| 175 |
def test_continuation_lines_in_docstring_triple_quoted(): |
| 176 |
s = '"""\\\n\\\n\\\n\\\n\\\n\\\n"""\nfrom __future__ import division\n' |
| 177 |
f = run(s, (8, 24)) |
| 178 |
assert f == 0 |
| 179 |
|
| 180 |
def test_blank_lines(): |
| 181 |
s = ('\n\t\n\nfrom __future__ import with_statement' |
| 182 |
' \n \n \nfrom __future__ import division') |
| 183 |
f = run(s, (7, 23)) |
| 184 |
assert f == 0 |
| 185 |
|
| 186 |
def test_dummy_semicolons(): |
| 187 |
s = ('from __future__ import division;\n' |
| 188 |
'from __future__ import with_statement;') |
| 189 |
f = run(s, (2, 23)) |
| 190 |
assert f == 0 |
| 191 |
|
| 192 |
def test_docstring_implicit_concatenation(): |
| 193 |
|
| 194 |
s = '''"""abc""" 'def' |
| 195 |
from __future__ import generators |
| 196 |
from __future__ import division |
| 197 |
''' |
| 198 |
f = run(s, (3, 24)) |
| 199 |
assert f == 0 |