classification
Title: Not so correct error message when initializing Struct with ill argument
Type: behavior Stage: resolved
Components: Extension Modules Versions: Python 3.7, Python 3.6
process
Status: closed Resolution: fixed
Dependencies: 30246 Superseder:
Assigned To: Nosy List: mark.dickinson, martin.panter, meador.inge, serhiy.storchaka, vajrasky
Priority: normal Keywords: patch

Created on 2013-12-15 09:28 by vajrasky, last changed 2017-09-14 03:26 by xiang.zhang. This issue is now closed.

Files
File name Uploaded Description Edit
better_error_message_struct_python_34_and_33.patch vajrasky, 2013-12-15 09:28 Python 3.3 and 3.4 review
better_error_message_struct_python_27.patch vajrasky, 2013-12-15 09:29 Python 2.7 review
better_error_message_struct_python_34_and_33_v2.patch vajrasky, 2013-12-15 14:43 Python 3.3 and 3.4 review
Messages (6)
msg206218 - (view) Author: Vajrasky Kok (vajrasky) * Date: 2013-12-15 09:28
Python 3.4 (3.3 is also afflicted:

>>> import struct
>>> struct.Struct(3)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: Struct() argument 1 must be a bytes object, not int
>>> struct.Struct('b')
<Struct object at 0x7fec04763180>

Python 2.7:

>>> import struct
>>> struct.Struct(3)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: Struct() argument 1 must be string, not int
>>> struct.Struct(u'b')
<Struct object at 0x17993e8>

Here is the patch to better error message for Python 3.4 and 3.3.
msg206219 - (view) Author: Vajrasky Kok (vajrasky) * Date: 2013-12-15 09:29
And here is the patch to better error message in Python 2.7.
msg206233 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2013-12-15 13:40
I think that error message in 2.7 is correct. "String" means both str and unicode.

As for 3.x, agree, it should be corrected. But I prefer "str or bytes" or "string or bytes object".
msg206235 - (view) Author: Vajrasky Kok (vajrasky) * Date: 2013-12-15 14:43
Here is the patch to address Serhiy's request.

Hmmm, if string means both string and unicode in Python 2.7, should we fix these behaviours?

>>> import _csv
>>> _csv.register_dialect(2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: dialect name must be a string or unicode
>>> ' cute cat '.strip(3)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: strip arg must be None, str or unicode
>>> import sqlite3
>>> conn = sqlite3.connect(':memory:')
>>> c = conn.cursor()
>>> c.execute(3)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: operation parameter must be str or unicode
msg236115 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2015-02-16 22:06
Closely related:

* Issue 16349: document byte string format argument support
* Issue 21071: should the Struct.format property be bytes or text?
msg302113 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2017-09-13 20:48
For Python 2.7, this change doesn’t seem important enough for a bug fix.
History
Date User Action Args
2017-09-14 03:26:10xiang.zhangsetversions: + Python 3.6, Python 3.7, - Python 3.3, Python 3.4
2017-09-14 03:25:33xiang.zhangsetstatus: open -> closed
dependencies: + fix several error messages in struct, - Document whether it's safe to use bytes for struct format string
resolution: fixed
stage: resolved
2017-09-13 20:48:14martin.pantersetmessages: + msg302113
2016-04-15 04:00:33martin.pantersetdependencies: + Document whether it's safe to use bytes for struct format string, - Not so correct error message when initializing Struct with ill argument
2016-04-15 04:00:33martin.panterunlinkissue19985 dependencies
2016-04-14 23:43:40martin.pantersetdependencies: + Not so correct error message when initializing Struct with ill argument
2016-04-14 23:43:40martin.panterlinkissue19985 dependencies
2015-02-16 22:06:14martin.pantersetnosy: + martin.panter
messages: + msg236115
2013-12-15 14:43:10vajraskysetfiles: + better_error_message_struct_python_34_and_33_v2.patch

messages: + msg206235
2013-12-15 13:40:49serhiy.storchakasetnosy: + mark.dickinson, meador.inge
2013-12-15 13:40:25serhiy.storchakasetnosy: + serhiy.storchaka

messages: + msg206233
versions: - Python 2.7
2013-12-15 09:29:11vajraskysetfiles: + better_error_message_struct_python_27.patch

messages: + msg206219
2013-12-15 09:28:50vajraskycreate