|
7 | 7 |
|
8 | 8 | from .crud import CRUDModel
|
9 | 9 | from .declarative import declarative_base, declared_attr
|
| 10 | +from .exceptions import UninitializedError |
10 | 11 | from .schema import GinoSchemaVisitor, patch_schema
|
11 | 12 | from . import json_support
|
12 | 13 |
|
@@ -361,6 +362,9 @@ def bind(self):
|
361 | 362 | :class:`~sqlalchemy.engine.url.URL`).
|
362 | 363 |
|
363 | 364 | """
|
| 365 | + if self._bind is None: |
| 366 | + return _PlaceHolder( |
| 367 | + UninitializedError('Gino engine is not initialized.')) |
364 | 368 | return self._bind
|
365 | 369 |
|
366 | 370 | # noinspection PyMethodOverriding,PyAttributeOutsideInit
|
@@ -483,3 +487,20 @@ def transaction(self, *args, **kwargs):
|
483 | 487 |
|
484 | 488 | """
|
485 | 489 | return self.bind.transaction(*args, **kwargs)
|
| 490 | + |
| 491 | + |
| 492 | +class _PlaceHolder: |
| 493 | + __slots__ = '_exception' |
| 494 | + |
| 495 | + def __init__(self, exception): |
| 496 | + self._exception = exception |
| 497 | + |
| 498 | + def __getattribute__(self, item): |
| 499 | + if item == '_exception': |
| 500 | + return super().__getattribute__(item) |
| 501 | + raise self._exception |
| 502 | + |
| 503 | + def __setattr__(self, key, value): |
| 504 | + if key == '_exception': |
| 505 | + return super().__setattr__(key, value) |
| 506 | + raise UninitializedError(self._exception) |
0 commit comments