On Mon, 2005-11-21 at 02:11 +0200, Hannu Krosing wrote:
> Hi
>
> It seems that plpython is unable to return bytea string when it contains
> NUL bytes:
>
> hannu=# CREATE OR REPLACE FUNCTION get_bytea_with_nul() RETURNS bytea AS
> '
> return ''aa\\0bb''
> ' LANGUAGE plpythonu SECURITY DEFINER;
>
> hannu=# select get_bytea_with_nul();
> get_bytea_with_nul
> --------------------
> aa
> (1 row)
>
>
> probably related to plpythons way of generating return value via
> converting python objcet to its string representation and then letting
> postgres's input func to convert it back.
Ok, I was able to successfuly return all bytea values from plpython by
creating a bytea class that oveloads strings __str__ method to generate
something that postgresql's bytea type input method understands:
create or replace function get_bytea256() returns bytea as $$
class bytea(str): def __str__(self): res = [] for c in self: if (c in ("\000","'","\\")):
res.append(r"\%03o" % ord(c)) else: res.append(c) return ''.join(res)
return bytea("".join([chr(i) for i in range(256)]))
$$ language plpythonu;
please note that this is a quick proof-of-concept implementation which
contains several gross inefficiencies :p
-----------------------
Hannu Krosing