Skip to content

Commit 55784bf

Browse files
authored
Refactor (#360)
* factor out _CPython module * factor out _Py module * factor out _pyconvert, _pymacro and _pywrap modules * move pyconst macro * _Py module now loads Python juliacall module * pyconvert now forward-declared in _Py * standard module layout * don't depend on top-level of PythonCall * factor out _jlwrap module * adds conversion rules for ctypes, numpy and pandas * compat * case hack * case hack pt 2 * test coverage * add non-exported conversion API * tests --------- Co-authored-by: Christopher Doris <github.com/cjdoris>
1 parent 1497b3a commit 55784bf

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

82 files changed

+2911
-2452
lines changed

pysrc/juliacall/ipython.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,12 @@ def load_ipython_extension(ip):
6969
# redirect stdout/stderr
7070
if ip.__class__.__name__ == 'TerminalInteractiveShell':
7171
# no redirection in the terminal
72-
PythonCall.seval("""begin
72+
PythonCall.seval("""module _ipython
7373
function _flush_stdio()
7474
end
7575
end""")
7676
else:
77-
PythonCall.seval("""begin
77+
PythonCall.seval("""module _ipython
7878
const _redirected_stdout = redirect_stdout()
7979
const _redirected_stderr = redirect_stderr()
8080
const _py_stdout = PyIO(pyimport("sys" => "stdout"); line_buffering=true)
@@ -92,10 +92,10 @@ def load_ipython_extension(ip):
9292
end
9393
nothing
9494
end""")
95-
ip.events.register('post_execute', PythonCall._flush_stdio)
95+
ip.events.register('post_execute', PythonCall._ipython._flush_stdio)
9696
# push displays
9797
PythonCall.seval("""begin
98-
pushdisplay(PythonDisplay())
99-
pushdisplay(IPythonDisplay())
98+
pushdisplay(_compat.PythonDisplay())
99+
pushdisplay(_compat.IPythonDisplay())
100100
nothing
101101
end""")

src/CPython/_.jl

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"""
2+
module _CPython
3+
4+
This module provides a direct interface to the Python C API.
5+
"""
6+
module _CPython
7+
8+
using Base: @kwdef
9+
using UnsafePointers: UnsafePtr
10+
using CondaPkg: CondaPkg
11+
using Pkg: Pkg
12+
using Requires: @require
13+
using Libdl: dlpath, dlopen, dlopen_e, dlclose, dlsym, dlsym_e, RTLD_LAZY, RTLD_DEEPBIND, RTLD_GLOBAL
14+
15+
# import Base: @kwdef
16+
# import CondaPkg
17+
# import Pkg
18+
# using Libdl, Requires, UnsafePointers, Serialization, ..Utils
19+
20+
include("consts.jl")
21+
include("pointers.jl")
22+
include("extras.jl")
23+
include("context.jl")
24+
include("gil.jl")
25+
26+
function __init__()
27+
init_context()
28+
end
29+
30+
end

src/cpython/consts.jl renamed to src/CPython/consts.jl

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -324,12 +324,6 @@ const PyTypePtr = Ptr{PyTypeObject}
324324
value::T
325325
end
326326

327-
@kwdef struct PyJuliaValueObject
328-
ob_base::PyObject = PyObject()
329-
value::Int = 0
330-
weaklist::PyPtr = C_NULL
331-
end
332-
333327
@kwdef struct PyArrayInterface
334328
two::Cint = 0
335329
nd::Cint = 0

src/cpython/context.jl renamed to src/CPython/context.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,14 @@ function init_context()
137137
# Get function pointers from the library
138138
init_pointers()
139139

140+
# Compare libpath with PyCall
141+
@require PyCall="438e738f-606a-5dbb-bf0a-cddfbfd45ab0" init_pycall(PyCall)
142+
140143
# Initialize the interpreter
141144
with_gil() do
142145
CTX.is_preinitialized = Py_IsInitialized() != 0
143146
if CTX.is_preinitialized
144-
@assert CTX.which == :PyCall
147+
@assert CTX.which == :PyCall || CTX.matches_pycall isa Bool
145148
else
146149
@assert CTX.which != :PyCall
147150
# Find ProgramName and PythonHome
@@ -211,9 +214,6 @@ function init_context()
211214
ENV["JULIA_PYTHONCALL_EXE"] = CTX.exe_path::String
212215
end
213216

214-
# Compare libpath with PyCall
215-
@require PyCall="438e738f-606a-5dbb-bf0a-cddfbfd45ab0" init_pycall(PyCall)
216-
217217
with_gil() do
218218

219219
# Get the python version
File renamed without changes.
File renamed without changes.
File renamed without changes.

src/cpython/pointers.jl renamed to src/CPython/pointers.jl

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -274,8 +274,6 @@ const CAPI_OBJECTS = Set([
274274
$([:($name :: PyPtr = C_NULL) for name in CAPI_EXCEPTIONS]...)
275275
$([:($name :: PyPtr = C_NULL) for name in CAPI_OBJECTS]...)
276276
PyOS_InputHookPtr :: Ptr{Ptr{Cvoid}} = C_NULL
277-
PyJuliaBase_Type :: PyPtr = C_NULL
278-
PyExc_JuliaError :: PyPtr = C_NULL
279277
end
280278

281279
const POINTERS = CAPIPointers()
@@ -289,7 +287,7 @@ const POINTERS = CAPIPointers()
289287
end
290288
for name in CAPI_FUNCS
291289
]...)
292-
$([:(p.$name = Base.unsafe_load(Ptr{PyPtr}(dlsym(lib, $(QuoteNode(name)))))) for name in CAPI_EXCEPTIONS]...)
290+
$([:(p.$name = Base.unsafe_load(Ptr{PyPtr}(dlsym(lib, $(QuoteNode(name)))::Ptr))) for name in CAPI_EXCEPTIONS]...)
293291
$([:(p.$name = dlsym(lib, $(QuoteNode(name)))) for name in CAPI_OBJECTS]...)
294292
p.PyOS_InputHookPtr = dlsym(CTX.lib_ptr, :PyOS_InputHook)
295293
end

src/Py.jl renamed to src/Py/Py.jl

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ py_finalizer(x::Py) = GC.enqueue(getptr(x))
4747

4848
ispy(::Py) = true
4949
getptr(x::Py) = getfield(x, :ptr)
50+
pyconvert(::Type{Py}, x::Py) = x
5051

5152
setptr!(x::Py, ptr::C.PyPtr) = (setfield!(x, :ptr, ptr); x)
5253

@@ -148,7 +149,6 @@ Py(x::AbstractRange{<:Union{Int8,Int16,Int32,Int64,Int128,UInt8,UInt16,UInt32,UI
148149
Py(x::Date) = pydate(x)
149150
Py(x::Time) = pytime(x)
150151
Py(x::DateTime) = pydatetime(x)
151-
Py(x) = ispy(x) ? throw(MethodError(Py, (x,))) : pyjl(x)
152152

153153
Base.string(x::Py) = pyisnull(x) ? "<py NULL>" : pystr(String, x)
154154
Base.print(io::IO, x::Py) = print(io, string(x))
@@ -251,12 +251,8 @@ function Base.show(io::IO, ::MIME"text/plain", o::Py)
251251
end
252252
end
253253

254-
Base.show(io::IO, mime::MIME, o::Py) = pyshow(io, mime, o)
255-
Base.show(io::IO, mime::MIME"text/csv", o::Py) = pyshow(io, mime, o)
256-
Base.show(io::IO, mime::MIME"text/tab-separated-values", o::Py) = pyshow(io, mime, o)
257254

258255
Base.showable(::MIME"text/plain", ::Py) = true
259-
Base.showable(mime::MIME, o::Py) = pyshowable(mime, o)
260256

261257
Base.getproperty(x::Py, k::Symbol) = pygetattr(x, string(k))
262258
Base.getproperty(x::Py, k::String) = pygetattr(x, k)

src/Py/_.jl

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"""
2+
module _Py
3+
4+
Defines the `Py` type and directly related functions.
5+
"""
6+
module _Py
7+
8+
const VERSION = v"0.9.15"
9+
const ROOT_DIR = dirname(dirname(@__DIR__))
10+
11+
using .._CPython: _CPython as C
12+
using .._Utils: _Utils as Utils
13+
using Base: @propagate_inbounds, @kwdef
14+
using Dates: Date, Time, DateTime, year, month, day, hour, minute, second, millisecond, microsecond, nanosecond
15+
using MacroTools: @capture
16+
using Markdown: Markdown
17+
# using MacroTools, Dates, Tables, Markdown, Serialization, Requires, Pkg, REPL
18+
19+
include("gc.jl")
20+
include("Py.jl")
21+
include("err.jl")
22+
include("config.jl")
23+
include("consts.jl")
24+
include("builtins.jl")
25+
include("stdlib.jl")
26+
include("juliacall.jl")
27+
include("pyconst_macro.jl")
28+
29+
function __init__()
30+
C.with_gil() do
31+
init_consts()
32+
init_datetime()
33+
init_stdlib()
34+
init_juliacall()
35+
end
36+
end
37+
38+
end

0 commit comments

Comments
 (0)