code: def get_module(tmp_path):
      """Some codes to generate data and manage temporary buffers use when
      sharing with numpy via the array interface protocol.
      """
  
      if not sys.platform.startswith("linux"):
          pytest.skip("link fails on cygwin")
  
      prologue = """
          #include <Python.h>
          #define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
          #include <numpy/arrayobject.h>
          #include <stdio.h>
          #include <math.h>
  
          NPY_NO_EXPORT
          void delete_array_struct(PyObject *cap) {
  
              /* get the array interface structure */
              PyArrayInterface *inter = (PyArrayInterface*)
                  PyCapsule_GetPointer(cap, NULL);
  
              /* get the buffer by which data was shared */
              double *ptr = (double*)PyCapsule_GetContext(cap);
  
              /* for the purposes of the regression test set the elements
                 to nan */
              for (npy_intp i = 0; i < inter->shape[0]; ++i)
                  ptr[i] = nan("");
  
              /* free the shared buffer */
              free(ptr);
  
              /* free the array interface structure */
              free(inter->shape);
              free(inter);
  
              fprintf(stderr, "delete_array_struct\\ncap = %ld inter = %ld"
                  " ptr = %ld\\n", (long)cap, (long)inter, (long)ptr);
          }
          """
  
      functions = [
          (
              "new_array_struct",
              "METH_VARARGS",
              """
  
              long long n_elem = 0;
              double value = 0.0;
  
              if (!PyArg_ParseTuple(args, "Ld", &n_elem, &value)) {
                  Py_RETURN_NONE;
              }
  
              /* allocate and initialize the data to share with numpy */
              long long n_bytes = n_elem*sizeof(double);
              double *data = (double*)malloc(n_bytes);
  
              if (!data) {
                  PyErr_Format(PyExc_MemoryError,
                      "Failed to malloc %lld bytes", n_bytes);
  
                  Py_RETURN_NONE;
              }
  
              for (long long i = 0; i < n_elem; ++i) {
                  data[i] = value;
              }
  
              /* calculate the shape and stride */
              int nd = 1;
  
              npy_intp *ss = (npy_intp*)malloc(2*nd*sizeof(npy_intp));
              npy_intp *shape = ss;
              npy_intp *stride = ss + nd;
  
              shape[0] = n_elem;
              stride[0] = sizeof(double);
  
              /* construct the array interface */
              PyArrayInterface *inter = (PyArrayInterface*)
                  malloc(sizeof(PyArrayInterface));
  
              memset(inter, 0, sizeof(PyArrayInterface));
  
              inter->two = 2;
              inter->nd = nd;
              inter->typekind = 'f';
              inter->itemsize = sizeof(double);
              inter->shape = shape;
              inter->strides = stride;
              inter->data = data;
              inter->flags = NPY_ARRAY_WRITEABLE | NPY_ARRAY_NOTSWAPPED |
                             NPY_ARRAY_ALIGNED | NPY_ARRAY_C_CONTIGUOUS;
  
              /* package into a capsule */
              PyObject *cap = PyCapsule_New(inter, NULL, delete_array_struct);
  
              /* save the pointer to the data */
              PyCapsule_SetContext(cap, data);
  
              fprintf(stderr, "new_array_struct\\ncap = %ld inter = %ld"
                  " ptr = %ld\\n", (long)cap, (long)inter, (long)data);
  
              return cap;
          """,
          )
      ]
  
      more_init = "import_array();"
  
      try:
          import array_interface_testing
  
          return array_interface_testing
      except ImportError:
          pass
  
      # if it does not exist, build and load it
      return extbuild.build_and_import_extension(
          "array_interface_testing",
          functions,
          prologue=prologue,
          include_dirs=[np.get_include()],
          build_dir=tmp_path,
          more_init=more_init,
      )
  
imports: import sys
  import pytest
  import numpy
  from numpy.testing import extbuild
label_desc: Use image node_id 1 for calling a global function with example usage: get_module(tmp_path) and returns: extbuild, array_interface_testing