Add Python3.8 compatible SaferScanner

This is necessary because Python 3.8 renamed `sre_parse.Pattern` to `sre_parse.State` (see https://bugs.python.org/issue34681 and https://github.com/python/cpython/pull/9310/files for details)

 patch by Eduard Tudenhöfner, reviewed by Mick Semb Wever for CASSANDRA-15573
This commit is contained in:
Eduard Tudenhoefner 2020-04-06 17:36:03 +02:00 committed by Mick Semb Wever
parent c05443c098
commit 24c8a21c1c
No known key found for this signature in database
GPG Key ID: E91335D77E3E87CB
2 changed files with 18 additions and 1 deletions

View File

@ -1,5 +1,5 @@
4.0-alpha4
* Allow cqlsh to run with Python2.7/Python3.6+ (CASSANDRA-15659)
* Allow cqlsh to run with Python2.7/Python3.6+ (CASSANDRA-15659,CASSANDRA-15573)
* Improve logging around incremental repair (CASSANDRA-15599)
* Do not check cdc_raw_directory filesystem space if CDC disabled (CASSANDRA-15688)
* Replace array iterators with get by index (CASSANDRA-15394)

View File

@ -21,6 +21,7 @@
import re
import six
from sre_constants import BRANCH, SUBPATTERN, GROUPREF, GROUPREF_IGNORE, GROUPREF_EXISTS
from sys import version_info
class SaferScannerBase(re.Scanner):
@ -82,4 +83,20 @@ class Py36SaferScanner(SaferScannerBase):
self.scanner = re.sre_compile.compile(p)
class Py38SaferScanner(SaferScannerBase):
def __init__(self, lexicon, flags=0):
self.lexicon = lexicon
p = []
s = re.sre_parse.State()
s.flags = flags
for phrase, action in lexicon:
gid = s.opengroup()
p.append(re.sre_parse.SubPattern(s, [(SUBPATTERN, (gid, 0, 0, re.sre_parse.parse(phrase, flags))), ]))
s.closegroup(gid, p[-1])
p = re.sre_parse.SubPattern(s, [(BRANCH, (None, p))])
self.p = p
self.scanner = re.sre_compile.compile(p)
SaferScanner = Py36SaferScanner if six.PY3 else Py2SaferScanner
SaferScanner = Py38SaferScanner if version_info >= (3, 8) else SaferScanner