Skip to content

Commit

Permalink
bpo-15999: Accept arbitrary values for boolean parameters. (#15609)
Browse files Browse the repository at this point in the history
builtins and extension module functions and methods that expect boolean values for parameters now accept any Python object rather than just a bool or int type. This is more consistent with how native Python code itself behaves.
  • Loading branch information
serhiy-storchaka authored Dec 3, 2022
1 parent c68573b commit a87c46e
Show file tree
Hide file tree
Showing 52 changed files with 297 additions and 302 deletions.
4 changes: 4 additions & 0 deletions Doc/whatsnew/3.12.rst
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,10 @@ Other Language Changes
In a future Python version they will be eventually a :exc:`SyntaxError`.
(Contributed by Victor Stinner in :gh:`98401`.)

* All builtin and extension callables expecting boolean parameters now accept
arguments of any type instead of just :class:`bool` and :class:`int`.
(Contributed by Serhiy Storchaka in :gh:`60203`.)


New Modules
===========
Expand Down
4 changes: 0 additions & 4 deletions Lib/test/test_itertools.py
Original file line number Diff line number Diff line change
Expand Up @@ -739,10 +739,6 @@ def test_cycle_setstate(self):
c.__setstate__((tuple('defg'), 0))
take(20, c)

# The second argument in the setstate tuple must be an int
with self.assertRaises(TypeError):
cycle('defg').__setstate__((list('abcdefg'), 'x'))

self.assertRaises(TypeError, cycle('').__setstate__, ())
self.assertRaises(TypeError, cycle('').__setstate__, ([],))

Expand Down
6 changes: 0 additions & 6 deletions Lib/test/test_posix.py
Original file line number Diff line number Diff line change
Expand Up @@ -1646,12 +1646,6 @@ def test_resetids(self):
)
support.wait_process(pid, exitcode=0)

def test_resetids_wrong_type(self):
with self.assertRaises(TypeError):
self.spawn_func(sys.executable,
[sys.executable, "-c", "pass"],
os.environ, resetids=None)

def test_setpgroup(self):
pid = self.spawn_func(
sys.executable,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
All built-in functions now accept arguments of any type instead of just
``bool`` and ``int`` for boolean parameters.
60 changes: 30 additions & 30 deletions Modules/_codecsmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -256,14 +256,14 @@ _codecs_escape_encode_impl(PyObject *module, PyObject *data,
_codecs.utf_7_decode
data: Py_buffer
errors: str(accept={str, NoneType}) = None
final: bool(accept={int}) = False
final: bool = False
/
[clinic start generated code]*/

static PyObject *
_codecs_utf_7_decode_impl(PyObject *module, Py_buffer *data,
const char *errors, int final)
/*[clinic end generated code: output=0cd3a944a32a4089 input=22c395d357815d26]*/
/*[clinic end generated code: output=0cd3a944a32a4089 input=dbf8c8998102dc7d]*/
{
Py_ssize_t consumed = data->len;
PyObject *decoded = PyUnicode_DecodeUTF7Stateful(data->buf, data->len,
Expand All @@ -276,14 +276,14 @@ _codecs_utf_7_decode_impl(PyObject *module, Py_buffer *data,
_codecs.utf_8_decode
data: Py_buffer
errors: str(accept={str, NoneType}) = None
final: bool(accept={int}) = False
final: bool = False
/
[clinic start generated code]*/

static PyObject *
_codecs_utf_8_decode_impl(PyObject *module, Py_buffer *data,
const char *errors, int final)
/*[clinic end generated code: output=10f74dec8d9bb8bf input=f611b3867352ba59]*/
/*[clinic end generated code: output=10f74dec8d9bb8bf input=ca06bc8a9c970e25]*/
{
Py_ssize_t consumed = data->len;
PyObject *decoded = PyUnicode_DecodeUTF8Stateful(data->buf, data->len,
Expand All @@ -296,14 +296,14 @@ _codecs_utf_8_decode_impl(PyObject *module, Py_buffer *data,
_codecs.utf_16_decode
data: Py_buffer
errors: str(accept={str, NoneType}) = None
final: bool(accept={int}) = False
final: bool = False
/
[clinic start generated code]*/

static PyObject *
_codecs_utf_16_decode_impl(PyObject *module, Py_buffer *data,
const char *errors, int final)
/*[clinic end generated code: output=783b442abcbcc2d0 input=191d360bd7309180]*/
/*[clinic end generated code: output=783b442abcbcc2d0 input=5b0f52071ba6cadc]*/
{
int byteorder = 0;
/* This is overwritten unless final is true. */
Expand All @@ -318,14 +318,14 @@ _codecs_utf_16_decode_impl(PyObject *module, Py_buffer *data,
_codecs.utf_16_le_decode
data: Py_buffer
errors: str(accept={str, NoneType}) = None
final: bool(accept={int}) = False
final: bool = False
/
[clinic start generated code]*/

static PyObject *
_codecs_utf_16_le_decode_impl(PyObject *module, Py_buffer *data,
const char *errors, int final)
/*[clinic end generated code: output=899b9e6364379dcd input=c6904fdc27fb4724]*/
/*[clinic end generated code: output=899b9e6364379dcd input=115bd8c7b783d0bf]*/
{
int byteorder = -1;
/* This is overwritten unless final is true. */
Expand All @@ -340,14 +340,14 @@ _codecs_utf_16_le_decode_impl(PyObject *module, Py_buffer *data,
_codecs.utf_16_be_decode
data: Py_buffer
errors: str(accept={str, NoneType}) = None
final: bool(accept={int}) = False
final: bool = False
/
[clinic start generated code]*/

static PyObject *
_codecs_utf_16_be_decode_impl(PyObject *module, Py_buffer *data,
const char *errors, int final)
/*[clinic end generated code: output=49f6465ea07669c8 input=e49012400974649b]*/
/*[clinic end generated code: output=49f6465ea07669c8 input=63131422b01f9cb4]*/
{
int byteorder = 1;
/* This is overwritten unless final is true. */
Expand All @@ -370,14 +370,14 @@ _codecs.utf_16_ex_decode
data: Py_buffer
errors: str(accept={str, NoneType}) = None
byteorder: int = 0
final: bool(accept={int}) = False
final: bool = False
/
[clinic start generated code]*/

static PyObject *
_codecs_utf_16_ex_decode_impl(PyObject *module, Py_buffer *data,
const char *errors, int byteorder, int final)
/*[clinic end generated code: output=0f385f251ecc1988 input=5a9c19f2e6b6cf0e]*/
/*[clinic end generated code: output=0f385f251ecc1988 input=f368a51cf384bf4c]*/
{
/* This is overwritten unless final is true. */
Py_ssize_t consumed = data->len;
Expand All @@ -394,14 +394,14 @@ _codecs_utf_16_ex_decode_impl(PyObject *module, Py_buffer *data,
_codecs.utf_32_decode
data: Py_buffer
errors: str(accept={str, NoneType}) = None
final: bool(accept={int}) = False
final: bool = False
/
[clinic start generated code]*/

static PyObject *
_codecs_utf_32_decode_impl(PyObject *module, Py_buffer *data,
const char *errors, int final)
/*[clinic end generated code: output=2fc961807f7b145f input=fd7193965627eb58]*/
/*[clinic end generated code: output=2fc961807f7b145f input=fcdf3658c5e9b5f3]*/
{
int byteorder = 0;
/* This is overwritten unless final is true. */
Expand All @@ -416,14 +416,14 @@ _codecs_utf_32_decode_impl(PyObject *module, Py_buffer *data,
_codecs.utf_32_le_decode
data: Py_buffer
errors: str(accept={str, NoneType}) = None
final: bool(accept={int}) = False
final: bool = False
/
[clinic start generated code]*/

static PyObject *
_codecs_utf_32_le_decode_impl(PyObject *module, Py_buffer *data,
const char *errors, int final)
/*[clinic end generated code: output=ec8f46b67a94f3e6 input=9078ec70acfe7613]*/
/*[clinic end generated code: output=ec8f46b67a94f3e6 input=12220556e885f817]*/
{
int byteorder = -1;
/* This is overwritten unless final is true. */
Expand All @@ -438,14 +438,14 @@ _codecs_utf_32_le_decode_impl(PyObject *module, Py_buffer *data,
_codecs.utf_32_be_decode
data: Py_buffer
errors: str(accept={str, NoneType}) = None
final: bool(accept={int}) = False
final: bool = False
/
[clinic start generated code]*/

static PyObject *
_codecs_utf_32_be_decode_impl(PyObject *module, Py_buffer *data,
const char *errors, int final)
/*[clinic end generated code: output=ff82bae862c92c4e input=f1ae1bbbb86648ff]*/
/*[clinic end generated code: output=ff82bae862c92c4e input=2bc669b4781598db]*/
{
int byteorder = 1;
/* This is overwritten unless final is true. */
Expand All @@ -468,14 +468,14 @@ _codecs.utf_32_ex_decode
data: Py_buffer
errors: str(accept={str, NoneType}) = None
byteorder: int = 0
final: bool(accept={int}) = False
final: bool = False
/
[clinic start generated code]*/

static PyObject *
_codecs_utf_32_ex_decode_impl(PyObject *module, Py_buffer *data,
const char *errors, int byteorder, int final)
/*[clinic end generated code: output=6bfb177dceaf4848 input=e46a73bc859d0bd0]*/
/*[clinic end generated code: output=6bfb177dceaf4848 input=4a2323d0013620df]*/
{
Py_ssize_t consumed = data->len;
PyObject *decoded = PyUnicode_DecodeUTF32Stateful(data->buf, data->len,
Expand All @@ -490,14 +490,14 @@ _codecs_utf_32_ex_decode_impl(PyObject *module, Py_buffer *data,
_codecs.unicode_escape_decode
data: Py_buffer(accept={str, buffer})
errors: str(accept={str, NoneType}) = None
final: bool(accept={int}) = True
final: bool = True
/
[clinic start generated code]*/

static PyObject *
_codecs_unicode_escape_decode_impl(PyObject *module, Py_buffer *data,
const char *errors, int final)
/*[clinic end generated code: output=b284f97b12c635ee input=6154f039a9f7c639]*/
/*[clinic end generated code: output=b284f97b12c635ee input=15019f081ffe272b]*/
{
Py_ssize_t consumed = data->len;
PyObject *decoded = _PyUnicode_DecodeUnicodeEscapeStateful(data->buf, data->len,
Expand All @@ -510,14 +510,14 @@ _codecs_unicode_escape_decode_impl(PyObject *module, Py_buffer *data,
_codecs.raw_unicode_escape_decode
data: Py_buffer(accept={str, buffer})
errors: str(accept={str, NoneType}) = None
final: bool(accept={int}) = True
final: bool = True
/
[clinic start generated code]*/

static PyObject *
_codecs_raw_unicode_escape_decode_impl(PyObject *module, Py_buffer *data,
const char *errors, int final)
/*[clinic end generated code: output=11dbd96301e2879e input=2d166191beb3235a]*/
/*[clinic end generated code: output=11dbd96301e2879e input=b93f823aa8c343ad]*/
{
Py_ssize_t consumed = data->len;
PyObject *decoded = _PyUnicode_DecodeRawUnicodeEscapeStateful(data->buf, data->len,
Expand Down Expand Up @@ -586,14 +586,14 @@ _codecs_charmap_decode_impl(PyObject *module, Py_buffer *data,
_codecs.mbcs_decode
data: Py_buffer
errors: str(accept={str, NoneType}) = None
final: bool(accept={int}) = False
final: bool = False
/
[clinic start generated code]*/

static PyObject *
_codecs_mbcs_decode_impl(PyObject *module, Py_buffer *data,
const char *errors, int final)
/*[clinic end generated code: output=39b65b8598938c4b input=1c1d50f08fa53789]*/
/*[clinic end generated code: output=39b65b8598938c4b input=f144ad1ed6d8f5a6]*/
{
Py_ssize_t consumed = data->len;
PyObject *decoded = PyUnicode_DecodeMBCSStateful(data->buf, data->len,
Expand All @@ -605,14 +605,14 @@ _codecs_mbcs_decode_impl(PyObject *module, Py_buffer *data,
_codecs.oem_decode
data: Py_buffer
errors: str(accept={str, NoneType}) = None
final: bool(accept={int}) = False
final: bool = False
/
[clinic start generated code]*/

static PyObject *
_codecs_oem_decode_impl(PyObject *module, Py_buffer *data,
const char *errors, int final)
/*[clinic end generated code: output=da1617612f3fcad8 input=81b67cba811022e5]*/
/*[clinic end generated code: output=da1617612f3fcad8 input=629bf87376d211b4]*/
{
Py_ssize_t consumed = data->len;
PyObject *decoded = PyUnicode_DecodeCodePageStateful(CP_OEMCP,
Expand All @@ -625,14 +625,14 @@ _codecs.code_page_decode
codepage: int
data: Py_buffer
errors: str(accept={str, NoneType}) = None
final: bool(accept={int}) = False
final: bool = False
/
[clinic start generated code]*/

static PyObject *
_codecs_code_page_decode_impl(PyObject *module, int codepage,
Py_buffer *data, const char *errors, int final)
/*[clinic end generated code: output=53008ea967da3fff input=c5f58d036cb63575]*/
/*[clinic end generated code: output=53008ea967da3fff input=6a32589b0658c277]*/
{
Py_ssize_t consumed = data->len;
PyObject *decoded = PyUnicode_DecodeCodePageStateful(codepage,
Expand Down
Loading

0 comments on commit a87c46e

Please sign in to comment.