pybind / pybind/pybind11

[BUG]: TypeError raised on C++ integer overflow during type conversion

Open
#4,708 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.10.4

Problem description

When passing a python integer to a C++ function, if the Python integer overflows the C++ integer, pybind11 raises TypeError.

For parity with the CPython interpreter, the best exception to raise would probably be the OverflowError as this is the exception raised by PyLong_As* family of functions provided by the CPython stable ABI.

Reproducible example code
//C++ code
#include <iostream>
#include <cstdint>

#include <pybind11/pybind11.h>

namespace py = pybind11;

int64_t int_overflow64(int64_t a) {
    return a;
}

int32_t int_overflow32(int32_t a) {
    return a;
}

int16_t int_overflow16(int16_t a) {
    return a;
}

int8_t int_overflow8(int8_t a) {
    return a;
}

void overflow_test(py::int_ i){
    const long value = PyLong_AsLong(i.ptr());
    if (PyErr_Occurred()){
        throw py::error_already_set();
    }
}

PYBIND11_MODULE(overflowExample, m) {
    m.def("int_overflow64", &int_overflow64, "");
    m.def("int_overflow32", &int_overflow32, "");
    m.def("int_overflow16", &int_overflow16, "");
    m.def("int_overflow8", &int_overflow8, "");
    m.def("overflow_test", &overflow_test, "");
}


# Python Code:
import overflowExample
overflowExample.int_overflow64(9999999999999) # Type Error
overflowExample.int_overflow32(9999999999999) # Type Error
overflowExample.int_overflow16(9999999999999) # Type Error
overflowExample.int_overflow8(9999999999999)  # Type Error
overflowExample.overflow_test(9999999999999)  # Overflow Error
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

Start by running the supplied C++/Python reproducer, especially int_overflow64, int_overflow32, int_overflow16, int_overflow8, and overflow_test, and compare their exception types. Trace the integer conversion path used by those entry points; done means overflowing conversions raise OverflowError consistently with PyLong_As* behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, python
Domain
api, backend-api-design
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.