PyO3 / PyO3/pyo3

`PyCFunction::new` and `PyCFunction::new_with_keywords` necessarily leak a `PyMethodDef`

Open
#5,469 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

needs-design
Dominant language
Rust
Stars
16.2k
Forks
1k
Avg merge
2d 6h
Merged PRs (30d)
66

Description

Follow up to #5459

PyCFunction::new and PyCFunction::new_with_keywords both have to leak PyMethodDef because there is nowhere to store the one they create to produce Python functions.

I see possible solutions:

  • replace these constructors with one which takes &'static PyMethodDef.
  • expand PyCFunction::new_closure into a family of functions which work similarly for various function-like types (and Python call protocols, e.g. new_closure only supports tuple & dict convention but "vectorcall" is more efficient)

Needs experimentation to see what feels good.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by examining the PyCFunction::new, PyCFunction::new_with_keywords, and PyCFunction::new_closure entry points, then read the context from follow-up #5459. Compare the ownership of the created PyMethodDef and the supported tuple-and-dict versus vectorcall protocols; done means selecting and validating an API approach that avoids the reported leak.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, rust
Domain
api, backend-api-design
Issue type
Refactor
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.