Skip to content

Commit

Permalink
more Python 3 compatibility (#1795)
Browse files Browse the repository at this point in the history
* avoid using nose.tools without dependency being declared

* seek(0)

* subprocess decode

* import urlparse

* fix hash arg encode

* print function

* replace tabs used for indenting Python code with spaces
  • Loading branch information
dirk-thomas authored Aug 16, 2019
1 parent 4eebe55 commit 7671747
Show file tree
Hide file tree
Showing 30 changed files with 163 additions and 156 deletions.
2 changes: 1 addition & 1 deletion clients/rospy/src/rospy/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ def __call__(self, caller_id, msg):
- caller_id (str): Id to identify the caller
- msg (str): Contents of message to log
"""
msg_hash = md5(msg).hexdigest()
msg_hash = md5(msg.encode()).hexdigest()

if msg_hash != self.last_logging_msg_table.get(caller_id):
self.last_logging_msg_table[caller_id] = msg_hash
Expand Down
5 changes: 3 additions & 2 deletions clients/rospy/src/rospy/impl/tcpros_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ def __init__(self, protocol, name, header=None):
else: # Python 3.x
self.read_buff = BytesIO()
self.write_buff = BytesIO()

#self.write_buff = StringIO()
self.header = header

Expand Down Expand Up @@ -651,7 +651,7 @@ def read_header(self):
if sock is None:
return
sock.setblocking(1)
# TODO: add bytes received to self.stat_bytes
# TODO: add bytes received to self.stat_bytes
self._validate_header(read_ros_handshake_header(sock, self.read_buff, self.protocol.buff_size))

def send_message(self, msg, seq):
Expand All @@ -670,6 +670,7 @@ def send_message(self, msg, seq):
serialize_message(self.write_buff, seq, msg)
self.write_data(self.write_buff.getvalue())
self.write_buff.truncate(0)
self.write_buff.seek(0)

def write_data(self, data):
"""
Expand Down
2 changes: 1 addition & 1 deletion clients/rospy/src/rospy/impl/tcpros_pubsub.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ def robust_connect_subscriber(conn, dest_addr, dest_port, pub_uri, receive_cb, r

# check to see if publisher state has changed
conn.done = not check_if_still_publisher(resolved_topic_name, pub_uri)

if not conn.done:
conn.receive_loop(receive_cb)

Expand Down
1 change: 1 addition & 0 deletions clients/rospy/src/rospy/impl/udpros.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ def send_message(self, msg, seq):
serialize_message(self.write_buff, seq, msg)
self.write_data(self.write_buff.getvalue())
self.write_buff.truncate(0)
self.write_buff.seek(0)

def write_data(self, data):
"""
Expand Down
2 changes: 1 addition & 1 deletion test/test_roslaunch/test/params_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def test_commandandfile(self):

# test 'command' attribute
if os.name != 'nt': # skip testcase for `cat` command in Windows
self.assertEquals(get_param("commandoutput"), binary_data)
self.assertEquals(get_param("commandoutput"), binary_data.decode())
# test 'textfile' attribute
self.assertEquals(get_param("textfile"), text_data)
## test 'binfile' attribute
Expand Down
12 changes: 9 additions & 3 deletions test/test_rosmaster/test/client_verification/test_slave_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def setUp(self):
time.sleep(0.1)
if not self.node_api:
self.fail("master did not return XML-RPC API for [%s, %s]"%(self.caller_id, self.test_node))
print "[%s] API = %s"%(self.test_node, self.node_api)
print("[%s] API = %s"%(self.test_node, self.node_api))
self.assert_(self.node_api.startswith('http'))
self.node = ServerProxy(self.node_api)

Expand Down Expand Up @@ -173,7 +173,10 @@ def check_uri(self, uri):
"""
validates a URI as being http(s)
"""
import urlparse
try:
import urllib.parse as urlparse
except ImportError:
import urlparse
parsed = urlparse.urlparse(uri)
self.assert_(parsed[0] in ['http', 'https'], 'protocol [%s] is [%s] invalid'%(parsed[0], uri))
self.assert_(parsed[1], 'host missing [%s]'%uri)
Expand Down Expand Up @@ -323,7 +326,10 @@ def test_getMasterUri(self):
self.check_uri(uri)

# check against env, canonicalize for comparison
import urlparse
try:
import urllib.parse as urlparse
except ImportError:
import urlparse
master_env = rosgraph.get_master_uri()
if not master_env.endswith('/'):
master_env = master_env + '/'
Expand Down
10 changes: 8 additions & 2 deletions test/test_rosmaster/test/master.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,10 @@ def setUp(self):

## validates a URI as being http(s)
def _checkUri(self, uri):
import urlparse
try:
import urllib.parse as urlparse
except ImportError:
import urlparse
parsed = urlparse.urlparse(uri)
self.assert_(parsed[0] in ['http', 'https'], 'protocol [%s] in [%s] invalid'%(parsed[0], uri))
self.assert_(parsed[1], 'host missing [%s]'%uri)
Expand All @@ -98,7 +101,10 @@ def _testGetMasterUri(self):
self._checkUri(uri)

# make sure we agree on ports
import urlparse
try:
import urllib.parse as urlparse
except ImportError:
import urlparse
parsed = urlparse.urlparse(uri)
parsed2 = urlparse.urlparse(self.master_uri)

Expand Down
10 changes: 5 additions & 5 deletions test/test_rosmaster/test/nodes/add_two_ints_client
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def add_two_ints_client(x, y):
# create a handle to the add_two_ints service
add_two_ints = rospy.ServiceProxy('add_two_ints', AddTwoInts)

print "Requesting %s+%s"%(x, y)
print("Requesting %s+%s"%(x, y))

# simplified style
resp1 = add_two_ints(x, y)
Expand All @@ -76,7 +76,7 @@ def add_two_ints_client(x, y):
raise Exception("test failure, returned sum was %s"%resp2.sum)
return resp1.sum
except rospy.ServiceException as e:
print "Service call failed: %s"%e
print("Service call failed: %s"%e)

def usage():
return "%s [x y]"%sys.argv[0]
Expand All @@ -93,9 +93,9 @@ if __name__ == "__main__":
x = string.atoi(argv[1])
y = string.atoi(argv[2])
except:
print usage()
print(usage())
sys.exit(1)
else:
print usage()
print(usage())
sys.exit(1)
print "%s + %s = %s"%(x, y, add_two_ints_client(x, y))
print("%s + %s = %s"%(x, y, add_two_ints_client(x, y)))
6 changes: 3 additions & 3 deletions test/test_rosmaster/test/nodes/testAllCommonFlows
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ BOOTSTRAP_VERSION = "0.1"

# Read in ROS_ROOT
if 'ROS_ROOT' not in os.environ:
print """\nCannot run ROS: ROS_ROOT is not set.\nPlease set the ROS_ROOT environment variable to the
location of your ROS install.\n"""
print("""\nCannot run ROS: ROS_ROOT is not set.\nPlease set the ROS_ROOT environment variable to the
location of your ROS install.\n""")
sys.exit(-1)
rosRoot = os.environ['ROS_ROOT']

Expand All @@ -23,7 +23,7 @@ rospackLatest = os.popen(os.path.join(rosRoot,'rospack')+' latest rospy', 'r')
rospyDir = rospackLatest.read()
rospackLatest.close()
if rospyDir is None or not os.path.isdir(rospyDir.strip()):
print "\nERROR: Cannot locate rospy installation.\n"
print("\nERROR: Cannot locate rospy installation.\n")
sys.exit(-1)

# Run launcher bootstrapper
Expand Down
6 changes: 3 additions & 3 deletions test/test_rosmaster/test/nodes/testMaster
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ BOOTSTRAP_VERSION = "0.1"

# Read in ROS_ROOT
if 'ROS_ROOT' not in os.environ:
print """\nCannot run ROS: ROS_ROOT is not set.\nPlease set the ROS_ROOT environment variable to the
location of your ROS install.\n"""
print("""\nCannot run ROS: ROS_ROOT is not set.\nPlease set the ROS_ROOT environment variable to the
location of your ROS install.\n""")
sys.exit(-1)
rosRoot = os.environ['ROS_ROOT']

Expand All @@ -23,7 +23,7 @@ rospackLatest = os.popen(os.path.join(rosRoot,'rospack')+' latest rospy', 'r')
rospyDir = rospackLatest.read()
rospackLatest.close()
if rospyDir is None or not os.path.isdir(rospyDir.strip()):
print "\nERROR: Cannot locate rospy installation.\n"
print("\nERROR: Cannot locate rospy installation.\n")
sys.exit(-1)

# Run launcher bootstrapper
Expand Down
6 changes: 3 additions & 3 deletions test/test_rosmaster/test/nodes/testSlave
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ BOOTSTRAP_VERSION = "0.1"

# Read in ROS_ROOT
if 'ROS_ROOT' not in os.environ:
print """\nCannot run ROS: ROS_ROOT is not set.\nPlease set the ROS_ROOT environment variable to the
location of your ROS install.\n"""
print("""\nCannot run ROS: ROS_ROOT is not set.\nPlease set the ROS_ROOT environment variable to the
location of your ROS install.\n""")
sys.exit(-1)
rosRoot = os.environ['ROS_ROOT']

Expand All @@ -23,7 +23,7 @@ rospackLatest = os.popen(os.path.join(rosRoot,'rospack')+' latest rospy', 'r')
rospyDir = rospackLatest.read()
rospackLatest.close()
if rospyDir is None or not os.path.isdir(rospyDir.strip()):
print "\nERROR: Cannot locate rospy installation.\n"
print("\nERROR: Cannot locate rospy installation.\n")
sys.exit(-1)

# Run launcher bootstrapper
Expand Down
14 changes: 7 additions & 7 deletions test/test_rosmaster/test/testMaster.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def testGraphState(master, graphNodes, graphFlows):
assert not diff, "Graph nodes %s does not match expected %s: %s"%(graph[0], graphNodes, diff)
# stringify for comparison
expectedFlows = ["%s:%s:1"%f for f in graphFlows] # :1 = connected
print graph[1]
print(graph[1])
remoteFlows = ["%s:%s:%s"%(src,snk,c) for (src,snk,c) in graph[1]]
if expectedFlows or remoteFlows:
#assert set(expectedFlows) ^ set(remoteFlows), "Graph flows [%s] does not match expected [%s]"%(graph[1], graphFlows)
Expand All @@ -95,7 +95,7 @@ def testParamState(master, myState):
for (k, v) in myState.items():
if HAS_PARAM:
assert apiSuccess(master.hasParam(callerId, k))
print "verifying parameter %s"%k
print("verifying parameter %s"%k)
v2 = apiSuccess(master.getParam(callerId, k))
if isinstance(v2, DateTime):
assert DateTime(v) == v2, "[%s]: %s != %s, %s"%(k, v, v2, v2.__class__)
Expand Down Expand Up @@ -124,7 +124,7 @@ def _testSetParam(self, ctx, myState, testVals, master):
count = 0
for val in vals:
key = "%s-%s"%(type,count)
print "master.setParam(%s,%s,%s)"%(callerId, key, val)
print("master.setParam(%s,%s,%s)"%(callerId, key, val))
master.setParam(callerId, key, val)
if HAS_PARAM:
assert apiSuccess(master.hasParam(callerId, key))
Expand Down Expand Up @@ -161,14 +161,14 @@ def testParamValues(self):
]
master = self.master

print "Putting parameters onto the server"
print("Putting parameters onto the server")
# put our params into the parameter server
contexts = ['', 'scope1', 'scope2', 'scope.subscope1', 'scope.sub1.sub2']
myState = {}
for ctx in contexts:
self._testSetParam(ctx, myState, testVals, master)

print "Deleting all of our parameters"
print("Deleting all of our parameters")
# delete all of our parameters
paramKeys = myState.keys()
for key in paramKeys:
Expand Down Expand Up @@ -493,7 +493,7 @@ def testGetFlowNames(self):
#testNode must have :in and :out
#:in and :out
testFlowNames = ["%s:%s"%(name, v) for v in ["in", "out"]]
print "FLOW NAMES", apiSuccess(master.getFlowNames('master', ''))
print("FLOW NAMES", apiSuccess(master.getFlowNames('master', '')))
flows = apiSuccess(master.getFlowNames('%s.caller'%subgraph, ''))
assert not set([x[0] for x in flows]) ^ set(testFlowNames), "%s vs. %s"%([x[0] for x in flows], testFlowNames)

Expand Down Expand Up @@ -656,7 +656,7 @@ def testAddNode(self):
[['wg.addNodeTest', 'testAddNodeA'], [TEST_MACHINE, 0]],
]
for fullname, args in tests:
print "testAddNode: testing", fullname
print("testAddNode: testing", fullname)
subcontext, name = fullname
if subcontext:
fullname = '%s.%s'%(subcontext, name)
Expand Down
4 changes: 2 additions & 2 deletions test/test_rosmaster/test/testSlave.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ def _sink_StartFlows(self, tests, sourceUris, sinks):
sourceUri = sourceUris[sourceName]
sink = sinks[sinkName]
callerId, sourceLocator, sinkLocator = test[1]
print "Testing", test
print("Testing", test)
val = apiSuccess(sink.sinkConnectFlow(callerId, sourceLocator, sinkLocator, sourceUri, reliable))
assert type(val) == int

Expand All @@ -290,7 +290,7 @@ def _sink_KillFlows(self, tests, sinks):
sourceName, sinkName = test[0]
sink = sinks[sinkName]
callerId, sourceLocator, sinkLocator = test[1]
print "Testing", test
print("Testing", test)
assert 1 == apiSuccess(sink.sinkKillFlow(callerId, sourceLocator, sinkLocator)),\
"sink did not report 1 flow killed: %s, %s"%(getLastVal(), getLastMsg())

Expand Down
40 changes: 20 additions & 20 deletions test/test_rosparam/test/check_rosparam_command_line_online.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,56 +69,56 @@ def test_rosparam(self):
'/g2/string', '/g2/int', '/g2/float',
]
# - we aren't matching against the core services as those can make the test suites brittle
output = Popen([cmd, 'list'], stdout=PIPE).communicate()[0]
output = Popen([cmd, 'list'], stdout=PIPE).communicate()[0].decode()
l = set(output.split())
for t in params:
self.assert_(t in l)

# get
# - strings
output = Popen([cmd, 'get', "string"], stdout=PIPE).communicate()[0]
output = Popen([cmd, 'get', "string"], stdout=PIPE).communicate()[0].decode()
self.assertEquals('foo-value', output.strip())
# -- pretty
output = Popen([cmd, 'get', '-p', "string"], stdout=PIPE).communicate()[0]
output = Popen([cmd, 'get', '-p', "string"], stdout=PIPE).communicate()[0].decode()
self.assertEquals('foo-value', output.strip())
output = Popen([cmd, 'get', "/string"], stdout=PIPE).communicate()[0]
output = Popen([cmd, 'get', "/string"], stdout=PIPE).communicate()[0].decode()
self.assertEquals('foo-value', output.strip())
output = Popen([cmd, 'get', "g1/string"], stdout=PIPE).communicate()[0]
output = Popen([cmd, 'get', "g1/string"], stdout=PIPE).communicate()[0].decode()
self.assertEquals('g1-foo-value', output.strip())
output = Popen([cmd, 'get', "/g1/string"], stdout=PIPE).communicate()[0]
output = Popen([cmd, 'get', "/g1/string"], stdout=PIPE).communicate()[0].decode()
self.assertEquals('g1-foo-value', output.strip())
output = Popen([cmd, 'get', "/g2/string"], stdout=PIPE).communicate()[0]
output = Popen([cmd, 'get', "/g2/string"], stdout=PIPE).communicate()[0].decode()
self.assertEquals('g2-foo-value', output.strip())
# - ints
output = Popen([cmd, 'get', "int"], stdout=PIPE).communicate()[0]
output = Popen([cmd, 'get', "int"], stdout=PIPE).communicate()[0].decode()
self.assertEquals('1', output.strip())
# -- pretty
output = Popen([cmd, 'get', '-p', "int"], stdout=PIPE).communicate()[0]
output = Popen([cmd, 'get', '-p', "int"], stdout=PIPE).communicate()[0].decode()
self.assertEquals('1', output.strip())
output = Popen([cmd, 'get', "/int"], stdout=PIPE).communicate()[0]
output = Popen([cmd, 'get', "/int"], stdout=PIPE).communicate()[0].decode()
self.assertEquals('1', output.strip())
output = Popen([cmd, 'get', "g1/int"], stdout=PIPE).communicate()[0]
output = Popen([cmd, 'get', "g1/int"], stdout=PIPE).communicate()[0].decode()
self.assertEquals('10', output.strip())
output = Popen([cmd, 'get', "/g1/int"], stdout=PIPE).communicate()[0]
output = Popen([cmd, 'get', "/g1/int"], stdout=PIPE).communicate()[0].decode()
self.assertEquals('10', output.strip())
output = Popen([cmd, 'get', "/g2/int"], stdout=PIPE).communicate()[0]
output = Popen([cmd, 'get', "/g2/int"], stdout=PIPE).communicate()[0].decode()
self.assertEquals('20', output.strip())
# - floats
output = Popen([cmd, 'get', "float"], stdout=PIPE).communicate()[0]
output = Popen([cmd, 'get', "float"], stdout=PIPE).communicate()[0].decode()
self.assertEquals('1.0', output.strip())
# -- pretty
output = Popen([cmd, 'get', '-p', "float"], stdout=PIPE).communicate()[0]
output = Popen([cmd, 'get', '-p', "float"], stdout=PIPE).communicate()[0].decode()
self.assertEquals('1.0', output.strip())
output = Popen([cmd, 'get', "/float"], stdout=PIPE).communicate()[0]
output = Popen([cmd, 'get', "/float"], stdout=PIPE).communicate()[0].decode()
self.assertEquals('1.0', output.strip())
output = Popen([cmd, 'get', "g1/float"], stdout=PIPE).communicate()[0]
output = Popen([cmd, 'get', "g1/float"], stdout=PIPE).communicate()[0].decode()
self.assertEquals('10.0', output.strip())
output = Popen([cmd, 'get', "/g1/float"], stdout=PIPE).communicate()[0]
output = Popen([cmd, 'get', "/g1/float"], stdout=PIPE).communicate()[0].decode()
self.assertEquals('10.0', output.strip())
output = Popen([cmd, 'get', "/g2/float"], stdout=PIPE).communicate()[0]
output = Popen([cmd, 'get', "/g2/float"], stdout=PIPE).communicate()[0].decode()
self.assertEquals('20.0', output.strip())
# - dictionary
output = Popen([cmd, 'get', "g1"], stdout=PIPE).communicate()[0]
output = Popen([cmd, 'get', "g1"], stdout=PIPE).communicate()[0].decode()
import yaml
d = yaml.safe_load(output)
self.assertEquals(d['float'], 10.0)
Expand Down
2 changes: 1 addition & 1 deletion test/test_rospy/nodes/talker
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def talker():
count = 0
while not rospy.is_shutdown():
str = "hello world %d"%count
print str
print(str)
pub.publish(String(str))
count += 1
rospy.sleep(0.01)
Expand Down
2 changes: 1 addition & 1 deletion tools/roslaunch/scripts/roslaunch-complete
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,6 @@ except roslaunch.core.RLException:
# Currently don't put := after that as the completion will escape := to \:\=
# Unfortunately we also want the escaping for roslaunch, when completing filenames
complete_args = [a for a in sorted(roslaunch_args.keys())]
print " ".join(complete_args)
print(" ".join(complete_args))
sys.exit(0)

2 changes: 1 addition & 1 deletion tools/roslaunch/src/roslaunch/remoteprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ def _ssh_exec(self, command, address, port, username=None, password=None):
except ImportError as e:
_logger.error("cannot use SSH: paramiko is not installed")
return None, "paramiko is not installed"
#load user's ssh configuration
#load user's ssh configuration
config_block = {'hostname': None, 'user': None, 'identityfile': None}
ssh_config = paramiko.SSHConfig()
try:
Expand Down
Loading

0 comments on commit 7671747

Please sign in to comment.