Skip to content

Commit 2ad9382

Browse files
bpo-42431: Fix outdated bytes comments (GH-23458)
Also move definitions of internal macros F_LJUST etc to private header.
1 parent f3c3ea9 commit 2ad9382

File tree

11 files changed

+65
-60
lines changed

11 files changed

+65
-60
lines changed

Include/bytesobject.h

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
/* Bytes (String) object interface */
2+
/* Bytes object interface */
33

44
#ifndef Py_BYTESOBJECT_H
55
#define Py_BYTESOBJECT_H
@@ -10,23 +10,20 @@ extern "C" {
1010
#include <stdarg.h>
1111

1212
/*
13-
Type PyBytesObject represents a character string. An extra zero byte is
13+
Type PyBytesObject represents a byte string. An extra zero byte is
1414
reserved at the end to ensure it is zero-terminated, but a size is
1515
present so strings with null bytes in them can be represented. This
1616
is an immutable object type.
1717
18-
There are functions to create new string objects, to test
19-
an object for string-ness, and to get the
20-
string value. The latter function returns a null pointer
18+
There are functions to create new bytes objects, to test
19+
an object for bytes-ness, and to get the
20+
byte string value. The latter function returns a null pointer
2121
if the object is not of the proper type.
2222
There is a variant that takes an explicit size as well as a
2323
variant that assumes a zero-terminated string. Note that none of the
24-
functions should be applied to nil objects.
24+
functions should be applied to NULL pointer.
2525
*/
2626

27-
/* Caching the hash (ob_shash) saves recalculation of a string's hash value.
28-
This significantly speeds up dict lookups. */
29-
3027
PyAPI_DATA(PyTypeObject) PyBytes_Type;
3128
PyAPI_DATA(PyTypeObject) PyBytesIter_Type;
3229

@@ -50,26 +47,16 @@ PyAPI_FUNC(PyObject *) PyBytes_DecodeEscape(const char *, Py_ssize_t,
5047
const char *, Py_ssize_t,
5148
const char *);
5249

53-
/* Provides access to the internal data buffer and size of a string
54-
object or the default encoded version of a Unicode object. Passing
55-
NULL as *len parameter will force the string buffer to be
56-
0-terminated (passing a string with embedded NULL characters will
50+
/* Provides access to the internal data buffer and size of a bytes object.
51+
Passing NULL as len parameter will force the string buffer to be
52+
0-terminated (passing a string with embedded NUL characters will
5753
cause an exception). */
5854
PyAPI_FUNC(int) PyBytes_AsStringAndSize(
59-
PyObject *obj, /* string or Unicode object */
55+
PyObject *obj, /* bytes object */
6056
char **s, /* pointer to buffer variable */
61-
Py_ssize_t *len /* pointer to length variable or NULL
62-
(only possible for 0-terminated
63-
strings) */
57+
Py_ssize_t *len /* pointer to length variable or NULL */
6458
);
6559

66-
/* Flags used by string formatting */
67-
#define F_LJUST (1<<0)
68-
#define F_SIGN (1<<1)
69-
#define F_BLANK (1<<2)
70-
#define F_ALT (1<<3)
71-
#define F_ZERO (1<<4)
72-
7360
#ifndef Py_LIMITED_API
7461
# define Py_CPYTHON_BYTESOBJECT_H
7562
# include "cpython/bytesobject.h"

Include/cpython/bytesobject.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ typedef struct {
1010
/* Invariants:
1111
* ob_sval contains space for 'ob_size+1' elements.
1212
* ob_sval[ob_size] == 0.
13-
* ob_shash is the hash of the string or -1 if not computed yet.
13+
* ob_shash is the hash of the byte string or -1 if not computed yet.
1414
*/
1515
} PyBytesObject;
1616

Include/internal/pycore_format.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#ifndef Py_INTERNAL_FORMAT_H
2+
#define Py_INTERNAL_FORMAT_H
3+
#ifdef __cplusplus
4+
extern "C" {
5+
#endif
6+
7+
#ifndef Py_BUILD_CORE
8+
# error "this header requires Py_BUILD_CORE define"
9+
#endif
10+
11+
/* Format codes
12+
* F_LJUST '-'
13+
* F_SIGN '+'
14+
* F_BLANK ' '
15+
* F_ALT '#'
16+
* F_ZERO '0'
17+
*/
18+
#define F_LJUST (1<<0)
19+
#define F_SIGN (1<<1)
20+
#define F_BLANK (1<<2)
21+
#define F_ALT (1<<3)
22+
#define F_ZERO (1<<4)
23+
24+
#ifdef __cplusplus
25+
}
26+
#endif
27+
#endif /* !Py_INTERNAL_FORMAT_H */

Makefile.pre.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1112,6 +1112,7 @@ PYTHON_HEADERS= \
11121112
$(srcdir)/Include/internal/pycore_context.h \
11131113
$(srcdir)/Include/internal/pycore_dtoa.h \
11141114
$(srcdir)/Include/internal/pycore_fileutils.h \
1115+
$(srcdir)/Include/internal/pycore_format.h \
11151116
$(srcdir)/Include/internal/pycore_getopt.h \
11161117
$(srcdir)/Include/internal/pycore_gil.h \
11171118
$(srcdir)/Include/internal/pycore_hamt.h \

Objects/bytearrayobject.c

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@ class bytearray "PyByteArrayObject *" "&PyByteArray_Type"
1313
[clinic start generated code]*/
1414
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=5535b77c37a119e0]*/
1515

16+
/* For PyByteArray_AS_STRING(). */
1617
char _PyByteArray_empty_string[] = "";
1718

18-
/* end nullbytes support */
19-
2019
/* Helpers */
2120

2221
static int
@@ -266,7 +265,7 @@ PyByteArray_Concat(PyObject *a, PyObject *b)
266265

267266
result = (PyByteArrayObject *) \
268267
PyByteArray_FromStringAndSize(NULL, va.len + vb.len);
269-
// result->ob_bytes is NULL if result is an empty string:
268+
// result->ob_bytes is NULL if result is an empty bytearray:
270269
// if va.len + vb.len equals zero.
271270
if (result != NULL && result->ob_bytes != NULL) {
272271
memcpy(result->ob_bytes, va.buf, va.len);
@@ -1007,9 +1006,6 @@ bytearray_richcompare(PyObject *self, PyObject *other, int op)
10071006
Py_buffer self_bytes, other_bytes;
10081007
int cmp;
10091008

1010-
/* Bytes can be compared to anything that supports the (binary)
1011-
buffer API. Except that a comparison with Unicode is always an
1012-
error, even if the comparison is for equality. */
10131009
if (!PyObject_CheckBuffer(self) || !PyObject_CheckBuffer(other)) {
10141010
if (PyUnicode_Check(self) || PyUnicode_Check(other)) {
10151011
if (_Py_GetConfig()->bytes_warning && (op == Py_EQ || op == Py_NE)) {
@@ -1021,6 +1017,7 @@ bytearray_richcompare(PyObject *self, PyObject *other, int op)
10211017
Py_RETURN_NOTIMPLEMENTED;
10221018
}
10231019

1020+
/* Bytearrays can be compared to anything that supports the buffer API. */
10241021
if (PyObject_GetBuffer(self, &self_bytes, PyBUF_SIMPLE) != 0) {
10251022
PyErr_Clear();
10261023
Py_RETURN_NOTIMPLEMENTED;
@@ -1328,7 +1325,7 @@ bytearray_translate_impl(PyByteArrayObject *self, PyObject *table,
13281325
if (trans_table[c] != -1)
13291326
*output++ = (char)trans_table[c];
13301327
}
1331-
/* Fix the size of the resulting string */
1328+
/* Fix the size of the resulting bytearray */
13321329
if (inlen > 0)
13331330
if (PyByteArray_Resize(result, output - output_start) < 0) {
13341331
Py_CLEAR(result);
@@ -2083,7 +2080,7 @@ bytearray.hex
20832080
How many bytes between separators. Positive values count from the
20842081
right, negative values count from the left.
20852082
2086-
Create a str of hexadecimal numbers from a bytearray object.
2083+
Create a string of hexadecimal numbers from a bytearray object.
20872084
20882085
Example:
20892086
>>> value = bytearray([0xb9, 0x01, 0xef])
@@ -2099,7 +2096,7 @@ Create a str of hexadecimal numbers from a bytearray object.
20992096

21002097
static PyObject *
21012098
bytearray_hex_impl(PyByteArrayObject *self, PyObject *sep, int bytes_per_sep)
2102-
/*[clinic end generated code: output=29c4e5ef72c565a0 input=814c15830ac8c4b5]*/
2099+
/*[clinic end generated code: output=29c4e5ef72c565a0 input=808667e49bcccb54]*/
21032100
{
21042101
char* argbuf = PyByteArray_AS_STRING(self);
21052102
Py_ssize_t arglen = PyByteArray_GET_SIZE(self);
@@ -2358,7 +2355,7 @@ PyTypeObject PyByteArray_Type = {
23582355
PyObject_Del, /* tp_free */
23592356
};
23602357

2361-
/*********************** Bytes Iterator ****************************/
2358+
/*********************** Bytearray Iterator ****************************/
23622359

23632360
typedef struct {
23642361
PyObject_HEAD

Objects/bytesobject.c

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "Python.h"
66
#include "pycore_abstract.h" // _PyIndex_Check()
77
#include "pycore_bytes_methods.h" // _Py_bytes_startswith()
8+
#include "pycore_format.h" // F_LJUST
89
#include "pycore_initconfig.h" // _PyStatus_OK()
910
#include "pycore_object.h" // _PyObject_GC_TRACK
1011
#include "pycore_pymem.h" // PYMEM_CLEANBYTE
@@ -21,11 +22,11 @@ class bytes "PyBytesObject *" "&PyBytes_Type"
2122

2223
_Py_IDENTIFIER(__bytes__);
2324

24-
/* PyBytesObject_SIZE gives the basic size of a string; any memory allocation
25-
for a string of length n should request PyBytesObject_SIZE + n bytes.
25+
/* PyBytesObject_SIZE gives the basic size of a bytes object; any memory allocation
26+
for a bytes object of length n should request PyBytesObject_SIZE + n bytes.
2627
2728
Using PyBytesObject_SIZE instead of sizeof(PyBytesObject) saves
28-
3 bytes per string allocation on a typical system.
29+
3 or 7 bytes per bytes object allocation on a typical system.
2930
*/
3031
#define PyBytesObject_SIZE (offsetof(PyBytesObject, ob_sval) + 1)
3132

@@ -439,19 +440,6 @@ getnextarg(PyObject *args, Py_ssize_t arglen, Py_ssize_t *p_argidx)
439440
return NULL;
440441
}
441442

442-
/* Format codes
443-
* F_LJUST '-'
444-
* F_SIGN '+'
445-
* F_BLANK ' '
446-
* F_ALT '#'
447-
* F_ZERO '0'
448-
*/
449-
#define F_LJUST (1<<0)
450-
#define F_SIGN (1<<1)
451-
#define F_BLANK (1<<2)
452-
#define F_ALT (1<<3)
453-
#define F_ZERO (1<<4)
454-
455443
/* Returns a new reference to a PyBytes object, or NULL on failure. */
456444

457445
static char*
@@ -1560,7 +1548,7 @@ bytes_richcompare(PyBytesObject *a, PyBytesObject *b, int op)
15601548
case Py_EQ:
15611549
case Py_LE:
15621550
case Py_GE:
1563-
/* a string is equal to itself */
1551+
/* a byte string is equal to itself */
15641552
Py_RETURN_TRUE;
15651553
case Py_NE:
15661554
case Py_LT:
@@ -2149,7 +2137,7 @@ bytes_translate_impl(PyBytesObject *self, PyObject *table,
21492137
Py_INCREF(input_obj);
21502138
return input_obj;
21512139
}
2152-
/* Fix the size of the resulting string */
2140+
/* Fix the size of the resulting byte string */
21532141
if (inlen > 0)
21542142
_PyBytes_Resize(&result, output - output_start);
21552143
return result;
@@ -2453,7 +2441,7 @@ bytes.hex
24532441
How many bytes between separators. Positive values count from the
24542442
right, negative values count from the left.
24552443
2456-
Create a str of hexadecimal numbers from a bytes object.
2444+
Create a string of hexadecimal numbers from a bytes object.
24572445
24582446
Example:
24592447
>>> value = b'\xb9\x01\xef'
@@ -2469,7 +2457,7 @@ Create a str of hexadecimal numbers from a bytes object.
24692457

24702458
static PyObject *
24712459
bytes_hex_impl(PyBytesObject *self, PyObject *sep, int bytes_per_sep)
2472-
/*[clinic end generated code: output=1f134da504064139 input=f1238d3455990218]*/
2460+
/*[clinic end generated code: output=1f134da504064139 input=1a21282b1f1ae595]*/
24732461
{
24742462
const char *argbuf = PyBytes_AS_STRING(self);
24752463
Py_ssize_t arglen = PyBytes_GET_SIZE(self);
@@ -2771,7 +2759,7 @@ _PyBytes_FromIterator(PyObject *it, PyObject *x)
27712759
Py_ssize_t i, size;
27722760
_PyBytesWriter writer;
27732761

2774-
/* For iterator version, create a string object and resize as needed */
2762+
/* For iterator version, create a bytes object and resize as needed */
27752763
size = PyObject_LengthHint(x, 64);
27762764
if (size == -1 && PyErr_Occurred())
27772765
return NULL;

Objects/clinic/bytearrayobject.c.h

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Objects/clinic/bytesobject.c.h

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Objects/unicodeobject.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
4242
#include "Python.h"
4343
#include "pycore_abstract.h" // _PyIndex_Check()
4444
#include "pycore_bytes_methods.h" // _Py_bytes_lower()
45+
#include "pycore_format.h" // F_LJUST
4546
#include "pycore_initconfig.h" // _PyStatus_OK()
4647
#include "pycore_interp.h" // PyInterpreterState.fs_codec
4748
#include "pycore_object.h" // _PyObject_GC_TRACK()

PCbuild/pythoncore.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@
176176
<ClInclude Include="..\Include\internal\pycore_context.h" />
177177
<ClInclude Include="..\Include\internal\pycore_dtoa.h" />
178178
<ClInclude Include="..\Include\internal\pycore_fileutils.h" />
179+
<ClInclude Include="..\Include\internal\pycore_format.h" />
179180
<ClInclude Include="..\Include\internal\pycore_gc.h" />
180181
<ClInclude Include="..\Include\internal\pycore_getopt.h" />
181182
<ClInclude Include="..\Include\internal\pycore_gil.h" />

PCbuild/pythoncore.vcxproj.filters

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,9 @@
510510
<ClInclude Include="..\Include\internal\pycore_fileutils.h">
511511
<Filter>Include\internal</Filter>
512512
</ClInclude>
513+
<ClInclude Include="..\Include\internal\pycore_format.h">
514+
<Filter>Include\internal</Filter>
515+
</ClInclude>
513516
<ClInclude Include="..\Include\internal\pycore_gc.h">
514517
<Filter>Include\internal</Filter>
515518
</ClInclude>

0 commit comments

Comments
 (0)