# Can an OpenTURNS PythonFunction transmit exceptions from the Python function?

**URL:** <https://openturns.discourse.group/t/can-an-openturns-pythonfunction-transmit-exceptions-from-the-python-function/408>\
**Category:** Python usage\
**Created:** [March 6, 2025, 4:05pm UTC](https://openturns.discourse.group/t/can-an-openturns-pythonfunction-transmit-exceptions-from-the-python-function/408 "2025-03-06T16:05:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![MerlinKeller](https://yyz2.discourse-cdn.com/free1/user_avatar/openturns.discourse.group/merlinkeller/32/190_2.png) [@MerlinKeller](https://openturns.discourse.group/u/MerlinKeller)\
**Post date:** [March 6, 2025, 4:05pm UTC](https://openturns.discourse.group/t/can-an-openturns-pythonfunction-transmit-exceptions-from-the-python-function/408/1 "2025-03-06T16:05:25Z")

</div>

Hi,

I ran recently in a problem using an OpenTURNS PythonFunction, which I reproduce here in a simplified form:

I created the following PythonFunction

```auto
import openturns as ot
import numpy as np

def myfunc(x):
    return x[:1] / x[1:]

my_OTfunc = ot.PythonFunction(2, 1, myfunc)

```

Note my mistake: the division operator “/” does not apply to `ot.Point` objects, so the above code has every reason to crash. However, I tested my code by typing:

```auto
x = np.array([1, 2])
myfunc(x)
my_OTfunc (x)

```

The Python function `myfunc` worked well because it operated on a `np.array`. The OpenTURNS Python Function `my_OTfunc` crashed, because it first converted the numpy array into a `ot.Point`. This means that `myfunc` actually returned the exception obtained by typing:

```auto
myfunc(ot.Point(x))

```

which is:

```auto
TypeError: unsupported operand type(s) for /: 'Point' and 'Point'

```

However, the only exception I got was:

```auto
RuntimeError: InternalException : Python exception

```

which is rather poorly informative; it then took me (at least) twenty minutes to realize my mistake. Which leads to my question: would it be possible to modify the `ot.PythonFunction` structure so that it transmits the exceptions raised by the Python Function it is built upon?

Many thanks in advance for your answer,

Best  
Merlin

---

<div class="post-metadata">

**Author:** ![schueller](https://yyz2.discourse-cdn.com/free1/user_avatar/openturns.discourse.group/schueller/32/3_2.png) [@schueller](https://openturns.discourse.group/u/schueller)\
**Post date:** [March 7, 2025, 4:03pm UTC](https://openturns.discourse.group/t/can-an-openturns-pythonfunction-transmit-exceptions-from-the-python-function/408/2 "2025-03-07T16:03:04Z")

</div>

its a bug with the 1.24 with recent Python it seems, I fixed it a few weeks ago in my misc branch:

`RuntimeError: InternalException : Python exception: <class 'TypeError'>: sequence index must be integer, not 'slice'`

which version of Python are you using ?

---

<div class="post-metadata">

**Author:** ![MerlinKeller](https://yyz2.discourse-cdn.com/free1/user_avatar/openturns.discourse.group/merlinkeller/32/190_2.png) [@MerlinKeller](https://openturns.discourse.group/u/MerlinKeller)\
**Post date:** [March 10, 2025, 10:14am UTC](https://openturns.discourse.group/t/can-an-openturns-pythonfunction-transmit-exceptions-from-the-python-function/408/3 "2025-03-10T10:14:33Z")

</div>

Hi Julien,

Thanks for your feedback and yes, I am using openturns 1.24…

Best,  
Merlin

---

<div class="post-metadata">

**Author:** ![schueller](https://yyz2.discourse-cdn.com/free1/user_avatar/openturns.discourse.group/schueller/32/3_2.png) [@schueller](https://openturns.discourse.group/u/schueller)\
**Post date:** [March 10, 2025, 1:34pm UTC](https://openturns.discourse.group/t/can-an-openturns-pythonfunction-transmit-exceptions-from-the-python-function/408/4 "2025-03-10T13:34:59Z")

</div>

and Python version ?

---

<div class="post-metadata">

**Author:** ![MerlinKeller](https://yyz2.discourse-cdn.com/free1/user_avatar/openturns.discourse.group/merlinkeller/32/190_2.png) [@MerlinKeller](https://openturns.discourse.group/u/MerlinKeller)\
**Post date:** [March 13, 2025, 2:32pm UTC](https://openturns.discourse.group/t/can-an-openturns-pythonfunction-transmit-exceptions-from-the-python-function/408/5 "2025-03-13T14:32:16Z")

</div>

Python 3.10.13… Seems like I was looking for trouble! 😅
