diff options
author | Barry Warsaw <barry@python.org> | 2021-09-16 02:55:24 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-09-16 02:55:24 (GMT) |
commit | 07e737d002cdbf0bfee53248a652a86c9f93f02b (patch) | |
tree | e7c371587a38da2e4e095e603d86fcaf8bc9b1a5 /Objects/clinic | |
parent | a9757bf34d8b4cb3c24bbb70d50a06c815e2e8f3 (diff) | |
download | cpython-07e737d002cdbf0bfee53248a652a86c9f93f02b.zip cpython-07e737d002cdbf0bfee53248a652a86c9f93f02b.tar.gz cpython-07e737d002cdbf0bfee53248a652a86c9f93f02b.tar.bz2 |
bpo-45155 : Default arguments for int.to_bytes(length=1, byteorder=sys.byteorder) (#28265)
Add default arguments for int.to_bytes() and int.from_bytes()
Co-authored-by: Brandt Bucher <brandtbucher@gmail.com>
Diffstat (limited to 'Objects/clinic')
-rw-r--r-- | Objects/clinic/longobject.c.h | 94 |
1 files changed, 59 insertions, 35 deletions
diff --git a/Objects/clinic/longobject.c.h b/Objects/clinic/longobject.c.h index 4bd47b1..d50c4af 100644 --- a/Objects/clinic/longobject.c.h +++ b/Objects/clinic/longobject.c.h @@ -226,20 +226,21 @@ int_as_integer_ratio(PyObject *self, PyObject *Py_UNUSED(ignored)) } PyDoc_STRVAR(int_to_bytes__doc__, -"to_bytes($self, /, length, byteorder, *, signed=False)\n" +"to_bytes($self, /, length=1, byteorder=\'big\', *, signed=False)\n" "--\n" "\n" "Return an array of bytes representing an integer.\n" "\n" " length\n" " Length of bytes object to use. An OverflowError is raised if the\n" -" integer is not representable with the given number of bytes.\n" +" integer is not representable with the given number of bytes. Default\n" +" is length 1.\n" " byteorder\n" " The byte order used to represent the integer. If byteorder is \'big\',\n" " the most significant byte is at the beginning of the byte array. If\n" " byteorder is \'little\', the most significant byte is at the end of the\n" " byte array. To request the native byte order of the host system, use\n" -" `sys.byteorder\' as the byte order value.\n" +" `sys.byteorder\' as the byte order value. Default is to use \'big\'.\n" " signed\n" " Determines whether two\'s complement is used to represent the integer.\n" " If signed is False and a negative integer is given, an OverflowError\n" @@ -259,35 +260,49 @@ int_to_bytes(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject * static const char * const _keywords[] = {"length", "byteorder", "signed", NULL}; static _PyArg_Parser _parser = {NULL, _keywords, "to_bytes", 0}; PyObject *argsbuf[3]; - Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 2; - Py_ssize_t length; - PyObject *byteorder; + Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0; + Py_ssize_t length = 1; + PyObject *byteorder = NULL; int is_signed = 0; - args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 2, 0, argsbuf); + args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 2, 0, argsbuf); if (!args) { goto exit; } - { - Py_ssize_t ival = -1; - PyObject *iobj = _PyNumber_Index(args[0]); - if (iobj != NULL) { - ival = PyLong_AsSsize_t(iobj); - Py_DECREF(iobj); + if (!noptargs) { + goto skip_optional_pos; + } + if (args[0]) { + { + Py_ssize_t ival = -1; + PyObject *iobj = _PyNumber_Index(args[0]); + if (iobj != NULL) { + ival = PyLong_AsSsize_t(iobj); + Py_DECREF(iobj); + } + if (ival == -1 && PyErr_Occurred()) { + goto exit; + } + length = ival; } - if (ival == -1 && PyErr_Occurred()) { - goto exit; + if (!--noptargs) { + goto skip_optional_pos; } - length = ival; - } - if (!PyUnicode_Check(args[1])) { - _PyArg_BadArgument("to_bytes", "argument 'byteorder'", "str", args[1]); - goto exit; } - if (PyUnicode_READY(args[1]) == -1) { - goto exit; + if (args[1]) { + if (!PyUnicode_Check(args[1])) { + _PyArg_BadArgument("to_bytes", "argument 'byteorder'", "str", args[1]); + goto exit; + } + if (PyUnicode_READY(args[1]) == -1) { + goto exit; + } + byteorder = args[1]; + if (!--noptargs) { + goto skip_optional_pos; + } } - byteorder = args[1]; +skip_optional_pos: if (!noptargs) { goto skip_optional_kwonly; } @@ -303,7 +318,7 @@ exit: } PyDoc_STRVAR(int_from_bytes__doc__, -"from_bytes($type, /, bytes, byteorder, *, signed=False)\n" +"from_bytes($type, /, bytes, byteorder=\'big\', *, signed=False)\n" "--\n" "\n" "Return the integer represented by the given array of bytes.\n" @@ -318,7 +333,7 @@ PyDoc_STRVAR(int_from_bytes__doc__, " the most significant byte is at the beginning of the byte array. If\n" " byteorder is \'little\', the most significant byte is at the end of the\n" " byte array. To request the native byte order of the host system, use\n" -" `sys.byteorder\' as the byte order value.\n" +" `sys.byteorder\' as the byte order value. Default is to use \'big\'.\n" " signed\n" " Indicates whether two\'s complement is used to represent the integer."); @@ -336,24 +351,33 @@ int_from_bytes(PyTypeObject *type, PyObject *const *args, Py_ssize_t nargs, PyOb static const char * const _keywords[] = {"bytes", "byteorder", "signed", NULL}; static _PyArg_Parser _parser = {NULL, _keywords, "from_bytes", 0}; PyObject *argsbuf[3]; - Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 2; + Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1; PyObject *bytes_obj; - PyObject *byteorder; + PyObject *byteorder = NULL; int is_signed = 0; - args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 2, 0, argsbuf); + args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 2, 0, argsbuf); if (!args) { goto exit; } bytes_obj = args[0]; - if (!PyUnicode_Check(args[1])) { - _PyArg_BadArgument("from_bytes", "argument 'byteorder'", "str", args[1]); - goto exit; + if (!noptargs) { + goto skip_optional_pos; } - if (PyUnicode_READY(args[1]) == -1) { - goto exit; + if (args[1]) { + if (!PyUnicode_Check(args[1])) { + _PyArg_BadArgument("from_bytes", "argument 'byteorder'", "str", args[1]); + goto exit; + } + if (PyUnicode_READY(args[1]) == -1) { + goto exit; + } + byteorder = args[1]; + if (!--noptargs) { + goto skip_optional_pos; + } } - byteorder = args[1]; +skip_optional_pos: if (!noptargs) { goto skip_optional_kwonly; } @@ -367,4 +391,4 @@ skip_optional_kwonly: exit: return return_value; } -/*[clinic end generated code: output=ea18e51af5b53591 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=16a375d93769b227 input=a9049054013a1b77]*/ |