Skip to content

Commit

Permalink
Fixed a very old bug where a Generator Function (a yield function) di…
Browse files Browse the repository at this point in the history
…dn't work as a BNode prefix generator, even though it should have. This was revieled by a newly failing test after the change to better string concatenation in the BNode constructor.
  • Loading branch information
ashleysommer committed Jul 30, 2024
1 parent fcb11d2 commit da841ac
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions rdflib/term.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
from datetime import date, datetime, time, timedelta
from decimal import Decimal
from re import compile, sub
from types import GeneratorType
from typing import (
TYPE_CHECKING,
Any,
Expand Down Expand Up @@ -456,7 +457,7 @@ class BNode(IdentifiedNode):
def __new__(
cls,
value: Optional[str] = None,
_sn_gen: Optional[Callable[[], str]] = None,
_sn_gen: Optional[Union[Callable[[], str], GeneratorType]] = None,
_prefix: str = _unique_id(),
) -> BNode:
"""
Expand All @@ -466,12 +467,19 @@ def __new__(
# so that BNode values do not collide with ones created with
# a different instance of this module at some other time.
if _sn_gen is not None:
node_id = _sn_gen()
if callable(_sn_gen):
sn_result: Union[str, GeneratorType] = _sn_gen()
else:
sn_result = _sn_gen
if isinstance(sn_result, GeneratorType):
node_id = next(sn_result)
else:
node_id = sn_result
else:
node_id = uuid4().hex
# note, for two (and only two) immutable strings,
# note, for two (and only two) string variables,
# concat with + is faster than f"{x}{y}"
value = _prefix + node_id
value = _prefix + f"{node_id}"
else:
# TODO: check that value falls within acceptable bnode value range
# for RDF/XML needs to be something that can be serialized
Expand Down

0 comments on commit da841ac

Please sign in to comment.