summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNicholas Car <nicholas.car@surroundaustralia.com>2022-01-15 12:20:39 +1000
committerGitHub <noreply@github.com>2022-01-15 12:20:39 +1000
commit55addaecde6aa70e5c9635138f43db1601d61b6d (patch)
tree32e7efe533bb69dc30afd9708fe18f9aba69dbcb
parent70a34fd7b890937b3989bbc704dba3a4cf01b9e6 (diff)
parent9be2f762385e0c35cd0956df53edfa967dfef83a (diff)
downloadrdflib-55addaecde6aa70e5c9635138f43db1601d61b6d.tar.gz
Merge pull request #1659 from gtfierro/fix-issue-1492
Make unregister_custom_function idempotent
-rw-r--r--rdflib/plugins/sparql/operators.py16
-rw-r--r--test/test_issue274.py7
2 files changed, 16 insertions, 7 deletions
diff --git a/rdflib/plugins/sparql/operators.py b/rdflib/plugins/sparql/operators.py
index f1c3c1dc..18b96658 100644
--- a/rdflib/plugins/sparql/operators.py
+++ b/rdflib/plugins/sparql/operators.py
@@ -13,6 +13,7 @@ import random
import uuid
import hashlib
import datetime as py_datetime # naming conflict with function within this module
+import warnings
from functools import reduce
@@ -611,10 +612,17 @@ def custom_function(uri, override=False, raw=False):
return decorator
-def unregister_custom_function(uri, func):
- if _CUSTOM_FUNCTIONS.get(uri, (None, None))[0] != func:
- raise ValueError("This function is not registered as %s" % uri.n3())
- del _CUSTOM_FUNCTIONS[uri]
+def unregister_custom_function(uri, func=None):
+ """
+ The 'func' argument is included for compatibility with existing code.
+ A previous implementation checked that the function associated with
+ the given uri was actually 'func', but this is not necessary as the
+ uri should uniquely identify the function.
+ """
+ if _CUSTOM_FUNCTIONS.get(uri):
+ del _CUSTOM_FUNCTIONS[uri]
+ else:
+ warnings.warn("This function is not registered as %s" % uri.n3())
def Function(e, ctx):
diff --git a/test/test_issue274.py b/test/test_issue274.py
index 248b495c..6a139735 100644
--- a/test/test_issue274.py
+++ b/test/test_issue274.py
@@ -1,5 +1,6 @@
from .testutils import eq_
from unittest import TestCase
+import pytest
from rdflib import BNode, Graph, Literal, Namespace, RDFS, XSD
from rdflib.plugins.sparql.operators import (
@@ -186,9 +187,9 @@ class TestCustom(TestCase):
def test_register_override(self):
register_custom_function(EX.f, self.f, override=True)
- def test_wrong_unregister_fails(self):
- with self.assertRaises(ValueError):
- unregister_custom_function(EX.f, lambda x, y: None)
+ def test_wrong_unregister_warns(self):
+ with pytest.warns(UserWarning):
+ unregister_custom_function(EX.notexist)
def test_f(self):
res = query("""SELECT (ex:f(42, "hello") as ?x) {}""")