Re: plpython and bytea

From: Hannu Krosing <hannu(at)skype(dot)net>
To: pgsql-hackers(at)postgresql(dot)org
Subject: Re: plpython and bytea
Date: 2005-11-26 00:16:38
Message-ID: 1132964199.4098.31.camel@dell9300
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

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

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Joshua D. Drake 2005-11-26 01:48:10 Re: PL/php in pg_pltemplate
Previous Message Bruce Momjian 2005-11-26 00:13:28 Re: SHOW ALL output too wide