istr.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #ifndef _MULTIDICT_ISTR_H
  2. #define _MULTIDICT_ISTR_H
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif
  6. typedef struct {
  7. PyUnicodeObject str;
  8. PyObject * canonical;
  9. } istrobject;
  10. PyDoc_STRVAR(istr__doc__, "istr class implementation");
  11. static PyTypeObject istr_type;
  12. static inline void
  13. istr_dealloc(istrobject *self)
  14. {
  15. Py_XDECREF(self->canonical);
  16. PyUnicode_Type.tp_dealloc((PyObject*)self);
  17. }
  18. static inline PyObject *
  19. istr_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
  20. {
  21. PyObject *x = NULL;
  22. static char *kwlist[] = {"object", "encoding", "errors", 0};
  23. PyObject *encoding = NULL;
  24. PyObject *errors = NULL;
  25. PyObject *s = NULL;
  26. PyObject * ret = NULL;
  27. if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOO:str",
  28. kwlist, &x, &encoding, &errors)) {
  29. return NULL;
  30. }
  31. if (x != NULL && Py_TYPE(x) == &istr_type) {
  32. Py_INCREF(x);
  33. return x;
  34. }
  35. ret = PyUnicode_Type.tp_new(type, args, kwds);
  36. if (!ret) {
  37. goto fail;
  38. }
  39. s =_PyObject_CallMethodId(ret, &PyId_lower, NULL);
  40. if (!s) {
  41. goto fail;
  42. }
  43. ((istrobject*)ret)->canonical = s;
  44. s = NULL; /* the reference is stollen by .canonical */
  45. return ret;
  46. fail:
  47. Py_XDECREF(ret);
  48. return NULL;
  49. }
  50. static PyTypeObject istr_type = {
  51. PyVarObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type), 0)
  52. "multidict._multidict.istr",
  53. sizeof(istrobject),
  54. .tp_dealloc = (destructor)istr_dealloc,
  55. .tp_flags = Py_TPFLAGS_DEFAULT
  56. | Py_TPFLAGS_BASETYPE
  57. | Py_TPFLAGS_UNICODE_SUBCLASS,
  58. .tp_doc = istr__doc__,
  59. .tp_base = DEFERRED_ADDRESS(&PyUnicode_Type),
  60. .tp_new = (newfunc)istr_new,
  61. };
  62. static inline int
  63. istr_init(void)
  64. {
  65. istr_type.tp_base = &PyUnicode_Type;
  66. if (PyType_Ready(&istr_type) < 0) {
  67. return -1;
  68. }
  69. return 0;
  70. }
  71. #ifdef __cplusplus
  72. }
  73. #endif
  74. #endif