Skip to content

Commit

Permalink
Fix(NameMapper): Fix mapping test
Browse files Browse the repository at this point in the history
Python 3.13 brough a new mapping type `FrameLocalsProxy`.
  • Loading branch information
phdru committed Jun 21, 2024
1 parent b4fabaf commit 6dbed77
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
18 changes: 15 additions & 3 deletions Cheetah/NameMapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,17 @@ def _isInstanceOrClass(obj):
return False


FrameLocalsProxyType = None


def isMapping(obj):
return isinstance(obj, Mapping) \
or (FrameLocalsProxyType and isinstance(obj, FrameLocalsProxyType))


def hasKey(obj, key):
"""Determine if 'obj' has 'key' """
if isinstance(obj, Mapping) and key in obj:
if isMapping(obj) and key in obj:
return True
elif hasattr(obj, key):
return True
Expand All @@ -225,7 +233,7 @@ def hasKey(obj, key):
def _valueForName(obj, name, executeCallables=False):
nameChunks = name.split('.')
for key in nameChunks:
if isinstance(obj, Mapping) and key in obj:
if isMapping(obj) and key in obj:
nextObj = obj[key]
else:
try:
Expand Down Expand Up @@ -274,7 +282,7 @@ class NotFound(LookupError):
pass

def valueForKey(obj, key):
if isinstance(obj, Mapping) and key in obj:
if isMapping(obj) and key in obj:
return obj[key]
elif hasattr(obj, key):
return getattr(obj, key)
Expand Down Expand Up @@ -319,6 +327,10 @@ def __valueForName():
try:
if not frame:
frame = inspect.stack()[1][0]
global FrameLocalsProxyType
if (FrameLocalsProxyType is None) \
and (sys.version_info[:2] >= (3, 13)):
FrameLocalsProxyType = type(frame.f_locals)
key = name.split('.')[0]
for namespace in _namespaces(frame, searchList):
if hasKey(namespace, key):
Expand Down
3 changes: 3 additions & 0 deletions docs/news.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ Bug fixes:
- Fixed ``_namemapper.c``: Silent an error
from ``PyMapping_HasKeyString`` under Python 3.13+.

- Fixed mapping test in ``NameMapper.py``:
Python 3.13 brough a new mapping type ``FrameLocalsProxy``.

Tests:

- tox: Run tests under Python 3.13.
Expand Down

0 comments on commit 6dbed77

Please sign in to comment.