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

Some test modules fail when run as a script #104057

Closed
Eclips4 opened this issue May 1, 2023 · 15 comments · Fixed by #104059
Closed

Some test modules fail when run as a script #104057

Eclips4 opened this issue May 1, 2023 · 15 comments · Fixed by #104059
Labels
tests Tests in the Lib/test dir type-bug An unexpected behavior, bug, or error

Comments

@Eclips4
Copy link
Member

Eclips4 commented May 1, 2023

 C:\Users\KIRILL-1\CLionProjects\cpython> ./python Lib/test/test_module.py
Running Debug|x64 interpreter...
.....................F..............
======================================================================
FAIL: test_module_repr_with_full_loader (__main__.ModuleTests.test_module_repr_with_full_loader)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Users\KIRILL-1\CLionProjects\cpython\Lib\test\test_module.py", line 238, in test_module_repr_with_full_loader
    self.assertEqual(
AssertionError: "<module 'foo' (<class '__main__.FullLoader'>)>" != "<module 'foo' (<class 'test.test_module.FullLoader'>)>"      
- <module 'foo' (<class '__main__.FullLoader'>)>
?                        ^  ^^^^^
+ <module 'foo' (<class 'test.test_module.FullLoader'>)>
?                        ^^^^^^^^^  ^^^^^


----------------------------------------------------------------------
Ran 36 tests in 0.217s

FAILED (failures=1)

I'll soon send a PR to fix it.

Linked PRs

@Eclips4 Eclips4 added the type-bug An unexpected behavior, bug, or error label May 1, 2023
@zware zware reopened this May 1, 2023
@zware zware changed the title Running test_module file directly fails Some test modules fail when run as a script May 1, 2023
@terryjreedy
Copy link
Member

terryjreedy commented May 1, 2023

I grepped with IDLE for other candidates to fix.

Searching "<class 'test." in F:\dev\3x\Lib\test\*.py ...
F:\dev\3x\Lib\test\test_descrtut.py: 42:     <class 'test.test_descrtut.defaultdict'>
F:\dev\3x\Lib\test\test_descrtut.py: 49:     <class 'test.test_descrtut.defaultdict'>
F:\dev\3x\Lib\test\test_descrtut.py: 51:     <class 'test.test_descrtut.defaultdict'>
F:\dev\3x\Lib\test\test_descrtut.py: 267:     classmethod <class 'test.test_descrtut.C'> 1
F:\dev\3x\Lib\test\test_descrtut.py: 270:     classmethod <class 'test.test_descrtut.C'> 1
F:\dev\3x\Lib\test\test_descrtut.py: 276:     classmethod <class 'test.test_descrtut.D'> 1
F:\dev\3x\Lib\test\test_descrtut.py: 279:     classmethod <class 'test.test_descrtut.D'> 1
F:\dev\3x\Lib\test\test_descrtut.py: 295:     classmethod <class 'test.test_descrtut.C'> 1
F:\dev\3x\Lib\test\test_descrtut.py: 299:     classmethod <class 'test.test_descrtut.C'> 1
F:\dev\3x\Lib\test\test_metaclass.py: 121:     Prepare called: ('C', (<class 'test.test_metaclass.B'>, <class 'object'>)) {'other': 'haha'}
F:\dev\3x\Lib\test\test_module.py: 239:             repr(m), "<module 'foo' (<class 'test.test_module.FullLoader'>)>")
F:\dev\3x\Lib\test\test_pdb.py: 582:     <class 'test.test_pdb.MyClass'>
F:\dev\3x\Lib\test\test_typing.py: 120:             "<class 'test.test_typing.AnyTests.test_repr.<locals>.Sub'>",
Hits found: 13
(Hint: right-click to open locations.)

This caught the test_module line you just fixed. Some of these are in interactive output and I don't know if they will fail.

@Eclips4
Copy link
Member Author

Eclips4 commented May 1, 2023

Yeah, I saw these lines, and this should be discussed. Otherwise, I found some another scenarios with the same issue (easy to fix).

@Eclips4
Copy link
Member Author

Eclips4 commented May 1, 2023

However, I'll try to run file test_support on Windows and got interpreter crash. Can someone confirm that?
./python Lib/test/test_support.py
Cannot confirm this on Linux.
Please, use the current main branch if you try to reproduce it.

Eclips4 added a commit to Eclips4/cpython that referenced this issue May 1, 2023
@terryjreedy
Copy link
Member

main built yesterday

F:\dev\3x>python -m test.test_support
Running Debug|x64 interpreter...
.....................s.F..

Crash. Rebuild and try again. Same crash. "Debug assertion Failed."
File minkernel/ctrs/ucrt/src/appcrt/lowio/write.cpp line 50, expression (_osfile(fh) & FOPEN)

@Eclips4
Copy link
Member Author

Eclips4 commented May 1, 2023

F:\dev\3x>python -m test.test_support
Running Debug|x64 interpreter...
.....................s.F..

Crash. Rebuild and try again. Same crash. "Debug assertion Failed."
File minkernel/ctrs/ucrt/src/appcrt/lowio/write.cpp line 50, expression (_osfile(fh) & FOPEN)

Thanks!
I think, it's a minimal reproducible example:

import os


def make_bad_fd():
    file = open("test_file.txt", "wb")
    try:
        return file.fileno()
    finally:
        file.close()



fd = make_bad_fd()
os.write(fd, b"foo")

@terryjreedy
Copy link
Member

f:\dev\3x>python -m test -uall -v test_support runs fine.

Running test_support from IDLE editor: get same Assertion Failure box. After Ignore, failure is

.....................s.F.......s.........s...
======================================================================
FAIL: test_ignored_deprecations_are_silent (__main__.TestSupport.test_ignored_deprecations_are_silent)
Test support.ignore_deprecations_from() silences warnings
----------------------------------------------------------------------
Traceback (most recent call last):
  File "f:\dev\3x\Lib\test\test_support.py", line 52, in test_ignored_deprecations_are_silent
    self.assertEqual(len(messages), 0, messages)
AssertionError: 1 != 0 : ['You should NOT be seeing this.']

@Eclips4
Copy link
Member Author

Eclips4 commented May 1, 2023

f:\dev\3x>python -m test -uall -v test_support runs fine.

Running test_support from IDLE editor: get same Assertion Failure box. After Ignore, failure is

.....................s.F.......s.........s...
======================================================================
FAIL: test_ignored_deprecations_are_silent (__main__.TestSupport.test_ignored_deprecations_are_silent)
Test support.ignore_deprecations_from() silences warnings
----------------------------------------------------------------------
Traceback (most recent call last):
  File "f:\dev\3x\Lib\test\test_support.py", line 52, in test_ignored_deprecations_are_silent
    self.assertEqual(len(messages), 0, messages)
AssertionError: 1 != 0 : ['You should NOT be seeing this.']

That's easy to fix. Just replace

        cls._test_support_token = support.ignore_deprecations_from(
            "test.test_support", like=".*You should NOT be seeing this.*"
        )

to this:

        cls._test_support_token = support.ignore_deprecations_from(
            __main__, like=".*You should NOT be seeing this.*"
        )

Eclips4 added a commit to Eclips4/cpython that referenced this issue May 1, 2023
carljm added a commit to carljm/cpython that referenced this issue May 1, 2023
* main: (463 commits)
  pythongh-104057: Fix direct invocation of test_super (python#104064)
  pythongh-87092: Expose assembler to unit tests (python#103988)
  pythongh-97696: asyncio eager tasks factory (python#102853)
  pythongh-84436: Immortalize in _PyStructSequence_InitBuiltinWithFlags() (pythongh-104054)
  pythongh-104057: Fix direct invocation of test_module (pythonGH-104059)
  pythongh-100458: Clarify Enum.__format__() change of mixed-in types in the whatsnew/3.11.rst (pythonGH-100387)
  pythongh-104018: disallow "z" format specifier in %-format of byte strings (pythonGH-104033)
  pythongh-104016: Fixed off by 1 error in f string tokenizer (python#104047)
  pythonGH-103629: Update Unpack's repr in compliance with PEP 692 (python#104048)
  pythongh-102799: replace sys.exc_info by sys.exception in inspect and traceback modules (python#104032)
  Fix typo in "expected" word in few source files (python#104034)
  pythongh-103824: fix use-after-free error in Parser/tokenizer.c (python#103993)
  pythongh-104035: Do not ignore user-defined `__{get,set}state__` in slotted frozen dataclasses (python#104041)
  pythongh-104028: Reduce object creation while calling callback function from gc (pythongh-104030)
  pythongh-104036: Fix direct invocation of test_typing (python#104037)
  pythongh-102213: Optimize the performance of `__getattr__` (pythonGH-103761)
  pythongh-103895: Improve how invalid `Exception.__notes__` are displayed (python#103897)
  Adjust expression from `==` to `!=` in alignment with the meaning of the paragraph. (pythonGH-104021)
  pythongh-88496: Fix IDLE test hang on macOS (python#104025)
  Improve int test coverage (python#104024)
  ...
pablogsal pushed a commit to pablogsal/cpython that referenced this issue May 1, 2023
Signed-off-by: Pablo Galindo <pablogsal@gmail.com>
pablogsal pushed a commit to pablogsal/cpython that referenced this issue May 1, 2023
Signed-off-by: Pablo Galindo <pablogsal@gmail.com>
@chgnrdv
Copy link
Contributor

chgnrdv commented May 2, 2023

Same is for test_descrtut and test_extcall. test_peg_generator and test_importlib fail too, but for other reason. Actually, there are a lot of tests that fail when directly invoked. For example, all tests that import asyncio (or some other module that does it) fail with the same exception:

$ grep -r "^import asyncio" Lib/test/test_*.py
Lib/test/test_builtin.py:import asyncio
Lib/test/test_contextlib_async.py:import asyncio
Lib/test/test_inspect.py:import asyncio
Lib/test/test_logging.py:import asyncio
Lib/test/test_os.py:import asyncio
Lib/test/test_sys_settrace.py:import asyncio
$ ./python Lib/test/test_pdb.py 
Traceback (most recent call last):
  File "/home/.../cpython/Lib/test/test_pdb.py", line 20, in <module>
    from unittest.mock import patch
  File "/home/.../cpython/Lib/unittest/mock.py", line 26, in <module>
    import asyncio
  File "/home/.../cpython/Lib/asyncio/__init__.py", line 8, in <module>
    from .base_events import *
  File "/home/.../cpython/Lib/asyncio/base_events.py", line 45, in <module>
    from . import staggered
  File "/home/.../cpython/Lib/asyncio/staggered.py", line 11, in <module>
    from . import tasks
  File "/home/.../cpython/Lib/asyncio/tasks.py", line 946, in <module>
    eager_task_factory = create_eager_task_factory(Task)
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/.../cpython/Lib/asyncio/tasks.py", line 936, in create_eager_task_factory
    raise TypeError(
TypeError: Provided constructor does not support eager task execution

@Eclips4
Copy link
Member Author

Eclips4 commented May 2, 2023

For example, all tests that import asyncio (or some other module that does it) fail with the same exception:

Please, read the #104070, I think you got the same problem

@chgnrdv
Copy link
Contributor

chgnrdv commented May 2, 2023

For example, all tests that import asyncio (or some other module that does it) fail with the same exception:

Please, read the #104070, I think you got the same problem

Yep, you're right. A lot of modules still fail for other reasons though.

@eryksun
Copy link
Contributor

eryksun commented May 2, 2023

main built yesterday

F:\dev\3x>python -m test.test_support
Running Debug|x64 interpreter...
.....................s.F..

Crash. Rebuild and try again. Same crash. "Debug assertion Failed." File minkernel/ctrs/ucrt/src/appcrt/lowio/write.cpp line 50, expression (_osfile(fh) & FOPEN)

If you run a test directly, such as python_d -m test.test_support -vv -k test_make_bad_fd, then the reporting modes and reporting files for the debug C runtime's non-standard

  • assertions -- i.e. the macros _ASSERT, _ASSERTE, and _ASSERT_EXPR, or the macros _RPT0 and _RPTN used with the _CRT_ASSERT report type;
  • warnings -- i.e. the macros _RPT0 and _RPTN used with the _CRT_WARN report type; and
  • errors -- i.e. the macros _RPT0 and _RPTN used with the _CRT_ERROR report type --

all remain set to their default values. The assertion and error report types default to showing a dialog box, while the warning report type defaults to outputting a message to an attached debugger, if any. For example:

>>> from msvcrt import *
>>> CrtSetReportMode(CRT_ASSERT, CRTDBG_REPORT_MODE) == CRTDBG_MODE_WNDW
True
>>> CrtSetReportMode(CRT_ERROR, CRTDBG_REPORT_MODE) == CRTDBG_MODE_WNDW
True
>>> CrtSetReportMode(CRT_WARN, CRTDBG_REPORT_MODE) == CRTDBG_MODE_DEBUG
True

On the other hand, if you run the main test suite normally, such as python_d -m test test_support -vv -m test_make_bad_fd, then _CrtSetReportMode() and _CrtSetReportFile() get called to configure the report mode for the _CRT_ASSERT, _CRT_WARN, and _CRT_ERROR report types. They either get configured to no reporting (i.e. mode 0) or, if verbose mode is enabled (i.e. -vv command-line option) to reporting to stderr (i.e. _CRTDBG_MODE_FILE mode with the file set to _CRTDBG_FILE_STDERR). Also, WinAPI SetErrorMode() is called with SEM_FAILCRITICALERRORS | SEM_NOALIGNMENTFAULTEXCEPT | SEM_NOGPFAULTERRORBOX | SEM_NOOPENFILEERRORBOX in order to suppress any message boxes due to system error reporting.


One step that's missing is to explicitly configure the report mode for the standard assert macro in debug builds. This gets configured by the C runtime's _set_error_mode() function. For a console application (e.g. "python.exe"), the default behavior is to write to stderr (i.e. _OUT_TO_STDERR), which is what we want. On the other hand, for a GUI application (e.g. "pythonw.exe"), the default behavior is to show a message box (i.e. _OUT_TO_MSGBOX). If we add any tests that specifically use "pythonw.exe" instead of "python.exe", then we should explicitly configure _set_error_mode(_OUT_TO_STDERR), particularly in the SuppressCrashReport context manager, and ensure that the "pythonw.exe" process is spawned with a valid stderr file. Also, it appears that the msvcrt module wraps the set_error_mode() function, but it doesn't define the required constants OUT_TO_DEFAULT, OUT_TO_STDERR, OUT_TO_MSGBOX, and REPORT_ERRMODE. That oversight should be corrected.

@eryksun
Copy link
Contributor

eryksun commented May 2, 2023

You're running the tests incorrectly for how the debug build is supported on Windows. The debug CRT report modes are not set for each individual test, except in test cases that explicitly use the SuppressCrashReport() context manager. Generally you have to run the main test suite and supply the test you want to run as a command-line option, with an optional -m filter for test cases and -vv if you want the debug CRT assertion, warning and error messages written to stderr. For example: python_d -m test test_support -vv -m test_make_bad_fd.

@chgnrdv
Copy link
Contributor

chgnrdv commented May 2, 2023

@eryksun, are you replying to me? I use Linux, so I guess that none of errors that I mentioned above relate to how test output is handled by system, and most of them happen regardless of -m flag usage (but doesn't happen if libregrtest is involved, i.e. as in python -m test test_support example).

carljm added a commit to carljm/cpython that referenced this issue May 2, 2023
* main: (760 commits)
  pythonGH-104102: Optimize `pathlib.Path.glob()` handling of `../` pattern segments (pythonGH-104103)
  pythonGH-104104: Optimize `pathlib.Path.glob()` by avoiding repeated calls to `os.path.normcase()` (pythonGH-104105)
  pythongh-103822: [Calendar] change return value to enum for day and month APIs (pythonGH-103827)
  pythongh-65022: Fix description of tuple return value in copyreg (python#103892)
  pythonGH-103525: Improve exception message from `pathlib.PurePath()` (pythonGH-103526)
  pythongh-84436: Add integration C API tests for immortal objects (pythongh-103962)
  pythongh-103743: Add PyUnstable_Object_GC_NewWithExtraData (pythonGH-103744)
  pythongh-102997: Update Windows installer to SQLite 3.41.2. (python#102999)
  pythonGH-103484: Fix redirected permanently URLs (python#104001)
  Improve assert_type phrasing (python#104081)
  pythongh-102997: Update macOS installer to SQLite 3.41.2. (pythonGH-102998)
  pythonGH-103472: close response in HTTPConnection._tunnel (python#103473)
  pythongh-88496: IDLE - fix another test on macOS (python#104075)
  pythongh-94673: Hide Objects in PyTypeObject Behind Accessors (pythongh-104074)
  pythongh-94673: Properly Initialize and Finalize Static Builtin Types for Each Interpreter (pythongh-104072)
  pythongh-104016: Skip test for deeply neste f-strings on wasi (python#104071)
  pythongh-104057: Fix direct invocation of test_super (python#104064)
  pythongh-87092: Expose assembler to unit tests (python#103988)
  pythongh-97696: asyncio eager tasks factory (python#102853)
  pythongh-84436: Immortalize in _PyStructSequence_InitBuiltinWithFlags() (pythongh-104054)
  ...
@arhadthedev arhadthedev added the tests Tests in the Lib/test dir label May 3, 2023
carljm added a commit to carljm/cpython that referenced this issue May 11, 2023
* main:
  pythongh-104057: Fix direct invocation of test_support (pythonGH-104069)
  pythongh-87729: improve hit rate of LOAD_SUPER_ATTR specialization (python#104270)
  pythongh-101819: Fix inverted debug preprocessor check in winconsoleio.c (python#104388)
@hugovk
Copy link
Member

hugovk commented Nov 10, 2023

Thanks for the fixes! Anything left here, or okay to close?

@Eclips4
Copy link
Member Author

Eclips4 commented Nov 10, 2023

Thanks for the fixes! Anything left here, or okay to close?

I think we did everything we had to do :)
Thanks for the remind, Hugo!

@Eclips4 Eclips4 closed this as completed Nov 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
tests Tests in the Lib/test dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants