codon icon indicating copy to clipboard operation
codon copied to clipboard

Calling function in python extension module with wrong type crashes python.

Open ghuls opened this issue 9 months ago • 3 comments

Compiling the following function to a python extension module and calling it with the wrong type, will crash the python interpreter instead of just trowing an exception:

$ cat wrong_type.codon
def wrong_arg(x: int):
    return x
❯ ipython
Python 3.11.8 | packaged by conda-forge | (main, Feb 16 2024, 20:53:32) [GCC 12.3.0]
Type 'copyright', 'credits' or 'license' for more information
IPython 8.32.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import wrong_type

In [2]: wrong_type.wrong_arg(1)
Out[2]: 1

In [3]: wrong_type.wrong_arg(1.2)
PyError: conversion error: Python object did not have type 'int'

Raised from: std.internal.python._conversion_error:0
/software/codon/codon-0.18.1/lib/codon/stdlib/internal/python.codon:1293:5
Aborted (core dumped)

ghuls avatar Feb 27 '25 13:02 ghuls

Thanks for the report; what system/OS are you on? On my Mac I see TypeError: could not find callable method 'wrong_arg' for given arguments as expected. We also have some tests that cover this which you can run via

# from codon repo
cd test/python
python3 setup.py build_ext --inplace
python3 pyext.py

arshajii avatar Mar 10 '25 15:03 arshajii

Rocky Linux 8.9

It must be related to some ld flags then, as I generated the extension with this script: https://github.com/exaloop/codon/issues/308#issuecomment-2688088976

ghuls avatar Mar 11 '25 10:03 ghuls

With thisldd command, the crash occurs:

"${LD}" -o "${extension_library_file}" "${extension_object_file}" \
    -shared -l codonrt -L "${CODON_PATH}/lib/codon" -rpath "${CODON_PATH}/lib/codon"

With this minimal GCC command, it works (not crashing the interpreter):

"${GCC}" -o "${extension_library_file}" "${extension_object_file}" \
    -shared -l codonrt -L "${CODON_PATH}/lib/codon" -Wl,-R"${CODON_PATH}/lib/codon"

ghuls avatar Mar 11 '25 11:03 ghuls