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:
(underbash
):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:
- How to Solve k8s Nodal issues: /sys/fs/cgroup/memory/docker: no space left on device\““: unknown.
- [Solved] Violation of check constraint (GYPTS.SYS_C0016857)
- Keil Compile Error: ..\OBJ\USART.axf: error: L6002U: Could not open file ..\obj\sys.o: No such file or directory
- The loop of life and death occurs when the El table component of element UI is bidirectional bound
- [Solved] Error: Another program is already listening on a port that one of our HTTP servers is configured to
- [Solved] MetaStoreClient lost connection. Attempting to reconnect (1 of 1) after 1s. getCurrentNotificatio
- [Solved] SyntaxError: Missing parentheses in call to ‘print‘. Did you mean print(e)?
- [Solved] Idea 2020 uses SVN error: ‘C: \ program’ is not an internal or external command
- vscode: Error loading workspace: err: exit status 1: stderr: build flag -mod=readonly only valid
- [Solved] A needed class was not found. This could be due to an error in your runpath. Missing class: scala/co
- [Solved] At least one of these environment variable is needed to run this program
- Python Importerror: DLL load failed: the page file is too small to complete the operation.
- [Solved] Upstream connect error or disconnect occurs after the k8s istio virtual machine is restarted
- [Solved] jenkins-deleteDir Error (FilePath is missing)
- Centos7 hive started to report an error. There is no route to the host. The firewall has been closed
- [Solved] (error) MISCONF Redis is configured to save RDB snapshots, but is currently not able to persist on disk.
- Introduce wangeditor5 to run an error: You may need an appropriate loader to handle this file type (es6 compatibility issue)
- Maven package error: [WARNING] The POM for xxx is missing, no dependency inform