473,322 Members | 1,510 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Extending Python - Understanding Flags...

I'm trying to understand the argument flags that are used in the
method table of an extension module written in C.

First let me ask this question about the method table. Is it an C
array named "PyMethodDef"?

Now, onto my questions about the arguments:

[1] I see that even when the Python function we are supplying takes no
arguments, (the argument flag is METH_NOARGS), that we still pass the
C function a reference to "self". Is this "self" actually a pointer to
the section of memory that represents the object in Python that is
calling the method?

[2] What is the difference between "positional" arguments indicated by
the METH_VARARGS argument flag, and "keyword" arguments that are
indicated by the METH_KEYWORDS argument flag? Does one determine the
identity of an argument based on the order or position in which the
arguments are passed to the C function, while the other uses keys in a
dictionary to identify the arguments?

That should be the start of my customization questions... :]

If it is any consolation I hope to take what I learn and put together
a tutorial on expanding Python for those that only have a little
experience with C programming, like myself. I plan to explain
everything like I was teaching a 6-year old. :]

I was just going to put this on a wiki, but is there a way to include
with other Python documentation on an "official" site?

Thanks for the help.

Scott Huey
Jun 13 '06 #1
3 1781
On 13/06/2006 1:45 PM, Redefined Horizons wrote:
I'm trying to understand the argument flags that are used in the
method table of an extension module written in C.

First let me ask this question about the method table. Is it an C
array named "PyMethodDef"?
Its *type* is PyMethodDef (there is a typedef for that in python.h). You
can name it what you like, but in a module called "foo", calling this
thing "foo_methods" might be a reasonable idea.

Please read section 1.4 of the Extending and Embedding Manual.

Now, onto my questions about the arguments:

[1] I see that even when the Python function we are supplying takes no
arguments, (the argument flag is METH_NOARGS), that we still pass the
C function a reference to "self". Is this "self" actually a pointer to
the section of memory that represents the object in Python that is
calling the method?
This is independent of whether it is NOARGS or VARARGS. Irrespective of
whether the C function is intended to appear to the Python caller as a
method of a type/class, or as module-level function, its first arg is
self. In the latter case, it should be unused by the called function. If
you were to print its contents (using the %p format in printf, if
available), you would find that it is NULL.

Please read section 1.1 of the manual.

[2] What is the difference between "positional" arguments indicated by
the METH_VARARGS argument flag, and "keyword" arguments that are
indicated by the METH_KEYWORDS argument flag? Does one determine the
identity of an argument based on the order or position in which the
arguments are passed to the C function, while the other uses keys in a
dictionary to identify the arguments?
The difference is the same as if the called function is a Python
function. In one sense, there is no difference -- even if you declare
keywords, a call can still use positional arguments. Note that
METH_KEYWORDS is not a substitute for METH_VARARGS; it's an add-on -- or
a bitwise-or-on :-)

def afunction(foo, bar, zot):
# positional args, caller's only option is afunction(42, 1, 666)
def bfunction(foo=0, bar="plugh", zot=None):
# keyword args, caller can do:
bfunction(zot=10**100) # using a keyword: args foo & bar get default values
bfunction(10, "xyzzy") # positional: foo = 10; bar = "xyzzy"; zot = 10**100

In a Python function, nothing extra needs to be done.
In a C-extension function, a char * [] is required to supply the names
of the arguments. The default value caper is handled by the caller
initialising the C variables that will receive the argument values.

Manual: please read section 1.4 and section 1.8.
That should be the start of my customization questions... :]

If it is any consolation I hope to take what I learn and put together
a tutorial on expanding Python for those that only have a little
experience with C programming, like myself. I plan to explain
everything like I was teaching a 6-year old. :]

I was just going to put this on a wiki, but is there a way to include
with other Python documentation on an "official" site?


The manual is in fact written as a tutorial. You may prefer to propose
changes to the manual, rather than to publish yet another document.

A good idea is to download the Python source and go looking in the
Modules directory. If module foo is implemented in C, its source will be
there as foomodule.c. For a simple module that provides only functions,
look at e.g. binascii. For something that defines types, try datetime.
They and others should provide you with good examples.

HTH,
John
Jun 13 '06 #2
On 13/06/2006 3:11 PM, John Machin wrote:
def bfunction(foo=0, bar="plugh", zot=None):
# keyword args, caller can do:
bfunction(zot=10**100) # using a keyword: args foo & bar get default values
bfunction(10, "xyzzy") # positional: foo = 10; bar = "xyzzy"; zot = 10**100


The last line above should end with
zot = None
instead of
zot = 10**100

Jun 13 '06 #3
John Machin <sj******@lexicon.net> wrote:
...
If it is any consolation I hope to take what I learn and put together
a tutorial on expanding Python for those that only have a little
experience with C programming, like myself. I plan to explain
everything like I was teaching a 6-year old. :]

I was just going to put this on a wiki, but is there a way to include
with other Python documentation on an "official" site?


The manual is in fact written as a tutorial. You may prefer to propose
changes to the manual, rather than to publish yet another document.


I don't think the manual even TRIES to address "those that only have a
little experience with C programming", nor should it -- there's a vast
niche for a truly tutorial document based on learn-by-doing, which takes
somebody with a "C 101" course as their only C experience, and a good
knowledge of Python, and turns them into writers of C extension modules
for Python.

I suggest to the OP that he starts this effort as a wiki, publicize it
widely but with good taste to try and attract willing helpers, and think
about possibly get it included on Python's official site only if and
when it becomes more mature and well-accepted. Best of luck: I think
it's a worthwhile project and I hope I can be of help (I have published
a number of shorter documents on this subject, and I'd be happy to allow
them to be shared in as much as I retain copyright on most of them --
alas, can't do that for the chapter of Python in a Nutshell that deals
with writing Python extensions in C, since that's (C) O'Reilly!-).
Alex
Jun 14 '06 #4

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

Similar topics

24
by: Jean-Baptiste PERIN | last post by:
Hi, I'm trying to make a windows dll reachable from a python script .. and I'm encountering troubles during link step .. I use "lcc" to compile and link I use python 2.3 under win XP ...
0
by: Benjamin Rutt | last post by:
I have a rather large C++ project which has his own build system (scons) and I would prefer to stay inside scons in order to build some python extensions/embeddings (that is, I prefer to avoid...
2
by: ajikoe | last post by:
Hi, I tried to follow the example in swig homepage. I found error which I don't understand. I use bcc32, I already include directory where my python.h exist in bcc32.cfg. /* File : example.c...
5
by: vbgunz | last post by:
Hello everyone. I own two books. Learning Python and Python in a nutshell. When cross referencing the two books to try and clarify the ideas behind extending methods and delegates, this is where...
2
by: A. Farber | last post by:
Hello, I'm programming a web application and for that I'm trying to extend the standard C string functions, so that they can handle the application/x-www-form-urlencoded format (which encodes...
1
by: jeremito | last post by:
I am trying to learn how to extend and/or embed Python. I have looked at the document "Extending and Embedding the Python Interpreter" and also "Python/C API Reference Manual. In the examples...
5
by: Melissa Evans | last post by:
Hi. I'm new to Python. :) I've modified grappy.py, http://www.stacken.kth.se/~mattiasa/projects/grappy/, a postfix policy daemon for greylisting. to use LDAP as a backend instead of SQL (with...
0
by: Carl Douglas | last post by:
Hi Python fans, I am developing a DLL that is loaded by a host application on windows. I'm using python 2.5. My DLL uses an embedded python interpreter which can access the host application...
16
by: per9000 | last post by:
Hi, I recently started working a lot more in python than I have done in the past. And I discovered something that totally removed the pretty pink clouds of beautifulness that had surrounded my...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.