Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-77782: Py_FdIsInteractive() now uses PyConfig.interactive #93916

Merged
merged 2 commits into from
Jun 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Doc/c-api/sys.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ Operating System Utilities

Return true (nonzero) if the standard I/O file *fp* with name *filename* is
deemed interactive. This is the case for files for which ``isatty(fileno(fp))``
is true. If the global flag :c:data:`Py_InteractiveFlag` is true, this function
is true. If the :c:member:`PyConfig.interactive` is non-zero, this function
also returns true if the *filename* pointer is ``NULL`` or if the name is equal to
one of the strings ``'<stdin>'`` or ``'???'``.

This function must not be called before Python is initialized.


.. c:function:: void PyOS_BeforeFork()

Expand Down
17 changes: 9 additions & 8 deletions Lib/test/test_capi.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,17 @@ def test_no_FatalError_infinite_loop(self):
'import _testcapi;'
'_testcapi.crash_no_current_thread()'],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
stderr=subprocess.PIPE,
text=True)
(out, err) = p.communicate()
self.assertEqual(out, b'')
self.assertEqual(out, '')
# This used to cause an infinite loop.
self.assertTrue(err.rstrip().startswith(
b'Fatal Python error: '
b'PyThreadState_Get: '
b'the function must be called with the GIL held, '
b'but the GIL is released '
b'(the current Python thread state is NULL)'),
msg = ("Fatal Python error: PyThreadState_Get: "
"the function must be called with the GIL held, "
"after Python initialization and before Python finalization, "
"but the GIL is released "
"(the current Python thread state is NULL)")
self.assertTrue(err.rstrip().startswith(msg),
err)

def test_memoryview_from_NULL_pointer(self):
Expand Down
6 changes: 3 additions & 3 deletions Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -347,9 +347,9 @@ void _Py_NO_RETURN
_Py_FatalError_TstateNULL(const char *func)
{
_Py_FatalErrorFunc(func,
"the function must be called with the GIL held, "
"but the GIL is released "
"(the current Python thread state is NULL)");
"the function must be called with the GIL held, "
"after Python initialization and before Python finalization, "
"but the GIL is released (the current Python thread state is NULL)");
}

int
Expand Down
2 changes: 1 addition & 1 deletion Python/initconfig.c
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ int Py_UTF8Mode = 0;
int Py_DebugFlag = 0; /* Needed by parser.c */
int Py_VerboseFlag = 0; /* Needed by import.c */
int Py_QuietFlag = 0; /* Needed by sysmodule.c */
int Py_InteractiveFlag = 0; /* Needed by Py_FdIsInteractive() below */
int Py_InteractiveFlag = 0; /* Previously, was used by Py_FdIsInteractive() */
int Py_InspectFlag = 0; /* Needed to determine whether to exit at SystemExit */
int Py_OptimizeFlag = 0; /* Needed by compile.c */
int Py_NoSiteFlag = 0; /* Suppress 'import site' */
Expand Down
22 changes: 12 additions & 10 deletions Python/pylifecycle.c
Original file line number Diff line number Diff line change
Expand Up @@ -2938,28 +2938,30 @@ Py_Exit(int sts)
int
Py_FdIsInteractive(FILE *fp, const char *filename)
{
if (isatty((int)fileno(fp)))
if (isatty(fileno(fp))) {
return 1;
if (!Py_InteractiveFlag)
}
if (!_Py_GetConfig()->interactive) {
return 0;
return (filename == NULL) ||
(strcmp(filename, "<stdin>") == 0) ||
(strcmp(filename, "???") == 0);
}
return ((filename == NULL)
|| (strcmp(filename, "<stdin>") == 0)
|| (strcmp(filename, "???") == 0));
}


int
_Py_FdIsInteractive(FILE *fp, PyObject *filename)
{
if (isatty((int)fileno(fp))) {
if (isatty(fileno(fp))) {
return 1;
}
if (!Py_InteractiveFlag) {
if (!_Py_GetConfig()->interactive) {
return 0;
}
return (filename == NULL) ||
(PyUnicode_CompareWithASCIIString(filename, "<stdin>") == 0) ||
(PyUnicode_CompareWithASCIIString(filename, "???") == 0);
return ((filename == NULL)
|| (PyUnicode_CompareWithASCIIString(filename, "<stdin>") == 0)
|| (PyUnicode_CompareWithASCIIString(filename, "???") == 0));
}


Expand Down
1 change: 1 addition & 0 deletions Python/pystate.c
Original file line number Diff line number Diff line change
Expand Up @@ -2136,6 +2136,7 @@ _Py_GetConfig(void)
{
assert(PyGILState_Check());
PyThreadState *tstate = _PyThreadState_GET();
_Py_EnsureTstateNotNULL(tstate);
return _PyInterpreterState_GetConfig(tstate->interp);
}

Expand Down