complexobject.c.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*[clinic input]
  2. preserve
  3. [clinic start generated code]*/
  4. #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
  5. # include "pycore_gc.h" // PyGC_Head
  6. # include "pycore_runtime.h" // _Py_ID()
  7. #endif
  8. PyDoc_STRVAR(complex_conjugate__doc__,
  9. "conjugate($self, /)\n"
  10. "--\n"
  11. "\n"
  12. "Return the complex conjugate of its argument. (3-4j).conjugate() == 3+4j.");
  13. #define COMPLEX_CONJUGATE_METHODDEF \
  14. {"conjugate", (PyCFunction)complex_conjugate, METH_NOARGS, complex_conjugate__doc__},
  15. static PyObject *
  16. complex_conjugate_impl(PyComplexObject *self);
  17. static PyObject *
  18. complex_conjugate(PyComplexObject *self, PyObject *Py_UNUSED(ignored))
  19. {
  20. return complex_conjugate_impl(self);
  21. }
  22. PyDoc_STRVAR(complex___getnewargs____doc__,
  23. "__getnewargs__($self, /)\n"
  24. "--\n"
  25. "\n");
  26. #define COMPLEX___GETNEWARGS___METHODDEF \
  27. {"__getnewargs__", (PyCFunction)complex___getnewargs__, METH_NOARGS, complex___getnewargs____doc__},
  28. static PyObject *
  29. complex___getnewargs___impl(PyComplexObject *self);
  30. static PyObject *
  31. complex___getnewargs__(PyComplexObject *self, PyObject *Py_UNUSED(ignored))
  32. {
  33. return complex___getnewargs___impl(self);
  34. }
  35. PyDoc_STRVAR(complex___format____doc__,
  36. "__format__($self, format_spec, /)\n"
  37. "--\n"
  38. "\n"
  39. "Convert to a string according to format_spec.");
  40. #define COMPLEX___FORMAT___METHODDEF \
  41. {"__format__", (PyCFunction)complex___format__, METH_O, complex___format____doc__},
  42. static PyObject *
  43. complex___format___impl(PyComplexObject *self, PyObject *format_spec);
  44. static PyObject *
  45. complex___format__(PyComplexObject *self, PyObject *arg)
  46. {
  47. PyObject *return_value = NULL;
  48. PyObject *format_spec;
  49. if (!PyUnicode_Check(arg)) {
  50. _PyArg_BadArgument("__format__", "argument", "str", arg);
  51. goto exit;
  52. }
  53. if (PyUnicode_READY(arg) == -1) {
  54. goto exit;
  55. }
  56. format_spec = arg;
  57. return_value = complex___format___impl(self, format_spec);
  58. exit:
  59. return return_value;
  60. }
  61. PyDoc_STRVAR(complex___complex____doc__,
  62. "__complex__($self, /)\n"
  63. "--\n"
  64. "\n"
  65. "Convert this value to exact type complex.");
  66. #define COMPLEX___COMPLEX___METHODDEF \
  67. {"__complex__", (PyCFunction)complex___complex__, METH_NOARGS, complex___complex____doc__},
  68. static PyObject *
  69. complex___complex___impl(PyComplexObject *self);
  70. static PyObject *
  71. complex___complex__(PyComplexObject *self, PyObject *Py_UNUSED(ignored))
  72. {
  73. return complex___complex___impl(self);
  74. }
  75. PyDoc_STRVAR(complex_new__doc__,
  76. "complex(real=0, imag=0)\n"
  77. "--\n"
  78. "\n"
  79. "Create a complex number from a string or numbers.\n"
  80. "\n"
  81. "If a string is given, parse it as a complex number.\n"
  82. "If a single number is given, convert it to a complex number.\n"
  83. "If the \'real\' or \'imag\' arguments are given, create a complex number\n"
  84. "with the specified real and imaginary components.");
  85. static PyObject *
  86. complex_new_impl(PyTypeObject *type, PyObject *r, PyObject *i);
  87. static PyObject *
  88. complex_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
  89. {
  90. PyObject *return_value = NULL;
  91. #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
  92. #define NUM_KEYWORDS 2
  93. static struct {
  94. PyGC_Head _this_is_not_used;
  95. PyObject_VAR_HEAD
  96. PyObject *ob_item[NUM_KEYWORDS];
  97. } _kwtuple = {
  98. .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
  99. .ob_item = { &_Py_ID(real), &_Py_ID(imag), },
  100. };
  101. #undef NUM_KEYWORDS
  102. #define KWTUPLE (&_kwtuple.ob_base.ob_base)
  103. #else // !Py_BUILD_CORE
  104. # define KWTUPLE NULL
  105. #endif // !Py_BUILD_CORE
  106. static const char * const _keywords[] = {"real", "imag", NULL};
  107. static _PyArg_Parser _parser = {
  108. .keywords = _keywords,
  109. .fname = "complex",
  110. .kwtuple = KWTUPLE,
  111. };
  112. #undef KWTUPLE
  113. PyObject *argsbuf[2];
  114. PyObject * const *fastargs;
  115. Py_ssize_t nargs = PyTuple_GET_SIZE(args);
  116. Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 0;
  117. PyObject *r = NULL;
  118. PyObject *i = NULL;
  119. fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 0, 2, 0, argsbuf);
  120. if (!fastargs) {
  121. goto exit;
  122. }
  123. if (!noptargs) {
  124. goto skip_optional_pos;
  125. }
  126. if (fastargs[0]) {
  127. r = fastargs[0];
  128. if (!--noptargs) {
  129. goto skip_optional_pos;
  130. }
  131. }
  132. i = fastargs[1];
  133. skip_optional_pos:
  134. return_value = complex_new_impl(type, r, i);
  135. exit:
  136. return return_value;
  137. }
  138. /*[clinic end generated code: output=9558b8449db17dc1 input=a9049054013a1b77]*/