Re: adding import in pl/python function

From: Szymon Guz <mabewlun(at)gmail(dot)com>
To: Claudio Freire <klaussfreire(at)gmail(dot)com>
Cc: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: adding import in pl/python function
Date: 2013-05-24 20:02:36
Message-ID: CAFjNrYsG6UmVaP68o=fjiLSSaK9YAhx8T9x+ZbqUft56ktbA4g@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On 24 May 2013 21:46, Claudio Freire <klaussfreire(at)gmail(dot)com> wrote:

> On Fri, May 24, 2013 at 4:22 PM, Szymon Guz <mabewlun(at)gmail(dot)com> wrote:
> > Hm... maybe you're right. I think I don't understand fully how the
> > procedures are executed, and I need to read more to get it.
>
>
> Well, it's easy.
>
> Instead of PLyFloat_FromNumeric[0], you can make a
> PLyDecimal_FromNumeric. There, you'd do with the Python/C[1]:
>
> PyObject *decimal = PyImport_Import("decimal");
> PyObject *decimal_dict = PyModule_GetDict(decimal);
> PyObject *decimal_ctor = PyDict_GetItemString(decimal_dict, "Decimal");
>
> And invoke it with a string rep of your Numeric:
>
> PyObject *value = PyObject_CallFunction(decimal_ctor, "S", string_value);
>
> Add of course all kinds of error checking and reference count boiler
> plate, and you'd have a very dumb version of it.
>
> To make it more "pro", you'd want to do all that stuff to get
> decimal_ctor only at initialization time. Especially since you don't
> want to fumble with the import lock right there in _FromNumeric.
>
> And to make it totally "pro", you can even freeze Decimal (using
> pyfreeze) if you'd like. I would only do this in contexts where you
> don't have a stdlib of course. Not sure whether windows falls into
> that category. Linux doesn't.
>
>
> [0]
> http://git.postgresql.org/gitweb/?p=postgresql.git;a=blob;f=src/pl/plpython/plpy_typeio.c#l518
> [1] http://docs.python.org/2/c-api/import.html
>

Thanks, I will take a look at this, looks pretty easy. However testing on
Windows will be pretty funny :)

thanks,
Szymon

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Andres Freund 2013-05-24 20:03:05 Re: getting rid of freezing
Previous Message Josh Berkus 2013-05-24 19:49:31 Re: getting rid of freezing