-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
bpo-21071: struct.Struct.format type is now str #845
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
@Haypo, thanks for your PR! By analyzing the history of the files in this pull request, we identified @mdickinson, @Yhg1s and @serhiy-storchaka to be potential reviewers. |
Misc/NEWS
Outdated
@@ -291,6 +291,9 @@ Extension Modules | |||
Library | |||
------- | |||
|
|||
- bpo-21071: struct.Struct.format is now a Unicode instead, instead of a bytes |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unicode instead → Unicode string
bytes string → byte string [or bytes object if you prefer]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed
Doc/library/struct.rst
Outdated
@@ -443,6 +443,9 @@ The :mod:`struct` module also defines the following type: | |||
|
|||
The format string used to construct this Struct object. | |||
|
|||
.. versionchanged:: 3.6 | |||
The format string is now a Unicode string, instead of a bytes string. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
3.6 → 3.7
I think either “byte string” or “bytes object” would read better.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe use just str
and bytes
? There is nothing specific to the Unicode standard here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed, I used "str" and "bytes" types.
@vadmium, @serhiy-storchaka: I updated my PR to take in account your remarks. Would you mind to review my PR again, please? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this change breaks backward compatibility, it should be documented in What's New.
What is the result of the discussion on Python-Dev?
@@ -362,6 +362,9 @@ Extension Modules | |||
Library | |||
------- | |||
|
|||
- bpo-21071: struct.Struct.format type is now :class:`str` instead of |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think :class:
is not needed.
@@ -1957,8 +1957,8 @@ s_pack_into(PyObject *self, PyObject **args, Py_ssize_t nargs, PyObject *kwnames | |||
static PyObject * | |||
s_get_format(PyStructObject *self, void *unused) | |||
{ | |||
Py_INCREF(self->s_format); | |||
return self->s_format; | |||
return PyUnicode_FromStringAndSize(PyBytes_AS_STRING(self->s_format), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not make s_format
an instance of str?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried to write minimum changes. I expect that most of the code is written to work with C strings char*, not with Python Unicode strings.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FWIW I already did this in my format-str.patch. I was able to get a char* out of the string with PyUnicode_AsUTF8.
No answer: https://mail.python.org/pipermail/python-dev/2017-March/147688.html |
Ok, done. The change is now documented in the changelog (Misc/NEWS), twice in What's New in Python 3.7 (struct module and Porting to Python 3.7), and in struct documentation. I don't think that you can document it more :-) Seriously, I don't think that anyone rely on the exact type of struct.Struct.format. If someone cares, it will be trivial to notice, and also likely trivial to fix the code. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, but add a reference to the issue and wait yet short time for a reaction on Python-Dev.
Doc/whatsnew/3.7.rst
Outdated
@@ -425,6 +431,9 @@ Changes in the Python API | |||
``makedirs()``. | |||
(Contributed by Serhiy Storchaka in :issue:`19930`.) | |||
|
|||
* The :attr:`struct.Struct.format` type is now :class:`str` instead of | |||
:class:`bytes` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a reference to issue.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, done
Doc/whatsnew/3.7.rst
Outdated
struct | ||
------ | ||
|
||
The :attr:`struct.Struct.format` type is now :class:`str` instead of |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually I'm not sure that mentioning this change here is needed. This is not a new feature.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, removed
@@ -618,6 +618,14 @@ def test_issue29802(self): | |||
# Shouldn't crash. | |||
self.assertEqual(struct.unpack(b'b', b'a'), (b'a'[0],)) | |||
|
|||
def test_format_attr(self): | |||
s = struct.Struct('=i2H') | |||
self.assertEqual(s.format, '=i2H') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO, self.assertIsInstance(s.format, str)
would be better to understand (for future readers) the behavior change.
@serhiy-storchaka: "LGTM, but add a reference to the issue and wait yet short time for a reaction on Python-Dev." Serhiy wrote an email to my old thread: @ncoghlan wrote "As long as it's noted in the "Porting to Python 3.7" section of the 3.7 What's New guide, this seems like a sensible change to me." It's the case, I added an entry to Porting to Python 3.7. I will wait until Friday (June 23) evening. |
It's today. I began by rebasing my change. |
No description provided.