Skip to content

Commit 52a6170

Browse files
author
Christopher Doris
committed
add Py to the API
1 parent 740fe2f commit 52a6170

File tree

3 files changed

+21
-20
lines changed

3 files changed

+21
-20
lines changed

src/API/exports.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# Core
2+
export Py
23
export ispy
34
export pyis
45
export pyrepr

src/API/types.jl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,21 @@
88
PYCONVERT_PRIORITY_FALLBACK = -100
99
end
1010

11+
# Core
12+
13+
"""
14+
Py(x)
15+
16+
Convert `x` to a Python object, of type `Py`.
17+
18+
Conversion happens according to [these rules](@ref jl2py-conversion).
19+
20+
Such an object supports attribute access (`obj.attr`), indexing (`obj[idx]`), calling
21+
(`obj(arg1, arg2)`), iteration (`for x in obj`), arithmetic (`obj + obj2`) and comparison
22+
(`obj > obj2`), among other things. These operations convert all their arguments to `Py` and
23+
return `Py`.
24+
"""
25+
mutable struct Py
26+
ptr::Ptr{Cvoid}
27+
Py(::Val{:new}, ptr::Ptr{Cvoid}) = finalizer(Core.py_finalizer, new(ptr))
28+
end

src/Core/Py.jl

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,31 +24,13 @@ Get the underlying pointer from the Python object `x`.
2424
"""
2525
getptr(x) = ispy(x) ? getptr(Py(x)::Py) : throw(MethodError(getptr, (x,)))
2626

27-
"""
28-
Py(x)
29-
30-
Convert `x` to a Python object, of type `Py`.
31-
32-
Conversion happens according to [these rules](@ref jl2py-conversion).
33-
34-
Such an object supports attribute access (`obj.attr`), indexing (`obj[idx]`), calling
35-
(`obj(arg1, arg2)`), iteration (`for x in obj`), arithmetic (`obj + obj2`) and comparison
36-
(`obj > obj2`), among other things. These operations convert all their arguments to `Py` and
37-
return `Py`.
38-
"""
39-
mutable struct Py
40-
ptr::C.PyPtr
41-
Py(::Val{:new}, ptr::C.PyPtr) = finalizer(py_finalizer, new(ptr))
42-
end
43-
export Py
44-
4527
py_finalizer(x::Py) = GC.enqueue(getptr(x))
4628

4729
ispy(::Py) = true
48-
getptr(x::Py) = getfield(x, :ptr)
30+
getptr(x::Py) = C.PyPtr(getfield(x, :ptr))
4931
pyconvert(::Type{Py}, x::Py) = x
5032

51-
setptr!(x::Py, ptr::C.PyPtr) = (setfield!(x, :ptr, ptr); x)
33+
setptr!(x::Py, ptr::C.PyPtr) = (setfield!(x, :ptr, Ptr{Cvoid}(ptr)); x)
5234

5335
const PYNULL_CACHE = Py[]
5436

0 commit comments

Comments
 (0)