github.phpd.cn/thought-machine/please@v12.2.0+incompatible/test/cc_rules/clang/so_test.cc (about)

     1  // Simple Python extension, this happens to be a handy way of testing that
     2  // cc_shared_object actually does something useful.
     3  
     4  #include <string>
     5  #include <Python.h>
     6  
     7  #include "test/cc_rules/clang/embedded_files.h"
     8  
     9  
    10  namespace thought_machine {
    11  
    12  PyObject* get_file1(PyObject *self, PyObject *args) {
    13      return PyUnicode_FromString(embedded_file1_contents().c_str());
    14  }
    15  
    16  PyObject* get_file3(PyObject *self, PyObject *args) {
    17      return PyUnicode_FromString(embedded_file3_contents().c_str());
    18  }
    19  
    20  static PyMethodDef so_test_methods[] = {
    21      {"get_embedded_file_1", get_file1, METH_VARARGS, "gets the first embedded file"},
    22      {"get_embedded_file_3", get_file3, METH_VARARGS, "gets the third embedded file"},
    23      {NULL, NULL, 0, NULL}
    24  };
    25  
    26  #if PY_MAJOR_VERSION >= 3
    27  #define GETSTATE(m) ((struct module_state*)PyModule_GetState(m))
    28  
    29  struct module_state {
    30      PyObject *error;
    31  };
    32  
    33  static int so_test_traverse(PyObject *m, visitproc visit, void *arg) {
    34      Py_VISIT(GETSTATE(m)->error);
    35      return 0;
    36  }
    37  
    38  static int so_test_clear(PyObject *m) {
    39      Py_CLEAR(GETSTATE(m)->error);
    40      return 0;
    41  }
    42  
    43  static struct PyModuleDef so_test_def = {
    44      PyModuleDef_HEAD_INIT,
    45      "so_test",
    46      NULL,
    47      sizeof(struct module_state),
    48      so_test_methods,
    49      NULL,
    50      so_test_traverse,
    51      so_test_clear,
    52      NULL
    53  };
    54  
    55  #endif  // PY_MAJOR_VERSION >= 3
    56  
    57  }  // namespace thought_machine
    58  
    59  #if PY_MAJOR_VERSION >= 3
    60  PyMODINIT_FUNC PyInit_so_test() {
    61      return PyModule_Create(&thought_machine::so_test_def);
    62  }
    63  #else
    64  PyMODINIT_FUNC initso_test() {
    65      Py_InitModule("so_test", thought_machine::so_test_methods);
    66  }
    67  #endif