/lazy/_undefined.c

https://github.com/llllllllll/lazy_python · C · 92 lines · 75 code · 17 blank · 0 comment · 8 complexity · 7a7178ada2e3087aa80d276edb113a42 MD5 · raw file

  1. #include <Python.h>
  2. #include "lazy.h"
  3. static PyObject *
  4. undefined_strict(PyObject *self, PyObject *_)
  5. {
  6. PyErr_SetObject((PyObject*) Py_TYPE(self), self);
  7. return NULL;
  8. }
  9. static PyMethodDef strict = {
  10. "__strict__",
  11. (PyCFunction) undefined_strict,
  12. METH_NOARGS,
  13. ""
  14. };
  15. PyDoc_STRVAR(module_doc,"An undefined value.");
  16. static struct PyModuleDef _undefined_module = {
  17. PyModuleDef_HEAD_INIT,
  18. "lazy._undefined",
  19. module_doc,
  20. -1,
  21. NULL,
  22. NULL,
  23. NULL,
  24. NULL,
  25. NULL
  26. };
  27. PyMODINIT_FUNC
  28. PyInit__undefined(void)
  29. {
  30. LzExported *lazy_symbols;
  31. PyObject *strict_meth = NULL;
  32. PyObject *undefined_inner_type = NULL;
  33. PyObject *undefined_inner = NULL;
  34. PyObject *undefined = NULL;
  35. PyObject *m;
  36. int err;
  37. if (!(lazy_symbols =
  38. PyCapsule_Import("lazy._thunk._exported_symbols", 0))) {
  39. return NULL;
  40. }
  41. if (!(undefined_inner_type = PyErr_NewException(
  42. "lazy._undefined.undefined", NULL, NULL))) {
  43. return NULL;
  44. }
  45. if (!(undefined_inner = PyObject_CallFunctionObjArgs(
  46. undefined_inner_type, NULL))) {
  47. goto error;
  48. }
  49. if (!(undefined = lazy_symbols->LzThunk_FromExpr(undefined_inner))) {
  50. goto error;
  51. }
  52. if (!(m = PyModule_Create(&_undefined_module))) {
  53. goto error;
  54. }
  55. if (!(strict_meth = PyCFunction_NewEx(&strict, undefined_inner, m))) {
  56. goto error;
  57. }
  58. err = PyObject_SetAttrString(undefined_inner_type,
  59. "__strict__",
  60. strict_meth);
  61. Py_DECREF(strict_meth);
  62. if (err) {
  63. goto error;
  64. }
  65. if (PyObject_SetAttrString(m, "undefined", undefined)) {
  66. goto error;
  67. }
  68. return m;
  69. error:
  70. Py_XDECREF(strict_meth);
  71. Py_XDECREF(undefined_inner_type);
  72. Py_XDECREF(undefined_inner);
  73. Py_XDECREF(undefined);
  74. return NULL;
  75. }