Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #92186 > unrolled thread

Embedded Python and C Callback functions

Started bydoc.mefisto@gmail.com
First post2015-06-06 11:09 -0700
Last post2015-06-08 07:44 +0200
Articles 9 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  Embedded Python and C Callback functions doc.mefisto@gmail.com - 2015-06-06 11:09 -0700
    Re: Embedded Python and C Callback functions dieter <dieter@handshake.de> - 2015-06-07 08:02 +0200
      Re: Embedded Python and C Callback functions doc.mefisto@gmail.com - 2015-06-07 01:37 -0700
      Re: Embedded Python and C Callback functions doc.mefisto@gmail.com - 2015-06-07 01:56 -0700
        Re: Embedded Python and C Callback functions Laura Creighton <lac@openend.se> - 2015-06-07 11:49 +0200
          Re: Embedded Python and C Callback functions doc.mefisto@gmail.com - 2015-06-07 02:51 -0700
        Re: Embedded Python and C Callback functions Stefan Behnel <stefan_ml@behnel.de> - 2015-06-07 22:32 +0200
      Re: Embedded Python and C Callback functions doc.mefisto@gmail.com - 2015-06-07 02:15 -0700
        Re: Embedded Python and C Callback functions dieter <dieter@handshake.de> - 2015-06-08 07:44 +0200

#92186 — Embedded Python and C Callback functions

Fromdoc.mefisto@gmail.com
Date2015-06-06 11:09 -0700
SubjectEmbedded Python and C Callback functions
Message-ID<daad7377-7a61-4e9e-a442-4ba48f7212b2@googlegroups.com>
Hi. I'm a newbie in python. But I want embed it in my C program.

There is such method of my class:
@staticmethod
def install_instr_callback(callback):
    # set hook for every change of PC
    m68k.set_instr_hook_callback(callback)

And in my C code there is such callback function:
static PyObject *my_instr_callback(PyObject *self);

I want call install_instr_callback from C with specifying of my callback. I do:
PyObject_CallMethod(myClass, "install_instr_callback", "(O)", my_instr_callback);
But python writes me: [b]null argument to internal routine[/b].

How can I fix this problem?

[toc] | [next] | [standalone]


#92204

Fromdieter <dieter@handshake.de>
Date2015-06-07 08:02 +0200
Message-ID<mailman.229.1433656976.13271.python-list@python.org>
In reply to#92186
doc.mefisto@gmail.com writes:

> Hi. I'm a newbie in python. But I want embed it in my C program.
>
> There is such method of my class:
> @staticmethod
> def install_instr_callback(callback):
>     # set hook for every change of PC
>     m68k.set_instr_hook_callback(callback)
>
> And in my C code there is such callback function:
> static PyObject *my_instr_callback(PyObject *self);
>
> I want call install_instr_callback from C with specifying of my callback. I do:
> PyObject_CallMethod(myClass, "install_instr_callback", "(O)", my_instr_callback);
> But python writes me: [b]null argument to internal routine[/b].

If this output really comes from "PyObject_CallMethod", then
some passed in argument is likely "null" - maybe "myclass"?

The Python-C interface is complicated. It might be advicible to
use e.g. "cython" to get the boilder plate code and some intricacies
taken care of. I have done this with "dm.xmlsec.binding". You
can find in its source also an example for the callback defined
in Python and called from "C".

[toc] | [prev] | [next] | [standalone]


#92212

Fromdoc.mefisto@gmail.com
Date2015-06-07 01:37 -0700
Message-ID<49451412-92e4-4a22-9f2b-1d14ee35f836@googlegroups.com>
In reply to#92204
No, myclass is not null. I think my style of passing arguments is wrong.

[toc] | [prev] | [next] | [standalone]


#92214

Fromdoc.mefisto@gmail.com
Date2015-06-07 01:56 -0700
Message-ID<620b003e-a6d1-4113-b6dc-ad3cf0eaeccd@googlegroups.com>
In reply to#92204
And I can't use Cython, because I have C++ module, and I have to use it.

[toc] | [prev] | [next] | [standalone]


#92220

FromLaura Creighton <lac@openend.se>
Date2015-06-07 11:49 +0200
Message-ID<mailman.234.1433670594.13271.python-list@python.org>
In reply to#92214
In a message of Sun, 07 Jun 2015 01:56:47 -0700, doc.mefisto@gmail.com writes:
>And I can't use Cython, because I have C++ module, and I have to use it.
>-- 
>https://mail.python.org/mailman/listinfo/python-list

Are you using Boost?
http://www.boost.org/doc/libs/1_58_0/libs/python/doc/

It handles lots of the setup for you.

Laura

[toc] | [prev] | [next] | [standalone]


#92221

Fromdoc.mefisto@gmail.com
Date2015-06-07 02:51 -0700
Message-ID<1c3a5e09-8b1a-4546-bf3b-e6130429dbe3@googlegroups.com>
In reply to#92220
> >And I can't use Cython, because I have C++ module, and I have to use it.
> >-- 
> >https://mail.python.org/mailman/listinfo/python-list
> 
> Are you using Boost?
> http://www.boost.org/doc/libs/1_58_0/libs/python/doc/
> 
> It handles lots of the setup for you.
> 
> Laura

No, I'm not using Boost. Pure Python.h

[toc] | [prev] | [next] | [standalone]


#92301

FromStefan Behnel <stefan_ml@behnel.de>
Date2015-06-07 22:32 +0200
Message-ID<mailman.275.1433709156.13271.python-list@python.org>
In reply to#92214
doc.mefisto@gmail.com schrieb am 07.06.2015 um 10:56:
> And I can't use Cython, because I have C++ module, and I have to use it.

That's not a valid reason. Cython supports C++ code just fine.

http://docs.cython.org/src/userguide/wrapping_CPlusPlus.html

Stefan

[toc] | [prev] | [next] | [standalone]


#92216

Fromdoc.mefisto@gmail.com
Date2015-06-07 02:15 -0700
Message-ID<3726dccc-76cb-45a2-b0a8-e41ef300be96@googlegroups.com>
In reply to#92204
I'm trying to debug and find my error. It goes wrong when:
 PyObject *v;
            v = va_arg(*p_va, PyObject *);
            if (v != NULL) {
                if (*(*p_format - 1) != 'N')
                    Py_INCREF(v);
            }

it tries to PyINCREF to my passed callback.

[toc] | [prev] | [next] | [standalone]


#92327

Fromdieter <dieter@handshake.de>
Date2015-06-08 07:44 +0200
Message-ID<mailman.290.1433742309.13271.python-list@python.org>
In reply to#92216
doc.mefisto@gmail.com writes:

> I'm trying to debug and find my error. It goes wrong when:
>  PyObject *v;
>             v = va_arg(*p_va, PyObject *);
>             if (v != NULL) {
>                 if (*(*p_format - 1) != 'N')
>                     Py_INCREF(v);
>             }
>
> it tries to PyINCREF to my passed callback.

This indicates that it expects the "callback" to be a Python object.

Check the C-API specification. It should tell you which parameters
are expected to be Python objects and which C objects.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web