mirror of
https://github.com/psycopg/psycopg2.git
synced 2025-02-26 05:30:33 +03:00
Merge branch 'master' into ws
This commit is contained in:
commit
2218e73c28
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -4,6 +4,7 @@ MANIFEST
|
||||||
*.pidb
|
*.pidb
|
||||||
*.pyc
|
*.pyc
|
||||||
*.sw[po]
|
*.sw[po]
|
||||||
|
*.egg-info/
|
||||||
dist/*
|
dist/*
|
||||||
build/*
|
build/*
|
||||||
doc/src/_build/*
|
doc/src/_build/*
|
||||||
|
|
|
@ -19,10 +19,7 @@ def main():
|
||||||
|
|
||||||
def iter_file_base(fn):
|
def iter_file_base(fn):
|
||||||
f = open(fn)
|
f = open(fn)
|
||||||
if sys.version_info[0] >= 3:
|
|
||||||
have_line = iter(f).__next__
|
have_line = iter(f).__next__
|
||||||
else:
|
|
||||||
have_line = iter(f).next
|
|
||||||
|
|
||||||
while not have_line().startswith('.. toctree'):
|
while not have_line().startswith('.. toctree'):
|
||||||
pass
|
pass
|
||||||
|
|
|
@ -17,13 +17,6 @@ from datetime import datetime
|
||||||
import psycopg2
|
import psycopg2
|
||||||
from psycopg2.extensions import adapt, register_adapter
|
from psycopg2.extensions import adapt, register_adapter
|
||||||
|
|
||||||
try:
|
|
||||||
sorted()
|
|
||||||
except:
|
|
||||||
def sorted(seq):
|
|
||||||
seq.sort()
|
|
||||||
return seq
|
|
||||||
|
|
||||||
# Here is the adapter for every object that we may ever need to
|
# Here is the adapter for every object that we may ever need to
|
||||||
# insert in the database. It receives the original object and does
|
# insert in the database. It receives the original object and does
|
||||||
# its job on that instance
|
# its job on that instance
|
||||||
|
|
|
@ -110,16 +110,16 @@ class DictCursorBase(_cursor):
|
||||||
try:
|
try:
|
||||||
if self._prefetch:
|
if self._prefetch:
|
||||||
res = super(DictCursorBase, self).__iter__()
|
res = super(DictCursorBase, self).__iter__()
|
||||||
first = res.next()
|
first = next(res)
|
||||||
if self._query_executed:
|
if self._query_executed:
|
||||||
self._build_index()
|
self._build_index()
|
||||||
if not self._prefetch:
|
if not self._prefetch:
|
||||||
res = super(DictCursorBase, self).__iter__()
|
res = super(DictCursorBase, self).__iter__()
|
||||||
first = res.next()
|
first = next(res)
|
||||||
|
|
||||||
yield first
|
yield first
|
||||||
while 1:
|
while 1:
|
||||||
yield res.next()
|
yield next(res)
|
||||||
except StopIteration:
|
except StopIteration:
|
||||||
return
|
return
|
||||||
|
|
||||||
|
@ -349,7 +349,7 @@ class NamedTupleCursor(_cursor):
|
||||||
def __iter__(self):
|
def __iter__(self):
|
||||||
try:
|
try:
|
||||||
it = super(NamedTupleCursor, self).__iter__()
|
it = super(NamedTupleCursor, self).__iter__()
|
||||||
t = it.next()
|
t = next(it)
|
||||||
|
|
||||||
nt = self.Record
|
nt = self.Record
|
||||||
if nt is None:
|
if nt is None:
|
||||||
|
@ -358,7 +358,7 @@ class NamedTupleCursor(_cursor):
|
||||||
yield nt._make(t)
|
yield nt._make(t)
|
||||||
|
|
||||||
while 1:
|
while 1:
|
||||||
yield nt._make(it.next())
|
yield nt._make(next(it))
|
||||||
except StopIteration:
|
except StopIteration:
|
||||||
return
|
return
|
||||||
|
|
||||||
|
@ -1144,7 +1144,7 @@ def _paginate(seq, page_size):
|
||||||
while 1:
|
while 1:
|
||||||
try:
|
try:
|
||||||
for i in xrange(page_size):
|
for i in xrange(page_size):
|
||||||
page.append(it.next())
|
page.append(next(it))
|
||||||
yield page
|
yield page
|
||||||
page = []
|
page = []
|
||||||
except StopIteration:
|
except StopIteration:
|
||||||
|
|
|
@ -276,7 +276,7 @@ class SQL(Composable):
|
||||||
rv = []
|
rv = []
|
||||||
it = iter(seq)
|
it = iter(seq)
|
||||||
try:
|
try:
|
||||||
rv.append(it.next())
|
rv.append(next(it))
|
||||||
except StopIteration:
|
except StopIteration:
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -29,7 +29,7 @@ warnings.simplefilter('error') # noqa
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
from testconfig import dsn
|
from testconfig import dsn
|
||||||
from testutils import unittest
|
import unittest
|
||||||
|
|
||||||
import test_async
|
import test_async
|
||||||
import test_bugX000
|
import test_bugX000
|
||||||
|
|
|
@ -23,7 +23,8 @@
|
||||||
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
|
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
|
||||||
# License for more details.
|
# License for more details.
|
||||||
|
|
||||||
from testutils import unittest, skip_before_postgres, slow
|
import unittest
|
||||||
|
from testutils import skip_before_postgres, slow
|
||||||
|
|
||||||
import psycopg2
|
import psycopg2
|
||||||
from psycopg2 import extensions as ext
|
from psycopg2 import extensions as ext
|
||||||
|
|
|
@ -29,7 +29,8 @@ import psycopg2
|
||||||
from psycopg2 import extras
|
from psycopg2 import extras
|
||||||
|
|
||||||
from testconfig import dsn
|
from testconfig import dsn
|
||||||
from testutils import ConnectingTestCase, unittest, skip_before_postgres, slow
|
import unittest
|
||||||
|
from testutils import ConnectingTestCase, skip_before_postgres, slow
|
||||||
|
|
||||||
from test_replication import ReplicationTestCase, skip_repl_if_green
|
from test_replication import ReplicationTestCase, skip_repl_if_green
|
||||||
from psycopg2.extras import LogicalReplicationConnection, StopReplication
|
from psycopg2.extras import LogicalReplicationConnection, StopReplication
|
||||||
|
|
|
@ -31,7 +31,8 @@ import psycopg2.extensions
|
||||||
from psycopg2 import extras
|
from psycopg2 import extras
|
||||||
|
|
||||||
from testconfig import dsn
|
from testconfig import dsn
|
||||||
from testutils import unittest, ConnectingTestCase, skip_before_postgres, slow
|
import unittest
|
||||||
|
from testutils import ConnectingTestCase, skip_before_postgres, slow
|
||||||
|
|
||||||
|
|
||||||
class CancelTests(ConnectingTestCase):
|
class CancelTests(ConnectingTestCase):
|
||||||
|
|
|
@ -35,7 +35,7 @@ import psycopg2.errorcodes
|
||||||
from psycopg2 import extensions as ext
|
from psycopg2 import extensions as ext
|
||||||
|
|
||||||
from testutils import (
|
from testutils import (
|
||||||
script_to_py3, unittest, decorate_all_tests, skip_if_no_superuser,
|
unittest, decorate_all_tests, skip_if_no_superuser,
|
||||||
skip_before_postgres, skip_after_postgres, skip_before_libpq,
|
skip_before_postgres, skip_after_postgres, skip_before_libpq,
|
||||||
ConnectingTestCase, skip_if_tpc_disabled, skip_if_windows, slow)
|
ConnectingTestCase, skip_if_tpc_disabled, skip_if_windows, slow)
|
||||||
|
|
||||||
|
@ -1566,7 +1566,7 @@ while True:
|
||||||
cur.execute(%(query)r, ("Hello, world!",))
|
cur.execute(%(query)r, ("Hello, world!",))
|
||||||
""" % {'dsn': dsn, 'query': query})
|
""" % {'dsn': dsn, 'query': query})
|
||||||
|
|
||||||
proc = sp.Popen([sys.executable, '-c', script_to_py3(script)],
|
proc = sp.Popen([sys.executable, '-c', script],
|
||||||
stdout=sp.PIPE, stderr=sp.PIPE)
|
stdout=sp.PIPE, stderr=sp.PIPE)
|
||||||
(out, err) = proc.communicate()
|
(out, err) = proc.communicate()
|
||||||
self.assertNotEqual(proc.returncode, 0)
|
self.assertNotEqual(proc.returncode, 0)
|
||||||
|
|
|
@ -24,7 +24,8 @@
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
import string
|
import string
|
||||||
from testutils import (unittest, ConnectingTestCase, decorate_all_tests,
|
import unittest
|
||||||
|
from testutils import (ConnectingTestCase, decorate_all_tests,
|
||||||
skip_before_postgres, slow)
|
skip_before_postgres, slow)
|
||||||
from cStringIO import StringIO
|
from cStringIO import StringIO
|
||||||
from itertools import cycle, izip
|
from itertools import cycle, izip
|
||||||
|
@ -32,7 +33,7 @@ from subprocess import Popen, PIPE
|
||||||
|
|
||||||
import psycopg2
|
import psycopg2
|
||||||
import psycopg2.extensions
|
import psycopg2.extensions
|
||||||
from testutils import skip_copy_if_green, script_to_py3
|
from testutils import skip_copy_if_green
|
||||||
from testconfig import dsn
|
from testconfig import dsn
|
||||||
|
|
||||||
|
|
||||||
|
@ -324,7 +325,7 @@ except psycopg2.ProgrammingError:
|
||||||
conn.close()
|
conn.close()
|
||||||
""" % {'dsn': dsn})
|
""" % {'dsn': dsn})
|
||||||
|
|
||||||
proc = Popen([sys.executable, '-c', script_to_py3(script)])
|
proc = Popen([sys.executable, '-c', script])
|
||||||
proc.communicate()
|
proc.communicate()
|
||||||
self.assertEqual(0, proc.returncode)
|
self.assertEqual(0, proc.returncode)
|
||||||
|
|
||||||
|
@ -343,7 +344,7 @@ except psycopg2.ProgrammingError:
|
||||||
conn.close()
|
conn.close()
|
||||||
""" % {'dsn': dsn})
|
""" % {'dsn': dsn})
|
||||||
|
|
||||||
proc = Popen([sys.executable, '-c', script_to_py3(script)], stdout=PIPE)
|
proc = Popen([sys.executable, '-c', script], stdout=PIPE)
|
||||||
proc.communicate()
|
proc.communicate()
|
||||||
self.assertEqual(0, proc.returncode)
|
self.assertEqual(0, proc.returncode)
|
||||||
|
|
||||||
|
|
|
@ -26,7 +26,8 @@ import time
|
||||||
import pickle
|
import pickle
|
||||||
import psycopg2
|
import psycopg2
|
||||||
import psycopg2.extensions
|
import psycopg2.extensions
|
||||||
from testutils import (unittest, ConnectingTestCase, skip_before_postgres,
|
import unittest
|
||||||
|
from testutils import (ConnectingTestCase, skip_before_postgres,
|
||||||
skip_if_no_getrefcount, slow, skip_if_no_superuser,
|
skip_if_no_getrefcount, slow, skip_if_no_superuser,
|
||||||
skip_if_windows)
|
skip_if_windows)
|
||||||
|
|
||||||
|
@ -334,9 +335,9 @@ class CursorTests(ConnectingTestCase):
|
||||||
# timestamp will not be influenced by the pause in Python world.
|
# timestamp will not be influenced by the pause in Python world.
|
||||||
curs.execute("""select clock_timestamp() from generate_series(1,2)""")
|
curs.execute("""select clock_timestamp() from generate_series(1,2)""")
|
||||||
i = iter(curs)
|
i = iter(curs)
|
||||||
t1 = (i.next())[0] # the brackets work around a 2to3 bug
|
t1 = next(i)[0]
|
||||||
time.sleep(0.2)
|
time.sleep(0.2)
|
||||||
t2 = (i.next())[0]
|
t2 = next(i)[0]
|
||||||
self.assert_((t2 - t1).microseconds * 1e-6 < 0.1,
|
self.assert_((t2 - t1).microseconds * 1e-6 < 0.1,
|
||||||
"named cursor records fetched in 2 roundtrips (delta: %s)"
|
"named cursor records fetched in 2 roundtrips (delta: %s)"
|
||||||
% (t2 - t1))
|
% (t2 - t1))
|
||||||
|
|
|
@ -25,7 +25,8 @@
|
||||||
import math
|
import math
|
||||||
import psycopg2
|
import psycopg2
|
||||||
from psycopg2.tz import FixedOffsetTimezone, ZERO
|
from psycopg2.tz import FixedOffsetTimezone, ZERO
|
||||||
from testutils import unittest, ConnectingTestCase, skip_before_postgres
|
import unittest
|
||||||
|
from testutils import ConnectingTestCase, skip_before_postgres
|
||||||
|
|
||||||
|
|
||||||
def total_seconds(d):
|
def total_seconds(d):
|
||||||
|
|
|
@ -22,7 +22,8 @@
|
||||||
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
|
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
|
||||||
# License for more details.
|
# License for more details.
|
||||||
|
|
||||||
from testutils import unittest, ConnectingTestCase, slow
|
import unittest
|
||||||
|
from testutils import ConnectingTestCase, slow
|
||||||
|
|
||||||
try:
|
try:
|
||||||
reload
|
reload
|
||||||
|
|
|
@ -18,7 +18,8 @@ import time
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
import psycopg2
|
import psycopg2
|
||||||
import psycopg2.extras
|
import psycopg2.extras
|
||||||
from testutils import unittest, ConnectingTestCase, skip_before_postgres
|
import unittest
|
||||||
|
from testutils import ConnectingTestCase, skip_before_postgres
|
||||||
|
|
||||||
|
|
||||||
class ExtrasDictCursorTests(ConnectingTestCase):
|
class ExtrasDictCursorTests(ConnectingTestCase):
|
||||||
|
@ -310,22 +311,22 @@ class NamedTupleCursorTest(ConnectingTestCase):
|
||||||
i = iter(curs)
|
i = iter(curs)
|
||||||
self.assertEqual(curs.rownumber, 0)
|
self.assertEqual(curs.rownumber, 0)
|
||||||
|
|
||||||
t = i.next()
|
t = next(i)
|
||||||
self.assertEqual(t.i, 1)
|
self.assertEqual(t.i, 1)
|
||||||
self.assertEqual(t.s, 'foo')
|
self.assertEqual(t.s, 'foo')
|
||||||
self.assertEqual(curs.rownumber, 1)
|
self.assertEqual(curs.rownumber, 1)
|
||||||
self.assertEqual(curs.rowcount, 3)
|
self.assertEqual(curs.rowcount, 3)
|
||||||
|
|
||||||
t = i.next()
|
t = next(i)
|
||||||
self.assertEqual(t.i, 2)
|
self.assertEqual(t.i, 2)
|
||||||
self.assertEqual(t.s, 'bar')
|
self.assertEqual(t.s, 'bar')
|
||||||
self.assertEqual(curs.rownumber, 2)
|
self.assertEqual(curs.rownumber, 2)
|
||||||
self.assertEqual(curs.rowcount, 3)
|
self.assertEqual(curs.rowcount, 3)
|
||||||
|
|
||||||
t = i.next()
|
t = next(i)
|
||||||
self.assertEqual(t.i, 3)
|
self.assertEqual(t.i, 3)
|
||||||
self.assertEqual(t.s, 'baz')
|
self.assertEqual(t.s, 'baz')
|
||||||
self.assertRaises(StopIteration, i.next)
|
self.assertRaises(StopIteration, next, i)
|
||||||
self.assertEqual(curs.rownumber, 3)
|
self.assertEqual(curs.rownumber, 3)
|
||||||
self.assertEqual(curs.rowcount, 3)
|
self.assertEqual(curs.rowcount, 3)
|
||||||
|
|
||||||
|
|
|
@ -17,7 +17,7 @@
|
||||||
from datetime import date
|
from datetime import date
|
||||||
|
|
||||||
import testutils
|
import testutils
|
||||||
from testutils import unittest
|
import unittest
|
||||||
|
|
||||||
import psycopg2
|
import psycopg2
|
||||||
import psycopg2.extras
|
import psycopg2.extras
|
||||||
|
|
|
@ -20,7 +20,7 @@ import sys
|
||||||
from functools import wraps
|
from functools import wraps
|
||||||
|
|
||||||
import testutils
|
import testutils
|
||||||
from testutils import unittest
|
import unittest
|
||||||
|
|
||||||
import psycopg2
|
import psycopg2
|
||||||
import psycopg2.extras
|
import psycopg2.extras
|
||||||
|
|
|
@ -29,7 +29,8 @@ from functools import wraps
|
||||||
|
|
||||||
import psycopg2
|
import psycopg2
|
||||||
import psycopg2.extensions
|
import psycopg2.extensions
|
||||||
from testutils import (unittest, decorate_all_tests, skip_if_tpc_disabled,
|
import unittest
|
||||||
|
from testutils import (decorate_all_tests, skip_if_tpc_disabled,
|
||||||
ConnectingTestCase, skip_if_green, slow)
|
ConnectingTestCase, skip_if_green, slow)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -26,8 +26,9 @@ import os
|
||||||
import sys
|
import sys
|
||||||
from subprocess import Popen
|
from subprocess import Popen
|
||||||
|
|
||||||
from testutils import (unittest, skip_before_postgres,
|
import unittest
|
||||||
ConnectingTestCase, skip_copy_if_green, script_to_py3, slow)
|
from testutils import (skip_before_postgres,
|
||||||
|
ConnectingTestCase, skip_copy_if_green, slow)
|
||||||
|
|
||||||
import psycopg2
|
import psycopg2
|
||||||
|
|
||||||
|
@ -322,7 +323,7 @@ sys.path.insert(0, %r)
|
||||||
import _psycopg
|
import _psycopg
|
||||||
""" % (pardir, pkgdir))
|
""" % (pardir, pkgdir))
|
||||||
|
|
||||||
proc = Popen([sys.executable, '-c', script_to_py3(script)])
|
proc = Popen([sys.executable, '-c', script])
|
||||||
proc.communicate()
|
proc.communicate()
|
||||||
self.assertEqual(0, proc.returncode)
|
self.assertEqual(0, proc.returncode)
|
||||||
|
|
||||||
|
|
|
@ -22,11 +22,11 @@
|
||||||
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
|
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
|
||||||
# License for more details.
|
# License for more details.
|
||||||
|
|
||||||
from testutils import unittest
|
import unittest
|
||||||
|
|
||||||
import psycopg2
|
import psycopg2
|
||||||
from psycopg2 import extensions
|
from psycopg2 import extensions
|
||||||
from testutils import ConnectingTestCase, script_to_py3, slow
|
from testutils import ConnectingTestCase, slow
|
||||||
from testconfig import dsn
|
from testconfig import dsn
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
|
@ -61,7 +61,7 @@ import %(module)s as psycopg2
|
||||||
import %(module)s.extensions as ext
|
import %(module)s.extensions as ext
|
||||||
conn = psycopg2.connect(%(dsn)r)
|
conn = psycopg2.connect(%(dsn)r)
|
||||||
conn.set_isolation_level(ext.ISOLATION_LEVEL_AUTOCOMMIT)
|
conn.set_isolation_level(ext.ISOLATION_LEVEL_AUTOCOMMIT)
|
||||||
print conn.get_backend_pid()
|
print(conn.get_backend_pid())
|
||||||
curs = conn.cursor()
|
curs = conn.cursor()
|
||||||
curs.execute("NOTIFY " %(name)r %(payload)r)
|
curs.execute("NOTIFY " %(name)r %(payload)r)
|
||||||
curs.close()
|
curs.close()
|
||||||
|
@ -70,7 +70,7 @@ conn.close()
|
||||||
'module': psycopg2.__name__,
|
'module': psycopg2.__name__,
|
||||||
'dsn': dsn, 'sec': sec, 'name': name, 'payload': payload})
|
'dsn': dsn, 'sec': sec, 'name': name, 'payload': payload})
|
||||||
|
|
||||||
return Popen([sys.executable, '-c', script_to_py3(script)], stdout=PIPE)
|
return Popen([sys.executable, '-c', script], stdout=PIPE)
|
||||||
|
|
||||||
@slow
|
@slow
|
||||||
def test_notifies_received_on_poll(self):
|
def test_notifies_received_on_poll(self):
|
||||||
|
|
|
@ -25,7 +25,8 @@
|
||||||
import dbapi20
|
import dbapi20
|
||||||
import dbapi20_tpc
|
import dbapi20_tpc
|
||||||
from testutils import skip_if_tpc_disabled
|
from testutils import skip_if_tpc_disabled
|
||||||
from testutils import unittest, decorate_all_tests
|
import unittest
|
||||||
|
from testutils import decorate_all_tests
|
||||||
import psycopg2
|
import psycopg2
|
||||||
|
|
||||||
from testconfig import dsn
|
from testconfig import dsn
|
||||||
|
|
|
@ -24,7 +24,8 @@
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
import testutils
|
import testutils
|
||||||
from testutils import unittest, ConnectingTestCase
|
import unittest
|
||||||
|
from testutils import ConnectingTestCase
|
||||||
|
|
||||||
import psycopg2
|
import psycopg2
|
||||||
import psycopg2.extensions
|
import psycopg2.extensions
|
||||||
|
|
|
@ -27,7 +27,8 @@ from psycopg2.extras import (
|
||||||
PhysicalReplicationConnection, LogicalReplicationConnection, StopReplication)
|
PhysicalReplicationConnection, LogicalReplicationConnection, StopReplication)
|
||||||
|
|
||||||
import testconfig
|
import testconfig
|
||||||
from testutils import unittest, ConnectingTestCase
|
import unittest
|
||||||
|
from testutils import ConnectingTestCase
|
||||||
from testutils import skip_before_postgres, skip_if_green
|
from testutils import skip_before_postgres, skip_if_green
|
||||||
|
|
||||||
skip_repl_if_green = skip_if_green("replication not supported in green mode")
|
skip_repl_if_green = skip_if_green("replication not supported in green mode")
|
||||||
|
|
|
@ -24,7 +24,8 @@
|
||||||
|
|
||||||
import datetime as dt
|
import datetime as dt
|
||||||
from cStringIO import StringIO
|
from cStringIO import StringIO
|
||||||
from testutils import (unittest, ConnectingTestCase,
|
import unittest
|
||||||
|
from testutils import (ConnectingTestCase,
|
||||||
skip_before_postgres, skip_before_python, skip_copy_if_green)
|
skip_before_postgres, skip_before_python, skip_copy_if_green)
|
||||||
|
|
||||||
import psycopg2
|
import psycopg2
|
||||||
|
@ -344,11 +345,11 @@ class ComposedTest(ConnectingTestCase):
|
||||||
def test_iter(self):
|
def test_iter(self):
|
||||||
obj = sql.Composed([sql.SQL("foo"), sql.SQL('bar')])
|
obj = sql.Composed([sql.SQL("foo"), sql.SQL('bar')])
|
||||||
it = iter(obj)
|
it = iter(obj)
|
||||||
i = it.next()
|
i = next(it)
|
||||||
self.assertEqual(i, sql.SQL('foo'))
|
self.assertEqual(i, sql.SQL('foo'))
|
||||||
i = it.next()
|
i = next(it)
|
||||||
self.assertEqual(i, sql.SQL('bar'))
|
self.assertEqual(i, sql.SQL('bar'))
|
||||||
self.assertRaises(StopIteration, it.next)
|
self.assertRaises(StopIteration, next, it)
|
||||||
|
|
||||||
|
|
||||||
class PlaceholderTest(ConnectingTestCase):
|
class PlaceholderTest(ConnectingTestCase):
|
||||||
|
|
|
@ -23,7 +23,8 @@
|
||||||
# License for more details.
|
# License for more details.
|
||||||
|
|
||||||
import threading
|
import threading
|
||||||
from testutils import unittest, ConnectingTestCase, skip_before_postgres, slow
|
import unittest
|
||||||
|
from testutils import ConnectingTestCase, skip_before_postgres, slow
|
||||||
|
|
||||||
import psycopg2
|
import psycopg2
|
||||||
from psycopg2.extensions import (
|
from psycopg2.extensions import (
|
||||||
|
|
|
@ -27,7 +27,8 @@ import decimal
|
||||||
import sys
|
import sys
|
||||||
from functools import wraps
|
from functools import wraps
|
||||||
import testutils
|
import testutils
|
||||||
from testutils import unittest, ConnectingTestCase, decorate_all_tests
|
import unittest
|
||||||
|
from testutils import ConnectingTestCase, decorate_all_tests
|
||||||
|
|
||||||
import psycopg2
|
import psycopg2
|
||||||
|
|
||||||
|
|
|
@ -22,7 +22,8 @@ from datetime import date, datetime
|
||||||
from functools import wraps
|
from functools import wraps
|
||||||
from pickle import dumps, loads
|
from pickle import dumps, loads
|
||||||
|
|
||||||
from testutils import (unittest, skip_if_no_uuid, skip_before_postgres,
|
import unittest
|
||||||
|
from testutils import (skip_if_no_uuid, skip_before_postgres,
|
||||||
ConnectingTestCase, decorate_all_tests, py3_raises_typeerror, slow)
|
ConnectingTestCase, decorate_all_tests, py3_raises_typeerror, slow)
|
||||||
|
|
||||||
import psycopg2
|
import psycopg2
|
||||||
|
@ -812,30 +813,6 @@ class AdaptTypeTestCase(ConnectingTestCase):
|
||||||
return oid
|
return oid
|
||||||
|
|
||||||
|
|
||||||
def skip_if_json_module(f):
|
|
||||||
"""Skip a test if a Python json module *is* available"""
|
|
||||||
@wraps(f)
|
|
||||||
def skip_if_json_module_(self):
|
|
||||||
if psycopg2.extras.json is not None:
|
|
||||||
return self.skipTest("json module is available")
|
|
||||||
|
|
||||||
return f(self)
|
|
||||||
|
|
||||||
return skip_if_json_module_
|
|
||||||
|
|
||||||
|
|
||||||
def skip_if_no_json_module(f):
|
|
||||||
"""Skip a test if no Python json module is available"""
|
|
||||||
@wraps(f)
|
|
||||||
def skip_if_no_json_module_(self):
|
|
||||||
if psycopg2.extras.json is None:
|
|
||||||
return self.skipTest("json module not available")
|
|
||||||
|
|
||||||
return f(self)
|
|
||||||
|
|
||||||
return skip_if_no_json_module_
|
|
||||||
|
|
||||||
|
|
||||||
def skip_if_no_json_type(f):
|
def skip_if_no_json_type(f):
|
||||||
"""Skip a test if PostgreSQL json type is not available"""
|
"""Skip a test if PostgreSQL json type is not available"""
|
||||||
@wraps(f)
|
@wraps(f)
|
||||||
|
@ -851,23 +828,6 @@ def skip_if_no_json_type(f):
|
||||||
|
|
||||||
|
|
||||||
class JsonTestCase(ConnectingTestCase):
|
class JsonTestCase(ConnectingTestCase):
|
||||||
@skip_if_json_module
|
|
||||||
def test_module_not_available(self):
|
|
||||||
from psycopg2.extras import Json
|
|
||||||
self.assertRaises(ImportError, Json(None).getquoted)
|
|
||||||
|
|
||||||
@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
|
|
||||||
return "hi"
|
|
||||||
|
|
||||||
self.assertEqual(MyJson(None).getquoted(), "'hi'")
|
|
||||||
|
|
||||||
@skip_if_no_json_module
|
|
||||||
def test_adapt(self):
|
def test_adapt(self):
|
||||||
from psycopg2.extras import json, Json
|
from psycopg2.extras import json, Json
|
||||||
|
|
||||||
|
@ -879,7 +839,6 @@ class JsonTestCase(ConnectingTestCase):
|
||||||
self.assertQuotedEqual(curs.mogrify("%s", (Json(obj),)),
|
self.assertQuotedEqual(curs.mogrify("%s", (Json(obj),)),
|
||||||
psycopg2.extensions.QuotedString(json.dumps(obj)).getquoted())
|
psycopg2.extensions.QuotedString(json.dumps(obj)).getquoted())
|
||||||
|
|
||||||
@skip_if_no_json_module
|
|
||||||
def test_adapt_dumps(self):
|
def test_adapt_dumps(self):
|
||||||
from psycopg2.extras import json, Json
|
from psycopg2.extras import json, Json
|
||||||
|
|
||||||
|
@ -897,7 +856,6 @@ class JsonTestCase(ConnectingTestCase):
|
||||||
self.assertQuotedEqual(curs.mogrify("%s", (Json(obj, dumps=dumps),)),
|
self.assertQuotedEqual(curs.mogrify("%s", (Json(obj, dumps=dumps),)),
|
||||||
b"'123.45'")
|
b"'123.45'")
|
||||||
|
|
||||||
@skip_if_no_json_module
|
|
||||||
def test_adapt_subclass(self):
|
def test_adapt_subclass(self):
|
||||||
from psycopg2.extras import json, Json
|
from psycopg2.extras import json, Json
|
||||||
|
|
||||||
|
@ -915,7 +873,6 @@ class JsonTestCase(ConnectingTestCase):
|
||||||
obj = Decimal('123.45')
|
obj = Decimal('123.45')
|
||||||
self.assertQuotedEqual(curs.mogrify("%s", (MyJson(obj),)), b"'123.45'")
|
self.assertQuotedEqual(curs.mogrify("%s", (MyJson(obj),)), b"'123.45'")
|
||||||
|
|
||||||
@skip_if_no_json_module
|
|
||||||
def test_register_on_dict(self):
|
def test_register_on_dict(self):
|
||||||
from psycopg2.extras import Json
|
from psycopg2.extras import Json
|
||||||
psycopg2.extensions.register_adapter(dict, Json)
|
psycopg2.extensions.register_adapter(dict, Json)
|
||||||
|
@ -937,7 +894,6 @@ class JsonTestCase(ConnectingTestCase):
|
||||||
self.assertRaises(psycopg2.ProgrammingError,
|
self.assertRaises(psycopg2.ProgrammingError,
|
||||||
psycopg2.extras.register_json, self.conn)
|
psycopg2.extras.register_json, self.conn)
|
||||||
|
|
||||||
@skip_if_no_json_module
|
|
||||||
@skip_before_postgres(9, 2)
|
@skip_before_postgres(9, 2)
|
||||||
def test_default_cast(self):
|
def test_default_cast(self):
|
||||||
curs = self.conn.cursor()
|
curs = self.conn.cursor()
|
||||||
|
@ -948,7 +904,6 @@ class JsonTestCase(ConnectingTestCase):
|
||||||
curs.execute("""select array['{"a": 100.0, "b": null}']::json[]""")
|
curs.execute("""select array['{"a": 100.0, "b": null}']::json[]""")
|
||||||
self.assertEqual(curs.fetchone()[0], [{'a': 100.0, 'b': None}])
|
self.assertEqual(curs.fetchone()[0], [{'a': 100.0, 'b': None}])
|
||||||
|
|
||||||
@skip_if_no_json_module
|
|
||||||
@skip_if_no_json_type
|
@skip_if_no_json_type
|
||||||
def test_register_on_connection(self):
|
def test_register_on_connection(self):
|
||||||
psycopg2.extras.register_json(self.conn)
|
psycopg2.extras.register_json(self.conn)
|
||||||
|
@ -956,7 +911,6 @@ class JsonTestCase(ConnectingTestCase):
|
||||||
curs.execute("""select '{"a": 100.0, "b": null}'::json""")
|
curs.execute("""select '{"a": 100.0, "b": null}'::json""")
|
||||||
self.assertEqual(curs.fetchone()[0], {'a': 100.0, 'b': None})
|
self.assertEqual(curs.fetchone()[0], {'a': 100.0, 'b': None})
|
||||||
|
|
||||||
@skip_if_no_json_module
|
|
||||||
@skip_if_no_json_type
|
@skip_if_no_json_type
|
||||||
def test_register_on_cursor(self):
|
def test_register_on_cursor(self):
|
||||||
curs = self.conn.cursor()
|
curs = self.conn.cursor()
|
||||||
|
@ -964,7 +918,6 @@ class JsonTestCase(ConnectingTestCase):
|
||||||
curs.execute("""select '{"a": 100.0, "b": null}'::json""")
|
curs.execute("""select '{"a": 100.0, "b": null}'::json""")
|
||||||
self.assertEqual(curs.fetchone()[0], {'a': 100.0, 'b': None})
|
self.assertEqual(curs.fetchone()[0], {'a': 100.0, 'b': None})
|
||||||
|
|
||||||
@skip_if_no_json_module
|
|
||||||
@skip_if_no_json_type
|
@skip_if_no_json_type
|
||||||
def test_register_globally(self):
|
def test_register_globally(self):
|
||||||
old = psycopg2.extensions.string_types.get(114)
|
old = psycopg2.extensions.string_types.get(114)
|
||||||
|
@ -982,7 +935,6 @@ class JsonTestCase(ConnectingTestCase):
|
||||||
if olda:
|
if olda:
|
||||||
psycopg2.extensions.register_type(olda)
|
psycopg2.extensions.register_type(olda)
|
||||||
|
|
||||||
@skip_if_no_json_module
|
|
||||||
@skip_if_no_json_type
|
@skip_if_no_json_type
|
||||||
def test_loads(self):
|
def test_loads(self):
|
||||||
json = psycopg2.extras.json
|
json = psycopg2.extras.json
|
||||||
|
@ -996,7 +948,6 @@ class JsonTestCase(ConnectingTestCase):
|
||||||
self.assert_(isinstance(data['a'], Decimal))
|
self.assert_(isinstance(data['a'], Decimal))
|
||||||
self.assertEqual(data['a'], Decimal('100.0'))
|
self.assertEqual(data['a'], Decimal('100.0'))
|
||||||
|
|
||||||
@skip_if_no_json_module
|
|
||||||
@skip_if_no_json_type
|
@skip_if_no_json_type
|
||||||
def test_no_conn_curs(self):
|
def test_no_conn_curs(self):
|
||||||
from psycopg2._json import _get_json_oids
|
from psycopg2._json import _get_json_oids
|
||||||
|
@ -1023,7 +974,6 @@ class JsonTestCase(ConnectingTestCase):
|
||||||
if olda:
|
if olda:
|
||||||
psycopg2.extensions.register_type(olda)
|
psycopg2.extensions.register_type(olda)
|
||||||
|
|
||||||
@skip_if_no_json_module
|
|
||||||
@skip_before_postgres(9, 2)
|
@skip_before_postgres(9, 2)
|
||||||
def test_register_default(self):
|
def test_register_default(self):
|
||||||
curs = self.conn.cursor()
|
curs = self.conn.cursor()
|
||||||
|
@ -1042,7 +992,6 @@ class JsonTestCase(ConnectingTestCase):
|
||||||
self.assert_(isinstance(data[0]['a'], Decimal))
|
self.assert_(isinstance(data[0]['a'], Decimal))
|
||||||
self.assertEqual(data[0]['a'], Decimal('100.0'))
|
self.assertEqual(data[0]['a'], Decimal('100.0'))
|
||||||
|
|
||||||
@skip_if_no_json_module
|
|
||||||
@skip_if_no_json_type
|
@skip_if_no_json_type
|
||||||
def test_null(self):
|
def test_null(self):
|
||||||
psycopg2.extras.register_json(self.conn)
|
psycopg2.extras.register_json(self.conn)
|
||||||
|
@ -1052,7 +1001,6 @@ class JsonTestCase(ConnectingTestCase):
|
||||||
curs.execute("""select NULL::json[]""")
|
curs.execute("""select NULL::json[]""")
|
||||||
self.assertEqual(curs.fetchone()[0], None)
|
self.assertEqual(curs.fetchone()[0], None)
|
||||||
|
|
||||||
@skip_if_no_json_module
|
|
||||||
def test_no_array_oid(self):
|
def test_no_array_oid(self):
|
||||||
curs = self.conn.cursor()
|
curs = self.conn.cursor()
|
||||||
t1, t2 = psycopg2.extras.register_json(curs, oid=25)
|
t1, t2 = psycopg2.extras.register_json(curs, oid=25)
|
||||||
|
@ -1064,7 +1012,6 @@ class JsonTestCase(ConnectingTestCase):
|
||||||
self.assertEqual(data['a'], 100)
|
self.assertEqual(data['a'], 100)
|
||||||
self.assertEqual(data['b'], None)
|
self.assertEqual(data['b'], None)
|
||||||
|
|
||||||
@skip_if_no_json_module
|
|
||||||
def test_str(self):
|
def test_str(self):
|
||||||
snowman = u"\u2603"
|
snowman = u"\u2603"
|
||||||
obj = {'a': [1, 2, snowman]}
|
obj = {'a': [1, 2, snowman]}
|
||||||
|
@ -1075,7 +1022,6 @@ class JsonTestCase(ConnectingTestCase):
|
||||||
self.assert_(s.startswith("'"))
|
self.assert_(s.startswith("'"))
|
||||||
self.assert_(s.endswith("'"))
|
self.assert_(s.endswith("'"))
|
||||||
|
|
||||||
@skip_if_no_json_module
|
|
||||||
@skip_before_postgres(8, 2)
|
@skip_before_postgres(8, 2)
|
||||||
def test_scs(self):
|
def test_scs(self):
|
||||||
cnn_on = self.connect(options="-c standard_conforming_strings=on")
|
cnn_on = self.connect(options="-c standard_conforming_strings=on")
|
||||||
|
@ -1188,7 +1134,6 @@ class JsonbTestCase(ConnectingTestCase):
|
||||||
curs.execute("""select NULL::jsonb[]""")
|
curs.execute("""select NULL::jsonb[]""")
|
||||||
self.assertEqual(curs.fetchone()[0], None)
|
self.assertEqual(curs.fetchone()[0], None)
|
||||||
|
|
||||||
decorate_all_tests(JsonbTestCase, skip_if_no_json_module)
|
|
||||||
decorate_all_tests(JsonbTestCase, skip_if_no_jsonb_type)
|
decorate_all_tests(JsonbTestCase, skip_if_no_jsonb_type)
|
||||||
|
|
||||||
|
|
||||||
|
@ -1619,7 +1564,7 @@ class RangeCasterTestCase(ConnectingTestCase):
|
||||||
from psycopg2.tz import FixedOffsetTimezone
|
from psycopg2.tz import FixedOffsetTimezone
|
||||||
cur = self.conn.cursor()
|
cur = self.conn.cursor()
|
||||||
|
|
||||||
d1 = date(2012, 01, 01)
|
d1 = date(2012, 1, 1)
|
||||||
d2 = date(2012, 12, 31)
|
d2 = date(2012, 12, 31)
|
||||||
r = DateRange(d1, d2)
|
r = DateRange(d1, d2)
|
||||||
cur.execute("select %s", (r,))
|
cur.execute("select %s", (r,))
|
||||||
|
|
|
@ -25,7 +25,8 @@
|
||||||
import psycopg2
|
import psycopg2
|
||||||
import psycopg2.extensions as ext
|
import psycopg2.extensions as ext
|
||||||
|
|
||||||
from testutils import unittest, ConnectingTestCase
|
import unittest
|
||||||
|
from testutils import ConnectingTestCase
|
||||||
|
|
||||||
|
|
||||||
class WithTestCase(ConnectingTestCase):
|
class WithTestCase(ConnectingTestCase):
|
||||||
|
|
|
@ -371,34 +371,6 @@ def skip_if_windows(f):
|
||||||
return skip_if_windows_
|
return skip_if_windows_
|
||||||
|
|
||||||
|
|
||||||
def script_to_py3(script):
|
|
||||||
"""Convert a script to Python3 syntax if required."""
|
|
||||||
if sys.version_info[0] < 3:
|
|
||||||
return script
|
|
||||||
|
|
||||||
import tempfile
|
|
||||||
f = tempfile.NamedTemporaryFile(suffix=".py", delete=False)
|
|
||||||
f.write(script.encode())
|
|
||||||
f.flush()
|
|
||||||
filename = f.name
|
|
||||||
f.close()
|
|
||||||
|
|
||||||
# 2to3 is way too chatty
|
|
||||||
import logging
|
|
||||||
logging.basicConfig(filename=os.devnull)
|
|
||||||
|
|
||||||
from lib2to3.main import main
|
|
||||||
if main("lib2to3.fixes", ['--no-diffs', '-w', '-n', filename]):
|
|
||||||
raise Exception('py3 conversion failed')
|
|
||||||
|
|
||||||
f2 = open(filename)
|
|
||||||
try:
|
|
||||||
return f2.read()
|
|
||||||
finally:
|
|
||||||
f2.close()
|
|
||||||
os.remove(filename)
|
|
||||||
|
|
||||||
|
|
||||||
class py3_raises_typeerror(object):
|
class py3_raises_typeerror(object):
|
||||||
def __enter__(self):
|
def __enter__(self):
|
||||||
pass
|
pass
|
||||||
|
|
Loading…
Reference in New Issue
Block a user