Skip to content

Commit 045b94c

Browse files
author
Christopher Doris
committed
undocument C.CTX and provide alternatives
1 parent b28afc1 commit 045b94c

File tree

5 files changed

+51
-3
lines changed

5 files changed

+51
-3
lines changed

docs/src/pycall.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ On this page, we give some tips for migrating between the two modules and a comp
1010
- On Unix (Linux, Mac, etc.) the Python interpreter used by PythonCall and PyCall must be the same (see below).
1111
- On Windows, it appears to be possible for PythonCall and PyCall to use different interpreters.
1212
- 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.
13-
- 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.
13+
- 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.
1414

1515
## Comparison
1616

src/C/C.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ include("pointers.jl")
1717
include("extras.jl")
1818
include("context.jl")
1919
include("gil.jl")
20+
include("api.jl")
2021

2122
function __init__()
2223
init_context()

src/C/api.jl

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"""
2+
python_executable_path()
3+
4+
Path to the Python interpreter, or `missing` if not known.
5+
"""
6+
python_executable_path() = CTX.exe_path
7+
8+
"""
9+
python_library_path()
10+
11+
Path to libpython, or `missing` if not known.
12+
"""
13+
python_library_path() = CTX.lib_path
14+
15+
"""
16+
python_library_handle()
17+
18+
Handle to the open libpython, or `C_NULL` if not known.
19+
"""
20+
python_library_handle() = CTX.lib_ptr
21+
22+
"""
23+
python_version()
24+
25+
The version of Python, or `missing` if not known.
26+
"""
27+
python_version() = CTX.version

src/PythonCall.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,16 @@ include("Compat/Compat.jl")
1717
for m in [:Core, :Convert, :PyMacro, :Wrap, :JlWrap, :Compat]
1818
for k in names(@eval($m))
1919
if k != m
20-
@eval using .$m: $k
20+
@eval const $k = $m.$k
2121
@eval export $k
2222
end
2323
end
2424
end
2525

2626
# non-exported API
27+
for k in [:python_executable_path, :python_library_path, :python_library_handle, :python_version]
28+
@eval const $k = C.$k
29+
end
2730
for k in [:pynew, :pyisnull, :pycopy!, :getptr, :pydel!, :unsafe_pynext, :PyNULL, :CONFIG]
2831
@eval const $k = Core.$k
2932
end

test/C.jl

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,18 @@
1-
# TODO
1+
@testitem "python info" begin
2+
@testset "python_executable_path" begin
3+
@test PythonCall.python_executable_path() isa String
4+
@test occursin("python", PythonCall.python_executable_path())
5+
end
6+
@testset "python_library_path" begin
7+
@test PythonCall.python_library_path() isa String
8+
@test occursin("python", PythonCall.python_library_path())
9+
end
10+
@testset "python_library_handle" begin
11+
@test PythonCall.python_library_handle() isa Ptr{Cvoid}
12+
@test PythonCall.python_library_handle() != C_NULL
13+
end
14+
@testset "python_version" begin
15+
@test PythonCall.python_version() isa VersionNumber
16+
@test PythonCall.python_version().major == 3
17+
end
18+
end

0 commit comments

Comments
 (0)