mirror of
https://github.com/psycopg/psycopg2.git
synced 2025-07-01 10:13:05 +03:00
Python source cleanup using flake8
This commit is contained in:
parent
4458c9b4c9
commit
91d2158de7
|
@ -47,19 +47,20 @@ Homepage: http://initd.org/projects/psycopg2
|
|||
|
||||
# Import the DBAPI-2.0 stuff into top-level module.
|
||||
|
||||
from psycopg2._psycopg import BINARY, NUMBER, STRING, DATETIME, ROWID
|
||||
from psycopg2._psycopg import ( # noqa
|
||||
BINARY, NUMBER, STRING, DATETIME, ROWID,
|
||||
|
||||
from psycopg2._psycopg import Binary, Date, Time, Timestamp
|
||||
from psycopg2._psycopg import DateFromTicks, TimeFromTicks, TimestampFromTicks
|
||||
Binary, Date, Time, Timestamp,
|
||||
DateFromTicks, TimeFromTicks, TimestampFromTicks,
|
||||
|
||||
from psycopg2._psycopg import Error, Warning, DataError, DatabaseError, ProgrammingError
|
||||
from psycopg2._psycopg import IntegrityError, InterfaceError, InternalError
|
||||
from psycopg2._psycopg import NotSupportedError, OperationalError
|
||||
Error, Warning, DataError, DatabaseError, ProgrammingError, IntegrityError,
|
||||
InterfaceError, InternalError, NotSupportedError, OperationalError,
|
||||
|
||||
from psycopg2._psycopg import _connect, apilevel, threadsafety, paramstyle
|
||||
from psycopg2._psycopg import __version__, __libpq_version__
|
||||
_connect, apilevel, threadsafety, paramstyle,
|
||||
__version__, __libpq_version__,
|
||||
)
|
||||
|
||||
from psycopg2 import tz
|
||||
from psycopg2 import tz # noqa
|
||||
|
||||
|
||||
# Register default adapters.
|
||||
|
|
|
@ -51,6 +51,7 @@ JSONARRAY_OID = 199
|
|||
JSONB_OID = 3802
|
||||
JSONBARRAY_OID = 3807
|
||||
|
||||
|
||||
class Json(object):
|
||||
"""
|
||||
An `~psycopg2.extensions.ISQLQuote` wrapper to adapt a Python object to
|
||||
|
@ -143,6 +144,7 @@ def register_json(conn_or_curs=None, globally=False, loads=None,
|
|||
|
||||
return JSON, JSONARRAY
|
||||
|
||||
|
||||
def register_default_json(conn_or_curs=None, globally=False, loads=None):
|
||||
"""
|
||||
Create and register :sql:`json` typecasters for PostgreSQL 9.2 and following.
|
||||
|
@ -155,6 +157,7 @@ def register_default_json(conn_or_curs=None, globally=False, loads=None):
|
|||
return register_json(conn_or_curs=conn_or_curs, globally=globally,
|
||||
loads=loads, oid=JSON_OID, array_oid=JSONARRAY_OID)
|
||||
|
||||
|
||||
def register_default_jsonb(conn_or_curs=None, globally=False, loads=None):
|
||||
"""
|
||||
Create and register :sql:`jsonb` typecasters for PostgreSQL 9.4 and following.
|
||||
|
@ -167,6 +170,7 @@ def register_default_jsonb(conn_or_curs=None, globally=False, loads=None):
|
|||
return register_json(conn_or_curs=conn_or_curs, globally=globally,
|
||||
loads=loads, oid=JSONB_OID, array_oid=JSONBARRAY_OID, name='jsonb')
|
||||
|
||||
|
||||
def _create_json_typecasters(oid, array_oid, loads=None, name='JSON'):
|
||||
"""Create typecasters for json data type."""
|
||||
if loads is None:
|
||||
|
@ -188,6 +192,7 @@ def _create_json_typecasters(oid, array_oid, loads=None, name='JSON'):
|
|||
|
||||
return JSON, JSONARRAY
|
||||
|
||||
|
||||
def _get_json_oids(conn_or_curs, name='json'):
|
||||
# lazy imports
|
||||
from psycopg2.extensions import STATUS_IN_TRANSACTION
|
||||
|
@ -215,6 +220,3 @@ def _get_json_oids(conn_or_curs, name='json'):
|
|||
raise conn.ProgrammingError("%s data type not found" % name)
|
||||
|
||||
return r
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -30,6 +30,7 @@ from psycopg2._psycopg import ProgrammingError, InterfaceError
|
|||
from psycopg2.extensions import ISQLQuote, adapt, register_adapter
|
||||
from psycopg2.extensions import new_type, new_array_type, register_type
|
||||
|
||||
|
||||
class Range(object):
|
||||
"""Python representation for a PostgreSQL |range|_ type.
|
||||
|
||||
|
@ -78,42 +79,50 @@ class Range(object):
|
|||
@property
|
||||
def lower_inf(self):
|
||||
"""`!True` if the range doesn't have a lower bound."""
|
||||
if self._bounds is None: return False
|
||||
if self._bounds is None:
|
||||
return False
|
||||
return self._lower is None
|
||||
|
||||
@property
|
||||
def upper_inf(self):
|
||||
"""`!True` if the range doesn't have an upper bound."""
|
||||
if self._bounds is None: return False
|
||||
if self._bounds is None:
|
||||
return False
|
||||
return self._upper is None
|
||||
|
||||
@property
|
||||
def lower_inc(self):
|
||||
"""`!True` if the lower bound is included in the range."""
|
||||
if self._bounds is None: return False
|
||||
if self._lower is None: return False
|
||||
if self._bounds is None or self._lower is None:
|
||||
return False
|
||||
return self._bounds[0] == '['
|
||||
|
||||
@property
|
||||
def upper_inc(self):
|
||||
"""`!True` if the upper bound is included in the range."""
|
||||
if self._bounds is None: return False
|
||||
if self._upper is None: return False
|
||||
if self._bounds is None or self._upper is None:
|
||||
return False
|
||||
return self._bounds[1] == ']'
|
||||
|
||||
def __contains__(self, x):
|
||||
if self._bounds is None: return False
|
||||
if self._bounds is None:
|
||||
return False
|
||||
|
||||
if self._lower is not None:
|
||||
if self._bounds[0] == '[':
|
||||
if x < self._lower: return False
|
||||
if x < self._lower:
|
||||
return False
|
||||
else:
|
||||
if x <= self._lower: return False
|
||||
if x <= self._lower:
|
||||
return False
|
||||
|
||||
if self._upper is not None:
|
||||
if self._bounds[1] == ']':
|
||||
if x > self._upper: return False
|
||||
if x > self._upper:
|
||||
return False
|
||||
else:
|
||||
if x >= self._upper: return False
|
||||
if x >= self._upper:
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
|
@ -295,7 +304,8 @@ class RangeCaster(object):
|
|||
self.adapter.name = pgrange
|
||||
else:
|
||||
try:
|
||||
if issubclass(pgrange, RangeAdapter) and pgrange is not RangeAdapter:
|
||||
if issubclass(pgrange, RangeAdapter) \
|
||||
and pgrange is not RangeAdapter:
|
||||
self.adapter = pgrange
|
||||
except TypeError:
|
||||
pass
|
||||
|
@ -436,14 +446,17 @@ class NumericRange(Range):
|
|||
"""
|
||||
pass
|
||||
|
||||
|
||||
class DateRange(Range):
|
||||
"""Represents :sql:`daterange` values."""
|
||||
pass
|
||||
|
||||
|
||||
class DateTimeRange(Range):
|
||||
"""Represents :sql:`tsrange` values."""
|
||||
pass
|
||||
|
||||
|
||||
class DateTimeTZRange(Range):
|
||||
"""Represents :sql:`tstzrange` values."""
|
||||
pass
|
||||
|
@ -508,5 +521,3 @@ tsrange_caster._register()
|
|||
tstzrange_caster = RangeCaster('tstzrange', DateTimeTZRange,
|
||||
oid=3910, subtype_oid=1184, array_oid=3911)
|
||||
tstzrange_caster._register()
|
||||
|
||||
|
||||
|
|
|
@ -29,6 +29,7 @@ This module contains symbolic names for all PostgreSQL error codes.
|
|||
# http://www.postgresql.org/docs/current/static/errcodes-appendix.html
|
||||
#
|
||||
|
||||
|
||||
def lookup(code, _cache={}):
|
||||
"""Lookup an error code or class code and return its symbolic name.
|
||||
|
||||
|
|
|
@ -33,43 +33,38 @@ This module holds all the extensions to the DBAPI-2.0 provided by psycopg.
|
|||
# License for more details.
|
||||
|
||||
import re as _re
|
||||
import sys as _sys
|
||||
|
||||
from psycopg2._psycopg import UNICODE, INTEGER, LONGINTEGER, BOOLEAN, FLOAT
|
||||
from psycopg2._psycopg import TIME, DATE, INTERVAL, DECIMAL
|
||||
from psycopg2._psycopg import BINARYARRAY, BOOLEANARRAY, DATEARRAY, DATETIMEARRAY
|
||||
from psycopg2._psycopg import DECIMALARRAY, FLOATARRAY, INTEGERARRAY, INTERVALARRAY
|
||||
from psycopg2._psycopg import LONGINTEGERARRAY, ROWIDARRAY, STRINGARRAY, TIMEARRAY
|
||||
from psycopg2._psycopg import UNICODEARRAY
|
||||
from psycopg2._psycopg import ( # noqa
|
||||
BINARYARRAY, BOOLEAN, BOOLEANARRAY, DATE, DATEARRAY, DATETIMEARRAY,
|
||||
DECIMAL, DECIMALARRAY, FLOAT, FLOATARRAY, INTEGER, INTEGERARRAY,
|
||||
INTERVAL, INTERVALARRAY, LONGINTEGER, LONGINTEGERARRAY, ROWIDARRAY,
|
||||
STRINGARRAY, TIME, TIMEARRAY, UNICODE, UNICODEARRAY,
|
||||
AsIs, Binary, Boolean, Float, Int, QuotedString, )
|
||||
|
||||
from psycopg2._psycopg import Binary, Boolean, Int, Float, QuotedString, AsIs
|
||||
try:
|
||||
from psycopg2._psycopg import MXDATE, MXDATETIME, MXINTERVAL, MXTIME
|
||||
from psycopg2._psycopg import MXDATEARRAY, MXDATETIMEARRAY, MXINTERVALARRAY, MXTIMEARRAY
|
||||
from psycopg2._psycopg import DateFromMx, TimeFromMx, TimestampFromMx
|
||||
from psycopg2._psycopg import IntervalFromMx
|
||||
from psycopg2._psycopg import ( # noqa
|
||||
MXDATE, MXDATETIME, MXINTERVAL, MXTIME,
|
||||
MXDATEARRAY, MXDATETIMEARRAY, MXINTERVALARRAY, MXTIMEARRAY,
|
||||
DateFromMx, TimeFromMx, TimestampFromMx, IntervalFromMx, )
|
||||
except ImportError:
|
||||
pass
|
||||
|
||||
try:
|
||||
from psycopg2._psycopg import PYDATE, PYDATETIME, PYINTERVAL, PYTIME
|
||||
from psycopg2._psycopg import PYDATEARRAY, PYDATETIMEARRAY, PYINTERVALARRAY, PYTIMEARRAY
|
||||
from psycopg2._psycopg import DateFromPy, TimeFromPy, TimestampFromPy
|
||||
from psycopg2._psycopg import IntervalFromPy
|
||||
from psycopg2._psycopg import ( # noqa
|
||||
PYDATE, PYDATETIME, PYINTERVAL, PYTIME,
|
||||
PYDATEARRAY, PYDATETIMEARRAY, PYINTERVALARRAY, PYTIMEARRAY,
|
||||
DateFromPy, TimeFromPy, TimestampFromPy, IntervalFromPy, )
|
||||
except ImportError:
|
||||
pass
|
||||
|
||||
from psycopg2._psycopg import adapt, adapters, encodings, connection, cursor
|
||||
from psycopg2._psycopg import lobject, Xid, libpq_version, parse_dsn, quote_ident
|
||||
from psycopg2._psycopg import string_types, binary_types, new_type, new_array_type, register_type
|
||||
from psycopg2._psycopg import ISQLQuote, Notify, Diagnostics, Column
|
||||
from psycopg2._psycopg import ( # noqa
|
||||
adapt, adapters, encodings, connection, cursor,
|
||||
lobject, Xid, libpq_version, parse_dsn, quote_ident,
|
||||
string_types, binary_types, new_type, new_array_type, register_type,
|
||||
ISQLQuote, Notify, Diagnostics, Column,
|
||||
QueryCanceledError, TransactionRollbackError,
|
||||
set_wait_callback, get_wait_callback, )
|
||||
|
||||
from psycopg2._psycopg import QueryCanceledError, TransactionRollbackError
|
||||
|
||||
try:
|
||||
from psycopg2._psycopg import set_wait_callback, get_wait_callback
|
||||
except ImportError:
|
||||
pass
|
||||
|
||||
"""Isolation level values."""
|
||||
ISOLATION_LEVEL_AUTOCOMMIT = 0
|
||||
|
@ -78,6 +73,7 @@ ISOLATION_LEVEL_READ_COMMITTED = 1
|
|||
ISOLATION_LEVEL_REPEATABLE_READ = 2
|
||||
ISOLATION_LEVEL_SERIALIZABLE = 3
|
||||
|
||||
|
||||
"""psycopg connection status values."""
|
||||
STATUS_SETUP = 0
|
||||
STATUS_READY = 1
|
||||
|
@ -89,12 +85,14 @@ STATUS_PREPARED = 5
|
|||
# This is a useful mnemonic to check if the connection is in a transaction
|
||||
STATUS_IN_TRANSACTION = STATUS_BEGIN
|
||||
|
||||
|
||||
"""psycopg asynchronous connection polling values"""
|
||||
POLL_OK = 0
|
||||
POLL_READ = 1
|
||||
POLL_WRITE = 2
|
||||
POLL_ERROR = 3
|
||||
|
||||
|
||||
"""Backend transaction status values."""
|
||||
TRANSACTION_STATUS_IDLE = 0
|
||||
TRANSACTION_STATUS_ACTIVE = 1
|
||||
|
@ -194,7 +192,7 @@ def _param_escape(s,
|
|||
|
||||
|
||||
# Create default json typecasters for PostgreSQL 9.2 oids
|
||||
from psycopg2._json import register_default_json, register_default_jsonb
|
||||
from psycopg2._json import register_default_json, register_default_jsonb # noqa
|
||||
|
||||
try:
|
||||
JSON, JSONARRAY = register_default_json()
|
||||
|
@ -206,7 +204,7 @@ del register_default_json, register_default_jsonb
|
|||
|
||||
|
||||
# Create default Range typecasters
|
||||
from psycopg2. _range import Range
|
||||
from psycopg2. _range import Range # noqa
|
||||
del Range
|
||||
|
||||
|
||||
|
|
|
@ -40,10 +40,23 @@ from psycopg2 import extensions as _ext
|
|||
from psycopg2.extensions import cursor as _cursor
|
||||
from psycopg2.extensions import connection as _connection
|
||||
from psycopg2.extensions import adapt as _A, quote_ident
|
||||
from psycopg2._psycopg import REPLICATION_PHYSICAL, REPLICATION_LOGICAL
|
||||
from psycopg2._psycopg import ReplicationConnection as _replicationConnection
|
||||
from psycopg2._psycopg import ReplicationCursor as _replicationCursor
|
||||
from psycopg2._psycopg import ReplicationMessage
|
||||
|
||||
from psycopg2._psycopg import ( # noqa
|
||||
REPLICATION_PHYSICAL, REPLICATION_LOGICAL,
|
||||
ReplicationConnection as _replicationConnection,
|
||||
ReplicationCursor as _replicationCursor,
|
||||
ReplicationMessage)
|
||||
|
||||
|
||||
# expose the json adaptation stuff into the module
|
||||
from psycopg2._json import ( # noqa
|
||||
json, Json, register_json, register_default_json, register_default_jsonb)
|
||||
|
||||
|
||||
# Expose range-related objects
|
||||
from psycopg2._range import ( # noqa
|
||||
Range, NumericRange, DateRange, DateTimeRange, DateTimeTZRange,
|
||||
register_range, RangeAdapter, RangeCaster)
|
||||
|
||||
|
||||
class DictCursorBase(_cursor):
|
||||
|
@ -109,6 +122,7 @@ class DictConnection(_connection):
|
|||
kwargs.setdefault('cursor_factory', DictCursor)
|
||||
return super(DictConnection, self).cursor(*args, **kwargs)
|
||||
|
||||
|
||||
class DictCursor(DictCursorBase):
|
||||
"""A cursor that keeps a list of column name -> index mappings."""
|
||||
|
||||
|
@ -133,6 +147,7 @@ class DictCursor(DictCursorBase):
|
|||
self.index[self.description[i][0]] = i
|
||||
self._query_executed = 0
|
||||
|
||||
|
||||
class DictRow(list):
|
||||
"""A row object that allow by-column-name access to data."""
|
||||
|
||||
|
@ -195,10 +210,10 @@ class DictRow(list):
|
|||
|
||||
# drop the crusty Py2 methods
|
||||
if _sys.version_info[0] > 2:
|
||||
items = iteritems; del iteritems
|
||||
keys = iterkeys; del iterkeys
|
||||
values = itervalues; del itervalues
|
||||
del has_key
|
||||
items = iteritems # noqa
|
||||
keys = iterkeys # noqa
|
||||
values = itervalues # noqa
|
||||
del iteritems, iterkeys, itervalues, has_key
|
||||
|
||||
|
||||
class RealDictConnection(_connection):
|
||||
|
@ -207,6 +222,7 @@ class RealDictConnection(_connection):
|
|||
kwargs.setdefault('cursor_factory', RealDictCursor)
|
||||
return super(RealDictConnection, self).cursor(*args, **kwargs)
|
||||
|
||||
|
||||
class RealDictCursor(DictCursorBase):
|
||||
"""A cursor that uses a real dict as the base type for rows.
|
||||
|
||||
|
@ -236,6 +252,7 @@ class RealDictCursor(DictCursorBase):
|
|||
self.column_mapping.append(self.description[i][0])
|
||||
self._query_executed = 0
|
||||
|
||||
|
||||
class RealDictRow(dict):
|
||||
"""A `!dict` subclass representing a data record."""
|
||||
|
||||
|
@ -268,6 +285,7 @@ class NamedTupleConnection(_connection):
|
|||
kwargs.setdefault('cursor_factory', NamedTupleCursor)
|
||||
return super(NamedTupleConnection, self).cursor(*args, **kwargs)
|
||||
|
||||
|
||||
class NamedTupleCursor(_cursor):
|
||||
"""A cursor that generates results as `~collections.namedtuple`.
|
||||
|
||||
|
@ -372,11 +390,13 @@ class LoggingConnection(_connection):
|
|||
|
||||
def _logtofile(self, msg, curs):
|
||||
msg = self.filter(msg, curs)
|
||||
if msg: self._logobj.write(msg + _os.linesep)
|
||||
if msg:
|
||||
self._logobj.write(msg + _os.linesep)
|
||||
|
||||
def _logtologger(self, msg, curs):
|
||||
msg = self.filter(msg, curs)
|
||||
if msg: self._logobj.debug(msg)
|
||||
if msg:
|
||||
self._logobj.debug(msg)
|
||||
|
||||
def _check(self):
|
||||
if not hasattr(self, '_logobj'):
|
||||
|
@ -388,6 +408,7 @@ class LoggingConnection(_connection):
|
|||
kwargs.setdefault('cursor_factory', LoggingCursor)
|
||||
return super(LoggingConnection, self).cursor(*args, **kwargs)
|
||||
|
||||
|
||||
class LoggingCursor(_cursor):
|
||||
"""A cursor that logs queries using its connection logging facilities."""
|
||||
|
||||
|
@ -428,6 +449,7 @@ class MinTimeLoggingConnection(LoggingConnection):
|
|||
kwargs.setdefault('cursor_factory', MinTimeLoggingCursor)
|
||||
return LoggingConnection.cursor(self, *args, **kwargs)
|
||||
|
||||
|
||||
class MinTimeLoggingCursor(LoggingCursor):
|
||||
"""The cursor sub-class companion to `MinTimeLoggingConnection`."""
|
||||
|
||||
|
@ -479,18 +501,23 @@ class ReplicationCursor(_replicationCursor):
|
|||
|
||||
if slot_type == REPLICATION_LOGICAL:
|
||||
if output_plugin is None:
|
||||
raise psycopg2.ProgrammingError("output plugin name is required to create logical replication slot")
|
||||
raise psycopg2.ProgrammingError(
|
||||
"output plugin name is required to create "
|
||||
"logical replication slot")
|
||||
|
||||
command += "LOGICAL %s" % quote_ident(output_plugin, self)
|
||||
|
||||
elif slot_type == REPLICATION_PHYSICAL:
|
||||
if output_plugin is not None:
|
||||
raise psycopg2.ProgrammingError("cannot specify output plugin name when creating physical replication slot")
|
||||
raise psycopg2.ProgrammingError(
|
||||
"cannot specify output plugin name when creating "
|
||||
"physical replication slot")
|
||||
|
||||
command += "PHYSICAL"
|
||||
|
||||
else:
|
||||
raise psycopg2.ProgrammingError("unrecognized replication type: %s" % repr(slot_type))
|
||||
raise psycopg2.ProgrammingError(
|
||||
"unrecognized replication type: %s" % repr(slot_type))
|
||||
|
||||
self.execute(command)
|
||||
|
||||
|
@ -513,7 +540,8 @@ class ReplicationCursor(_replicationCursor):
|
|||
if slot_name:
|
||||
command += "SLOT %s " % quote_ident(slot_name, self)
|
||||
else:
|
||||
raise psycopg2.ProgrammingError("slot name is required for logical replication")
|
||||
raise psycopg2.ProgrammingError(
|
||||
"slot name is required for logical replication")
|
||||
|
||||
command += "LOGICAL "
|
||||
|
||||
|
@ -523,25 +551,29 @@ class ReplicationCursor(_replicationCursor):
|
|||
# don't add "PHYSICAL", before 9.4 it was just START_REPLICATION XXX/XXX
|
||||
|
||||
else:
|
||||
raise psycopg2.ProgrammingError("unrecognized replication type: %s" % repr(slot_type))
|
||||
raise psycopg2.ProgrammingError(
|
||||
"unrecognized replication type: %s" % repr(slot_type))
|
||||
|
||||
if type(start_lsn) is str:
|
||||
lsn = start_lsn.split('/')
|
||||
lsn = "%X/%08X" % (int(lsn[0], 16), int(lsn[1], 16))
|
||||
else:
|
||||
lsn = "%X/%08X" % ((start_lsn >> 32) & 0xFFFFFFFF, start_lsn & 0xFFFFFFFF)
|
||||
lsn = "%X/%08X" % ((start_lsn >> 32) & 0xFFFFFFFF,
|
||||
start_lsn & 0xFFFFFFFF)
|
||||
|
||||
command += lsn
|
||||
|
||||
if timeline != 0:
|
||||
if slot_type == REPLICATION_LOGICAL:
|
||||
raise psycopg2.ProgrammingError("cannot specify timeline for logical replication")
|
||||
raise psycopg2.ProgrammingError(
|
||||
"cannot specify timeline for logical replication")
|
||||
|
||||
command += " TIMELINE %d" % timeline
|
||||
|
||||
if options:
|
||||
if slot_type == REPLICATION_PHYSICAL:
|
||||
raise psycopg2.ProgrammingError("cannot specify output plugin options for physical replication")
|
||||
raise psycopg2.ProgrammingError(
|
||||
"cannot specify output plugin options for physical replication")
|
||||
|
||||
command += " ("
|
||||
for k, v in options.iteritems():
|
||||
|
@ -579,6 +611,7 @@ class UUID_adapter(object):
|
|||
def __str__(self):
|
||||
return "'%s'::uuid" % self._uuid
|
||||
|
||||
|
||||
def register_uuid(oids=None, conn_or_curs=None):
|
||||
"""Create the UUID type and an uuid.UUID adapter.
|
||||
|
||||
|
@ -643,6 +676,7 @@ class Inet(object):
|
|||
def __str__(self):
|
||||
return str(self.addr)
|
||||
|
||||
|
||||
def register_inet(oid=None, conn_or_curs=None):
|
||||
"""Create the INET type and an Inet adapter.
|
||||
|
||||
|
@ -862,6 +896,7 @@ WHERE typname = 'hstore';
|
|||
|
||||
return tuple(rv0), tuple(rv1)
|
||||
|
||||
|
||||
def register_hstore(conn_or_curs, globally=False, unicode=False,
|
||||
oid=None, array_oid=None):
|
||||
"""Register adapter and typecaster for `!dict`\-\ |hstore| conversions.
|
||||
|
@ -1062,6 +1097,7 @@ ORDER BY attnum;
|
|||
return self(tname, type_oid, type_attrs,
|
||||
array_oid=array_oid, schema=schema)
|
||||
|
||||
|
||||
def register_composite(name, conn_or_curs, globally=False, factory=None):
|
||||
"""Register a typecaster to convert a composite type into a tuple.
|
||||
|
||||
|
@ -1084,17 +1120,7 @@ def register_composite(name, conn_or_curs, globally=False, factory=None):
|
|||
_ext.register_type(caster.typecaster, not globally and conn_or_curs or None)
|
||||
|
||||
if caster.array_typecaster is not None:
|
||||
_ext.register_type(caster.array_typecaster, not globally and conn_or_curs or None)
|
||||
_ext.register_type(
|
||||
caster.array_typecaster, not globally and conn_or_curs or None)
|
||||
|
||||
return caster
|
||||
|
||||
|
||||
# expose the json adaptation stuff into the module
|
||||
from psycopg2._json import json, Json, register_json
|
||||
from psycopg2._json import register_default_json, register_default_jsonb
|
||||
|
||||
|
||||
# Expose range-related objects
|
||||
from psycopg2._range import Range, NumericRange
|
||||
from psycopg2._range import DateRange, DateTimeRange, DateTimeTZRange
|
||||
from psycopg2._range import register_range, RangeAdapter, RangeCaster
|
||||
|
|
18
lib/pool.py
18
lib/pool.py
|
@ -74,8 +74,10 @@ class AbstractConnectionPool(object):
|
|||
|
||||
def _getconn(self, key=None):
|
||||
"""Get a free connection and assign it to 'key' if not None."""
|
||||
if self.closed: raise PoolError("connection pool is closed")
|
||||
if key is None: key = self._getkey()
|
||||
if self.closed:
|
||||
raise PoolError("connection pool is closed")
|
||||
if key is None:
|
||||
key = self._getkey()
|
||||
|
||||
if key in self._used:
|
||||
return self._used[key]
|
||||
|
@ -91,8 +93,10 @@ class AbstractConnectionPool(object):
|
|||
|
||||
def _putconn(self, conn, key=None, close=False):
|
||||
"""Put away a connection."""
|
||||
if self.closed: raise PoolError("connection pool is closed")
|
||||
if key is None: key = self._rused.get(id(conn))
|
||||
if self.closed:
|
||||
raise PoolError("connection pool is closed")
|
||||
if key is None:
|
||||
key = self._rused.get(id(conn))
|
||||
|
||||
if not key:
|
||||
raise PoolError("trying to put unkeyed connection")
|
||||
|
@ -129,7 +133,8 @@ class AbstractConnectionPool(object):
|
|||
an already closed connection. If you call .closeall() make sure
|
||||
your code can deal with it.
|
||||
"""
|
||||
if self.closed: raise PoolError("connection pool is closed")
|
||||
if self.closed:
|
||||
raise PoolError("connection pool is closed")
|
||||
for conn in self._pool + list(self._used.values()):
|
||||
try:
|
||||
conn.close()
|
||||
|
@ -221,7 +226,8 @@ class PersistentConnectionPool(AbstractConnectionPool):
|
|||
key = self.__thread.get_ident()
|
||||
self._lock.acquire()
|
||||
try:
|
||||
if not conn: conn = self._used[key]
|
||||
if not conn:
|
||||
conn = self._used[key]
|
||||
self._putconn(conn, key, close)
|
||||
finally:
|
||||
self._lock.release()
|
||||
|
|
|
@ -28,14 +28,15 @@ old code while porting to psycopg 2. Import it as follows::
|
|||
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
|
||||
# License for more details.
|
||||
|
||||
import psycopg2._psycopg as _2psycopg
|
||||
import psycopg2._psycopg as _2psycopg # noqa
|
||||
from psycopg2.extensions import cursor as _2cursor
|
||||
from psycopg2.extensions import connection as _2connection
|
||||
|
||||
from psycopg2 import *
|
||||
from psycopg2 import * # noqa
|
||||
import psycopg2.extensions as _ext
|
||||
_2connect = connect
|
||||
|
||||
|
||||
def connect(*args, **kwargs):
|
||||
"""connect(dsn, ...) -> new psycopg 1.1.x compatible connection object"""
|
||||
kwargs['connection_factory'] = connection
|
||||
|
@ -43,6 +44,7 @@ def connect(*args, **kwargs):
|
|||
conn.set_isolation_level(_ext.ISOLATION_LEVEL_READ_COMMITTED)
|
||||
return conn
|
||||
|
||||
|
||||
class connection(_2connection):
|
||||
"""psycopg 1.1.x connection."""
|
||||
|
||||
|
@ -92,4 +94,3 @@ class cursor(_2cursor):
|
|||
for row in rows:
|
||||
res.append(self.__build_dict(row))
|
||||
return res
|
||||
|
||||
|
|
|
@ -31,6 +31,7 @@ import time
|
|||
|
||||
ZERO = datetime.timedelta(0)
|
||||
|
||||
|
||||
class FixedOffsetTimezone(datetime.tzinfo):
|
||||
"""Fixed offset in minutes east from UTC.
|
||||
|
||||
|
@ -102,6 +103,7 @@ else:
|
|||
DSTOFFSET = STDOFFSET
|
||||
DSTDIFF = DSTOFFSET - STDOFFSET
|
||||
|
||||
|
||||
class LocalTimezone(datetime.tzinfo):
|
||||
"""Platform idea of local timezone.
|
||||
|
||||
|
|
|
@ -19,8 +19,8 @@
|
|||
# code defines the DBAPITypeObject fundamental types and warns for
|
||||
# undefined types.
|
||||
|
||||
import sys, os, string, copy
|
||||
from string import split, join, strip
|
||||
import sys
|
||||
from string import split, strip
|
||||
|
||||
|
||||
# here is the list of the foundamental types we want to import from
|
||||
|
@ -75,7 +75,6 @@ def error(msg):
|
|||
"""Report an error on stderr."""
|
||||
sys.stderr.write(msg + '\n')
|
||||
|
||||
|
||||
# read couples from stdin and build list
|
||||
read_types = []
|
||||
for l in sys.stdin.readlines():
|
||||
|
@ -98,7 +97,7 @@ for t in basic_types:
|
|||
found_types[k].append(int(found[0][1]))
|
||||
|
||||
# now outputs to stdout the right C-style definitions
|
||||
stypes = "" ; sstruct = ""
|
||||
stypes = sstruct = ""
|
||||
for t in basic_types:
|
||||
k = t[0]
|
||||
s = str(found_types[k])
|
||||
|
|
|
@ -23,6 +23,7 @@ from collections import defaultdict
|
|||
|
||||
from BeautifulSoup import BeautifulSoup as BS
|
||||
|
||||
|
||||
def main():
|
||||
if len(sys.argv) != 2:
|
||||
print >>sys.stderr, "usage: %s /path/to/errorcodes.py" % sys.argv[0]
|
||||
|
@ -41,6 +42,7 @@ def main():
|
|||
for line in generate_module_data(classes, errors):
|
||||
print >>f, line
|
||||
|
||||
|
||||
def read_base_file(filename):
|
||||
rv = []
|
||||
for line in open(filename):
|
||||
|
@ -50,6 +52,7 @@ def read_base_file(filename):
|
|||
|
||||
raise ValueError("can't find the separator. Is this the right file?")
|
||||
|
||||
|
||||
def parse_errors_txt(url):
|
||||
classes = {}
|
||||
errors = defaultdict(dict)
|
||||
|
@ -84,6 +87,7 @@ def parse_errors_txt(url):
|
|||
|
||||
return classes, errors
|
||||
|
||||
|
||||
def parse_errors_sgml(url):
|
||||
page = BS(urllib2.urlopen(url))
|
||||
table = page('table')[1]('tbody')[0]
|
||||
|
@ -130,6 +134,7 @@ errors_txt_url = \
|
|||
"http://git.postgresql.org/gitweb/?p=postgresql.git;a=blob_plain;" \
|
||||
"f=src/backend/utils/errcodes.txt;hb=REL%s_STABLE"
|
||||
|
||||
|
||||
def fetch_errors(versions):
|
||||
classes = {}
|
||||
errors = defaultdict(dict)
|
||||
|
@ -148,6 +153,7 @@ def fetch_errors(versions):
|
|||
|
||||
return classes, errors
|
||||
|
||||
|
||||
def generate_module_data(classes, errors):
|
||||
yield ""
|
||||
yield "# Error classes"
|
||||
|
@ -163,7 +169,6 @@ def generate_module_data(classes, errors):
|
|||
for errcode, errlabel in sorted(errors[clscode].items()):
|
||||
yield "%s = %r" % (errlabel, errcode)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.exit(main())
|
||||
|
||||
|
||||
|
|
|
@ -25,6 +25,7 @@ import unittest
|
|||
from pprint import pprint
|
||||
from collections import defaultdict
|
||||
|
||||
|
||||
def main():
|
||||
opt = parse_args()
|
||||
|
||||
|
@ -58,6 +59,7 @@ def main():
|
|||
|
||||
return rv
|
||||
|
||||
|
||||
def parse_args():
|
||||
import optparse
|
||||
|
||||
|
@ -104,4 +106,3 @@ def dump(i, opt):
|
|||
|
||||
if __name__ == '__main__':
|
||||
sys.exit(main())
|
||||
|
||||
|
|
55
setup.py
55
setup.py
|
@ -25,34 +25,9 @@ UPDATEs. psycopg2 also provide full asynchronous operations and support
|
|||
for coroutine libraries.
|
||||
"""
|
||||
|
||||
# note: if you are changing the list of supported Python version please fix
|
||||
# the docs in install.rst and the /features/ page on the website.
|
||||
classifiers = """\
|
||||
Development Status :: 5 - Production/Stable
|
||||
Intended Audience :: Developers
|
||||
License :: OSI Approved :: GNU Library or Lesser General Public License (LGPL)
|
||||
License :: OSI Approved :: Zope Public License
|
||||
Programming Language :: Python
|
||||
Programming Language :: Python :: 2.6
|
||||
Programming Language :: Python :: 2.7
|
||||
Programming Language :: Python :: 3
|
||||
Programming Language :: Python :: 3.1
|
||||
Programming Language :: Python :: 3.2
|
||||
Programming Language :: Python :: 3.3
|
||||
Programming Language :: Python :: 3.4
|
||||
Programming Language :: Python :: 3.5
|
||||
Programming Language :: C
|
||||
Programming Language :: SQL
|
||||
Topic :: Database
|
||||
Topic :: Database :: Front-Ends
|
||||
Topic :: Software Development
|
||||
Topic :: Software Development :: Libraries :: Python Modules
|
||||
Operating System :: Microsoft :: Windows
|
||||
Operating System :: Unix
|
||||
"""
|
||||
|
||||
# Note: The setup.py must be compatible with both Python 2 and 3
|
||||
|
||||
|
||||
import os
|
||||
import sys
|
||||
import re
|
||||
|
@ -87,6 +62,33 @@ except ImportError:
|
|||
|
||||
PSYCOPG_VERSION = '2.7.dev0'
|
||||
|
||||
|
||||
# note: if you are changing the list of supported Python version please fix
|
||||
# the docs in install.rst and the /features/ page on the website.
|
||||
classifiers = """\
|
||||
Development Status :: 5 - Production/Stable
|
||||
Intended Audience :: Developers
|
||||
License :: OSI Approved :: GNU Library or Lesser General Public License (LGPL)
|
||||
License :: OSI Approved :: Zope Public License
|
||||
Programming Language :: Python
|
||||
Programming Language :: Python :: 2.6
|
||||
Programming Language :: Python :: 2.7
|
||||
Programming Language :: Python :: 3
|
||||
Programming Language :: Python :: 3.1
|
||||
Programming Language :: Python :: 3.2
|
||||
Programming Language :: Python :: 3.3
|
||||
Programming Language :: Python :: 3.4
|
||||
Programming Language :: Python :: 3.5
|
||||
Programming Language :: C
|
||||
Programming Language :: SQL
|
||||
Topic :: Database
|
||||
Topic :: Database :: Front-Ends
|
||||
Topic :: Software Development
|
||||
Topic :: Software Development :: Libraries :: Python Modules
|
||||
Operating System :: Microsoft :: Windows
|
||||
Operating System :: Unix
|
||||
"""
|
||||
|
||||
version_flags = ['dt', 'dec']
|
||||
|
||||
PLATFORM_IS_WINDOWS = sys.platform.lower().startswith('win')
|
||||
|
@ -445,6 +447,7 @@ class psycopg_build_ext(build_ext):
|
|||
if hasattr(self, "finalize_" + sys.platform):
|
||||
getattr(self, "finalize_" + sys.platform)()
|
||||
|
||||
|
||||
def is_py_64():
|
||||
# sys.maxint not available since Py 3.1;
|
||||
# sys.maxsize not available before Py 2.6;
|
||||
|
|
|
@ -52,6 +52,7 @@ if sys.version_info[:2] >= (2, 5):
|
|||
else:
|
||||
test_with = None
|
||||
|
||||
|
||||
def test_suite():
|
||||
# If connection to test db fails, bail out early.
|
||||
import psycopg2
|
||||
|
|
|
@ -33,6 +33,7 @@ import StringIO
|
|||
|
||||
from testutils import ConnectingTestCase
|
||||
|
||||
|
||||
class PollableStub(object):
|
||||
"""A 'pollable' wrapper allowing analysis of the `poll()` calls."""
|
||||
def __init__(self, pollable):
|
||||
|
@ -68,6 +69,7 @@ class AsyncTests(ConnectingTestCase):
|
|||
def test_connection_setup(self):
|
||||
cur = self.conn.cursor()
|
||||
sync_cur = self.sync_conn.cursor()
|
||||
del cur, sync_cur
|
||||
|
||||
self.assert_(self.conn.async)
|
||||
self.assert_(not self.sync_conn.async)
|
||||
|
@ -108,6 +110,7 @@ class AsyncTests(ConnectingTestCase):
|
|||
def test_async_after_async(self):
|
||||
cur = self.conn.cursor()
|
||||
cur2 = self.conn.cursor()
|
||||
del cur2
|
||||
|
||||
cur.execute("insert into table1 values (1)")
|
||||
|
||||
|
@ -422,14 +425,14 @@ class AsyncTests(ConnectingTestCase):
|
|||
def test_async_cursor_gone(self):
|
||||
import gc
|
||||
cur = self.conn.cursor()
|
||||
cur.execute("select 42;");
|
||||
cur.execute("select 42;")
|
||||
del cur
|
||||
gc.collect()
|
||||
self.assertRaises(psycopg2.InterfaceError, self.wait, self.conn)
|
||||
|
||||
# The connection is still usable
|
||||
cur = self.conn.cursor()
|
||||
cur.execute("select 42;");
|
||||
cur.execute("select 42;")
|
||||
self.wait(self.conn)
|
||||
self.assertEqual(cur.fetchone(), (42,))
|
||||
|
||||
|
@ -449,4 +452,3 @@ def test_suite():
|
|||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
|
|
|
@ -26,6 +26,7 @@ import psycopg2
|
|||
import time
|
||||
import unittest
|
||||
|
||||
|
||||
class DateTimeAllocationBugTestCase(unittest.TestCase):
|
||||
def test_date_time_allocation_bug(self):
|
||||
d1 = psycopg2.Date(2002, 12, 25)
|
||||
|
@ -35,6 +36,7 @@ class DateTimeAllocationBugTestCase(unittest.TestCase):
|
|||
t1 = psycopg2.Timestamp(2002, 12, 25, 13, 45, 30)
|
||||
t2 = psycopg2.TimestampFromTicks(
|
||||
time.mktime((2002, 12, 25, 13, 45, 30, 0, 0, 0)))
|
||||
del d1, d2, t1, t2
|
||||
|
||||
|
||||
def test_suite():
|
||||
|
|
|
@ -29,6 +29,7 @@ import gc
|
|||
|
||||
from testutils import ConnectingTestCase, skip_if_no_uuid
|
||||
|
||||
|
||||
class StolenReferenceTestCase(ConnectingTestCase):
|
||||
@skip_if_no_uuid
|
||||
def test_stolen_reference_bug(self):
|
||||
|
@ -41,8 +42,10 @@ class StolenReferenceTestCase(ConnectingTestCase):
|
|||
curs.execute("select 'b5219e01-19ab-4994-b71e-149225dc51e4'::uuid")
|
||||
curs.fetchone()
|
||||
|
||||
|
||||
def test_suite():
|
||||
return unittest.TestLoader().loadTestsFromName(__name__)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
|
|
@ -32,6 +32,7 @@ from psycopg2 import extras
|
|||
from testconfig import dsn
|
||||
from testutils import unittest, ConnectingTestCase, skip_before_postgres
|
||||
|
||||
|
||||
class CancelTests(ConnectingTestCase):
|
||||
|
||||
def setUp(self):
|
||||
|
@ -71,6 +72,7 @@ class CancelTests(ConnectingTestCase):
|
|||
except Exception, e:
|
||||
errors.append(e)
|
||||
raise
|
||||
del cur
|
||||
|
||||
thread1 = threading.Thread(target=neverending, args=(self.conn, ))
|
||||
# wait a bit to make sure that the other thread is already in
|
||||
|
|
|
@ -27,17 +27,16 @@ import sys
|
|||
import time
|
||||
import threading
|
||||
from operator import attrgetter
|
||||
from StringIO import StringIO
|
||||
|
||||
import psycopg2
|
||||
import psycopg2.errorcodes
|
||||
import psycopg2.extensions
|
||||
ext = psycopg2.extensions
|
||||
from psycopg2 import extensions as ext
|
||||
|
||||
from testutils import (
|
||||
unittest, decorate_all_tests, skip_if_no_superuser,
|
||||
skip_before_postgres, skip_after_postgres, skip_before_libpq,
|
||||
ConnectingTestCase, skip_if_tpc_disabled, skip_if_windows)
|
||||
|
||||
from testutils import unittest, decorate_all_tests, skip_if_no_superuser
|
||||
from testutils import skip_before_postgres, skip_after_postgres, skip_before_libpq
|
||||
from testutils import ConnectingTestCase, skip_if_tpc_disabled
|
||||
from testutils import skip_if_windows
|
||||
from testconfig import dsn, dbname
|
||||
|
||||
|
||||
|
@ -112,8 +111,14 @@ class ConnectionTests(ConnectingTestCase):
|
|||
cur = conn.cursor()
|
||||
if self.conn.server_version >= 90300:
|
||||
cur.execute("set client_min_messages=debug1")
|
||||
cur.execute("create temp table table1 (id serial); create temp table table2 (id serial);")
|
||||
cur.execute("create temp table table3 (id serial); create temp table table4 (id serial);")
|
||||
cur.execute("""
|
||||
create temp table table1 (id serial);
|
||||
create temp table table2 (id serial);
|
||||
""")
|
||||
cur.execute("""
|
||||
create temp table table3 (id serial);
|
||||
create temp table table4 (id serial);
|
||||
""")
|
||||
self.assertEqual(4, len(conn.notices))
|
||||
self.assert_('table1' in conn.notices[0])
|
||||
self.assert_('table2' in conn.notices[1])
|
||||
|
@ -126,7 +131,8 @@ class ConnectionTests(ConnectingTestCase):
|
|||
if self.conn.server_version >= 90300:
|
||||
cur.execute("set client_min_messages=debug1")
|
||||
for i in range(0, 100, 10):
|
||||
sql = " ".join(["create temp table table%d (id serial);" % j for j in range(i, i + 10)])
|
||||
sql = " ".join(["create temp table table%d (id serial);" % j
|
||||
for j in range(i, i + 10)])
|
||||
cur.execute(sql)
|
||||
|
||||
self.assertEqual(50, len(conn.notices))
|
||||
|
@ -141,8 +147,13 @@ class ConnectionTests(ConnectingTestCase):
|
|||
if self.conn.server_version >= 90300:
|
||||
cur.execute("set client_min_messages=debug1")
|
||||
|
||||
cur.execute("create temp table table1 (id serial); create temp table table2 (id serial);")
|
||||
cur.execute("create temp table table3 (id serial); create temp table table4 (id serial);")
|
||||
cur.execute("""
|
||||
create temp table table1 (id serial);
|
||||
create temp table table2 (id serial);
|
||||
""")
|
||||
cur.execute("""
|
||||
create temp table table3 (id serial);
|
||||
create temp table table4 (id serial);""")
|
||||
self.assertEqual(len(conn.notices), 4)
|
||||
self.assert_('table1' in conn.notices.popleft())
|
||||
self.assert_('table2' in conn.notices.popleft())
|
||||
|
@ -152,7 +163,8 @@ class ConnectionTests(ConnectingTestCase):
|
|||
|
||||
# not limited, but no error
|
||||
for i in range(0, 100, 10):
|
||||
sql = " ".join(["create temp table table2_%d (id serial);" % j for j in range(i, i + 10)])
|
||||
sql = " ".join(["create temp table table2_%d (id serial);" % j
|
||||
for j in range(i, i + 10)])
|
||||
cur.execute(sql)
|
||||
|
||||
self.assertEqual(len([n for n in conn.notices if 'CREATE TABLE' in n]),
|
||||
|
@ -315,14 +327,16 @@ class ParseDsnTestCase(ConnectingTestCase):
|
|||
def test_parse_dsn(self):
|
||||
from psycopg2 import ProgrammingError
|
||||
|
||||
self.assertEqual(ext.parse_dsn('dbname=test user=tester password=secret'),
|
||||
self.assertEqual(
|
||||
ext.parse_dsn('dbname=test user=tester password=secret'),
|
||||
dict(user='tester', password='secret', dbname='test'),
|
||||
"simple DSN parsed")
|
||||
|
||||
self.assertRaises(ProgrammingError, ext.parse_dsn,
|
||||
"dbname=test 2 user=tester password=secret")
|
||||
|
||||
self.assertEqual(ext.parse_dsn("dbname='test 2' user=tester password=secret"),
|
||||
self.assertEqual(
|
||||
ext.parse_dsn("dbname='test 2' user=tester password=secret"),
|
||||
dict(user='tester', password='secret', dbname='test 2'),
|
||||
"DSN with quoting parsed")
|
||||
|
||||
|
@ -485,7 +499,8 @@ class IsolationLevelsTestCase(ConnectingTestCase):
|
|||
|
||||
levels = [
|
||||
(None, psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT),
|
||||
('read uncommitted', psycopg2.extensions.ISOLATION_LEVEL_READ_UNCOMMITTED),
|
||||
('read uncommitted',
|
||||
psycopg2.extensions.ISOLATION_LEVEL_READ_UNCOMMITTED),
|
||||
('read committed', psycopg2.extensions.ISOLATION_LEVEL_READ_COMMITTED),
|
||||
('repeatable read', psycopg2.extensions.ISOLATION_LEVEL_REPEATABLE_READ),
|
||||
('serializable', psycopg2.extensions.ISOLATION_LEVEL_SERIALIZABLE),
|
||||
|
|
|
@ -41,6 +41,7 @@ if sys.version_info[0] < 3:
|
|||
else:
|
||||
from io import TextIOBase as _base
|
||||
|
||||
|
||||
class MinimalRead(_base):
|
||||
"""A file wrapper exposing the minimal interface to copy from."""
|
||||
def __init__(self, f):
|
||||
|
@ -52,6 +53,7 @@ class MinimalRead(_base):
|
|||
def readline(self):
|
||||
return self.f.readline()
|
||||
|
||||
|
||||
class MinimalWrite(_base):
|
||||
"""A file wrapper exposing the minimal interface to copy to."""
|
||||
def __init__(self, f):
|
||||
|
@ -110,6 +112,7 @@ class CopyTests(ConnectingTestCase):
|
|||
f.write("%s\n" % (i,))
|
||||
|
||||
f.seek(0)
|
||||
|
||||
def cols():
|
||||
raise ZeroDivisionError()
|
||||
yield 'id'
|
||||
|
@ -209,9 +212,11 @@ class CopyTests(ConnectingTestCase):
|
|||
exp_size = 123
|
||||
# hack here to leave file as is, only check size when reading
|
||||
real_read = f.read
|
||||
|
||||
def read(_size, f=f, exp_size=exp_size):
|
||||
self.assertEqual(_size, exp_size)
|
||||
return real_read(_size)
|
||||
|
||||
f.read = read
|
||||
curs.copy_expert('COPY tcopy (data) FROM STDIN', f, size=exp_size)
|
||||
curs.execute("select data from tcopy;")
|
||||
|
@ -316,7 +321,7 @@ try:
|
|||
except psycopg2.ProgrammingError:
|
||||
pass
|
||||
conn.close()
|
||||
""" % { 'dsn': dsn,})
|
||||
""" % {'dsn': dsn})
|
||||
|
||||
proc = Popen([sys.executable, '-c', script_to_py3(script)])
|
||||
proc.communicate()
|
||||
|
@ -334,7 +339,7 @@ try:
|
|||
except psycopg2.ProgrammingError:
|
||||
pass
|
||||
conn.close()
|
||||
""" % { 'dsn': dsn,})
|
||||
""" % {'dsn': dsn})
|
||||
|
||||
proc = Popen([sys.executable, '-c', script_to_py3(script)], stdout=PIPE)
|
||||
proc.communicate()
|
||||
|
|
|
@ -29,6 +29,7 @@ import psycopg2.extensions
|
|||
from testutils import unittest, ConnectingTestCase, skip_before_postgres
|
||||
from testutils import skip_if_no_namedtuple, skip_if_no_getrefcount
|
||||
|
||||
|
||||
class CursorTests(ConnectingTestCase):
|
||||
|
||||
def test_close_idempotent(self):
|
||||
|
@ -47,8 +48,10 @@ class CursorTests(ConnectingTestCase):
|
|||
conn = self.conn
|
||||
cur = conn.cursor()
|
||||
cur.execute("create temp table test_exc (data int);")
|
||||
|
||||
def buggygen():
|
||||
yield 1 // 0
|
||||
|
||||
self.assertRaises(ZeroDivisionError,
|
||||
cur.executemany, "insert into test_exc values (%s)", buggygen())
|
||||
cur.close()
|
||||
|
@ -102,8 +105,7 @@ class CursorTests(ConnectingTestCase):
|
|||
# issue #81: reference leak when a parameter value is referenced
|
||||
# more than once from a dict.
|
||||
cur = self.conn.cursor()
|
||||
i = lambda x: x
|
||||
foo = i('foo') * 10
|
||||
foo = (lambda x: x)('foo') * 10
|
||||
import sys
|
||||
nref1 = sys.getrefcount(foo)
|
||||
cur.mogrify("select %(foo)s, %(foo)s, %(foo)s", {'foo': foo})
|
||||
|
@ -158,7 +160,8 @@ class CursorTests(ConnectingTestCase):
|
|||
curs = self.conn.cursor()
|
||||
w = ref(curs)
|
||||
del curs
|
||||
import gc; gc.collect()
|
||||
import gc
|
||||
gc.collect()
|
||||
self.assert_(w() is None)
|
||||
|
||||
def test_null_name(self):
|
||||
|
@ -193,16 +196,16 @@ class CursorTests(ConnectingTestCase):
|
|||
|
||||
self._create_withhold_table()
|
||||
curs = self.conn.cursor("W")
|
||||
self.assertEqual(curs.withhold, False);
|
||||
self.assertEqual(curs.withhold, False)
|
||||
curs.withhold = True
|
||||
self.assertEqual(curs.withhold, True);
|
||||
self.assertEqual(curs.withhold, True)
|
||||
curs.execute("select data from withhold order by data")
|
||||
self.conn.commit()
|
||||
self.assertEqual(curs.fetchall(), [(10,), (20,), (30,)])
|
||||
curs.close()
|
||||
|
||||
curs = self.conn.cursor("W", withhold=True)
|
||||
self.assertEqual(curs.withhold, True);
|
||||
self.assertEqual(curs.withhold, True)
|
||||
curs.execute("select data from withhold order by data")
|
||||
self.conn.commit()
|
||||
self.assertEqual(curs.fetchall(), [(10,), (20,), (30,)])
|
||||
|
@ -270,12 +273,12 @@ class CursorTests(ConnectingTestCase):
|
|||
for t in range(2):
|
||||
if not t:
|
||||
curs = self.conn.cursor("S")
|
||||
self.assertEqual(curs.scrollable, None);
|
||||
self.assertEqual(curs.scrollable, None)
|
||||
curs.scrollable = True
|
||||
else:
|
||||
curs = self.conn.cursor("S", scrollable=True)
|
||||
|
||||
self.assertEqual(curs.scrollable, True);
|
||||
self.assertEqual(curs.scrollable, True)
|
||||
curs.itersize = 10
|
||||
|
||||
# complex enough to make postgres cursors declare without
|
||||
|
|
|
@ -27,6 +27,7 @@ import psycopg2
|
|||
from psycopg2.tz import FixedOffsetTimezone, ZERO
|
||||
from testutils import unittest, ConnectingTestCase, skip_before_postgres
|
||||
|
||||
|
||||
class CommonDatetimeTestsMixin:
|
||||
|
||||
def execute(self, *args):
|
||||
|
@ -355,8 +356,10 @@ class mxDateTimeTests(ConnectingTestCase, CommonDatetimeTestsMixin):
|
|||
psycopg2.extensions.register_type(self.INTERVAL, self.conn)
|
||||
psycopg2.extensions.register_type(psycopg2.extensions.MXDATEARRAY, self.conn)
|
||||
psycopg2.extensions.register_type(psycopg2.extensions.MXTIMEARRAY, self.conn)
|
||||
psycopg2.extensions.register_type(psycopg2.extensions.MXDATETIMEARRAY, self.conn)
|
||||
psycopg2.extensions.register_type(psycopg2.extensions.MXINTERVALARRAY, self.conn)
|
||||
psycopg2.extensions.register_type(
|
||||
psycopg2.extensions.MXDATETIMEARRAY, self.conn)
|
||||
psycopg2.extensions.register_type(
|
||||
psycopg2.extensions.MXINTERVALARRAY, self.conn)
|
||||
|
||||
def tearDown(self):
|
||||
self.conn.close()
|
||||
|
@ -549,22 +552,30 @@ class FixedOffsetTimezoneTests(unittest.TestCase):
|
|||
|
||||
def test_repr_with_positive_offset(self):
|
||||
tzinfo = FixedOffsetTimezone(5 * 60)
|
||||
self.assertEqual(repr(tzinfo), "psycopg2.tz.FixedOffsetTimezone(offset=300, name=None)")
|
||||
self.assertEqual(repr(tzinfo),
|
||||
"psycopg2.tz.FixedOffsetTimezone(offset=300, name=None)")
|
||||
|
||||
def test_repr_with_negative_offset(self):
|
||||
tzinfo = FixedOffsetTimezone(-5 * 60)
|
||||
self.assertEqual(repr(tzinfo), "psycopg2.tz.FixedOffsetTimezone(offset=-300, name=None)")
|
||||
self.assertEqual(repr(tzinfo),
|
||||
"psycopg2.tz.FixedOffsetTimezone(offset=-300, name=None)")
|
||||
|
||||
def test_repr_with_name(self):
|
||||
tzinfo = FixedOffsetTimezone(name="FOO")
|
||||
self.assertEqual(repr(tzinfo), "psycopg2.tz.FixedOffsetTimezone(offset=0, name='FOO')")
|
||||
self.assertEqual(repr(tzinfo),
|
||||
"psycopg2.tz.FixedOffsetTimezone(offset=0, name='FOO')")
|
||||
|
||||
def test_instance_caching(self):
|
||||
self.assert_(FixedOffsetTimezone(name="FOO") is FixedOffsetTimezone(name="FOO"))
|
||||
self.assert_(FixedOffsetTimezone(7 * 60) is FixedOffsetTimezone(7 * 60))
|
||||
self.assert_(FixedOffsetTimezone(-9 * 60, 'FOO') is FixedOffsetTimezone(-9 * 60, 'FOO'))
|
||||
self.assert_(FixedOffsetTimezone(9 * 60) is not FixedOffsetTimezone(9 * 60, 'FOO'))
|
||||
self.assert_(FixedOffsetTimezone(name='FOO') is not FixedOffsetTimezone(9 * 60, 'FOO'))
|
||||
self.assert_(FixedOffsetTimezone(name="FOO")
|
||||
is FixedOffsetTimezone(name="FOO"))
|
||||
self.assert_(FixedOffsetTimezone(7 * 60)
|
||||
is FixedOffsetTimezone(7 * 60))
|
||||
self.assert_(FixedOffsetTimezone(-9 * 60, 'FOO')
|
||||
is FixedOffsetTimezone(-9 * 60, 'FOO'))
|
||||
self.assert_(FixedOffsetTimezone(9 * 60)
|
||||
is not FixedOffsetTimezone(9 * 60, 'FOO'))
|
||||
self.assert_(FixedOffsetTimezone(name='FOO')
|
||||
is not FixedOffsetTimezone(9 * 60, 'FOO'))
|
||||
|
||||
def test_pickle(self):
|
||||
# ticket #135
|
||||
|
|
|
@ -32,6 +32,7 @@ except NameError:
|
|||
from threading import Thread
|
||||
from psycopg2 import errorcodes
|
||||
|
||||
|
||||
class ErrocodeTests(ConnectingTestCase):
|
||||
def test_lookup_threadsafe(self):
|
||||
|
||||
|
@ -39,6 +40,7 @@ class ErrocodeTests(ConnectingTestCase):
|
|||
MAX_CYCLES = 2000
|
||||
|
||||
errs = []
|
||||
|
||||
def f(pg_code='40001'):
|
||||
try:
|
||||
errorcodes.lookup(pg_code)
|
||||
|
|
|
@ -39,7 +39,8 @@ class ExtrasDictCursorTests(ConnectingTestCase):
|
|||
self.assert_(isinstance(cur, psycopg2.extras.DictCursor))
|
||||
self.assertEqual(cur.name, None)
|
||||
# overridable
|
||||
cur = self.conn.cursor('foo', cursor_factory=psycopg2.extras.NamedTupleCursor)
|
||||
cur = self.conn.cursor('foo',
|
||||
cursor_factory=psycopg2.extras.NamedTupleCursor)
|
||||
self.assertEqual(cur.name, 'foo')
|
||||
self.assert_(isinstance(cur, psycopg2.extras.NamedTupleCursor))
|
||||
|
||||
|
@ -80,7 +81,6 @@ class ExtrasDictCursorTests(ConnectingTestCase):
|
|||
self.failUnless(row[0] == 'bar')
|
||||
return row
|
||||
|
||||
|
||||
def testDictCursorWithPlainCursorRealFetchOne(self):
|
||||
self._testWithPlainCursorReal(lambda curs: curs.fetchone())
|
||||
|
||||
|
@ -110,7 +110,6 @@ class ExtrasDictCursorTests(ConnectingTestCase):
|
|||
row = getter(curs)
|
||||
self.failUnless(row['foo'] == 'bar')
|
||||
|
||||
|
||||
def testDictCursorWithNamedCursorFetchOne(self):
|
||||
self._testWithNamedCursor(lambda curs: curs.fetchone())
|
||||
|
||||
|
@ -146,7 +145,6 @@ class ExtrasDictCursorTests(ConnectingTestCase):
|
|||
self.failUnless(row['foo'] == 'bar')
|
||||
self.failUnless(row[0] == 'bar')
|
||||
|
||||
|
||||
def testDictCursorRealWithNamedCursorFetchOne(self):
|
||||
self._testWithNamedCursorReal(lambda curs: curs.fetchone())
|
||||
|
||||
|
@ -176,12 +174,12 @@ class ExtrasDictCursorTests(ConnectingTestCase):
|
|||
self._testIterRowNumber(curs)
|
||||
|
||||
def _testWithNamedCursorReal(self, getter):
|
||||
curs = self.conn.cursor('aname', cursor_factory=psycopg2.extras.RealDictCursor)
|
||||
curs = self.conn.cursor('aname',
|
||||
cursor_factory=psycopg2.extras.RealDictCursor)
|
||||
curs.execute("SELECT * FROM ExtrasDictCursorTests")
|
||||
row = getter(curs)
|
||||
self.failUnless(row['foo'] == 'bar')
|
||||
|
||||
|
||||
def _testNamedCursorNotGreedy(self, curs):
|
||||
curs.itersize = 2
|
||||
curs.execute("""select clock_timestamp() as ts from generate_series(1,3)""")
|
||||
|
@ -235,7 +233,7 @@ class NamedTupleCursorTest(ConnectingTestCase):
|
|||
from psycopg2.extras import NamedTupleConnection
|
||||
|
||||
try:
|
||||
from collections import namedtuple
|
||||
from collections import namedtuple # noqa
|
||||
except ImportError:
|
||||
return
|
||||
|
||||
|
@ -346,7 +344,7 @@ class NamedTupleCursorTest(ConnectingTestCase):
|
|||
|
||||
def test_error_message(self):
|
||||
try:
|
||||
from collections import namedtuple
|
||||
from collections import namedtuple # noqa
|
||||
except ImportError:
|
||||
# an import error somewhere
|
||||
from psycopg2.extras import NamedTupleConnection
|
||||
|
@ -390,6 +388,7 @@ class NamedTupleCursorTest(ConnectingTestCase):
|
|||
from psycopg2.extras import NamedTupleCursor
|
||||
f_orig = NamedTupleCursor._make_nt
|
||||
calls = [0]
|
||||
|
||||
def f_patched(self_):
|
||||
calls[0] += 1
|
||||
return f_orig(self_)
|
||||
|
|
|
@ -29,6 +29,7 @@ import psycopg2.extras
|
|||
|
||||
from testutils import ConnectingTestCase
|
||||
|
||||
|
||||
class ConnectionStub(object):
|
||||
"""A `connection` wrapper allowing analysis of the `poll()` calls."""
|
||||
def __init__(self, conn):
|
||||
|
@ -43,6 +44,7 @@ class ConnectionStub(object):
|
|||
self.polls.append(rv)
|
||||
return rv
|
||||
|
||||
|
||||
class GreenTestCase(ConnectingTestCase):
|
||||
def setUp(self):
|
||||
self._cb = psycopg2.extensions.get_wait_callback()
|
||||
|
|
|
@ -32,6 +32,7 @@ import psycopg2.extensions
|
|||
from testutils import unittest, decorate_all_tests, skip_if_tpc_disabled
|
||||
from testutils import ConnectingTestCase, skip_if_green
|
||||
|
||||
|
||||
def skip_if_no_lo(f):
|
||||
@wraps(f)
|
||||
def skip_if_no_lo_(self):
|
||||
|
@ -158,7 +159,7 @@ class LargeObjectTests(LargeObjectTestCase):
|
|||
|
||||
def test_read(self):
|
||||
lo = self.conn.lobject()
|
||||
length = lo.write(b"some data")
|
||||
lo.write(b"some data")
|
||||
lo.close()
|
||||
|
||||
lo = self.conn.lobject(lo.oid)
|
||||
|
@ -169,7 +170,7 @@ class LargeObjectTests(LargeObjectTestCase):
|
|||
|
||||
def test_read_binary(self):
|
||||
lo = self.conn.lobject()
|
||||
length = lo.write(b"some data")
|
||||
lo.write(b"some data")
|
||||
lo.close()
|
||||
|
||||
lo = self.conn.lobject(lo.oid, "rb")
|
||||
|
@ -181,7 +182,7 @@ class LargeObjectTests(LargeObjectTestCase):
|
|||
def test_read_text(self):
|
||||
lo = self.conn.lobject()
|
||||
snowman = u"\u2603"
|
||||
length = lo.write(u"some data " + snowman)
|
||||
lo.write(u"some data " + snowman)
|
||||
lo.close()
|
||||
|
||||
lo = self.conn.lobject(lo.oid, "rt")
|
||||
|
@ -193,7 +194,7 @@ class LargeObjectTests(LargeObjectTestCase):
|
|||
def test_read_large(self):
|
||||
lo = self.conn.lobject()
|
||||
data = "data" * 1000000
|
||||
length = lo.write("some" + data)
|
||||
lo.write("some" + data)
|
||||
lo.close()
|
||||
|
||||
lo = self.conn.lobject(lo.oid)
|
||||
|
@ -399,6 +400,7 @@ def skip_if_no_truncate(f):
|
|||
|
||||
return skip_if_no_truncate_
|
||||
|
||||
|
||||
class LargeObjectTruncateTests(LargeObjectTestCase):
|
||||
def test_truncate(self):
|
||||
lo = self.conn.lobject()
|
||||
|
@ -450,15 +452,19 @@ def _has_lo64(conn):
|
|||
|
||||
return (True, "this server and build support the lo64 API")
|
||||
|
||||
|
||||
def skip_if_no_lo64(f):
|
||||
@wraps(f)
|
||||
def skip_if_no_lo64_(self):
|
||||
lo64, msg = _has_lo64(self.conn)
|
||||
if not lo64: return self.skipTest(msg)
|
||||
else: return f(self)
|
||||
if not lo64:
|
||||
return self.skipTest(msg)
|
||||
else:
|
||||
return f(self)
|
||||
|
||||
return skip_if_no_lo64_
|
||||
|
||||
|
||||
class LargeObject64Tests(LargeObjectTestCase):
|
||||
def test_seek_tell_truncate_greater_than_2gb(self):
|
||||
lo = self.conn.lobject()
|
||||
|
@ -477,11 +483,14 @@ def skip_if_lo64(f):
|
|||
@wraps(f)
|
||||
def skip_if_lo64_(self):
|
||||
lo64, msg = _has_lo64(self.conn)
|
||||
if lo64: return self.skipTest(msg)
|
||||
else: return f(self)
|
||||
if lo64:
|
||||
return self.skipTest(msg)
|
||||
else:
|
||||
return f(self)
|
||||
|
||||
return skip_if_lo64_
|
||||
|
||||
|
||||
class LargeObjectNot64Tests(LargeObjectTestCase):
|
||||
def test_seek_larger_than_2gb(self):
|
||||
lo = self.conn.lobject()
|
||||
|
|
|
@ -79,7 +79,7 @@ conn.close()
|
|||
proc = self.notify('foo', 1)
|
||||
|
||||
t0 = time.time()
|
||||
ready = select.select([self.conn], [], [], 5)
|
||||
select.select([self.conn], [], [], 5)
|
||||
t1 = time.time()
|
||||
self.assert_(0.99 < t1 - t0 < 4, t1 - t0)
|
||||
|
||||
|
@ -217,6 +217,6 @@ conn.close()
|
|||
def test_suite():
|
||||
return unittest.TestLoader().loadTestsFromName(__name__)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
|
|
|
@ -30,6 +30,7 @@ import psycopg2
|
|||
|
||||
from testconfig import dsn
|
||||
|
||||
|
||||
class Psycopg2Tests(dbapi20.DatabaseAPI20Test):
|
||||
driver = psycopg2
|
||||
connect_args = ()
|
||||
|
|
|
@ -24,8 +24,8 @@
|
|||
|
||||
import psycopg2
|
||||
import psycopg2.extensions
|
||||
from psycopg2.extras import PhysicalReplicationConnection, LogicalReplicationConnection
|
||||
from psycopg2.extras import StopReplication
|
||||
from psycopg2.extras import (
|
||||
PhysicalReplicationConnection, LogicalReplicationConnection, StopReplication)
|
||||
|
||||
import testconfig
|
||||
from testutils import unittest
|
||||
|
@ -70,14 +70,16 @@ class ReplicationTestCase(ConnectingTestCase):
|
|||
# generate some events for our replication stream
|
||||
def make_replication_events(self):
|
||||
conn = self.connect()
|
||||
if conn is None: return
|
||||
if conn is None:
|
||||
return
|
||||
cur = conn.cursor()
|
||||
|
||||
try:
|
||||
cur.execute("DROP TABLE dummy1")
|
||||
except psycopg2.ProgrammingError:
|
||||
conn.rollback()
|
||||
cur.execute("CREATE TABLE dummy1 AS SELECT * FROM generate_series(1, 5) AS id")
|
||||
cur.execute(
|
||||
"CREATE TABLE dummy1 AS SELECT * FROM generate_series(1, 5) AS id")
|
||||
conn.commit()
|
||||
|
||||
|
||||
|
@ -85,7 +87,8 @@ class ReplicationTest(ReplicationTestCase):
|
|||
@skip_before_postgres(9, 0)
|
||||
def test_physical_replication_connection(self):
|
||||
conn = self.repl_connect(connection_factory=PhysicalReplicationConnection)
|
||||
if conn is None: return
|
||||
if conn is None:
|
||||
return
|
||||
cur = conn.cursor()
|
||||
cur.execute("IDENTIFY_SYSTEM")
|
||||
cur.fetchall()
|
||||
|
@ -93,7 +96,8 @@ class ReplicationTest(ReplicationTestCase):
|
|||
@skip_before_postgres(9, 4)
|
||||
def test_logical_replication_connection(self):
|
||||
conn = self.repl_connect(connection_factory=LogicalReplicationConnection)
|
||||
if conn is None: return
|
||||
if conn is None:
|
||||
return
|
||||
cur = conn.cursor()
|
||||
cur.execute("IDENTIFY_SYSTEM")
|
||||
cur.fetchall()
|
||||
|
@ -101,19 +105,23 @@ class ReplicationTest(ReplicationTestCase):
|
|||
@skip_before_postgres(9, 4) # slots require 9.4
|
||||
def test_create_replication_slot(self):
|
||||
conn = self.repl_connect(connection_factory=PhysicalReplicationConnection)
|
||||
if conn is None: return
|
||||
if conn is None:
|
||||
return
|
||||
cur = conn.cursor()
|
||||
|
||||
self.create_replication_slot(cur)
|
||||
self.assertRaises(psycopg2.ProgrammingError, self.create_replication_slot, cur)
|
||||
self.assertRaises(
|
||||
psycopg2.ProgrammingError, self.create_replication_slot, cur)
|
||||
|
||||
@skip_before_postgres(9, 4) # slots require 9.4
|
||||
def test_start_on_missing_replication_slot(self):
|
||||
conn = self.repl_connect(connection_factory=PhysicalReplicationConnection)
|
||||
if conn is None: return
|
||||
if conn is None:
|
||||
return
|
||||
cur = conn.cursor()
|
||||
|
||||
self.assertRaises(psycopg2.ProgrammingError, cur.start_replication, self.slot)
|
||||
self.assertRaises(psycopg2.ProgrammingError,
|
||||
cur.start_replication, self.slot)
|
||||
|
||||
self.create_replication_slot(cur)
|
||||
cur.start_replication(self.slot)
|
||||
|
@ -121,13 +129,16 @@ class ReplicationTest(ReplicationTestCase):
|
|||
@skip_before_postgres(9, 4) # slots require 9.4
|
||||
def test_start_and_recover_from_error(self):
|
||||
conn = self.repl_connect(connection_factory=LogicalReplicationConnection)
|
||||
if conn is None: return
|
||||
if conn is None:
|
||||
return
|
||||
cur = conn.cursor()
|
||||
|
||||
self.create_replication_slot(cur, output_plugin='test_decoding')
|
||||
|
||||
# try with invalid options
|
||||
cur.start_replication(slot_name=self.slot, options={'invalid_param': 'value'})
|
||||
cur.start_replication(
|
||||
slot_name=self.slot, options={'invalid_param': 'value'})
|
||||
|
||||
def consume(msg):
|
||||
pass
|
||||
# we don't see the error from the server before we try to read the data
|
||||
|
@ -139,7 +150,8 @@ class ReplicationTest(ReplicationTestCase):
|
|||
@skip_before_postgres(9, 4) # slots require 9.4
|
||||
def test_stop_replication(self):
|
||||
conn = self.repl_connect(connection_factory=LogicalReplicationConnection)
|
||||
if conn is None: return
|
||||
if conn is None:
|
||||
return
|
||||
cur = conn.cursor()
|
||||
|
||||
self.create_replication_slot(cur, output_plugin='test_decoding')
|
||||
|
@ -147,6 +159,7 @@ class ReplicationTest(ReplicationTestCase):
|
|||
self.make_replication_events()
|
||||
|
||||
cur.start_replication(self.slot)
|
||||
|
||||
def consume(msg):
|
||||
raise StopReplication()
|
||||
self.assertRaises(StopReplication, cur.consume_stream, consume)
|
||||
|
@ -155,8 +168,10 @@ class ReplicationTest(ReplicationTestCase):
|
|||
class AsyncReplicationTest(ReplicationTestCase):
|
||||
@skip_before_postgres(9, 4) # slots require 9.4
|
||||
def test_async_replication(self):
|
||||
conn = self.repl_connect(connection_factory=LogicalReplicationConnection, async=1)
|
||||
if conn is None: return
|
||||
conn = self.repl_connect(
|
||||
connection_factory=LogicalReplicationConnection, async=1)
|
||||
if conn is None:
|
||||
return
|
||||
self.wait(conn)
|
||||
cur = conn.cursor()
|
||||
|
||||
|
@ -169,9 +184,10 @@ class AsyncReplicationTest(ReplicationTestCase):
|
|||
self.make_replication_events()
|
||||
|
||||
self.msg_count = 0
|
||||
|
||||
def consume(msg):
|
||||
# just check the methods
|
||||
log = "%s: %s" % (cur.io_timestamp, repr(msg))
|
||||
"%s: %s" % (cur.io_timestamp, repr(msg))
|
||||
|
||||
self.msg_count += 1
|
||||
if self.msg_count > 3:
|
||||
|
@ -193,8 +209,10 @@ class AsyncReplicationTest(ReplicationTestCase):
|
|||
select([cur], [], [])
|
||||
self.assertRaises(StopReplication, process_stream)
|
||||
|
||||
|
||||
def test_suite():
|
||||
return unittest.TestLoader().loadTestsFromName(__name__)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
|
|
@ -29,6 +29,7 @@ import psycopg2
|
|||
from psycopg2.extensions import (
|
||||
ISOLATION_LEVEL_SERIALIZABLE, STATUS_BEGIN, STATUS_READY)
|
||||
|
||||
|
||||
class TransactionTests(ConnectingTestCase):
|
||||
|
||||
def setUp(self):
|
||||
|
@ -147,6 +148,7 @@ class DeadlockSerializationTests(ConnectingTestCase):
|
|||
self.thread1_error = exc
|
||||
step1.set()
|
||||
conn.close()
|
||||
|
||||
def task2():
|
||||
try:
|
||||
conn = self.connect()
|
||||
|
@ -195,6 +197,7 @@ class DeadlockSerializationTests(ConnectingTestCase):
|
|||
self.thread1_error = exc
|
||||
step1.set()
|
||||
conn.close()
|
||||
|
||||
def task2():
|
||||
try:
|
||||
conn = self.connect()
|
||||
|
|
|
@ -68,13 +68,16 @@ class TypesBasicTests(ConnectingTestCase):
|
|||
"wrong decimal quoting: " + str(s))
|
||||
s = self.execute("SELECT %s AS foo", (decimal.Decimal("NaN"),))
|
||||
self.failUnless(str(s) == "NaN", "wrong decimal quoting: " + str(s))
|
||||
self.failUnless(type(s) == decimal.Decimal, "wrong decimal conversion: " + repr(s))
|
||||
self.failUnless(type(s) == decimal.Decimal,
|
||||
"wrong decimal conversion: " + repr(s))
|
||||
s = self.execute("SELECT %s AS foo", (decimal.Decimal("infinity"),))
|
||||
self.failUnless(str(s) == "NaN", "wrong decimal quoting: " + str(s))
|
||||
self.failUnless(type(s) == decimal.Decimal, "wrong decimal conversion: " + repr(s))
|
||||
self.failUnless(type(s) == decimal.Decimal,
|
||||
"wrong decimal conversion: " + repr(s))
|
||||
s = self.execute("SELECT %s AS foo", (decimal.Decimal("-infinity"),))
|
||||
self.failUnless(str(s) == "NaN", "wrong decimal quoting: " + str(s))
|
||||
self.failUnless(type(s) == decimal.Decimal, "wrong decimal conversion: " + repr(s))
|
||||
self.failUnless(type(s) == decimal.Decimal,
|
||||
"wrong decimal conversion: " + repr(s))
|
||||
|
||||
def testFloatNan(self):
|
||||
try:
|
||||
|
@ -150,9 +153,12 @@ class TypesBasicTests(ConnectingTestCase):
|
|||
# ticket #42
|
||||
import datetime
|
||||
curs = self.conn.cursor()
|
||||
curs.execute("create table array_test (id integer, col timestamp without time zone[])")
|
||||
curs.execute(
|
||||
"create table array_test "
|
||||
"(id integer, col timestamp without time zone[])")
|
||||
|
||||
curs.execute("insert into array_test values (%s, %s)", (1, [datetime.date(2011,2,14)]))
|
||||
curs.execute("insert into array_test values (%s, %s)",
|
||||
(1, [datetime.date(2011, 2, 14)]))
|
||||
curs.execute("select col from array_test where id = 1")
|
||||
self.assertEqual(curs.fetchone()[0], [datetime.datetime(2011, 2, 14, 0, 0)])
|
||||
|
||||
|
@ -331,7 +337,8 @@ class TypesBasicTests(ConnectingTestCase):
|
|||
@testutils.skip_before_postgres(8, 2)
|
||||
def testGenericArrayNull(self):
|
||||
def caster(s, cur):
|
||||
if s is None: return "nada"
|
||||
if s is None:
|
||||
return "nada"
|
||||
return int(s) * 2
|
||||
base = psycopg2.extensions.new_type((23,), "INT4", caster)
|
||||
array = psycopg2.extensions.new_array_type((1007,), "INT4ARRAY", base)
|
||||
|
@ -346,7 +353,9 @@ class TypesBasicTests(ConnectingTestCase):
|
|||
class AdaptSubclassTest(unittest.TestCase):
|
||||
def test_adapt_subtype(self):
|
||||
from psycopg2.extensions import adapt
|
||||
class Sub(str): pass
|
||||
|
||||
class Sub(str):
|
||||
pass
|
||||
s1 = "hel'lo"
|
||||
s2 = Sub(s1)
|
||||
self.assertEqual(adapt(s1).getquoted(), adapt(s2).getquoted())
|
||||
|
@ -354,9 +363,14 @@ class AdaptSubclassTest(unittest.TestCase):
|
|||
def test_adapt_most_specific(self):
|
||||
from psycopg2.extensions import adapt, register_adapter, AsIs
|
||||
|
||||
class A(object): pass
|
||||
class B(A): pass
|
||||
class C(B): pass
|
||||
class A(object):
|
||||
pass
|
||||
|
||||
class B(A):
|
||||
pass
|
||||
|
||||
class C(B):
|
||||
pass
|
||||
|
||||
register_adapter(A, lambda a: AsIs("a"))
|
||||
register_adapter(B, lambda b: AsIs("b"))
|
||||
|
@ -370,8 +384,11 @@ class AdaptSubclassTest(unittest.TestCase):
|
|||
def test_no_mro_no_joy(self):
|
||||
from psycopg2.extensions import adapt, register_adapter, AsIs
|
||||
|
||||
class A: pass
|
||||
class B(A): pass
|
||||
class A:
|
||||
pass
|
||||
|
||||
class B(A):
|
||||
pass
|
||||
|
||||
register_adapter(A, lambda a: AsIs("a"))
|
||||
try:
|
||||
|
@ -383,8 +400,11 @@ class AdaptSubclassTest(unittest.TestCase):
|
|||
def test_adapt_subtype_3(self):
|
||||
from psycopg2.extensions import adapt, register_adapter, AsIs
|
||||
|
||||
class A: pass
|
||||
class B(A): pass
|
||||
class A:
|
||||
pass
|
||||
|
||||
class B(A):
|
||||
pass
|
||||
|
||||
register_adapter(A, lambda a: AsIs("a"))
|
||||
try:
|
||||
|
@ -443,7 +463,8 @@ class ByteaParserTest(unittest.TestCase):
|
|||
|
||||
def test_full_hex(self, upper=False):
|
||||
buf = ''.join(("%02x" % i) for i in range(256))
|
||||
if upper: buf = buf.upper()
|
||||
if upper:
|
||||
buf = buf.upper()
|
||||
buf = '\\x' + buf
|
||||
rv = self.cast(buf.encode('utf8'))
|
||||
if sys.version_info[0] < 3:
|
||||
|
|
|
@ -37,6 +37,7 @@ def filter_scs(conn, s):
|
|||
else:
|
||||
return s.replace(b"E'", b"'")
|
||||
|
||||
|
||||
class TypesExtrasTests(ConnectingTestCase):
|
||||
"""Test that all type conversions are working."""
|
||||
|
||||
|
@ -60,7 +61,8 @@ class TypesExtrasTests(ConnectingTestCase):
|
|||
def testUUIDARRAY(self):
|
||||
import uuid
|
||||
psycopg2.extras.register_uuid()
|
||||
u = [uuid.UUID('9c6d5a77-7256-457e-9461-347b4358e350'), uuid.UUID('9c6d5a77-7256-457e-9461-347b4358e352')]
|
||||
u = [uuid.UUID('9c6d5a77-7256-457e-9461-347b4358e350'),
|
||||
uuid.UUID('9c6d5a77-7256-457e-9461-347b4358e352')]
|
||||
s = self.execute("SELECT %s AS foo", (u,))
|
||||
self.failUnless(u == s)
|
||||
# array with a NULL element
|
||||
|
@ -110,7 +112,8 @@ class TypesExtrasTests(ConnectingTestCase):
|
|||
a.getquoted())
|
||||
|
||||
def test_adapt_fail(self):
|
||||
class Foo(object): pass
|
||||
class Foo(object):
|
||||
pass
|
||||
self.assertRaises(psycopg2.ProgrammingError,
|
||||
psycopg2.extensions.adapt, Foo(), ext.ISQLQuote, None)
|
||||
try:
|
||||
|
@ -130,6 +133,7 @@ def skip_if_no_hstore(f):
|
|||
|
||||
return skip_if_no_hstore_
|
||||
|
||||
|
||||
class HstoreTestCase(ConnectingTestCase):
|
||||
def test_adapt_8(self):
|
||||
if self.conn.server_version >= 90000:
|
||||
|
@ -155,7 +159,8 @@ class HstoreTestCase(ConnectingTestCase):
|
|||
self.assertEqual(ii[2], filter_scs(self.conn, b"(E'c' => NULL)"))
|
||||
if 'd' in o:
|
||||
encc = u'\xe0'.encode(psycopg2.extensions.encodings[self.conn.encoding])
|
||||
self.assertEqual(ii[3], filter_scs(self.conn, b"(E'd' => E'" + encc + b"')"))
|
||||
self.assertEqual(ii[3],
|
||||
filter_scs(self.conn, b"(E'd' => E'" + encc + b"')"))
|
||||
|
||||
def test_adapt_9(self):
|
||||
if self.conn.server_version < 90000:
|
||||
|
@ -402,7 +407,9 @@ class HstoreTestCase(ConnectingTestCase):
|
|||
from psycopg2.extras import register_hstore
|
||||
register_hstore(None, globally=True, oid=oid, array_oid=aoid)
|
||||
try:
|
||||
cur.execute("select null::hstore, ''::hstore, 'a => b'::hstore, '{a=>b}'::hstore[]")
|
||||
cur.execute("""
|
||||
select null::hstore, ''::hstore,
|
||||
'a => b'::hstore, '{a=>b}'::hstore[]""")
|
||||
t = cur.fetchone()
|
||||
self.assert_(t[0] is None)
|
||||
self.assertEqual(t[1], {})
|
||||
|
@ -449,6 +456,7 @@ def skip_if_no_composite(f):
|
|||
|
||||
return skip_if_no_composite_
|
||||
|
||||
|
||||
class AdaptTypeTestCase(ConnectingTestCase):
|
||||
@skip_if_no_composite
|
||||
def test_none_in_record(self):
|
||||
|
@ -463,8 +471,11 @@ class AdaptTypeTestCase(ConnectingTestCase):
|
|||
# the None adapter is not actually invoked in regular adaptation
|
||||
|
||||
class WonkyAdapter(object):
|
||||
def __init__(self, obj): pass
|
||||
def getquoted(self): return "NOPE!"
|
||||
def __init__(self, obj):
|
||||
pass
|
||||
|
||||
def getquoted(self):
|
||||
return "NOPE!"
|
||||
|
||||
curs = self.conn.cursor()
|
||||
|
||||
|
@ -481,6 +492,7 @@ class AdaptTypeTestCase(ConnectingTestCase):
|
|||
|
||||
def test_tokenization(self):
|
||||
from psycopg2.extras import CompositeCaster
|
||||
|
||||
def ok(s, v):
|
||||
self.assertEqual(CompositeCaster.tokenize(s), v)
|
||||
|
||||
|
@ -531,7 +543,7 @@ class AdaptTypeTestCase(ConnectingTestCase):
|
|||
self.assertEqual(v[2], date(2011, 1, 2))
|
||||
|
||||
try:
|
||||
from collections import namedtuple
|
||||
from collections import namedtuple # noqa
|
||||
except ImportError:
|
||||
pass
|
||||
else:
|
||||
|
@ -581,7 +593,7 @@ class AdaptTypeTestCase(ConnectingTestCase):
|
|||
self.assertEqual(r, v)
|
||||
|
||||
try:
|
||||
from collections import namedtuple
|
||||
from collections import namedtuple # noqa
|
||||
except ImportError:
|
||||
pass
|
||||
else:
|
||||
|
@ -666,7 +678,7 @@ class AdaptTypeTestCase(ConnectingTestCase):
|
|||
@skip_if_no_composite
|
||||
@skip_before_postgres(8, 4)
|
||||
def test_composite_array(self):
|
||||
oid = self._create_type("type_isd",
|
||||
self._create_type("type_isd",
|
||||
[('anint', 'integer'), ('astring', 'text'), ('adate', 'date')])
|
||||
|
||||
t = psycopg2.extras.register_composite("type_isd", self.conn)
|
||||
|
@ -825,6 +837,7 @@ def skip_if_json_module(f):
|
|||
|
||||
return skip_if_json_module_
|
||||
|
||||
|
||||
def skip_if_no_json_module(f):
|
||||
"""Skip a test if no Python json module is available"""
|
||||
@wraps(f)
|
||||
|
@ -836,6 +849,7 @@ def skip_if_no_json_module(f):
|
|||
|
||||
return skip_if_no_json_module_
|
||||
|
||||
|
||||
def skip_if_no_json_type(f):
|
||||
"""Skip a test if PostgreSQL json type is not available"""
|
||||
@wraps(f)
|
||||
|
@ -849,6 +863,7 @@ def skip_if_no_json_type(f):
|
|||
|
||||
return skip_if_no_json_type_
|
||||
|
||||
|
||||
class JsonTestCase(ConnectingTestCase):
|
||||
@skip_if_json_module
|
||||
def test_module_not_available(self):
|
||||
|
@ -858,6 +873,7 @@ class JsonTestCase(ConnectingTestCase):
|
|||
@skip_if_json_module
|
||||
def test_customizable_with_module_not_available(self):
|
||||
from psycopg2.extras import Json
|
||||
|
||||
class MyJson(Json):
|
||||
def dumps(self, obj):
|
||||
assert obj is None
|
||||
|
@ -889,7 +905,9 @@ class JsonTestCase(ConnectingTestCase):
|
|||
|
||||
curs = self.conn.cursor()
|
||||
obj = Decimal('123.45')
|
||||
dumps = lambda obj: json.dumps(obj, cls=DecimalEncoder)
|
||||
|
||||
def dumps(obj):
|
||||
return json.dumps(obj, cls=DecimalEncoder)
|
||||
self.assertEqual(curs.mogrify("%s", (Json(obj, dumps=dumps),)),
|
||||
b"'123.45'")
|
||||
|
||||
|
@ -923,7 +941,6 @@ class JsonTestCase(ConnectingTestCase):
|
|||
finally:
|
||||
del psycopg2.extensions.adapters[dict, ext.ISQLQuote]
|
||||
|
||||
|
||||
def test_type_not_available(self):
|
||||
curs = self.conn.cursor()
|
||||
curs.execute("select oid from pg_type where typname = 'json'")
|
||||
|
@ -982,7 +999,9 @@ class JsonTestCase(ConnectingTestCase):
|
|||
@skip_if_no_json_type
|
||||
def test_loads(self):
|
||||
json = psycopg2.extras.json
|
||||
loads = lambda x: json.loads(x, parse_float=Decimal)
|
||||
|
||||
def loads(s):
|
||||
return json.loads(s, parse_float=Decimal)
|
||||
psycopg2.extras.register_json(self.conn, loads=loads)
|
||||
curs = self.conn.cursor()
|
||||
curs.execute("""select '{"a": 100.0, "b": null}'::json""")
|
||||
|
@ -998,7 +1017,9 @@ class JsonTestCase(ConnectingTestCase):
|
|||
|
||||
old = psycopg2.extensions.string_types.get(114)
|
||||
olda = psycopg2.extensions.string_types.get(199)
|
||||
loads = lambda x: psycopg2.extras.json.loads(x, parse_float=Decimal)
|
||||
|
||||
def loads(s):
|
||||
return psycopg2.extras.json.loads(s, parse_float=Decimal)
|
||||
try:
|
||||
new, newa = psycopg2.extras.register_json(
|
||||
loads=loads, oid=oid, array_oid=array_oid)
|
||||
|
@ -1020,7 +1041,8 @@ class JsonTestCase(ConnectingTestCase):
|
|||
def test_register_default(self):
|
||||
curs = self.conn.cursor()
|
||||
|
||||
loads = lambda x: psycopg2.extras.json.loads(x, parse_float=Decimal)
|
||||
def loads(s):
|
||||
return psycopg2.extras.json.loads(s, parse_float=Decimal)
|
||||
psycopg2.extras.register_default_json(curs, loads=loads)
|
||||
|
||||
curs.execute("""select '{"a": 100.0, "b": null}'::json""")
|
||||
|
@ -1070,6 +1092,7 @@ class JsonTestCase(ConnectingTestCase):
|
|||
def skip_if_no_jsonb_type(f):
|
||||
return skip_before_postgres(9, 4)(f)
|
||||
|
||||
|
||||
class JsonbTestCase(ConnectingTestCase):
|
||||
@staticmethod
|
||||
def myloads(s):
|
||||
|
@ -1118,7 +1141,10 @@ class JsonbTestCase(ConnectingTestCase):
|
|||
|
||||
def test_loads(self):
|
||||
json = psycopg2.extras.json
|
||||
loads = lambda x: json.loads(x, parse_float=Decimal)
|
||||
|
||||
def loads(s):
|
||||
return json.loads(s, parse_float=Decimal)
|
||||
|
||||
psycopg2.extras.register_json(self.conn, loads=loads, name='jsonb')
|
||||
curs = self.conn.cursor()
|
||||
curs.execute("""select '{"a": 100.0, "b": null}'::jsonb""")
|
||||
|
@ -1134,7 +1160,9 @@ class JsonbTestCase(ConnectingTestCase):
|
|||
def test_register_default(self):
|
||||
curs = self.conn.cursor()
|
||||
|
||||
loads = lambda x: psycopg2.extras.json.loads(x, parse_float=Decimal)
|
||||
def loads(s):
|
||||
return psycopg2.extras.json.loads(s, parse_float=Decimal)
|
||||
|
||||
psycopg2.extras.register_default_jsonb(curs, loads=loads)
|
||||
|
||||
curs.execute("""select '{"a": 100.0, "b": null}'::jsonb""")
|
||||
|
@ -1200,7 +1228,7 @@ class RangeTestCase(unittest.TestCase):
|
|||
('[)', True, False),
|
||||
('(]', False, True),
|
||||
('()', False, False),
|
||||
('[]', True, True),]:
|
||||
('[]', True, True)]:
|
||||
r = Range(10, 20, bounds)
|
||||
self.assertEqual(r.lower, 10)
|
||||
self.assertEqual(r.upper, 20)
|
||||
|
@ -1294,11 +1322,11 @@ class RangeTestCase(unittest.TestCase):
|
|||
self.assert_(not Range(empty=True))
|
||||
|
||||
def test_eq_hash(self):
|
||||
from psycopg2.extras import Range
|
||||
def assert_equal(r1, r2):
|
||||
self.assert_(r1 == r2)
|
||||
self.assert_(hash(r1) == hash(r2))
|
||||
|
||||
from psycopg2.extras import Range
|
||||
assert_equal(Range(empty=True), Range(empty=True))
|
||||
assert_equal(Range(), Range())
|
||||
assert_equal(Range(10, None), Range(10, None))
|
||||
|
@ -1321,8 +1349,11 @@ class RangeTestCase(unittest.TestCase):
|
|||
def test_eq_subclass(self):
|
||||
from psycopg2.extras import Range, NumericRange
|
||||
|
||||
class IntRange(NumericRange): pass
|
||||
class PositiveIntRange(IntRange): pass
|
||||
class IntRange(NumericRange):
|
||||
pass
|
||||
|
||||
class PositiveIntRange(IntRange):
|
||||
pass
|
||||
|
||||
self.assertEqual(Range(10, 20), IntRange(10, 20))
|
||||
self.assertEqual(PositiveIntRange(10, 20), IntRange(10, 20))
|
||||
|
@ -1508,7 +1539,8 @@ class RangeCasterTestCase(ConnectingTestCase):
|
|||
from psycopg2.tz import FixedOffsetTimezone
|
||||
cur = self.conn.cursor()
|
||||
ts1 = datetime(2000, 1, 1, tzinfo=FixedOffsetTimezone(600))
|
||||
ts2 = datetime(2000,12,31,23,59,59,999, tzinfo=FixedOffsetTimezone(600))
|
||||
ts2 = datetime(2000, 12, 31, 23, 59, 59, 999,
|
||||
tzinfo=FixedOffsetTimezone(600))
|
||||
cur.execute("select tstzrange(%s, %s, '[]')", (ts1, ts2))
|
||||
r = cur.fetchone()[0]
|
||||
self.assert_(isinstance(r, DateTimeTZRange))
|
||||
|
@ -1599,7 +1631,8 @@ class RangeCasterTestCase(ConnectingTestCase):
|
|||
self.assert_(r1.isempty)
|
||||
|
||||
ts1 = datetime(2000, 1, 1, tzinfo=FixedOffsetTimezone(600))
|
||||
ts2 = datetime(2000,12,31,23,59,59,999, tzinfo=FixedOffsetTimezone(600))
|
||||
ts2 = datetime(2000, 12, 31, 23, 59, 59, 999,
|
||||
tzinfo=FixedOffsetTimezone(600))
|
||||
r = DateTimeTZRange(ts1, ts2, '(]')
|
||||
cur.execute("select %s", (r,))
|
||||
r1 = cur.fetchone()[0]
|
||||
|
@ -1736,6 +1769,6 @@ decorate_all_tests(RangeCasterTestCase, skip_if_no_range)
|
|||
def test_suite():
|
||||
return unittest.TestLoader().loadTestsFromName(__name__)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
|
|
|
@ -30,6 +30,7 @@ import psycopg2.extensions as ext
|
|||
|
||||
from testutils import unittest, ConnectingTestCase
|
||||
|
||||
|
||||
class WithTestCase(ConnectingTestCase):
|
||||
def setUp(self):
|
||||
ConnectingTestCase.setUp(self)
|
||||
|
@ -113,6 +114,7 @@ class WithConnectionTestCase(WithTestCase):
|
|||
|
||||
def test_subclass_commit(self):
|
||||
commits = []
|
||||
|
||||
class MyConn(ext.connection):
|
||||
def commit(self):
|
||||
commits.append(None)
|
||||
|
@ -131,6 +133,7 @@ class WithConnectionTestCase(WithTestCase):
|
|||
|
||||
def test_subclass_rollback(self):
|
||||
rollbacks = []
|
||||
|
||||
class MyConn(ext.connection):
|
||||
def rollback(self):
|
||||
rollbacks.append(None)
|
||||
|
@ -189,6 +192,7 @@ class WithCursorTestCase(WithTestCase):
|
|||
|
||||
def test_subclass(self):
|
||||
closes = []
|
||||
|
||||
class MyCurs(ext.cursor):
|
||||
def close(self):
|
||||
closes.append(None)
|
||||
|
|
|
@ -69,8 +69,8 @@ else:
|
|||
# Silence warnings caused by the stubbornness of the Python unittest
|
||||
# maintainers
|
||||
# http://bugs.python.org/issue9424
|
||||
if not hasattr(unittest.TestCase, 'assert_') \
|
||||
or unittest.TestCase.assert_ is not unittest.TestCase.assertTrue:
|
||||
if (not hasattr(unittest.TestCase, 'assert_')
|
||||
or unittest.TestCase.assert_ is not unittest.TestCase.assertTrue):
|
||||
# mavaff...
|
||||
unittest.TestCase.assert_ = unittest.TestCase.assertTrue
|
||||
unittest.TestCase.failUnless = unittest.TestCase.assertTrue
|
||||
|
@ -175,7 +175,7 @@ def skip_if_no_uuid(f):
|
|||
@wraps(f)
|
||||
def skip_if_no_uuid_(self):
|
||||
try:
|
||||
import uuid
|
||||
import uuid # noqa
|
||||
except ImportError:
|
||||
return self.skipTest("uuid not available in this Python version")
|
||||
|
||||
|
@ -223,7 +223,7 @@ def skip_if_no_namedtuple(f):
|
|||
@wraps(f)
|
||||
def skip_if_no_namedtuple_(self):
|
||||
try:
|
||||
from collections import namedtuple
|
||||
from collections import namedtuple # noqa
|
||||
except ImportError:
|
||||
return self.skipTest("collections.namedtuple not available")
|
||||
else:
|
||||
|
@ -237,7 +237,7 @@ def skip_if_no_iobase(f):
|
|||
@wraps(f)
|
||||
def skip_if_no_iobase_(self):
|
||||
try:
|
||||
from io import TextIOBase
|
||||
from io import TextIOBase # noqa
|
||||
except ImportError:
|
||||
return self.skipTest("io.TextIOBase not found.")
|
||||
else:
|
||||
|
@ -249,6 +249,7 @@ def skip_if_no_iobase(f):
|
|||
def skip_before_postgres(*ver):
|
||||
"""Skip a test on PostgreSQL before a certain version."""
|
||||
ver = ver + (0,) * (3 - len(ver))
|
||||
|
||||
def skip_before_postgres_(f):
|
||||
@wraps(f)
|
||||
def skip_before_postgres__(self):
|
||||
|
@ -261,9 +262,11 @@ def skip_before_postgres(*ver):
|
|||
return skip_before_postgres__
|
||||
return skip_before_postgres_
|
||||
|
||||
|
||||
def skip_after_postgres(*ver):
|
||||
"""Skip a test on PostgreSQL after (including) a certain version."""
|
||||
ver = ver + (0,) * (3 - len(ver))
|
||||
|
||||
def skip_after_postgres_(f):
|
||||
@wraps(f)
|
||||
def skip_after_postgres__(self):
|
||||
|
@ -276,6 +279,7 @@ def skip_after_postgres(*ver):
|
|||
return skip_after_postgres__
|
||||
return skip_after_postgres_
|
||||
|
||||
|
||||
def libpq_version():
|
||||
import psycopg2
|
||||
v = psycopg2.__libpq_version__
|
||||
|
@ -283,9 +287,11 @@ def libpq_version():
|
|||
v = psycopg2.extensions.libpq_version()
|
||||
return v
|
||||
|
||||
|
||||
def skip_before_libpq(*ver):
|
||||
"""Skip a test if libpq we're linked to is older than a certain version."""
|
||||
ver = ver + (0,) * (3 - len(ver))
|
||||
|
||||
def skip_before_libpq_(f):
|
||||
@wraps(f)
|
||||
def skip_before_libpq__(self):
|
||||
|
@ -298,9 +304,11 @@ def skip_before_libpq(*ver):
|
|||
return skip_before_libpq__
|
||||
return skip_before_libpq_
|
||||
|
||||
|
||||
def skip_after_libpq(*ver):
|
||||
"""Skip a test if libpq we're linked to is newer than a certain version."""
|
||||
ver = ver + (0,) * (3 - len(ver))
|
||||
|
||||
def skip_after_libpq_(f):
|
||||
@wraps(f)
|
||||
def skip_after_libpq__(self):
|
||||
|
@ -313,6 +321,7 @@ def skip_after_libpq(*ver):
|
|||
return skip_after_libpq__
|
||||
return skip_after_libpq_
|
||||
|
||||
|
||||
def skip_before_python(*ver):
|
||||
"""Skip a test on Python before a certain version."""
|
||||
def skip_before_python_(f):
|
||||
|
@ -327,6 +336,7 @@ def skip_before_python(*ver):
|
|||
return skip_before_python__
|
||||
return skip_before_python_
|
||||
|
||||
|
||||
def skip_from_python(*ver):
|
||||
"""Skip a test on Python after (including) a certain version."""
|
||||
def skip_from_python_(f):
|
||||
|
@ -341,6 +351,7 @@ def skip_from_python(*ver):
|
|||
return skip_from_python__
|
||||
return skip_from_python_
|
||||
|
||||
|
||||
def skip_if_no_superuser(f):
|
||||
"""Skip a test if the database user running the test is not a superuser"""
|
||||
@wraps(f)
|
||||
|
@ -357,6 +368,7 @@ def skip_if_no_superuser(f):
|
|||
|
||||
return skip_if_no_superuser_
|
||||
|
||||
|
||||
def skip_if_green(reason):
|
||||
def skip_if_green_(f):
|
||||
@wraps(f)
|
||||
|
@ -372,6 +384,7 @@ def skip_if_green(reason):
|
|||
|
||||
skip_copy_if_green = skip_if_green("copy in async mode currently not supported")
|
||||
|
||||
|
||||
def skip_if_no_getrefcount(f):
|
||||
@wraps(f)
|
||||
def skip_if_no_getrefcount_(self):
|
||||
|
@ -381,6 +394,7 @@ def skip_if_no_getrefcount(f):
|
|||
return f(self)
|
||||
return skip_if_no_getrefcount_
|
||||
|
||||
|
||||
def skip_if_windows(f):
|
||||
"""Skip a test if run on windows"""
|
||||
@wraps(f)
|
||||
|
@ -419,6 +433,7 @@ def script_to_py3(script):
|
|||
f2.close()
|
||||
os.remove(filename)
|
||||
|
||||
|
||||
class py3_raises_typeerror(object):
|
||||
|
||||
def __enter__(self):
|
||||
|
|
Loading…
Reference in New Issue
Block a user