SHARE
TWEET

Untitled

a guest Jun 26th, 2019 77 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ---------------"header.h"-----------------------
  2.  
  3. typedef void* HANDLE
  4.  
  5. int open(HANDLE* a_handle_pointer , int open_mode)
  6.  
  7. int use(HANDLE a_handle, int usage_mode )
  8.      
  9. ---------------"main.c" -----------------
  10.  
  11. #include"header.h"
  12.  
  13. HANDLE my_handle ;
  14. int results ;
  15. if(open(&my_handle ,1) == 0) /* open a handle with mode 1 */
  16. {
  17.     printf ("failed to open n);
  18.     return 0;
  19. }
  20.  
  21. else printf("open success n");
  22.  
  23. use(handle , 2); /* use handle (opened with open) in mode 2 */
  24.      
  25. from libc.stdint cimport uintptr_t
  26. cdef extern from "header.h":
  27.      ctypedef void*     HANDLE
  28.      int open(HANDLE* a_handle_pointer , int open_mode)
  29.  
  30.  
  31. def Open( uintptr_t a_handle_pointer , int open_mode)
  32.  
  33.     return open(<HANDLE*> a_handle_pointer , open_mode)
  34.      
  35. " TypeError: an integer is required " when calling the function.
  36.  
  37. >>>from my_module import open
  38. >>>open (handle , 1)
  39.      
  40. // my_module.c: My Python extension!
  41.  
  42. /* Get us the CPython headers.
  43.  */
  44. #include "Python.h"
  45.  
  46. /* And your function's headers, of course.
  47.  */
  48. #include "header.h"
  49.  
  50. /* Actual structures used to store
  51.  * a 'my_module.Handle' internally.
  52.  */
  53. typedef struct
  54. {
  55.     PyObject_HEAD /* The base of all PyObjects. */
  56.     HANDLE handle; /* Our handle, great! */
  57. } my_module_HandleObject;
  58.  
  59. /* The type 'my_module.Handle'. This variable contains
  60.  * a lot of strange, zero, and NULLified fields. Their
  61.  * purpose and story is too obscure for SO, so better
  62.  * off look at the docs for more details.
  63.  */
  64. static PyTypeObject my_module_HandleType =
  65. {
  66.     PyVarObject_HEAD_INIT(NULL, 0)
  67.     "my_module.Handle", /* Of course, this is the type's name. */
  68.     sizeof(my_module_HandleObject), /* An object's size. */
  69.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* ... Don't ask. */
  70.     Py_TPFLAGS_DEFAULT, /* The type's flags. There's nothing special about ours, so use the defaults. */
  71.     NULL /* No docstrings for you! */
  72. };
  73.  
  74. /* The "wrapper" function. It takes a tuple of
  75.  * CPython PyObject's and returns a PyObject.
  76.  */
  77. static PyObject *my_module_load(PyObject *self, PyObject *args)
  78. {
  79.     int load_mode;
  80.     if(!PyArg_ParseTuple(args, "i", &load_mode) != 0) { /* Parse the argument list. It should have one single integer ("i") parameter. */
  81.         return NULL;
  82.     }
  83.  
  84.     /* Create a Handle object, so as to put
  85.      * in itthe handle we're about to get.
  86.      */
  87.     my_module_HandleObject *the_object = PyObject_New(my_module_HandleObject, &my_module_HandleType);
  88.     if(the_object == NULL) {
  89.         return NULL;
  90.     }
  91.  
  92.     /* Finally, do our stuff.
  93.      */
  94.     if(load(&the_object->handle, load_mode) == -1) {
  95.         Py_DECREF(the_object);
  96.         PyErr_SetFromErrno(NULL);
  97.         return NULL;
  98.     }
  99.  
  100.     return (PyObject*)the_object;
  101. }
  102.  
  103. /* The method table. It is a list of structures, each
  104.  * describing a method of our module.
  105.  */
  106. static struct PyMethodDef my_module_functions[] =
  107. {
  108.     {
  109.         "load", /* The method's name, as seen from Python code. */
  110.         (PyCFunction)my_module_load, /* The method itself. */
  111.         METH_VARARGS, /* This means the method takes arguments. */
  112.         NULL, /* We don't have documentation for this, do we? */
  113.     }, { NULL, NULL, 0, NULL } /* End of the list. */
  114. };
  115.  
  116. /* Used to describe the module itself. */
  117. static struct PyModuleDef my_module =
  118. {
  119.     PyModuleDef_HEAD_INIT,
  120.     "my_module", /* The module's name. */
  121.     NULL, /* No docstring. */
  122.     -1,
  123.     my_module_functions,
  124.     NULL, NULL, NULL, NULL
  125. };
  126.  
  127. /* This function _must_ be named this way
  128.  * in order for the module to be named as
  129.  * 'my_module'. This function is sort of
  130.  * the initialization routine for the module.
  131.  */
  132. PyMODINIT_FUNC PyInit_my_module()
  133. {
  134.     my_module_HandleType.tp_new = PyType_GenericNew; /* AFAIK, this is the type's constructor. Use the default. */
  135.     if(PyType_Ready(&my_module_HandleType) < 0) { // Uh, oh. Something went wrong!
  136.         return NULL;
  137.     }
  138.  
  139.     PyObject *this_module = PyModule_Create(&my_module); /* Export the whole module. */
  140.     if(this_module == NULL) {
  141.         return NULL;
  142.     }
  143.  
  144.     Py_INCREF(&my_module_HandleType);
  145.     PyModule_AddObject(this_module, "Handle", (PyObject*)&my_module_HandleType);
  146.  
  147.     return this_module;
  148. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top