Skip site navigation (1) Skip section navigation (2)

Peripheral Links

Header And Logo

PostgreSQL
| The world's most advanced open source database.

Site Navigation

Search for
  Advanced Search

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: Sat, 26 Nov 2005 02:16:38 +0200
  • Message-id: <1132964199(dot)4098(dot)31(dot)camel(at)dell9300>

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





Home | Main Index | Thread Index

Privacy Policy | PostgreSQL Archives hosted by Command Prompt, Inc. | Designed by tinysofa
Copyright © 1996 – 2008 PostgreSQL Global Development Group