Skip to content

All TestCase classes use a shared stack for class cleanup #99645

Closed
@serhiy-storchaka

Description

@serhiy-storchaka

TestCase class methods addClassCleanup() and doClassCleanups() are similar to instance methods addCleanup() and doCleanups(). add*Cleanup() add a callback into a list, and do*Cleanups() pop them from a list and call. The main difference is that the latter methods use a list stored as an instance attribute while the former use a list stored as a class attribute.

The problem is that the class attribute TestCase._class_cleanups is shared between all TestCase subclasses. It usually does not cause the problem, because tests in different test classes are run sequentially, but if you run a new test suite while running a test, and the outer test class use addClassCleanup(), the callback registered for the outer test class will be called when cleaning up the inner test class. It can happen when you test unittest itself. It really happened, and was unnoticed only because the outer class did not use addClassCleanup().

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.10only security fixes3.11only security fixes3.12only security fixestype-bugAn unexpected behavior, bug, or error

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions