| 1 |
from pypy.interpreter import gateway |
| 2 |
from rpython.rlib.objectmodel import dict_to_switch |
| 3 |
from rpython.rlib.unroll import unrolling_iterable |
| 4 |
|
| 5 |
|
| 6 |
app = gateway.applevel(""" |
| 7 |
def syntax_warning(msg, fn, lineno, offset): |
| 8 |
import warnings |
| 9 |
try: |
| 10 |
warnings.warn_explicit(msg, SyntaxWarning, fn, lineno) |
| 11 |
except SyntaxWarning: |
| 12 |
raise SyntaxError(msg, (fn, lineno, offset, msg)) |
| 13 |
""", filename=__file__) |
| 14 |
_emit_syntax_warning = app.interphook("syntax_warning") |
| 15 |
del app |
| 16 |
|
| 17 |
def syntax_warning(space, msg, fn, lineno, offset): |
| 18 |
"""Raise an applevel SyntaxWarning. |
| 19 |
|
| 20 |
If the user has set this warning to raise an error, a SyntaxError will be |
| 21 |
raised.""" |
| 22 |
w_msg = space.newtext(msg) |
| 23 |
w_filename = space.newfilename(fn) |
| 24 |
w_lineno = space.newint(lineno) |
| 25 |
w_offset = space.newint(offset) |
| 26 |
_emit_syntax_warning(space, w_msg, w_filename, w_lineno, w_offset) |
| 27 |
|
| 28 |
|
| 29 |
def parse_future(space, tree, feature_flags): |
| 30 |
from pypy.interpreter.astcompiler import ast |
| 31 |
future_lineno = 0 |
| 32 |
future_column = 0 |
| 33 |
flags = 0 |
| 34 |
have_docstring = False |
| 35 |
body = None |
| 36 |
if isinstance(tree, ast.Module): |
| 37 |
body = tree.body |
| 38 |
elif isinstance(tree, ast.Interactive): |
| 39 |
body = tree.body |
| 40 |
if body is None: |
| 41 |
return 0, 0, 0 |
| 42 |
for stmt in body: |
| 43 |
if isinstance(stmt, ast.Expr) and isinstance(stmt.value, ast.Constant): |
| 44 |
constant = stmt.value |
| 45 |
assert isinstance(constant, ast.Constant) |
| 46 |
if space.isinstance_w(constant.value, space.w_unicode): |
| 47 |
if have_docstring: |
| 48 |
break |
| 49 |
else: |
| 50 |
have_docstring = True |
| 51 |
elif isinstance(stmt, ast.ImportFrom): |
| 52 |
if stmt.module == "__future__": |
| 53 |
future_lineno = stmt.lineno |
| 54 |
future_column = stmt.col_offset |
| 55 |
for alias in stmt.names: |
| 56 |
assert isinstance(alias, ast.alias) |
| 57 |
|
| 58 |
|
| 59 |
flags |= feature_flags.get(alias.name, 0) |
| 60 |
else: |
| 61 |
break |
| 62 |
else: |
| 63 |
break |
| 64 |
return flags, future_lineno, future_column |
| 65 |
|
| 66 |
|
| 67 |
class ForbiddenNameAssignment(Exception): |
| 68 |
|
| 69 |
def __init__(self, name, node): |
| 70 |
self.name = name |
| 71 |
self.node = node |
| 72 |
|
| 73 |
|
| 74 |
def check_forbidden_name(space, name): |
| 75 |
"""Raise an error if the name cannot be assigned to.""" |
| 76 |
if name == "__debug__": |
| 77 |
return True |
| 78 |
if name in ("async", "await"): |
| 79 |
space.warn(space.newtext( |
| 80 |
"'async' and 'await' will become reserved keywords" |
| 81 |
" in Python 3.7"), space.w_DeprecationWarning) |
| 82 |
return False |
| 83 |
|
| 84 |
|
| 85 |
def mangle(name, klass): |
| 86 |
if not name.startswith('__'): |
| 87 |
return name |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
if name.endswith('__') or '.' in name: |
| 92 |
return name |
| 93 |
try: |
| 94 |
i = 0 |
| 95 |
while klass[i] == '_': |
| 96 |
i = i + 1 |
| 97 |
except IndexError: |
| 98 |
return name |
| 99 |
return "_%s%s" % (klass[i:], name) |
| 100 |
|
| 101 |
|
| 102 |
def intern_if_common_string(space, w_const): |
| 103 |
|
| 104 |
from pypy.objspace.std.unicodeobject import _isidentifier |
| 105 |
if (space.is_w(space.type(w_const), space.w_unicode) and |
| 106 |
_isidentifier(space.utf8_w(w_const))): |
| 107 |
return space.new_interned_w_str(w_const) |
| 108 |
return w_const |
| 109 |
|
| 110 |
|
| 111 |
def new_identifier(space, name): |
| 112 |
|
| 113 |
|
| 114 |
for c in name: |
| 115 |
if ord(c) > 0x80: |
| 116 |
break |
| 117 |
else: |
| 118 |
return name |
| 119 |
|
| 120 |
from pypy.module.unicodedata.interp_ucd import ucd |
| 121 |
w_name = space.newtext(name) |
| 122 |
w_id = space.call_method(ucd, 'normalize', space.newtext('NFKC'), w_name) |
| 123 |
return space.text_w(w_id) |