GitXplorerGitXplorer
r

python-c-api-exmple

public
0 stars
0 forks
0 issues

Commits

List of commits on branch master.
Verified
7c6dfdb8062e4ad5aff267a3bff6d1bcce1d48fb

Update README.md

rrobinsonweng committed 4 years ago
Unverified
79a5ca2de145817ecfc8f5528fdc5ce39c4c1947

adding explain how c handle python error(README.md)

rrobinsonweng committed 4 years ago
Unverified
5aaa7c23a821f82290a8a495fbbb953cf3a462cf

edit comment in src(spammodule.c)

rrobinsonweng committed 4 years ago
Unverified
e2834fbcf13904881458b624b6f8d8337fe8d807

adding sorce code im example3

rrobinsonweng committed 4 years ago
Unverified
7fb837159f711953dcf101e4591470019a2532ac

adding explain how C handle Python args

rrobinsonweng committed 4 years ago
Unverified
cd121ad5d09101e93e1bdec6a89061b26e189ebb

adding more comment about handling args between python and c(spammodule.c)

rrobinsonweng committed 4 years ago

README

The README file for this repository.

Python-C-extension-via-python-api

Some practice and demo about python c extension via python api, also some general notes.

There are four parts of creating Python function in c:

  • Python header files
    • Python.h
      • Incording to Python document, this header preprocess alot of things (such as stdlib.h, stdio.h), so you shuould always include it first before any other staderd header file.
      • #define PY_SSIZE_T_CLEAN
  • Method functions
    • The file then definds the function to be called in Python, use two python object as input, then returns a python object back to interpreter as result, or return a NULL to trigger an exception(Note: return a NULL didn't handle an error).
    • Incording to Python Programming by Mark Lutz

      you shuould use more specific type names, but don't always have to, because Python calls them by pointer, not name.

  • Registration table
    • The Python.h file provides an initalized table (e.g PyMethodDef, PyModuleDef)that maps function attribute to function pointers(address)
  • Initialzation funciton
    • Finally, the Python.h provide a initialzation function, witch Python call the fist time when this module is imported into a python Program. When Py_Init_module is called, the function will bulid a dict table in the current module.Once so initialized, calls from Python are routed through registration table's function table.