Skip to content

TypeError not cleared when handling complex arguments #484

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mhochsteger opened this issue Nov 7, 2016 · 0 comments
Closed

TypeError not cleared when handling complex arguments #484

mhochsteger opened this issue Nov 7, 2016 · 0 comments

Comments

@mhochsteger
Copy link
Contributor

Since commit 496feac
the following code executes the right function but quits with an unhandled exception.

Failing code

test.cpp

#include <pybind11/pybind11.h>
#include <pybind11/complex.h>
#include <iostream>

namespace py = pybind11;

PYBIND11_PLUGIN(test) {
  py::module m("test");

  m.def("test", [] ( float x ) { 
      std::cout << x << std::endl;
      });
  m.def("test", [] ( std::complex<float> x ) { 
      std::cout << x << std::endl;
      });

  return m.ptr();
}

test.py

import test

test.test(1)
test.test(2j)

The output:

$ python3 test.py
1
(0,2)
TypeError: can't convert complex to int

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "test.py", line 4, in <module>
    test.test(2j)
SystemError: <built-in method test of PyCapsule object at 0x7fba58d193c0> returned a result with an error set

Suggested solution

In cast.h, line 453, it can happen that PyNumber_Long fails. In this case it leaves an unhandled TypeError.

 451             PyErr_Clear();
 452             if (type_error && PyNumber_Check(src.ptr()))
 453                 return load(object(PyNumber_Long(src.ptr()), true), false);
 454             return false;

My suggestion is to call PyErr_Clear() again after trying the conversion:

            if (type_error && PyNumber_Check(src.ptr())) {
                bool res = load(object(PyNumber_Long(src.ptr()), true), false);
                PyErr_Clear();
                return res; 
            }    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant