pybind / pybind/pybind11

[BUG]: returning nullptr from a function supposed to return Eigen...* gives errr

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

Nobody has claimed this yet.

triage
Dominant language
C++
Stars
18k
Forks
2.3k
Avg merge
5d 17h
Merged PRs (30d)
10

Description

Required prerequisites
What version (or hash if on master) of pybind11 are you using?

2.13.6

Problem description

https://github.com/pybind/pybind11/issues/731 makes me believe that pybind11 should be able to handle returning nullptr for all data types, hence, I think this is an issue

Reproducible example code
(.venv-linux) sin3point14@DESKTOP-D97C2FN:~/random/cpp/pybind11-test$ cat main.cpp
#include <pybind11/pybind11.h>
#include <pybind11/eigen.h>
#include <pybind11/stl.h>

#include <Eigen/Eigen>

PYBIND11_MODULE(tsting, m)
{
    m.def("ret_nullptr", []() -> Eigen::MatrixXd* {return nullptr;});
}
(.venv-linux) sin3point14@DESKTOP-D97C2FN:~/random/cpp/pybind11-test$ python3
Python 3.12.3 (main, Feb  4 2025, 14:48:35) [GCC 13.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tsting
>>> tsting.ret_nullptr()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: PyCapsule_New called with null pointer

I get a segmentation fault in the project where I discovered this. But i guess this should also not have errored.

Is this a regression? Put the last known working version here if it is.

Not a regression

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

The reproducer is the ret_nullptr binding in main.cpp, using pybind11/eigen.h; first build and import it, then call tsting.ret_nullptr(). Trace the Eigen pointer conversion and establish regression coverage for nullptr handling. Done means the call no longer raises “PyCapsule_New called with null pointer” or causes a segmentation fault.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, python
Domain
api
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.