1
1
mirror of https://github.com/psycopg/psycopg2.git synced 2025-02-25 21:20:32 +03:00

Merge branch 'master' into ws

This commit is contained in:
Daniele Varrazzo 2017-12-02 14:47:29 +00:00
commit 2218e73c28
29 changed files with 69 additions and 144 deletions

1
.gitignore vendored
View File

@ -4,6 +4,7 @@ MANIFEST
*.pidb
*.pyc
*.sw[po]
*.egg-info/
dist/*
build/*
doc/src/_build/*

View File

@ -19,10 +19,7 @@ def main():
def iter_file_base(fn):
f = open(fn)
if sys.version_info[0] >= 3:
have_line = iter(f).__next__
else:
have_line = iter(f).next
have_line = iter(f).__next__
while not have_line().startswith('.. toctree'):
pass

View File

@ -17,13 +17,6 @@ from datetime import datetime
import psycopg2
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
# insert in the database. It receives the original object and does
# its job on that instance

View File

@ -110,16 +110,16 @@ class DictCursorBase(_cursor):
try:
if self._prefetch:
res = super(DictCursorBase, self).__iter__()
first = res.next()
first = next(res)
if self._query_executed:
self._build_index()
if not self._prefetch:
res = super(DictCursorBase, self).__iter__()
first = res.next()
first = next(res)
yield first
while 1:
yield res.next()
yield next(res)
except StopIteration:
return
@ -349,7 +349,7 @@ class NamedTupleCursor(_cursor):
def __iter__(self):
try:
it = super(NamedTupleCursor, self).__iter__()
t = it.next()
t = next(it)
nt = self.Record
if nt is None:
@ -358,7 +358,7 @@ class NamedTupleCursor(_cursor):
yield nt._make(t)
while 1:
yield nt._make(it.next())
yield nt._make(next(it))
except StopIteration:
return
@ -1144,7 +1144,7 @@ def _paginate(seq, page_size):
while 1:
try:
for i in xrange(page_size):
page.append(it.next())
page.append(next(it))
yield page
page = []
except StopIteration:

View File

@ -276,7 +276,7 @@ class SQL(Composable):
rv = []
it = iter(seq)
try:
rv.append(it.next())
rv.append(next(it))
except StopIteration:
pass
else:

View File

@ -29,7 +29,7 @@ warnings.simplefilter('error') # noqa
import sys
from testconfig import dsn
from testutils import unittest
import unittest
import test_async
import test_bugX000

View File

@ -23,7 +23,8 @@
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
# License for more details.
from testutils import unittest, skip_before_postgres, slow
import unittest
from testutils import skip_before_postgres, slow
import psycopg2
from psycopg2 import extensions as ext

View File

@ -29,7 +29,8 @@ import psycopg2
from psycopg2 import extras
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 psycopg2.extras import LogicalReplicationConnection, StopReplication

View File

@ -31,7 +31,8 @@ import psycopg2.extensions
from psycopg2 import extras
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):

View File

@ -35,7 +35,7 @@ import psycopg2.errorcodes
from psycopg2 import extensions as ext
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,
ConnectingTestCase, skip_if_tpc_disabled, skip_if_windows, slow)
@ -1566,7 +1566,7 @@ while True:
cur.execute(%(query)r, ("Hello, world!",))
""" % {'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)
(out, err) = proc.communicate()
self.assertNotEqual(proc.returncode, 0)

View File

@ -24,7 +24,8 @@
import sys
import string
from testutils import (unittest, ConnectingTestCase, decorate_all_tests,
import unittest
from testutils import (ConnectingTestCase, decorate_all_tests,
skip_before_postgres, slow)
from cStringIO import StringIO
from itertools import cycle, izip
@ -32,7 +33,7 @@ from subprocess import Popen, PIPE
import psycopg2
import psycopg2.extensions
from testutils import skip_copy_if_green, script_to_py3
from testutils import skip_copy_if_green
from testconfig import dsn
@ -324,7 +325,7 @@ except psycopg2.ProgrammingError:
conn.close()
""" % {'dsn': dsn})
proc = Popen([sys.executable, '-c', script_to_py3(script)])
proc = Popen([sys.executable, '-c', script])
proc.communicate()
self.assertEqual(0, proc.returncode)
@ -343,7 +344,7 @@ except psycopg2.ProgrammingError:
conn.close()
""" % {'dsn': dsn})
proc = Popen([sys.executable, '-c', script_to_py3(script)], stdout=PIPE)
proc = Popen([sys.executable, '-c', script], stdout=PIPE)
proc.communicate()
self.assertEqual(0, proc.returncode)

View File

@ -26,7 +26,8 @@ import time
import pickle
import psycopg2
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_windows)
@ -334,9 +335,9 @@ class CursorTests(ConnectingTestCase):
# timestamp will not be influenced by the pause in Python world.
curs.execute("""select clock_timestamp() from generate_series(1,2)""")
i = iter(curs)
t1 = (i.next())[0] # the brackets work around a 2to3 bug
t1 = next(i)[0]
time.sleep(0.2)
t2 = (i.next())[0]
t2 = next(i)[0]
self.assert_((t2 - t1).microseconds * 1e-6 < 0.1,
"named cursor records fetched in 2 roundtrips (delta: %s)"
% (t2 - t1))

View File

@ -25,7 +25,8 @@
import math
import psycopg2
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):

View File

@ -22,7 +22,8 @@
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
# License for more details.
from testutils import unittest, ConnectingTestCase, slow
import unittest
from testutils import ConnectingTestCase, slow
try:
reload

View File

@ -18,7 +18,8 @@ import time
from datetime import timedelta
import psycopg2
import psycopg2.extras
from testutils import unittest, ConnectingTestCase, skip_before_postgres
import unittest
from testutils import ConnectingTestCase, skip_before_postgres
class ExtrasDictCursorTests(ConnectingTestCase):
@ -310,22 +311,22 @@ class NamedTupleCursorTest(ConnectingTestCase):
i = iter(curs)
self.assertEqual(curs.rownumber, 0)
t = i.next()
t = next(i)
self.assertEqual(t.i, 1)
self.assertEqual(t.s, 'foo')
self.assertEqual(curs.rownumber, 1)
self.assertEqual(curs.rowcount, 3)
t = i.next()
t = next(i)
self.assertEqual(t.i, 2)
self.assertEqual(t.s, 'bar')
self.assertEqual(curs.rownumber, 2)
self.assertEqual(curs.rowcount, 3)
t = i.next()
t = next(i)
self.assertEqual(t.i, 3)
self.assertEqual(t.s, 'baz')
self.assertRaises(StopIteration, i.next)
self.assertRaises(StopIteration, next, i)
self.assertEqual(curs.rownumber, 3)
self.assertEqual(curs.rowcount, 3)

View File

@ -17,7 +17,7 @@
from datetime import date
import testutils
from testutils import unittest
import unittest
import psycopg2
import psycopg2.extras

View File

@ -20,7 +20,7 @@ import sys
from functools import wraps
import testutils
from testutils import unittest
import unittest
import psycopg2
import psycopg2.extras

View File

@ -29,7 +29,8 @@ from functools import wraps
import psycopg2
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)

View File

@ -26,8 +26,9 @@ import os
import sys
from subprocess import Popen
from testutils import (unittest, skip_before_postgres,
ConnectingTestCase, skip_copy_if_green, script_to_py3, slow)
import unittest
from testutils import (skip_before_postgres,
ConnectingTestCase, skip_copy_if_green, slow)
import psycopg2
@ -322,7 +323,7 @@ sys.path.insert(0, %r)
import _psycopg
""" % (pardir, pkgdir))
proc = Popen([sys.executable, '-c', script_to_py3(script)])
proc = Popen([sys.executable, '-c', script])
proc.communicate()
self.assertEqual(0, proc.returncode)

View File

@ -22,11 +22,11 @@
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
# License for more details.
from testutils import unittest
import unittest
import psycopg2
from psycopg2 import extensions
from testutils import ConnectingTestCase, script_to_py3, slow
from testutils import ConnectingTestCase, slow
from testconfig import dsn
import sys
@ -61,7 +61,7 @@ import %(module)s as psycopg2
import %(module)s.extensions as ext
conn = psycopg2.connect(%(dsn)r)
conn.set_isolation_level(ext.ISOLATION_LEVEL_AUTOCOMMIT)
print conn.get_backend_pid()
print(conn.get_backend_pid())
curs = conn.cursor()
curs.execute("NOTIFY " %(name)r %(payload)r)
curs.close()
@ -70,7 +70,7 @@ conn.close()
'module': psycopg2.__name__,
'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
def test_notifies_received_on_poll(self):

View File

@ -25,7 +25,8 @@
import dbapi20
import dbapi20_tpc
from testutils import skip_if_tpc_disabled
from testutils import unittest, decorate_all_tests
import unittest
from testutils import decorate_all_tests
import psycopg2
from testconfig import dsn

View File

@ -24,7 +24,8 @@
import sys
import testutils
from testutils import unittest, ConnectingTestCase
import unittest
from testutils import ConnectingTestCase
import psycopg2
import psycopg2.extensions

View File

@ -27,7 +27,8 @@ from psycopg2.extras import (
PhysicalReplicationConnection, LogicalReplicationConnection, StopReplication)
import testconfig
from testutils import unittest, ConnectingTestCase
import unittest
from testutils import ConnectingTestCase
from testutils import skip_before_postgres, skip_if_green
skip_repl_if_green = skip_if_green("replication not supported in green mode")

View File

@ -24,7 +24,8 @@
import datetime as dt
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)
import psycopg2
@ -344,11 +345,11 @@ class ComposedTest(ConnectingTestCase):
def test_iter(self):
obj = sql.Composed([sql.SQL("foo"), sql.SQL('bar')])
it = iter(obj)
i = it.next()
i = next(it)
self.assertEqual(i, sql.SQL('foo'))
i = it.next()
i = next(it)
self.assertEqual(i, sql.SQL('bar'))
self.assertRaises(StopIteration, it.next)
self.assertRaises(StopIteration, next, it)
class PlaceholderTest(ConnectingTestCase):

View File

@ -23,7 +23,8 @@
# License for more details.
import threading
from testutils import unittest, ConnectingTestCase, skip_before_postgres, slow
import unittest
from testutils import ConnectingTestCase, skip_before_postgres, slow
import psycopg2
from psycopg2.extensions import (

View File

@ -27,7 +27,8 @@ import decimal
import sys
from functools import wraps
import testutils
from testutils import unittest, ConnectingTestCase, decorate_all_tests
import unittest
from testutils import ConnectingTestCase, decorate_all_tests
import psycopg2

View File

@ -22,7 +22,8 @@ from datetime import date, datetime
from functools import wraps
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)
import psycopg2
@ -812,30 +813,6 @@ class AdaptTypeTestCase(ConnectingTestCase):
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):
"""Skip a test if PostgreSQL json type is not available"""
@wraps(f)
@ -851,23 +828,6 @@ def skip_if_no_json_type(f):
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):
from psycopg2.extras import json, Json
@ -879,7 +839,6 @@ class JsonTestCase(ConnectingTestCase):
self.assertQuotedEqual(curs.mogrify("%s", (Json(obj),)),
psycopg2.extensions.QuotedString(json.dumps(obj)).getquoted())
@skip_if_no_json_module
def test_adapt_dumps(self):
from psycopg2.extras import json, Json
@ -897,7 +856,6 @@ class JsonTestCase(ConnectingTestCase):
self.assertQuotedEqual(curs.mogrify("%s", (Json(obj, dumps=dumps),)),
b"'123.45'")
@skip_if_no_json_module
def test_adapt_subclass(self):
from psycopg2.extras import json, Json
@ -915,7 +873,6 @@ class JsonTestCase(ConnectingTestCase):
obj = Decimal('123.45')
self.assertQuotedEqual(curs.mogrify("%s", (MyJson(obj),)), b"'123.45'")
@skip_if_no_json_module
def test_register_on_dict(self):
from psycopg2.extras import Json
psycopg2.extensions.register_adapter(dict, Json)
@ -937,7 +894,6 @@ class JsonTestCase(ConnectingTestCase):
self.assertRaises(psycopg2.ProgrammingError,
psycopg2.extras.register_json, self.conn)
@skip_if_no_json_module
@skip_before_postgres(9, 2)
def test_default_cast(self):
curs = self.conn.cursor()
@ -948,7 +904,6 @@ class JsonTestCase(ConnectingTestCase):
curs.execute("""select array['{"a": 100.0, "b": null}']::json[]""")
self.assertEqual(curs.fetchone()[0], [{'a': 100.0, 'b': None}])
@skip_if_no_json_module
@skip_if_no_json_type
def test_register_on_connection(self):
psycopg2.extras.register_json(self.conn)
@ -956,7 +911,6 @@ class JsonTestCase(ConnectingTestCase):
curs.execute("""select '{"a": 100.0, "b": null}'::json""")
self.assertEqual(curs.fetchone()[0], {'a': 100.0, 'b': None})
@skip_if_no_json_module
@skip_if_no_json_type
def test_register_on_cursor(self):
curs = self.conn.cursor()
@ -964,7 +918,6 @@ class JsonTestCase(ConnectingTestCase):
curs.execute("""select '{"a": 100.0, "b": null}'::json""")
self.assertEqual(curs.fetchone()[0], {'a': 100.0, 'b': None})
@skip_if_no_json_module
@skip_if_no_json_type
def test_register_globally(self):
old = psycopg2.extensions.string_types.get(114)
@ -982,7 +935,6 @@ class JsonTestCase(ConnectingTestCase):
if olda:
psycopg2.extensions.register_type(olda)
@skip_if_no_json_module
@skip_if_no_json_type
def test_loads(self):
json = psycopg2.extras.json
@ -996,7 +948,6 @@ class JsonTestCase(ConnectingTestCase):
self.assert_(isinstance(data['a'], Decimal))
self.assertEqual(data['a'], Decimal('100.0'))
@skip_if_no_json_module
@skip_if_no_json_type
def test_no_conn_curs(self):
from psycopg2._json import _get_json_oids
@ -1023,7 +974,6 @@ class JsonTestCase(ConnectingTestCase):
if olda:
psycopg2.extensions.register_type(olda)
@skip_if_no_json_module
@skip_before_postgres(9, 2)
def test_register_default(self):
curs = self.conn.cursor()
@ -1042,7 +992,6 @@ class JsonTestCase(ConnectingTestCase):
self.assert_(isinstance(data[0]['a'], Decimal))
self.assertEqual(data[0]['a'], Decimal('100.0'))
@skip_if_no_json_module
@skip_if_no_json_type
def test_null(self):
psycopg2.extras.register_json(self.conn)
@ -1052,7 +1001,6 @@ class JsonTestCase(ConnectingTestCase):
curs.execute("""select NULL::json[]""")
self.assertEqual(curs.fetchone()[0], None)
@skip_if_no_json_module
def test_no_array_oid(self):
curs = self.conn.cursor()
t1, t2 = psycopg2.extras.register_json(curs, oid=25)
@ -1064,7 +1012,6 @@ class JsonTestCase(ConnectingTestCase):
self.assertEqual(data['a'], 100)
self.assertEqual(data['b'], None)
@skip_if_no_json_module
def test_str(self):
snowman = u"\u2603"
obj = {'a': [1, 2, snowman]}
@ -1075,7 +1022,6 @@ class JsonTestCase(ConnectingTestCase):
self.assert_(s.startswith("'"))
self.assert_(s.endswith("'"))
@skip_if_no_json_module
@skip_before_postgres(8, 2)
def test_scs(self):
cnn_on = self.connect(options="-c standard_conforming_strings=on")
@ -1188,7 +1134,6 @@ class JsonbTestCase(ConnectingTestCase):
curs.execute("""select NULL::jsonb[]""")
self.assertEqual(curs.fetchone()[0], None)
decorate_all_tests(JsonbTestCase, skip_if_no_json_module)
decorate_all_tests(JsonbTestCase, skip_if_no_jsonb_type)
@ -1619,7 +1564,7 @@ class RangeCasterTestCase(ConnectingTestCase):
from psycopg2.tz import FixedOffsetTimezone
cur = self.conn.cursor()
d1 = date(2012, 01, 01)
d1 = date(2012, 1, 1)
d2 = date(2012, 12, 31)
r = DateRange(d1, d2)
cur.execute("select %s", (r,))

View File

@ -25,7 +25,8 @@
import psycopg2
import psycopg2.extensions as ext
from testutils import unittest, ConnectingTestCase
import unittest
from testutils import ConnectingTestCase
class WithTestCase(ConnectingTestCase):

View File

@ -371,34 +371,6 @@ def skip_if_windows(f):
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):
def __enter__(self):
pass