Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix thrown exceptions is within functions in WCI #26

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

agoscinski
Copy link

When an exception is thrown within a WCI, the last frame is used as context to add the input lines. This does not work if the exception is thrown within a function within WCI. Therefore we now iterate through all traceback frames to find the one corresponding to WCI.

Still WIP, because the error message is reprinted completely, better to update the old one
Currently fix produces this

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
File ~/code/scicode-widgets/.tox/tests-lab-3/lib/python3.11/site-packages/widget_code_input/__init__.py:168, in WidgetCodeInput.get_function_object.<locals>.catch_exceptions.<locals>.wrapper(*args, **kwargs)
    167 try:
--> 168     return func(*args, **kwargs)
    169 except Exception as exc:

File widget_code_input:9, in rtest(x)

File ~/code/scicode-widgets/.tox/tests-lab-3/lib/python3.11/site-packages/numpy/__init__.py:324, in __getattr__(attr)
    323 if attr in __former_attrs__:
--> 324     raise AttributeError(__former_attrs__[attr])
    326 if attr == 'testing':

AttributeError: module 'numpy' has no attribute 'float'.
`np.float` was a deprecated alias for the builtin `float`. To avoid this error in existing code, use `float` by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use `np.float64` here.
The aliases was originally deprecated in NumPy 1.20; for more details and guidance see the original release note at:
    https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations

The above exception was the direct cause of the following exception:

CodeValidationError                       Traceback (most recent call last)
Cell In[3], line 1
----> 1 ex03_wci.run(5)

File ~/code/scicode-widgets/.tox/tests-lab-3/lib/python3.11/site-packages/scwidgets/code/_widget_code_input.py:87, in CodeInput.run(self, *args, **kwargs)
     86 def run(self, *args, **kwargs) -> Check.FunOutParamsT:
---> 87     return self.get_function_object()(*args, **kwargs)

File ~/code/scicode-widgets/.tox/tests-lab-3/lib/python3.11/site-packages/widget_code_input/__init__.py:171, in WidgetCodeInput.get_function_object.<locals>.catch_exceptions.<locals>.wrapper(*args, **kwargs)
    169 except Exception as exc:
    170     err_msg = format_generic_error_msg(exc, code_widget=self)
--> 171     raise CodeValidationError(err_msg, orig_exc=exc) from exc

CodeValidationError: AttributeError in code input: module 'numpy' has no attribute 'float'.
`np.float` was a deprecated alias for the builtin `float`. To avoid this error in existing code, use `float` by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use `np.float64` here.
The aliases was originally deprecated in NumPy 1.20; for more details and guidance see the original release note at:
    https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations
        7     # - try 5 and run and then change back to x then run, output does not vanish
        8     # - try "return np.float(x)", wci fails, error message does not vainsh
--->    9     np.array([5, str], dtype=np.float)
       10     return np.float(x)

When an exception is thrown within a WCI, the last frame is used as
context to add the input lines. This does not work if the exception is
thrown within a function within WCI.  Therefore we now iterate through
all traceback frames to find the one corresponding to WCI.
@agoscinski
Copy link
Author

It seems like one cannot change the error message later in the error message stack. I can iterate through the traceback frames retrieving some information about the errors in the stack but I cannot get the current exception info and adapt as with sys.exc_info() the current one.

@agoscinski agoscinski marked this pull request as ready for review July 15, 2024 06:51
agoscinski added a commit to osscar-org/scicode-widgets that referenced this pull request Jul 15, 2024
When an exception is thrown within a WCI, the last frame is used as context to add the input lines. This does not work if the exception is thrown within a function within WCI. Therefore we now iterate through all traceback frames to find the one corresponding to WCI.

Will be fixed in wci osscar-org/widget-code-input#26
agoscinski added a commit to osscar-org/scicode-widgets that referenced this pull request Sep 23, 2024
When an exception is thrown within a WCI, the last frame is used as
context to add the input lines. This does not work if the exception is
thrown within a function within WCI. Therefore we now iterate through
all traceback frames to find the one corresponding to WCI.

The corresponding PR in WCI osscar-org/widget-code-input#26
to solve it there.
agoscinski added a commit to osscar-org/scicode-widgets that referenced this pull request Sep 23, 2024
When an exception is thrown within a WCI, the last frame is used as
context to add the input lines. This does not work if the exception is
thrown within a function within WCI. Therefore we now iterate through
all traceback frames to find the one corresponding to WCI.

The corresponding PR in WCI osscar-org/widget-code-input#26
to solve it there.
agoscinski added a commit to osscar-org/scicode-widgets that referenced this pull request Sep 24, 2024
When an exception is thrown within a WCI, the last frame is used as
context to add the input lines. This does not work if the exception is
thrown within a function within WCI. Therefore we now iterate through
all traceback frames to find the one corresponding to WCI.

The corresponding PR in WCI osscar-org/widget-code-input#26
to solve it there.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant