Re: proposal: PL/Pythonu - function ereport - Mailing list pgsql-hackers

From Pavel Stehule
Subject Re: proposal: PL/Pythonu - function ereport
Date
Msg-id CAFj8pRBkE8TyLEC+6d8HcBgy8dAxQ+MTxRzNSQp_9K09y3dbtw@mail.gmail.com
Whole thread Raw
In response to Re: proposal: PL/Pythonu - function ereport  (Catalin Iacob <iacobcatalin@gmail.com>)
Responses Re: proposal: PL/Pythonu - function ereport
List pgsql-hackers


2016-02-17 16:54 GMT+01:00 Catalin Iacob <iacobcatalin@gmail.com>:
On Wed, Feb 17, 2016 at 3:32 PM, Pavel Stehule <pavel.stehule@gmail.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

pgsql-hackers by date:

Previous
From: Michael Paquier
Date:
Subject: Re: WIP: Access method extendability
Next
From: Pavel Stehule
Date:
Subject: Re: proposal: function parse_ident