From c1f74b300548912f92edb4ae004dc486e0b7c8bf Mon Sep 17 00:00:00 2001 From: Tom McDermott Date: Mon, 5 Aug 2019 21:09:14 +1000 Subject: [PATCH] docs/library: Warn that ustruct doesn't handle spaces in format strings. And also add a test to capture the CPython difference. --- docs/library/ustruct.rst | 5 +++++ .../cpydiff/modules_struct_whitespace_in_format.py | 13 +++++++++++++ 2 files changed, 18 insertions(+) create mode 100644 tests/cpydiff/modules_struct_whitespace_in_format.py diff --git a/docs/library/ustruct.rst b/docs/library/ustruct.rst index 357d622b2b..bfcd84e2d8 100644 --- a/docs/library/ustruct.rst +++ b/docs/library/ustruct.rst @@ -12,6 +12,11 @@ Supported format codes: ``b``, ``B``, ``h``, ``H``, ``i``, ``I``, ``l``, ``L``, ``q``, ``Q``, ``s``, ``P``, ``f``, ``d`` (the latter 2 depending on the floating-point support). +.. admonition:: Difference to CPython + :class: attention + + Whitespace is not supported in format strings. + Functions --------- diff --git a/tests/cpydiff/modules_struct_whitespace_in_format.py b/tests/cpydiff/modules_struct_whitespace_in_format.py new file mode 100644 index 0000000000..dd0f8b48fc --- /dev/null +++ b/tests/cpydiff/modules_struct_whitespace_in_format.py @@ -0,0 +1,13 @@ +""" +categories: Modules,struct +description: Struct pack with whitespace in format, whitespace ignored by CPython, error on uPy +cause: MicroPython is optimised for code size. +workaround: Don't use spaces in format strings. +""" +import struct + +try: + print(struct.pack('b b', 1, 2)) + print('Should have worked') +except: + print('struct.error')