Skip to content

Commit

Permalink
undocument C.CTX and provide alternatives
Browse files Browse the repository at this point in the history
  • Loading branch information
Christopher Doris committed Nov 7, 2023
1 parent b28afc1 commit 045b94c
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 3 deletions.
2 changes: 1 addition & 1 deletion docs/src/pycall.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ On this page, we give some tips for migrating between the two modules and a comp
- On Unix (Linux, Mac, etc.) the Python interpreter used by PythonCall and PyCall must be the same (see below).
- On Windows, it appears to be possible for PythonCall and PyCall to use different interpreters.
- To force PythonCall to use the same Python interpreter as PyCall, set the environment variable `JULIA_PYTHONCALL_EXE` to `"@PyCall"`. Note that this will opt out of automatic dependency management using CondaPkg.
- Alternatively, to force PyCall to use the same interpreter as PythonCall, set the environment variable `PYTHON` to `PythonCall.C.CTX.exe_path` and then `Pkg.build("PyCall")`. You will need to do this each time you change project, because PythonCall by default uses a different Python for each project.
- Alternatively, to force PyCall to use the same interpreter as PythonCall, set the environment variable `PYTHON` to `PythonCall.python_executable_path()` and then `Pkg.build("PyCall")`. You will need to do this each time you change project, because PythonCall by default uses a different Python for each project.

## Comparison

Expand Down
1 change: 1 addition & 0 deletions src/C/C.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ include("pointers.jl")
include("extras.jl")
include("context.jl")
include("gil.jl")
include("api.jl")

function __init__()
init_context()
Expand Down
27 changes: 27 additions & 0 deletions src/C/api.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"""
python_executable_path()
Path to the Python interpreter, or `missing` if not known.
"""
python_executable_path() = CTX.exe_path

"""
python_library_path()
Path to libpython, or `missing` if not known.
"""
python_library_path() = CTX.lib_path

"""
python_library_handle()
Handle to the open libpython, or `C_NULL` if not known.
"""
python_library_handle() = CTX.lib_ptr

"""
python_version()
The version of Python, or `missing` if not known.
"""
python_version() = CTX.version
5 changes: 4 additions & 1 deletion src/PythonCall.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@ include("Compat/Compat.jl")
for m in [:Core, :Convert, :PyMacro, :Wrap, :JlWrap, :Compat]
for k in names(@eval($m))
if k != m
@eval using .$m: $k
@eval const $k = $m.$k
@eval export $k
end
end
end

# non-exported API
for k in [:python_executable_path, :python_library_path, :python_library_handle, :python_version]
@eval const $k = C.$k
end
for k in [:pynew, :pyisnull, :pycopy!, :getptr, :pydel!, :unsafe_pynext, :PyNULL, :CONFIG]
@eval const $k = Core.$k
end
Expand Down
19 changes: 18 additions & 1 deletion test/C.jl
Original file line number Diff line number Diff line change
@@ -1 +1,18 @@
# TODO
@testitem "python info" begin
@testset "python_executable_path" begin
@test PythonCall.python_executable_path() isa String
@test occursin("python", PythonCall.python_executable_path())
end
@testset "python_library_path" begin
@test PythonCall.python_library_path() isa String
@test occursin("python", PythonCall.python_library_path())
end
@testset "python_library_handle" begin
@test PythonCall.python_library_handle() isa Ptr{Cvoid}
@test PythonCall.python_library_handle() != C_NULL
end
@testset "python_version" begin
@test PythonCall.python_version() isa VersionNumber
@test PythonCall.python_version().major == 3
end
end

0 comments on commit 045b94c

Please sign in to comment.