Skip to content

[BUG] Small memory leak with default arguments to cpdef functions #4412

@da-woods

Description

@da-woods

Describe the bug

cpdef functions appear to generate their default arguments twice - once for the CFuncDefNode and once for the DefNode.

To Reproduce
Code to reproduce the behaviour:

cpdef nonfused(keep="nonono{0}".format(1) ):
    return keep

This generates the code

__pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_kp_s_nonono_0, __pyx_n_s_format); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 10, __pyx_L1_error)
  __Pyx_GOTREF(__pyx_t_1);
  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_tuple__16, NULL); if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 10, __pyx_L1_error)
  __Pyx_GOTREF(__pyx_t_2);
  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
  __pyx_k__9 = __pyx_t_2;
  __Pyx_GIVEREF(__pyx_t_2);
  __pyx_t_2 = 0;
  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_kp_s_nonono_0, __pyx_n_s_format); if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 10, __pyx_L1_error)
  __Pyx_GOTREF(__pyx_t_2);
  __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_tuple__16, NULL); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 10, __pyx_L1_error)
  __Pyx_GOTREF(__pyx_t_1);
  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
  __pyx_k__9 = __pyx_t_1;
  __Pyx_GIVEREF(__pyx_t_1);
  __pyx_t_1 = 0;

Note that __pyx_k__9 is overwritten twice, so the initial assignment to it is leaked

Environment (please complete the following information):

  • Cython version: current master branch

Additional context

For literals (which is common) no assignment is generated and so this is a non-issue.
Discovered while looking into #4409 (but unlike that it doesn't specifically relate to fused types)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions