diff options
author | J. Nick Koston <nick@koston.org> | 2023-04-19 18:39:18 -0400 |
---|---|---|
committer | Federico Caselli <cfederico87@gmail.com> | 2023-04-26 20:19:17 +0200 |
commit | ff198e35f0e04b8d38df25df234e72259069b4d1 (patch) | |
tree | c48db9a0366b48c8caaa35ad9ab83a354aaa7d32 /lib/sqlalchemy/engine/row.py | |
parent | 9f675fd042b05977f1b38887c2fbbb54ecd424f7 (diff) | |
download | sqlalchemy-ff198e35f0e04b8d38df25df234e72259069b4d1.tar.gz |
Prebuild the row string to position lookup for Rows
Improved :class:`_engine.Row` implementation to optimize
``__getattr__`` performance.
The serialization of a :class:`_engine.Row` to pickle has changed with
this change. Pickle saved by older SQLAlchemy versions can still be loaded,
but new pickle saved by this version cannot be loaded by older ones.
Fixes: #9678
Closes: #9668
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/9668
Pull-request-sha: 86b8ccd1959dbd91b1208f7a648a91f217e1f866
Change-Id: Ia85c26a59e1a57ba2bf0d65578c6168f82a559f2
Diffstat (limited to 'lib/sqlalchemy/engine/row.py')
-rw-r--r-- | lib/sqlalchemy/engine/row.py | 28 |
1 files changed, 3 insertions, 25 deletions
diff --git a/lib/sqlalchemy/engine/row.py b/lib/sqlalchemy/engine/row.py index e15ea7b17..4b767da09 100644 --- a/lib/sqlalchemy/engine/row.py +++ b/lib/sqlalchemy/engine/row.py @@ -34,12 +34,8 @@ from ..util._has_cy import HAS_CYEXTENSION if TYPE_CHECKING or not HAS_CYEXTENSION: from ._py_row import BaseRow as BaseRow - from ._py_row import KEY_INTEGER_ONLY - from ._py_row import KEY_OBJECTS_ONLY else: from sqlalchemy.cyextension.resultproxy import BaseRow as BaseRow - from sqlalchemy.cyextension.resultproxy import KEY_INTEGER_ONLY - from sqlalchemy.cyextension.resultproxy import KEY_OBJECTS_ONLY if TYPE_CHECKING: from .result import _KeyType @@ -80,8 +76,6 @@ class Row(BaseRow, Sequence[Any], Generic[_TP]): __slots__ = () - _default_key_style = KEY_INTEGER_ONLY - def __setattr__(self, name: str, value: Any) -> NoReturn: raise AttributeError("can't set attribute") @@ -134,24 +128,12 @@ class Row(BaseRow, Sequence[Any], Generic[_TP]): .. versionadded:: 1.4 """ - return RowMapping( - self._parent, - None, - self._keymap, - RowMapping._default_key_style, - self._data, - ) + return RowMapping(self._parent, None, self._key_to_index, self._data) def _filter_on_values( self, filters: Optional[Sequence[Optional[_ResultProcessorType[Any]]]] ) -> Row[Any]: - return Row( - self._parent, - filters, - self._keymap, - self._key_style, - self._data, - ) + return Row(self._parent, filters, self._key_to_index, self._data) if not TYPE_CHECKING: @@ -198,9 +180,7 @@ class Row(BaseRow, Sequence[Any], Generic[_TP]): def __getitem__(self, index: slice) -> Sequence[Any]: ... - def __getitem__( - self, index: Union[int, slice] - ) -> Union[Any, Sequence[Any]]: + def __getitem__(self, index: Union[int, slice]) -> Any: ... def __lt__(self, other: Any) -> bool: @@ -337,8 +317,6 @@ class RowMapping(BaseRow, typing.Mapping["_KeyType", Any]): __slots__ = () - _default_key_style = KEY_OBJECTS_ONLY - if TYPE_CHECKING: def __getitem__(self, key: _KeyType) -> Any: |