Skip to content

Commit 41ba4e3

Browse files
committed
make udapy work in Python 3.13
The behavior of `exec` and `eval` [has changed in Python 3.13](https://docs.python.org/3/whatsnew/3.13.html#defined-mutation-semantics-for-locals), so now we need to provide explicit `namespace` for these calls.
1 parent 07e4816 commit 41ba4e3

File tree

2 files changed

+6
-5
lines changed

2 files changed

+6
-5
lines changed

.circleci/config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,4 @@ workflows:
5959
- build-and-test:
6060
matrix:
6161
parameters:
62-
python-version: ["3.9", "3.13"]
62+
python-version: ["3.9", "3.11", "3.13"]

udapi/core/run.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,9 @@ def _import_blocks(block_names, block_args):
9999
:param block_args: A list of block arguments to be passed to block constructor.
100100
:return: A list of initialized objects.
101101
:rtype: list
102-
103102
"""
104103
blocks = []
104+
namespace = {} # Create a namespace dictionary to store imported classes
105105

106106
for (block_id, block_name) in enumerate(block_names):
107107
# Importing module dynamically.
@@ -115,7 +115,7 @@ def _import_blocks(block_names, block_args):
115115
try:
116116
command = "from " + module + " import " + class_name + " as b" + str(block_id)
117117
logging.debug("Trying to run command: %s", command)
118-
exec(command) # pylint: disable=exec-used
118+
exec(command, namespace) # Pass namespace as globals
119119
except ModuleNotFoundError as err:
120120
package_name = ".".join(module.split(".")[:-1])
121121
package_blocks = _blocks_in_a_package(package_name)
@@ -130,10 +130,11 @@ def _import_blocks(block_names, block_args):
130130
raise
131131

132132
# Run the imported module.
133-
kwargs = block_args[block_id] # pylint: disable=unused-variable
133+
kwargs = block_args[block_id]
134+
namespace['kwargs'] = kwargs # Add kwargs to the namespace
134135
command = "b%s(**kwargs)" % block_id
135136
logging.debug("Trying to evaluate this: %s", command)
136-
new_block_instance = eval(command) # pylint: disable=eval-used
137+
new_block_instance = eval(command, namespace) # Pass namespace as globals
137138
args = ' '.join(f"{k}={v}" for k,v in kwargs.items())
138139
blocks.append((block_name, new_block_instance, args))
139140

0 commit comments

Comments
 (0)