[Cython] weird code in argument unpacking (memoryview related?)

mark florisson markflorisson88 at gmail.com
Fri Mar 30 14:25:28 CEST 2012

On 30 March 2012 12:19, Stefan Behnel <stefan_ml at behnel.de> wrote:
> Hi,
> this code in Nodes.py around line 3950, at the end of the DefNodeWrapper's
> method generate_keyword_unpacking_code(), was added as part of the memory
> view changes, back in July last year:
> """
>        # convert arg values to their final type and assign them
>        for i, arg in enumerate(all_args):
>            if arg.default and not arg.type.is_pyobject:
>                code.putln("if (values[%d]) {" % i)
>            if arg.default and not arg.type.is_pyobject:
>                code.putln('} else {')
>                code.putln(
>                    "%s = %s;" % (
>                        arg.entry.cname,
>                        arg.calculate_default_value_code(code)))
>                if arg.type.is_memoryviewslice:
>                    code.put_incref_memoryviewslice(arg.entry.cname,
>                                                    have_gil=True)
>                code.putln('}')
> """
> By being overly complicated, it hides rather well what it's actually meant
> to achieve. It doesn't do at all what its comment says and also refers to
> the default value of the argument, which is already assigned way before
> this place in the code. Therefore, I'd be surprised if it generated
> anything but dead C code, because the case that "values[i]" is NULL should
> never happen.
> Mark, could you comment on this? Is this just a left-over from a broken
> merge or something?

Hm, IIRC I was fixing acquisition count errors for memoryview slices
(of which there were many) for arguments with default values, I was
cleaning up after the initial memoryview implementation.

All I did was add another case for memoryview slices in old code (I
only wrote the last 3 lines, you wrote the other ones in 2008). Very
similar but more recent code (probably copied and pasted) can be found
around line ~3730 in generate_tuple_and_keyword_parsing_code (instead
of generate_keyword_unpacking_code).

There are tests for default arguments and acquisition count errors
should fail immediately and hard, so I trust your judgement to remove
any or all of this.

> Stefan
> _______________________________________________
> cython-devel mailing list
> cython-devel at python.org
> http://mail.python.org/mailman/listinfo/cython-devel

More information about the cython-devel mailing list