An error occurs when trying to pipe a python program to CD- sys.excepthook is missing lost sys.stderr

The answer was found on stackoverflow at

NB: I have not attempted to reproduce the problem described below under Windows, or with versions of Python other than 2.7.3.
The most reliable way to elicit the problem in question is to pipe the output of the following test script through : (under bash):

try:
    for n in range(20):
        print n
except:
    pass

I.e.:

% python testscript.py | :
close failed in file object destructor:
sys.excepthook is missing
lost sys.stderr

My question is:

How can I modify the test script above to avoid the error message when the script is run as shown (under Unix/bash)?

(As the test script shows, the error cannot be trapped with a try-except.)
The example above is, admittedly, highly artificial, but I’m running into the same problem sometimes when the output of a script of mine is piped through some 3rd party software.
The error message is certainly harmless, but it is disconcerting to end-users, so I would like to silence it.
EDIT: The following script, which differs from the original one above only in that it redefines sys.excepthook, behaves exactly like the one given above.

import sys
STDERR = sys.stderr
def excepthook(*args):
    print >> STDERR, 'caught'
    print >> STDERR, args

sys.excepthook = excepthook

try:
    for n in range(20):
        print n
except:
    pass


 

How can I modify the test script above to avoid the error message when the script is run as shown (under Unix/bash)?

You will need to prevent the script from writing anything to standard output. That means removing any print statements and any use of sys.stdout.write, as well as any code that calls those.
The reason this is happening is that you’re piping a nonzero amount of output from your Python script to something which never reads from standard input. This is not unique to the : command; you can get the same result by piping to any command which doesn’t read standard input, such as

python testscript.py | cd .

Or for a simpler example, consider a script printer.py containing nothing more than

print 'abcde'

Then

python printer.py | python printer.py

will produce the same error.

When you pipe the output of one program into another, the output produced by the writing program gets backed up in a buffer, and waits for the reading program to request that data from the buffer. As long as the buffer is nonempty, any attempt to close the writing file object is supposed to fail with an error. This is the root cause of the messages you’re seeing.

The specific code that triggers the error is in the C language implementation of Python, which explains why you can’t catch it with a try/except block: it runs after the contents of your script has finished processing. Basically, while Python is shutting itself down, it attempts to close stdout, but that fails because there is still buffered output waiting to be read. So Python tries to report this error as it would normally, but  sys.excepthook  has already been removed as part of the finalization procedure, so that fails. Python then tries to print a message to  sys.stderr , but that has already been deallocated so again, it fails. The reason you see the messages on the screen is that the Python code does contain a contingency fprintf to write out some output to the file pointer directly, even if Python’s output object doesn’t exist.

I can provide details of the relevant parts of the C code if you’re interested.

 

Deeply impressed, very deep and in place.

I learned a few points, which are displayed as follows:

1. Pipeline command | flows data from standard output stdout to standard input stdin, so the command that does not read data from stdin cannot obtain data through pipeline command. Is it accurate

2. CD command is input through the command line of the terminal (device [keyboard] Read data, so Python <pythonScript.py>The CD will fail.

3. How to transfer values to commands such as CD?

It is quoted from Chapter 11 of Uncle Niao’s Linux private dishes: “in a string of commands, you need to provide information through other commands. You can use reverse single quotation marks”‘command ‘”or” $(command) ”

 

 

Read More: