JuliaPy / JuliaPy/PythonCall.jl

Idea: Do not load libpython when precompiling

Đang mở
#701 2 bình luận 0 reaction 1 người được giao Được @cjdoris nhận Xem trên GitHub
Ngôn ngữ chính
Julia
Star
1.1k
Fork
86
Merge trung bình
1 ngày 22 giờ
Pull request đã merge (30 ngày)
3

Mô tả

I propose that when PythonCall is being precompiled, we do not load/initialise libpython. Instead, have shims for the C functions that we use which raise an error or do something else trivial when used.

This means that we do not need to resolve a CondaPkg environment when precompiling, which will make precompilation much faster, simpler and more reliable.

This is breaking because it restricts which Python operations you are allowed to perform during `__init__`. I suggest that we make all of our shims return errors (clearly stating that you cannot perform this Python operation during module initialisation) except for those which return Python objects, which should return `PyPtr(1)` (or anything non-NULL) instead.

In particular, we should allow these operations at init:
- incref, decref, initialize, finalize and other such functions we only use internally
- `pyimport`, `pygetattr`, `pygetitem`, `pyrepr`, `pystr`, `pycall`, `pyhash` (always return 1)
- creation of python objects from nothing, bool, string, number, etc. (Py)
- GIL handling

And we'll need to disallow the following functions, which require an actual Python interpreter to return reasonable answers:
- `pylen`, `pyhasattr`, `pynext` (`PyIter_Next`)
- `pyconvert` (such as `PyLong_AsLongLong` etc)

Concretely, we can make shim functions like this:
```julia
shim_error() =
error("You cannot perform this Python operation during module initialisation.")

shim_PyObject_IsTrue(::PyPtr) = shim_error()
shim_PyObject_Length(::PyPtr) = shim_error()
...

shim_Py_IncRef(::PyPtr) = nothing
shim_PyImport_ImportModule(::Ptr{Cchar}) = PyPtr(1)
shim_PyObject_GetAttr(::PyPtr, ::PyPtr, ::PyPtr) = PyPtr(1)
shim_PyLong_FromLongLong(::Clonglong) = PyPtr(1)
...
```

And then in `init_pointers` set the fields of `POINTERS` like this:
```julia
p.PyObject_IsTrue = @cfunction(PyObject_IsTrue_shim, Cint, (PyPtr,))
p.PyImport_ImportModule = @cfunction(PyImport_ImportModule_shim, PyPtr, (Ptr{Cchar},))
...
```

We should also initialise all the exceptions and other object pointers to `PyPtr(1)`.

Hướng dẫn đóng góp

Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.