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

Documentation for PYTHONUNBUFFERED is not correct #128392

Open
WolframAlph opened this issue Jan 1, 2025 · 4 comments
Open

Documentation for PYTHONUNBUFFERED is not correct #128392

WolframAlph opened this issue Jan 1, 2025 · 4 comments
Labels
docs Documentation in the Doc dir

Comments

@WolframAlph
Copy link
Contributor

WolframAlph commented Jan 1, 2025

Documentation

Follow up on: #128377

According to docs, PYTHONUNBUFFERED is true if set as a non-empty string. Setting it to "0" (which is non-empty string) has effect of disabling it though.
Related C code:

cpython/Python/initconfig.c

Lines 1786 to 1790 in d903b17

int unbuffered_stdio = 0;
_Py_get_env_flag(use_env, &unbuffered_stdio, "PYTHONUNBUFFERED");
if (unbuffered_stdio) {
config->buffered_stdio = 0;
}

cpython/Python/preconfig.c

Lines 563 to 578 in d903b17

void
_Py_get_env_flag(int use_environment, int *flag, const char *name)
{
const char *var = _Py_GetEnv(use_environment, name);
if (!var) {
return;
}
int value;
if (_Py_str_to_int(var, &value) < 0 || value < 0) {
/* PYTHONDEBUG=text and PYTHONDEBUG=-2 behave as PYTHONDEBUG=1 */
value = 1;
}
if (*flag < value) {
*flag = value;
}
}

cpython/Python/preconfig.c

Lines 545 to 560 in d903b17

int
_Py_str_to_int(const char *str, int *result)
{
const char *endptr = str;
errno = 0;
long value = strtol(str, (char **)&endptr, 10);
if (*endptr != '\0' || errno == ERANGE) {
return -1;
}
if (value < INT_MIN || value > INT_MAX) {
return -1;
}
*result = (int)value;
return 0;
}

We should probably rephrase it from:
If this is set to a non-empty string...
to:
If this is set...

Linked PRs

@serhiy-storchaka
Copy link
Member

It seems that there is a discrepancy between the code and the documentation for several other environmental variables too. We should investigate when it was introduced and whether it was intentional. And document the code change if it was intentional.

In any case the proposed documentation change does not look correct to me.

@WolframAlph
Copy link
Contributor Author

It seems that there is a discrepancy between the code and the documentation for several other environmental variables too. We should investigate when it was introduced and whether it was intentional. And document the code change if it was intentional.

I will look into that.

In any case the proposed documentation change does not look correct to me.

Do you suggest that If this is set does not explicitly explain how Python reads and converts those env variables?

@serhiy-storchaka
Copy link
Member

"If this is set" means that the variable is set to any value, including empty string and "0".

@WolframAlph
Copy link
Contributor Author

WolframAlph commented Jan 2, 2025

I see. The reason behind this wording is actually following other env var descriptions. For instance PYTHONNOUSERSITE which is extracted with the same _Py_get_env_flag. I will go over all other env vars and investigate this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir
Projects
Status: Todo
Development

No branches or pull requests

2 participants