Skip to content
Closed
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
10 changes: 7 additions & 3 deletions extmod/modubinascii.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <string.h>

#include "py/runtime.h"
#include "py/objstr.h"
#include "py/binary.h"
#include "extmod/modubinascii.h"

Expand All @@ -43,7 +44,7 @@ static void check_not_unicode(const mp_obj_t arg) {
mp_obj_t mod_binascii_hexlify(size_t n_args, const mp_obj_t *args) {
// Second argument is for an extension to allow a separator to be used
// between values.
const char *sep = NULL;
const byte *sep = NULL;
mp_buffer_info_t bufinfo;
check_not_unicode(args[0]);
mp_get_buffer_raise(args[0], &bufinfo, MP_BUFFER_READ);
Expand All @@ -58,8 +59,11 @@ mp_obj_t mod_binascii_hexlify(size_t n_args, const mp_obj_t *args) {
size_t out_len = bufinfo.len * 2;
if (n_args > 1) {
// 1-char separator between hex numbers
out_len += bufinfo.len - 1;
sep = mp_obj_str_get_str(args[1]);
GET_STR_DATA_LEN(args[1], sep_data, sep_len);
if (sep_len > 0) {
sep = sep_data;
out_len += bufinfo.len - 1;
}
}
vstr_init_len(&vstr, out_len);
byte *in = bufinfo.buf, *out = (byte*)vstr.buf;
Expand Down
10 changes: 10 additions & 0 deletions tests/extmod/ubinascii_micropython.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,15 @@
a = binascii.hexlify(b'123', ':')
print(a)

# Ensure that the second argument changes the output but still contains.
# the hex values.
assert binascii.hexlify(b'spam', b'-') != binascii.hexlify(b'spam')
assert binascii.hexlify(b'spam', b'-')[:2] == binascii.hexlify(b's')
assert binascii.hexlify(b'spam', b'-')[-2:] == binascii.hexlify(b'm')

# zero length buffer
print(binascii.hexlify(b'', b':'))

# A zero length sep should act like no sep.
# https://github.com/micropython/micropython/issues/2287
assert binascii.hexlify(b'spam', b'') == binascii.hexlify(b'spam')