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-112730: Make the test suite resilient to color-activation environment variables #117672

Merged
merged 8 commits into from
Apr 24, 2024

Conversation

pablogsal
Copy link
Member

@pablogsal pablogsal commented Apr 9, 2024

.github/workflows/reusable-ubuntu.yml Outdated Show resolved Hide resolved
Lib/test/support/__init__.py Outdated Show resolved Hide resolved
Lib/test/test_traceback.py Outdated Show resolved Hide resolved
Lib/test/support/__init__.py Outdated Show resolved Hide resolved
@hugovk hugovk changed the title gh-112730: Make the test suite relient to color-activation environment variables gh-112730: Make the test suite resilient to color-activation environment variables Apr 9, 2024
Lib/traceback.py Outdated Show resolved Hide resolved
Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
@gvanrossum
Copy link
Member

I am kind of sad that the solution is to add a special hack to suppress color to every test that would break. My ideal solution would be to suppress the color variables when -E is used, or for the traceback code to be able to detect whether -E is used.

@pablogsal
Copy link
Member Author

pablogsal commented Apr 9, 2024

I am kind of sad that the solution is to add a special hack to suppress color to every test that would break. My ideal solution would be to suppress the color variables when -E is used, or for the traceback code to be able to detect whether -E is used.

That only takes half of the tests because that fixes the cases where the traceback is taken from a subprocess. The other half which is when the traceback is taken from the process that runs test itself you need the decorator because the test suite cannot be executed itself always with -E.

To be honest I am happy to go this route (ignore on -E) but as @gpshead had some concerns with that I went with the most general solution first

@gvanrossum
Copy link
Member

Got it. I wonder if _can_colorize() is also a bit naive? Its default is to checking whether stderr is a tty, even when its caller is going to write to some other stream.

@hugovk
Copy link
Member

hugovk commented Apr 9, 2024

That came up for termcolor and we replaced:

    return (
        hasattr(sys.stdout, "isatty")
        and sys.stdout.isatty()
        and os.environ.get("TERM") != "dumb"
    )

With:

    # Then check system:
    if os.environ.get("TERM") == "dumb":
        return False
    if not hasattr(sys.stdout, "fileno"):
        return False

    try:
        return os.isatty(sys.stdout.fileno())
    except io.UnsupportedOperation:
        return sys.stdout.isatty()

https://github.com/termcolor/termcolor/pull/56/files

Lib/test/support/__init__.py Outdated Show resolved Hide resolved
Copy link
Member

@gpshead gpshead left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So long as the number of decorator applications isn't huge I think this works. We should pay attention to users during the alpha and beta cycle. I'm assuming they aren't likely to find issues with this in CI and they probably do not enable colors there. It feels more likely to happen to an end user developer working within their local dev environment (which I believe is how we found it amongst ourselves?)

Lib/test/test_traceback.py Outdated Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's enough decorators in this test file I suggest just disabling it via setUpModule / tearDownModule instead.

@gpshead
Copy link
Member

gpshead commented Apr 10, 2024

FWIW I do not mind if we just wind up disabling it entirely on -E, especially for the initial release of the feature. I agree that regardless of a PYTHON_ environment variable name prefix, it is a natural behavior change for people to look to -E and expect it to do.

@pablogsal
Copy link
Member Author

Ok, I will move to disable on -E then and remove as many decorators as possible.

pablogsal and others added 3 commits April 10, 2024 11:19
Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
@hugovk
Copy link
Member

hugovk commented Apr 14, 2024

This test is failing:

FAIL: test_colorized_detection_checks_for_environment_variables (test.test_traceback.TestColorizedTraceback.test_colorized_detection_checks_for_environment_variables)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "D:\a\cpython\cpython\Lib\test\test_traceback.py", line 4376, in test_colorized_detection_checks_for_environment_variables
    self.assertEqual(traceback._can_colorize(), False)
    ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError: True != False

Does it also need @force_not_colorized?

@pablogsal pablogsal force-pushed the no_colorize branch 2 times, most recently from 1526a99 to 4882250 Compare April 24, 2024 15:19
Signed-off-by: Pablo Galindo <pablogsal@gmail.com>
@pablogsal pablogsal merged commit 345e1e0 into python:main Apr 24, 2024
31 checks passed
@pablogsal pablogsal deleted the no_colorize branch April 24, 2024 20:25
@kulikjak
Copy link
Contributor

kulikjak commented Apr 25, 2024

Hi. With this change, I am seeking several failures like the following one in test_traceback and test_tracemalloc:

======================================================================
FAIL: test_env_var_invalid (test.test_tracemalloc.TestCommandLine.test_env_var_invalid) (nframe=-1)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/..../pythonmain/cpython-main/Lib/test/test_tracemalloc.py", line 959, in test_env_var_invalid
    self.check_env_var_invalid(nframe)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^
  File "/..../pythonmain/cpython-main/Lib/test/test_tracemalloc.py", line 953, in check_env_var_invalid
    self.fail(f"unexpected output: {stderr!a}")
    ~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError: unexpected output: b'ld.so.1: python: fatal: libpython3.13.so.1.0: open failed: No such file or directory\nld.so.1: python: fatal: relocation error: file /..../pythonmain/build/sparcv9/python: symbol Py_BytesMain: referenced symbol not found\n'

It seems that because the environment is now being cleared in those tests, LD_LIBRARY_PATH is no longer set in the subprocess, and libpython3.13.so.1.0 thus not found.

I am seeing this on Oracle Solaris, though atm I don't think this is specific to us? Also, I am executing the test suite in a pretty standard way:

cd /..../pythonmain/build/sparcv9; EXTRATESTOPTS="-v test_traceback test_tracemalloc" /usr/gnu/bin/make test

@pablogsal
Copy link
Member Author

I will look into this today!

@ericsnowcurrently
Copy link
Member

FWIW, I've seen failures in test_traceback/test_tracemalloc on a number of stable buildbots since this change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants