| 1 |
|
| 2 |
from rpython.rlib import rutf8 |
| 3 |
from pypy.interpreter.baseobjspace import W_Root |
| 4 |
from pypy.interpreter.error import OperationError, oefmt |
| 5 |
from pypy.interpreter import unicodehelper |
| 6 |
from pypy.interpreter.pyparser.error import SyntaxError |
| 7 |
from rpython.rlib.rstring import StringBuilder |
| 8 |
|
| 9 |
|
| 10 |
class W_FString(W_Root): |
| 11 |
def __init__(self, unparsed, raw_mode, token, content_offset=0): |
| 12 |
assert isinstance(unparsed, str) |
| 13 |
self.unparsed = unparsed |
| 14 |
self.raw_mode = raw_mode |
| 15 |
self.current_index = 0 |
| 16 |
self.token = token |
| 17 |
|
| 18 |
|
| 19 |
self.content_offset = content_offset |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
def parsestr(space, encoding, s, token=None, astbuilder=None): |
| 26 |
"""Parses a string or unicode literal, and return usually |
| 27 |
a wrapped value. If we get an f-string, then instead return |
| 28 |
an unparsed but unquoted W_FString instance. |
| 29 |
|
| 30 |
If encoding=None, the source string is ascii only. |
| 31 |
In other cases, the source string is in utf-8 encoding. |
| 32 |
|
| 33 |
When a bytes string is returned, it will be encoded with the |
| 34 |
original encoding. |
| 35 |
|
| 36 |
Yes, it's very inefficient. |
| 37 |
Yes, CPython has very similar code. |
| 38 |
""" |
| 39 |
|
| 40 |
|
| 41 |
ps = 0 |
| 42 |
quote = s[ps] |
| 43 |
rawmode = False |
| 44 |
unicode_literal = True |
| 45 |
saw_u = False |
| 46 |
saw_f = False |
| 47 |
|
| 48 |
|
| 49 |
if quote == 'b' or quote == 'B': |
| 50 |
ps += 1 |
| 51 |
quote = s[ps] |
| 52 |
unicode_literal = False |
| 53 |
elif quote == 'u' or quote == 'U': |
| 54 |
ps += 1 |
| 55 |
quote = s[ps] |
| 56 |
saw_u = True |
| 57 |
elif quote == 'r' or quote == 'R': |
| 58 |
ps += 1 |
| 59 |
quote = s[ps] |
| 60 |
rawmode = True |
| 61 |
elif quote == 'f' or quote == 'F': |
| 62 |
ps += 1 |
| 63 |
quote = s[ps] |
| 64 |
saw_f = True |
| 65 |
|
| 66 |
if not saw_u: |
| 67 |
if quote == 'r' or quote == 'R': |
| 68 |
ps += 1 |
| 69 |
quote = s[ps] |
| 70 |
rawmode = True |
| 71 |
elif quote == 'b' or quote == 'B': |
| 72 |
ps += 1 |
| 73 |
quote = s[ps] |
| 74 |
unicode_literal = False |
| 75 |
elif quote == 'f' or quote == 'F': |
| 76 |
ps += 1 |
| 77 |
quote = s[ps] |
| 78 |
saw_f = True |
| 79 |
|
| 80 |
if quote != "'" and quote != '"': |
| 81 |
raise_app_valueerror(space, |
| 82 |
'Internal error: parser passed unquoted literal') |
| 83 |
ps += 1 |
| 84 |
q = len(s) - 1 |
| 85 |
assert q >= 0 |
| 86 |
if s[q] != quote: |
| 87 |
raise_app_valueerror(space, 'Internal error: parser passed unmatched ' |
| 88 |
'quotes in literal') |
| 89 |
if q-ps >= 4 and s[ps] == quote and s[ps+1] == quote: |
| 90 |
|
| 91 |
ps += 2 |
| 92 |
if s[q-1] != quote or s[q-2] != quote: |
| 93 |
raise_app_valueerror(space, 'Internal error: parser passed ' |
| 94 |
'unmatched triple quotes in literal') |
| 95 |
q -= 2 |
| 96 |
|
| 97 |
assert 0 <= ps <= q |
| 98 |
if unicode_literal: |
| 99 |
if saw_f: |
| 100 |
return W_FString(s[ps:q], rawmode, token, ps) |
| 101 |
elif rawmode: |
| 102 |
length = unicodehelper.check_utf8_or_raise(space, s, ps, q) |
| 103 |
return space.newutf8(s[ps:q], length) |
| 104 |
else: |
| 105 |
if encoding is None: |
| 106 |
substr = s[ps:q] |
| 107 |
else: |
| 108 |
length = unicodehelper.check_utf8_or_raise(space, s, ps, q) |
| 109 |
if "\\" not in s: |
| 110 |
return space.newutf8(s[ps:q], length) |
| 111 |
substr = decode_unicode_utf8(space, s, ps, q) |
| 112 |
r = decode_unicode_escape(space, substr, astbuilder, token) |
| 113 |
v, length, pos = r |
| 114 |
return space.newutf8(v, length) |
| 115 |
|
| 116 |
substr = s[ps : q] |
| 117 |
|
| 118 |
for i, c in enumerate(substr): |
| 119 |
if ord(c) > 0x7F: |
| 120 |
raise SyntaxError("bytes can only contain ASCII literal characters", |
| 121 |
token.lineno, token.column + ps + i + 1) |
| 122 |
|
| 123 |
if rawmode or '\\' not in substr: |
| 124 |
return space.newbytes(substr) |
| 125 |
|
| 126 |
v, first_escape_error_char = _PyString_DecodeEscape( |
| 127 |
space, substr, 'strict', encoding) |
| 128 |
if first_escape_error_char != '': |
| 129 |
if len(first_escape_error_char) == 4: |
| 130 |
msg = "invalid octal escape sequence '%s'" % first_escape_error_char |
| 131 |
else: |
| 132 |
msg = "invalid escape sequence '%s'" % first_escape_error_char |
| 133 |
if astbuilder: |
| 134 |
astbuilder.deprecation_warn(msg, token) |
| 135 |
|
| 136 |
return space.newbytes(v) |
| 137 |
|
| 138 |
def decode_unicode_utf8(space, s, ps, q): |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
lis = [] |
| 143 |
end = q |
| 144 |
|
| 145 |
|
| 146 |
while ps < end: |
| 147 |
if s[ps] == '\\': |
| 148 |
lis.append(s[ps]) |
| 149 |
ps += 1 |
| 150 |
if ps >= end or ord(s[ps]) & 0x80: |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
lis.append("u005c") |
| 156 |
if ps >= end: |
| 157 |
break |
| 158 |
if ord(s[ps]) & 0x80: |
| 159 |
cp = rutf8.codepoint_at_pos(s, ps) |
| 160 |
hexa = hex(cp + 0x10000000) |
| 161 |
lis.append('\\U0') |
| 162 |
lis.append(hexa[3:]) |
| 163 |
ps = rutf8.next_codepoint_pos(s, ps) |
| 164 |
else: |
| 165 |
lis.append(s[ps]) |
| 166 |
ps += 1 |
| 167 |
return ''.join(lis) |
| 168 |
|
| 169 |
def _PyString_DecodeEscape(space, s, errors, recode_encoding): |
| 170 |
""" |
| 171 |
Unescape a backslash-escaped string. If recode_encoding is non-zero, |
| 172 |
the string is UTF-8 encoded and should be re-encoded in the |
| 173 |
specified encoding. |
| 174 |
""" |
| 175 |
builder = StringBuilder(len(s)) |
| 176 |
ps = 0 |
| 177 |
end = len(s) |
| 178 |
first_escape_error_char = '' |
| 179 |
while ps < end: |
| 180 |
if s[ps] != '\\': |
| 181 |
|
| 182 |
|
| 183 |
if recode_encoding and ord(s[ps]) & 0x80: |
| 184 |
w, ps = decode_utf8_recode(space, s, ps, end, recode_encoding) |
| 185 |
|
| 186 |
builder.append(w) |
| 187 |
else: |
| 188 |
builder.append(s[ps]) |
| 189 |
ps += 1 |
| 190 |
continue |
| 191 |
|
| 192 |
ps += 1 |
| 193 |
if ps == end: |
| 194 |
raise_app_valueerror(space, 'Trailing \\ in string') |
| 195 |
prevps = ps |
| 196 |
ch = s[ps] |
| 197 |
ps += 1 |
| 198 |
|
| 199 |
if ch == '\n': |
| 200 |
pass |
| 201 |
elif ch == '\\': |
| 202 |
builder.append('\\') |
| 203 |
elif ch == "'": |
| 204 |
builder.append("'") |
| 205 |
elif ch == '"': |
| 206 |
builder.append('"') |
| 207 |
elif ch == 'b': |
| 208 |
builder.append("\010") |
| 209 |
elif ch == 'f': |
| 210 |
builder.append('\014') |
| 211 |
elif ch == 't': |
| 212 |
builder.append('\t') |
| 213 |
elif ch == 'n': |
| 214 |
builder.append('\n') |
| 215 |
elif ch == 'r': |
| 216 |
builder.append('\r') |
| 217 |
elif ch == 'v': |
| 218 |
builder.append('\013') |
| 219 |
elif ch == 'a': |
| 220 |
builder.append('\007') |
| 221 |
elif ch in '01234567': |
| 222 |
|
| 223 |
span = ps |
| 224 |
span += (span < end) and (s[span] in '01234567') |
| 225 |
span += (span < end) and (s[span] in '01234567') |
| 226 |
octal = s[prevps : span] |
| 227 |
|
| 228 |
|
| 229 |
raw = int(octal, 8) |
| 230 |
if raw >= 256: |
| 231 |
first_escape_error_char = "\\" + octal |
| 232 |
num = raw & 0xFF |
| 233 |
builder.append(chr(num)) |
| 234 |
ps = span |
| 235 |
elif ch == 'x': |
| 236 |
if ps+2 <= end and isxdigit(s[ps]) and isxdigit(s[ps + 1]): |
| 237 |
hexa = s[ps : ps + 2] |
| 238 |
num = int(hexa, 16) |
| 239 |
builder.append(chr(num)) |
| 240 |
ps += 2 |
| 241 |
else: |
| 242 |
if errors == 'strict': |
| 243 |
raise_app_valueerror( |
| 244 |
space, "invalid \\x escape at position %d" % (ps - 2)) |
| 245 |
elif errors == 'replace': |
| 246 |
builder.append('?') |
| 247 |
elif errors == 'ignore': |
| 248 |
pass |
| 249 |
else: |
| 250 |
raise oefmt(space.w_ValueError, "decoding error; " |
| 251 |
"unknown error handling code: %s", errors) |
| 252 |
if ps+1 <= end and isxdigit(s[ps]): |
| 253 |
ps += 1 |
| 254 |
else: |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
builder.append('\\') |
| 259 |
ps -= 1 |
| 260 |
assert ps >= 0 |
| 261 |
if first_escape_error_char == '': |
| 262 |
first_escape_error_char = "\\" + ch |
| 263 |
continue |
| 264 |
|
| 265 |
|
| 266 |
buf = builder.build() |
| 267 |
return buf, first_escape_error_char |
| 268 |
|
| 269 |
|
| 270 |
def PyString_DecodeEscape(space, s, errors, recode_encoding): |
| 271 |
buf, first_escape_error_char = _PyString_DecodeEscape(space, s, errors, recode_encoding) |
| 272 |
if first_escape_error_char != '': |
| 273 |
if len(first_escape_error_char) == 4: |
| 274 |
space.warn(space.newtext("invalid octal escape sequence '%s'" % first_escape_error_char), space.w_DeprecationWarning) |
| 275 |
else: |
| 276 |
space.warn(space.newtext("invalid escape sequence '%s'" % first_escape_error_char), space.w_DeprecationWarning) |
| 277 |
return buf, first_escape_error_char |
| 278 |
|
| 279 |
|
| 280 |
def decode_unicode_escape(space, string, astbuilder, token): |
| 281 |
from pypy.interpreter.unicodehelper import str_decode_unicode_escape |
| 282 |
from pypy.module._codecs import interp_codecs |
| 283 |
state = space.fromcache(interp_codecs.CodecState) |
| 284 |
unicodedata_handler = state.get_unicodedata_handler(space) |
| 285 |
w_s = space.newbytes(string) |
| 286 |
s, ulen, blen, first_escape_error_char = str_decode_unicode_escape( |
| 287 |
space, string, w_s, "strict", |
| 288 |
final=True, |
| 289 |
errorhandler=state.decode_error_handler, |
| 290 |
ud_handler=unicodedata_handler) |
| 291 |
if first_escape_error_char is not None and astbuilder is not None: |
| 292 |
if len(first_escape_error_char) > 1: |
| 293 |
msg = "invalid octal escape sequence '\\%s'" % first_escape_error_char |
| 294 |
else: |
| 295 |
msg = "invalid escape sequence '\\%s'" % first_escape_error_char |
| 296 |
astbuilder.deprecation_warn(msg, token) |
| 297 |
return s, ulen, blen |
| 298 |
|
| 299 |
def isxdigit(ch): |
| 300 |
return (ch >= '0' and ch <= '9' or |
| 301 |
ch >= 'a' and ch <= 'f' or |
| 302 |
ch >= 'A' and ch <= 'F') |
| 303 |
|
| 304 |
|
| 305 |
def check_utf8(space, s, ps, end): |
| 306 |
assert ps >= 0 |
| 307 |
pt = ps |
| 308 |
|
| 309 |
while ps < end and ord(s[ps]) & 0x80: |
| 310 |
ps += 1 |
| 311 |
try: |
| 312 |
rutf8.check_utf8(s, True, pt, ps) |
| 313 |
except rutf8.CheckError as e: |
| 314 |
lgt, flag = rutf8.check_utf8(s, True, pt, e.pos) |
| 315 |
unicodehelper.decode_error_handler(space)('strict', 'utf8', |
| 316 |
'invalid utf-8', s, pt + lgt, pt + lgt + 1) |
| 317 |
return s[pt:ps] |
| 318 |
|
| 319 |
def decode_utf8_recode(space, s, ps, end, recode_encoding): |
| 320 |
p = ps |
| 321 |
while p < end and ord(s[p]) & 0x80: |
| 322 |
p += 1 |
| 323 |
lgt = unicodehelper.check_utf8_or_raise(space, s, ps, p) |
| 324 |
w_v = unicodehelper.encode(space, space.newutf8(s[ps:p], lgt), |
| 325 |
recode_encoding) |
| 326 |
v = space.bytes_w(w_v) |
| 327 |
return v, p |
| 328 |
|
| 329 |
def raise_app_valueerror(space, msg): |
| 330 |
raise OperationError(space.w_ValueError, space.newtext(msg)) |