Thread: Fix for bug in plpython bool type conversion

Fix for bug in plpython bool type conversion

From
Guido Goldstein
Date:
Hi!

The attached patch fixes a bug in plpython.

This bug was found while creating sql from trigger functions
written in plpython and later running the generated sql.
The problem was that boolean was was silently converted to
integer, which is ok for python but fails when the created
sql is used.

The patch uses the Py_RETURN_xxx macros shown at
 http://docs.python.org/api/boolObjects.html .

It would be nice if someone could test and comment
on the patch.

Cheers
  Guido
--- postgresql-8.2.1.orig/src/pl/plpython/plpython.c    2006-11-21 22:51:05.000000000 +0100
+++ postgresql-8.2.1/src/pl/plpython/plpython.c    2007-01-17 18:06:58.185497734 +0100
@@ -1580,8 +1580,8 @@
 PLyBool_FromString(const char *src)
 {
     if (src[0] == 't')
-        return PyInt_FromLong(1);
-    return PyInt_FromLong(0);
+        Py_RETURN_TRUE;
+    Py_RETURN_FALSE;
 }

 static PyObject *

Re: [HACKERS] Fix for bug in plpython bool type conversion

From
Bruce Momjian
Date:
Patch applied.  Thanks.

---------------------------------------------------------------------------


Guido Goldstein wrote:
> Hi!
>
> The attached patch fixes a bug in plpython.
>
> This bug was found while creating sql from trigger functions
> written in plpython and later running the generated sql.
> The problem was that boolean was was silently converted to
> integer, which is ok for python but fails when the created
> sql is used.
>
> The patch uses the Py_RETURN_xxx macros shown at
>  http://docs.python.org/api/boolObjects.html .
>
> It would be nice if someone could test and comment
> on the patch.
>
> Cheers
>   Guido

> --- postgresql-8.2.1.orig/src/pl/plpython/plpython.c    2006-11-21 22:51:05.000000000 +0100
> +++ postgresql-8.2.1/src/pl/plpython/plpython.c    2007-01-17 18:06:58.185497734 +0100
> @@ -1580,8 +1580,8 @@
>  PLyBool_FromString(const char *src)
>  {
>      if (src[0] == 't')
> -        return PyInt_FromLong(1);
> -    return PyInt_FromLong(0);
> +        Py_RETURN_TRUE;
> +    Py_RETURN_FALSE;
>  }
>
>  static PyObject *
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 5: don't forget to increase your free space map settings

--
  Bruce Momjian   bruce@momjian.us
  EnterpriseDB    http://www.enterprisedb.com

  + If your life is a hard drive, Christ can be your backup. +