Commit Graph

169 Commits

Author SHA1 Message Date
Mike Bayer 2db54ee92e Leave bytestring exception messages as bytestrings
Fixed a regression introduced in version 1.2 where a refactor
of the :class:`.SQLAlchemyError` base exception class introduced an
inappropriate coercion of a plain string message into Unicode under
python 2k, which is not handled by the Python interpreter for characters
outside of the platform's encoding (typically ascii).  The
:class:`.SQLAlchemyError` class now passes a bytestring through under
Py2K for ``__str__()`` as is the behavior of exception objects in general
under Py2K, does a safe coercion to unicode utf-8 with
backslash fallback for ``__unicode__()``.  For Py3K the message is
typically unicode already, but if not is again safe-coerced with utf-8
with backslash fallback for the ``__str__()`` method.

Fixes: #4429
Change-Id: I2289da3f2c45c7d0041fa43d838958f7614defc3
2019-01-10 18:02:00 -05:00
Mike Bayer 1e278de4cc Post black reformatting
Applied on top of a pure run of black -l 79 in
I7eda77fed3d8e73df84b3651fd6cfcfe858d4dc9,  this set of changes
resolves all remaining flake8 conditions for those codes
we have enabled in setup.cfg.

Included are resolutions for all remaining flake8 issues
including shadowed builtins, long lines, import order, unused
imports, duplicate imports, and docstring issues.

Change-Id: I4f72d3ba1380dd601610ff80b8fb06a2aff8b0fe
2019-01-06 18:23:11 -05:00
Mike Bayer 1e1a38e780 Run black -l 79 against all source files
This is a straight reformat run using black as is, with no edits
applied at all.

The black run will format code consistently, however in
some cases that are prevalent in SQLAlchemy code it produces
too-long lines.   The too-long lines will be resolved in the
following commit that will resolve all remaining flake8 issues
including shadowed builtins, long lines, import order, unused
imports, duplicate imports, and docstring issues.

Change-Id: I7eda77fed3d8e73df84b3651fd6cfcfe858d4dc9
2019-01-06 17:34:50 +00:00
Mike Bayer 0b0a4c8ba2 Handle PostgreSQL enums in remote schemas
Fixed issue where a :class:`.postgresql.ENUM` or a custom domain present
in a remote schema would not be recognized within column reflection if
the name of the enum/domain or the name of the schema required quoting.
A new parsing scheme now fully parses out quoted or non-quoted tokens
including support for SQL-escaped quotes.

Fixed issue where multiple :class:`.postgresql.ENUM` objects referred to
by the same :class:`.MetaData` object would fail to be created if
multiple objects had the same name under different schema names.  The
internal memoization the Postgresql dialect uses to track if it has
created a particular :class:`.postgresql.ENUM` in the database during
a DDL creation sequence now takes schema name into account.

Fixes: #4416
Change-Id: I8cf03069e10b12f409e9b6796e24fc5850979955
2018-12-21 13:53:16 -05:00
Korn, Uwe 85dd766853 Mention Properties keys in __dir__
The Python builtin ``dir()`` is now supported for a SQLAlchemy "properties"
object, such as that of a Core columns collection (e.g. ``.c``),
``mapper.attrs``, etc.  Allows iPython autocompletion to work as well.
Pull request courtesy Uwe Korn.

Change-Id: I8696729542d1b74a566642a3a63fd500f64588cd
Pull-request: https://github.com/zzzeek/sqlalchemy/pull/458

Mention Properties keys in __dir__

Change-Id: I88939955857c8df5eed0b87bc27c45357780b17d
2018-08-01 12:30:57 -04:00
Mike Bayer 1393eac44c Implement remove() for _empty_collection
Fixed regression caused in 1.2.3 due to fix from 🎫`4181` where
the changes to the event system involving :class:`.Engine` and
:class:`.OptionEngine` did not accommodate for event removals, which
would raise an ``AttributeError`` when invoked at the class
level.

Change-Id: I1c9083829d74dd710716d28b0eaca4fa15e86313
Fixes: #4190
2018-02-19 17:01:43 -05:00
Mike Bayer 064f82986c Implement an error lookup
Add codes to commonly raised error messages and classes
that link back to fixed documentation sections
giving background on these messages.

Change-Id: I78d0660add7026bb662e20305a59283b20616954
2017-12-27 10:33:22 -05:00
Mike Bayer 1a990ee332 Ensure Oracle index w/ col DESC etc. is reflected
Fixed bug where an index reflected under Oracle with an expression like
"column DESC" would not be returned, if the table also had no primary
key, as a result of logic that attempts to filter out the
index implicitly added by Oracle onto the primary key columns.

Reworked the "filter out the primary key index" logic in oracle
get_indexes() to be clearer.

This changeset also adds an internal check to ColumnCollection
to accomodate for the case of a column being added twice,
as well as adding a private _table argument to Index such that
reflection can specify the Table explicitly.  The _table
argument can become part of public API in a later revision
or release if needed.

Change-Id: I745711e03b3e450b7f31185fc70e10d3823063fa
Fixes: #4042
2017-08-14 01:18:06 -04:00
Khairi Hafsham 772374735d Make all tests to be PEP8 compliant
tested using pycodestyle version 2.2.0

Fixes: #3885
Change-Id: I5df43adc3aefe318f9eeab72a078247a548ec566
Pull-request: https://github.com/zzzeek/sqlalchemy/pull/343
2017-02-07 11:21:56 -05:00
Mike Bayer 665b92d83f Check for __module__ not present in util.wrap_callable()
The newly added wrap_callable() function assumes __module__
is present when this is not the case for objects such as
functools.partial.

Change-Id: Ia226260e9a65419e26d5c1f7187512f7fd4bb7c1
Fixes: #3823
2016-10-14 13:26:35 -04:00
Pieter Mulder fcfff77128 Repair pickling for Properties object
Fixed bug whereby the ``__getstate__`` / ``__setstate__``
methods for sqlalchemy.util.Properties were
non-working due to the transition in the 1.0 series to ``__slots__``.
The issue potentially impacted some third-party applications.
Pull request courtesy Pieter Mulder.

Fixes: #3728
Change-Id: I01ebd425bbfe145747fea2edd0d2d412c74fd84d
Pull-request: https://github.com/zzzeek/sqlalchemy/pull/286
(cherry picked from commit cab57e9bab04fbdea44690c08dff379a29eaab32)
2016-06-29 17:21:31 -04:00
Mike Bayer 01809bddff - Fixed bug in "to_list" conversion where a single bytes object
would be turned into a list of individual characters.  This would
impact among other things using the :meth:`.Query.get` method
on a primary key that's a bytes object.
fixes #3660
2016-02-21 20:52:43 -05:00
Mike Bayer 8a1e619fb2 - revert the change first made in a6fe4dc, as we are now generalizing
the warning here to all safe_reraise() cases in Python 2.
- Revisiting 🎫`2696`, first released in 1.0.10, which attempts to
work around Python 2's lack of exception context reporting by emitting
a warning for an exception that was interrupted by a second exception
when attempting to roll back the already-failed transaction; this
issue continues to occur for MySQL backends in conjunction with a
savepoint that gets unexpectedly lost, which then causes a
"no such savepoint" error when the rollback is attempted, obscuring
what the original condition was.

The approach has been generalized to the Core "safe
reraise" function which takes place across the ORM and Core in any
place that a transaction is being rolled back in response to an error
which occurred trying to commit, including the context managers
provided by :class:`.Session` and :class:`.Connection`, and taking
place for operations such as a failure on "RELEASE SAVEPOINT".
Previously, the fix was only in place for a specific path within
the ORM flush/commit process; it now takes place for all transational
context managers as well.
fixes #2696
2016-01-28 15:01:31 -05:00
Mike Bayer d4d9a65248 - Fixed bug where some exception re-raise scenarios would attach
the exception to itself as the "cause"; while the Python 3 interpreter
is OK with this, it could cause endless loops in iPython.
fixes #3625
- add tests for reraise, raise_from_cause
- raise_from_cause is the same on py2k/3k, use just one function
2016-01-18 17:35:44 -05:00
Mike Bayer 1f7a1f777d - A deep improvement to the recently added :meth:.TextClause.columns
method, and its interaction with result-row processing, now allows
the columns passed to the method to be positionally matched with the
result columns in the statement, rather than matching on name alone.
The advantage to this includes that when linking a textual SQL statement
to an ORM or Core table model, no system of labeling or de-duping of
common column names needs to occur, which also means there's no need
to worry about how label names match to ORM columns and so-forth.  In
addition, the :class:`.ResultProxy` has been further enhanced to
map column and string keys to a row with greater precision in some
cases.  fixes #3501
- reorganize the initialization of ResultMetaData for readability
and complexity; use the name "cursor_description", define the
task of "merging" cursor_description with compiled column information
as its own function, and also define "name extraction" as a separate task.
- fully change the name we use in the "ambiguous column" error to be the
actual name that was ambiguous, modify the C ext also
2016-01-14 18:06:26 -05:00
Mike Bayer d533b8e922 - The ORM and Core tutorials, which have always been in doctest format,
are now exercised within the normal unit test suite in both Python
2 and Python 3.
- remove the old testdocs.py runner and replace with test/base/test_tutorials.py
- use pytest's unicode fixer so that we can test for unicode strings
in both py2k/3k
- use py3k format overall for prints, exceptions
- add other fixers to guarantee deterministic results
- add skips and ellipses to outputs that aren't worth matching
2015-12-10 17:09:47 -05:00
Iuri de Silvio d9178320d6 Fix _assert_unorderable_types on py36.
The error message changed to:

`'<' not supported between instances of 'foo' and 'bar'`
2015-11-30 14:50:25 -02:00
Mike Bayer e31211c578 - convert wrap_callable() to a general purpose update_wrapper-like
function; the caller still passes in the "wrapper"
- move tests for wrap_callable() to be generic util tests
- changelog for pullreq github:204
2015-10-29 14:28:00 -04:00
Mike Bayer 575f080850 - Fixed an issue where a particular base class within utils
didn't implement ``__slots__``, and therefore meant all subclasses
of that class didn't either, negating the rationale for ``__slots__``
to be in use.  Didn't cause any issue except on IronPython
which apparently does not implement ``__slots__`` behavior compatibly
with cPython.
Fixes #3494
2015-07-20 20:35:04 -04:00
Mike Bayer 657be357de - Fixed an internal "memoization" routine for method types such
that a Python descriptor is no longer used; repairs inspectability
of these methods including support for Sphinx documentation.
2015-06-08 18:36:27 -04:00
Mike Bayer d178707eca - Added support for the case of the misbehaving DBAPI that has
pep-249 exception names linked to exception classes of an entirely
different name, preventing SQLAlchemy's own exception wrapping from
wrapping the error appropriately.
The SQLAlchemy dialect in use needs to implement a new
accessor :attr:`.DefaultDialect.dbapi_exception_translation_map`
to support this feature; this is implemented now for the py-postgresql
dialect.
fixes #3421
2015-05-15 12:38:28 -04:00
Sebastian Bank e0d7f52439 unit-test deterministic topological sort 2015-03-07 19:36:41 +01:00
Mike Bayer b3bf7915b5 - scale back _Dispatch and _JoinedDispatcher to use a __getitem__ scheme
to start up listener collections; this pulls the overhead off of construction
and makes performance much like the descriptor version, while still allowing
slots.   Fix up some profiles.
2015-01-04 19:05:31 -05:00
Mike Bayer cf27232563 - wip - start factoring events so that we aren't using descriptors for dispatch,
allowing us to move to __slots__
2015-01-04 18:06:02 -05:00
Mike Bayer de11f94982 - The :meth:.PropComparator.of_type modifier has been
improved in conjunction with loader directives such as
:func:`.joinedload` and :func:`.contains_eager` such that if
two :meth:`.PropComparator.of_type` modifiers of the same
base type/path are encountered, they will be joined together
into a single "polymorphic" entity, rather than replacing
the entity of type A with the one of type B.  E.g.
a joinedload of ``A.b.of_type(BSub1)->BSub1.c`` combined with
joinedload of ``A.b.of_type(BSub2)->BSub2.c`` will create a
single joinedload of ``A.b.of_type((BSub1, BSub2)) -> BSub1.c, BSub2.c``,
without the need for the ``with_polymorphic`` to be explicit
in the query.
fixes #3256
2014-11-24 18:49:32 -05:00
Mike Bayer 6f40eb37cb - Exception messages have been spiffed up a bit. The SQL statement
and parameters are not displayed if None, reducing confusion for
error messages that weren't related to a statement.  The full
module and classname for the DBAPI-level exception is displayed,
making it clear that this is a wrapped DBAPI exception.  The
statement and parameters themselves are bounded within a bracketed
sections to better isolate them from the error message and from
each other.
fixes #3172
2014-10-17 19:37:45 -04:00
Mike Bayer 2924f8685c pep8 2014-10-17 19:29:32 -04:00
Mike Bayer fb09ad7551 - The `__module__` attribute is now set for all those SQL and
ORM functions that are derived as "public factory" symbols, which
should assist with documentation tools being able to report on the
target module.
fixes #3218
2014-10-14 11:59:48 -04:00
Mike Bayer 0426d174e4 - the test_except test was doing an unnecessary workaround of some kind,
take that out, restore the better exception logic in exc
2014-10-12 19:37:14 -04:00
Mike Bayer b36cdefba2 - Fixed bug that affected generally the same classes of event
as that of 🎫`3199`, when the ``named=True`` parameter
would be used.  Some events would fail to register, and others
would not invoke the event arguments correctly, generally in the
case of when an event was "wrapped" for adaption in some other way.
The "named" mechanics have been rearranged to not interfere with
the argument signature expected by internal wrapper functions.
fixes #3197
2014-09-18 17:49:07 -04:00
Mike Bayer 9ae4db27b9 - Fixed bug that affected many classes of event, particularly
ORM events but also engine events, where the usual logic of
"de duplicating" a redundant call to :func:`.event.listen`
with the same arguments would fail, for those events where the
listener function is wrapped.  An assertion would be hit within
registry.py.  This assertion has now been integrated into the
deduplication check, with the added bonus of a simpler means
of checking deduplication across the board.
fixes #3199
2014-09-18 15:24:40 -04:00
Mike Bayer 685a014c64 - A new implementation for :class:.KeyedTuple used by the
:class:`.Query` object offers dramatic speed improvements when
fetching large numbers of column-oriented rows.
fixes #3176
2014-08-28 12:25:21 -04:00
Mike Bayer 4a4cccfee5 - Removing (or adding) an event listener at the same time that the event
is being run itself, either from inside the listener or from a
concurrent thread, now raises a RuntimeError, as the collection used is
now an instance of ``colletions.deque()`` and does not support changes
while being iterated.  Previously, a plain Python list was used where
removal from inside the event itself would produce silent failures.
fixes #3163
2014-08-14 14:40:28 -04:00
Mike Bayer c0c6aaa58a pep8 2014-08-14 14:21:03 -04:00
Mike Bayer 326a3d85f7 - fix unit test affected by #3075 2014-07-29 19:09:48 -04:00
Mike Bayer 009df6a3d0 - Added a new keyword argument `once=True to :func:.event.listen`
and :func:`.event.listens_for`.  This is a convenience feature which
will wrap the given listener such that it is only invoked once.
2014-03-11 12:27:10 -04:00
Mike Bayer c9843bb395 put fails for these various things that don't work on pypy, not sure what approach
should be taken
2014-03-08 13:52:27 -05:00
Mike Bayer 66470d8656 fix pypy failures 2014-03-03 16:18:09 -05:00
Mike Bayer bf89ca2e10 - get util.get_callable_argspec() to be completely bulletproof for 2.6-3.4,
methods, classes, builtins, functools.partial(), everything known so far
- use get_callable_argspec() within ColumnDefault._maybe_wrap_callable, re: #2979
2014-03-02 13:59:06 -05:00
Mike Bayer c2f86c92b1 restore the contracts of update/extend to the degree that the same column identity
isn't appended to the list.  reflection makes use of this.
2014-02-27 13:50:47 -05:00
Mike Bayer 302ad6228a - Some changes to how the :attr:.FromClause.c collection behaves
when presented with duplicate columns.  The behavior of emitting a
warning and replacing the old column with the same name still
remains to some degree; the replacement in particular is to maintain
backwards compatibility.  However, the replaced column still remains
associated with the ``c`` collection now in a collection ``._all_columns``,
which is used by constructs such as aliases and unions, to deal with
the set of columns in ``c`` more towards what is actually in the
list of columns rather than the unique set of key names.  This helps
with situations where SELECT statements with same-named columns
are used in unions and such, so that the union can match the columns
up positionally and also there's some chance of :meth:`.FromClause.corresponding_column`
still being usable here (it can now return a column that is only
in selectable.c._all_columns and not otherwise named).
The new collection is underscored as we still need to decide where this
list might end up.   Theoretically it
would become the result of iter(selectable.c), however this would mean
that the length of the iteration would no longer match the length of
keys(), and that behavior needs to be checked out.
fixes #2974
- add a bunch more tests for ColumnCollection
2014-02-26 15:34:49 -05:00
Mike Bayer e60529da79 - Fixed bug where events set to listen at the class
level (e.g. on the :class:`.Mapper` or :class:`.ClassManager`
level, as opposed to on an individual mapped class, and also on
:class:`.Connection`) that also made use of internal argument conversion
(which is most within those categories) would fail to be removable.
fixes #2973
2014-02-25 16:55:42 -05:00
Mike Bayer c450cd6cb6 - Fixed regression where using a `functools.partial()` with the event
system would cause a recursion overflow due to usage of inspect.getargspec()
on it in order to detect a legacy calling signature for certain events,
and apparently there's no way to do this with a partial object.  Instead
we skip the legacy check and assume the modern style; the check itself
now only occurs for the SessionEvents.after_bulk_update and
SessionEvents.after_bulk_delete events.  Those two events will require
the new signature style if assigned to a "partial" event listener.
[ticket:2905]
2014-01-04 00:35:48 -05:00
Mike Bayer 2692238f45 - Improvements to the system by which SQL types generate within
``__repr__()``, particularly with regards to the MySQL integer/numeric/
character types which feature a wide variety of keyword arguments.
The ``__repr__()`` is important for use with Alembic autogenerate
for when Python code is rendered in a migration script.
[ticket:2893]
2013-12-18 18:26:15 -05:00
Mike Bayer 3d4d933329 - add support for removal of instance methods as event listeners, taking
into account the id() of the function itself and self, [ticket:2832]
2013-10-01 12:14:34 -04:00
Mike Bayer 9769628ac1 - add WeakSequence.append()
- fix and test weakref cleanout for WeakSequence, [ticket:2794]
2013-08-18 16:05:38 -04:00
Mike Bayer 4505425a38 - Removal of event listeners is now implemented. The feature is
provided via the :func:`.event.remove` function.
[ticket:2268]
- reorganization of event.py module into a package; with the addition of the
docstring work as well as the new registry for removal, there's a lot more code now.
the package separates concerns and provides a top-level doc for each subsection
of functionality
- the remove feature works by providing the EventKey object which associates
the user-provided arguments to listen() with a global, weak-referencing registry.
This registry stores a collection of _ListenerCollection and _DispatchDescriptor
objects associated with each set of arguments, as well as the wrapped function
which was applied to that collection.  The EventKey can then be recreated for
a removal, all the _ListenerCollection and _DispatchDescriptor objects are located,
and the correct wrapped function is removed from each one.
2013-07-26 14:21:58 -04:00
Mike Bayer 02a81707dc - create a new system where we can decorate an event method
with @_legacy_signature, will inspect incoming listener functions
to see if they match an older signature, will wrap into a newer sig
- add an event listen argument named=True, will send all args as
kw args so that event listeners can be written with **kw, any combination
of names
- add a doc system to events that writes out the various calling styles
for a given event, produces deprecation messages automatically.
a little concerned that it's a bit verbose but will look at it up
on RTD for awhile to get a feel.
- change the calling signature for bulk update/delete events - we have
the BulkUD object right there, and there's at least six or seven things
people might want to see, so just send the whole BulkUD in
[ticket:2775]
2013-07-08 13:39:56 -04:00
Mike Bayer b38a76cd1d - replace most explicitly-named test objects called "Mock..." with
actual mock objects from the mock library.  I'd like to use mock
for new tests so we might as well use it in obvious places.
- use unittest.mock in py3.3
- changelog
- add a note to README.unittests
- add tests_require in setup.py
- have tests import from sqlalchemy.testing.mock
- apply usage of mock to one of the event tests.  we can be using
this approach all over the place.
2013-06-30 18:35:12 -04:00
Mike Bayer 0d9ec9fe84 remove all remaining start/end py2k/py3k blocks 2013-06-07 17:51:12 -04:00