This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: struct.pack error messages do not indicate which argument was invalid
Type: enhancement Stage: patch review
Components: Library (Lib) Versions: Python 3.11
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: serhiy.storchaka Nosy List: alynn, louielu, mark.dickinson, meador.inge, serhiy.storchaka
Priority: low Keywords: easy (C)

Created on 2015-03-03 17:03 by alynn, last changed 2022-04-11 14:58 by admin.

Pull Requests
URL Status Linked Edit
PR 291 closed louielu, 2017-02-25 14:10
Messages (4)
msg237153 - (view) Author: Alistair Lynn (alynn) Date: 2015-03-03 17:03
In this example:

  struct.pack('!hhhh', 0x5FFF, 0x6FFF, 0x7FFF, 0x8FFF)

Python errors that the 'h' format requires -32768 <= number <= 32767, but it does not indicate which of the arguments is at fault. In this contrived example it's clearly the fourth one, but in dealing with large amounts of data it would be useful to indicate which argument was invalid.

This would hopefully not be a functional change, just a slightly more helpful error message.
msg288565 - (view) Author: Louie Lu (louielu) * Date: 2017-02-25 14:11
Adding PyErr_SetString and PyErr_Format wrapper, with a global offset
variable to handle this.

struct.pack('!h', 0x8FFFF)
Traceback (most recent call last):
  File "tests.py", line 5, in <module>
    struct.pack('!h', 0x8FFFF)
struct.error: Raise at offset 1, 'h' format requires -32768 <= number <= 32767
msg288569 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-02-25 15:10
1. Using global variable doesn't look good to me.

2. "at offset 1" looks confusing to me. What is offset? Is this an offset of packed item in created bytes object? The you shouldn't get the odd number for the '!h' format.

I think it would be better to report the number of the packed item. struct.pack() already formats similar errors when pass unsuitable number of items.

>>> struct.pack('<hb', 1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
struct.error: pack expected 2 items for packing (got 1)
>>> struct.pack('<hb', 1, 2, 3)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
struct.error: pack expected 2 items for packing (got 3)

3. It is not safe to use the fixed length array for formatting error message. Once the underlying error message can be changed and will overflow the buffer. The "%zd" format in sprintf() is not portable.
msg288570 - (view) Author: Louie Lu (louielu) * Date: 2017-02-25 15:49
> 1. Using global variable doesn't look good to me.

That's true, but I'm not sure if there have other methods to do this.

If not using global variable, we will need to change a bunch of the
function arguments. Since the arguments didn't contain the
information about which item is in the process and raise the error.


> 2. "at offset 1" looks confusing to me. What is offset?

Make the change to: 
>>> struct.pack('hh', , 0x7FFFF, 0x8FFFF)
struct.error: 'h' format requires -32768 <= number <= 32767, got bad value at item 2

(or probably, "got bad value at index 2")

> 3. It is not safe to use the fixed length array for formatting error message. Once the underlying error message can be changed and will overflow the buffer.

Change to snprintf.

> The "%zd" format in sprintf() is not portable.

Change to "%ld"
History
Date User Action Args
2022-04-11 14:58:13adminsetgithub: 67766
2022-03-24 00:24:53iritkatrielsetversions: + Python 3.11, - Python 3.7
2017-02-26 07:33:44serhiy.storchakasetpriority: normal -> low
assignee: serhiy.storchaka
stage: needs patch -> patch review
2017-02-25 15:49:08louielusetmessages: + msg288570
2017-02-25 15:10:39serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg288569
2017-02-25 14:11:12louielusetnosy: + louielu
messages: + msg288565
2017-02-25 14:10:03louielusetpull_requests: + pull_request262
2016-10-08 19:20:36r.david.murraysetkeywords: + easy (C)
stage: needs patch
versions: + Python 3.7, - Python 2.7, Python 3.4
2015-03-03 23:04:35ned.deilysetnosy: + mark.dickinson, meador.inge
2015-03-03 17:03:05alynncreate