[BUG]: returning nullptr from a function supposed to return Eigen...* gives errr
Nobody has claimed this yet.
- Dominant language
- C++
- Stars
- 18k
- Forks
- 2.3k
- Avg merge
- 5d 17h
- Merged PRs (30d)
- 10
Description
Required prerequisites
- Make sure you've read the documentation. Your issue may be addressed there.
- Search the issue tracker and Discussions to verify that this hasn't already been reported. +1 or comment there if it has.
- Consider asking first in the Gitter chat room or in a Discussion.
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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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