PythonCall.jl icon indicating copy to clipboard operation
PythonCall.jl copied to clipboard

`pyconvert_add_rule` must be executed before using `pyimport` in `__init__()`

Open johroj opened this issue 1 year ago • 1 comments

Affects: PythonCall

Description:

It took me ages to figure out why the following did not manage to add a conversion rule, despite it following the documentation here and here

const mylib = pynew()
function __init__()
    pycopy!(mylib, pyimport("mylib")
    pyconvert_add_rule(".......", T, _func)
end

It seems swapping the order solves the issue

const mylib = pynew()
function __init__()
    pyconvert_add_rule(".......", T, _func)
    pycopy!(mylib, pyimport("mylib")
end

i.e. the conversion rule must be added before pyimport is called. I guess it is too late to add the rules once the python executable has started? Perhaps this should be clarified in the documentation?

johroj avatar May 16 '24 21:05 johroj

No that's a bug, you should be able to add rules at any point. What version of PythonCall are you on? Please post some complete reproducing code which demonstrates the issue.

cjdoris avatar May 20 '24 21:05 cjdoris

I am certain I verified the bug multiple times before filing the bug report, but I cannot reproduce the issue, unfortunately.

johroj avatar Sep 10 '24 12:09 johroj

Thanks for closing. I have some vague recollection that we fixed a bug a while back to do with cacheing these conversion rules - maybe that fixed your issue.

cjdoris avatar Sep 10 '24 15:09 cjdoris