Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix code to support python 3.11 #15

Merged
merged 1 commit into from
Dec 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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