classification
Title: NameError: name 'open' is not defined
Type: crash Stage: resolved
Components: Library (Lib) Versions: Python 3.6
process
Status: closed Resolution: duplicate
Dependencies: Superseder: logging: Trying to log during Python finalization with NameError: name 'open' is not defined
View: 26789
Assigned To: Nosy List: Sandeep, serhiy.storchaka, vinay.sajip, vstinner, xtreak
Priority: normal Keywords:

Created on 2020-01-31 19:15 by Sandeep, last changed 2020-10-30 16:59 by vstinner. This issue is now closed.

Messages (9)
msg361125 - (view) Author: Sandeep (Sandeep) * Date: 2020-01-31 19:18
Hi 
   I wrote a python script which gets file from other server and it works fine till the time I don't introduces logging feature to it. 
Please advice how to fix below

This is the code I am using for logging '

import logging
logging.basicConfig(filename=localLogfile, level=logging.DEBUG)

--Error Message 

Traceback (most recent call last):
  File "/d01/python3/lib64/python3.6/site-packages/pysftp/__init__.py", line 1013, in __del__
  File "/d01/python3/lib64/python3.6/site-packages/pysftp/__init__.py", line 785, in close
  File "/d01/python3/lib64/python3.6/site-packages/paramiko/sftp_client.py", line 194, in close
  File "/d01/python3/lib64/python3.6/site-packages/paramiko/sftp_client.py", line 185, in _log
  File "/d01/python3/lib64/python3.6/site-packages/paramiko/sftp.py", line 158, in _log
  File "/opt/rh/rh-python36/root/usr/lib64/python3.6/logging/__init__.py", line 1372, in log
  File "/opt/rh/rh-python36/root/usr/lib64/python3.6/logging/__init__.py", line 1442, in _log
  File "/opt/rh/rh-python36/root/usr/lib64/python3.6/logging/__init__.py", line 1452, in handle
  File "/opt/rh/rh-python36/root/usr/lib64/python3.6/logging/__init__.py", line 1514, in callHandlers
  File "/opt/rh/rh-python36/root/usr/lib64/python3.6/logging/__init__.py", line 863, in handle
  File "/opt/rh/rh-python36/root/usr/lib64/python3.6/logging/__init__.py", line 1069, in emit
  File "/opt/rh/rh-python36/root/usr/lib64/python3.6/logging/__init__.py", line 1059, in _open
NameError: name 'open' is not defined
msg361127 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2020-01-31 19:41
See also https://bugs.python.org/issue26789
msg361219 - (view) Author: Vinay Sajip (vinay.sajip) * (Python committer) Date: 2020-02-02 06:37
Please attach a small script which allows reproduction of this issue - otherwise, it's unlikely we'll be able to make much progress, and I'll probably close it in a week or so if you haven't been able to do this. Thanks!
msg361222 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2020-02-02 07:53
Symptoms as when try to log very late at shutdown stage, when the content of the builtin module is restored to its original value which does not include open imported from io.
msg361224 - (view) Author: Vinay Sajip (vinay.sajip) * (Python committer) Date: 2020-02-02 08:28
Well, my comment/request was prompted by whether this issue is a duplicate of bpo-26789, because it's not clear from the OP. If it is, then it can be closed as such. I don't think this is specifically a logging problem, more a case of how things become inaccessible as the interpreter shuts down. Any code which calls "open" (or some other builtins) would run into this problem, not just and not specifically logging. I'm not sure, but I don't believe we have a good answer to this problem in general.
msg379951 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2020-10-30 11:59
I think this is a multi-level problem, and it perhaps should be solved on different levels separately.

As for logging, it can be used at shutdown by user code, it may help to understand the problem with the user code, and it may work most of time. But if it happen too late, it fails, and the printed traceback does not help but confuses user. The logging module should try the best to help the user. It should be ready to handle such situation. It is not failure of the end user that this error happens, and they cannot do anything in any way, so it is better to suppress the confusion traceback (maybe emit a warning?).
msg379965 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2020-10-30 14:23
I mark this issue as a duplicate of bpo-26789.
msg379983 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2020-10-30 16:51
Issue26789 is about asyncio, but this issue about logging.
msg379985 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2020-10-30 16:59
> Issue26789 is about asyncio, but this issue about logging.

I chose to reassign Issue26789 to the logging module.
History
Date User Action Args
2020-10-30 16:59:21vstinnersetmessages: + msg379985
2020-10-30 16:51:25serhiy.storchakasetmessages: + msg379983
2020-10-30 14:23:33vstinnersetstatus: open -> closed

superseder: logging: Trying to log during Python finalization with NameError: name 'open' is not defined

nosy: + vstinner
messages: + msg379965
resolution: duplicate
stage: resolved
2020-10-30 11:59:16serhiy.storchakasetmessages: + msg379951
2020-02-02 08:28:51vinay.sajipsetmessages: + msg361224
2020-02-02 07:53:27serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg361222
2020-02-02 06:37:09vinay.sajipsetmessages: + msg361219
2020-01-31 19:41:25xtreaksetstatus: pending -> open
nosy: + vinay.sajip, xtreak
messages: + msg361127

2020-01-31 19:18:57Sandeepsetstatus: open -> pending

messages: + msg361125
2020-01-31 19:15:26Sandeepcreate