Skip to content

Commit 957519b

Browse files
committed
make _sre fallback for TRegex configurable, so we can pass one shared-engine leak test
1 parent a664d48 commit 957519b

File tree

4 files changed

+18
-4
lines changed

4 files changed

+18
-4
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
import com.oracle.graal.python.nodes.truffle.PythonArithmeticTypes;
6666
import com.oracle.graal.python.runtime.ExecutionContext.IndirectCallContext;
6767
import com.oracle.graal.python.runtime.PythonContext;
68+
import com.oracle.graal.python.runtime.PythonCore;
6869
import com.oracle.graal.python.runtime.PythonOptions;
6970
import com.oracle.graal.python.runtime.exception.PythonErrorType;
7071
import com.oracle.truffle.api.CompilerAsserts;
@@ -96,6 +97,12 @@ protected List<? extends NodeFactory<? extends PythonBuiltinBaseNode>> getNodeFa
9697
return SREModuleBuiltinsFactory.getFactories();
9798
}
9899

100+
@Override
101+
public void initialize(PythonCore core) {
102+
builtinConstants.put("_use_sre_fallback", core.getContext().getLanguage().getEngineOption(PythonOptions.TRegexUsesSREFallback));
103+
super.initialize(core);
104+
}
105+
99106
@Builtin(name = "_build_regex_engine", minNumOfPositionalArgs = 1)
100107
@GenerateNodeFactory
101108
abstract static class BuildRegexEngine extends PythonUnaryBuiltinNode {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/PythonOptions.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,12 @@ private PythonOptions() {
173173

174174
// disabling TRegex has an effect on the _sre Python functions that are
175175
// dynamically created, so we cannot change that option again.
176-
@EngineOption @Option(category = OptionCategory.EXPERT, help = "Use the optimized TRegex engine and call the CPython sre engine only as a fallback. Default true") //
176+
@EngineOption @Option(category = OptionCategory.EXPERT, help = "Use the optimized TRegex engine. Default true") //
177177
public static final OptionKey<Boolean> WithTRegex = new OptionKey<>(true);
178178

179+
@EngineOption @Option(category = OptionCategory.EXPERT, help = "Use the CPython sre engine as a fallback to the TRegex engine.") //
180+
public static final OptionKey<Boolean> TRegexUsesSREFallback = new OptionKey<>(true);
181+
179182
@Option(category = OptionCategory.EXPERT, help = "Switch on/off using lazy strings for performance reasons. Default true.") //
180183
public static final OptionKey<Boolean> LazyStrings = new OptionKey<>(true);
181184

graalpython/lib-graalpython/_sre.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,12 @@ def fallback_compiler(pattern, flags):
134134
if engine_builder:
135135
global TREGEX_ENGINE_STR
136136
global TREGEX_ENGINE_BYTES
137-
TREGEX_ENGINE_STR = engine_builder("Flavor=PythonStr", configure_fallback_compiler("str"))
138-
TREGEX_ENGINE_BYTES = engine_builder("Flavor=PythonBytes", configure_fallback_compiler("bytes"))
137+
if _use_sre_fallback:
138+
TREGEX_ENGINE_STR = engine_builder("Flavor=PythonStr", configure_fallback_compiler("str"))
139+
TREGEX_ENGINE_BYTES = engine_builder("Flavor=PythonBytes", configure_fallback_compiler("bytes"))
140+
else:
141+
TREGEX_ENGINE_STR = engine_builder("Flavor=PythonStr")
142+
TREGEX_ENGINE_BYTES = engine_builder("Flavor=PythonBytes")
139143

140144
def new_compile(p, flags=0):
141145
if isinstance(p, (str, bytes)):

mx.graalpython/mx_graalpython.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ def punittest(ars):
208208
assert run_leak_launcher(["--lang", "python", "--code", "import _testcapi, mmap, bz2; print(memoryview(b'').nbytes)", "--forbidden-class", "com.oracle.graal.python.builtins.objects.object.PythonObject", "--python.ForceImportSite"]) == 0
209209

210210
# test leaks with shared engine Python code only
211-
assert run_leak_launcher(["--lang", "python", "--shared-engine", "--code", "pass", "--forbidden-class", "com.oracle.graal.python.builtins.objects.object.PythonObject", "--python.ForceImportSite"]) == 0
211+
assert run_leak_launcher(["--lang", "python", "--shared-engine", "--code", "pass", "--forbidden-class", "com.oracle.graal.python.builtins.objects.object.PythonObject", "--python.ForceImportSite", "--python.TRegexUsesSREFallback=false"]) == 0
212212

213213
# test leaks with shared engine when some C module code is involved
214214
# Not working due to GR-26175

0 commit comments

Comments
 (0)