|
14 | 14 | """ |
15 | 15 |
|
16 | 16 | import atexit |
| 17 | +import importlib |
17 | 18 | import os |
18 | 19 | import signal |
19 | 20 | import sys |
@@ -295,27 +296,27 @@ def initSessions(self): |
295 | 296 | moduleName = setting('SessionModule') |
296 | 297 | className = moduleName.rpartition('.')[2] |
297 | 298 | try: |
298 | | - exec(f'from {moduleName} import {className}') |
299 | | - cls = locals()[className] |
| 299 | + module = importlib.import_module(moduleName) |
| 300 | + cls = getattr(module, className) |
300 | 301 | if not isinstance(cls, type): |
301 | | - raise ImportError |
| 302 | + raise ImportError(f'{cls!r} is not a type') |
302 | 303 | self._sessionClass = cls |
303 | | - except ImportError: |
| 304 | + except (ImportError, AttributeError) as err: |
304 | 305 | print(f"ERROR: Could not import Session class '{className}'" |
305 | | - f" from module '{moduleName}'") |
| 306 | + f" from module '{moduleName}':\n{err}") |
306 | 307 | self._sessionClass = None |
307 | 308 | moduleName = setting('SessionStore') |
308 | 309 | if moduleName in ( |
309 | 310 | 'Dynamic', 'File', 'Memcached', 'Memory', 'Redis', 'Shelve'): |
310 | 311 | moduleName = f'Session{moduleName}Store' |
311 | 312 | className = moduleName.rpartition('.')[2] |
312 | 313 | try: |
313 | | - exec(f'from {moduleName} import {className}') |
314 | | - cls = locals()[className] |
| 314 | + module = importlib.import_module(moduleName) |
| 315 | + cls = getattr(module, className) |
315 | 316 | if not isinstance(cls, type): |
316 | | - raise ImportError |
| 317 | + raise ImportError(f'{cls!r} is not a type') |
317 | 318 | self._sessions = cls(self) |
318 | | - except ImportError as err: |
| 319 | + except (ImportError, AttributeError) as err: |
319 | 320 | print(f"ERROR: Could not import SessionStore class '{className}'" |
320 | 321 | f" from module '{moduleName}':\n{err}") |
321 | 322 | self._sessions = None |
|
0 commit comments