Skip to content

Commit d6e7a9e

Browse files
committed
[GR-28410] Integrate TRegex error handling changes
PullRequest: graalpython/1534
2 parents 3d8d608 + 84f6e49 commit d6e7a9e

File tree

3 files changed

+23
-15
lines changed

3 files changed

+23
-15
lines changed

graalpython/com.oracle.graal.python.test/src/tests/unittest_tags/test_re.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*graalpython.lib-python.3.test.test_re.PatternReprTests.test_unknown_flags
1010
*graalpython.lib-python.3.test.test_re.PatternReprTests.test_without_flags
1111
*graalpython.lib-python.3.test.test_re.ReTests.test_anyall
12+
*graalpython.lib-python.3.test.test_re.ReTests.test_backref_group_name_in_exception
1213
*graalpython.lib-python.3.test.test_re.ReTests.test_basic_re_sub
1314
*graalpython.lib-python.3.test.test_re.ReTests.test_big_codesize
1415
*graalpython.lib-python.3.test.test_re.ReTests.test_bigcharset
@@ -35,12 +36,15 @@
3536
*graalpython.lib-python.3.test.test_re.ReTests.test_bytes_str_mixing
3637
*graalpython.lib-python.3.test.test_re.ReTests.test_case_helpers
3738
*graalpython.lib-python.3.test.test_re.ReTests.test_category
39+
*graalpython.lib-python.3.test.test_re.ReTests.test_character_set_errors
3840
*graalpython.lib-python.3.test.test_re.ReTests.test_compile
3941
*graalpython.lib-python.3.test.test_re.ReTests.test_constants
4042
*graalpython.lib-python.3.test.test_re.ReTests.test_debug_flag
4143
*graalpython.lib-python.3.test.test_re.ReTests.test_dollar_matches_twice
4244
*graalpython.lib-python.3.test.test_re.ReTests.test_enum
45+
*graalpython.lib-python.3.test.test_re.ReTests.test_error
4346
*graalpython.lib-python.3.test.test_re.ReTests.test_flags
47+
*graalpython.lib-python.3.test.test_re.ReTests.test_group_name_in_exception
4448
*graalpython.lib-python.3.test.test_re.ReTests.test_groupdict
4549
*graalpython.lib-python.3.test.test_re.ReTests.test_ignore_case
4650
*graalpython.lib-python.3.test.test_re.ReTests.test_ignore_case_range
@@ -50,7 +54,10 @@
5054
*graalpython.lib-python.3.test.test_re.ReTests.test_large_subn
5155
*graalpython.lib-python.3.test.test_re.ReTests.test_lookahead
5256
*graalpython.lib-python.3.test.test_re.ReTests.test_lookbehind
57+
*graalpython.lib-python.3.test.test_re.ReTests.test_multiple_repeat
5358
*graalpython.lib-python.3.test.test_re.ReTests.test_not_literal
59+
*graalpython.lib-python.3.test.test_re.ReTests.test_nothing_to_repeat
60+
*graalpython.lib-python.3.test.test_re.ReTests.test_other_escapes
5461
*graalpython.lib-python.3.test.test_re.ReTests.test_pickling
5562
*graalpython.lib-python.3.test.test_re.ReTests.test_qualified_re_split
5663
*graalpython.lib-python.3.test.test_re.ReTests.test_qualified_re_sub
@@ -59,6 +66,7 @@
5966
*graalpython.lib-python.3.test.test_re.ReTests.test_re_escape_non_ascii
6067
*graalpython.lib-python.3.test.test_re.ReTests.test_re_escape_non_ascii_bytes
6168
*graalpython.lib-python.3.test.test_re.ReTests.test_re_findall
69+
*graalpython.lib-python.3.test.test_re.ReTests.test_re_groupref_exists
6270
*graalpython.lib-python.3.test.test_re.ReTests.test_re_split
6371
*graalpython.lib-python.3.test.test_re.ReTests.test_re_subn
6472
*graalpython.lib-python.3.test.test_re.ReTests.test_repeat_minmax_overflow_maxrepeat
@@ -68,5 +76,6 @@
6876
*graalpython.lib-python.3.test.test_re.ReTests.test_search_star_plus
6977
*graalpython.lib-python.3.test.test_re.ReTests.test_special_escapes
7078
*graalpython.lib-python.3.test.test_re.ReTests.test_stack_overflow
79+
*graalpython.lib-python.3.test.test_re.ReTests.test_symbolic_groups
7180
*graalpython.lib-python.3.test.test_re.ReTests.test_unlimited_zero_width_repeat
7281
*graalpython.lib-python.3.test.test_re.ReTests.test_weakref

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/SREModuleBuiltins.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@
9090
import com.oracle.truffle.api.nodes.Node;
9191
import com.oracle.truffle.api.profiles.BranchProfile;
9292
import com.oracle.truffle.api.source.Source;
93+
import com.oracle.truffle.api.source.SourceSection;
9394

9495
@CoreFunctions(defineModule = "_sre")
9596
public class SREModuleBuiltins extends PythonBuiltins {
@@ -240,7 +241,7 @@ Object call(Object pattern, Object flags, PFunction fallbackCompiler,
240241
@Cached CastToJavaStringNode toStringNode,
241242
@Cached ToRegexSourceNode toRegexSourceNode,
242243
@Cached CallNode callFallbackCompilerNode,
243-
@CachedLibrary(limit = "1") InteropLibrary exceptionLib,
244+
@CachedLibrary(limit = "2") InteropLibrary exceptionLib,
244245
@CachedLibrary(limit = "2") InteropLibrary compiledRegexLib,
245246
@CachedContext(PythonLanguage.class) PythonContext context) {
246247
try {
@@ -262,13 +263,16 @@ Object call(Object pattern, Object flags, PFunction fallbackCompiler,
262263
}
263264
}
264265

265-
private Object handleError(RuntimeException e, BranchProfile syntaxError, BranchProfile potentialSyntaxError, InteropLibrary exceptionLib) {
266+
private Object handleError(RuntimeException e, BranchProfile syntaxError, BranchProfile potentialSyntaxError, InteropLibrary lib) {
266267
try {
267-
if (exceptionLib.isException(e)) {
268+
if (lib.isException(e)) {
268269
potentialSyntaxError.enter();
269-
if (exceptionLib.getExceptionType(e) == ExceptionType.PARSE_ERROR) {
270+
if (lib.getExceptionType(e) == ExceptionType.PARSE_ERROR) {
270271
syntaxError.enter();
271-
throw raise(ValueError, "%s", e);
272+
Object reason = lib.asString(lib.getExceptionMessage(e));
273+
SourceSection sourceSection = lib.getSourceLocation(e);
274+
int position = sourceSection.getCharIndex();
275+
throw raise(ValueError, reason, position);
272276
}
273277
}
274278
} catch (UnsupportedMessageException e1) {

graalpython/lib-graalpython/_sre.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -264,14 +264,9 @@ def __tregex_compile(self, pattern, flags=None):
264264
try:
265265
self.__compiled_regexes[(pattern, flags)] = tregex_compile_internal(pattern, flags, fallback_compiler)
266266
except ValueError as e:
267-
message = str(e)
268-
boundary = message.rfind(" at position ")
269-
if boundary == -1:
270-
raise error(message, pattern)
271-
else:
272-
position = int(message[boundary + len(" at position "):])
273-
message = message[:boundary]
274-
raise error(message, pattern, position)
267+
if len(e.args) == 2:
268+
raise error(e.args[0], pattern, e.args[1]) from None
269+
raise
275270
return self.__compiled_regexes[(pattern, flags)]
276271

277272

@@ -391,15 +386,15 @@ def group(pattern, match_result, group_nr, string):
391386
group_nr = int(c)
392387
group_str = group(pattern, match_result, group_nr, string)
393388
if group_str is None:
394-
raise error("invalid group reference %s at position %s" % (group_nr, pos))
389+
raise error("invalid group reference %s" % group_nr)
395390
result += repl[start:pos] + group_str
396391
start = pos + 2
397392
elif c == 'g':
398393
group_ref, group_ref_end, digits_only = self.__extract_groupname(repl, pos + 2)
399394
if group_ref:
400395
group_str = group(pattern, match_result, int(group_ref) if digits_only else pattern.groups[group_ref], string)
401396
if group_str is None:
402-
raise error("invalid group reference %s at position %s" % (group_ref, pos))
397+
raise error("invalid group reference %s" % group_ref)
403398
result += repl[start:pos] + group_str
404399
start = group_ref_end + 1
405400
elif c == '\\':

0 commit comments

Comments
 (0)