Skip to content

Passing mock to open() crashes Test Run #22624

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
Rafq77 opened this issue Dec 11, 2023 · 4 comments
Closed

Passing mock to open() crashes Test Run #22624

Rafq77 opened this issue Dec 11, 2023 · 4 comments
Assignees
Labels
area-testing triage-needed Needs assignment to the proper sub-team

Comments

@Rafq77
Copy link

Rafq77 commented Dec 11, 2023

Type: Bug

Behaviour

Test execution fails the test while running Tests that passes MagicMock object to open() method.
First observed while testing fix for: #18030
Was using previously different Test Explorer (littlefoxteam.vscode-python-test-adapter), which had no issues with that test. However testing with previous vscode-python extensions down to "v2023.18.0" showed similar results (failures on that test).

Running the test directly via command line yields no errors (Test runs successfully).

Expected vs. Actual

Expected: Run, Debug Run, Run directly from console have the same output/result
Actual: Debugging and running directly from console end in successful result. Running from within test explorer crashes the test.

Steps to reproduce:

Run following Test via Test Explorer:

from unittest.mock import MagicMock

def test_test_magic_mock_instance():
    tmp = MagicMock()
    openHexOutputFile = open(tmp , "a", encoding="utf-8");
    openHexOutputFile.close()

Run Fails:
image

Running pytest via command line directly works:

>pytest -k "test_test_magic_mock_instance" test_section.py
================================================= test session starts =================================================
platform win32 -- Python 3.11.2, pytest-7.4.2, pluggy-1.3.0
rootdir: C:\<project_path>\py.build\_tests
plugins: cov-4.1.0, mock-3.11.1
collected 20 items / 19 deselected / 1 selected

test_section.py .                                                                                                [100%]

========================================== 1 passed, 19 deselected in 0.13s ===========================================

Diagnostic data

  • Python version (& distribution if applicable, e.g. Anaconda): 3.11.2
  • Type of virtual environment used (e.g. conda, venv, virtualenv, etc.): Global
  • Value of the python.languageServer setting: Default
Output for Python in the Output panel (ViewOutput, change the drop-down the upper-right of the Output panel to Python)

CLIENT: Server listening on port 53060...
Received JSON data in run script
Running pytest with args: ['-p', 'vscode_pytest', '--rootdir', 'c:\\<proj_path>', '--ignore=c:\\<proj_path>/tests/googletest', '--ignore=c:\\<proj_path>/mbed-os', '-s', '--capture=sys', 'c:\\<proj_path>tools\\py.build\\_tests\\test_section.py::test_test_magic_mock_instance']
============================= test session starts =============================
platform win32 -- Python 3.11.2, pytest-7.4.2, pluggy-1.3.0
rootdir: c:\<proj_path>
plugins: cov-4.1.0, mock-3.11.1
collected 1 item

hw\hw.e002\tools\py.build\_tests\test_section.py Traceback (most recent call last):
  File "C:\Program Files\Python311\Lib\site-packages\_pytest\main.py", line 271, in wrap_session
    session.exitstatus = doit(config, session) or 0
                         ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\Python311\Lib\site-packages\_pytest\main.py", line 325, in _main
    config.hook.pytest_runtestloop(session=session)
  File "C:\Program Files\Python311\Lib\site-packages\pluggy\_hooks.py", line 493, in __call__
    return self._hookexec(self.name, self._hookimpls, kwargs, firstresult)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\Python311\Lib\site-packages\pluggy\_manager.py", line 115, in _hookexec
    return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\Python311\Lib\site-packages\pluggy\_callers.py", line 152, in _multicall
    return outcome.get_result()
           ^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\Python311\Lib\site-packages\pluggy\_result.py", line 114, in get_result
    raise exc.with_traceback(exc.__traceback__)
  File "C:\Program Files\Python311\Lib\site-packages\pluggy\_callers.py", line 77, in _multicall
    res = hook_impl.function(*args)
          ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\Python311\Lib\site-packages\_pytest\main.py", line 350, in pytest_runtestloop
    item.config.hook.pytest_runtest_protocol(item=item, nextitem=nextitem)
  File "C:\Program Files\Python311\Lib\site-packages\pluggy\_hooks.py", line 493, in __call__
    return self._hookexec(self.name, self._hookimpls, kwargs, firstresult)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\Python311\Lib\site-packages\pluggy\_manager.py", line 115, in _hookexec
    return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\Python311\Lib\site-packages\pluggy\_callers.py", line 152, in _multicall
    return outcome.get_result()
           ^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\Python311\Lib\site-packages\pluggy\_result.py", line 114, in get_result
    raise exc.with_traceback(exc.__traceback__)
  File "C:\Program Files\Python311\Lib\site-packages\pluggy\_callers.py", line 77, in _multicall
    res = hook_impl.function(*args)
          ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\Python311\Lib\site-packages\_pytest\runner.py", line 114, in pytest_runtest_protocol
    runtestprotocol(item, nextitem=nextitem)
  File "C:\Program Files\Python311\Lib\site-packages\_pytest\runner.py", line 133, in runtestprotocol
    reports.append(call_and_report(item, "call", log))
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\Python311\Lib\site-packages\_pytest\runner.py", line 226, in call_and_report
    hook.pytest_runtest_logreport(report=report)
  File "C:\Program Files\Python311\Lib\site-packages\pluggy\_hooks.py", line 493, in __call__
    return self._hookexec(self.name, self._hookimpls, kwargs, firstresult)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\Python311\Lib\site-packages\pluggy\_manager.py", line 115, in _hookexec
    return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\Python311\Lib\site-packages\pluggy\_callers.py", line 113, in _multicall
    raise exception.with_traceback(exception.__traceback__)
  File "C:\Program Files\Python311\Lib\site-packages\pluggy\_callers.py", line 77, in _multicall
    res = hook_impl.function(*args)
          ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\Python311\Lib\site-packages\_pytest\terminal.py", line 599, in pytest_runtest_logreport
    self._tw.write(letter, **markup)
  File "C:\Program Files\Python311\Lib\site-packages\_pytest\_io\terminalwriter.py", line 155, in write
    self._file.write(msg)
OSError: [Errno 9] Bad file descriptor

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Program Files\Python311\Lib\site-packages\_pytest\main.py", line 291, in wrap_session
    config.notify_exception(excinfo, config.option)
  File "C:\Program Files\Python311\Lib\site-packages\_pytest\config\__init__.py", line 1108, in notify_exception
    res = self.hook.pytest_internalerror(excrepr=excrepr, excinfo=excinfo)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\Python311\Lib\site-packages\pluggy\_hooks.py", line 493, in __call__
    return self._hookexec(self.name, self._hookimpls, kwargs, firstresult)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\Python311\Lib\site-packages\pluggy\_manager.py", line 115, in _hookexec
    return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\Python311\Lib\site-packages\pluggy\_callers.py", line 113, in _multicall
    raise exception.with_traceback(exception.__traceback__)
  File "C:\Program Files\Python311\Lib\site-packages\pluggy\_callers.py", line 77, in _multicall
    res = hook_impl.function(*args)
          ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\Python311\Lib\site-packages\_pytest\terminal.py", line 527, in pytest_internalerror
    self.write_line("INTERNALERROR> " + line)
  File "C:\Program Files\Python311\Lib\site-packages\_pytest\terminal.py", line 482, in write_line
    self.ensure_newline()
  File "C:\Program Files\Python311\Lib\site-packages\_pytest\terminal.py", line 448, in ensure_newline
    self._tw.line()
  File "C:\Program Files\Python311\Lib\site-packages\_pytest\_io\terminalwriter.py", line 171, in line
    self.write("\n")
  File "C:\Program Files\Python311\Lib\site-packages\_pytest\_io\terminalwriter.py", line 155, in write
    self._file.write(msg)
OSError: [Errno 9] Bad file descriptor

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "c:\Users\<USER>\.vscode\extensions\ms-python.python-2023.22.0\pythonFiles\vscode_pytest\run_pytest_script.py", line 68, in <module>
    pytest.main(arg_array)
  File "C:\Program Files\Python311\Lib\site-packages\_pytest\config\__init__.py", line 169, in main
    ret: Union[ExitCode, int] = config.hook.pytest_cmdline_main(
                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\Python311\Lib\site-packages\pluggy\_hooks.py", line 493, in __call__
    return self._hookexec(self.name, self._hookimpls, kwargs, firstresult)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\Python311\Lib\site-packages\pluggy\_manager.py", line 115, in _hookexec
    return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\Python311\Lib\site-packages\pluggy\_callers.py", line 113, in _multicall
    raise exception.with_traceback(exception.__traceback__)
  File "C:\Program Files\Python311\Lib\site-packages\pluggy\_callers.py", line 77, in _multicall
    res = hook_impl.function(*args)
          ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\Python311\Lib\site-packages\_pytest\main.py", line 318, in pytest_cmdline_main
    return wrap_session(config, _main)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\Python311\Lib\site-packages\_pytest\main.py", line 306, in wrap_session
    config.hook.pytest_sessionfinish(
  File "C:\Program Files\Python311\Lib\site-packages\pluggy\_hooks.py", line 493, in __call__
    return self._hookexec(self.name, self._hookimpls, kwargs, firstresult)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\Python311\Lib\site-packages\pluggy\_manager.py", line 115, in _hookexec
    return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\Python311\Lib\site-packages\pluggy\_callers.py", line 130, in _multicall
    teardown[0].send(outcome)
  File "C:\Program Files\Python311\Lib\site-packages\_pytest\terminal.py", line 858, in pytest_sessionfinish
    self._tw.line("")
  File "C:\Program Files\Python311\Lib\site-packages\_pytest\_io\terminalwriter.py", line 171, in line
    self.write("\n")
  File "C:\Program Files\Python311\Lib\site-packages\_pytest\_io\terminalwriter.py", line 155, in write
    self._file.write(msg)
OSError: [Errno 9] Bad file descriptor
Finished running tests!

User Settings


languageServer: "Jedi"

testing
• pytestArgs: "<placeholder>"
• pytestEnabled: true

experiments
• optInto: ["pythonTestAdapter"]

Extension version: 2023.23.13391009
VS Code version: Code 1.84.2 (1a5daa3a0231a0fbba4f14db7ec463cf99d7768e, 2023-11-09T10:51:52.184Z)
OS version: Windows_NT x64 10.0.19045
Modes:

System Info
Item Value
CPUs Intel(R) Core(TM) i7-10850H CPU @ 2.70GHz (12 x 2712)
GPU Status 2d_canvas: enabled
canvas_oop_rasterization: enabled_on
direct_rendering_display_compositor: disabled_off_ok
gpu_compositing: enabled
multiple_raster_threads: enabled_on
opengl: enabled_on
rasterization: enabled
raw_draw: disabled_off_ok
video_decode: enabled
video_encode: enabled
vulkan: disabled_off
webgl: enabled
webgl2: enabled
webgpu: enabled
Load (avg) undefined
Memory (System) 31.64GB (16.97GB free)
Process Argv --crash-reporter-id 56596b91-477c-41ad-8393-33181aeda2a8
Screen Reader no
VM 0%
A/B Experiments
vsliv368cf:30146710
vsreu685:30147344
python383cf:30185419
vspor879:30202332
vspor708:30202333
vspor363:30204092
vslsvsres303:30308271
vserr242cf:30382550
pythontb:30283811
vsjup518:30340749
pythonptprofiler:30281270
vsdfh931cf:30280410
vshan820:30294714
vstes263:30335439
vscorecescf:30445987
vscod805:30301674
binariesv615:30325510
bridge0708:30335490
bridge0723:30353136
vsaa593:30376534
pythonvs932:30410667
py29gd2263:30899288
vsclangdf:30486550
c4g48928:30535728
dsvsc012cf:30540253
azure-dev_surveyone:30548225
282f8724:30602487
f6dab269:30613381
2i9eh265:30646982
showlangstatbar:30737416
0bi6i642:30917235
fixshowwlkth:30771522
showindicator:30805244
pythongtdpath:30769146
i26e3531:30792625
welcomedialog:30910333
pythonnosmt12:30797651
pythonidxpt:30866567
pythonnoceb:30805159
asynctok:30898717
dsvsc013:30795093
dsvsc014:30804076
dsvsc015:30845448
pythontestfixt:30902429
pyreplss1:30897532
pythonmypyd1:30879173
pythoncet0:30885854
2e7ec940:30885897
pythontbext0:30879054
dsvsc016:30899300
dsvsc017:30899301
dsvsc018:30899302
aa_t_chat:30882232
dsvsc019:30917259
@github-actions github-actions bot added the triage-needed Needs assignment to the proper sub-team label Dec 11, 2023
@eleanorjboyd eleanorjboyd self-assigned this Dec 11, 2023
@eleanorjboyd
Copy link
Member

Will investigate- thanks for the detailed report!

@eleanorjboyd
Copy link
Member

Hi! I tried this myself with the code you sent over and successfully got it to run. Could you try running it in the terminal with all these parameters 'python' '-m' 'pytest' '--rootdir', 'c:\\<proj_path>', '--ignore=c:\\<proj_path>/tests/googletest', '--ignore=c:\\<proj_path>/mbed-os', '-s', '--capture=sys', 'c:\\<proj_path>tools\\py.build\\_tests\\test_section.py::test_test_magic_mock_instance' and see what happens? Secondly could you try removing those args and seeing if it works?
Thanks and let me know

@eleanorjboyd eleanorjboyd added the info-needed Issue requires more information from poster label Dec 13, 2023
@Rafq77
Copy link
Author

Rafq77 commented Dec 14, 2023

Thank you for your effort. It looks like the problem doesn't hide within the vscode-extension and I am truly sorry for that.

What I could confirm is that the usage of arguments -s and --capture=sys triggers the mentioned behavior on my side. Additionaly the provided test code was also triggering an error, when the open method was given argument a for appending.

I can execute a successful run when the append on the open() method is replaced with w together with pytest arguments reduced by the misfortunate -s and --capture=sys parameters.

@Rafq77 Rafq77 closed this as completed Dec 14, 2023
@github-actions github-actions bot removed the info-needed Issue requires more information from poster label Dec 14, 2023
@eleanorjboyd
Copy link
Member

No need to apologies! Glad you got it figured out! Happy coding!

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 14, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-testing triage-needed Needs assignment to the proper sub-team
Projects
None yet
Development

No branches or pull requests

2 participants