Extending & Embedding Python Using C - A First Module
Written by Mike James   
Tuesday, 05 March 2024
Article Index
Extending & Embedding Python Using C - A First Module
Initializing the arith Module
Example Module

Example Module

To make the remaining examples easier we can make use of an examples module that you can re-use as a template:

#define PY_SSIZE_T_CLEAN
#include <Python.h>
static PyObject* exampleFunction(PyObject *self, 
PyObject *args) { //example function code } static PyMethodDef AddMethods[] = { {"exampleFunction", exampleFunction, METH_VARARGS,
"an example"}, {NULL, NULL, 0, NULL} // sentinel }; static struct PyModuleDef addmodule = { PyModuleDef_HEAD_INIT, "example", "C library to test API", -1, AddMethods }; PyMODINIT_FUNC PyInit_example(void) { return PyModule_Create(&addmodule); }

Also make sure that the file names used in task.json are correct and that example compiles.

In future chapters we will simply add functions to this module and add them to the method definition. This saves having to repeat the same task.json and launch.json files.

Summary

  • The main principle of the C API is that all Python objects are represented by a specific C Struct that is an extension of PyObject and as a result any Python object can be referenced by a PyObject*

  • The C API contains many of the functions that implement Python. In many cases there is a simple equivalence between Python functions and C functions.

  • Every module has an initialization function with the name PyInit_modulename.

  • The initialization function is called when the module is imported and it creates the module object and returns it.

  • The PyModuleDef struct has fields that determine how the module will be treated by the system.

  • An array of PyMethodDef structs specifies the methods/functions that the module provides.

  • The PyArg_ParseTuple function can be used to parse the tuple of arguments passed to the function.

  • A simple calculation demonstrates that converting a Python function to C can speed it up by a factor of roughly 50.

  • To avoid having to change the compiler settings, a standard example extension is used in the rest of this book.

Extending & Embedding Python Using C

By Mike James

extendPython360

Buy from Amazon.

Contents

       Preface

  1. Extending And Embedding Python
  2. A First C Module Using Linux ***NEW!!!
  3. A First C Module Using Windows
  4. Module Basics
        Extract: A First Module
        Extract: 
    Pi 
  5. Arguments
  6. Returning Python Objects
  7. Objects And Attributes
  8. More Complex Objects – Tuples, Lists and Dicts
  9. Errors, Exceptions And Reference Counting
        Extract:
    Exceptions 
  10. Bytes And Strings
  11. Modules And Attributes
  12. New Types
  13. Advanced Types
  14. Threads And The GIL
  15. Embedding Python

<ASIN:B0CK3X93KF>

To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.

Banner


Remembering Robert Dennard, Inventor of DRAM
19/05/2024

Robert Dennard, the IBM engineer who invented the key memory technology DRAM that we now rely on in our computers smartphones and tablets,  passed away on April 23rd, 2024, at the age of 91.



Google Reduces Support For Python, Dart And Flutter
01/05/2024

There are many reports that Google has removed people from its Python, Dart and Flutter teams and possibly more. What does this say about relying on Google as a source of technology for your projects? [ ... ]


More News

raspberry pi books

 

Comments




or email your comment to: comments@i-programmer.info



Last Updated ( Tuesday, 05 March 2024 )