473,498 Members | 1,819 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C/API Clarification

First of all, let me say I really do appreciate--and frequently use--the
ample and easy to read Python documentation. However, there are a few
things I'm still unclear on, even after asking multiple questions here
on the list (thanks everyone!) and reading the "Extending" and
"Reference" docs from start to finish. I'm new to Python extension
writing--but I'm learning fast. Still, I have a few questions that would
make my weekend so much better if I had some guidance on them or,
better, examples of other people's working code. :) (Real code, not
over-simplified examples that don't ever make sense in the real
world) :)

Most of my woes are the result of trying to wrap some highly dynamic C++
code. People, of course, are quick to say "use Boost"--which I'm sure is
great--but doesn't actually teach me anything about Python. :)

1. Nowhere in the docs was I able to see an example of a custom object
using anything but a METH_NOARGS method. This is pretty silly since I
would assume that most class methods take a few arguments. I'm using:

PyObject* foo(cstruct* self, PyObject* args, PyObject* kargs)

....and it works, but I'm not even really sure if this is right or where
in the world I got the idea from! According to the docs, METH_VARARGS is
(PyObject*, PyObject*)? My prototype shouldn't even compile... but it
does, even at -Wall. This is an area I really feel like the docs should
elaborate on. All of the tp_* functions are infinitely easier to
implement as they have very specific purposes. However, very little
attention is given to just general type methods, though it could be just
me missing something. :)

2. No where in the docs does it show how to "correctly" create an
instance of your custom PyTypeObject in C (rather than in the
interpreter). I'm using:

PyObject* newobject = _PyObject_New(&PyType_myType);
PyObject* nulltuple = Py_BuildValue("()");
myType_init((cstruct*)(newobject), nulltuple, NULL);

....and it works (or appears to so far), but I really doubt this is how
it's done. I'm guessing I'm not supposed to go anywhere near the
_PyObject_New function. :)

3. I'm not able to return the "self" argument (useful, for instance,
when you want to chain a group of method calls) without screwing things
up really, really bad. For example:

PyObject* somefunc(cstruct* self, PyObject* args, PyObject* kargs) {
self->pointerToData->doSomeStuff();

return (PyObject*)(self);
}

....returns "something"; it has the methods you would expect, but trying
to use the object when it gets back to the interpreter is pretty much
undefined behavior. :) It's seen as a "<refcnt...>" instance in the
interpreter, even though a dir() shows it still has the methods you
would expect.

I'm sorry if I come off as a newb or sound preachy--it certainly isn't
my intention. I love Python and will continue using it even if I never
quite grok it's C API. :)

Weeeeeeeeeee.

Oct 7 '05 #1
1 1252
Sam
Jeremy Moles writes:
in the world I got the idea from! According to the docs, METH_VARARGS is
(PyObject*, PyObject*)?
Right.
My prototype shouldn't even compile... but it
Why wouldn't it compile?
2. No where in the docs does it show how to "correctly" create an
instance of your custom PyTypeObject in C (rather than in the
interpreter). I'm using:
That's what tp_alloc is for. Then you call tp_init to initialize it.

But, your tp_new handler should already invoke tp_alloc, so it's cleaner to
call tp_new, followed by tp_init.
3. I'm not able to return the "self" argument (useful, for instance,
when you want to chain a group of method calls) without screwing things
up really, really bad. For example:

PyObject* somefunc(cstruct* self, PyObject* args, PyObject* kargs) {
self->pointerToData->doSomeStuff();

return (PyObject*)(self);
}

...returns "something"; it has the methods you would expect, but trying
to use the object when it gets back to the interpreter is pretty much
undefined behavior. :) It's seen as a "<refcnt...>" instance in the
interpreter, even though a dir() shows it still has the methods you
would expect.


You need to increment the reference count of the object, in this case.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.7 (GNU/Linux)

iD8DBQBDRxfWx9p3GYHlUOIRAnuVAJ98wt1vRCfvFkzg9BRQgd j1kFBsFACfSukC
RXCoRc+BCD4D+H1BYX+8tSg=
=NQ7s
-----END PGP SIGNATURE-----

Oct 8 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2192
by: Wes | last post by:
I am trying to secure different files, mostly pdf, so only the person suppose to see the file that was designed for that individual can see it. I am using sessions to secure the actual web pages,...
4
2870
by: Shea Martin | last post by:
Which of the following do I use delete instead of just delete. //1.) // not sure about this one, as char is of size 1 char *str = new char; //2.) //not sure about this one, as it is a...
3
4063
by: John D. Sanders | last post by:
I have just upgraded MySQL from version 3.23 to version 4.1.8 and now I am getting the following error when I try to run a script that worked: install_driver(mysql) failed: Can't load...
8
1810
by: Sai Kit Tong | last post by:
In the article, the description for "Modiy DLL That Contains Consumers That Use Managed Code and DLL Exports or Managed Entry Points" suggests the creation of the class ManagedWrapper. If I...
2
2902
by: ravir | last post by:
Hi, I am new to this group. I am working in Perl and shellscripts. I have a clarification regarding perl grep and pattern matching. I am writing a perl script to automate the process of code...
0
7005
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7168
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6891
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7381
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5465
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4916
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3096
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3087
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
659
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.