Skip to content

Do not show exception in non-debugging mode - #160

Merged
stevengj merged 5 commits into
JuliaPy:masterfrom
tkf:noshow
May 3, 2018
Merged

stevengj merged 5 commits into
JuliaPy:masterfrom
tkf:noshow

Conversation

@tkf

@tkf tkf commented Apr 28, 2018

Copy link
Copy Markdown
Member

fixes #159

Comment thread julia/core.py Outdated
self._debug("Retrieving exception infos...")
stderr = self.api.jl_stderr_obj()
self._debug("libjulia stderr = " + str(stderr))
self.api.jl_call2(self.api.show, stderr, exoc)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just need to wrap self._debug("libjulia stderr = " + str(stderr)) in if self.is_debugging: but I find wrapping the whole debug related code makes code easier to read.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I get the impression that the only point of this call to self.api.show in the first place was to make sure that the whole Julia exception is printed — maybe this code can simply be deleted now that showerror is included in the Python exception?

@stevengj

stevengj commented Apr 28, 2018 •

Copy link
Copy Markdown
Member

I don't agree with this change. If you evaluate Julia code and it throws an exception, then the exception should be shown — in fact, it should probably be converted to a Python exception and thrown in Python. (When you evaluate Python code that throws an exception, it is not generally hidden from the caller, and pyjulia should be no different.)

If import is printing an excessive number of exception messages, then import should be fixed.

@tkf

tkf commented Apr 28, 2018

Copy link
Copy Markdown
Member Author

it should probably be converted to a Python exception and thrown in Python

It is raised as Python exception at the end of Julia.check_exception, as I noted in #159:

pyjulia/julia/core.py

Lines 426 to 432 in 2eb704c

self.api.jl_call2(self.api.show, stderr, exoc)
self._debug("show called ...")
# self.api.jl_printf(self.api.jl_stderr_stream(), "\n");
exception_type = self.api.jl_typeof_str(exoc).decode('utf-8')
raise JuliaError(u'Exception \'{}\' occurred while calling julia code:\n{}'
.format(exception_type, src))

This is just below the code I edited.

The quirk in pyjulia I reported in #159 is that the error in Julia reported twice: first via Base.show and then Python exception.

@stevengj

stevengj commented Apr 28, 2018 •

Copy link
Copy Markdown
Member

Ah, okay. But in that case the actual Julia exception output should probably be put into the exception text rather than into debugging output, via something like:

self.sprint(self.eval("Base.showerror"), exoc)

instead of jl_typeof_str

@tkf

tkf commented Apr 28, 2018

Copy link
Copy Markdown
Member Author

So I implemented what you suggested but actually I'm not sure if it is the best way to go, since, in principle, check_exception can now have infinite recursion (and so a stackoverflow). This is because self.sprint and self.showerror may in turn call check_exception. I know this is very unlikely but feels a bit unsafe.

I think a better approach would be to define a helper Julia function/macro that evals the code and wraps the result into a "Result" struct, something like

struct Result{T}
    result :: Nullable{T}
    error :: String
end

where .result is null and .error is set to showerror of the exception, when the code throws.

@tkf

tkf commented Apr 28, 2018 •

Copy link
Copy Markdown
Member Author

(Actually, retrieving Nullable requires Julia function calls so maybe just let result to be the original returned object and .error to be nothing (no error) or a String (error message). It doesn't have to type-stable when talking to Python.)

Comment thread julia/core.py Outdated
if res is None:
exception = self.api.jl_typeof_str(exoc).decode('utf-8')
else:
exception = self.sprint(self.showerror, self._as_pyobj(res))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is _as_pyobj needed here?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, nevermind, I see that exoc is a raw Julia pointer.

Comment thread julia/core.py Outdated
# self.api.jl_printf(self.api.jl_stderr_stream(), "\n");

exception_type = self.api.jl_typeof_str(exoc).decode('utf-8')
res = self.api.jl_call2(void_p(self.api.convert), void_p(self.api.PyObject), void_p(exoc))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably should be self.api.jl_call1(void_p(self.api.PyObject), void_p(exoc)) (the convert method may go away in a future PyCall version, since this is really a constructor call)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a little worried about the interaction of this raw jl_value_t* manipulation with garbage collection in Julia, but I guess that's something that needs to be fixed more generally.

@tkf tkf Apr 29, 2018 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm.... It segfaults somehow when I apply your suggestion tkf@e8da821

Also, I should say I'm not fully aware of how Julia's GC works and how GCs of Julia and Python interact in PyCall.jl and pyjulia. I just followed the similar existing code in pyjulia. I'd be a lot more confident if you can pull only the first commit.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reading git blame, it looks like it is changed from self.api.jl_call1(void_p(self.api.PyObject), void_p(ans)) to self.api.jl_call2(void_p(self.api.convert), void_p(self.api.PyObject), void_p(ans)) in 0912ac4#diff-2f4f81856f2b92c6a4bb1e5bba2cf28aR380 by @Keno (PR #54). Not clear why from git message.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe jl_call1 only works for actual Function objects these days, and we'll have to dig to find out how to call the constructor. Let's continue to use convert for now, I guess.

tkf added a commit to tkf/pyjulia that referenced this pull request Apr 29, 2018
> the convert method may go away in a future PyCall version, since this
> is really a constructor call
>
> --- @stevengj
> JuliaPy#160 (comment)
@tkf

tkf commented May 1, 2018

Copy link
Copy Markdown
Member Author

@stevengj Are there any other issues to be solved before merging it?

@stevengj

stevengj commented May 1, 2018

Copy link
Copy Markdown
Member

As mentioned above, I think the "Retrieving exception infos" debugging output code can be deleted — it is redundant now that the exception output is included in the Python exception.

@tkf

tkf commented May 1, 2018

Copy link
Copy Markdown
Member Author

Sorry, I didn't notice that comment. I forgot GitHub issue is not linearizing the comments...

I thought it could be useful in some edge cases. For example, if an error occurred during self.add_module_functions("Base"), attributes self.sprint and self.showerror may not be set.

(...which makes me realize that I should make sure that they exist before using it; tweaked the code)

Having said that, I'm not the one maintaining the code so I'm happy with removing if self.is_debugging: block I added. I did it in the last commit.

@stevengj

stevengj commented May 1, 2018

Copy link
Copy Markdown
Member

I guess CI is broken right now, but is it possible to add a test? For example, sqrt(-1) throws.

@tkf

tkf commented May 1, 2018

Copy link
Copy Markdown
Member Author

There is already a test:

def test_call_error(self):
try:
julia._call('undefined_function_name()')
self.fail('No error?')
except JuliaError:
pass

I tested it locally.

@tkf

tkf commented May 2, 2018

Copy link
Copy Markdown
Member Author

I made the test a bit more specific to make sure we call showerror.

@stevengj
stevengj merged commit 7ebad22 into JuliaPy:master May 3, 2018
@stevengj

stevengj commented May 3, 2018

Copy link
Copy Markdown
Member

Thanks!

1 similar comment
@tkf

tkf commented May 3, 2018

Copy link
Copy Markdown
Member Author

Thanks!

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.

Julia exceptions are unconditionally printed

2 participants