Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 843cfc3

Browse files
bpo-15999: Clean up of handling boolean arguments.
* Use the 'p' format unit instead of manually called PyObject_IsTrue(). * Pass boolean value instead 0/1 integers to functions that needs boolean. * Convert some arguments to boolean only once.
1 parent 6a650aa commit 843cfc3

File tree

8 files changed

+23
-27
lines changed

8 files changed

+23
-27
lines changed

Modules/_io/_iomodule.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -383,8 +383,10 @@ _io_open_impl(PyObject *module, PyObject *file, const char *mode,
383383
encoding = "utf-8";
384384
}
385385
#endif
386-
raw = PyObject_CallFunction(RawIO_class,
387-
"OsiO", path_or_fd, rawmode, closefd, opener);
386+
raw = PyObject_CallFunction(RawIO_class, "OsOO",
387+
path_or_fd, rawmode,
388+
closefd ? Py_True : Py_False,
389+
opener);
388390
}
389391

390392
if (raw == NULL)
@@ -476,10 +478,10 @@ _io_open_impl(PyObject *module, PyObject *file, const char *mode,
476478

477479
/* wraps into a TextIOWrapper */
478480
wrapper = PyObject_CallFunction((PyObject *)&PyTextIOWrapper_Type,
479-
"Osssi",
481+
"OsssO",
480482
buffer,
481483
encoding, errors, newline,
482-
line_buffering);
484+
line_buffering ? Py_True : Py_False);
483485
if (wrapper == NULL)
484486
goto error;
485487
result = wrapper;

Modules/_io/stringio.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -716,7 +716,7 @@ _io_StringIO___init___impl(stringio *self, PyObject *value,
716716
if (self->readuniversal) {
717717
self->decoder = PyObject_CallFunction(
718718
(PyObject *)&PyIncrementalNewlineDecoder_Type,
719-
"Oi", Py_None, (int) self->readtranslate);
719+
"OO", Py_None, self->readtranslate ? Py_True : Py_False);
720720
if (self->decoder == NULL)
721721
return -1;
722722
}

Modules/_io/textio.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -882,7 +882,7 @@ _textiowrapper_set_decoder(textio *self, PyObject *codec_info,
882882
if (self->readuniversal) {
883883
PyObject *incrementalDecoder = PyObject_CallFunction(
884884
(PyObject *)&PyIncrementalNewlineDecoder_Type,
885-
"Oi", self->decoder, (int)self->readtranslate);
885+
"OO", self->decoder, self->readtranslate ? Py_True : Py_False);
886886
if (incrementalDecoder == NULL)
887887
return -1;
888888
Py_CLEAR(self->decoder);

Modules/itertoolsmodule.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,10 +1059,10 @@ cycle_reduce(cycleobject *lz, PyObject *Py_UNUSED(ignored))
10591059
}
10601060
Py_DECREF(res);
10611061
}
1062-
return Py_BuildValue("O(N)(Oi)", Py_TYPE(lz), it, lz->saved, 1);
1062+
return Py_BuildValue("O(N)(OO)", Py_TYPE(lz), it, lz->saved, Py_True);
10631063
}
1064-
return Py_BuildValue("O(O)(Oi)", Py_TYPE(lz), lz->it, lz->saved,
1065-
lz->firstpass);
1064+
return Py_BuildValue("O(O)(OO)", Py_TYPE(lz), lz->it, lz->saved,
1065+
lz->firstpass ? Py_True : Py_False);
10661066
}
10671067

10681068
static PyObject *

Modules/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ pymain_run_module(const wchar_t *modname, int set_argv0)
292292
Py_DECREF(runmodule);
293293
return pymain_exit_err_print();
294294
}
295-
runargs = Py_BuildValue("(Oi)", module, set_argv0);
295+
runargs = Py_BuildValue("(OO)", module, set_argv0 ? Py_True : Py_False);
296296
if (runargs == NULL) {
297297
fprintf(stderr,
298298
"Could not create arguments for runpy._run_module_as_main\n");

Objects/fileobject.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ PyFile_FromFd(int fd, const char *name, const char *mode, int buffering, const c
3838
io = PyImport_ImportModule("_io");
3939
if (io == NULL)
4040
return NULL;
41-
stream = _PyObject_CallMethodId(io, &PyId_open, "isisssi", fd, mode,
41+
stream = _PyObject_CallMethodId(io, &PyId_open, "isisssO", fd, mode,
4242
buffering, encoding, errors,
43-
newline, closefd);
43+
newline, closefd ? Py_True : Py_False);
4444
Py_DECREF(io);
4545
if (stream == NULL)
4646
return NULL;

Python/bltinmodule.c

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1820,8 +1820,9 @@ static PyObject *
18201820
builtin_print(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
18211821
{
18221822
static const char * const _keywords[] = {"sep", "end", "file", "flush", 0};
1823-
static struct _PyArg_Parser _parser = {"|OOOO:print", _keywords, 0};
1824-
PyObject *sep = NULL, *end = NULL, *file = NULL, *flush = NULL;
1823+
static struct _PyArg_Parser _parser = {"|OOOp:print", _keywords, 0};
1824+
PyObject *sep = NULL, *end = NULL, *file = NULL;
1825+
int flush = 0;
18251826
int i, err;
18261827

18271828
if (kwnames != NULL &&
@@ -1883,18 +1884,11 @@ builtin_print(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject
18831884
if (err)
18841885
return NULL;
18851886

1886-
if (flush != NULL) {
1887-
PyObject *tmp;
1888-
int do_flush = PyObject_IsTrue(flush);
1889-
if (do_flush == -1)
1887+
if (flush) {
1888+
PyObject *tmp = _PyObject_CallMethodIdNoArgs(file, &PyId_flush);
1889+
if (tmp == NULL)
18901890
return NULL;
1891-
else if (do_flush) {
1892-
tmp = _PyObject_CallMethodIdNoArgs(file, &PyId_flush);
1893-
if (tmp == NULL)
1894-
return NULL;
1895-
else
1896-
Py_DECREF(tmp);
1897-
}
1891+
Py_DECREF(tmp);
18981892
}
18991893

19001894
Py_RETURN_NONE;

Python/pylifecycle.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1735,10 +1735,10 @@ create_stdio(const PyConfig *config, PyObject* io,
17351735
mode = "wb";
17361736
else
17371737
mode = "rb";
1738-
buf = _PyObject_CallMethodId(io, &PyId_open, "isiOOOi",
1738+
buf = _PyObject_CallMethodId(io, &PyId_open, "isiOOOO",
17391739
fd, mode, buffering,
17401740
Py_None, Py_None, /* encoding, errors */
1741-
Py_None, 0); /* newline, closefd */
1741+
Py_None, Py_False); /* newline, closefd */
17421742
if (buf == NULL)
17431743
goto error;
17441744

0 commit comments

Comments
 (0)