473,406 Members | 2,769 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,406 developers and data experts.

Extending Python

kaarthikeyapreyan
Python an easy to program language becomes very powerful when they interact close with your system,for this purpose he gets friendly with this guy named C.
Python establishes tightly bound wrappers with C ,so that it can extend the C APIs,and also get into closer relation with the system interface.Writing wrappers in python is achieved by using PyObjects in the C code its easy to distinguish the python code in the C file as most of the python objects and APIs start with Py or PY.
In the process of writing a python wrapper for a C function the following steps emerge

#myadd.c
Expand|Select|Wrap|Line Numbers
  1. int add(int a, int b)
  2. {
  3.   return a+b;
  4. }
  5.  
This simple C function takes two arguments and retuns their sum

#wrapping.c
#include<python2.4/Python.h>

The first step would be to include a Python header ,which includes a few of the necessary commands like stdio.h,string.h...
The second step would be to convert function arguments from Python to C,and to return results in a Python-friendly form.

Expand|Select|Wrap|Line Numbers
  1. PyObject *wrap_add(PyObject *self, PyObject *args) {
  2.      int a,b,result;
  3.      if (!PyArg_ParseTuple(args,"ii",&a,&b))
  4.           return NULL;
  5.      result = add(a,b);
  6.      return Py_BuildValue("i",result);
  7. }
The python wrap function has two PyObject arguments self and args. self is used when C implements a builtin functions and args for the python tuple which containd the arguments
The conversion of data between Python and C is performed using two functions :
int PyArg_ParseTuple(PyObject *args, char *format, ...)
PyObject *Py_BuildValue(char *format, ...)

The PyArg_ParseTuple is a python API which checks for the types of the argument that are passed,stored in the memory if they are right and return a null when wrong.The Py_BuildValue returns the result in a python form from the C function.
In our case add takes two arguments which is passed to the PyArg_ParseTuple then the C function add is called and the value is stored in a integer result,which is retrived using the Py_BuildValue

The last step would be to make the interpreter understand that we have implemented a wrapper.We intialise it using the function

Expand|Select|Wrap|Line Numbers
  1. static PyMethodDef exampleMethods[] = {
  2.          { "add", wrap_add, METH_VARARGS,"A simple addition program" },
  3.          { NULL, NULL, 0, NULL }
  4. };
  5. void initmyadd() {
  6.          PyObject *m;
  7.          m = Py_InitModule("myadd", exampleMethods);
  8. }
In the PyMethodDef The first value is the name which I would like to give for the method in python(U can have a different name instead of add),The METH_VARARGS is a flag telling the interpreter the calling convention to be used for the C function.The fourth is the doc string.
When myadd is called for the first time It calls initmyadd,which creates a module object (which is inserted in the dictionary sys.modules under the key "myadd"),and inserts built-in function objects into the newly created module based upon the table (an array of PyMethodDef structures) that was passed as its second argument. Py_InitModule() returns null when intializations go wrong.PyMODINIT_FUNC API is used for void return type function declarations.
Apr 3 '08 #1
0 3895

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Alicia Haumann | last post by:
I accidentally sent this to webmaster@python.org, so this could be a duplicate if "webmaster" forwards it to this list. :{ Hi, there. Thanks for any help that can be offered. I've been...
3
by: stefan | last post by:
Hi Folks, I currenty extended some of my C++ functionality to python and also embedded python to use python functionality in my C++ system (and use as well these extended functions). While...
1
by: Richard Townsend | last post by:
In the "Extending and Embedding" part of the Python documentation: section 5.4 "Extending Embedded Python" - it describes how to use a Python extension module from Python that is embedded in a C...
3
by: Marco Meoni | last post by:
Hi all! I've a problem with a C++ class that has to be included in a python application. One way to do it is Extending and Embedding the Python Interpreter Now i have 2 questions 1) Is there a...
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...
3
by: Redefined Horizons | last post by:
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...
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...
6
by: Qun Cao | last post by:
Hi Everyone, I am a beginner on cross language development. My problem at hand is to build a python interface for a C++ application built on top of a 3D game engine. The purpose of this python...
7
by: Maximus Decimus | last post by:
HI all, I am using python v2.5 and I am an amateur working on python. I am extending python for my research work and would like some help and guidance w.r.t this matter from you experienced...
0
by: Tim Spens | last post by:
--- On Fri, 6/27/08, Tim Spens <t_spens@yahoo.comwrote: I think I know where the problem is but I'm unsure how to fix it. When I call Register_Handler(...) from python via...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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
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,...
0
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...

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.