Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions src/Core/Py.jl
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,25 @@ Py(x::Date) = pydate(x)
Py(x::Time) = pytime(x)
Py(x::DateTime) = pydatetime(x)

function _py_on_main_thread_if_needed(f)
if C.PyGILState_Check() == 1
f()
else
C.on_main_thread(f)
end
end

Base.string(x::Py) = pyisnull(x) ? "<py NULL>" : pystr(String, x)
Base.print(io::IO, x::Py) = print(io, string(x))

function Base.show(io::IO, x::Py)
_py_on_main_thread_if_needed() do
_show(io, x)
nothing
end
end

function _show(io::IO, x::Py)
if get(io, :typeinfo, Any) == Py
if pyisnull(x)
print(io, "NULL")
Expand Down Expand Up @@ -292,13 +307,9 @@ function _propertynames(x::Py, private::Bool)
end

function Base.propertynames(x::Py, private::Bool = false)
if C.PyGILState_Check() == 1
_py_on_main_thread_if_needed() do
_propertynames(x, private)
else
C.on_main_thread() do
_propertynames(x, private)
end::Vector{Symbol}
end
end::Vector{Symbol}
end

Base.Bool(x::Py) = pytruth(x)
Expand Down
14 changes: 13 additions & 1 deletion src/Wrap/PyDict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,19 @@ function Base.iterate(x::PyDict{K,V}, it::Py = pyiter(x)) where {K,V}
return (k => v, it)
end

function Base.iterate(x::Base.KeySet{K,PyDict{K,V}}, it::Py = pyiter(x.dict)) where {K,V}
function Base.iterate(x::Base.KeySet{K,<:PyDict{K}})::Union{Nothing,Tuple{K,Py}} where {K}
_py_on_main_thread_if_needed() do
_iterate(x, pyiter(x.dict))
end
end

function Base.iterate(x::Base.KeySet{K,<:PyDict{K}}, it::Py)::Union{Nothing,Tuple{K,Py}} where {K}
_py_on_main_thread_if_needed() do
_iterate(x, it)
end
end

function _iterate(x::Base.KeySet{K,<:PyDict{K}}, it::Py) where {K}
k_ = unsafe_pynext(it)
pyisnull(k_) && return nothing
k = pyconvert(K, k_)
Expand Down
2 changes: 1 addition & 1 deletion src/Wrap/Wrap.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ using Base: @propagate_inbounds
using Tables: Tables
using UnsafePointers: UnsafePtr

import ..Core: Py, ispy
import ..Core: Py, ispy, _py_on_main_thread_if_needed

include("PyIterable.jl")
include("PyDict.jl")
Expand Down
1 change: 1 addition & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
Markdown = "d6f4376e-aef5-505a-96c1-9c027394607a"
PyCall = "438e738f-606a-5dbb-bf0a-cddfbfd45ab0"
PythonCall = "6099a3de-0909-46bc-b1f4-468b9a2dfc0d"
REPL = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb"
Serialization = "9e88b42a-f829-5b0c-bbe9-9e923198166b"
Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Expand Down
10 changes: 10 additions & 0 deletions test/Wrap.jl
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,16 @@ end
@testset "iterate keys" begin
@test collect(keys(z)) == ["foo"]
end
@testset "complete keys from another thread" begin
using REPL
task = Threads.@spawn begin
completions, _, _ = REPL.REPLCompletions.completions("y[", 2, @__MODULE__)
length(completions)
end
wait(task)
completion_count = fetch(task)
@test completion_count == 1
end
@testset "getindex" begin
@test z["foo"] === 12
@test_throws KeyError z["bar"]
Expand Down
Loading