]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.14] gh-137668: Document that ord() supports also bytes and bytearray (GH-137669...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Mon, 8 Sep 2025 14:59:10 +0000 (16:59 +0200)
committerGitHub <noreply@github.com>
Mon, 8 Sep 2025 14:59:10 +0000 (17:59 +0300)
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Doc/library/functions.rst
Python/bltinmodule.c
Python/clinic/bltinmodule.c.h

index 249579d8414b0a35fc9c8eee15c9e668658977f4..33840c3414fd445d1ce4ada252f6220949d3a5bc 100644 (file)
@@ -1559,13 +1559,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 09b6c97538388365770c19c86c261cab9dad8e73..1ca23626ea52fd3bd2e8b1e86821d8bcf154034f 100644 (file)
@@ -2119,15 +2119,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 c826a5724f769cd5ce069a96d502bca33d074a47..c1fcbbba8e2f5786efc214d34327c06ae8bd8576 100644 (file)
@@ -831,10 +831,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__},
@@ -1268,4 +1274,4 @@ builtin_issubclass(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
 exit:
     return return_value;
 }
-/*[clinic end generated code: output=e7a5d0851d7f2cfb input=a9049054013a1b77]*/
+/*[clinic end generated code: output=cd5f80e3dc3082d5 input=a9049054013a1b77]*/