Skip to content

Julia exceptions are unconditionally printed #159

Closed

Description

@tkf

I noticed that import julia.<julia module> prints a lot of UndefVarError when <julia module> exports undefined variables. I think exporting undefined variables is better be avoided ideally. However, it seems that pyjulia explicitly allows it by suppressing all exceptions

pyjulia/julia/core.py

Lines 155 to 162 in 2eb704c

def isafunction(julia, julia_name, mod_name=""):
code = "isa({}, Function)".format(julia_name)
if mod_name:
code = "isa({}.{}, Function)".format(mod_name, julia_name)
try:
return julia.eval(code)
except:
return False

Anyway, here is an example:

Python 3.6.4 (default, Jan  5 2018, 02:35:40)
[GCC 7.2.1 20171224] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import julia
>>> j = julia.Julia()
>>> j.using('DiffEqBase')
>>> import julia.DiffEqBase
UndefVarError(:ConstantLagDDEProblem)UndefVarError(:ConstantLagDDEProblem)UndefVarError(:DAEParameterizedFunction)UndefVarError(:DAEParameterizedFunction)UndefVarError(:DDEParameterizedFunction)UndefVarError(:DDEParameterizedFunction)UndefVarError(:DEParameters)UndefVarError(:DEParameters)UndefVarError(:HasParamFuncs)UndefVarError(:HasParamFuncs)UndefVarError(:Mesh)UndefVarError(:Mesh)UndefVarError(:OutputParameterizedFunction)UndefVarError(:OutputParameterizedFunction)UndefVarError(:ParameterizedFunction)UndefVarError(:ParameterizedFunction)UndefVarError(:ProbParameterizedFunction)UndefVarError(:ProbParameterizedFunction)UndefVarError(:SecondOrderSDEProblem)UndefVarError(:SecondOrderSDEProblem)UndefVarError(:construct_correlated_noisefunc)UndefVarError(:construct_correlated_noisefunc)UndefVarError(:has_paramfuncs)UndefVarError(:has_paramfuncs)UndefVarError(:is_sparse_noise)UndefVarError(:is_sparse_noise)UndefVarError(:isautodifferentiable)UndefVarError(:isautodifferentiable)UndefVarError(:noise_class)UndefVarError(:noise_class)UndefVarError(:num_params)UndefVarError(:num_params)UndefVarError(:param_values)UndefVarError(:param_values)UndefVarError(:set_param_values!)UndefVarError(:set_param_values!)UndefVarError(:white_noise_func_wrapper)UndefVarError(:white_noise_func_wrapper)UndefVarError(:white_noise_func_wrapper!)UndefVarError(:white_noise_func_wrapper!)>>>

When I initialize it with julia.Julia(debug=True), I get

...
exception occured? None
exception occured? None
exception occured? None
exception occured? 140476532807120
Retrieving exception infos...
libjulia stderr = 140476567600544
UndefVarError(:white_noise_func_wrapper)show called ...
exception occured? 140476532807296
Retrieving exception infos...
libjulia stderr = 140476567600544
UndefVarError(:white_noise_func_wrapper)show called ...
exception occured? 140476532807472
Retrieving exception infos...
libjulia stderr = 140476567600544
UndefVarError(:white_noise_func_wrapper!)show called ...
exception occured? 140476532807648
Retrieving exception infos...
libjulia stderr = 140476567600544
UndefVarError(:white_noise_func_wrapper!)show called ...

It looks like this happens because Base.show is called whenever exception occurred:

pyjulia/julia/core.py

Lines 416 to 432 in 2eb704c

def check_exception(self, src=None):
exoc = self.api.jl_exception_occurred()
self._debug("exception occured? " + str(exoc))
if not exoc:
# self._debug("No Exception")
self.api.jl_exception_clear()
return
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)
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))

But why do you need to print it when JuliaError is going to be raised anyway?

(see also: SciML/diffeqpy#24)

Activity

  1. tkf commented on Apr 28, 2018

    @tkf
    MemberAuthor

    Here is a workaround I came up with:

    import julia
    j = julia.Julia()
    j.using('DiffEqBase')
    show = j.api.show
    try:
        j.api.show = j._call('identity')
        import julia.DiffEqBase
    finally:
        j.api.show = show
  2. tkf commented on Apr 28, 2018

    @tkf
    MemberAuthor

    Probably j.api.show = j._call('tuple') should be used instead of j.api.show = j._call('identity') since j.api.show is called with two arguments. I have no idea why above snippet works.

  3. added a commit that references this issue on Apr 28, 2018
    44c62d6
  4. stevengj commented on Apr 28, 2018

    @stevengj
    Member

    The correct fix here is just to fix import. The current design, where it attempts to import every symbol in the module all at once, is broken; see also #144.

  5. tkf commented on Apr 28, 2018

    @tkf
    MemberAuthor

    The problem here is more basic than module loader; it's about Julia.check_exception. Implementing lazy import still requires this to be fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions