Skip to content
Merged
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
9 changes: 6 additions & 3 deletions src/PyCall.jl
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,11 @@ function trygetproperty(o::PyObject, s::AbstractString, d)
return p == C_NULL ? d : PyObject(p)
end

propertynames(o::PyObject) = ispynull(o) ? Symbol[] : map(x->Symbol(first(x)), PyIterator{PyObject}(pycall(inspect."getmembers", PyObject, o)))
function propertynames(o::PyObject)
ispynull(o) && return Symbol[]
names = @pycheckn ccall((@pysym :PyObject_Dir), PyObject, (PyPtr,), o)
return convert(Vector{Symbol}, names)
end

# avoiding method ambiguity
setproperty!(o::PyObject, s::Symbol, v) = _setproperty!(o,s,v)
Expand Down Expand Up @@ -366,8 +370,7 @@ hasproperty(o::PyObject, s::AbstractString) = pyhasproperty(o, s)

#########################################################################

keys(o::PyObject) = Symbol[m[1] for m in pycall(inspect."getmembers",
PyVector{Tuple{Symbol,PyObject}}, o)]
@deprecate keys(o::PyObject) propertynames(o)

#########################################################################
# Create anonymous composite w = pywrap(o) wrapping the object o
Expand Down
7 changes: 6 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -295,17 +295,22 @@ const PyInt = pyversion < v"3" ? Int : Clonglong
class A:
class B:
C = 1
@property
def D(self):
raise NotImplementedError
"""
A = py"A"
@test hasproperty(A, "B")
@test getproperty(A, "B") == py"A.B"
@test :B in propertynames(A)
@static if VERSION >= v"0.7-"
@test :D in propertynames(A.B)
@test A.B.C == 1
@test_throws KeyError A.X
end
setproperty!(py"A.B", "C", 2)
@test py"A.B.C" == 2
@test_deprecated keys(A)

# buffers
let b = PyCall.PyBuffer(pyutf8("test string"))
Expand All @@ -317,7 +322,7 @@ const PyInt = pyversion < v"3" ? Int : Clonglong

let o = PyObject(1+2im)
@test PyCall.hasproperty(o, :real) # replace by Base.hasproperty in the future
@test :real in keys(o)
@test :real in propertynames(o)
@test o.real == 1
end

Expand Down