Skip to content

Commit 709d785

Browse files
committed
Merge pull request #210 in LCL/wolframclientforpython from ECE-124/externalfunction to master
* commit '55460c8ac2e6982cef62514138eeaef67135c02c': using command instead of name returning ExternalFunction when the last expression is a class or a function
2 parents e7cfc0c + 55460c8 commit 709d785

2 files changed

Lines changed: 20 additions & 11 deletions

File tree

wolframclient/serializers/base.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -134,12 +134,22 @@ def serialize_tzinfo(
134134
def _serialize_external_object(self, o):
135135

136136
yield "System", "Python"
137-
yield "Type", "PythonFunction"
137+
138+
if callable(o):
139+
yield "Type", "PythonFunction"
140+
try:
141+
# force tuple to avoid calling this method again on `map`.
142+
yield "Arguments", tuple(map(force_text, first(inspect.getfullargspec(o))))
143+
except TypeError:
144+
# this function can fail with TypeError unsupported callable
145+
pass
146+
else:
147+
yield "Type", "PythonObject"
138148

139149
if hasattr(o, "__name__"):
140-
yield "Name", force_text(o.__name__)
150+
yield "Command", force_text(o.__name__)
141151
else:
142-
yield "Name", force_text(o.__class__.__name__)
152+
yield "Command", force_text(o.__class__.__name__)
143153

144154
is_module = inspect.ismodule(o)
145155

@@ -155,13 +165,7 @@ def _serialize_external_object(self, o):
155165
yield "IsMethod", inspect.ismethod(o),
156166
yield "IsCallable", callable(o),
157167

158-
if callable(o):
159-
try:
160-
# force tuple to avoid calling this method again on `map`.
161-
yield "Arguments", tuple(map(force_text, first(inspect.getfullargspec(o))))
162-
except TypeError:
163-
# this function can fail with TypeError unsupported callable
164-
pass
168+
165169

166170
def serialize_external_object(self, obj):
167171
return self.serialize_function(

wolframclient/utils/externalevaluate.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ def execute_from_string(code, globals={}, **opts):
6464
if not expressions:
6565
return
6666

67-
if isinstance(last(expressions), ast.Expr):
67+
last_expr = last(expressions)
68+
69+
if isinstance(last_expr, ast.Expr):
6870
result = expressions.pop(-1)
6971

7072
if expressions:
@@ -73,6 +75,9 @@ def execute_from_string(code, globals={}, **opts):
7375
if result:
7476
return eval(compile(ast.Expression(result.value), "", "eval"), env)
7577

78+
elif isinstance(last_expr, (ast.FunctionDef, ast.ClassDef)):
79+
return env[last_expr.name]
80+
7681

7782
class SideEffectSender(logging.Handler):
7883
def emit(self, record):

0 commit comments

Comments
 (0)