77CREATE FUNCTION test_type_conversion_bool(x bool) RETURNS bool AS $$
88plpy.info(x, type(x))
99return x
10- $$ LANGUAGEplpythonu ;
10+ $$ LANGUAGEplpython3u ;
1111SELECT * FROM test_type_conversion_bool(true);
1212INFO: (True, <class 'bool'>)
1313CONTEXT: PL/Python function "test_type_conversion_bool"
@@ -51,7 +51,7 @@ elif n == 5:
5151 ret = [0]
5252plpy.info(ret, not not ret)
5353return ret
54- $$ LANGUAGEplpythonu ;
54+ $$ LANGUAGEplpython3u ;
5555SELECT * FROM test_type_conversion_bool_other(0);
5656INFO: (0, False)
5757CONTEXT: PL/Python function "test_type_conversion_bool_other"
@@ -103,7 +103,7 @@ CONTEXT: PL/Python function "test_type_conversion_bool_other"
103103CREATE FUNCTION test_type_conversion_char(x char) RETURNS char AS $$
104104plpy.info(x, type(x))
105105return x
106- $$ LANGUAGEplpythonu ;
106+ $$ LANGUAGEplpython3u ;
107107SELECT * FROM test_type_conversion_char('a');
108108INFO: ('a', <class 'str'>)
109109CONTEXT: PL/Python function "test_type_conversion_char"
@@ -123,7 +123,7 @@ CONTEXT: PL/Python function "test_type_conversion_char"
123123CREATE FUNCTION test_type_conversion_int2(x int2) RETURNS int2 AS $$
124124plpy.info(x, type(x))
125125return x
126- $$ LANGUAGEplpythonu ;
126+ $$ LANGUAGEplpython3u ;
127127SELECT * FROM test_type_conversion_int2(100::int2);
128128INFO: (100, <class 'int'>)
129129CONTEXT: PL/Python function "test_type_conversion_int2"
@@ -151,7 +151,7 @@ CONTEXT: PL/Python function "test_type_conversion_int2"
151151CREATE FUNCTION test_type_conversion_int4(x int4) RETURNS int4 AS $$
152152plpy.info(x, type(x))
153153return x
154- $$ LANGUAGEplpythonu ;
154+ $$ LANGUAGEplpython3u ;
155155SELECT * FROM test_type_conversion_int4(100);
156156INFO: (100, <class 'int'>)
157157CONTEXT: PL/Python function "test_type_conversion_int4"
@@ -179,25 +179,25 @@ CONTEXT: PL/Python function "test_type_conversion_int4"
179179CREATE FUNCTION test_type_conversion_int8(x int8) RETURNS int8 AS $$
180180plpy.info(x, type(x))
181181return x
182- $$ LANGUAGEplpythonu ;
182+ $$ LANGUAGEplpython3u ;
183183SELECT * FROM test_type_conversion_int8(100);
184- INFO: (100L , <type 'long '>)
184+ INFO: (100 , <class 'int '>)
185185CONTEXT: PL/Python function "test_type_conversion_int8"
186186 test_type_conversion_int8
187187---------------------------
188188 100
189189(1 row)
190190
191191SELECT * FROM test_type_conversion_int8(-100);
192- INFO: (-100L , <type 'long '>)
192+ INFO: (-100 , <class 'int '>)
193193CONTEXT: PL/Python function "test_type_conversion_int8"
194194 test_type_conversion_int8
195195---------------------------
196196 -100
197197(1 row)
198198
199199SELECT * FROM test_type_conversion_int8(5000000000);
200- INFO: (5000000000L , <type 'long '>)
200+ INFO: (5000000000 , <class 'int '>)
201201CONTEXT: PL/Python function "test_type_conversion_int8"
202202 test_type_conversion_int8
203203---------------------------
@@ -215,7 +215,7 @@ CONTEXT: PL/Python function "test_type_conversion_int8"
215215CREATE FUNCTION test_type_conversion_numeric(x numeric) RETURNS numeric AS $$
216216plpy.info(x, type(x))
217217return x
218- $$ LANGUAGEplpythonu ;
218+ $$ LANGUAGEplpython3u ;
219219/* The current implementation converts numeric to float. */
220220SELECT * FROM test_type_conversion_numeric(100);
221221INFO: (100.0, <class 'float'>)
@@ -252,7 +252,7 @@ CONTEXT: PL/Python function "test_type_conversion_numeric"
252252CREATE FUNCTION test_type_conversion_float4(x float4) RETURNS float4 AS $$
253253plpy.info(x, type(x))
254254return x
255- $$ LANGUAGEplpythonu ;
255+ $$ LANGUAGEplpython3u ;
256256SELECT * FROM test_type_conversion_float4(100);
257257INFO: (100.0, <class 'float'>)
258258CONTEXT: PL/Python function "test_type_conversion_float4"
@@ -288,7 +288,7 @@ CONTEXT: PL/Python function "test_type_conversion_float4"
288288CREATE FUNCTION test_type_conversion_float8(x float8) RETURNS float8 AS $$
289289plpy.info(x, type(x))
290290return x
291- $$ LANGUAGEplpythonu ;
291+ $$ LANGUAGEplpython3u ;
292292SELECT * FROM test_type_conversion_float8(100);
293293INFO: (100.0, <class 'float'>)
294294CONTEXT: PL/Python function "test_type_conversion_float8"
@@ -324,7 +324,7 @@ CONTEXT: PL/Python function "test_type_conversion_float8"
324324CREATE FUNCTION test_type_conversion_text(x text) RETURNS text AS $$
325325plpy.info(x, type(x))
326326return x
327- $$ LANGUAGEplpythonu ;
327+ $$ LANGUAGEplpython3u ;
328328SELECT * FROM test_type_conversion_text('hello world');
329329INFO: ('hello world', <class 'str'>)
330330CONTEXT: PL/Python function "test_type_conversion_text"
@@ -344,7 +344,7 @@ CONTEXT: PL/Python function "test_type_conversion_text"
344344CREATE FUNCTION test_type_conversion_bytea(x bytea) RETURNS bytea AS $$
345345plpy.info(x, type(x))
346346return x
347- $$ LANGUAGEplpythonu ;
347+ $$ LANGUAGEplpython3u ;
348348SELECT * FROM test_type_conversion_bytea('hello world');
349349INFO: (b'hello world', <class 'bytes'>)
350350CONTEXT: PL/Python function "test_type_conversion_bytea"
@@ -372,14 +372,14 @@ CONTEXT: PL/Python function "test_type_conversion_bytea"
372372CREATE FUNCTION test_type_marshal() RETURNS bytea AS $$
373373import marshal
374374return marshal.dumps('hello world')
375- $$ LANGUAGEplpythonu ;
375+ $$ LANGUAGEplpython3u ;
376376CREATE FUNCTION test_type_unmarshal(x bytea) RETURNS text AS $$
377377import marshal
378378try:
379379 return marshal.loads(x)
380- except ValueError, e:
380+ except ValueError as e:
381381 return 'FAILED: ' + str(e)
382- $$ LANGUAGEplpythonu ;
382+ $$ LANGUAGEplpython3u ;
383383SELECT test_type_unmarshal(x) FROM test_type_marshal() x;
384384 test_type_unmarshal
385385---------------------
@@ -392,7 +392,7 @@ SELECT test_type_unmarshal(x) FROM test_type_marshal() x;
392392CREATE DOMAIN booltrue AS bool CHECK (VALUE IS TRUE OR VALUE IS NULL);
393393CREATE FUNCTION test_type_conversion_booltrue(x booltrue, y bool) RETURNS booltrue AS $$
394394return y
395- $$ LANGUAGEplpythonu ;
395+ $$ LANGUAGEplpython3u ;
396396SELECT * FROM test_type_conversion_booltrue(true, true);
397397 test_type_conversion_booltrue
398398-------------------------------
@@ -409,7 +409,7 @@ CREATE DOMAIN uint2 AS int2 CHECK (VALUE >= 0);
409409CREATE FUNCTION test_type_conversion_uint2(x uint2, y int) RETURNS uint2 AS $$
410410plpy.info(x, type(x))
411411return y
412- $$ LANGUAGEplpythonu ;
412+ $$ LANGUAGEplpython3u ;
413413SELECT * FROM test_type_conversion_uint2(100::uint2, 50);
414414INFO: (100, <class 'int'>)
415415CONTEXT: PL/Python function "test_type_conversion_uint2"
@@ -435,7 +435,7 @@ CONTEXT: PL/Python function "test_type_conversion_uint2"
435435CREATE DOMAIN nnint AS int CHECK (VALUE IS NOT NULL);
436436CREATE FUNCTION test_type_conversion_nnint(x nnint, y int) RETURNS nnint AS $$
437437return y
438- $$ LANGUAGEplpythonu ;
438+ $$ LANGUAGEplpython3u ;
439439SELECT * FROM test_type_conversion_nnint(10, 20);
440440 test_type_conversion_nnint
441441----------------------------
@@ -452,7 +452,7 @@ CREATE DOMAIN bytea10 AS bytea CHECK (octet_length(VALUE) = 10 AND VALUE IS NOT
452452CREATE FUNCTION test_type_conversion_bytea10(x bytea10, y bytea) RETURNS bytea10 AS $$
453453plpy.info(x, type(x))
454454return y
455- $$ LANGUAGEplpythonu ;
455+ $$ LANGUAGEplpython3u ;
456456SELECT * FROM test_type_conversion_bytea10('hello wold', 'hello wold');
457457INFO: (b'hello wold', <class 'bytes'>)
458458CONTEXT: PL/Python function "test_type_conversion_bytea10"
@@ -483,7 +483,7 @@ PL/Python function "test_type_conversion_bytea10"
483483CREATE FUNCTION test_type_conversion_array_int4(x int4[]) RETURNS int4[] AS $$
484484plpy.info(x, type(x))
485485return x
486- $$ LANGUAGEplpythonu ;
486+ $$ LANGUAGEplpython3u ;
487487SELECT * FROM test_type_conversion_array_int4(ARRAY[0, 100]);
488488INFO: ([0, 100], <class 'list'>)
489489CONTEXT: PL/Python function "test_type_conversion_array_int4"
@@ -531,7 +531,7 @@ CONTEXT: PL/Python function "test_type_conversion_array_int4"
531531CREATE FUNCTION test_type_conversion_array_bytea(x bytea[]) RETURNS bytea[] AS $$
532532plpy.info(x, type(x))
533533return x
534- $$ LANGUAGEplpythonu ;
534+ $$ LANGUAGEplpython3u ;
535535SELECT * FROM test_type_conversion_array_bytea(ARRAY[E'\\xdeadbeef'::bytea, NULL]);
536536INFO: ([b'\xde\xad\xbe\xef', None], <class 'list'>)
537537CONTEXT: PL/Python function "test_type_conversion_array_bytea"
@@ -542,7 +542,7 @@ CONTEXT: PL/Python function "test_type_conversion_array_bytea"
542542
543543CREATE FUNCTION test_type_conversion_array_mixed1() RETURNS text[] AS $$
544544return [123, 'abc']
545- $$ LANGUAGEplpythonu ;
545+ $$ LANGUAGEplpython3u ;
546546SELECT * FROM test_type_conversion_array_mixed1();
547547 test_type_conversion_array_mixed1
548548-----------------------------------
@@ -551,20 +551,20 @@ SELECT * FROM test_type_conversion_array_mixed1();
551551
552552CREATE FUNCTION test_type_conversion_array_mixed2() RETURNS int[] AS $$
553553return [123, 'abc']
554- $$ LANGUAGEplpythonu ;
554+ $$ LANGUAGEplpython3u ;
555555SELECT * FROM test_type_conversion_array_mixed2();
556556ERROR: invalid input syntax for integer: "abc"
557557CONTEXT: while creating return value
558558PL/Python function "test_type_conversion_array_mixed2"
559559CREATE FUNCTION test_type_conversion_array_record() RETURNS type_record[] AS $$
560560return [None]
561- $$ LANGUAGEplpythonu ;
561+ $$ LANGUAGEplpython3u ;
562562SELECT * FROM test_type_conversion_array_record();
563563ERROR: PL/Python functions cannot return type type_record[]
564564DETAIL: PL/Python does not support conversion to arrays of row types.
565565CREATE FUNCTION test_type_conversion_array_string() RETURNS text[] AS $$
566566return 'abc'
567- $$ LANGUAGEplpythonu ;
567+ $$ LANGUAGEplpython3u ;
568568SELECT * FROM test_type_conversion_array_string();
569569 test_type_conversion_array_string
570570-----------------------------------
@@ -573,7 +573,7 @@ SELECT * FROM test_type_conversion_array_string();
573573
574574CREATE FUNCTION test_type_conversion_array_tuple() RETURNS text[] AS $$
575575return ('abc', 'def')
576- $$ LANGUAGEplpythonu ;
576+ $$ LANGUAGEplpython3u ;
577577SELECT * FROM test_type_conversion_array_tuple();
578578 test_type_conversion_array_tuple
579579----------------------------------
@@ -582,8 +582,69 @@ SELECT * FROM test_type_conversion_array_tuple();
582582
583583CREATE FUNCTION test_type_conversion_array_error() RETURNS int[] AS $$
584584return 5
585- $$ LANGUAGEplpythonu ;
585+ $$ LANGUAGEplpython3u ;
586586SELECT * FROM test_type_conversion_array_error();
587587ERROR: PL/Python: return value of function with array return type is not a Python sequence
588588CONTEXT: while creating return value
589589PL/Python function "test_type_conversion_array_error"
590+ --
591+ -- Prepared statements
592+ --
593+ CREATE OR REPLACE FUNCTION test_prep_bool_input() RETURNS int
594+ LANGUAGE plpython3u
595+ AS $$
596+ plan = plpy.prepare("SELECT CASE WHEN $1 THEN 1 ELSE 0 END AS val", ['boolean'])
597+ rv = plpy.execute(plan, ['fa'], 5) # 'fa' is true in Python
598+ return rv[0]['val']
599+ $$;
600+ SELECT test_prep_bool_input(); -- 1
601+ test_prep_bool_input
602+ ----------------------
603+ 1
604+ (1 row)
605+
606+ CREATE OR REPLACE FUNCTION test_prep_bool_output() RETURNS bool
607+ LANGUAGE plpython3u
608+ AS $$
609+ plan = plpy.prepare("SELECT $1 = 1 AS val", ['int'])
610+ rv = plpy.execute(plan, [0], 5)
611+ plpy.info(rv[0])
612+ return rv[0]['val']
613+ $$;
614+ SELECT test_prep_bool_output(); -- false
615+ INFO: {'val': False}
616+ CONTEXT: PL/Python function "test_prep_bool_output"
617+ test_prep_bool_output
618+ -----------------------
619+ f
620+ (1 row)
621+
622+ CREATE OR REPLACE FUNCTION test_prep_bytea_input(bb bytea) RETURNS int
623+ LANGUAGE plpython3u
624+ AS $$
625+ plan = plpy.prepare("SELECT octet_length($1) AS val", ['bytea'])
626+ rv = plpy.execute(plan, [bb], 5)
627+ return rv[0]['val']
628+ $$;
629+ SELECT test_prep_bytea_input(E'a\\000b'); -- 3 (embedded null formerly truncated value)
630+ test_prep_bytea_input
631+ -----------------------
632+ 3
633+ (1 row)
634+
635+ CREATE OR REPLACE FUNCTION test_prep_bytea_output() RETURNS bytea
636+ LANGUAGE plpython3u
637+ AS $$
638+ plan = plpy.prepare("SELECT decode('aa00bb', 'hex') AS val")
639+ rv = plpy.execute(plan, [], 5)
640+ plpy.info(rv[0])
641+ return rv[0]['val']
642+ $$;
643+ SELECT test_prep_bytea_output();
644+ INFO: {'val': b'\xaa\x00\xbb'}
645+ CONTEXT: PL/Python function "test_prep_bytea_output"
646+ test_prep_bytea_output
647+ ------------------------
648+ \xaa00bb
649+ (1 row)
650+