blessed: ValueError: underlying buffer has been detached (on Travis CI)

On some environments (Travis CI on windows instances), Terminal() fails inside the constructor:

.eggs\blessed-1.16.1-py3.7.egg\blessed\terminal.py:185: in __init__
    callable(stream.fileno) else None)
E   ValueError: underlying buffer has been detached

In such an environment where stdout/filestream can be detached, sys.__stdout__ can be invalidated where sys.stdout does not. For a reference, please have a look at this: https://github.com/numpy/numpy/issues/2761#issuecomment-10672029

Suggestion: Why not sys.stdout?

Anyway Terminal() fails in some test environments (but works okay in normal use cases). It seems that the Travis-CI (windows) tests of blessed do not have this issue – they seem to be using TestTerminal which is basically Terminal(kind=...) but they appear to be subjective to this problem.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 21 (8 by maintainers)

Commits related to this issue

Most upvoted comments

Suggestion: Why not sys.stdout?

Because we have to deal directly with the console device and sys.stdout is not always attached to the console.

Are you using numpy? It looks like they introduced a bug here. Their goal was to force to UTF-8, but they didn’t do it in a very good way as far as I can tell. Later they added a check so they didn’t force it if it was already UTF-8. If it’s not, they redefine sys.stdoutand detach sys.__stdout__ with this:

    sys.stdout = io.TextIOWrapper(
        sys.stdout.detach(), 'utf-8', errors, newline, line_buffering)

In 3.7 and later they should probably be using sys.stdout.reconfigure() and, in earlier versions on Python 3, possibly something like this:

    sys.stdout = io.TextIOWrapper(
        sys.stdout.buffer, 'utf-8', errors, newline, line_buffering)

I’m not sure if that has side effects of doing that, but the way they are doing it, they’re breaking documented interfaces.

So I would say there isn’t really much reason to check if sys.__stdout__ is detached because it shouldn’t be unless you’re doing things you probably shouldn’t be doing. We could make it fail in a nicer way, but I feel like it should be failing in this case.

I suggest you file a bug with numpy to not break sys.__stdout__. For your tests, as a workaround, you can specify sys.stdout as your stream when instantiating Terminal() or force your encoding to ‘UTF-8’ before calling Terminal().

We will push a minor release fix of these things to pypi by end of weekend if that helps you any, thanks for helping us @wookayin

Sorry to miss it today, it’s late though, but we’ll make a 1.17.1 minor re-release tomorrow

Sounds great, and very best wishes to you in Ann Arbor, I did live there a number of years ago 😃