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

Export of SymPy symbol from module gives ReadOnlyMemoryError #365

Open
mzaffalon opened this issue Jul 24, 2020 · 5 comments
Open

Export of SymPy symbol from module gives ReadOnlyMemoryError #365

mzaffalon opened this issue Jul 24, 2020 · 5 comments

Comments

@mzaffalon
Copy link
Contributor

When I import the following module

module MyMod
using SymPy: symbols
export b
b = symbols("b")
end

and I try to access b from the REPL, I get:

julia> using MyMod; b
Error showing value of type SymPy.Sym:
ERROR: ReadOnlyMemoryError()
Stacktrace:
 [1] macro expansion at C:\Users\michele.zaffalon\.julia\packages\PyCall\zqDXB\src\exception.jl:93 [inlined]
 [2] #110 at C:\Users\michele.zaffalon\.julia\packages\PyCall\zqDXB\src\pyfncall.jl:43 [inlined]
 [3] disable_sigint at .\c.jl:446 [inlined]
 [4] __pycall! at C:\Users\michele.zaffalon\.julia\packages\PyCall\zqDXB\src\pyfncall.jl:42 [inlined]
 [5] _pycall!(::PyCall.PyObject, ::PyCall.PyObject, ::Tuple{SymPy.Sym}, ::Int64, ::Ptr{Nothing}) at C:\Users\michele.zaffalon\.julia\packages\PyCall\zqDXB\src\pyfncall.jl:29
 [6] _pycall! at C:\Users\michele.zaffalon\.julia\packages\PyCall\zqDXB\src\pyfncall.jl:11 [inlined]
 [7] #_#117 at C:\Users\michele.zaffalon\.julia\packages\PyCall\zqDXB\src\pyfncall.jl:86 [inlined]
 [8] (::PyCall.PyObject)(::SymPy.Sym) at C:\Users\michele.zaffalon\.julia\packages\PyCall\zqDXB\src\pyfncall.jl:86
 [9] show(::IOContext{REPL.Terminals.TTYTerminal}, ::MIME{Symbol("text/plain")}, ::SymPy.Sym) at C:\Users\michele.zaffalon\.julia\packages\SymPy\7D3De\src\types.jl:73
 [10] display(::REPL.REPLDisplay, ::MIME{Symbol("text/plain")}, ::Any) at D:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.4\REPL\src\REPL.jl:137
 [11] display(::REPL.REPLDisplay, ::Any) at D:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.4\REPL\src\REPL.jl:141
 [12] display(::Any) at .\multimedia.jl:323
 [13] #invokelatest#1 at .\essentials.jl:712 [inlined]
 [14] invokelatest at .\essentials.jl:711 [inlined]
 [15] print_response(::IO, ::Any, ::Bool, ::Bool, ::Any) at D:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.4\REPL\src\REPL.jl:161
 [16] print_response(::REPL.AbstractREPL, ::Any, ::Bool, ::Bool) at D:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.4\REPL\src\REPL.jl:146
 [17] (::REPL.var"#do_respond#38"{Bool,REPL.var"#48#57"{REPL.LineEditREPL,REPL.REPLHistoryProvider},REPL.LineEditREPL,REPL.LineEdit.Prompt})(::Any, ::Any, ::Any) at D:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.4\REPL\src\REPL.jl:729
 [18] #invokelatest#1 at .\essentials.jl:712 [inlined]
 [19] invokelatest at .\essentials.jl:711 [inlined]
 [20] run_interface(::REPL.Terminals.TextTerminal, ::REPL.LineEdit.ModalInterface, ::REPL.LineEdit.MIState) at D:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.4\REPL\src\LineEdit.jl:2354
 [21] run_frontend(::REPL.LineEditREPL, ::REPL.REPLBackendRef) at D:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.4\REPL\src\REPL.jl:1055
 [22] run_repl(::REPL.AbstractREPL, ::Any) at D:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.4\REPL\src\REPL.jl:206
 [23] (::Base.var"#764#766"{Bool,Bool,Bool,Bool})(::Module) at .\client.jl:383
 [24] #invokelatest#1 at .\essentials.jl:712 [inlined]
 [25] invokelatest at .\essentials.jl:711 [inlined]
 [26] run_main_repl(::Bool, ::Bool, ::Bool, ::Bool, ::Bool) at .\client.jl:367
 [27] exec_options(::Base.JLOptions) at .\client.jl:305
 [28] _start() at .\client.jl:484

Ref: https://discourse.julialang.org/t/export-of-sympy-symbol-from-module-gives-readonlymemoryerror/43426

@jverzani
Copy link
Collaborator

SOrry this took so long. It is cumbersome to do this, but https://github.com/jverzani/SymPyTest.jl/blob/master/src/SymPyTest.jl shows how.

One thing showing up in the test package is testing against x64 fails under travis.

@mzaffalon
Copy link
Contributor Author

Thank you for taking the time of doing this.
After loading your example module, I still get a ERROR: UndefVarError: b not defined. Is this what you mean with the comment "no show method, so careful"?

@jverzani
Copy link
Collaborator

No, sorry. That comment is that showing this object b = SymPy.Sym(SymPy.PyCall.PyNULL()); before its __pyobject__ is set will cause an error. You should have a b defined when the package is loaded.

Can you run the tests in the package?

This worked for me on a new image from within a Jupyter notebook:

] add https://github.com/jverzani/SymPyTest.jl
] test SymPyTest

If that fails, I wonder if there is an underlying OS issue.

@mzaffalon
Copy link
Contributor Author

mzaffalon commented Jul 22, 2021

Sorry for the delayed answer. Yes, the test run. I ended up doing something like this:

module SymPyExportDefs

using SymPy

export a, b, c

const a = SymPy.Sym(SymPy.PyCall.PyNULL());
const b = SymPy.Sym(SymPy.PyCall.PyNULL());
const c = SymPy.Sym(SymPy.PyCall.PyNULL());

function __init__()
    copy!(a.__pyobject__, SymPy.PyCall.PyObject(SymPy.symbols("a", positive=true)))
    copy!(b.__pyobject__, SymPy.PyCall.PyObject(SymPy.symbols("b")))
    copy!(c.__pyobject__, SymPy.PyCall.PyObject(a + b))
end

end

and the output is

julia> c
a + b
julia> c - a
b
julia> sqrt(a^2)
a

Indeed exporting a definition is cumbersome.

@jverzani
Copy link
Collaborator

Yeah, that is how it needs to be done. It isn't pretty, but seemingly needed to work with run-time generated objects from python.

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

No branches or pull requests

2 participants