From 47d9de98ed6b62d8017ac6ec3a94c1bd30883ffa Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Wed, 13 Feb 2019 08:51:26 +0100 Subject: [PATCH] Raise ImportError and not ValueError when sqlite3 is missing Excepting ValueError when creating the SQLiteSession could hide other errors (e.g. using a newer session file on an older version of the library). Instead, the original error is raised, as if sqlite3 was being imported within its __init__ method. --- telethon/client/telegrambaseclient.py | 2 +- telethon/sessions/sqlite.py | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/telethon/client/telegrambaseclient.py b/telethon/client/telegrambaseclient.py index 02cb05e9..56e1926f 100644 --- a/telethon/client/telegrambaseclient.py +++ b/telethon/client/telegrambaseclient.py @@ -202,7 +202,7 @@ class TelegramBaseClient(abc.ABC): if isinstance(session, str) or session is None: try: session = SQLiteSession(session) - except ValueError: + except ImportError: import warnings warnings.warn( 'The sqlite3 module is not available under this ' diff --git a/telethon/sessions/sqlite.py b/telethon/sessions/sqlite.py index ebb773e3..f01843dc 100644 --- a/telethon/sessions/sqlite.py +++ b/telethon/sessions/sqlite.py @@ -14,8 +14,10 @@ from ..tl.types import ( try: import sqlite3 -except ImportError: + sqlite3_err = None +except ImportError as e: sqlite3 = None + sqlite3_err = type(e) EXTENSION = '.session' CURRENT_VERSION = 5 # database version @@ -32,7 +34,7 @@ class SQLiteSession(MemorySession): def __init__(self, session_id=None): if sqlite3 is None: - raise ValueError('sqlite3 is not installed') + raise sqlite3_err super().__init__() self.filename = ':memory:'