summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorBarry Warsaw <barry@python.org>2021-09-16 02:55:24 (GMT)
committerGitHub <noreply@github.com>2021-09-16 02:55:24 (GMT)
commit07e737d002cdbf0bfee53248a652a86c9f93f02b (patch)
treee7c371587a38da2e4e095e603d86fcaf8bc9b1a5 /Objects
parenta9757bf34d8b4cb3c24bbb70d50a06c815e2e8f3 (diff)
downloadcpython-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')
-rw-r--r--Objects/clinic/longobject.c.h94
-rw-r--r--Objects/longobject.c25
2 files changed, 74 insertions, 45 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]*/
diff --git a/Objects/longobject.c b/Objects/longobject.c
index 3b6df12..33fea64 100644
--- a/Objects/longobject.c
+++ b/Objects/longobject.c
@@ -5521,15 +5521,16 @@ int_as_integer_ratio_impl(PyObject *self)
/*[clinic input]
int.to_bytes
- length: Py_ssize_t
+ length: Py_ssize_t = 1
Length of bytes object to use. An OverflowError is raised if the
- integer is not representable with the given number of bytes.
- byteorder: unicode
+ integer is not representable with the given number of bytes. Default
+ is length 1.
+ byteorder: unicode(c_default="NULL") = "big"
The byte order used to represent the integer. If byteorder is 'big',
the most significant byte is at the beginning of the byte array. If
byteorder is 'little', the most significant byte is at the end of the
byte array. To request the native byte order of the host system, use
- `sys.byteorder' as the byte order value.
+ `sys.byteorder' as the byte order value. Default is to use 'big'.
*
signed as is_signed: bool = False
Determines whether two's complement is used to represent the integer.
@@ -5542,12 +5543,14 @@ Return an array of bytes representing an integer.
static PyObject *
int_to_bytes_impl(PyObject *self, Py_ssize_t length, PyObject *byteorder,
int is_signed)
-/*[clinic end generated code: output=89c801df114050a3 input=ddac63f4c7bf414c]*/
+/*[clinic end generated code: output=89c801df114050a3 input=d42ecfb545039d71]*/
{
int little_endian;
PyObject *bytes;
- if (_PyUnicode_EqualToASCIIId(byteorder, &PyId_little))
+ if (byteorder == NULL)
+ little_endian = 0;
+ else if (_PyUnicode_EqualToASCIIId(byteorder, &PyId_little))
little_endian = 1;
else if (_PyUnicode_EqualToASCIIId(byteorder, &PyId_big))
little_endian = 0;
@@ -5586,12 +5589,12 @@ int.from_bytes
support the buffer protocol or be an iterable object producing bytes.
Bytes and bytearray are examples of built-in objects that support the
buffer protocol.
- byteorder: unicode
+ byteorder: unicode(c_default="NULL") = "big"
The byte order used to represent the integer. If byteorder is 'big',
the most significant byte is at the beginning of the byte array. If
byteorder is 'little', the most significant byte is at the end of the
byte array. To request the native byte order of the host system, use
- `sys.byteorder' as the byte order value.
+ `sys.byteorder' as the byte order value. Default is to use 'big'.
*
signed as is_signed: bool = False
Indicates whether two's complement is used to represent the integer.
@@ -5602,12 +5605,14 @@ Return the integer represented by the given array of bytes.
static PyObject *
int_from_bytes_impl(PyTypeObject *type, PyObject *bytes_obj,
PyObject *byteorder, int is_signed)
-/*[clinic end generated code: output=efc5d68e31f9314f input=cdf98332b6a821b0]*/
+/*[clinic end generated code: output=efc5d68e31f9314f input=33326dccdd655553]*/
{
int little_endian;
PyObject *long_obj, *bytes;
- if (_PyUnicode_EqualToASCIIId(byteorder, &PyId_little))
+ if (byteorder == NULL)
+ little_endian = 0;
+ else if (_PyUnicode_EqualToASCIIId(byteorder, &PyId_little))
little_endian = 1;
else if (_PyUnicode_EqualToASCIIId(byteorder, &PyId_big))
little_endian = 0;