survey icon indicating copy to clipboard operation
survey copied to clipboard

Not working with pytest. `io.UnsupportedOperation: redirected stdin is pseudofile, has no fileno()` on import

Open erikw opened this issue 9 months ago • 2 comments

Hello,

thank you for this cool lib. I'm using it in a project now and I noticed that when I was going to write a test and I imported my implementation module, I got an error from survey.

I extracted this to a minimal example showing the problem. Set up dependencies:

$ pip install survey==5.4.2 pytest==8.3.4

Create implementation file project.py:

import survey

nbr = survey.routines.numeric("Enter a number: ", decimal=False)
print(nbr)

Create a test file test_project.py. It can be empty of tests even! The error gets triggered by just the import of the project module:

import project

Now run the test file with pytest and observe the error:

$ pytest test_project.py
================================================== test session starts ===================================================
platform linux -- Python 3.12.8, pytest-8.3.4, pluggy-1.5.0
rootdir: /home/ubuntu/test
plugins: typeguard-4.4.1
collected 0 items / 1 error                                                                                              

========================================================= ERRORS =========================================================
____________________________________________ ERROR collecting test_project.py ____________________________________________
test_project.py:1: in <module>
    import project
project.py:1: in <module>
    import survey
../.local/lib/python3.12/site-packages/survey/__init__.py:3: in <module>
    from . import _system as system
../.local/lib/python3.12/site-packages/survey/_system.py:15: in <module>
    _io = io = _core.IO(sys.stdin, sys.stdout)
../.local/lib/python3.12/site-packages/survey/_core/_io.py:53: in __init__
    super().__init__(i.buffer, o.buffer, *args, **kwargs)
../.local/lib/python3.12/site-packages/survey/_core/_io_os_posix.py:17: in __init__
    self._i_fd = self._i.fileno()
/usr/local/lib/python3.12/site-packages/_pytest/capture.py:227: in fileno
    raise UnsupportedOperation("redirected stdin is pseudofile, has no fileno()")
E   io.UnsupportedOperation: redirected stdin is pseudofile, has no fileno()
================================================ short test summary info =================================================
ERROR test_project.py - io.UnsupportedOperation: redirected stdin is pseudofile, has no fileno()

Any ideas on how I can fix this, so I can write test for some functions defined in the same module that imports survey?

Thank you! :)

Versions

  • Python: 3.12.8
  • Survey: 5.4.2
  • Pytest: 8.3.4
  • OS: Ubuntu 24.04.1

erikw avatar Jan 16 '25 15:01 erikw