Skip to content

test_inspect fails with --forever argument #107446

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

Closed
Eclips4 opened this issue Jul 29, 2023 · 5 comments
Closed

test_inspect fails with --forever argument #107446

Eclips4 opened this issue Jul 29, 2023 · 5 comments
Assignees
Labels
3.13 bugs and security fixes release-blocker tests Tests in the Lib/test dir type-bug An unexpected behavior, bug, or error

Comments

@Eclips4
Copy link
Member

Eclips4 commented Jul 29, 2023

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

@Eclips4 Eclips4 added the type-bug An unexpected behavior, bug, or error label Jul 29, 2023
@AlexWaygood AlexWaygood added the tests Tests in the Lib/test dir label Jul 29, 2023
@Eclips4
Copy link
Member Author

Eclips4 commented Jul 29, 2023

bisected to #106968
cc @gaogaotiantian

@gaogaotiantian gaogaotiantian self-assigned this Jul 29, 2023
@Eclips4
Copy link
Member Author

Eclips4 commented Jul 29, 2023

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)

@gaogaotiantian
Copy link
Member

gaogaotiantian commented Jul 29, 2023

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.

@gaogaotiantian
Copy link
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.

@Eclips4
Copy link
Member Author

Eclips4 commented Jul 30, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.13 bugs and security fixes release-blocker tests Tests in the Lib/test dir type-bug An unexpected behavior, bug, or error
Projects
Development

No branches or pull requests

4 participants