From 149920e363099952e52b7b7e8298ebb21f96aace Mon Sep 17 00:00:00 2001 From: Stephan Hadinger Date: Sun, 17 Oct 2021 16:05:13 +0200 Subject: [PATCH] Berry fix string literal --- lib/libesp32/Berry/src/be_byteslib.c | 13 ++++++++----- lib/libesp32/Berry/tests/bytes.be | 4 ++++ 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/lib/libesp32/Berry/src/be_byteslib.c b/lib/libesp32/Berry/src/be_byteslib.c index ecc665c31..78c20268e 100644 --- a/lib/libesp32/Berry/src/be_byteslib.c +++ b/lib/libesp32/Berry/src/be_byteslib.c @@ -579,11 +579,14 @@ static int m_init(bvm *vm) if (hex_in) { size_arg = strlen(hex_in) / 2; /* allocate headroom */ } - } - - /* check if fixed size that we have the right size */ - if (attr.fixed && size_arg > attr.size) { - be_raise(vm, BYTES_RESIZE_ERROR, BYTES_RESIZE_MESSAGE); + /* check if fixed size that we have the right size */ + if (size_arg > attr.size) { + if (attr.fixed) { + be_raise(vm, BYTES_RESIZE_ERROR, BYTES_RESIZE_MESSAGE); + } else { + attr.size = size_arg; + } + } } /* allocate */ diff --git a/lib/libesp32/Berry/tests/bytes.be b/lib/libesp32/Berry/tests/bytes.be index edb6c012e..9fe75ba4d 100644 --- a/lib/libesp32/Berry/tests/bytes.be +++ b/lib/libesp32/Berry/tests/bytes.be @@ -7,6 +7,8 @@ b=bytes(0) assert(str(b) == "bytes('')") b=bytes(1) assert(str(b) == "bytes('')") +b=bytes(-1) +assert(str(b) == "bytes('')") assert(b.size() == 0) b=bytes("a") @@ -164,3 +166,5 @@ b=bytes() b.fromstring("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.") assert(str(b) =="bytes('4C6F72656D20697073756D20646F6C6F722073697420616D65742C20636F6E73...')") assert(b.tostring(0) =="bytes('4C6F72656D20697073756D20646F6C6F722073697420616D65742C20636F6E73656374657475722061646970697363696E6720656C69742C2073656420646F20656975736D6F642074656D706F7220696E6369646964756E74207574206C61626F726520657420646F6C6F7265206D61676E6120616C697175612E')") + +assert(size(bytes('4C6F72656D20697073756D20646F6C6F722073697420616D65742C20636F6E73656374657475722061646970697363696E6720656C69742C2073656420646F20656975736D6F642074656D706F7220696E6369646964756E74207574206C61626F726520657420646F6C6F7265206D61676E6120616C697175612E')) == 123)