Skip to content

Commit 6f1cf68

Browse files
committed
00385: pythongh-94675: Add a regression test for rjsmin re slowdown
This tests a speed regression in the re module which prevented chromium from building in Fedora. python#94685
1 parent ae25e2b commit 6f1cf68

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

Lib/test/test_re.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from test.support import (gc_collect, bigmemtest, _2G,
22
cpython_only, captured_stdout,
3-
check_disallow_instantiation, is_emscripten, is_wasi)
3+
check_disallow_instantiation, is_emscripten, is_wasi,
4+
SHORT_TIMEOUT)
45
import locale
56
import re
67
import string
@@ -11,6 +12,14 @@
1112
from re import Scanner
1213
from weakref import proxy
1314

15+
# some platforms lack working multiprocessing
16+
try:
17+
import _multiprocessing
18+
except ImportError:
19+
multiprocessing = None
20+
else:
21+
import multiprocessing
22+
1423
# Misc tests from Tim Peters' re.doc
1524

1625
# WARNING: Don't change details in these tests if you don't know
@@ -2438,6 +2447,26 @@ def test_template_function_and_flag_is_deprecated(self):
24382447
self.assertTrue(template_re1.match('ahoy'))
24392448
self.assertFalse(template_re1.match('nope'))
24402449

2450+
@unittest.skipIf(multiprocessing is None, 'test requires multiprocessing')
2451+
def test_regression_gh94675(self):
2452+
pattern = re.compile(r'(?<=[({}])(((//[^\n]*)?[\n])([\000-\040])*)*'
2453+
r'((/[^/\[\n]*(([^\n]|(\[\n]*(]*)*\]))'
2454+
r'[^/\[]*)*/))((((//[^\n]*)?[\n])'
2455+
r'([\000-\040]|(/\*[^*]*\*+'
2456+
r'([^/*]\*+)*/))*)+(?=[^\000-\040);\]}]))')
2457+
input_js = '''a(function() {
2458+
///////////////////////////////////////////////////////////////////
2459+
});'''
2460+
p = multiprocessing.Process(target=pattern.sub, args=('', input_js))
2461+
p.start()
2462+
p.join(SHORT_TIMEOUT)
2463+
try:
2464+
self.assertFalse(p.is_alive(), 'pattern.sub() timed out')
2465+
finally:
2466+
if p.is_alive():
2467+
p.terminate()
2468+
p.join()
2469+
24412470

24422471
def get_debug_out(pat):
24432472
with captured_stdout() as out:
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add a regression test for :mod:`re` exponentional slowdown when using rjsmin.

0 commit comments

Comments
 (0)