Skip to content
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

Cannot directly use cppyy.gbl.std.complex['double'] with Python int #277

Closed
akira-okumura opened this issue Nov 28, 2024 · 4 comments
Closed

Comments

@akira-okumura
Copy link

I remember that I was able to use std::complex in Python seamlessly together with Python int and float before. However, recent cppyy does not accept binary operations between Python int and std::complex. When adding them, NotImplementedError is emitted. When dividing Python int by std::complex, it causes a segmentation fault.

This happens on at least

  • ROOT 6.32.08, macOS 15.1.1, Apple M2
  • ROOT 6.28, macOS 14.2.1, Apple M2
  • ROOT 6.32.06, macOS 14.7.1, Intel
$ ipython
Python 3.13.0 (main, Oct  7 2024, 05:02:14) [Clang 16.0.0 (clang-1600.0.26.4)]
Type 'copyright', 'credits' or 'license' for more information
IPython 8.29.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import cppyy

In [2]: cppyy.__path__
Out[2]: ['/usr/local/root-6.32.08_install/lib/cppyy']

In [3]: c = cppyy.gbl.std.complex['double'](1, 1)

In [4]: 1 + c
---------------------------------------------------------------------------
NotImplementedError                       Traceback (most recent call last)
Cell In[4], line 1
----> 1 1 + c

NotImplementedError: 

In [5]: 1/c
 *** Break *** segmentation violation
[/usr/local/root-6.32.08_install/lib/libcppyy_backend.so] (anonymous namespace)::TExceptionHandlerImp::HandleException(int) (no debug info)
[/usr/local/root-6.32.08_install/lib/libCore.so] TUnixSystem::DispatchSignals(ESignals) (no debug info)
[/usr/lib/system/libsystem_platform.dylib] _sigtramp (no debug info)
[/opt/homebrew/Cellar/python@3.13/3.13.0_1/Frameworks/Python.framework/Versions/3.13/Python] binary_op1 (no debug info)
[/opt/homebrew/Cellar/python@3.13/3.13.0_1/Frameworks/Python.framework/Versions/3.13/Python] binary_op1 (no debug info)
[/opt/homebrew/Cellar/python@3.13/3.13.0_1/Frameworks/Python.framework/Versions/3.13/Python] PyNumber_TrueDivide (no debug info)
[/opt/homebrew/Cellar/python@3.13/3.13.0_1/Frameworks/Python.framework/Versions/3.13/Python] _PyEval_EvalFrameDefault (no debug info)
[/opt/homebrew/Cellar/python@3.13/3.13.0_1/Frameworks/Python.framework/Versions/3.13/Python] PyEval_EvalCode (no debug info)
[/opt/homebrew/Cellar/python@3.13/3.13.0_1/Frameworks/Python.framework/Versions/3.13/Python] builtin_exec (no debug info)
[/opt/homebrew/Cellar/python@3.13/3.13.0_1/Frameworks/Python.framework/Versions/3.13/Python] _PyEval_EvalFrameDefault (no debug info)
[/opt/homebrew/Cellar/python@3.13/3.13.0_1/Frameworks/Python.framework/Versions/3.13/Python] gen_send_ex2 (no debug info)
[/opt/homebrew/Cellar/python@3.13/3.13.0_1/Frameworks/Python.framework/Versions/3.13/Python] gen_send_ex (no debug info)
[/opt/homebrew/Cellar/python@3.13/3.13.0_1/Frameworks/Python.framework/Versions/3.13/Python] _PyEval_EvalFrameDefault (no debug info)
[/opt/homebrew/Cellar/python@3.13/3.13.0_1/Frameworks/Python.framework/Versions/3.13/Python] method_vectorcall (no debug info)
[/opt/homebrew/Cellar/python@3.13/3.13.0_1/Frameworks/Python.framework/Versions/3.13/Python] _PyVectorcall_Call (no debug info)
[/opt/homebrew/Cellar/python@3.13/3.13.0_1/Frameworks/Python.framework/Versions/3.13/Python] _PyEval_EvalFrameDefault (no debug info)
[/opt/homebrew/Cellar/python@3.13/3.13.0_1/Frameworks/Python.framework/Versions/3.13/Python] PyEval_EvalCode (no debug info)
[/opt/homebrew/Cellar/python@3.13/3.13.0_1/Frameworks/Python.framework/Versions/3.13/Python] run_eval_code_obj (no debug info)
[/opt/homebrew/Cellar/python@3.13/3.13.0_1/Frameworks/Python.framework/Versions/3.13/Python] run_mod (no debug info)
[/opt/homebrew/Cellar/python@3.13/3.13.0_1/Frameworks/Python.framework/Versions/3.13/Python] pyrun_file (no debug info)
[/opt/homebrew/Cellar/python@3.13/3.13.0_1/Frameworks/Python.framework/Versions/3.13/Python] _PyRun_SimpleFileObject (no debug info)
[/opt/homebrew/Cellar/python@3.13/3.13.0_1/Frameworks/Python.framework/Versions/3.13/Python] _PyRun_AnyFileObject (no debug info)
[/opt/homebrew/Cellar/python@3.13/3.13.0_1/Frameworks/Python.framework/Versions/3.13/Python] pymain_run_file_obj (no debug info)
[/opt/homebrew/Cellar/python@3.13/3.13.0_1/Frameworks/Python.framework/Versions/3.13/Python] pymain_run_file (no debug info)
[/opt/homebrew/Cellar/python@3.13/3.13.0_1/Frameworks/Python.framework/Versions/3.13/Python] Py_RunMain (no debug info)
[/opt/homebrew/Cellar/python@3.13/3.13.0_1/Frameworks/Python.framework/Versions/3.13/Python] pymain_main (no debug info)
[/opt/homebrew/Cellar/python@3.13/3.13.0_1/Frameworks/Python.framework/Versions/3.13/Python] Py_BytesMain (no debug info)
[/usr/lib/dyld] start (no debug info)
 *** Break *** segmentation violation
[/usr/local/root-6.32.08_install/lib/libcppyy_backend.so] (anonymous namespace)::TExceptionHandlerImp::HandleException(int) (no debug info)
[/usr/local/root-6.32.08_install/lib/libCore.so] TUnixSystem::DispatchSignals(ESignals) (no debug info)
[/usr/lib/system/libsystem_platform.dylib] _sigtramp (no debug info)
[/opt/homebrew/Cellar/python@3.13/3.13.0_1/Frameworks/Python.framework/Versions/3.13/Python] binary_op1 (no debug info)
[/opt/homebrew/Cellar/python@3.13/3.13.0_1/Frameworks/Python.framework/Versions/3.13/Python] binary_op1 (no debug info)
[/opt/homebrew/Cellar/python@3.13/3.13.0_1/Frameworks/Python.framework/Versions/3.13/Python] PyNumber_TrueDivide (no debug info)
[/opt/homebrew/Cellar/python@3.13/3.13.0_1/Frameworks/Python.framework/Versions/3.13/Python] _PyEval_EvalFrameDefault (no debug info)
[/opt/homebrew/Cellar/python@3.13/3.13.0_1/Frameworks/Python.framework/Versions/3.13/Python] PyEval_EvalCode (no debug info)
[/opt/homebrew/Cellar/python@3.13/3.13.0_1/Frameworks/Python.framework/Versions/3.13/Python] builtin_exec (no debug info)
[/opt/homebrew/Cellar/python@3.13/3.13.0_1/Frameworks/Python.framework/Versions/3.13/Python] _PyEval_EvalFrameDefault (no debug info)
[/opt/homebrew/Cellar/python@3.13/3.13.0_1/Frameworks/Python.framework/Versions/3.13/Python] gen_send_ex2 (no debug info)
[/opt/homebrew/Cellar/python@3.13/3.13.0_1/Frameworks/Python.framework/Versions/3.13/Python] gen_send_ex (no debug info)
[/opt/homebrew/Cellar/python@3.13/3.13.0_1/Frameworks/Python.framework/Versions/3.13/Python] _PyEval_EvalFrameDefault (no debug info)
[/opt/homebrew/Cellar/python@3.13/3.13.0_1/Frameworks/Python.framework/Versions/3.13/Python] method_vectorcall (no debug info)
[/opt/homebrew/Cellar/python@3.13/3.13.0_1/Frameworks/Python.framework/Versions/3.13/Python] _PyVectorcall_Call (no debug info)
[/opt/homebrew/Cellar/python@3.13/3.13.0_1/Frameworks/Python.framework/Versions/3.13/Python] _PyEval_EvalFrameDefault (no debug info)
[/opt/homebrew/Cellar/python@3.13/3.13.0_1/Frameworks/Python.framework/Versions/3.13/Python] PyEval_EvalCode (no debug info)
[/opt/homebrew/Cellar/python@3.13/3.13.0_1/Frameworks/Python.framework/Versions/3.13/Python] run_eval_code_obj (no debug info)
[/opt/homebrew/Cellar/python@3.13/3.13.0_1/Frameworks/Python.framework/Versions/3.13/Python] run_mod (no debug info)
[/opt/homebrew/Cellar/python@3.13/3.13.0_1/Frameworks/Python.framework/Versions/3.13/Python] pyrun_file (no debug info)
[/opt/homebrew/Cellar/python@3.13/3.13.0_1/Frameworks/Python.framework/Versions/3.13/Python] _PyRun_SimpleFileObject (no debug info)
[/opt/homebrew/Cellar/python@3.13/3.13.0_1/Frameworks/Python.framework/Versions/3.13/Python] _PyRun_AnyFileObject (no debug info)
[/opt/homebrew/Cellar/python@3.13/3.13.0_1/Frameworks/Python.framework/Versions/3.13/Python] pymain_run_file_obj (no debug info)
[/opt/homebrew/Cellar/python@3.13/3.13.0_1/Frameworks/Python.framework/Versions/3.13/Python] pymain_run_file (no debug info)
[/opt/homebrew/Cellar/python@3.13/3.13.0_1/Frameworks/Python.framework/Versions/3.13/Python] Py_RunMain (no debug info)
[/opt/homebrew/Cellar/python@3.13/3.13.0_1/Frameworks/Python.framework/Versions/3.13/Python] pymain_main (no debug info)
[/opt/homebrew/Cellar/python@3.13/3.13.0_1/Frameworks/Python.framework/Versions/3.13/Python] Py_BytesMain (no debug info)
[/usr/lib/dyld] start (no debug info)

@wlav
Copy link
Owner

wlav commented Nov 30, 2024

The reason why it used to work and no longer does may be that the global operator+ for std::complex is templated only on the type of std::complex. The upshot is that when cppyy asks Cling for operator+(const std::complex<double>&, const int&), it demurs. However, if you first use a floating point addition, then it will instantiate operator+(const std::complex<double>&, const double&) and after that, cppyy can do integer addition as well through promotion. That is, this works:

>>> import cppyy
>>> c = cppyy.gbl.std.complex['double'](1, 1)
>>> 1. + c
(2+1j)
>>> 1 + c
(2+1j)
>>> 

Retrieving templated methods from Cling is unfortunately guess work. I've been bugging them for years to be able to either match templates by category or by getting better information out of Cling to do my own matching.

@akira-okumura
Copy link
Author

Thank you very much. I understand the situation.

Unfortunately, 1. + c does not work on my machine either. But the error message is different from 1 + c interestingly.

In [1]: import cppyy

In [2]: c = cppyy.gbl.std.complex['double'](1, 1)

In [3]: 1. + c
Error in <TClass::GetMethodWithPrototype>: 
Did not find matching TMethod <operator+> with "double&, std::complex<double>&" for __1
Error in <TClass::GetMethodWithPrototype>: 
Did not find matching TMethod <operator+> with "double, std::complex<double>" for __1
---------------------------------------------------------------------------
NotImplementedError                       Traceback (most recent call last)

For the moment, I will explicitly convert cppyy.gbl.std.complex['double'] to Python complex in my code.

c = method_returning_std_complex_double_from_Cpp()
c = c.real + c.imag*(0 + 1j)

@wlav
Copy link
Owner

wlav commented Nov 30, 2024

That may be on ROOT (which is following cppyy master more closely these days, but it's not the same): I don't see such an error message in cppyy master. Recommend to report it there.

@akira-okumura
Copy link
Author

Thank you. Done.

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

2 participants