Skip to content

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

Merged
merged 1 commit into from
Jun 23, 2017
Merged

bpo-21071: struct.Struct.format type is now str #845

merged 1 commit into from
Jun 23, 2017

Conversation

vstinner
Copy link
Member

No description provided.

@mention-bot
Copy link

@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
Copy link
Member

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]

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

@@ -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.
Copy link
Member

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.

Copy link
Member

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.

Copy link
Member Author

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.

@brettcannon brettcannon added the type-feature A feature request or enhancement label Mar 27, 2017
@vstinner
Copy link
Member Author

@vadmium, @serhiy-storchaka: I updated my PR to take in account your remarks. Would you mind to review my PR again, please?

Copy link
Member

@serhiy-storchaka serhiy-storchaka left a 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
Copy link
Member

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),
Copy link
Member

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?

Copy link
Member Author

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.

Copy link
Member

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.

@vstinner
Copy link
Member Author

What is the result of the discussion on Python-Dev?

No answer: https://mail.python.org/pipermail/python-dev/2017-March/147688.html

@vstinner
Copy link
Member Author

Since this change breaks backward compatibility, it should be documented in What's New.

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.

Copy link
Member

@serhiy-storchaka serhiy-storchaka left a 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.

@@ -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`
Copy link
Member

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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, done

struct
------

The :attr:`struct.Struct.format` type is now :class:`str` instead of
Copy link
Member

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.

Copy link
Member Author

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')
Copy link
Member

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.

@vstinner
Copy link
Member Author

@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:
https://mail.python.org/pipermail/python-dev/2017-June/148360.html

@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.

@vstinner
Copy link
Member Author

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.

@vstinner vstinner merged commit f87b85f into python:master Jun 23, 2017
@vstinner vstinner deleted the struct_format branch June 23, 2017 13:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-feature A feature request or enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants