Skip to content

with capsys.disabled() causes "I/O operation on closed file" #1873

Closed

Description

@brechtm

Sphinx.build() fails if contained in a capsys.disabled() context manager:

tests_regression/test_sphinxdocs.py:45: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
.tox/regression/lib/python3.5/site-packages/sphinx/application.py:296: in build
    self.builder.compile_update_catalogs()
.tox/regression/lib/python3.5/site-packages/sphinx/builders/__init__.py:205: in compile_update_catalogs
    self.compile_catalogs(catalogs, message)
.tox/regression/lib/python3.5/site-packages/sphinx/builders/__init__.py:166: in compile_catalogs
    self.info(bold('building [mo]: ') + message)
.tox/regression/lib/python3.5/site-packages/sphinx/application.py:370: in info
    self._log(message, self._status, nonl)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
self = <sphinx.application.Sphinx object at 0x7f701dde1588>
message = '\x1b[01mbuilding [mo]: \x1b[39;49;00mtargets for 0 po files that are out of date'
wfile = <_io.StringIO object at 0x7f701ef41b88>, nonl = False
    def _log(self, message, wfile, nonl=False):
        try:
>           wfile.write(message)
E           ValueError: I/O operation on closed file
.tox/regression/lib/python3.5/site-packages/sphinx/application.py:322: ValueError

Full log: https://travis-ci.org/brechtm/rinohtype/jobs/155119487

Running the tests with the -s flag instead of the context manager works fine.

Activity

  1. nicoddemus commented on Aug 26, 2016

    @nicoddemus
    Member

    Thanks for the report.

    Did this worked in a previous version? Could please you post a reproducible example?

  2. The-Compiler commented on Aug 26, 2016

    @The-Compiler
    Member

    Did this worked in a previous version?

    capsys.disabled() was added in 3.0 (by that guy with a penguin avatar) 😉

  3. nicoddemus commented on Aug 26, 2016

    @nicoddemus
    Member

    capsys.disabled() was added in 3.0 (by that guy with a penguin avatar)

    Duh 😅

    @brechtm I see you are constructing the sphinx object outside the with statement:

    sphinx = Sphinx(...)
    with capsys.disabled();
        sphinx.build()

    Try construct the sphinx object inside the with statement to see if it helps:

    with capsys.disabled();
        sphinx = Sphinx(...)
        sphinx.build()

    I suspect the error is happening because some code is opening and keeping a reference to a log file somewhere, and capsys.disabled() closes that file to restore the original stdin. If that's the case and the above suggestion does not work, I'm afraid there's nothing that can be done on our side.

  4. brechtm commented on Aug 26, 2016

    @brechtm
    Author

    Nope, the problem persists if I make the instantiation part of the context. It does work, however, if I also move the import statement to the content:

    with capsys.disabled():
        from sphinx.application import Sphinx
        sphinx = Sphinx(...)
        sphinx.build()
    

    Which might indicate some nasty trickery going on in the Sphinx modules, indeed :-)

  5. nicoddemus commented on Aug 26, 2016

    @nicoddemus
    Member

    It is probably setting up a logger at module level, which is actually the usual approach to logging.

    I think we can close this as there is nothing that can be done on Pytest's side. Feel free to reopen if you still want to discuss something or have other questions. 😁

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions