Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PyArg_Parse format unit like O& but providing context #58944

Open
larryhastings opened this issue May 7, 2012 · 4 comments
Open

Add PyArg_Parse format unit like O& but providing context #58944

larryhastings opened this issue May 7, 2012 · 4 comments
Assignees
Labels
3.13 bugs and security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement

Comments

@larryhastings
Copy link
Contributor

BPO 14739
Nosy @loewis, @birkenfeld, @larryhastings, @serhiy-storchaka, @iritkatriel

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = 'https://github.com/larryhastings'
closed_at = None
created_at = <Date 2012-05-07.09:35:09.214>
labels = ['interpreter-core', 'type-feature']
title = 'Add PyArg_Parse format unit like O& but providing context'
updated_at = <Date 2021-07-20.08:13:30.232>
user = 'https://github.com/larryhastings'

bugs.python.org fields:

activity = <Date 2021-07-20.08:13:30.232>
actor = 'iritkatriel'
assignee = 'larry'
closed = False
closed_date = None
closer = None
components = ['Interpreter Core']
creation = <Date 2012-05-07.09:35:09.214>
creator = 'larry'
dependencies = []
files = []
hgrepos = []
issue_num = 14739
keywords = []
message_count = 3.0
messages = ['160125', '160128', '397861']
nosy_count = 5.0
nosy_names = ['loewis', 'georg.brandl', 'larry', 'serhiy.storchaka', 'iritkatriel']
pr_nums = []
priority = 'normal'
resolution = None
stage = 'needs patch'
status = 'open'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue14739'
versions = []

@larryhastings
Copy link
Contributor Author

If you write a PyArg_Parse "converter", and your conversion hits an error, you must raise an exception and error out. But, since your converter has no context about the parameter, it can't provide any helpful information in the error.

For example, PyUnicode_FSConverter attempts to convert a PyUnicode object to a PyBytes object; if it gets back some other kind of object, it calls
     PyErr_SetString(PyExc_TypeError, "encoder failed to return bytes");
What parameter did this happen with?  When calling what function?  The hapless programmer is forced to guess.

In practice, the argument parser generally knows the name of the function and the name of the parameter. It'd be nice to encode those values in the error. But that information isn't passed in to the converter.

I propose adding a new format unit, let's call it 'O%', which is identical to 'O&' except for the signature of the converter function:

    int converter(PyObject *o, void *p, PyConverterContext_t *context);

where PyConverterContext_t is defined as

    typedef struct {
        char *function_name;
        char *parameter_name;
    } PyConverterContext_t;

If the function name or parameter name is not known, it will be NULL.

@larryhastings larryhastings self-assigned this May 7, 2012
@larryhastings larryhastings added interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement labels May 7, 2012
@serhiy-storchaka
Copy link
Member

It would be better if the functions PyArg_Parse* were wrapped converter's exception, in other exception with adding the names of function and parameter in the message. This will save us from necessity of the introduction of the new interface and immediately give effect to the old code.

@iritkatriel
Copy link
Member

Now we have exception chaining, so Serhiy's pattern is even simpler to implement than with exception wrappers.

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
@terryjreedy
Copy link
Member

4th comment to remove this from proposed auto-close list.

@iritkatriel iritkatriel added the 3.12 bugs and security fixes label Oct 6, 2022
@erlend-aasland erlend-aasland added 3.13 bugs and security fixes and removed 3.12 bugs and security fixes labels Jan 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.13 bugs and security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

5 participants