"Python coolbook" C extension Library-its five _c language level Python libraries call API (to be continued)

Source: Internet
Author: User

One, C level module add API

We still operate on the following structures,

#include <math.h>typedef struct Point {    double x, y;} Point;

The objective of this section is to encapsulate the two-point struct's operation function as the C-level API of sample library, which can be called by C library other than sample, first write the following function pointer struct instance,

/* pysample.c */static pyobject *pypoint_frompoint (point *p, int must_free) {  /* capsules and C-pointers are similar. Internally, they get a generic pointer and a name that can be      easily created using the Pycapsule_new () function. In addition, an optional destructor can be     bound to the capsule to release the underlying memory when the capsule object is garbage collected *  /return pycapsule_new (P, "point", Must_free del_point:null);} /* Utility functions */static point *pypoint_aspoint (Pyobject *obj) {  return (point *) pycapsule_getpointer (obj, "Poi NT ");} Static _pointapimethods _point_api = {  pypoint_aspoint,  pypoint_frompoint};

The structure is defined as follows, in a new header function

/* pysample.h *//* Public API table *//* The most important part here is the function pointer table _pointapimethods.   It is initialized when the module is exported, and then is found when the module is imported. */typedef struct {point  * (*aspoint) (Pyobject *);  Pyobject * (*frompoint) (point *, int);} _pointapimethods;

"Python coolbook" C extension Library-its five _c language level Python libraries call API (to be continued)

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.