New issue 3026: struct format does not support space in struct.py https://bitbucket.org/pypy/pypy/issues/3026/struct-format-does-not-support-space-in
Aurélien Lambert: While trying to import pytz in brython \(which is using pypy pure python implementation of libraries\), I encountered a bug on `struct.py` \([https://bitbucket.org/pypy/pypy/src/18626459a9b2/lib\_pypy/\_struct.py](https://bitbucket.org/pypy/pypy/src/18626459a9b2/lib_pypy/_struct.py)\): spaces are not allowed in the format string. This behavior is inconsistent with python documentation \(“Whitespace characters between formats are ignored;”\) and does not happen with the compiled struct module. ```python from test.struct import calcsize # local copy of pypy struct.py calcsize('>4s c 15x 6l') # format from pytz >>> StructError: is not a valid format ``` This is due to the functions `getmode` and `getNum` not checking for whitespaces. This implementation of `getNum` gets rid of middle spaces. ```python def getNum(fmt,i): num=None cur = fmt[i] while cur == ' ': i += 1 cur = fmt[i] while ('0'<= cur ) and ( cur <= '9'): if num == None: num = int(cur) else: num = 10*num + int(cur) i += 1 cur = fmt[i] return num,i ``` Getting rid of end spaces will be little trickier because all the function use `getNum` like this: ```python while i<len(fmt): num,i = getNum(fmt,i) cur = fmt[i] [...] i += 1 ``` I suggest either to systematically use `fmt = fmt.rstrip()`, either to change the behavior of `getNum` ```python def getNum(fmt,i): num=None cur = fmt[i] while cur == ' ': i += 1 cur = fmt[i] while ('0'<= cur ) and ( cur <= '9'): if num == None: num = int(cur) else: num = 10*num + int(cur) i += 1 cur = fmt[i] i += 1 while i < len(fmt) and fmt[i] == ' ': i += 1 return num,i,cur ``` And use it like this ```python while i<len(fmt): num,i,cur = getNum(fmt,i) [...] ``` _______________________________________________ pypy-issue mailing list pypy-issue@python.org https://mail.python.org/mailman/listinfo/pypy-issue