Skip to content

test_inspect fails with --forever argument #107446

Closed

Description

@Eclips4

Traceback:

...many lines...
test_unwrap_several (test.test_inspect.TestUnwrap.test_unwrap_several) ... ok

======================================================================
ERROR: test_class_with_method_from_other_module (test.test_inspect.TestBuggyCases.test_class_with_method_from_other_module)       
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Users\KIRILL-1\CLionProjects\cpython\Lib\test\test_inspect.py", line 992, in test_class_with_method_from_other_module  
    self.assertIn("correct", inspect.getsource(inspect_actual.A))
                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\KIRILL-1\CLionProjects\cpython\Lib\inspect.py", line 1317, in getsource
    lines, lnum = getsourcelines(object)
                  ^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\KIRILL-1\CLionProjects\cpython\Lib\inspect.py", line 1299, in getsourcelines
    lines, lnum = findsource(object)
                  ^^^^^^^^^^^^^^^^^^
  File "C:\Users\KIRILL-1\CLionProjects\cpython\Lib\inspect.py", line 1134, in findsource
    raise OSError('could not get source code')
OSError: could not get source code

----------------------------------------------------------------------
Ran 286 tests in 2.392s

FAILED (errors=1)
test test_inspect failed
test_inspect failed (1 error)

== Tests result: FAILURE ==

1 test OK.

1 test failed:
    test_inspect

Total duration: 7.2 sec
Tests result: FAILURE

Triend on current main.

Linked PRs

Activity

  1. Eclips4 commented on Jul 29, 2023

    @Eclips4
    MemberAuthor

    bisected to #106968
    cc @gaogaotiantian

  2. Eclips4 commented on Jul 29, 2023

    @Eclips4
    MemberAuthor

    Interesting. If we replace tempfile.TemporaryDirectory with something like that, error will gone.

        def test_class_with_method_from_other_module(self):
            tempdir = TESTFN + '_other_module_dir'
            os.mkdir(tempdir)
            with open(os.path.join(tempdir, 'inspect_actual%spy' % os.extsep),
                      'w', encoding='utf-8') as f:
                f.write(textwrap.dedent("""
                                    import inspect_other
                                    class A:
                                        def f(self):
                                            pass
                                    class A:
                                        def f(self):
                                            pass  # correct one
                                    A.f = inspect_other.A.f
                                    """))
            with open(os.path.join(tempdir, 'inspect_other%spy' % os.extsep),
                    'w', encoding='utf-8') as f:
                f.write(textwrap.dedent("""
                                    class A:
                                        def f(self):
                                            pass
                                    """))
            with DirsOnSysPath(tempdir):
                import inspect_actual
                self.assertIn("correct", inspect.getsource(inspect_actual.A))
            shutil.rmtree(tempdir)
  3. gaogaotiantian commented on Jul 29, 2023

    @gaogaotiantian
    Member

    The inspect_actual module was loaded then the temp dir is removed under --forever, so the source file is no where to be found. I'll do a PR soon.

  4. gaogaotiantian commented on Jul 29, 2023

    @gaogaotiantian
    Member

    I chose to simply do sys.modules.pop("inspect_actual"). We can try to fix the temp dir, but I do like the context of tempfile.TemporaryDirectory(), I think it's cleaner than TESTFN. However, I'm not opposed to use the same dir for the module.

  5. Eclips4 commented on Jul 30, 2023

    @Eclips4
    MemberAuthor

    Sure, solution with sys.modules.pop(...) more appropriate.

  6. added a commit that references this issue on Aug 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

3.13only security fixesrelease-blockertestsTests in the Lib/test dirtype-bugAn unexpected behavior, bug, or error

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions