Skip to content

Commit 1c92c0e

Browse files
authored
bpo-30052: Link bytes & bytearray to stdtypes not functions (GH-1271) (GH-1915)
Builtin container types have two potential link targets in the docs: - their entry in the list of builtin callables - their type documentation This change brings `bytes` and `bytearray` into line with other container types by having cross-references default to linking to their type documentation, rather than their builtin callable entry.. (cherry picked from commit c6db481)
1 parent e417d12 commit 1c92c0e

File tree

3 files changed

+92
-83
lines changed

3 files changed

+92
-83
lines changed

Doc/library/functions.rst

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ are always available. They are listed here in alphabetical order.
1616
:func:`ascii` :func:`enumerate` :func:`input` :func:`oct` :func:`staticmethod`
1717
:func:`bin` :func:`eval` :func:`int` :func:`open` |func-str|_
1818
:func:`bool` :func:`exec` :func:`isinstance` :func:`ord` :func:`sum`
19-
:func:`bytearray` :func:`filter` :func:`issubclass` :func:`pow` :func:`super`
20-
:func:`bytes` :func:`float` :func:`iter` :func:`print` |func-tuple|_
19+
|func-bytearray|_ :func:`filter` :func:`issubclass` :func:`pow` :func:`super`
20+
|func-bytes|_ :func:`float` :func:`iter` :func:`print` |func-tuple|_
2121
:func:`callable` :func:`format` :func:`len` :func:`property` :func:`type`
2222
:func:`chr` |func-frozenset|_ |func-list|_ |func-range|_ :func:`vars`
2323
:func:`classmethod` :func:`getattr` :func:`locals` :func:`repr` :func:`zip`
@@ -37,7 +37,8 @@ are always available. They are listed here in alphabetical order.
3737
.. |func-str| replace:: ``str()``
3838
.. |func-tuple| replace:: ``tuple()``
3939
.. |func-range| replace:: ``range()``
40-
40+
.. |func-bytearray| replace:: ``bytearray()``
41+
.. |func-bytes| replace:: ``bytes()``
4142

4243
.. function:: abs(x)
4344

@@ -99,6 +100,7 @@ are always available. They are listed here in alphabetical order.
99100

100101
.. _func-bytearray:
101102
.. class:: bytearray([source[, encoding[, errors]]])
103+
:noindex:
102104

103105
Return a new array of bytes. The :class:`bytearray` class is a mutable
104106
sequence of integers in the range 0 <= x < 256. It has most of the usual
@@ -128,6 +130,7 @@ are always available. They are listed here in alphabetical order.
128130

129131
.. _func-bytes:
130132
.. class:: bytes([source[, encoding[, errors]]])
133+
:noindex:
131134

132135
Return a new "bytes" object, which is an immutable sequence of integers in
133136
the range ``0 <= x < 256``. :class:`bytes` is an immutable version of

Doc/library/stdtypes.rst

Lines changed: 78 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -2264,8 +2264,8 @@ The :mod:`array` module supports efficient storage of basic data types like
22642264

22652265
.. _typebytes:
22662266

2267-
Bytes
2268-
-----
2267+
Bytes Objects
2268+
-------------
22692269

22702270
.. index:: object: bytes
22712271

@@ -2274,65 +2274,67 @@ binary protocols are based on the ASCII text encoding, bytes objects offer
22742274
several methods that are only valid when working with ASCII compatible
22752275
data and are closely related to string objects in a variety of other ways.
22762276

2277-
Firstly, the syntax for bytes literals is largely the same as that for string
2278-
literals, except that a ``b`` prefix is added:
2277+
.. class:: bytes([source[, encoding[, errors]]])
22792278

2280-
* Single quotes: ``b'still allows embedded "double" quotes'``
2281-
* Double quotes: ``b"still allows embedded 'single' quotes"``.
2282-
* Triple quoted: ``b'''3 single quotes'''``, ``b"""3 double quotes"""``
2279+
Firstly, the syntax for bytes literals is largely the same as that for string
2280+
literals, except that a ``b`` prefix is added:
22832281

2284-
Only ASCII characters are permitted in bytes literals (regardless of the
2285-
declared source code encoding). Any binary values over 127 must be entered
2286-
into bytes literals using the appropriate escape sequence.
2282+
* Single quotes: ``b'still allows embedded "double" quotes'``
2283+
* Double quotes: ``b"still allows embedded 'single' quotes"``.
2284+
* Triple quoted: ``b'''3 single quotes'''``, ``b"""3 double quotes"""``
22872285

2288-
As with string literals, bytes literals may also use a ``r`` prefix to disable
2289-
processing of escape sequences. See :ref:`strings` for more about the various
2290-
forms of bytes literal, including supported escape sequences.
2286+
Only ASCII characters are permitted in bytes literals (regardless of the
2287+
declared source code encoding). Any binary values over 127 must be entered
2288+
into bytes literals using the appropriate escape sequence.
22912289

2292-
While bytes literals and representations are based on ASCII text, bytes
2293-
objects actually behave like immutable sequences of integers, with each
2294-
value in the sequence restricted such that ``0 <= x < 256`` (attempts to
2295-
violate this restriction will trigger :exc:`ValueError`. This is done
2296-
deliberately to emphasise that while many binary formats include ASCII based
2297-
elements and can be usefully manipulated with some text-oriented algorithms,
2298-
this is not generally the case for arbitrary binary data (blindly applying
2299-
text processing algorithms to binary data formats that are not ASCII
2300-
compatible will usually lead to data corruption).
2290+
As with string literals, bytes literals may also use a ``r`` prefix to disable
2291+
processing of escape sequences. See :ref:`strings` for more about the various
2292+
forms of bytes literal, including supported escape sequences.
23012293

2302-
In addition to the literal forms, bytes objects can be created in a number of
2303-
other ways:
2294+
While bytes literals and representations are based on ASCII text, bytes
2295+
objects actually behave like immutable sequences of integers, with each
2296+
value in the sequence restricted such that ``0 <= x < 256`` (attempts to
2297+
violate this restriction will trigger :exc:`ValueError`. This is done
2298+
deliberately to emphasise that while many binary formats include ASCII based
2299+
elements and can be usefully manipulated with some text-oriented algorithms,
2300+
this is not generally the case for arbitrary binary data (blindly applying
2301+
text processing algorithms to binary data formats that are not ASCII
2302+
compatible will usually lead to data corruption).
23042303

2305-
* A zero-filled bytes object of a specified length: ``bytes(10)``
2306-
* From an iterable of integers: ``bytes(range(20))``
2307-
* Copying existing binary data via the buffer protocol: ``bytes(obj)``
2304+
In addition to the literal forms, bytes objects can be created in a number of
2305+
other ways:
23082306

2309-
Also see the :ref:`bytes <func-bytes>` built-in.
2307+
* A zero-filled bytes object of a specified length: ``bytes(10)``
2308+
* From an iterable of integers: ``bytes(range(20))``
2309+
* Copying existing binary data via the buffer protocol: ``bytes(obj)``
23102310

2311-
Since 2 hexadecimal digits correspond precisely to a single byte, hexadecimal
2312-
numbers are a commonly used format for describing binary data. Accordingly,
2313-
the bytes type has an additional class method to read data in that format:
2311+
Also see the :ref:`bytes <func-bytes>` built-in.
23142312

2315-
.. classmethod:: bytes.fromhex(string)
2313+
Since 2 hexadecimal digits correspond precisely to a single byte, hexadecimal
2314+
numbers are a commonly used format for describing binary data. Accordingly,
2315+
the bytes type has an additional class method to read data in that format:
23162316

2317-
This :class:`bytes` class method returns a bytes object, decoding the
2318-
given string object. The string must contain two hexadecimal digits per
2319-
byte, with ASCII spaces being ignored.
2317+
.. classmethod:: fromhex(string)
23202318

2321-
>>> bytes.fromhex('2Ef0 F1f2 ')
2322-
b'.\xf0\xf1\xf2'
2319+
This :class:`bytes` class method returns a bytes object, decoding the
2320+
given string object. The string must contain two hexadecimal digits per
2321+
byte, with ASCII whitespace being ignored.
23232322

2324-
A reverse conversion function exists to transform a bytes object into its
2325-
hexadecimal representation.
2323+
>>> bytes.fromhex('2Ef0 F1f2 ')
2324+
b'.\xf0\xf1\xf2'
23262325

2327-
.. method:: bytes.hex()
2326+
A reverse conversion function exists to transform a bytes object into its
2327+
hexadecimal representation.
23282328

2329-
Return a string object containing two hexadecimal digits for each
2330-
byte in the instance.
2329+
.. method:: hex()
23312330

2332-
>>> b'\xf0\xf1\xf2'.hex()
2333-
'f0f1f2'
2331+
Return a string object containing two hexadecimal digits for each
2332+
byte in the instance.
23342333

2335-
.. versionadded:: 3.5
2334+
>>> b'\xf0\xf1\xf2'.hex()
2335+
'f0f1f2'
2336+
2337+
.. versionadded:: 3.5
23362338

23372339
Since bytes objects are sequences of integers (akin to a tuple), for a bytes
23382340
object *b*, ``b[0]`` will be an integer, while ``b[0:1]`` will be a bytes
@@ -2362,45 +2364,49 @@ Bytearray Objects
23622364
.. index:: object: bytearray
23632365

23642366
:class:`bytearray` objects are a mutable counterpart to :class:`bytes`
2365-
objects. There is no dedicated literal syntax for bytearray objects, instead
2366-
they are always created by calling the constructor:
2367+
objects.
23672368

2368-
* Creating an empty instance: ``bytearray()``
2369-
* Creating a zero-filled instance with a given length: ``bytearray(10)``
2370-
* From an iterable of integers: ``bytearray(range(20))``
2371-
* Copying existing binary data via the buffer protocol: ``bytearray(b'Hi!')``
2369+
.. class:: bytearray([source[, encoding[, errors]]])
23722370

2373-
As bytearray objects are mutable, they support the
2374-
:ref:`mutable <typesseq-mutable>` sequence operations in addition to the
2375-
common bytes and bytearray operations described in :ref:`bytes-methods`.
2371+
There is no dedicated literal syntax for bytearray objects, instead
2372+
they are always created by calling the constructor:
23762373

2377-
Also see the :ref:`bytearray <func-bytearray>` built-in.
2374+
* Creating an empty instance: ``bytearray()``
2375+
* Creating a zero-filled instance with a given length: ``bytearray(10)``
2376+
* From an iterable of integers: ``bytearray(range(20))``
2377+
* Copying existing binary data via the buffer protocol: ``bytearray(b'Hi!')``
23782378

2379-
Since 2 hexadecimal digits correspond precisely to a single byte, hexadecimal
2380-
numbers are a commonly used format for describing binary data. Accordingly,
2381-
the bytearray type has an additional class method to read data in that format:
2379+
As bytearray objects are mutable, they support the
2380+
:ref:`mutable <typesseq-mutable>` sequence operations in addition to the
2381+
common bytes and bytearray operations described in :ref:`bytes-methods`.
23822382

2383-
.. classmethod:: bytearray.fromhex(string)
2383+
Also see the :ref:`bytearray <func-bytearray>` built-in.
23842384

2385-
This :class:`bytearray` class method returns bytearray object, decoding
2386-
the given string object. The string must contain two hexadecimal digits
2387-
per byte, with ASCII spaces being ignored.
2385+
Since 2 hexadecimal digits correspond precisely to a single byte, hexadecimal
2386+
numbers are a commonly used format for describing binary data. Accordingly,
2387+
the bytearray type has an additional class method to read data in that format:
23882388

2389-
>>> bytearray.fromhex('2Ef0 F1f2 ')
2390-
bytearray(b'.\xf0\xf1\xf2')
2389+
.. classmethod:: fromhex(string)
23912390

2392-
A reverse conversion function exists to transform a bytearray object into its
2393-
hexadecimal representation.
2391+
This :class:`bytearray` class method returns bytearray object, decoding
2392+
the given string object. The string must contain two hexadecimal digits
2393+
per byte, with ASCII whitespace being ignored.
23942394

2395-
.. method:: bytearray.hex()
2395+
>>> bytearray.fromhex('2Ef0 F1f2 ')
2396+
bytearray(b'.\xf0\xf1\xf2')
23962397

2397-
Return a string object containing two hexadecimal digits for each
2398-
byte in the instance.
2398+
A reverse conversion function exists to transform a bytearray object into its
2399+
hexadecimal representation.
23992400

2400-
>>> bytearray(b'\xf0\xf1\xf2').hex()
2401-
'f0f1f2'
2401+
.. method:: hex()
2402+
2403+
Return a string object containing two hexadecimal digits for each
2404+
byte in the instance.
24022405

2403-
.. versionadded:: 3.5
2406+
>>> bytearray(b'\xf0\xf1\xf2').hex()
2407+
'f0f1f2'
2408+
2409+
.. versionadded:: 3.5
24042410

24052411
Since bytearray objects are sequences of integers (akin to a list), for a
24062412
bytearray object *b*, ``b[0]`` will be an integer, while ``b[0:1]`` will be

Doc/reference/datamodel.rst

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -320,9 +320,9 @@ Sequences
320320

321321
A bytes object is an immutable array. The items are 8-bit bytes,
322322
represented by integers in the range 0 <= x < 256. Bytes literals
323-
(like ``b'abc'``) and the built-in function :func:`bytes` can be used to
324-
construct bytes objects. Also, bytes objects can be decoded to strings
325-
via the :meth:`~bytes.decode` method.
323+
(like ``b'abc'``) and the built-in :func:`bytes()` constructor
324+
can be used to create bytes objects. Also, bytes objects can be
325+
decoded to strings via the :meth:`~bytes.decode` method.
326326

327327
Mutable sequences
328328
.. index::
@@ -349,9 +349,9 @@ Sequences
349349
.. index:: bytearray
350350

351351
A bytearray object is a mutable array. They are created by the built-in
352-
:func:`bytearray` constructor. Aside from being mutable (and hence
353-
unhashable), byte arrays otherwise provide the same interface and
354-
functionality as immutable bytes objects.
352+
:func:`bytearray` constructor. Aside from being mutable
353+
(and hence unhashable), byte arrays otherwise provide the same interface
354+
and functionality as immutable :class:`bytes` objects.
355355

356356
.. index:: module: array
357357

@@ -1253,8 +1253,8 @@ Basic customization
12531253

12541254
.. index:: builtin: bytes
12551255

1256-
Called by :func:`bytes` to compute a byte-string representation of an
1257-
object. This should return a ``bytes`` object.
1256+
Called by :ref:`bytes <func-bytes>` to compute a byte-string representation
1257+
of an object. This should return a :class:`bytes` object.
12581258

12591259
.. index::
12601260
single: string; __format__() (object method)

0 commit comments

Comments
 (0)