Skip to content

Commit

Permalink
chore: remove pre python 3.5 regex related workaround (#2042)
Browse files Browse the repository at this point in the history
Removing a pre-python 3.5 regex related workaround as it uses a private
method from the `re` module, and mypy complains about this, it is also
no longer needed as we no longer support pre 3.5 versions of python.

No tests are added as all changed parts of the `Builtin_REPLACE`
function is already covered by existing tests.
  • Loading branch information
aucampia committed Jul 20, 2022
1 parent a2f96bc commit 14bb386
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 28 deletions.
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,22 @@ and will be removed for release.
<!-- -->
<!-- -->

- Removed a pre python 3.5 regex related workaround in the REPLACE SPARQL
function.
[PR #2042](https://github.com/RDFLib/rdflib/pull/2042).

<!-- -->
<!-- -->
<!-- CHANGE BARRIER: END -->
<!-- -->
<!-- -->

<!-- -->
<!-- -->
<!-- CHANGE BARRIER: START -->
<!-- -->
<!-- -->

- PLACEHOLDER.
Description of changes.
Closed [issue #....](https://github.com/RDFLib/rdflib/issues/).
Expand Down
29 changes: 1 addition & 28 deletions rdflib/plugins/sparql/operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import operator as pyop # python operators
import random
import re
import sys
import uuid
import warnings
from decimal import ROUND_HALF_UP, Decimal, InvalidOperation
Expand Down Expand Up @@ -227,28 +226,6 @@ def Builtin_REPLACE(expr, ctx):
# python uses \1, xpath/sparql uses $1
replacement = re.sub("\\$([0-9]*)", r"\\\1", replacement)

def _r(m):

# Now this is ugly.
# Python has a "feature" where unmatched groups return None
# then re.sub chokes on this.
# see http://bugs.python.org/issue1519638 , fixed and errs in py3.5

# this works around and hooks into the internal of the re module...

# the match object is replaced with a wrapper that
# returns "" instead of None for unmatched groups

class _m:
def __init__(self, m):
self.m = m
self.string = m.string

def group(self, n):
return m.group(n) or ""

return re._expand(pattern, _m(m), replacement)

cFlag = 0
if flags:
# Maps XPath REGEX flags (http://www.w3.org/TR/xpath-functions/#flags)
Expand All @@ -258,12 +235,8 @@ def group(self, n):

# @@FIXME@@ either datatype OR lang, NOT both

# this is necessary due to different treatment of unmatched groups in
# python versions. see comments above in _r(m).
compat_r = str(replacement) if sys.version_info[:2] >= (3, 5) else _r

return Literal(
re.sub(str(pattern), compat_r, text, cFlag),
re.sub(str(pattern), replacement, text, cFlag),
datatype=text.datatype,
lang=text.language,
)
Expand Down

0 comments on commit 14bb386

Please sign in to comment.