Skip to content

Commit

Permalink
change heuristics used to distinquish between sequence and mapping su…
Browse files Browse the repository at this point in the history
…bscription avoiding actual item access (as it may be expensive)
  • Loading branch information
d-maurer committed Oct 1, 2024
1 parent d67f096 commit b36a3c0
Showing 1 changed file with 5 additions and 15 deletions.
20 changes: 5 additions & 15 deletions src/DocumentTemplate/DT_Util.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,22 +458,12 @@ def sequence_supports_subscription(obj):
We are using a heuristics.
"""
# check wether *obj* might support sequence subscription
try:
obj[0]
except IndexError:
return True
except (AttributeError, TypeError, KeyError):
if not (hasattr(obj, "__getitem__") and hasattr(obj, "__len__")):
return False
# check that *obj* is not a mapping
try:
obj[None]
except (TypeError, ValueError, RuntimeError):
# we may need more exceptions above
# (to support sequence like objects using strange exceptions)
return True
except KeyError:
pass
return False
# check that *obj* is unlikely a mapping
if (hasattr(obj, "get") or hasattr(obj, "keys")):
return False
return True


def sequence_ensure_subscription(obj):
Expand Down

0 comments on commit b36a3c0

Please sign in to comment.