Skip to content

Commit 0c1bf09

Browse files
committed
Changes for PyCall API update getindex -> getproperty; fix #67
1 parent 33a8496 commit 0c1bf09

File tree

11 files changed

+92
-82
lines changed

11 files changed

+92
-82
lines changed

REQUIRE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
julia 0.7
2-
PyCall 1.17.1
2+
PyCall 1.90.0

docs/src/index.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,12 +175,12 @@ if `myproxy` is a `ServiceProxy` object, it can be called with
175175
in the `RobotOS` module with the same syntax as in rospy.
176176

177177
### Message Constants
178-
Message constants may be accessed using `getindex` syntax. For example for
178+
Message constants may be accessed using `getproperty` syntax. For example for
179179
[visualization_msgs/Marker.msg](http://docs.ros.org/api/visualization_msgs/html/msg/Marker.html)
180180
we have:
181181

182182
import .visualization_msgs.msg: Marker
183-
Marker[:SPHERE] == getindex(Marker, :SPHERE) == 2 # true
183+
Marker.SPHERE == getproperty(Marker, :SPHERE) == 2 # true
184184

185185
## ROS Integration
186186

src/RobotOS.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ include("callbacks.jl")
1818
function __init__()
1919
#Put julia's ARGS into python's so remappings will work
2020
copy!(_py_sys, pyimport("sys"))
21-
_py_sys["argv"] = ARGS
21+
_py_sys."argv" = ARGS
2222

2323
#Fill in empty PyObjects
24-
if ! (dirname(@__FILE__) in _py_sys["path"])
25-
pushfirst!(_py_sys["path"], dirname(@__FILE__))
24+
if ! (dirname(@__FILE__) in _py_sys."path")
25+
pushfirst!(_py_sys."path", dirname(@__FILE__))
2626
end
2727
copy!(_py_ros_callbacks, pyimport("ros_callbacks"))
2828

src/callbacks.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,18 @@ function _callback_async_loop(rosobj, cond)
1818
end
1919

2020
function _run_callbacks(sub::Subscriber{M}) where M
21-
while pycall(sub.queue["size"], PyAny) > 0
22-
msg = pycall(sub.queue["get"], PyObject)
21+
while pycall(sub.queue."size", PyAny) > 0
22+
msg = pycall(sub.queue."get", PyObject)
2323
sub.callback(convert(M, msg), sub.callback_args...)
2424
end
2525
end
2626

2727
function _run_callbacks(srv::Service{T}) where T
2828
ReqType = _srv_reqtype(T)
2929

30-
req = pycall(srv.cb_interface["get_request"], PyObject)
30+
req = pycall(srv.cb_interface."get_request", PyObject)
3131
response = srv.handler(convert(ReqType, req))
3232

3333
#Python callback is blocking until the response is ready
34-
pycall(srv.cb_interface["set_response"], PyAny, convert(PyObject, response))
34+
pycall(srv.cb_interface."set_response", PyAny, convert(PyObject, response))
3535
end

src/gentypes.jl

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ function addtype!(mod::ROSMsgModule, typ::String)
183183
end
184184
pymod, pyobj = _pyvars(_fullname(mod), typ)
185185

186-
deptypes = pyobj[:_slot_types]
186+
deptypes = pyobj._slot_types
187187
_importdeps!(mod, deptypes)
188188

189189
push!(mod.members, typ)
@@ -199,15 +199,15 @@ function addtype!(mod::ROSSrvModule, typ::String)
199199
@debug("Service type import: ", _fullname(mod), ".", typ)
200200
pymod, pyobj = _pyvars(_fullname(mod), typ)
201201

202-
if ! haskey(pyobj, "_request_class")
202+
if ! PyCall.hasproperty(pyobj, "_request_class")
203203
error(string("Incorrect service name: ", typ))
204204
end
205205

206206
#Immediately import dependencies from the Request/Response classes
207207
#Repeats are OK
208-
req_obj = pymod[string(typ,"Request")]
209-
resp_obj = pymod[string(typ,"Response")]
210-
deptypes = [req_obj[:_slot_types]; resp_obj[:_slot_types]]
208+
req_obj = getproperty(pymod, string(typ,"Request"))
209+
resp_obj = getproperty(pymod, string(typ,"Response"))
210+
deptypes = [req_obj._slot_types; resp_obj._slot_types]
211211
_importdeps!(mod, deptypes)
212212

213213
push!(mod.members, typ)
@@ -222,7 +222,7 @@ end
222222
function _pyvars(modname::String, typ::String)
223223
pymod = _import_rospy_pkg(modname)
224224
pyobj =
225-
try pymod[typ]
225+
try getproperty(pymod, typ)
226226
catch ex
227227
isa(ex, KeyError) || rethrow(ex)
228228
error("Message type '$typ' not found in ROS package '$modname', ",
@@ -269,7 +269,7 @@ function _import_rospy_pkg(package::String)
269269
_rospy_modules[package] = pyimport(package)
270270
catch ex
271271
show(ex)
272-
error("python import error: $(ex.val[:args][1])")
272+
error("python import error: $(ex.val.args[1])")
273273
end
274274
end
275275
_rospy_modules[package]
@@ -324,7 +324,7 @@ function modulecode(mod::ROSModule, rosrootmod::Module)
324324
push!(modcode,
325325
quote
326326
using PyCall
327-
import Base: convert, getindex
327+
import Base: convert, getproperty
328328
import RobotOS
329329
import RobotOS.Time
330330
import RobotOS.Duration
@@ -390,8 +390,8 @@ function buildtype(mod::ROSMsgModule, typename::String)
390390
global _rospy_objects
391391
fulltypestr = _rostypestr(mod, typename)
392392
pyobj = _rospy_objects[fulltypestr]
393-
memnames = pyobj[:__slots__]
394-
memtypes = pyobj[:_slot_types]
393+
memnames = pyobj.__slots__
394+
memtypes = pyobj._slot_types
395395
members = collect(zip(memnames, memtypes))
396396

397397
typecode(fulltypestr, :AbstractMsg, members)
@@ -404,16 +404,16 @@ function buildtype(mod::ROSSrvModule, typename::String)
404404

405405
req_typestr = _rostypestr(mod, string(typename,"Request"))
406406
reqobj = _rospy_objects[req_typestr]
407-
memnames = reqobj[:__slots__]
408-
memtypes = reqobj[:_slot_types]
407+
memnames = reqobj.__slots__
408+
memtypes = reqobj._slot_types
409409
reqmems = collect(zip(memnames, memtypes))
410410
pyreq = :(RobotOS._rospy_objects[$req_typestr])
411411
reqexprs = typecode(req_typestr, :AbstractSrv, reqmems)
412412

413413
resp_typestr = _rostypestr(mod, string(typename,"Response"))
414414
respobj = _rospy_objects[resp_typestr]
415-
memnames = respobj[:__slots__]
416-
memtypes = respobj[:_slot_types]
415+
memnames = respobj.__slots__
416+
memtypes = respobj._slot_types
417417
respmems = collect(zip(memnames, memtypes))
418418
pyresp = :(RobotOS._rospy_objects[$resp_typestr])
419419
respexprs = typecode(resp_typestr, :AbstractSrv, respmems)
@@ -436,7 +436,7 @@ end
436436
# (2) Default outer constructer with no arguments
437437
# (3) convert(PyObject, ...)
438438
# (4) convert(..., o::PyObject)
439-
# (5) getindex for accessing member constants
439+
# (5) getproperty for accessing member constants
440440
function typecode(rosname::String, super::Symbol, members::Vector)
441441
tname = _splittypestr(rosname)[2]
442442
@debug("Type: ", tname)
@@ -477,17 +477,27 @@ function typecode(rosname::String, super::Symbol, members::Vector)
477477
#(4) Convert from PyObject
478478
push!(exprs, :(
479479
function convert(jlt::Type{$jlsym}, o::PyObject)
480-
if convert(String, o["_type"]) != _typerepr(jlt)
480+
if convert(String, o."_type") != _typerepr(jlt)
481481
throw(InexactError(:convert, $jlsym, o))
482482
end
483483
jl = $jlsym()
484484
#Generated code here
485485
jl
486486
end
487487
))
488-
#(5) Accessing member variables through getindex
488+
#(5) Accessing member variables through getproperty
489489
push!(exprs, :(
490-
getindex(::Type{$jlsym}, s::Symbol) = RobotOS._rospy_objects[$rosname][s]
490+
function getproperty(::Type{$jlsym}, s::Symbol)
491+
try getproperty(RobotOS._rospy_objects[$rosname], s)
492+
catch ex
493+
isa(ex, KeyError) || rethrow(ex)
494+
try getfield($jlsym, s)
495+
catch ex2
496+
startswith(ex2.msg, "type DataType has no field") || rethrow(ex2)
497+
error("Message type '" * $("$jlsym") * "' has no property '$s'.")
498+
end
499+
end
500+
end
491501
))
492502

493503
#Now add the meat to the empty expressions above
@@ -534,25 +544,25 @@ function _addtypemember!(exprs, namestr, typestr)
534544
if arraylen >= 0
535545
memexpr = :($namesym::Array{$j_typ,1})
536546
defexpr = :([$j_def for i = 1:$arraylen])
537-
jlconexpr = :(jl.$namesym = convert(Array{$j_typ,1}, o[$namestr]))
547+
jlconexpr = :(jl.$namesym = convert(Array{$j_typ,1}, o.$namestr))
538548

539549
#uint8[] is string in rospy and PyCall's conversion to bytearray is
540550
#rejected by ROS
541551
if j_typ == :UInt8
542-
pyconexpr = :(py[$namestr] =
552+
pyconexpr = :(py.$namestr =
543553
pycall(pybuiltin("str"), PyObject, PyObject(o.$namesym))
544554
)
545555
elseif _isrostype(typestr)
546-
pyconexpr = :(py[$namestr] =
556+
pyconexpr = :(py.$namestr =
547557
convert(Array{PyObject,1}, o.$namesym))
548558
else
549-
pyconexpr = :(py[$namestr] = o.$namesym)
559+
pyconexpr = :(py.$namestr = o.$namesym)
550560
end
551561
else
552562
memexpr = :($namesym::$j_typ)
553563
defexpr = j_def
554-
jlconexpr = :(jl.$namesym = convert($j_typ, o[$namestr]))
555-
pyconexpr = :(py[$namestr] = convert(PyObject, o.$namesym))
564+
jlconexpr = :(jl.$namesym = convert($j_typ, o.$namestr))
565+
pyconexpr = :(py.$namestr = convert(PyObject, o.$namesym))
556566
end
557567
push!(typeargs, memexpr)
558568
insert!(jlconargs, length(jlconargs), jlconexpr)

src/pubsub.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ struct Publisher{MsgType<:AbstractMsg}
1414
function Publisher{MT}(topic::AbstractString; kwargs...) where MT <: AbstractMsg
1515
@debug("Creating <$(string(MT))> publisher on topic: '$topic'")
1616
rospycls = _get_rospy_class(MT)
17-
return new{MT}(__rospy__[:Publisher](ascii(topic), rospycls; kwargs...))
17+
return new{MT}(__rospy__.Publisher(ascii(topic), rospycls; kwargs...))
1818
end
1919
end
2020

@@ -27,7 +27,7 @@ Publisher(topic::AbstractString, ::Type{MT}; kwargs...) where {MT <: AbstractMsg
2727
Publish `msg` on `p`, a `Publisher` with matching message type.
2828
"""
2929
function publish(p::Publisher{MT}, msg::MT) where MT <: AbstractMsg
30-
pycall(p.o["publish"], PyAny, convert(PyObject, msg))
30+
pycall(p.o."publish", PyAny, convert(PyObject, msg))
3131
end
3232

3333
"""
@@ -52,8 +52,8 @@ mutable struct Subscriber{MsgType<:AbstractMsg}
5252
rospycls = _get_rospy_class(MT)
5353

5454
cond = Base.AsyncCondition()
55-
mqueue = _py_ros_callbacks["MessageQueue"](CB_NOTIFY_PTR[], cond.handle)
56-
subobj = __rospy__[:Subscriber](ascii(topic), rospycls, mqueue["storemsg"]; kwargs...)
55+
mqueue = _py_ros_callbacks."MessageQueue"(CB_NOTIFY_PTR[], cond.handle)
56+
subobj = __rospy__.Subscriber(ascii(topic), rospycls, mqueue."storemsg"; kwargs...)
5757

5858
rosobj = new{MT}(cb, cb_args, subobj, mqueue)
5959
cbloop = Task(() -> _callback_async_loop(rosobj, cond))

src/rospy.jl

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,17 @@ Initialize this node, registering it with the ROS master. All arguments are pass
1010
the rospy init_node function.
1111
"""
1212
init_node(name::AbstractString; args...) =
13-
__rospy__[:init_node](ascii(name); args...)
13+
__rospy__.init_node(ascii(name); args...)
1414

1515
"""
1616
is_shutdown()
1717
1818
Return the shutdown status of the node.
1919
"""
20-
is_shutdown() = __rospy__[:is_shutdown]()
20+
is_shutdown() = __rospy__.is_shutdown()
2121

22-
get_published_topics() = __rospy__[:get_published_topics]()
23-
get_ros_root() = __rospy__[:get_ros_root]()
22+
get_published_topics() = __rospy__.get_published_topics()
23+
get_ros_root() = __rospy__.get_ros_root()
2424

2525
"""
2626
spin()
@@ -45,9 +45,9 @@ default is given, throws a `KeyError` if the parameter cannot be found.
4545
function get_param(param_name::AbstractString, def=nothing)
4646
try
4747
if def == nothing
48-
__rospy__[:get_param](ascii(param_name))
48+
__rospy__.get_param(ascii(param_name))
4949
else
50-
__rospy__[:get_param](ascii(param_name), def)
50+
__rospy__.get_param(ascii(param_name), def)
5151
end
5252
catch ex
5353
throw(KeyError(pycall(pybuiltin("str"), PyAny, ex.val)[2:end-1]))
@@ -60,15 +60,15 @@ end
6060
Set the value of a parameter on the parameter server.
6161
"""
6262
set_param(param_name::AbstractString, val) =
63-
__rospy__[:set_param](ascii(param_name), val)
63+
__rospy__.set_param(ascii(param_name), val)
6464

6565
"""
6666
has_param(param_name)
6767
6868
Return a boolean specifying if a parameter exists on the parameter server.
6969
"""
7070
has_param(param_name::AbstractString) =
71-
__rospy__[:has_param](ascii(param_name))
71+
__rospy__.has_param(ascii(param_name))
7272

7373
"""
7474
delete_param(param_name)
@@ -77,23 +77,23 @@ Delete a parameter from the parameter server. Throws a `KeyError` if no such par
7777
"""
7878
function delete_param(param_name::AbstractString)
7979
try
80-
__rospy__[:delete_param](ascii(param_name))
80+
__rospy__.delete_param(ascii(param_name))
8181
catch ex
8282
throw(KeyError(pycall(pybuiltin("str"), PyAny, ex.val)[2:end-1]))
8383
end
8484
end
8585

8686
#Doesn't work for some reason
8787
#rospy_search_param(param_name::AbstractString) =
88-
# __rospy__[:rospy_search_param](ascii(param_name))
89-
get_param_names() = __rospy__[:get_param_names]()
88+
# __rospy__.rospy_search_param(ascii(param_name))
89+
get_param_names() = __rospy__.get_param_names()
9090

9191
#Logging API
92-
logdebug(msg, args...) = __rospy__[:logdebug](msg, args...)
93-
loginfo(msg, args...) = __rospy__[:loginfo](msg, args...)
94-
logwarn(msg, args...) = __rospy__[:logwarn](msg, args...)
95-
logerr(msg, args...) = __rospy__[:logerr](msg, args...)
96-
logfatal(msg, args...) = __rospy__[:logfatal](msg, args...)
92+
logdebug(msg, args...) = __rospy__.logdebug(msg, args...)
93+
loginfo(msg, args...) = __rospy__.loginfo(msg, args...)
94+
logwarn(msg, args...) = __rospy__.logwarn(msg, args...)
95+
logerr(msg, args...) = __rospy__.logerr(msg, args...)
96+
logfatal(msg, args...) = __rospy__.logfatal(msg, args...)
9797

9898
"""
9999
logdebug, loginfo, logwarn, logerr, logfatal
@@ -104,7 +104,7 @@ arguments directly.
104104
logdebug, loginfo, logwarn, logerr, logfatal
105105

106106
#Node information
107-
get_name() = __rospy__[:get_name]()
108-
get_namespace() = __rospy__[:get_namespace]()
109-
get_node_uri() = __rospy__[:get_node_uri]()
110-
get_caller_id() = __rospy__[:get_caller_id]()
107+
get_name() = __rospy__.get_name()
108+
get_namespace() = __rospy__.get_namespace()
109+
get_node_uri() = __rospy__.get_node_uri()
110+
get_caller_id() = __rospy__.get_caller_id()

src/services.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ struct ServiceProxy{SrvType <: AbstractService}
1414
function ServiceProxy{ST}(name::AbstractString; kwargs...) where ST <: AbstractService
1515
@debug("Creating <$ST> service proxy for '$name'")
1616
rospycls = _get_rospy_class(ST)
17-
new{ST}(__rospy__[:ServiceProxy](ascii(name), rospycls; kwargs...))
17+
new{ST}(__rospy__.ServiceProxy(ascii(name), rospycls; kwargs...))
1818
end
1919
end
2020

@@ -51,13 +51,13 @@ mutable struct Service{SrvType <: AbstractService}
5151
rospycls = _get_rospy_class(ST)
5252

5353
cond = Base.AsyncCondition()
54-
pysrv = _py_ros_callbacks["ServiceCallback"](CB_NOTIFY_PTR[], cond.handle)
54+
pysrv = _py_ros_callbacks."ServiceCallback"(CB_NOTIFY_PTR[], cond.handle)
5555

5656
srvobj = try
57-
__rospy__[:Service](ascii(name), rospycls, pysrv["srv_cb"]; kwargs...)
57+
__rospy__.Service(ascii(name), rospycls, pysrv."srv_cb"; kwargs...)
5858
catch err
5959
if isa(err, PyCall.PyError)
60-
error("Problem during service creation: $(err.val[:args][1])")
60+
error("Problem during service creation: $(err.val.args[1])")
6161
else
6262
rethrow(err)
6363
end
@@ -85,7 +85,7 @@ Throws an exception if the waiting timeout period is exceeded.
8585
"""
8686
function wait_for_service(service::AbstractString; kwargs...)
8787
try
88-
__rospy__[:wait_for_service](ascii(service); kwargs...)
88+
__rospy__.wait_for_service(ascii(service); kwargs...)
8989
catch ex
9090
error("Timeout exceeded waiting for service '$service'")
9191
end

0 commit comments

Comments
 (0)