diff --git a/src/genpy/dynamic.py b/src/genpy/dynamic.py index da314b3..5b0efff 100644 --- a/src/genpy/dynamic.py +++ b/src/genpy/dynamic.py @@ -161,7 +161,7 @@ def generate_dynamic(core_type, msg_cat): # write the entire text to a file and import it (it will get deleted when tmp_dir goes - above) tmp_file = tempfile.NamedTemporaryFile(suffix=".py",dir=tmp_dir,delete=False) - tmp_file.file.write(full_text) + tmp_file.file.write(full_text.encode()) tmp_file.file.close() # import our temporary file as a python module, which requires modifying sys.path diff --git a/src/genpy/generator.py b/src/genpy/generator.py index cd36811..9d8a421 100644 --- a/src/genpy/generator.py +++ b/src/genpy/generator.py @@ -429,7 +429,10 @@ def string_serializer_generator(package, type_, name, serialize): yield INDENT+"%s = %s.encode('utf-8')"%(var,var) #For unicode-strings in Python2, encode using utf-8 yield INDENT+"length = len(%s)"%(var) # Update the length after utf-8 conversion - yield pack2("' 0x03000000: if type(field_val) == str: - raise SerializationError('field %s is a unicode string instead of an ascii string'%field_name) + try: + field_val.encode('ascii') + except UnicodeEncodeError: + raise SerializationError('field %s is a non-ascii string'%field_name) + elif not type(field_val) == bytes: + raise SerializationError('field %s must be of type bytes or an ascii string'%field_name) else: if type(field_val) == unicode: raise SerializationError('field %s is a unicode string instead of an ascii string'%field_name) diff --git a/src/genpy/rostime.py b/src/genpy/rostime.py index ae7ad5e..1c8326e 100644 --- a/src/genpy/rostime.py +++ b/src/genpy/rostime.py @@ -120,9 +120,15 @@ def __str__(self): def __repr__(self): return "genpy.TVal[%d]"%self.to_nsec() + def __bool__(self): + """ + Return if time value is not zero + """ + return self.secs != 0 or self.nsecs != 0 + def __nonzero__(self): """ - Check if time value is zero + Check if time value is not zero """ return self.secs or self.nsecs @@ -278,6 +284,9 @@ def __eq__(self, other): return False return self.secs == other.secs and self.nsecs == other.nsecs + def __hash__(self): + return super(Time, self).__hash__() + class Duration(TVal): """ Duration represents the ROS 'duration' primitive, which consists @@ -429,3 +438,6 @@ def __eq__(self, other): if not isinstance(other, Duration): return False return self.secs == other.secs and self.nsecs == other.nsecs + + def __hash__(self): + return super(Duration, self).__hash__() diff --git a/test/files/array/string_fixed_ser.txt b/test/files/array/string_fixed_ser.txt index f539625..bc8ae8c 100644 --- a/test/files/array/string_fixed_ser.txt +++ b/test/files/array/string_fixed_ser.txt @@ -3,4 +3,7 @@ for val0 in data: if python3 or type(val0) == unicode: val0 = val0.encode('utf-8') length = len(val0) - buff.write(struct.pack(' 0x03000000: # Python3 + self.assertEquals("{1}", strify_message(set([1]))) + else: + self.assertEquals("set([1])", strify_message(set([1]))) def test_strify_yaml(self): def roundtrip(m): yaml_text = strify_message(m) - print yaml_text + print(yaml_text) loaded = yaml.load(yaml_text) - print "loaded", loaded + print("loaded", loaded) new_inst = m.__class__() if loaded is not None: fill_message_args(new_inst, [loaded]) @@ -629,7 +640,7 @@ def test_check_type(self): for t, v in valids: try: check_type('n', t, v) - except Exception, e: + except Exception as e: traceback.print_exc() raise Exception("failure type[%s] value[%s]: %s"%(t, v, str(e)))