]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.13] gh-137668: Document that ord() supports also bytes and bytearray (GH-137669...
authorSerhiy Storchaka <storchaka@gmail.com>
Wed, 13 Aug 2025 09:37:15 +0000 (12:37 +0300)
committerGitHub <noreply@github.com>
Wed, 13 Aug 2025 09:37:15 +0000 (09:37 +0000)
(cherry picked from commit 35759fe2faf1443455dfcb15ef7c435e34b492c7)

Doc/library/functions.rst
Python/bltinmodule.c
Python/clinic/bltinmodule.c.h

index 4acc24d352ac676f0ee6a64a8e033b5627d8187c..3bc941ef8fb3f272d7f4425f52ff68cdd48bd1a5 100644 (file)
@@ -1550,13 +1550,19 @@ are always available.  They are listed here in alphabetical order.
    .. versionchanged:: 3.11
       The ``'U'`` mode has been removed.
 
-.. function:: ord(c)
+.. function:: ord(character, /)
 
-   Given a string representing one Unicode character, return an integer
-   representing the Unicode code point of that character.  For example,
+   Return the ordinal value of a character.
+
+   If the argument is a one-character string, return the Unicode code point
+   of that character.  For example,
    ``ord('a')`` returns the integer ``97`` and ``ord('€')`` (Euro sign)
    returns ``8364``.  This is the inverse of :func:`chr`.
 
+   If the argument is a :class:`bytes` or :class:`bytearray` object of
+   length 1, return its single byte value.
+   For example, ``ord(b'a')`` returns the integer ``97``.
+
 
 .. function:: pow(base, exp, mod=None)
 
index cc93424664c69ce9822abb92b473b6ce58bcc3da..eaadf83667262e863593c764cf0c795f43a4cd00 100644 (file)
@@ -1969,15 +1969,21 @@ builtin_oct(PyObject *module, PyObject *number)
 /*[clinic input]
 ord as builtin_ord
 
-    c: object
+    character as c: object
     /
 
-Return the Unicode code point for a one-character string.
+Return the ordinal value of a character.
+
+If the argument is a one-character string, return the Unicode code
+point of that character.
+
+If the argument is a bytes or bytearray object of length 1, return its
+single byte value.
 [clinic start generated code]*/
 
 static PyObject *
 builtin_ord(PyObject *module, PyObject *c)
-/*[clinic end generated code: output=4fa5e87a323bae71 input=3064e5d6203ad012]*/
+/*[clinic end generated code: output=4fa5e87a323bae71 input=98d38480432e1177]*/
 {
     long ord;
     Py_ssize_t size;
index f75a8d4ac0ccd4b4b14e895ba5e971a215efce8a..2abdc6f3ec2ff6c32cc0d2093203453239c40847 100644 (file)
@@ -806,10 +806,16 @@ PyDoc_STRVAR(builtin_oct__doc__,
     {"oct", (PyCFunction)builtin_oct, METH_O, builtin_oct__doc__},
 
 PyDoc_STRVAR(builtin_ord__doc__,
-"ord($module, c, /)\n"
+"ord($module, character, /)\n"
 "--\n"
 "\n"
-"Return the Unicode code point for a one-character string.");
+"Return the ordinal value of a character.\n"
+"\n"
+"If the argument is a one-character string, return the Unicode code\n"
+"point of that character.\n"
+"\n"
+"If the argument is a bytes or bytearray object of length 1, return its\n"
+"single byte value.");
 
 #define BUILTIN_ORD_METHODDEF    \
     {"ord", (PyCFunction)builtin_ord, METH_O, builtin_ord__doc__},
@@ -1228,4 +1234,4 @@ builtin_issubclass(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
 exit:
     return return_value;
 }
-/*[clinic end generated code: output=435d3f286a863c49 input=a9049054013a1b77]*/
+/*[clinic end generated code: output=f686ad843368963c input=a9049054013a1b77]*/