mirror of
https://git.postgresql.org/git/postgresql.git
synced 2026-02-18 04:17:00 +08:00
PL/Python: Fix slicing support for result objects for Python 3
The old way of implementing slicing support by implementing PySequenceMethods.sq_slice no longer works in Python 3. You now have to implement PyMappingMethods.mp_subscript. Do this by simply proxying the call to the wrapped list of result dictionaries. Consolidate some of the subscripting regression tests. Jan Urbański
This commit is contained in:
@ -1,23 +1,3 @@
|
||||
--
|
||||
-- result objects
|
||||
--
|
||||
CREATE FUNCTION test_resultobject_access() RETURNS void
|
||||
AS $$
|
||||
rv = plpy.execute("SELECT fname, lname, username FROM users ORDER BY username")
|
||||
plpy.info([row for row in rv])
|
||||
rv[1] = dict([(k, v*2) for (k, v) in rv[1].items()])
|
||||
plpy.info([row for row in rv])
|
||||
$$ LANGUAGE plpythonu;
|
||||
SELECT test_resultobject_access();
|
||||
INFO: [{'lname': 'doe', 'username': 'j_doe', 'fname': 'jane'}, {'lname': 'doe', 'username': 'johnd', 'fname': 'john'}, {'lname': 'smith', 'username': 'slash', 'fname': 'rick'}, {'lname': 'doe', 'username': 'w_doe', 'fname': 'willem'}]
|
||||
CONTEXT: PL/Python function "test_resultobject_access"
|
||||
INFO: [{'lname': 'doe', 'username': 'j_doe', 'fname': 'jane'}, {'lname': 'doedoe', 'username': 'johndjohnd', 'fname': 'johnjohn'}, {'lname': 'smith', 'username': 'slash', 'fname': 'rick'}, {'lname': 'doe', 'username': 'w_doe', 'fname': 'willem'}]
|
||||
CONTEXT: PL/Python function "test_resultobject_access"
|
||||
test_resultobject_access
|
||||
--------------------------
|
||||
|
||||
(1 row)
|
||||
|
||||
--
|
||||
-- nested calls
|
||||
--
|
||||
@ -228,6 +208,61 @@ SELECT result_len_test($$UPDATE foo3 SET b= '' WHERE a = 2$$);
|
||||
0
|
||||
(1 row)
|
||||
|
||||
CREATE FUNCTION result_subscript_test() RETURNS void
|
||||
AS $$
|
||||
result = plpy.execute("SELECT 1 AS c UNION SELECT 2 "
|
||||
"UNION SELECT 3 UNION SELECT 4")
|
||||
|
||||
plpy.info(result[1]['c'])
|
||||
plpy.info(result[-1]['c'])
|
||||
|
||||
plpy.info([item['c'] for item in result[1:3]])
|
||||
plpy.info([item['c'] for item in result[::2]])
|
||||
|
||||
result[-1] = {'c': 1000}
|
||||
result[:2] = [{'c': 10}, {'c': 100}]
|
||||
plpy.info([item['c'] for item in result[:]])
|
||||
|
||||
# raises TypeError, but the message differs on Python 2.6, so silence it
|
||||
try:
|
||||
plpy.info(result['foo'])
|
||||
except TypeError:
|
||||
pass
|
||||
else:
|
||||
assert False, "TypeError not raised"
|
||||
|
||||
$$ LANGUAGE plpythonu;
|
||||
SELECT result_subscript_test();
|
||||
INFO: 2
|
||||
CONTEXT: PL/Python function "result_subscript_test"
|
||||
INFO: 4
|
||||
CONTEXT: PL/Python function "result_subscript_test"
|
||||
INFO: [2, 3]
|
||||
CONTEXT: PL/Python function "result_subscript_test"
|
||||
INFO: [1, 3]
|
||||
CONTEXT: PL/Python function "result_subscript_test"
|
||||
INFO: [10, 100, 3, 1000]
|
||||
CONTEXT: PL/Python function "result_subscript_test"
|
||||
result_subscript_test
|
||||
-----------------------
|
||||
|
||||
(1 row)
|
||||
|
||||
CREATE FUNCTION result_empty_test() RETURNS void
|
||||
AS $$
|
||||
result = plpy.execute("select 1 where false")
|
||||
|
||||
plpy.info(result[:])
|
||||
|
||||
$$ LANGUAGE plpythonu;
|
||||
SELECT result_empty_test();
|
||||
INFO: []
|
||||
CONTEXT: PL/Python function "result_empty_test"
|
||||
result_empty_test
|
||||
-------------------
|
||||
|
||||
(1 row)
|
||||
|
||||
-- cursor objects
|
||||
CREATE FUNCTION simple_cursor_test() RETURNS int AS $$
|
||||
res = plpy.cursor("select fname, lname from users")
|
||||
|
||||
Reference in New Issue
Block a user