Skip to content

Commit 155549a

Browse files
authored
CodeInput function property is now wrapped (#90)
This is not a breaking change because `inspect.unwrap` did not remove the try-catch block. The interface for the unwrapped function is now changed to `unwrapped_function`.
1 parent 0f65564 commit 155549a

File tree

1 file changed

+27
-28
lines changed

1 file changed

+27
-28
lines changed

src/scwidgets/code/_widget_code_input.py

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def __init__(
8282
)
8383

8484
@property
85-
def function(self) -> types.FunctionType:
85+
def unwrapped_function(self) -> types.FunctionType:
8686
"""
8787
Return the compiled function object.
8888
@@ -94,11 +94,33 @@ def function(self) -> types.FunctionType:
9494
:raise SyntaxError: if the function code has syntax errors (or if
9595
the function name is not a valid identifier)
9696
"""
97-
return inspect.unwrap(self.wrapped_function)
97+
globals_dict = {
98+
"__builtins__": globals()["__builtins__"],
99+
"__name__": "__main__",
100+
"__doc__": None,
101+
"__package__": None,
102+
}
103+
104+
if not is_valid_variable_name(self.function_name):
105+
raise SyntaxError("Invalid function name '{}'".format(self.function_name))
106+
107+
# Optionally one could do a ast.parse here already, to check syntax
108+
# before execution
109+
try:
110+
exec(
111+
compile(self.full_function_code, __name__, "exec", dont_inherit=True),
112+
globals_dict,
113+
)
114+
except SyntaxError as exc:
115+
raise CodeValidationError(
116+
format_syntax_error_msg(exc), orig_exc=exc
117+
) from exc
118+
119+
return globals_dict[self.function_name]
98120

99121
def __call__(self, *args, **kwargs) -> Check.FunOutParamsT:
100122
"""Calls the wrapped function"""
101-
return self.wrapped_function(*args, **kwargs)
123+
return self.function(*args, **kwargs)
102124

103125
def compatible_with_signature(self, parameters: List[str]) -> str:
104126
"""
@@ -223,7 +245,7 @@ def get_function_body(function: types.FunctionType) -> str:
223245
return source
224246

225247
@property
226-
def wrapped_function(self) -> types.FunctionType:
248+
def function(self) -> types.FunctionType:
227249
"""
228250
Return the compiled function object wrapped by an try-catch block
229251
raising a `CodeValidationError`.
@@ -236,29 +258,6 @@ def wrapped_function(self) -> types.FunctionType:
236258
:raise SyntaxError: if the function code has syntax errors (or if
237259
the function name is not a valid identifier)
238260
"""
239-
globals_dict = {
240-
"__builtins__": globals()["__builtins__"],
241-
"__name__": "__main__",
242-
"__doc__": None,
243-
"__package__": None,
244-
}
245-
246-
if not is_valid_variable_name(self.function_name):
247-
raise SyntaxError("Invalid function name '{}'".format(self.function_name))
248-
249-
# Optionally one could do a ast.parse here already, to check syntax
250-
# before execution
251-
try:
252-
exec(
253-
compile(self.full_function_code, __name__, "exec", dont_inherit=True),
254-
globals_dict,
255-
)
256-
except SyntaxError as exc:
257-
raise CodeValidationError(
258-
format_syntax_error_msg(exc), orig_exc=exc
259-
) from exc
260-
261-
function_object = globals_dict[self.function_name]
262261

263262
def catch_exceptions(func):
264263
@wraps(func)
@@ -274,7 +273,7 @@ def wrapper(*args, **kwargs):
274273

275274
return wrapper
276275

277-
return catch_exceptions(function_object)
276+
return catch_exceptions(self.unwrapped_function)
278277

279278

280279
# Temporary fix until https://github.com/osscar-org/widget-code-input/pull/26

0 commit comments

Comments
 (0)