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

Lift Python interoperability from C to ASR #2050

Open
Shaikh-Ubaid opened this issue Jun 27, 2023 · 4 comments
Open

Lift Python interoperability from C to ASR #2050

Shaikh-Ubaid opened this issue Jun 27, 2023 · 4 comments
Assignees

Comments

@Shaikh-Ubaid
Copy link
Collaborator

          The next step is to lift the Python interoperability from C to ASR (see #1996) for both `@pythoncall` and `@pythoncallable`, this will make it work in all our backends. It should transform BindC functions into directly calls to Python C/API, by probably having some module with the C/API interfaces and then calling them appropriately, all in ASR.

Originally posted by @certik in #1704 (comment)

@Shaikh-Ubaid
Copy link
Collaborator Author

Shaikh-Ubaid commented Jul 6, 2023

// Defined in `pyerrors.h`

PyAPI_DATA(PyObject *) PyExc_BaseException;
PyAPI_DATA(PyObject *) PyExc_Exception;
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03050000
PyAPI_DATA(PyObject *) PyExc_StopAsyncIteration;
#endif
PyAPI_DATA(PyObject *) PyExc_StopIteration;
PyAPI_DATA(PyObject *) PyExc_GeneratorExit;
PyAPI_DATA(PyObject *) PyExc_ArithmeticError;
PyAPI_DATA(PyObject *) PyExc_LookupError;

PyAPI_DATA(PyObject *) PyExc_AssertionError;
PyAPI_DATA(PyObject *) PyExc_AttributeError;
PyAPI_DATA(PyObject *) PyExc_BufferError;
PyAPI_DATA(PyObject *) PyExc_EOFError;
PyAPI_DATA(PyObject *) PyExc_FloatingPointError;
PyAPI_DATA(PyObject *) PyExc_OSError;
PyAPI_DATA(PyObject *) PyExc_ImportError;

It seems there are several predefined exception codes which get defined when including Python.h. We currently use the exception PyExc_ImportError as follows:

For example:

    if (_import_array() < 0) {
        PyErr_Print();
        PyErr_SetString(PyExc_ImportError, "numpy.core.multiarray failed to import");
        fprintf(stderr, "Failed to import numpy Python module(s)\n");
        return -1;
    }

How do we support the above exception codes at the ASR Level?
Do we currently have a way to declare a BindC variable?

@Shaikh-Ubaid
Copy link
Collaborator Author

The C backend also needs to include the following:

#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
#include <Python.h>
#include <numpy/arrayobject.h>

Do we have a mechanism in the ASR to support these?

@certik
Copy link
Contributor

certik commented Jul 7, 2023

For global variables, let's first implement them in LFortran and test it well: lfortran/lfortran#1951, then let's figure out some syntax in LPython to use it as well.

@certik
Copy link
Contributor

certik commented Jul 7, 2023

The next step is to do what is done here: #2107 (comment), create regular LPython code and try to get the interoperability with CPython working, using current LPython. If there are any bugs or missing features, let's fix that. After that works, let's create the ASR pass that will do the transformation from bind(python) automatically to the equivalent of our hand written manual code.

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