Commit Graph

60 Commits

Author SHA1 Message Date
Jon Dufresne
05c28cce78 Remove workarounds for namedtuple on Python <= 2.5
namedtuple is available on all Python versions supported by psycopg2. It
was first introduced in Python 2.6. Can remove all workarounds and
special documentation.
2017-11-26 13:55:30 -08:00
Daniele Varrazzo
8e28444897 Bunch of test tweaks to make the test grid green 2017-03-14 14:15:52 +00:00
Greg Ward
12317557da Always raise OperationalError when connection was closed externally.
From the DB-API (https://www.python.org/dev/peps/pep-0249/):

  OperationalError

  Exception raised for errors that are related to the database's
  operation and not necessarily under the control of the programmer,
  e.g. an unexpected disconnect occurs, [...]

Additionally, psycopg2 was inconsistent, at least in the async case:
depending on how the "connection closed" error was reported from the
kernel to libpq, it would sometimes raise OperationalError and
sometimes DatabaseError. Now it always raises OperationalError.
2017-03-14 12:14:00 +00:00
Greg Ward
b203a7c775 Always detect when a connection is closed behind psycopg2's back.
There's a race condition that only seems to happen over Unix-domain
sockets. Sometimes, the closed socket is reported by the kernel to
libpq like this (captured with strace):

  sendto(3, "Q\0\0\0\34select pg_backend_pid()\0", 29, MSG_NOSIGNAL, NULL, 0) = 29
  recvfrom(3, "E\0\0\0mSFATAL\0C57P01\0Mterminating "..., 16384, 0, NULL, NULL) = 110
  recvfrom(3, 0x12d0330, 16384, 0, 0, 0)  = -1 ECONNRESET (Connection reset by peer)

That is, psycopg2/libpq sees no error when sending the first query
after the connection is closed, but gets an error reading the result.
In that case, everything worked fine.

But sometimes, the error manifests like this:

  sendto(3, "Q\0\0\0\34select pg_backend_pid()\0", 29, MSG_NOSIGNAL, NULL, 0) = -1 EPIPE (Broken pipe)
  recvfrom(3, "E\0\0\0mSFATAL\0C57P01\0Mterminating "..., 16384, 0, NULL, NULL) = 110
  recvfrom(3, "", 16274, 0, NULL, NULL)   = 0
  recvfrom(3, "", 16274, 0, NULL, NULL)   = 0

i.e. libpq received an error when sending the query. This manifests as
a slightly different exception from a slightly different place. More
importantly, in this case connection.closed is left at 0 rather than
being set to 2, and that is the bug I'm fixing here.

Note that we see almost identical behaviour for sync and async
connections, and the fixes are the same. So I added extremely similar
test cases.

Finally, there is still a bug here: for async connections, we
sometimes raise DatabaseError (incorrect) and sometimes raise
OperationalError (correct). Will fix that next.
2017-03-14 12:08:03 +00:00
Daniele Varrazzo
3c124a0b87 Flake8 complaints 2017-03-14 12:06:46 +00:00
Daniele Varrazzo
3873c6c09f Deal consistently with E'' quotes in tests 2017-02-06 21:40:05 +00:00
Daniele Varrazzo
f24de0357f Allow skipping the slow test
It's not so much about tests being slow: some just get stuck and timeout
travis.

Skipped all tests taking about more than 0.2s to run on my laptop.
Fast testing takes about 8s instead of 24.
2017-02-02 01:53:50 +00:00
Daniele Varrazzo
faaef61c27 Merge branch 'master' into named-callproc 2016-12-26 01:49:42 +01:00
Daniele Varrazzo
05627ac0f9 Fix unicode mogrify test on python 2 2016-10-11 00:22:23 +01:00
Daniele Varrazzo
91d2158de7 Python source cleanup using flake8 2016-10-11 00:11:55 +01:00
Daniele Varrazzo
78649f8e90 Dropped use of b() "macro" and 2to3 fixer
Just use the b"" strings syntax supported from python 2.6.
2016-08-15 01:56:36 +01:00
Daniele Varrazzo
7302f348bc Added test with objects without length as callproc param 2015-12-12 17:51:08 -05:00
Daniele Varrazzo
d297976d6d Raise TypeError if the dict in callproc param contains non-strings
Check-and-conversion chain fixed and simplified. 'spname' was a
reference leak.
2015-12-12 17:51:08 -05:00
mrmilosz
c205f140a0 callproc: tests, docs, and comment/error-reporting touchups. 2015-12-12 17:49:59 -05:00
Owen Raccuglia
5af5fb4cc6 Allow pickling of cursor.description
This is for people using dtuple.py; a dtuple.DatabaseTuple instance
keeps a reference to cursor.description, which is not picklable because
psycopg2 doesn't export the Column namedtuple it uses.

This commit exports the Column namedtuple, and includes a test to verify
the pickle/unpickle works after exporting Column.
2015-02-08 02:31:33 +00:00
Daniele Varrazzo
c81522079e Added test to verify withhold cursors work in autocommit 2014-08-21 05:35:11 +01:00
Daniele Varrazzo
283a422b4d Added test to verify withhold transaction behaviour
A withhold cursor can read its data when the transaction is closed, so
it shouldn't start a new one upon movement/close.
2014-08-21 05:35:10 +01:00
Daniele Varrazzo
e076e935b9 Added test with objects without length as callproc param 2014-06-06 21:42:20 +02:00
Daniele Varrazzo
7b82be936d Don't segfault on uninitialized cursor
It can happen with bad cursor subclasses not calling super's init. Raise
an exception instead of segfaulting.

Closes #195
2014-02-26 19:41:59 +00:00
Daniele Varrazzo
2e3833f7fb Really test named cursors in test_scroll_named() 2013-10-16 19:10:21 +01:00
Daniele Varrazzo
a44db81d9b Skip tests on python implementations without getrefcount()
PyPy is one of these.
2013-05-06 10:39:24 +01:00
Daniele Varrazzo
0e06addc9f Testing boilerplate unified in a single base class
The class makes a connection always available, allows creating
new connection and closes everything on tear down.
2013-04-07 00:23:30 +01:00
Daniele Varrazzo
a79a5292e7 Added support with cursors without scroll clause
Using nothing is different from NO SCROLL, see DECLARE notes
in PG docs.
2012-08-15 09:44:44 +01:00
Daniele Varrazzo
d074b096be Added test for scrollable cursor 2012-08-15 01:11:26 +01:00
Daniele Varrazzo
27421f1e41 Name can be passed as None to cursor()
Makes invocation from subclasses and generic code easier.

Code simplified by using default values for keyword arguments
and avoiding needless conversions back and forth between Python and C
strings. Also added connection type check to cursor's init.
2012-04-11 17:32:10 +01:00
Daniele Varrazzo
84f2a370f6 close() methods don't raise errors if called on closed objects 2012-03-04 05:10:07 +00:00
Daniele Varrazzo
7221ea9ec5 Added test to check rowcount behaves fine during named cursor iteration
Actually *it doesn't*: once we iterate the first itersize records, rowcount
is reset to zero. If we want to fix it we need an extra member in the
cursor.
2012-02-23 22:04:22 +00:00
Daniele Varrazzo
ede0e145dd Added tests for cursor scroll 2011-12-26 22:00:25 +01:00
Daniele Varrazzo
ad3a198919 Fixed reference leak with arguments referenced more than once in queries
Plus, some more care in objects life cycle, mostly in exceptions handling.

Closes ticket #81.
2011-12-11 02:52:06 +00:00
Daniele Varrazzo
37a9eb3615 Test and document the named cursor stealing technique 2011-10-14 23:17:24 +01:00
Daniele Varrazzo
e4424bdfdc Fixed tests to run with antebellum Postgres versions 2011-10-05 00:12:35 +01:00
Federico Di Gregorio
880aa07a58 WITH HOLD documentation a argument parsing changes
Now any true value will do for the withhold parameter.
2011-08-10 19:21:12 +02:00
Federico Di Gregorio
479bdf7458 New 'withhold' parameter for connection.cursor() 2011-08-10 18:25:46 +02:00
Federico Di Gregorio
2f6336ea78 First try at curs.withhold implementation 2011-07-05 10:28:34 +02:00
Daniele Varrazzo
fcbe0466a6 Correctly detect an empty query sent to the backend
Closes ticket #46.
2011-03-04 20:30:43 +00:00
Daniele Varrazzo
c01a7edbf4 Fixed test for execution with older PostgreSQL versions 2011-02-23 08:43:01 +00:00
Daniele Varrazzo
1db9c9b8ce The cursor name can be a non-valid PostgreSQL identifier 2011-02-23 01:53:25 +00:00
Daniele Varrazzo
c620f18be1 Provide cursor.description as named tuple if possible
If namedtuple() is not available, use regular tuples.
2011-02-19 00:05:43 +00:00
Daniele Varrazzo
99b3c72312 Some cleanup in mogrify
- Raise an exception on incomplete placeholders.
- Minor speedups.
- Don't change the string in place (??!!) if the placeholder is not s
  and the value is null.

The latter point can be done because downstream we don't accept anything
different from s anyway (in the Bytes_Format function).

Notice that now the format string is constant whatever the arguments.
This means that executemany is still more inefficient than it should be
as mogrify may work only on the parameters. However this is an
implementation only worthwhile if we start supporting real parameters.

Let's talk about that for the next release.
2011-02-18 02:33:42 +00:00
Daniele Varrazzo
63ac6cdde5 Added cursor.itersize
The value is used to control the number of records to fetch per network
roundtrip in named cursors iteration. Used to avoid the inefficient
arraysize default of 1 without giving this value the magic meaning of
2000.
2011-02-17 12:36:02 +00:00
Daniele Varrazzo
c96ba553da Cleanup of skipping of testing methods on certain Py/PG versions 2011-02-15 17:11:07 +00:00
Daniele Varrazzo
be22dfb765 Skip test if clock_timestamp function is not available 2011-02-15 15:25:14 +00:00
Daniele Varrazzo
9e9c221637 Work around a 2to3 next fixer bug in a test 2011-02-11 17:54:41 +00:00
Federico Di Gregorio
da27142882 Merge branch 'python3' into python2 2011-02-06 16:47:05 +01:00
Daniele Varrazzo
fab31e9441 Fetch 'arraysize' records per roundtrip in named cursors iteration
Closes ticket #33.
2011-02-05 15:24:00 +01:00
Daniele Varrazzo
8a1de1ec4e Added test to verify named cursor efficiency.
Iter shouldn't fetch one record at time.
2011-02-01 03:01:47 +00:00
Daniele Varrazzo
79048ff19a Merge branch 'python2' into python3
Conflicts:
	NEWS-2.3
	tests/__init__.py
	tests/test_lobject.py
	tests/test_quote.py
	tests/testutils.py
2011-01-10 00:46:02 +00:00
Daniele Varrazzo
15a09da96d Added license to unit tests
As the test suite is now part of the source distribution.
2011-01-09 23:44:35 +00:00
Daniele Varrazzo
7c1b03bd45 All tests made executable. 2011-01-07 00:58:29 +00:00
Daniele Varrazzo
80bd6e2794 Merge branch 'python2' into python3
Conflicts:
	NEWS-2.3
	psycopg/connection_type.c
	tests/test_connection.py
	tests/types_basic.py
2011-01-03 21:43:02 +01:00