From 1d92d5d47d04d0dd524ee77e7138193a92f57d50 Mon Sep 17 00:00:00 2001 From: Harsh Daryani Date: Mon, 10 Apr 2023 12:29:52 +0000 Subject: [PATCH] Fix for : "[YCQL] YCQLSH - module 're' has no attribute 'sre_parse' - with Python 3.11 #16780" --- pylib/cqlshlib/saferscanner.py | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/pylib/cqlshlib/saferscanner.py b/pylib/cqlshlib/saferscanner.py index 8949321..6d7ba57 100644 --- a/pylib/cqlshlib/saferscanner.py +++ b/pylib/cqlshlib/saferscanner.py @@ -20,7 +20,10 @@ import re import six -from sre_constants import BRANCH, SUBPATTERN, GROUPREF, GROUPREF_IGNORE, GROUPREF_EXISTS +try: + from sre_constants import BRANCH, SUBPATTERN, GROUPREF, GROUPREF_IGNORE, GROUPREF_EXISTS +except ImportError: + from re._constants import BRANCH, SUBPATTERN, GROUPREF, GROUPREF_IGNORE, GROUPREF_EXISTS from sys import version_info @@ -99,5 +102,24 @@ def __init__(self, lexicon, flags=0): self.scanner = re.sre_compile.compile(p) +class Py311SaferScanner(SaferScannerBase): + + def __init__(self, lexicon, flags=0): + self.lexicon = lexicon + p = [] + s = re._parser.State() + s.flags = flags + for phrase, action in lexicon: + gid = s.opengroup() + p.append(re._parser.SubPattern(s, [(SUBPATTERN, (gid, 0, 0, re._parser.parse(phrase, flags))), ])) + s.closegroup(gid, p[-1]) + p = re._parser.SubPattern(s, [(BRANCH, (None, p))]) + self.p = p + self.scanner = re._compiler.compile(p) + + SaferScanner = Py36SaferScanner if six.PY3 else Py2SaferScanner -SaferScanner = Py38SaferScanner if version_info >= (3, 8) else SaferScanner +if version_info >= (3, 11): + SaferScanner = Py311SaferScanner +elif version_info >= (3, 8): + SaferScanner = Py38SaferScanner