LonamiWebs / Telethon

Pure Python 3 MTProto API Telegram client library, for bots too!
https://docs.telethon.dev
MIT License
9.77k stars 1.39k forks source link

“NameError: global name 'open' is not defined” in __del__ #1073

Closed fojetin closed 5 years ago

fojetin commented 5 years ago
Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/telethon/client/telegrambaseclient.py", line 352, in __del__
  File "/usr/local/lib/python3.5/dist-packages/telethon/sync.py", line 33, in syncified
  File "/usr/lib/python3.5/asyncio/base_events.py", line 387, in run_until_complete
  File "/usr/lib/python3.5/asyncio/futures.py", line 274, in result
  File "/usr/lib/python3.5/asyncio/tasks.py", line 239, in _step
  File "/usr/local/lib/python3.5/dist-packages/telethon/client/telegrambaseclient.py", line 325, in disconnect
  File "/usr/local/lib/python3.5/dist-packages/telethon/client/telegrambaseclient.py", line 339, in _disconnect
  File "/usr/local/lib/python3.5/dist-packages/telethon/network/mtprotosender.py", line 140, in disconnect
  File "/usr/local/lib/python3.5/dist-packages/telethon/network/mtprotosender.py", line 143, in _disconnect
  File "/usr/lib/python3.5/logging/__init__.py", line 1279, in info
  File "/usr/lib/python3.5/logging/__init__.py", line 1415, in _log
  File "/usr/lib/python3.5/logging/__init__.py", line 1425, in handle
  File "/usr/lib/python3.5/logging/__init__.py", line 1487, in callHandlers
  File "/usr/lib/python3.5/logging/__init__.py", line 855, in handle
  File "/usr/lib/python3.5/logging/__init__.py", line 1047, in emit
  File "/usr/lib/python3.5/logging/__init__.py", line 1037, in _open
NameError: name 'open' is not defined

I think bug related to this question: https://stackoverflow.com/questions/23422188/why-am-i-getting-nameerror-global-name-open-is-not-defined-in-del

Lonami commented 5 years ago

The best solution is to properly disconnect the client, though. with client will handle it for you.

vjaychander commented 5 years ago

logging_conf.txt logging_error.txt

Below is my logger generating script

def set_logger(conf_path, log_handler, log_name):

    print("Conf Path", conf_path)
    """Setup logging configuration"""

    if os.path.exists(conf_path):
        with open(conf_path, 'rt') as f:
            config = json.load(f)

        log_path = os.path.join(constants.LOG_DIR, log_name)

        # logging file name with timestamp
        config["handlers"]["file_handler"]["filename"] = log_path

        logging.config.dictConfig(config)
        logger = logging.getLogger(log_handler)
        return logger

    else:
        print("Logger Configuration file not found..")
        sys.exit(1)

if __name__ == "__main__":

    now = datetime.datetime.now()
    timestamp = now.strftime("%Y%m%d")
    log_name = "cvm_adapter_{}.log".format(timestamp)
    logger = setup("configs\logging_conf.json", __file__, "test_log.log")

I've 6 modules, from each module I'm calling the above function to return the logger. Everytime the module completes execution I'm getting the same error as

--- Logging error ---
Traceback (most recent call last):
  File "/usr/lib64/python3.6/logging/handlers.py", line 71, in emit
    if self.shouldRollover(record):
  File "/usr/lib64/python3.6/logging/handlers.py", line 185, in shouldRollover
    self.stream = self._open()
  File "/usr/lib64/python3.6/logging/__init__.py", line 1061, in _open
    return open(self.baseFilename, self.mode, encoding=self.encoding)
NameError: name 'open' is not defined
Call stack:
  File "/usr/local/lib/python3.6/site-packages/smpplib/client.py", line 97, in __del__
    logger.warning('%s was not closed' % self)
Message: '<smpplib.client.Client object at 0x7fe4ed622550> was not closed'
Arguments: ()

I've tried to close the client object using with but still got the same error.

Any help is really appreciated (client project)

Lonami commented 5 years ago

@vjaychander nowhere in your traceback it says "telethon", so please keep this repository's issue tracker for Telethon-related issues only.