Example of using C language to expand Python Functions

Source: Internet
Author: User
Tags split words

Click here to view the python Extension function in C language.

As long as Python is installed, no additional installation is required for python extension programming using C. python will prevent header files from being placed under/usr/include/python, the version varies slightly.

The following is a routine that will generate a module that can be imported by Python named example, which contains a splitwords function. This function accepts two parameters. The first is a string containing words, the second character is the word separator, which is also a string. Each character is used as a character to separate words. For convenience, all functions are stored in the example. c file.

// Filename example. c

# Include <python. h>

Pyobject * splitwords (const char * SZ, const char * SP)
{
// Python objects
Pyobject * List = pylist_new (0 );
If (null = List | null = sz)
Return list;
If (null = Sp ){
Pylist_append (list, py_buildvalue ("S", SZ ));
Return list;
}
// Split words
Const char * pbgn = NULL, * pend;
Size_t begin = 0, end;
Size_t Len = strlen (sz );
Char Buf [0x20];
While (begin <Len ){
If (strchr (SP, SZ [begin]) {
// String begins w/'SP'
++ Begin;
Continue;
}
Else {
Size_t loc = begin;
While (loc <Len ){
If (strchr (SP, SZ [loc]) {
// Word stops here.
End = LOC;
Break;
}
Else {
++ LOC;
Continue;
}
}
If (loc = Len) End = Len;
Size_t L = end-begin;
If (L> = 0x20) L = 0x19;
Strncpy (BUF, SZ + begin, L );
Buf [l] = '/0 ';
// Puts (BUF );
Pylist_append (list, py_buildvalue ("S", Buf ));
// Next word
Begin = end;
}
}

Return list;
}

Pyobject * wrap_splitwords (pyobject * Self, pyobject * ARGs)
{
Const char * sp, * SZ;
If (! Pyarg_parsetuple (ARGs, "SS", & sp, & sz ))
Return NULL;
Return (splitwords (SP, SZ ));
}

Static pymethoddef methods [] = {
{"Splitwords", wrap_splitwords, meth_varargs, "split words "},
{Null, null, 0, null}
};

Void initexample ()
{
Pyobject * m;
M = py_initmodule ("example", methods );
}

The Python object generated here is returned to Python, so you do not need to consider the reference count issue. Splitwords is an extension function in C language and encapsulated in wrap_splitwords. The list of methods in example is provided by methods, and the initialization function is initexample. As long as you have read the reference link above, no further explanation is required.

Because my Python version is 2.4.4, its header file directory is located under/usr/include/python2.4. Use GCC for compilation. Do not use g ++. Take this example as an example. The 'initexample 'symbol can be found in the so generated by GCC, while G ++ generates '_ z15initexamplev', python will encounter an error where the initialization function cannot be found during import:

Importerror: dynamic module does not define init function (initexample)

If you must use g ++, you may be able to put the C ++ extension into a single file to generate a static library file, and the encapsulated program still uses GCC. This idea has not been tested yet. Below we only use GCC:

Gcc-FPIC-c-I/usr/include/python2.4-I/usr/lib/python2.4/config example. c
Gcc-shared-O example. So example. o

As long as example. So can be found (in the current directory of Python running, or under/usr/lib, or specified by LD_LIBRARY_PATH), python can be directly imported:

>>> Import example
>>> S = 'AB, CDE: ghij klmno'
>>> Example. splitwords (S ,'')
['AB, CDE: ghij', 'klmno']
>>> Example. splitwords (S ,':')
['AB, cde', 'ghij', 'klmno']
>>> Example. splitwords (S ,':,')
['AB', 'cde', 'ghij', 'klmno']

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.