473,696 Members | 1,921 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 "PyMethodDe f"?

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 1803
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 "PyMethodDe f"?
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_method s" 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=1 0**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=1 0**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******@lexic on.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
3137
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 Here's the compile command which seems to work well : -----------------------------------------------------
0
984
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 using distutils directly to build my extensions). Can someone confirm that I'm doing the right thing to pick up the necessary dependencies to pick up the compilation flags (the first output line below, once I prefix it with '-I'), location of...
2
4448
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 */ #include <time.h>
5
3827
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 confusion veered it's ugly head :( Learning Python explains on page 324: Class Interface Techniques (21.3.3 in the ebook) the following is an extender method. ''' #################################### '''
2
3731
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 non-alphanumeric characters as %XY). I've written my own xstrlen(), xstrlcat(), xstrlcpy and xstrdup() (please see the source code on the bottom of this post).
1
1856
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 shown in "Extending..." there are some things I ma not familiar with so I turn to the index in the Reference Manual, but they are not listed. For example, PyEval_CallObject and PyDict_GetAttrString. My question is this: Is the documentation out of...
5
2180
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 python-ldap.) The daemon runs fine when testing but when I put it under load it core dumps quickly. What little analysis I know how to do shows similar information every time. Any advice on where to go from here? Thanks!
0
1135
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 through an API which I have exposed using SWIG 1.3.31. Therefore I have both extended and embedded Python at once: the SWIG generated code is statically linked into my DLL, and one of the
16
2598
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 previous python experiences: magic names (I felt almost as sad as when I discovered the strange pink worms that eat you in nethack, not to mention the mind flayers - I really hate them). I guess all programming languages have magic names to some...
0
8666
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9145
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9010
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8853
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7703
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6515
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4356
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3033
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
1992
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.