Re: proposal: PL/Pythonu - function ereport

From: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
To: Catalin Iacob <iacobcatalin(at)gmail(dot)com>
Cc: Alvaro Herrera <alvherre(at)2ndquadrant(dot)com>, Bruce Momjian <bruce(at)momjian(dot)us>, Robert Haas <robertmhaas(at)gmail(dot)com>, Jim Nasby <Jim(dot)Nasby(at)bluetreble(dot)com>, Peter Eisentraut <peter_e(at)gmx(dot)net>, Craig Ringer <craig(at)2ndquadrant(dot)com>, PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: proposal: PL/Pythonu - function ereport
Date: 2016-02-18 03:57:43
Message-ID: CAFj8pRBkE8TyLEC+6d8HcBgy8dAxQ+MTxRzNSQp_9K09y3dbtw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

2016-02-17 16:54 GMT+01:00 Catalin Iacob <iacobcatalin(at)gmail(dot)com>:

> On Wed, Feb 17, 2016 at 3:32 PM, Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
> wrote:
> >> Python 3 has keyword only arguments. It occurs to me they're exactly
> >> for "optional extra stuff" like detail, hint etc.
> >> Python 2 doesn't have that notion but you can kind of fake it since
> >> you get an args tuple and a kwargs dictionary.
> >
> >
> > I prefer a possibility to use both ways - positional form is shorter,
> > keywords can help with some parameters.
> >
> > But I cannot to imagine your idea, can you show it in detail?
>
> Sure, what I mean is:
>
> plpy.error('msg') # as before produces message 'msg'
> plpy.error(42) # as before produces message '42', including the
> conversion of the int to str
> plpy.error('msg', 'arg 2 is still part of msg') # as before, produces
> message '('msg', 'arg2 is still part of msg')'
> # and so on for as many positional arguments, nothing changes
> # I still think allowing more than one positional argument is
> unfortunate but for compatibility we keep allowing more
>
> # to pass detail you MUST use keyword args to disambiguate "I really
> want detail" vs. "I have argument 2 which is part of the messsage
> tuple for compatibility"
> plpy.error('msg', 42, detail='a detail') # produces message '('msg',
> 42)' and detail 'a detail'
> plpy.error('msg', detail=77) # produces message 'msg' and detail '77'
> so detail is also converted to str just like message for consistency
> # and so on for the others
> plpy.error('msg', 42, detail='a detail', hint='a hint')
> plpy.error('msg', 42, schema='sch')
>
> Only keyword arguments are treated specially and we know no existing
> code has keyword arguments since they didn't work before.
>
> Implementation wise, it's something like this but in C:
>
> def error(*args, **kwargs):
> if len(args) == 1:
> message = str(args[0])
> else:
> message = str(args)
>
> # fetch value from dictionary or None if the key is missing
> detail = kwargs.pop('detail', None)
> hint = kwargs.pop('hint', None)
>
> # use message, detail, hint etc. to raise exception for error and
> fatal/call ereport for the other levels
>
> Is it clear now? What do you think?
>

it doesn't look badly. Is there any possibility how to emulate it with
Python2 ? What do you think about some similar implementation on Python2?

Regards

Pavel

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Pavel Stehule 2016-02-18 03:59:20 Re: proposal: function parse_ident
Previous Message Michael Paquier 2016-02-18 03:55:17 Re: WIP: Access method extendability