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

bug skipping optional keyword arguments of type "w#" #40513

Closed
mdr0 mannequin opened this issue Jul 6, 2004 · 4 comments
Closed

bug skipping optional keyword arguments of type "w#" #40513

mdr0 mannequin opened this issue Jul 6, 2004 · 4 comments
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs)

Comments

@mdr0
Copy link
Mannequin

mdr0 mannequin commented Jul 6, 2004

BPO 985713
Nosy @birkenfeld
Files
  • Python-2.3.4-getargs-buffer-fix.diff: Python/getargs.c patch
  • 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 = None
    closed_at = <Date 2005-06-01.18:24:29.000>
    created_at = <Date 2004-07-06.04:37:06.000>
    labels = ['interpreter-core']
    title = 'bug skipping optional keyword arguments of type "w#"'
    updated_at = <Date 2005-06-01.18:24:29.000>
    user = 'https://bugs.python.org/mdr0'

    bugs.python.org fields:

    activity = <Date 2005-06-01.18:24:29.000>
    actor = 'georg.brandl'
    assignee = 'none'
    closed = True
    closed_date = None
    closer = None
    components = ['Interpreter Core']
    creation = <Date 2004-07-06.04:37:06.000>
    creator = 'mdr0'
    dependencies = []
    files = ['6076']
    hgrepos = []
    issue_num = 985713
    keywords = ['patch']
    message_count = 4.0
    messages = ['46317', '46318', '46319', '46320']
    nosy_count = 3.0
    nosy_names = ['georg.brandl', 'mdr0', 'mdehoon']
    pr_nums = []
    priority = 'normal'
    resolution = 'out of date'
    stage = None
    status = 'closed'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue985713'
    versions = []

    @mdr0
    Copy link
    Mannequin Author

    mdr0 mannequin commented Jul 6, 2004

    When using PyArg_ParseTupleAndKeywords(), if you use
    "w#" as an optional keyword argument, you cannot skip
    it and still specify an optional argument that follows
    it. For example, take the following function:

    static PyObject *
    dummy_func(PyObject *self, PyObject *args, PyObject *kwds)
    {
            char *string;
            char *buf = NULL;
            int bufsize = 0;
            unsigned short ushort = 5;
            static char *kwlist[] = { "string", "buffer",
    "ushort", NULL };
        if (! PyArg_ParseTupleAndKeywords(args, kwds,
    

    "s|w#H", kwlist,
    &string,
    &buf,
    &bufsize,
    &ushort))
    return NULL;

        printf("buf=%p, bufsize=%d, ushort=%hu\\n", buf,
    

    bufsize, ushort);
    snprintf(buf, bufsize, "you said \"%s\"\n",
    hostname);

        return Py_BuildValue("s", hostname);
    

    }

    You can call this function successfully as follows:

    # don't specify buffer or ushort
    dummy_func("text")

    Or like this:

    # specify buffer
    buf = array.array('c', [' ' for x in range(0, 1023)])
    dummy_func("text", buffer=buf)

    Or like this:

    # specify buffer and ushort
    buf = array.array('c', [' ' for x in range(0, 1023)])
    dummy_func("text", buffer=buf, ushort=10)

    However, the following does NOT work:

    # specify ushort without first specifying buffer
    dummy_func("text", ushort=10)

    It fails with the following error:

    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
    TypeError: argument 2 impossible<bad format char>

    This is because the skipitem() function in
    Python/getargs.c does not know how to skip arguments
    for the "w#" format unit.
    I've attached a patch that fixes this.

    Note that skipitem() is also missing code for many
    other legal format units, in addition to "w" and "w#".
    In particular:

    u, u# (see patch bpo-853890)
    es, es#
    et, et#
    I (capital i)
    k
    K
    D
    S
    U
    t#
    (tuple)

    It might be a good idea to refactor the code so that
    if/when new format units are added, the same code will
    handle them for both skipitem() and convertsimple().

    @mdr0 mdr0 mannequin closed this as completed Jul 6, 2004
    @mdr0 mdr0 mannequin added the interpreter-core (Objects, Python, Grammar, and Parser dirs) label Jul 6, 2004
    @mdr0 mdr0 mannequin closed this as completed Jul 6, 2004
    @mdr0 mdr0 mannequin added the interpreter-core (Objects, Python, Grammar, and Parser dirs) label Jul 6, 2004
    @mdr0
    Copy link
    Mannequin Author

    mdr0 mannequin commented Jul 6, 2004

    Logged In: YES
    user_id=994239

    Whoops! You'll need to change "hostname" to "string" in
    that example function. I missed a few occurances of that
    variable when I renamed it to make the example more clear...
    *sheepish grin*

    @mdehoon
    Copy link
    Mannequin

    mdehoon mannequin commented Feb 27, 2005

    Logged In: YES
    user_id=488897

    I applied your patch and ran the Python test suite, finding
    no problems with this patch. I'll send a patch review to
    python-dev.

    @birkenfeld
    Copy link
    Member

    Logged In: YES
    user_id=1188172

    Closing; subsumed by bpo-1212928.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 9, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    interpreter-core (Objects, Python, Grammar, and Parser dirs)
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant