KxSystems / embedPy

Allows the kdb+ interpreter to call Python functions
https://code.kx.com/q/interfaces
Apache License 2.0
89 stars 42 forks source link

Optional error message conforming to Python return. KXI-1467 #100

Closed cmccarthy1 closed 3 years ago

cmccarthy1 commented 3 years ago

Previously embedPy only provided the return 'value' of an error retrieved from Python rather than the full traceback. This is shown in the following example a.py

import numpy as np

class Element:
    def __init__(self): pass

a = Element()
periodicTable = np.array(range(7*32)).reshape((7,32))
periodicTable[0][0] = a

When imported this file causes an error however it does not provide information relating to the location of the error in the loaded file as would be provided by the Python traceback

$ q p.q
q).p.import[`a]
'import: int() argument must be a string, a bytes-like object or a number, not 'Element'

Users can now modify how errors are returned within their system by running .p.setpyerr[1] which will update the format that the error message gets returned, namely returning the error traceback if it can be formed so that it can be parsed by the user within a protected execution. This is left to the user.

$ q p.q
q).p.setpyerr[1]
q).p.getpyerr[]
1
q).p.import[`a]
'import: ['Traceback (most recent call last):\n', '  File "/Users/conormccarthy/projects/fusion/embedpy/embedpy_conor/a.py", line 8, in <module>\n    periodicTable[0][0] = a\n', "TypeError: int() argument must be a string, a bytes-like object or a number, not 'Element'\n"]
q).p.setpyerr[0]
q).p.import[`a]
'import: int() argument must be a string, a bytes-like object or a number, not 'Element'
q).p.getpyerr[]
0