Skip to content

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

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
brechtm opened this issue Aug 25, 2016 · 5 comments
Closed

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

brechtm opened this issue Aug 25, 2016 · 5 comments

Comments

@brechtm
Copy link

brechtm commented Aug 25, 2016

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.

@nicoddemus
Copy link
Member

Thanks for the report.

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

@The-Compiler
Copy link
Member

Did this worked in a previous version?

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

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

@brechtm
Copy link
Author

brechtm commented Aug 26, 2016

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 :-)

@nicoddemus
Copy link
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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants