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 raises unexpected error message
Type: behavior Stage: resolved
Components: Extension Modules, IO Versions: Python 3.2, Python 3.3, Python 3.4, Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: ezio.melotti, makimat, mark.dickinson, mesheb82, petri.lehtinen, python-dev, serhiy.storchaka, tshepang
Priority: normal Keywords: patch

Created on 2012-05-24 06:21 by mesheb82, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
issue14897.patch makimat, 2012-10-23 12:50 review
issue14897-2.patch makimat, 2012-10-24 06:21
Messages (16)
msg161481 - (view) Author: mesheb82 (mesheb82) Date: 2012-05-24 06:21
I found some unexpected behavior while working with the struct module.
>>> import struct

This works as expected:
>>> struct.pack('1s1s','3','4')
'34'

In this case, with bad input, the error message says I need 2 arguments, when I provide 2 arguments.
>>> struct.pack('1s1s','33')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
struct.error: pack requires exactly 2 arguments
msg161482 - (view) Author: mesheb82 (mesheb82) Date: 2012-05-24 06:23
Also, I tested this on Windows in Python 3.2.3 and Windows in Python 2.7.2
msg161487 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2012-05-24 08:22
Hmm.  Yes, that's not very clear.  The same message is used both for struct.pack and for Struct.pack (which struct.pack is really just a convenient alias for);  it makes a bit more sense for the latter:

>>> struct.Struct('1s1s').pack('33')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
struct.error: pack requires exactly 2 arguments
msg161488 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2012-05-24 08:41
Funny. struct.pack(fmt, args...) is just an alias to struct.Struct(fmt).pack(args...). The error message should be changed to explicitly state that we are talking about the data for packing, and not about the arguments of function. Or should remove mention of the number of arguments at all (leave only "too much" or "too little").
msg161489 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2012-05-24 08:47
It might help if the error message also stated how many arguments were actually received, like the TypeError message already does for bad function / method calls.  E.g., 

"struct.error: pack expected 2 items for packing (got 1)"
msg161493 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2012-05-24 09:33
> It might help if the error message also stated how many arguments were actually received, like the TypeError message already does for bad function / method calls.  E.g., 
> 
> "struct.error: pack expected 2 items for packing (got 1)"

Yes, this would be useful. But seldom implemented.

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: not enough arguments for format string
>>> '%s %s'%(123,456,789)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: not all arguments converted during string formatting

struct.pack also inconsistent in other error messages.

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
struct.error: argument for 's' must be a bytes object
>>> struct.pack('i', '123')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
struct.error: required argument is not an integer

For "s" is mentioned format, and for "i" no. It would be helpful to
mention also the number of the item.
msg173608 - (view) Author: Matti Mäki (makimat) * Date: 2012-10-23 12:50
Changed the error message as suggested by http://bugs.python.org/issue14897#msg161493

Also made similar change to struct.pack_into
msg173652 - (view) Author: Matti Mäki (makimat) * Date: 2012-10-24 06:21
A second try, now taking into account buffer and offset params in pack_into.
msg173656 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2012-10-24 08:34
LGTM.
msg174143 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2012-10-29 19:29
New changeset 626ed0e06fd6 by Petri Lehtinen in branch '2.7':
#14897: Enhance error messages of struct.pack and struct.pack_into
http://hg.python.org/cpython/rev/626ed0e06fd6

New changeset a555bd4026b0 by Petri Lehtinen in branch '3.2':
#14897: Enhance error messages of struct.pack and struct.pack_into
http://hg.python.org/cpython/rev/a555bd4026b0

New changeset 70d5906e0461 by Petri Lehtinen in branch '3.3':
#14897: Enhance error messages of struct.pack and struct.pack_into
http://hg.python.org/cpython/rev/70d5906e0461

New changeset ebc588a3db51 by Petri Lehtinen in branch 'default':
#14897: Enhance error messages of struct.pack and struct.pack_into
http://hg.python.org/cpython/rev/ebc588a3db51
msg174144 - (view) Author: Petri Lehtinen (petri.lehtinen) * (Python committer) Date: 2012-10-29 19:29
Applied, thanks!
msg174185 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2012-10-30 09:06
Matti Mäki, can you please submit a contributor form?

http://python.org/psf/contrib/contrib-form/
http://python.org/psf/contrib/
msg174189 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2012-10-30 09:36
> "Enhance error messages of struct.pack and struct.pack_into"

You probably should have used the word 'fix' rather than 'enhance' here:  else it smells like you're putting new features into a maintenance release. :-)
msg174191 - (view) Author: Petri Lehtinen (petri.lehtinen) * (Python committer) Date: 2012-10-30 09:44
> You probably should have used the word 'fix' rather than 'enhance' here:  else it smells like you're putting new features into a maintenance release. :-)

Ah, true. But unfixable now :(
msg174194 - (view) Author: Matti Mäki (makimat) * Date: 2012-10-30 10:03
I filled up the contributor form and gave it to Petri Lehtinen 2012-10-22
msg174195 - (view) Author: Petri Lehtinen (petri.lehtinen) * (Python committer) Date: 2012-10-30 10:05
Matti Mäki wrote:
> I filled up the contributor form and gave it to Petri Lehtinen 2012-10-22

Yeah, and I posted it (along with 14 other contributor forms received
at PyCon Finland) to the PSF last week. They just seem to be a bit
slow to process them.
History
Date User Action Args
2022-04-11 14:57:30adminsetgithub: 59102
2012-10-30 10:05:05petri.lehtinensetmessages: + msg174195
2012-10-30 10:03:14makimatsetmessages: + msg174194
2012-10-30 09:44:36petri.lehtinensetmessages: + msg174191
2012-10-30 09:36:31mark.dickinsonsetmessages: + msg174189
2012-10-30 09:06:59serhiy.storchakasetmessages: + msg174185
2012-10-29 19:29:52petri.lehtinensetstatus: open -> closed

nosy: + petri.lehtinen
messages: + msg174144

resolution: fixed
stage: commit review -> resolved
2012-10-29 19:29:05python-devsetnosy: + python-dev
messages: + msg174143
2012-10-24 21:26:07ezio.melottisetnosy: + ezio.melotti
2012-10-24 08:34:18serhiy.storchakasetstage: commit review
messages: + msg173656
components: + Extension Modules
versions: + Python 3.4
2012-10-24 06:21:14makimatsetfiles: + issue14897-2.patch

messages: + msg173652
2012-10-23 12:50:59makimatsetfiles: + issue14897.patch

nosy: + makimat
messages: + msg173608

keywords: + patch
2012-05-25 16:30:43tshepangsetnosy: + tshepang
2012-05-24 09:33:34serhiy.storchakasetmessages: + msg161493
2012-05-24 08:47:30mark.dickinsonsetmessages: + msg161489
2012-05-24 08:41:13serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg161488
2012-05-24 08:22:21mark.dickinsonsetnosy: + mark.dickinson

messages: + msg161487
versions: + Python 3.3
2012-05-24 06:23:46mesheb82setmessages: + msg161482
2012-05-24 06:21:58mesheb82create