summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@gmail.com>2017-06-23 13:11:12 (GMT)
committerGitHub <noreply@github.com>2017-06-23 13:11:12 (GMT)
commitf87b85f80853c580b1c8bf78a51b0e9a25f6e1a7 (patch)
tree8417421b14db403e4df4b264c4e4ec09e7c1f4b9
parenta4b091e135ccf345cfafdd8477aef897c5214f82 (diff)
downloadcpython-f87b85f80853c580b1c8bf78a51b0e9a25f6e1a7.zip
cpython-f87b85f80853c580b1c8bf78a51b0e9a25f6e1a7.tar.gz
cpython-f87b85f80853c580b1c8bf78a51b0e9a25f6e1a7.tar.bz2
bpo-21071: struct.Struct.format type is now str (#845)
-rw-r--r--Doc/library/struct.rst3
-rw-r--r--Doc/whatsnew/3.7.rst3
-rw-r--r--Lib/test/test_struct.py8
-rw-r--r--Misc/NEWS3
-rw-r--r--Modules/_struct.c4
5 files changed, 19 insertions, 2 deletions
diff --git a/Doc/library/struct.rst b/Doc/library/struct.rst
index bb32a65..2d0866c 100644
--- a/Doc/library/struct.rst
+++ b/Doc/library/struct.rst
@@ -443,6 +443,9 @@ The :mod:`struct` module also defines the following type:
The format string used to construct this Struct object.
+ .. versionchanged:: 3.7
+ The format string type is now :class:`str` instead of :class:`bytes`.
+
.. attribute:: size
The calculated size of the struct (and hence of the bytes object produced
diff --git a/Doc/whatsnew/3.7.rst b/Doc/whatsnew/3.7.rst
index 8c1636b..580da62 100644
--- a/Doc/whatsnew/3.7.rst
+++ b/Doc/whatsnew/3.7.rst
@@ -429,6 +429,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`. (Contributed by Victor Stinner in :issue:`21071`.)
+
CPython bytecode changes
------------------------
diff --git a/Lib/test/test_struct.py b/Lib/test/test_struct.py
index b2a5f42..a896468 100644
--- a/Lib/test/test_struct.py
+++ b/Lib/test/test_struct.py
@@ -618,6 +618,14 @@ class StructTest(unittest.TestCase):
# 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')
+
+ # use a bytes string
+ s2 = struct.Struct(s.format.encode())
+ self.assertEqual(s2.format, s.format)
+
class UnpackIteratorTest(unittest.TestCase):
"""
diff --git a/Misc/NEWS b/Misc/NEWS
index 4b6b8fc..33a1593 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -374,6 +374,9 @@ Extension Modules
Library
-------
+- bpo-21071: struct.Struct.format type is now :class:`str` instead of
+ :class:`bytes`.
+
- bpo-29212: Fix concurrent.futures.thread.ThreadPoolExecutor threads to have
a non repr() based thread name by default when no thread_name_prefix is
supplied. They will now identify themselves as "ThreadPoolExecutor-y_n".
diff --git a/Modules/_struct.c b/Modules/_struct.c
index 46e7b40..b4febf7 100644
--- a/Modules/_struct.c
+++ b/Modules/_struct.c
@@ -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),
+ PyBytes_GET_SIZE(self->s_format));
}
static PyObject *