-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Better support for serializing buffer protocol objects. Added tests.
Signed-off-by: Martin Pecka <[email protected]>
- Loading branch information
Showing
10 changed files
with
254 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,18 @@ | ||
# - if encoded as a list instead, serialize as bytes instead of string | ||
if type(data) in [list, tuple]: | ||
buff.write(_get_struct_8B().pack(*data)) | ||
# check for buffer protocol support | ||
try: | ||
# we want to process str and bytes by the except clause | ||
if isinstance(data, bytes) or isinstance(data, str): | ||
raise TypeError() | ||
tmp = memoryview(data) | ||
except TypeError: | ||
# - if encoded as a list instead, serialize as bytes instead of string | ||
if type(data) in [list, tuple]: | ||
buff.write(_get_struct_8B().pack(*data)) | ||
else: | ||
buff.write(_get_struct_8s().pack(data)) | ||
else: | ||
buff.write(_get_struct_8s().pack(data)) | ||
from genpy.generate_struct import memoryview_len | ||
tmp_len = memoryview_len(tmp) | ||
if tmp_len != 8: | ||
raise TypeError("expected 8 bytes, got %i" % (tmp_len,)) | ||
buff.write(tmp[:8]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,18 @@ | ||
# - if encoded as a list instead, serialize as bytes instead of string | ||
if type(data) in [list, tuple]: | ||
buff.write(_get_struct_8B().pack(*data)) | ||
# check for buffer protocol support | ||
try: | ||
# we want to process str and bytes by the except clause | ||
if isinstance(data, bytes) or isinstance(data, str): | ||
raise TypeError() | ||
tmp = memoryview(data) | ||
except TypeError: | ||
# - if encoded as a list instead, serialize as bytes instead of string | ||
if type(data) in [list, tuple]: | ||
buff.write(_get_struct_8B().pack(*data)) | ||
else: | ||
buff.write(_get_struct_8s().pack(data)) | ||
else: | ||
buff.write(_get_struct_8s().pack(data)) | ||
from genpy.generate_struct import memoryview_len | ||
tmp_len = memoryview_len(tmp) | ||
if tmp_len != 8: | ||
raise TypeError("expected 8 bytes, got %i" % (tmp_len,)) | ||
buff.write(tmp[:8]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,13 @@ | ||
length = len(data) | ||
# - if encoded as a list instead, serialize as bytes instead of string | ||
if type(data) in [list, tuple]: | ||
buff.write(struct.Struct('<I%sB'%length).pack(length, *data)) | ||
else: | ||
buff.write(struct.Struct('<I%ss'%length).pack(length, data)) | ||
# check for buffer protocol support | ||
try: | ||
tmp = memoryview(data) | ||
from genpy.generate_struct import memoryview_len | ||
buff.write(struct.Struct('<I').pack(memoryview_len(tmp))) | ||
buff.write(tmp) | ||
except TypeError: | ||
# - if encoded as a list instead, serialize as bytes instead of string | ||
if type(data) in [list, tuple]: | ||
buff.write(struct.Struct('<I%sB'%length).pack(length, *data)) | ||
else: | ||
buff.write(struct.Struct('<I%ss'%length).pack(length, data)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,13 @@ | ||
length = len(data) | ||
# - if encoded as a list instead, serialize as bytes instead of string | ||
if type(data) in [list, tuple]: | ||
buff.write(struct.Struct('<I%sB'%length).pack(length, *data)) | ||
else: | ||
buff.write(struct.Struct('<I%ss'%length).pack(length, data)) | ||
# check for buffer protocol support | ||
try: | ||
tmp = memoryview(data) | ||
from genpy.generate_struct import memoryview_len | ||
buff.write(struct.Struct('<I').pack(memoryview_len(tmp))) | ||
buff.write(tmp) | ||
except TypeError: | ||
# - if encoded as a list instead, serialize as bytes instead of string | ||
if type(data) in [list, tuple]: | ||
buff.write(struct.Struct('<I%sB'%length).pack(length, *data)) | ||
else: | ||
buff.write(struct.Struct('<I%ss'%length).pack(length, data)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
uint8[] data_var | ||
uint8[4] data_fixed |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters