docs: add clarification about transaction characteristics attributes
They don't affect autocommit connections as they used to in psycopg2, so
note it as a difference from psycopg2. Add more explicit warning about
this limitation.
fix: fix possible errors calling __repr__ from __del__.
The errors get ignored but print a warning on program exit and eclipse a
genuine warning.
The error is caused by the `pq.misc` module getting gc'd on interpreter
shutdown before `connection_summary()` is called. The solution is to
import `connection_summary` in the module namespace, which is similar to
the solution that proved working for #198. It is less robust than the
solution used by the Python devs to import the function in the method
via an argument default, but it should work adequately (as nobody
complained about #198 anymore).
In #591 discussion, I proposed that connection_summary is too complex to
be considered safe to call on __del__. Actually, looking at it, it seems
innocent enough, as it only calls objects methods, no functions from
module namespaces. As a consequence we assume that this commit fixes the
issue. As I can't reproduce it, will ask the OP if this is the case.
fix: don't clobber a Python exception on COPY FROM with QueryCanceled
We trigger the server to raise the QueryCanceled; however, the original
exception has more information (the traceback). We can consider the
server exception just a notification that cancellation worked as
expected.
This is a mild change in behaviour, as the fixed tests state. However,
raising QueryCanceled is not explicitly documented and not part of a
strict interface, so we can probably change the exception raised without
needing to wait for psycopg 4.
James Johnston [Wed, 12 Jul 2023 00:08:10 +0000 (17:08 -0700)]
Update documentation related to async and Ctrl-C
Now that #543 is fixed, users shouldn't need to manually set up
signal handlers to cancel operations any more. Update the documentation
to reflect this.
The issue previously mentioned about '# type: ignore[arg-type]' in
rows.py got resolved.
The new '# type: ignore[comparison-overlap]' in test_pipeline*.py are
due to https://github.com/python/mypy/issues/15509, a known regression
from Mypy 1.4. We use the workaround documented in the release blog post
https://mypy-lang.blogspot.com/2023/06/mypy-140-released.html (section
"Narrowing Enum Values Using “==”").
Denis Laxalde [Fri, 19 May 2023 07:06:55 +0000 (09:06 +0200)]
fix: finish the PGconn upon connection failure
Attaching the non-finished PGconn to exceptions raised in connect() is
causing problem, as described in issue #565, because the PGconn might
not get finished soon enough in application code and the socket would
remain open.
On the other hand, just removing the pgconn attribute from Error would
be a breaking change and we'd loose the inspection features introduced
in commit 9220293dc023b757f2a57702c14b1460ff8f25b0.
As an alternative, we define a new PGconn implementation that is
error-specific: it captures all attributes of the original PGconn and
fails upon other operations (methods call). Some attributes have a
default value since they are not available in old PostgreSQL versions.
Finally, the (real) PGconn is finished before raising exception in
generators.connect().
Denis Laxalde [Tue, 16 May 2023 11:28:37 +0000 (13:28 +0200)]
tests: check that OperationalError raised by connect() holds a pgconn
Sort of a non-regression test for the part of commit 9220293dc023b757f2a57702c14b1460ff8f25b0 concerning generators. It used
roughly the same logic as tests/pq/test_pgconn.py::test_used_password()
to determine if the test connection needs a password and gets skipped
otherwise.
Denis Laxalde [Thu, 8 Jun 2023 09:21:42 +0000 (11:21 +0200)]
fix: always validate PrepareManager cache in pipeline mode
Previously, when processing results in pipeline mode
(BasePipeline._process_results()), we'd run
'cursor._check_results(results)' early before calling
_prepared.validate() with prepared statement information. However, if
this check step fails, for example if the pipeline got aborted due to a
previous error, the latter step (PrepareManager cache validation) was
not run.
We fix this by reversing the logic, and checking results last.
However, this is not enough, because the results processing logic in
BasePipeline._fetch_gen() or _communicate_gen(), which sequentially
walked through fetched results, would typically stop at the first
exception and thus possibly never go through the step of validating
PrepareManager cache if a previous error happened.
We fix that by making sure that *all* results are processed, possibly
capturing the first exception and then re-raising it. In both
_communicate_gen() and _fetch_gen(), we no longer store results in the
'to_process' like, but process then upon reception as this logic is no
longer needed.
Denis Laxalde [Thu, 8 Jun 2023 11:38:44 +0000 (13:38 +0200)]
docs: remove outdated comments in PrepareManager's docstrings
PrepareManager's methods maybe_add_to_cache() and validate() are said to
only be used in pipeline mode, but this is wrong as can be seen in
BaseCursor._maybe_prepare_gen(). (Comments are probably a left-over from
a prior implementation of the pipeline mode.)
fix: don't reuse the same Transformer in composite dumper
We need different dumpers because, in case a composite contains another
composite, we need to call `dump_sequence()` on different sequences, so
we row dumpers must be distinct.
Daniele Varrazzo [Thu, 16 Mar 2023 21:15:51 +0000 (22:15 +0100)]
fix(pool): reinforce handling of errors in queued clients
Unlike the async counterpart, I have no idea how to test this condition,
because I don't think you can cancel a Python thread. I could put the
pool and the first client in threads and use the main thread as queued
client, but it seems pretty convoluted.
However, an error handling in the same place that fixed #509 in the
async pools doesn't hurt.
Denis Laxalde [Sun, 5 Feb 2023 17:40:16 +0000 (18:40 +0100)]
test: use Windows asyncio policy in "non-asyncio" pool tests
In commit b6cc8343159fc0a27365e09a3beef06433f3f1b5, we drop the global
asyncio.set_event_loop_policy() for Windows in conftest.py, replacing it
with asyncio_options to be used by the anyio test runner. However, test
cases in test_pool_async_noasyncio.py are not "async def" so they would
not use that anyio runner (nor did they use pytest-asyncio's runner
before); but they need the event loop policy for Windows still.
Accordingly, we configure the loop from "anyio_backend_options" in
asyncio_run() fixture.
Denis Laxalde [Wed, 17 Aug 2022 13:59:31 +0000 (15:59 +0200)]
test: use anyio instead of pytest-asyncio
This is in preparation for adding support for other async libraries,
through anyio. AnyIO pytest plugin is used in replacement for
pytest-asyncio:
- either the pytest.mark.asyncio is replaced by pytest.mark.anyio, or,
- we rely on the 'anyio_backend' fixture that is pulled in 'aconn_cls'
fixture (and hence 'aconn') providing automatic detection for test
functions using it.
The 'anyio_backend' fixture is parametrized to only use asyncio and
selects the event loop policy we need on Windows platform as previously
done in pytest_sessionstart(), but only for Python version 3.8 or
higher.
This fixture is defined in main conftest.py, as well as in
pool/conftest.py since we'll change the former to support more async
backend while keeping the later asyncio-only for now.
Function test_concurrency_async.py::test_ctrl_c is no longer 'async'
because its code does not directly use asyncio (it's done through a
subprocess); but the 'async def' was needed before in order for
pytest-asyncio to run it since the test module had a global
pytest.mark.asyncio (and we were using the "auto" mode).
Denis Laxalde [Mon, 9 Jan 2023 20:26:46 +0000 (21:26 +0100)]
refactor: merge BaseCursor's _set_results() and _set_results_from_pipeline()
Per refactoring of previous commits, these are similar: the only
difference is the use of "first_batch" flag in the execute() branch when
not in pipeline mode, but in that case since there is actually only one
"batch", the behavior is actually preserved.
Since _set_results_from_pipeline() previously included a call to
_check_results() but _set_results() does not, that is reported in caller
(BasePipeline._process_results()).