Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 7 additions & 32 deletions cbits/python.c
Original file line number Diff line number Diff line change
@@ -1,32 +1,6 @@
#include <inline-python.h>
#include <stdlib.h>

void inline_py_export_exception(
PyObject *e_type,
PyObject *e_value,
PyObject *e_trace,
char** p_msg
)
{
// Convert to python string object
PyObject *e_str = PyObject_Str(e_value);
if( 0 == e_str ) {
*p_msg = 0;
return;
}
// Convert to UTF8 C string
const char *err_msg = PyUnicode_AsUTF8(e_str);
if( 0 == e_str ) {
*p_msg = 0;
return;
}
// Copy message
int n = strlen(err_msg);
*p_msg = malloc(n+1);
strcpy(*p_msg, err_msg);
return;
}

PyObject *inline_py_function_wrapper(PyCFunction fun, int flags) {
PyMethodDef *meth = malloc(sizeof(PyMethodDef));
meth->ml_name = "[inline_python]";
Expand All @@ -44,18 +18,20 @@ PyObject *inline_py_function_wrapper(PyCFunction fun, int flags) {
}

int inline_py_unpack_iterable(PyObject *iterable, int n, PyObject **out) {
// Fill out with NULL. This way we can call XDECREF on them
for(int i = 0; i < n; i++) {
out[i] = NULL;
}
// Initialize iterator
// Initialize iterator. If object is not an iterable we treat this
// as not an exception but as a conversion failure
PyObject* iter = PyObject_GetIter( iterable );
if( PyErr_Occurred() ) {
PyErr_Clear();
return -1;
}
if( !PyIter_Check(iter) ) {
goto err_iter;
}
// Fill out with NULL. This way we can call XDECREF on them
for(int i = 0; i < n; i++) {
out[i] = NULL;
}
// Fill elements
for(int i = 0; i < n; i++) {
out[i] = PyIter_Next(iter);
Expand All @@ -81,7 +57,6 @@ int inline_py_unpack_iterable(PyObject *iterable, int n, PyObject **out) {
return -1;
}


void inline_py_free_capsule(PyObject* py) {
PyMethodDef *meth = PyCapsule_GetPointer(py, NULL);
// HACK: We want to release wrappers created by wrapper. It
Expand Down
39 changes: 11 additions & 28 deletions include/inline-python.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,23 @@
#include <Rts.h>


// Standard status codesu
#define INLINE_PY_OK 0
#define INLINE_PY_ERR_COMPILE 1
#define INLINE_PY_ERR_EVAL 2



// This macro checks for errors. If python exception is raised it
// clear it and returns 1 otherwise retruns 0
#define INLINE_PY_SIMPLE_ERROR_HANDLING() do { \
if( PyErr_Occurred() ) { \
PyObject *e_type, *e_value, *e_trace; \
PyErr_Fetch(&e_type, &e_value, &e_trace); \
return 1; \
} \
return 0; \
} while(0)

// Convert python exception into form suitable for haskell
void inline_py_export_exception(
PyObject *e_type,
PyObject *e_value,
PyObject *e_trace,
char** p_msg
);
// ----------------------------------------------------------------
// Standard status codes

#define IPY_OK 0
#define IPY_ERR_PYTHON 1
#define IPY_ERR_COMPILE 2

// ----------------------------------------------------------------

// Unpack iterable into array of PyObjects. Iterable must contain
// exactly N elements.
//
// On success returns 0 and fills `out` with N PyObjects
//
// On failure returns -1. Python exception is not cleared. It's
// responsibility of caller to deal with it. Content of `out` is
// undefined in this case.
// On failure return -1. Content of out is then undefined and it
// doesn't contain live python objects. If failure is due to python
// exception it's not cleared.
int inline_py_unpack_iterable(
PyObject *iterable,
int n,
Expand Down
7 changes: 5 additions & 2 deletions inline-python.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ common language
PatternSynonyms
ViewPatterns
LambdaCase
MultiWayIf
--
NoFieldSelectors
DuplicateRecordFields
Expand Down Expand Up @@ -66,10 +67,11 @@ Library
Python.Inline
Python.Types
Other-modules:
Python.Internal.Types
Python.Internal.Util
Python.Internal.Eval
Python.Internal.EvalQQ
Python.Internal.Program
Python.Internal.Types
Python.Internal.Util
Paths_inline_python
Autogen-modules:
Paths_inline_python
Expand All @@ -84,6 +86,7 @@ library test
hs-source-dirs: test
Exposed-modules:
TST.Run
TST.ToPy
TST.FromPy

test-suite inline-python-tests
Expand Down
2 changes: 2 additions & 0 deletions src/Python/Inline.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ module Python.Inline
, ToPy(..)
, FromPy(..)
, toPy
, fromPyEither
, fromPy
, fromPy'
) where


Expand Down
Loading
Loading