Skip to content

Commit 4b80ef5

Browse files
committed
Issue #8973: Add __all__ to struct module, so that help(struct) correctly
displays information for the struct.Struct class.
1 parent 3a810e6 commit 4b80ef5

3 files changed

Lines changed: 42 additions & 21 deletions

File tree

Lib/struct.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
__all__ = [
2+
# Functions
3+
'calcsize', 'pack', 'unpack', 'unpack', 'unpack_from',
4+
5+
# Classes
6+
'Struct',
7+
8+
# Exceptions
9+
'error'
10+
]
11+
112
from _struct import *
213
from _struct import _clearcache
314
from _struct import __doc__

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1299,6 +1299,9 @@ Library
12991299
Extension Modules
13001300
-----------------
13011301

1302+
- Issue #8973: Add __all__ to struct module; this ensures that
1303+
help(struct) includes documentation for the struct.Struct class.
1304+
13021305
- Issue #3129: Trailing digits in format string are no longer ignored.
13031306
For example, "1" or "ilib123" are now invalid formats and cause
13041307
``struct.error`` to be raised.

Modules/_struct.c

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1398,9 +1398,8 @@ s_unpack_internal(PyStructObject *soself, char *startfrom) {
13981398
PyDoc_STRVAR(s_unpack__doc__,
13991399
"S.unpack(buffer) -> (v1, v2, ...)\n\
14001400
\n\
1401-
Return tuple containing values unpacked according to this Struct's format.\n\
1402-
Requires len(buffer) == self.size. See struct.__doc__ for more on format\n\
1403-
strings.");
1401+
Return a tuple containing values unpacked according to S.format. Requires\n\
1402+
len(buffer) == S.size. See help(struct) for more on format strings.");
14041403

14051404
static PyObject *
14061405
s_unpack(PyObject *self, PyObject *input)
@@ -1426,12 +1425,10 @@ s_unpack(PyObject *self, PyObject *input)
14261425
}
14271426

14281427
PyDoc_STRVAR(s_unpack_from__doc__,
1429-
"S.unpack_from(buffer[, offset]) -> (v1, v2, ...)\n\
1428+
"S.unpack_from(buffer[, offset=0]) -> (v1, v2, ...)\n\
14301429
\n\
1431-
Return tuple containing values unpacked according to this Struct's format.\n\
1432-
Unlike unpack, unpack_from can unpack values from any object supporting\n\
1433-
the buffer API, not just str. Requires len(buffer[offset:]) >= self.size.\n\
1434-
See struct.__doc__ for more on format strings.");
1430+
Return a tuple containing values unpacked according to S.format. Requires\n\
1431+
len(buffer[offset:]) >= S.size. See help(struct) for more on format strings.");
14351432

14361433
static PyObject *
14371434
s_unpack_from(PyObject *self, PyObject *args, PyObject *kwds)
@@ -1566,8 +1563,8 @@ s_pack_internal(PyStructObject *soself, PyObject *args, int offset, char* buf)
15661563
PyDoc_STRVAR(s_pack__doc__,
15671564
"S.pack(v1, v2, ...) -> bytes\n\
15681565
\n\
1569-
Return a bytes containing values v1, v2, ... packed according to this\n\
1570-
Struct's format. See struct.__doc__ for more on format strings.");
1566+
Return a bytes object containing values v1, v2, ... packed according to\n\
1567+
S.format. See help(struct) for more on format strings.");
15711568

15721569
static PyObject *
15731570
s_pack(PyObject *self, PyObject *args)
@@ -1603,10 +1600,9 @@ s_pack(PyObject *self, PyObject *args)
16031600
PyDoc_STRVAR(s_pack_into__doc__,
16041601
"S.pack_into(buffer, offset, v1, v2, ...)\n\
16051602
\n\
1606-
Pack the values v1, v2, ... according to this Struct's format, write \n\
1607-
the packed bytes into the writable buffer buf starting at offset. Note\n\
1608-
that the offset is not an optional argument. See struct.__doc__ for \n\
1609-
more on format strings.");
1603+
Pack the values v1, v2, ... according to S.format and write the packed bytes\n\
1604+
into the writable buffer buf starting at offset. Note that the offset is not\n\
1605+
an optional argument. See help(struct) for more on format strings.");
16101606

16111607
static PyObject *
16121608
s_pack_into(PyObject *self, PyObject *args)
@@ -1796,7 +1792,10 @@ calcsize(PyObject *self, PyObject *fmt)
17961792
}
17971793

17981794
PyDoc_STRVAR(pack_doc,
1799-
"Return bytes containing values v1, v2, ... packed according to fmt.");
1795+
"pack(fmt, v1, v2, ...) -> bytes\n\
1796+
\n\
1797+
Return a bytes object containing values v1, v2, ... packed according to fmt.\n\
1798+
See help(struct) for more on format strings.");
18001799

18011800
static PyObject *
18021801
pack(PyObject *self, PyObject *args)
@@ -1825,8 +1824,11 @@ pack(PyObject *self, PyObject *args)
18251824
}
18261825

18271826
PyDoc_STRVAR(pack_into_doc,
1828-
"Pack the values v1, v2, ... according to fmt.\n\
1829-
Write the packed bytes into the writable buffer buf starting at offset.");
1827+
"pack_into(fmt, buffer, offset, v1, v2, ...)\n\
1828+
\n\
1829+
Pack the values v1, v2, ... according to fmt and write the packed bytes into\n\
1830+
the writable buffer buf starting at offset. Note that the offset is not an\n\
1831+
optional argument. See help(struct) for more on format strings.");
18301832

18311833
static PyObject *
18321834
pack_into(PyObject *self, PyObject *args)
@@ -1855,8 +1857,10 @@ pack_into(PyObject *self, PyObject *args)
18551857
}
18561858

18571859
PyDoc_STRVAR(unpack_doc,
1858-
"Unpack the bytes containing packed C structure data, according to fmt.\n\
1859-
Requires len(bytes) == calcsize(fmt).");
1860+
"unpack(fmt, buffer) -> (v1, v2, ...)\n\
1861+
\n\
1862+
Return a tuple containing values unpacked according to fmt. Requires\n\
1863+
len(buffer) == calcsize(fmt). See help(struct) for more on format strings.");
18601864

18611865
static PyObject *
18621866
unpack(PyObject *self, PyObject *args)
@@ -1875,8 +1879,11 @@ unpack(PyObject *self, PyObject *args)
18751879
}
18761880

18771881
PyDoc_STRVAR(unpack_from_doc,
1878-
"Unpack the buffer, containing packed C structure data, according to\n\
1879-
fmt, starting at offset. Requires len(buffer[offset:]) >= calcsize(fmt).");
1882+
"unpack_from(fmt, buffer[, offset=0]) -> (v1, v2, ...)\n\
1883+
\n\
1884+
Return a tuple containing values unpacked according to fmt. Requires\n\
1885+
len(buffer[offset:]) >= calcsize(fmt). See help(struct) for more on format\n\
1886+
strings.");
18801887

18811888
static PyObject *
18821889
unpack_from(PyObject *self, PyObject *args, PyObject *kwds)

0 commit comments

Comments
 (0)