Skip to content

Commit

Permalink
Fix code to support python 3.11
Browse files Browse the repository at this point in the history
as part of cleanup in pyhton source code,
some of the regex code is using internal resources
that was renamed.

Ref: python/cpython#91308
  • Loading branch information
fruch committed Dec 28, 2022
1 parent 3d6c8d9 commit c2dc72a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build-push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
- uses: actions/checkout@v3

- name: Build wheels
uses: pypa/cibuildwheel@v2.9.0
uses: pypa/cibuildwheel@v2.11.4

- uses: actions/upload-artifact@v3
with:
Expand Down
29 changes: 18 additions & 11 deletions pylib/cqlshlib/saferscanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,27 @@
from sre_constants import BRANCH, SUBPATTERN, GROUPREF, GROUPREF_IGNORE, GROUPREF_EXISTS
from sys import version_info

try:
sre_parse = re._parser
sre_compile = re._compiler
except AttributeError:
sre_parse = re.sre_parse
sre_compile = re.sre_compile


class SaferScannerBase(re.Scanner):

@classmethod
def subpat(cls, phrase, flags):
return cls.scrub_sub(re.sre_parse.parse(phrase, flags), flags)
return cls.scrub_sub(sre_parse.parse(phrase, flags), flags)

@classmethod
def scrub_sub(cls, sub, flags):
scrubbedsub = []
seqtypes = (type(()), type([]))
for op, arg in sub.data:
if type(arg) in seqtypes:
arg = [cls.scrub_sub(a, flags) if isinstance(a, re.sre_parse.SubPattern) else a
arg = [cls.scrub_sub(a, flags) if isinstance(a, sre_parse.SubPattern) else a
for a in arg]
if op in (BRANCH, SUBPATTERN):
arg = [None] + arg[1:]
Expand All @@ -46,39 +53,39 @@ def scrub_sub(cls, sub, flags):
raise ValueError("Named captures not allowed in SaferScanner lexicon")
if sub.pattern.flags ^ flags:
raise ValueError("RE flag setting not allowed in SaferScanner lexicon (%s)" % (bin(sub.pattern.flags),))
return re.sre_parse.SubPattern(sub.pattern, scrubbedsub)
return sre_parse.SubPattern(sub.pattern, scrubbedsub)


class Py36SaferScanner(SaferScannerBase):

def __init__(self, lexicon, flags=0):
self.lexicon = lexicon
p = []
s = re.sre_parse.Pattern()
s = sre_parse.Pattern()
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))), ]))
p.append(sre_parse.SubPattern(s, [(SUBPATTERN, (gid, 0, 0, sre_parse.parse(phrase, flags))), ]))
s.closegroup(gid, p[-1])
p = re.sre_parse.SubPattern(s, [(BRANCH, (None, p))])
p = sre_parse.SubPattern(s, [(BRANCH, (None, p))])
self.p = p
self.scanner = re.sre_compile.compile(p)
self.scanner = sre_compile.compile(p)


class Py38SaferScanner(SaferScannerBase):

def __init__(self, lexicon, flags=0):
self.lexicon = lexicon
p = []
s = re.sre_parse.State()
s = 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))), ]))
p.append(sre_parse.SubPattern(s, [(SUBPATTERN, (gid, 0, 0, sre_parse.parse(phrase, flags))), ]))
s.closegroup(gid, p[-1])
p = re.sre_parse.SubPattern(s, [(BRANCH, (None, p))])
p = sre_parse.SubPattern(s, [(BRANCH, (None, p))])
self.p = p
self.scanner = re.sre_compile.compile(p)
self.scanner = sre_compile.compile(p)


SaferScanner = Py38SaferScanner if version_info >= (3, 8) else Py36SaferScanner

0 comments on commit c2dc72a

Please sign in to comment.