Skip to content

Commit

Permalink
Merge pull request #17 from yugabyte/Py3.11_module_re_fix
Browse files Browse the repository at this point in the history
Fix for : "[YCQL] YCQLSH - module 're' has no attribute 'sre_parse' -…
  • Loading branch information
HarshDaryani896 authored Apr 10, 2023
2 parents 2921897 + 1d92d5d commit e075785
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions pylib/cqlshlib/saferscanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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

0 comments on commit e075785

Please sign in to comment.