Skip to content

Commit

Permalink
safer pop from working namespace
Browse files Browse the repository at this point in the history
log failure as a warning because it's weird, but not impossible
  • Loading branch information
minrk committed Oct 30, 2024
1 parent 7742389 commit 59b5a3f
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions ipyparallel/engine/kernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,10 @@ def do_apply(self, content, bufs, msg_id, reply_metadata):
try:
working = shell.user_ns

prefix = "_" + str(msg_id).replace("-", "") + "_"
prefix = f"_{str(msg_id).replace('-', '_')}_"

f, args, kwargs = unpack_apply_message(bufs, working, copy=False)

fname = getattr(f, '__name__', 'f')

fname = prefix + "f"
argname = prefix + "args"
kwargname = prefix + "kwargs"
Expand All @@ -153,13 +151,18 @@ def do_apply(self, content, bufs, msg_id, reply_metadata):
ns = {fname: f, argname: args, kwargname: kwargs, resultname: None}
# print ns
working.update(ns)
code = f"{resultname} = {fname}(*{argname},**{kwargname})"
code = f"{resultname} = {fname}(*{argname}, **{kwargname})"
try:
exec(code, shell.user_global_ns, shell.user_ns)
result = working.get(resultname)
finally:
for key in ns:
working.pop(key)
try:
working.pop(key)
except KeyError:
self.log.warning(
f"Failed to undefine temporary apply variable {key}, already undefined"
)

result_buf = serialize_object(
result,
Expand Down

0 comments on commit 59b5a3f

Please sign in to comment.