Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit fe23c00

Browse files
hroncokarhadthedevtiran
authoredAug 3, 2022
gh-94675: Add a regression test for rjsmin re slowdown (GH-94685)
Adds a regression test for an re slowdown observed by rjsmin. Uses multiprocessing to kill the test after SHORT_TIMEOUT. Co-authored-by: Oleg Iarygin <dralife@yandex.ru> Co-authored-by: Christian Heimes <christian@python.org>
1 parent dc2757a commit fe23c00

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed
 

‎Lib/test/test_re.py

+30-1
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
@@ -2407,6 +2416,26 @@ def test_template_function_and_flag_is_deprecated(self):
24072416
self.assertTrue(template_re1.match('ahoy'))
24082417
self.assertFalse(template_re1.match('nope'))
24092418

2419+
@unittest.skipIf(multiprocessing is None, 'test requires multiprocessing')
2420+
def test_regression_gh94675(self):
2421+
pattern = re.compile(r'(?<=[({}])(((//[^\n]*)?[\n])([\000-\040])*)*'
2422+
r'((/[^/\[\n]*(([^\n]|(\[\n]*(]*)*\]))'
2423+
r'[^/\[]*)*/))((((//[^\n]*)?[\n])'
2424+
r'([\000-\040]|(/\*[^*]*\*+'
2425+
r'([^/*]\*+)*/))*)+(?=[^\000-\040);\]}]))')
2426+
input_js = '''a(function() {
2427+
///////////////////////////////////////////////////////////////////
2428+
});'''
2429+
p = multiprocessing.Process(target=pattern.sub, args=('', input_js))
2430+
p.start()
2431+
p.join(SHORT_TIMEOUT)
2432+
try:
2433+
self.assertFalse(p.is_alive(), 'pattern.sub() timed out')
2434+
finally:
2435+
if p.is_alive():
2436+
p.terminate()
2437+
p.join()
2438+
24102439

24112440
def get_debug_out(pat):
24122441
with captured_stdout() as out:
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)
Please sign in to comment.