Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use numpy.array's "offset" param instead of slicing #33

Open
wants to merge 1 commit into
base: indigo-devel
Choose a base branch
from
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
4 changes: 2 additions & 2 deletions src/genpy/generate_numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@
'byte' : 'numpy.int8',
}
# TODO: this doesn't explicitly specify little-endian byte order on the numpy data instance
def unpack_numpy(var, count, dtype, buff):
def unpack_numpy(var, offset, count, dtype, buff):
"""
create numpy deserialization code
"""
return var + " = numpy.frombuffer(%s, dtype=%s, count=%s)"%(buff, dtype, count)
return var + " = numpy.frombuffer(%s, dtype=%s, count=%s, offset=%s)"%(buff, dtype, count, offset)

def pack_numpy(var):
"""
Expand Down
4 changes: 2 additions & 2 deletions src/genpy/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ def array_serializer_generator(msg_context, package, type_, name, serialize, is_
yield "end += struct.calcsize(pattern)"
if is_numpy:
dtype = NUMPY_DTYPE[base_type]
yield unpack_numpy(var, 'length', dtype, 'str[start:end]')
yield unpack_numpy(var, 'start', 'length', dtype, 'str')
else:
yield unpack2(var, 'pattern', 'str[start:end]')
else:
Expand All @@ -506,7 +506,7 @@ def array_serializer_generator(msg_context, package, type_, name, serialize, is_
yield "end += %s"%struct.calcsize('<%s'%pattern)
if is_numpy:
dtype = NUMPY_DTYPE[base_type]
yield unpack_numpy(var, length, dtype, 'str[start:end]')
yield unpack_numpy(var, 'start', length, dtype, 'str')
else:
yield unpack(var, pattern, 'str[start:end]')
if not serialize and base_type == 'bool':
Expand Down
2 changes: 1 addition & 1 deletion test/files/array/int16_fixed_deser_np.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
start = end
end += 20
data = numpy.frombuffer(str[start:end], dtype=numpy.int16, count=10)
data = numpy.frombuffer(str, dtype=numpy.int16, count=10, offset=start)
2 changes: 1 addition & 1 deletion test/files/array/int16_varlen_deser_np.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ end += 4
pattern = '<%sh'%length
start = end
end += struct.calcsize(pattern)
data = numpy.frombuffer(str[start:end], dtype=numpy.int16, count=length)
data = numpy.frombuffer(str, dtype=numpy.int16, count=length, offset=start)