On Wed, Mar 3, 2010 at 6:23 PM, Lisandro Dalcin <[email protected]> wrote:
> On 3 March 2010 23:08, Ondrej Certik <[email protected]> wrote:
>>>
>>> Am I being clear enough?
>>
>> Yes, that is clear to me, thanks.
>>
>
> OK. Once you have something working, let's polish it and ask other for
> opinions, then push to cython-devel.
If I go the global Python() class route, I would need to generate the
Python() class automatically, since the list of my Cython functions
can be quite long, so I don't want to keep updating the Python class
interface by hand.
So I am probably going to take the combination of both approaches that
you suggested:
1) implement the global Python() class, that takes care of execution
contexts, initializing python and the Cython API and has the following
methods:
void cmd(const char*) // runs a Python command in the (possibly local)
context/namespace
void insert_object(const char*, PyObject *) // inserts a Python object
into the context/namespace
PyObject *get_object(const char*) // gets a Python object from the context
2) the helper functions for doing the actual conversion to and from Python like:
static PyObject *(*c2py_CooMatrix)(struct CooMatrix *);
static PyObject *(*c2py_CSRMatrix)(struct CSRMatrix *);
static PyObject *(*c2py_int)(int);
static int (*py2c_int)(PyObject *);
static char *(*py2c_str)(PyObject *);
static double (*py2c_double)(PyObject *);
static PyObject *(*c2numpy_int)(int *, int);
static PyObject *(*c2numpy_int_inplace)(int *, int);
static PyObject *(*c2numpy_double)(double *, int);
static PyObject *(*c2numpy_double_inplace)(double *, int);
static void (*numpy2c_int_inplace)(PyObject *, int **, int *);
static void (*numpy2c_double_inplace)(PyObject *, double **, int *);
.... [way more]
Would be global and they would work out of the box in any C++ file, as
long as you instantiate the Python() class once anywhere.
>
>>
>> That is already implemented, here is my exec function:
>>
>> cdef api object run_cmd(const_char_p text, object namespace):
>> try:
>> verbose = namespace.get("verbose")
>> if verbose:
>> print "got a text:", text
>> if verbose:
>> print "evaluting in the namespace:"
>> print namespace
>> code = compile(text, "", "exec")
>> eval(code, {}, namespace)
>> if verbose:
>> print "new namespace:"
>> print namespace
>> return namespace
>> except SystemExit, e:
>> try:
>> exit_code = int(e)
>> except:
>> exit_code = -1
>> exit(exit_code)
>> except:
>> etype, value, tb = sys.exc_info()
>> s = "".join(traceback.format_exception(etype, value, tb))
>> s = "Exception raised in the Python code:\n" + s
>> throw_exception(s)
>>
>
> Mmm, are you sure you want to handle SystemExit by actually calling
> exit(e)? Is this 'exit' actually 'sys.exit' ? In such case, why just
> not use 'raise' to re-raise the exception?
Yes, maybe. I haven't thought about this much.
>
>>
>> Ok, great! i was worried if there were some issues, but it seems
>> everything will work very smoothly and the C++ guys in my group will
>> not even notice that they are using Python. :)
>>
>
> But they should notice, just to have a chance to fall in love with the beast
> :-)
:)
Ondrej
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev