On Thu, Mar 11, 2010 at 6:16 PM, Ondrej Certik <[email protected]> wrote:
> On Thu, Mar 11, 2010 at 6:14 PM, Ondrej Certik <[email protected]> wrote:
>> Hi,
>>
>> I am attaching the two files that I want Cython to generate.
>>
>> It seem to do the job. So far, I just did them by hand and I will test
>> my code more in the coming days. If all is fine, I will try to modify
>> Cython to do that automatically. The static->extern is easy, this
>
> Btw, and here is my Python() class so far:
>
> class Python {
> public:
> Python();
> Python(int argc, char* argv[]);
> ~Python();
> void eval(const char *text);
> void insert_object(const char *name, PyObject *o);
> PyObject *get_object(const char *name);
> };
>
>
> and implementation:
>
>
>
> #include <stdexcept>
>
> #include "python_api.h"
>
> static int python_count=0;
>
> Python::Python()
> {
> Python::Python(-1, NULL);
> }
>
> Python::Python(int argc, char* argv[])
> {
> python_count++;
> if (python_count == 1) {
> // This is a hack:
> putenv((char *)"PYTHONPATH=../..");
> Py_Initialize();
> if (argc >= 0)
> PySys_SetArgv(argc, argv);
> if (import__hermes_common())
> throw std::runtime_error("hermes_common failed to import.");
> }
> }
>
> Python::~Python()
> {
> python_count--;
> if (python_count == 0) {
> Py_Finalize();
> }
> }
>
> void Python::eval(const char *text)
> {
> cmd(text);
> }
>
> void Python::insert_object(const char *name, PyObject *o)
> {
> ::insert_object(name, o);
> }
>
> PyObject *Python::get_object(const char *name)
> {
> ::get_object(name);
> }
>
>
> It's still a work in progress. I'll rename insert_object to push(),
> get_object to pull(), eval() to exec() and also implement local
> namespace for each instance.
And here is how to use it:
void test_basic2()
{
Python *p = new Python();
p->insert_object("i", c2py_int(5));
p->eval("i = i*2");
int i = py2c_int(p->get_object("i"));
_assert(i == 10);
delete p;
}
All I need to do is to link with libhermes_common.so and that's it.
Ondrej
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev