cpython: 5e8b94397f81 (original) (raw)
--- a/Misc/NEWS +++ b/Misc/NEWS @@ -16,8 +16,6 @@ Core and Builtins
- Issue #22604: Fix assertion error in debug mode when dividing a complex number by (nan+0j). -- Issue #20152: Convert the array module to Argument Clinic. -
- Issue #21052: Do not raise ImportWarning when sys.path_hooks or sys.meta_path are set to None. @@ -177,6 +175,8 @@ Core and Builtins Library ------- +- Issue #20152: Convert the array and cmath modules to Argument Clinic. +
- Issue #18643: Add socket.socketpair() on Windows.
- Issue #22435: Fix a file descriptor leak when SocketServer bind fails.
new file mode 100644 --- /dev/null +++ b/Modules/clinic/cmathmodule.c.h @@ -0,0 +1,851 @@ +/[clinic input] +preserve +[clinic start generated code]/ + +PyDoc_STRVAR(cmath_acos__doc__, +"acos($module, z, /)\n" +"--\n" +"\n" +"Return the arc cosine of z."); + +#define CMATH_ACOS_METHODDEF [](#l2.15)
+ +static Py_complex +cmath_acos_impl(PyModuleDef *module, Py_complex z); + +static PyObject * +cmath_acos(PyModuleDef *module, PyObject *args) +{
- if (!PyArg_ParseTuple(args,
"D:acos",[](#l2.29)
&z))[](#l2.30)
goto exit;[](#l2.31)
- /* modifications for z */
- errno = 0; PyFPE_START_PROTECT("complex function", goto exit);
- _return_value = cmath_acos_impl(module, z);
- PyFPE_END_PROTECT(_return_value);
- if (errno == EDOM) {
PyErr_SetString(PyExc_ValueError, "math domain error");[](#l2.37)
goto exit;[](#l2.38)
- }
- else if (errno == ERANGE) {
PyErr_SetString(PyExc_OverflowError, "math range error");[](#l2.41)
goto exit;[](#l2.42)
- }
- else {
return_value = PyComplex_FromCComplex(_return_value);[](#l2.45)
- }
+} + +PyDoc_STRVAR(cmath_acosh__doc__, +"acosh($module, z, /)\n" +"--\n" +"\n" +"Return the hyperbolic arccosine of z."); + +#define CMATH_ACOSH_METHODDEF [](#l2.58)
+ +static Py_complex +cmath_acosh_impl(PyModuleDef *module, Py_complex z); + +static PyObject * +cmath_acosh(PyModuleDef *module, PyObject *args) +{
- if (!PyArg_ParseTuple(args,
"D:acosh",[](#l2.72)
&z))[](#l2.73)
goto exit;[](#l2.74)
- /* modifications for z */
- errno = 0; PyFPE_START_PROTECT("complex function", goto exit);
- _return_value = cmath_acosh_impl(module, z);
- PyFPE_END_PROTECT(_return_value);
- if (errno == EDOM) {
PyErr_SetString(PyExc_ValueError, "math domain error");[](#l2.80)
goto exit;[](#l2.81)
- }
- else if (errno == ERANGE) {
PyErr_SetString(PyExc_OverflowError, "math range error");[](#l2.84)
goto exit;[](#l2.85)
- }
- else {
return_value = PyComplex_FromCComplex(_return_value);[](#l2.88)
- }
+} + +PyDoc_STRVAR(cmath_asin__doc__, +"asin($module, z, /)\n" +"--\n" +"\n" +"Return the arc sine of z."); + +#define CMATH_ASIN_METHODDEF [](#l2.101)
+ +static Py_complex +cmath_asin_impl(PyModuleDef *module, Py_complex z); + +static PyObject * +cmath_asin(PyModuleDef *module, PyObject *args) +{
- if (!PyArg_ParseTuple(args,
"D:asin",[](#l2.115)
&z))[](#l2.116)
goto exit;[](#l2.117)
- /* modifications for z */
- errno = 0; PyFPE_START_PROTECT("complex function", goto exit);
- _return_value = cmath_asin_impl(module, z);
- PyFPE_END_PROTECT(_return_value);
- if (errno == EDOM) {
PyErr_SetString(PyExc_ValueError, "math domain error");[](#l2.123)
goto exit;[](#l2.124)
- }
- else if (errno == ERANGE) {
PyErr_SetString(PyExc_OverflowError, "math range error");[](#l2.127)
goto exit;[](#l2.128)
- }
- else {
return_value = PyComplex_FromCComplex(_return_value);[](#l2.131)
- }
+} + +PyDoc_STRVAR(cmath_asinh__doc__, +"asinh($module, z, /)\n" +"--\n" +"\n" +"Return the hyperbolic arc sine of z."); + +#define CMATH_ASINH_METHODDEF [](#l2.144)
+ +static Py_complex +cmath_asinh_impl(PyModuleDef *module, Py_complex z); + +static PyObject * +cmath_asinh(PyModuleDef *module, PyObject *args) +{
- if (!PyArg_ParseTuple(args,
"D:asinh",[](#l2.158)
&z))[](#l2.159)
goto exit;[](#l2.160)
- /* modifications for z */
- errno = 0; PyFPE_START_PROTECT("complex function", goto exit);
- _return_value = cmath_asinh_impl(module, z);
- PyFPE_END_PROTECT(_return_value);
- if (errno == EDOM) {
PyErr_SetString(PyExc_ValueError, "math domain error");[](#l2.166)
goto exit;[](#l2.167)
- }
- else if (errno == ERANGE) {
PyErr_SetString(PyExc_OverflowError, "math range error");[](#l2.170)
goto exit;[](#l2.171)
- }
- else {
return_value = PyComplex_FromCComplex(_return_value);[](#l2.174)
- }
+} + +PyDoc_STRVAR(cmath_atan__doc__, +"atan($module, z, /)\n" +"--\n" +"\n" +"Return the arc tangent of z."); + +#define CMATH_ATAN_METHODDEF [](#l2.187)
+ +static Py_complex +cmath_atan_impl(PyModuleDef *module, Py_complex z); + +static PyObject * +cmath_atan(PyModuleDef *module, PyObject *args) +{
- if (!PyArg_ParseTuple(args,
"D:atan",[](#l2.201)
&z))[](#l2.202)
goto exit;[](#l2.203)
- /* modifications for z */
- errno = 0; PyFPE_START_PROTECT("complex function", goto exit);
- _return_value = cmath_atan_impl(module, z);
- PyFPE_END_PROTECT(_return_value);
- if (errno == EDOM) {
PyErr_SetString(PyExc_ValueError, "math domain error");[](#l2.209)
goto exit;[](#l2.210)
- }
- else if (errno == ERANGE) {
PyErr_SetString(PyExc_OverflowError, "math range error");[](#l2.213)
goto exit;[](#l2.214)
- }
- else {
return_value = PyComplex_FromCComplex(_return_value);[](#l2.217)
- }
+} + +PyDoc_STRVAR(cmath_atanh__doc__, +"atanh($module, z, /)\n" +"--\n" +"\n" +"Return the hyperbolic arc tangent of z."); + +#define CMATH_ATANH_METHODDEF [](#l2.230)
+ +static Py_complex +cmath_atanh_impl(PyModuleDef *module, Py_complex z); + +static PyObject * +cmath_atanh(PyModuleDef *module, PyObject *args) +{
- if (!PyArg_ParseTuple(args,
"D:atanh",[](#l2.244)
&z))[](#l2.245)
goto exit;[](#l2.246)
- /* modifications for z */
- errno = 0; PyFPE_START_PROTECT("complex function", goto exit);
- _return_value = cmath_atanh_impl(module, z);
- PyFPE_END_PROTECT(_return_value);
- if (errno == EDOM) {
PyErr_SetString(PyExc_ValueError, "math domain error");[](#l2.252)
goto exit;[](#l2.253)
- }
- else if (errno == ERANGE) {
PyErr_SetString(PyExc_OverflowError, "math range error");[](#l2.256)
goto exit;[](#l2.257)
- }
- else {
return_value = PyComplex_FromCComplex(_return_value);[](#l2.260)
- }
+} + +PyDoc_STRVAR(cmath_cos__doc__, +"cos($module, z, /)\n" +"--\n" +"\n" +"Return the cosine of z."); + +#define CMATH_COS_METHODDEF [](#l2.273)
+ +static Py_complex +cmath_cos_impl(PyModuleDef *module, Py_complex z); + +static PyObject * +cmath_cos(PyModuleDef *module, PyObject *args) +{
- if (!PyArg_ParseTuple(args,
"D:cos",[](#l2.287)
&z))[](#l2.288)
goto exit;[](#l2.289)
- /* modifications for z */
- errno = 0; PyFPE_START_PROTECT("complex function", goto exit);
- _return_value = cmath_cos_impl(module, z);
- PyFPE_END_PROTECT(_return_value);
- if (errno == EDOM) {
PyErr_SetString(PyExc_ValueError, "math domain error");[](#l2.295)
goto exit;[](#l2.296)
- }
- else if (errno == ERANGE) {
PyErr_SetString(PyExc_OverflowError, "math range error");[](#l2.299)
goto exit;[](#l2.300)
- }
- else {
return_value = PyComplex_FromCComplex(_return_value);[](#l2.303)
- }
+} + +PyDoc_STRVAR(cmath_cosh__doc__, +"cosh($module, z, /)\n" +"--\n" +"\n" +"Return the hyperbolic cosine of z."); + +#define CMATH_COSH_METHODDEF [](#l2.316)
+ +static Py_complex +cmath_cosh_impl(PyModuleDef *module, Py_complex z); + +static PyObject * +cmath_cosh(PyModuleDef *module, PyObject *args) +{
- if (!PyArg_ParseTuple(args,
"D:cosh",[](#l2.330)
&z))[](#l2.331)
goto exit;[](#l2.332)
- /* modifications for z */
- errno = 0; PyFPE_START_PROTECT("complex function", goto exit);
- _return_value = cmath_cosh_impl(module, z);
- PyFPE_END_PROTECT(_return_value);
- if (errno == EDOM) {
PyErr_SetString(PyExc_ValueError, "math domain error");[](#l2.338)
goto exit;[](#l2.339)
- }
- else if (errno == ERANGE) {
PyErr_SetString(PyExc_OverflowError, "math range error");[](#l2.342)
goto exit;[](#l2.343)
- }
- else {
return_value = PyComplex_FromCComplex(_return_value);[](#l2.346)
- }
+} + +PyDoc_STRVAR(cmath_exp__doc__, +"exp($module, z, /)\n" +"--\n" +"\n" +"Return the exponential value e**z."); + +#define CMATH_EXP_METHODDEF [](#l2.359)
+ +static Py_complex +cmath_exp_impl(PyModuleDef *module, Py_complex z); + +static PyObject * +cmath_exp(PyModuleDef *module, PyObject *args) +{
- if (!PyArg_ParseTuple(args,
"D:exp",[](#l2.373)
&z))[](#l2.374)
goto exit;[](#l2.375)
- /* modifications for z */
- errno = 0; PyFPE_START_PROTECT("complex function", goto exit);
- _return_value = cmath_exp_impl(module, z);
- PyFPE_END_PROTECT(_return_value);
- if (errno == EDOM) {
PyErr_SetString(PyExc_ValueError, "math domain error");[](#l2.381)
goto exit;[](#l2.382)
- }
- else if (errno == ERANGE) {
PyErr_SetString(PyExc_OverflowError, "math range error");[](#l2.385)
goto exit;[](#l2.386)
- }
- else {
return_value = PyComplex_FromCComplex(_return_value);[](#l2.389)
- }
+} + +PyDoc_STRVAR(cmath_log10__doc__, +"log10($module, z, /)\n" +"--\n" +"\n" +"Return the base-10 logarithm of z."); + +#define CMATH_LOG10_METHODDEF [](#l2.402)
+ +static Py_complex +cmath_log10_impl(PyModuleDef *module, Py_complex z); + +static PyObject * +cmath_log10(PyModuleDef *module, PyObject *args) +{
- if (!PyArg_ParseTuple(args,
"D:log10",[](#l2.416)
&z))[](#l2.417)
goto exit;[](#l2.418)
- /* modifications for z */
- errno = 0; PyFPE_START_PROTECT("complex function", goto exit);
- _return_value = cmath_log10_impl(module, z);
- PyFPE_END_PROTECT(_return_value);
- if (errno == EDOM) {
PyErr_SetString(PyExc_ValueError, "math domain error");[](#l2.424)
goto exit;[](#l2.425)
- }
- else if (errno == ERANGE) {
PyErr_SetString(PyExc_OverflowError, "math range error");[](#l2.428)
goto exit;[](#l2.429)
- }
- else {
return_value = PyComplex_FromCComplex(_return_value);[](#l2.432)
- }
+} + +PyDoc_STRVAR(cmath_sin__doc__, +"sin($module, z, /)\n" +"--\n" +"\n" +"Return the sine of z."); + +#define CMATH_SIN_METHODDEF [](#l2.445)
+ +static Py_complex +cmath_sin_impl(PyModuleDef *module, Py_complex z); + +static PyObject * +cmath_sin(PyModuleDef *module, PyObject *args) +{
- if (!PyArg_ParseTuple(args,
"D:sin",[](#l2.459)
&z))[](#l2.460)
goto exit;[](#l2.461)
- /* modifications for z */
- errno = 0; PyFPE_START_PROTECT("complex function", goto exit);
- _return_value = cmath_sin_impl(module, z);
- PyFPE_END_PROTECT(_return_value);
- if (errno == EDOM) {
PyErr_SetString(PyExc_ValueError, "math domain error");[](#l2.467)
goto exit;[](#l2.468)
- }
- else if (errno == ERANGE) {
PyErr_SetString(PyExc_OverflowError, "math range error");[](#l2.471)
goto exit;[](#l2.472)
- }
- else {
return_value = PyComplex_FromCComplex(_return_value);[](#l2.475)
- }
+} + +PyDoc_STRVAR(cmath_sinh__doc__, +"sinh($module, z, /)\n" +"--\n" +"\n" +"Return the hyperbolic sine of z."); + +#define CMATH_SINH_METHODDEF [](#l2.488)
+ +static Py_complex +cmath_sinh_impl(PyModuleDef *module, Py_complex z); + +static PyObject * +cmath_sinh(PyModuleDef *module, PyObject *args) +{
- if (!PyArg_ParseTuple(args,
"D:sinh",[](#l2.502)
&z))[](#l2.503)
goto exit;[](#l2.504)
- /* modifications for z */
- errno = 0; PyFPE_START_PROTECT("complex function", goto exit);
- _return_value = cmath_sinh_impl(module, z);
- PyFPE_END_PROTECT(_return_value);
- if (errno == EDOM) {
PyErr_SetString(PyExc_ValueError, "math domain error");[](#l2.510)
goto exit;[](#l2.511)
- }
- else if (errno == ERANGE) {
PyErr_SetString(PyExc_OverflowError, "math range error");[](#l2.514)
goto exit;[](#l2.515)
- }
- else {
return_value = PyComplex_FromCComplex(_return_value);[](#l2.518)
- }
+} + +PyDoc_STRVAR(cmath_sqrt__doc__, +"sqrt($module, z, /)\n" +"--\n" +"\n" +"Return the square root of z."); + +#define CMATH_SQRT_METHODDEF [](#l2.531)
+ +static Py_complex +cmath_sqrt_impl(PyModuleDef *module, Py_complex z); + +static PyObject * +cmath_sqrt(PyModuleDef *module, PyObject *args) +{
- if (!PyArg_ParseTuple(args,
"D:sqrt",[](#l2.545)
&z))[](#l2.546)
goto exit;[](#l2.547)
- /* modifications for z */
- errno = 0; PyFPE_START_PROTECT("complex function", goto exit);
- _return_value = cmath_sqrt_impl(module, z);
- PyFPE_END_PROTECT(_return_value);
- if (errno == EDOM) {
PyErr_SetString(PyExc_ValueError, "math domain error");[](#l2.553)
goto exit;[](#l2.554)
- }
- else if (errno == ERANGE) {
PyErr_SetString(PyExc_OverflowError, "math range error");[](#l2.557)
goto exit;[](#l2.558)
- }
- else {
return_value = PyComplex_FromCComplex(_return_value);[](#l2.561)
- }
+} + +PyDoc_STRVAR(cmath_tan__doc__, +"tan($module, z, /)\n" +"--\n" +"\n" +"Return the tangent of z."); + +#define CMATH_TAN_METHODDEF [](#l2.574)
+ +static Py_complex +cmath_tan_impl(PyModuleDef *module, Py_complex z); + +static PyObject * +cmath_tan(PyModuleDef *module, PyObject *args) +{
- if (!PyArg_ParseTuple(args,
"D:tan",[](#l2.588)
&z))[](#l2.589)
goto exit;[](#l2.590)
- /* modifications for z */
- errno = 0; PyFPE_START_PROTECT("complex function", goto exit);
- _return_value = cmath_tan_impl(module, z);
- PyFPE_END_PROTECT(_return_value);
- if (errno == EDOM) {
PyErr_SetString(PyExc_ValueError, "math domain error");[](#l2.596)
goto exit;[](#l2.597)
- }
- else if (errno == ERANGE) {
PyErr_SetString(PyExc_OverflowError, "math range error");[](#l2.600)
goto exit;[](#l2.601)
- }
- else {
return_value = PyComplex_FromCComplex(_return_value);[](#l2.604)
- }
+} + +PyDoc_STRVAR(cmath_tanh__doc__, +"tanh($module, z, /)\n" +"--\n" +"\n" +"Return the hyperbolic tangent of z."); + +#define CMATH_TANH_METHODDEF [](#l2.617)
+ +static Py_complex +cmath_tanh_impl(PyModuleDef *module, Py_complex z); + +static PyObject * +cmath_tanh(PyModuleDef *module, PyObject *args) +{
- if (!PyArg_ParseTuple(args,
"D:tanh",[](#l2.631)
&z))[](#l2.632)
goto exit;[](#l2.633)
- /* modifications for z */
- errno = 0; PyFPE_START_PROTECT("complex function", goto exit);
- _return_value = cmath_tanh_impl(module, z);
- PyFPE_END_PROTECT(_return_value);
- if (errno == EDOM) {
PyErr_SetString(PyExc_ValueError, "math domain error");[](#l2.639)
goto exit;[](#l2.640)
- }
- else if (errno == ERANGE) {
PyErr_SetString(PyExc_OverflowError, "math range error");[](#l2.643)
goto exit;[](#l2.644)
- }
- else {
return_value = PyComplex_FromCComplex(_return_value);[](#l2.647)
- }
+} + +PyDoc_STRVAR(cmath_log__doc__, +"log($module, x, y_obj=None, /)\n" +"--\n" +"\n" +"The logarithm of z to the given base.\n" +"\n" +"If the base not specified, returns the natural logarithm (base e) of z."); + +#define CMATH_LOG_METHODDEF [](#l2.662)
+ +static PyObject * +cmath_log_impl(PyModuleDef *module, Py_complex x, PyObject *y_obj); + +static PyObject * +cmath_log(PyModuleDef *module, PyObject *args) +{
- if (!PyArg_ParseTuple(args,
"D|O:log",[](#l2.676)
&x, &y_obj))[](#l2.677)
goto exit;[](#l2.678)
- return_value = cmath_log_impl(module, x, y_obj);
+} + +PyDoc_STRVAR(cmath_phase__doc__, +"phase($module, z, /)\n" +"--\n" +"\n" +"Return argument, also known as the phase angle, of a complex."); + +#define CMATH_PHASE_METHODDEF [](#l2.691)
+ +static PyObject * +cmath_phase_impl(PyModuleDef *module, Py_complex z); + +static PyObject * +cmath_phase(PyModuleDef *module, PyObject *args) +{
- if (!PyArg_ParseTuple(args,
"D:phase",[](#l2.704)
&z))[](#l2.705)
goto exit;[](#l2.706)
- return_value = cmath_phase_impl(module, z);
+} + +PyDoc_STRVAR(cmath_polar__doc__, +"polar($module, z, /)\n" +"--\n" +"\n" +"Convert a complex from rectangular coordinates to polar coordinates.\n" +"\n" +"r is the distance from 0 and phi the phase angle."); + +#define CMATH_POLAR_METHODDEF [](#l2.721)
+ +static PyObject * +cmath_polar_impl(PyModuleDef *module, Py_complex z); + +static PyObject * +cmath_polar(PyModuleDef *module, PyObject *args) +{
- if (!PyArg_ParseTuple(args,
"D:polar",[](#l2.734)
&z))[](#l2.735)
goto exit;[](#l2.736)
- return_value = cmath_polar_impl(module, z);
+} + +PyDoc_STRVAR(cmath_rect__doc__, +"rect($module, r, phi, /)\n" +"--\n" +"\n" +"Convert from polar coordinates to rectangular coordinates."); + +#define CMATH_RECT_METHODDEF [](#l2.749)
+ +static PyObject * +cmath_rect_impl(PyModuleDef *module, double r, double phi); + +static PyObject * +cmath_rect(PyModuleDef *module, PyObject *args) +{
- if (!PyArg_ParseTuple(args,
"dd:rect",[](#l2.763)
&r, &phi))[](#l2.764)
goto exit;[](#l2.765)
- return_value = cmath_rect_impl(module, r, phi);
+} + +PyDoc_STRVAR(cmath_isfinite__doc__, +"isfinite($module, z, /)\n" +"--\n" +"\n" +"Return True if both the real and imaginary parts of z are finite, else False."); + +#define CMATH_ISFINITE_METHODDEF [](#l2.778)
+ +static PyObject * +cmath_isfinite_impl(PyModuleDef *module, Py_complex z); + +static PyObject * +cmath_isfinite(PyModuleDef *module, PyObject *args) +{
- if (!PyArg_ParseTuple(args,
"D:isfinite",[](#l2.791)
&z))[](#l2.792)
goto exit;[](#l2.793)
- return_value = cmath_isfinite_impl(module, z);
+} + +PyDoc_STRVAR(cmath_isnan__doc__, +"isnan($module, z, /)\n" +"--\n" +"\n" +"Checks if the real or imaginary part of z not a number (NaN)."); + +#define CMATH_ISNAN_METHODDEF [](#l2.806)
+ +static PyObject * +cmath_isnan_impl(PyModuleDef *module, Py_complex z); + +static PyObject * +cmath_isnan(PyModuleDef *module, PyObject *args) +{
- if (!PyArg_ParseTuple(args,
"D:isnan",[](#l2.819)
&z))[](#l2.820)
goto exit;[](#l2.821)
- return_value = cmath_isnan_impl(module, z);
+} + +PyDoc_STRVAR(cmath_isinf__doc__, +"isinf($module, z, /)\n" +"--\n" +"\n" +"Checks if the real or imaginary part of z is infinite."); + +#define CMATH_ISINF_METHODDEF [](#l2.834)
+ +static PyObject * +cmath_isinf_impl(PyModuleDef *module, Py_complex z); + +static PyObject * +cmath_isinf(PyModuleDef *module, PyObject *args) +{
- if (!PyArg_ParseTuple(args,
"D:isinf",[](#l2.847)
&z))[](#l2.848)
goto exit;[](#l2.849)
- return_value = cmath_isinf_impl(module, z);
+} +/[clinic end generated code: output=4407f898ae07c83d input=a9049054013a1b77]/
--- a/Modules/cmathmodule.c +++ b/Modules/cmathmodule.c @@ -8,6 +8,41 @@ float.h. We assume that FLT_RADIX is either 2 or 16. / #include <float.h> +#include "clinic/cmathmodule.c.h" +/[clinic input] +output preset file +module cmath +[clinic start generated code]/ +/[clinic end generated code: output=da39a3ee5e6b4b0d input=ef7e0fdd8a143c03]/ + +/[python input] +class Py_complex_protected_converter(Py_complex_converter):
+ + +class Py_complex_protected_return_converter(CReturnConverter):
- def render(self, function, data):
self.declare(data)[](#l3.24)
data.return_conversion.append("""[](#l3.25)
+PyFPE_END_PROTECT(_return_value); +if (errno == EDOM) {{
+}} +else if (errno == ERANGE) {{
+}} +""".strip()) +[python start generated code]/ +/[python end generated code: output=da39a3ee5e6b4b0d input=231019039a6fbb9a]/ + #if (FLT_RADIX != 2 && FLT_RADIX != 16) #error "Modules/cmathmodule.c expects FLT_RADIX to be 2 or 16" #endif @@ -48,12 +83,12 @@ #define CM_SCALE_DOWN (-(CM_SCALE_UP+1)/2) / forward declarations */ -static Py_complex c_asinh(Py_complex); -static Py_complex c_atanh(Py_complex); -static Py_complex c_cosh(Py_complex); -static Py_complex c_sinh(Py_complex); -static Py_complex c_sqrt(Py_complex); -static Py_complex c_tanh(Py_complex); +static Py_complex cmath_asinh_impl(PyModuleDef *, Py_complex); +static Py_complex cmath_atanh_impl(PyModuleDef *, Py_complex); +static Py_complex cmath_cosh_impl(PyModuleDef *, Py_complex); +static Py_complex cmath_sinh_impl(PyModuleDef *, Py_complex); +static Py_complex cmath_sqrt_impl(PyModuleDef *, Py_complex); +static Py_complex cmath_tanh_impl(PyModuleDef , Py_complex); static PyObject * math_error(void); / Code to deal with special values (infinities, NaNs, etc.). / @@ -123,8 +158,18 @@ special_type(double d) static Py_complex acos_special_values[7][7]; +/[clinic input] +cmath.acos -> Py_complex_protected +
+ +Return the arc cosine of z. +[clinic start generated code]/ + static Py_complex -c_acos(Py_complex z) +cmath_acos_impl(PyModuleDef module, Py_complex z) +/[clinic end generated code: output=7c1dd21ff818db6b input=bd6cbd78ae851927]/ { Py_complex s1, s2, r; @@ -145,10 +190,10 @@ c_acos(Py_complex z) } else { s1.real = 1.-z.real; s1.imag = -z.imag;
s1 = c_sqrt(s1);[](#l3.88)
s1 = cmath_sqrt_impl(module, s1);[](#l3.89) s2.real = 1.+z.real;[](#l3.90) s2.imag = z.imag;[](#l3.91)
s2 = c_sqrt(s2);[](#l3.92)
} @@ -156,16 +201,18 @@ c_acos(Py_complex z) return r; } -PyDoc_STRVAR(c_acos_doc, -"acos(x)\n" -"\n" -"Return the arc cosine of x."); - static Py_complex acosh_special_values[7][7]; +/[clinic input] +cmath.acosh = cmath.acos + +Return the hyperbolic arccosine of z. +[clinic start generated code]/ + static Py_complex -c_acosh(Py_complex z) +cmath_acosh_impl(PyModuleDef module, Py_complex z) +/[clinic end generated code: output=c23c776429def981 input=bc016412080bb3e9]*/ { Py_complex s1, s2, r; @@ -178,10 +225,10 @@ c_acosh(Py_complex z) } else { s1.real = z.real - 1.; s1.imag = z.imag;s2 = cmath_sqrt_impl(module, s2);[](#l3.93) r.real = 2.*atan2(s1.real, s2.real);[](#l3.94) r.imag = m_asinh(s2.real*s1.imag - s2.imag*s1.real);[](#l3.95)
s1 = c_sqrt(s1);[](#l3.126)
s1 = cmath_sqrt_impl(module, s1);[](#l3.127) s2.real = z.real + 1.;[](#l3.128) s2.imag = z.imag;[](#l3.129)
s2 = c_sqrt(s2);[](#l3.130)
} @@ -189,35 +236,38 @@ c_acosh(Py_complex z) return r; } -PyDoc_STRVAR(c_acosh_doc, -"acosh(x)\n" -"\n" -"Return the hyperbolic arccosine of x."); +/[clinic input] +cmath.asin = cmath.acos +Return the arc sine of z. +[clinic start generated code]/ static Py_complex -c_asin(Py_complex z) +cmath_asin_impl(PyModuleDef module, Py_complex z) +/[clinic end generated code: output=42d2346d46690826 input=be0bf0cfdd5239c5]/ { / asin(z) = -i asinh(iz) */ Py_complex s, r; s.real = -z.imag; s.imag = z.real;s2 = cmath_sqrt_impl(module, s2);[](#l3.131) r.real = m_asinh(s1.real*s2.real + s1.imag*s2.imag);[](#l3.132) r.imag = 2.*atan2(s1.imag, s2.real);[](#l3.133)
-PyDoc_STRVAR(c_asin_doc, -"asin(x)\n" -"\n" -"Return the arc sine of x."); - static Py_complex asinh_special_values[7][7]; +/[clinic input] +cmath.asinh = cmath.acos + +Return the hyperbolic arc sine of z. +[clinic start generated code]/ + static Py_complex -c_asinh(Py_complex z) +cmath_asinh_impl(PyModuleDef module, Py_complex z) +/[clinic end generated code: output=0c6664823c7b1b35 input=5a21fa0242928c9b]*/ { Py_complex s1, s2, r; @@ -235,10 +285,10 @@ c_asinh(Py_complex z) } else { s1.real = 1.+z.imag; s1.imag = -z.real;
s1 = c_sqrt(s1);[](#l3.190)
s1 = cmath_sqrt_impl(module, s1);[](#l3.191) s2.real = 1.-z.imag;[](#l3.192) s2.imag = z.real;[](#l3.193)
s2 = c_sqrt(s2);[](#l3.194)
} @@ -246,20 +296,22 @@ c_asinh(Py_complex z) return r; } -PyDoc_STRVAR(c_asinh_doc, -"asinh(x)\n" -"\n" -"Return the hyperbolic arc sine of x."); +/[clinic input] +cmath.atan = cmath.acos + +Return the arc tangent of z. +[clinic start generated code]/ static Py_complex -c_atan(Py_complex z) +cmath_atan_impl(PyModuleDef module, Py_complex z) +/[clinic end generated code: output=b7d44f02c6a5c3b5 input=3b21ff7d5eac632a]/ { / atan(z) = -i atanh(iz) */ Py_complex s, r; s.real = -z.imag; s.imag = z.real;s2 = cmath_sqrt_impl(module, s2);[](#l3.195) r.real = m_asinh(s1.real*s2.imag-s2.real*s1.imag);[](#l3.196) r.imag = atan2(z.imag, s1.real*s2.real-s1.imag*s2.imag);[](#l3.197)
- s = cmath_atanh_impl(module, s); r.real = s.imag; r.imag = -s.real; return r; @@ -295,16 +347,18 @@ c_atan2(Py_complex z) return atan2(z.imag, z.real); }
-PyDoc_STRVAR(c_atan_doc, -"atan(x)\n" -"\n" -"Return the arc tangent of x."); - static Py_complex atanh_special_values[7][7]; +/[clinic input] +cmath.atanh = cmath.acos + +Return the hyperbolic arc tangent of z. +[clinic start generated code]/ + static Py_complex -c_atanh(Py_complex z) +cmath_atanh_impl(PyModuleDef module, Py_complex z) +/[clinic end generated code: output=279e0b9fefc8da7c input=df19cdc9f9d431c9]/ { Py_complex r; double ay, h; @@ -313,7 +367,7 @@ c_atanh(Py_complex z) / Reduce to case where z.real >= 0., using atanh(z) = -atanh(-z). */ if (z.real < 0.) {
return _Py_c_neg(c_atanh(_Py_c_neg(z)));[](#l3.257)
} ay = fabs(z.imag); @@ -350,34 +404,38 @@ c_atanh(Py_complex z) return r; } -PyDoc_STRVAR(c_atanh_doc, -"atanh(x)\n" -"\n" -"Return the hyperbolic arc tangent of x."); +/[clinic input] +cmath.cos = cmath.acos + +Return the cosine of z. +[clinic start generated code]/ static Py_complex -c_cos(Py_complex z) +cmath_cos_impl(PyModuleDef module, Py_complex z) +/[clinic end generated code: output=9d1cdc1b5e761667 input=6022e39b77127ac7]/ { / cos(z) = cosh(iz) */ Py_complex r; r.real = -z.imag; r.imag = z.real;return _Py_c_neg(cmath_atanh_impl(module, _Py_c_neg(z)));[](#l3.258)
-PyDoc_STRVAR(c_cos_doc, -"cos(x)\n" -"\n" -"Return the cosine of x."); - /* cosh(infinity + iy) needs to be dealt with specially / static Py_complex cosh_special_values[7][7]; +/[clinic input] +cmath.cosh = cmath.acos + +Return the hyperbolic cosine of z. +[clinic start generated code]/ + static Py_complex -c_cosh(Py_complex z) +cmath_cosh_impl(PyModuleDef module, Py_complex z) +/[clinic end generated code: output=f3b5d3282b3024d3 input=d6b66339e9cc332b]/ { Py_complex r; double x_minus_one; @@ -426,18 +484,20 @@ c_cosh(Py_complex z) return r; } -PyDoc_STRVAR(c_cosh_doc, -"cosh(x)\n" -"\n" -"Return the hyperbolic cosine of x."); - / exp(infinity + iy) and exp(-infinity + iy) need special treatment for finite y / static Py_complex exp_special_values[7][7]; +/[clinic input] +cmath.exp = cmath.acos + +Return the exponential value e*z. +[clinic start generated code]/ + static Py_complex -c_exp(Py_complex z) +cmath_exp_impl(PyModuleDef module, Py_complex z) +/[clinic end generated code: output=6f8825eb2bcad9ba input=8b9e6cf8a92174c3]/ { Py_complex r; double l; @@ -486,12 +546,6 @@ c_exp(Py_complex z) return r; } -PyDoc_STRVAR(c_exp_doc, -"exp(x)\n" -"\n" -"Return the exponential value e**x."); - - static Py_complex log_special_values[7][7]; static Py_complex @@ -564,8 +618,15 @@ c_log(Py_complex z) } +/[clinic input] +cmath.log10 = cmath.acos + +Return the base-10 logarithm of z. +[clinic start generated code]/ + static Py_complex -c_log10(Py_complex z) +cmath_log10_impl(PyModuleDef module, Py_complex z) +/[clinic end generated code: output=c7c426ca0e782341 input=cff5644f73c1519c]/ { Py_complex r; int errno_save; @@ -578,36 +639,40 @@ c_log10(Py_complex z) return r; } -PyDoc_STRVAR(c_log10_doc, -"log10(x)\n" -"\n" -"Return the base-10 logarithm of x."); +/[clinic input] +cmath.sin = cmath.acos + +Return the sine of z. +[clinic start generated code]/ static Py_complex -c_sin(Py_complex z) +cmath_sin_impl(PyModuleDef module, Py_complex z) +/[clinic end generated code: output=e7f5e2b253825ac7 input=2d3519842a8b4b85]/ { / sin(z) = -i sin(iz) */ Py_complex s, r; s.real = -z.imag; s.imag = z.real;
-PyDoc_STRVAR(c_sin_doc, -"sin(x)\n" -"\n" -"Return the sine of x."); - /* sinh(infinity + iy) needs to be dealt with specially / static Py_complex sinh_special_values[7][7]; +/[clinic input] +cmath.sinh = cmath.acos + +Return the hyperbolic sine of z. +[clinic start generated code]/ + static Py_complex -c_sinh(Py_complex z) +cmath_sinh_impl(PyModuleDef module, Py_complex z) +/[clinic end generated code: output=d71fff8298043a95 input=d2d3fc8c1ddfd2dd]/ { Py_complex r; double x_minus_one; @@ -655,16 +720,18 @@ c_sinh(Py_complex z) return r; } -PyDoc_STRVAR(c_sinh_doc, -"sinh(x)\n" -"\n" -"Return the hyperbolic sine of x."); - static Py_complex sqrt_special_values[7][7]; +/[clinic input] +cmath.sqrt = cmath.acos + +Return the square root of z. +[clinic start generated code]/ + static Py_complex -c_sqrt(Py_complex z) +cmath_sqrt_impl(PyModuleDef module, Py_complex z) +/[clinic end generated code: output=b6bda283d0c5a7b4 input=7088b166fc9a58c7]/ { / Method: use symmetries to reduce to the case when x = z.real and y @@ -730,36 +797,40 @@ c_sqrt(Py_complex z) return r; } -PyDoc_STRVAR(c_sqrt_doc, -"sqrt(x)\n" -"\n" -"Return the square root of x."); +/[clinic input] +cmath.tan = cmath.acos + +Return the tangent of z. +[clinic start generated code]/ static Py_complex -c_tan(Py_complex z) +cmath_tan_impl(PyModuleDef module, Py_complex z) +/[clinic end generated code: output=df374bacf36d99b4 input=fc167e528767888e]/ { /* tan(z) = -i tanh(iz) */ Py_complex s, r; s.real = -z.imag; s.imag = z.real;
-PyDoc_STRVAR(c_tan_doc, -"tan(x)\n" -"\n" -"Return the tangent of x."); - /* tanh(infinity + iy) needs to be dealt with specially / static Py_complex tanh_special_values[7][7]; +/[clinic input] +cmath.tanh = cmath.acos + +Return the hyperbolic tangent of z. +[clinic start generated code]/ + static Py_complex -c_tanh(Py_complex z) +cmath_tanh_impl(PyModuleDef module, Py_complex z) +/[clinic end generated code: output=f578773d27a18e96 input=22f67f9dc6d29685]/ { / Formula: @@ -822,25 +893,33 @@ c_tanh(Py_complex z) return r; } -PyDoc_STRVAR(c_tanh_doc, -"tanh(x)\n" -"\n" -"Return the hyperbolic tangent of x."); + +/*[clinic input] +cmath.log
+ +The logarithm of z to the given base. + +If the base not specified, returns the natural logarithm (base e) of z. +[clinic start generated code]*/ static PyObject * -cmath_log(PyObject *self, PyObject *args) +cmath_log_impl(PyModuleDef module, Py_complex x, PyObject y_obj) +/[clinic end generated code: output=35e2a1e5229b5a46 input=ee0e823a7c6e68ea]/ {
- errno = 0; PyFPE_START_PROTECT("complex function", return 0) x = c_log(x);
- if (y_obj != NULL) {
y = PyComplex_AsCComplex(y_obj);[](#l3.538)
if (PyErr_Occurred()) {[](#l3.539)
return NULL;[](#l3.540)
} @@ -850,10 +929,6 @@ cmath_log(PyObject *self, PyObject args return PyComplex_FromCComplex(x); } -PyDoc_STRVAR(cmath_log_doc, -"log(x[, base]) -> the logarithm of x to the given base.\n[](#l3.550) -If the base not specified, returns the natural logarithm (base e) of x."); - / And now the glue to make them available from Python: */ @@ -869,57 +944,22 @@ math_error(void) return NULL; } -static PyObject * -math_1(PyObject *args, Py_complex (*func)(Py_complex)) -{}[](#l3.541) y = c_log(y);[](#l3.542) x = _Py_c_quot(x, y);[](#l3.543)
- Py_complex x,r ;
- if (!PyArg_ParseTuple(args, "D", &x))
return NULL;[](#l3.565)
- errno = 0;
- PyFPE_START_PROTECT("complex function", return 0);
- r = (*func)(x);
- PyFPE_END_PROTECT(r);
- if (errno == EDOM) {
PyErr_SetString(PyExc_ValueError, "math domain error");[](#l3.571)
return NULL;[](#l3.572)
- }
- else if (errno == ERANGE) {
PyErr_SetString(PyExc_OverflowError, "math range error");[](#l3.575)
return NULL;[](#l3.576)
- }
- else {
return PyComplex_FromCComplex(r);[](#l3.579)
- }
-} + +/*[clinic input] +cmath.phase -#define FUNC1(stubname, func) [](#l3.586)
- static PyObject * stubname(PyObject *self, PyObject *args) { [](#l3.587)
return math_1(args, func); \[](#l3.588)
- }
-FUNC1(cmath_acos, c_acos) -FUNC1(cmath_acosh, c_acosh) -FUNC1(cmath_asin, c_asin) -FUNC1(cmath_asinh, c_asinh) -FUNC1(cmath_atan, c_atan) -FUNC1(cmath_atanh, c_atanh) -FUNC1(cmath_cos, c_cos) -FUNC1(cmath_cosh, c_cosh) -FUNC1(cmath_exp, c_exp) -FUNC1(cmath_log10, c_log10) -FUNC1(cmath_sin, c_sin) -FUNC1(cmath_sinh, c_sinh) -FUNC1(cmath_sqrt, c_sqrt) -FUNC1(cmath_tan, c_tan) -FUNC1(cmath_tanh, c_tanh) +Return argument, also known as the phase angle, of a complex. +[clinic start generated code]*/ static PyObject * -cmath_phase(PyObject *self, PyObject args) +cmath_phase_impl(PyModuleDef module, Py_complex z) +/[clinic end generated code: output=e09eaf373cb624c3 input=5cf75228ba94b69d]/ {
+ errno = 0; PyFPE_START_PROTECT("arg function", return 0) phi = c_atan2(z); @@ -930,17 +970,23 @@ cmath_phase(PyObject *self, PyObject ar return PyFloat_FromDouble(phi); } -PyDoc_STRVAR(cmath_phase_doc, -"phase(z) -> float\n\n[](#l3.629) -Return argument, also known as the phase angle, of a complex."); +/[clinic input] +cmath.polar +
+ +Convert a complex from rectangular coordinates to polar coordinates. + +r is the distance from 0 and phi the phase angle. +[clinic start generated code]*/ static PyObject * -cmath_polar(PyObject *self, PyObject args) +cmath_polar_impl(PyModuleDef module, Py_complex z) +/[clinic end generated code: output=07d41b16c877875a input=26c353574fd1a861]/ {
+ PyFPE_START_PROTECT("polar function", return 0) phi = c_atan2(z); /* should not cause any exception / r = _Py_c_abs(z); / sets errno to ERANGE on overflow; otherwise 0 */ @@ -951,11 +997,6 @@ cmath_polar(PyObject self, PyObject ar return Py_BuildValue("dd", r, phi); } -PyDoc_STRVAR(cmath_polar_doc, -"polar(z) -> r: float, phi: float\n\n[](#l3.660) -Convert a complex from rectangular coordinates to polar coordinates. r is\n[](#l3.661) -the distance from 0 and phi the phase angle."); - / rect() isn't covered by the C99 standard, but it's not too hard to figure out 'spirit of C99' rules for special value handing: @@ -969,13 +1010,21 @@ the distance from 0 and phi the phase an static Py_complex rect_special_values[7][7]; +/[clinic input] +cmath.rect +
+ +Convert from polar coordinates to rectangular coordinates. +[clinic start generated code]*/ + static PyObject * -cmath_rect(PyObject *self, PyObject args) +cmath_rect_impl(PyModuleDef module, double r, double phi) +/[clinic end generated code: output=d97a8749bd63e9d5 input=24c5646d147efd69]/ { Py_complex z;
- double r, phi;
- if (!PyArg_ParseTuple(args, "dd:rect", &r, &phi))
errno = 0; PyFPE_START_PROTECT("rect function", return 0) @@ -1026,79 +1075,75 @@ cmath_rect(PyObject *self, PyObject *arg return PyComplex_FromCComplex(z);return NULL;[](#l3.689)
} -PyDoc_STRVAR(cmath_rect_doc, -"rect(r, phi) -> z: complex\n\n[](#l3.698) -Convert from polar coordinates to rectangular coordinates."); +/[clinic input] +cmath.isfinite = cmath.polar + +Return True if both the real and imaginary parts of z are finite, else False. +[clinic start generated code]/ static PyObject * -cmath_isfinite(PyObject *self, PyObject args) +cmath_isfinite_impl(PyModuleDef module, Py_complex z) +/[clinic end generated code: output=8f6682fa93de45d6 input=848e7ee701895815]/ {
- Py_complex z;
- if (!PyArg_ParseTuple(args, "D:isfinite", &z))
return PyBool_FromLong(Py_IS_FINITE(z.real) && Py_IS_FINITE(z.imag)); } -PyDoc_STRVAR(cmath_isfinite_doc, -"isfinite(z) -> bool\n[](#l3.718) -Return True if both the real and imaginary parts of z are finite, else False."); +/[clinic input] +cmath.isnan = cmath.polar + +Checks if the real or imaginary part of z not a number (NaN). +[clinic start generated code]/ static PyObject * -cmath_isnan(PyObject *self, PyObject args) +cmath_isnan_impl(PyModuleDef module, Py_complex z) +/[clinic end generated code: output=b85fe8c2047718ee input=71799f5d284c9baf]/ {return NULL;[](#l3.713)
- Py_complex z;
- if (!PyArg_ParseTuple(args, "D:isnan", &z))
return PyBool_FromLong(Py_IS_NAN(z.real) || Py_IS_NAN(z.imag)); } -PyDoc_STRVAR(cmath_isnan_doc, -"isnan(z) -> bool\n[](#l3.738) -Checks if the real or imaginary part of z not a number (NaN)"); +/[clinic input] +cmath.isinf = cmath.polar + +Checks if the real or imaginary part of z is infinite. +[clinic start generated code]/ static PyObject * -cmath_isinf(PyObject *self, PyObject args) +cmath_isinf_impl(PyModuleDef module, Py_complex z) +/[clinic end generated code: output=8ca9c6109e468bf4 input=363df155c7181329]/ {return NULL;[](#l3.733)
- Py_complex z;
- if (!PyArg_ParseTuple(args, "D:isinf", &z))
return PyBool_FromLong(Py_IS_INFINITY(z.real) || Py_IS_INFINITY(z.imag));return NULL;[](#l3.753)
} -PyDoc_STRVAR(cmath_isinf_doc, -"isinf(z) -> bool\n[](#l3.759) -Checks if the real or imaginary part of z is infinite."); - PyDoc_STRVAR(module_doc, "This module is always available. It provides access to mathematical\n" "functions for complex numbers."); static PyMethodDef cmath_methods[] = {
- {"acos", cmath_acos, METH_VARARGS, c_acos_doc},
- {"acosh", cmath_acosh, METH_VARARGS, c_acosh_doc},
- {"asin", cmath_asin, METH_VARARGS, c_asin_doc},
- {"asinh", cmath_asinh, METH_VARARGS, c_asinh_doc},
- {"atan", cmath_atan, METH_VARARGS, c_atan_doc},
- {"atanh", cmath_atanh, METH_VARARGS, c_atanh_doc},
- {"cos", cmath_cos, METH_VARARGS, c_cos_doc},
- {"cosh", cmath_cosh, METH_VARARGS, c_cosh_doc},
- {"exp", cmath_exp, METH_VARARGS, c_exp_doc},
- {"isfinite", cmath_isfinite, METH_VARARGS, cmath_isfinite_doc},
- {"isinf", cmath_isinf, METH_VARARGS, cmath_isinf_doc},
- {"isnan", cmath_isnan, METH_VARARGS, cmath_isnan_doc},
- {"log", cmath_log, METH_VARARGS, cmath_log_doc},
- {"log10", cmath_log10, METH_VARARGS, c_log10_doc},
- {"phase", cmath_phase, METH_VARARGS, cmath_phase_doc},
- {"polar", cmath_polar, METH_VARARGS, cmath_polar_doc},
- {"rect", cmath_rect, METH_VARARGS, cmath_rect_doc},
- {"sin", cmath_sin, METH_VARARGS, c_sin_doc},
- {"sinh", cmath_sinh, METH_VARARGS, c_sinh_doc},
- {"sqrt", cmath_sqrt, METH_VARARGS, c_sqrt_doc},
- {"tan", cmath_tan, METH_VARARGS, c_tan_doc},
- {"tanh", cmath_tanh, METH_VARARGS, c_tanh_doc},
- {NULL, NULL} /* sentinel */
- CMATH_ACOS_METHODDEF
- CMATH_ACOSH_METHODDEF
- CMATH_ASIN_METHODDEF
- CMATH_ASINH_METHODDEF
- CMATH_ATAN_METHODDEF
- CMATH_ATANH_METHODDEF
- CMATH_COS_METHODDEF
- CMATH_COSH_METHODDEF
- CMATH_EXP_METHODDEF
- CMATH_ISFINITE_METHODDEF
- CMATH_ISINF_METHODDEF
- CMATH_ISNAN_METHODDEF
- CMATH_LOG_METHODDEF
- CMATH_LOG10_METHODDEF
- CMATH_PHASE_METHODDEF
- CMATH_POLAR_METHODDEF
- CMATH_RECT_METHODDEF
- CMATH_SIN_METHODDEF
- CMATH_SINH_METHODDEF
- CMATH_SQRT_METHODDEF
- CMATH_TAN_METHODDEF
- CMATH_TANH_METHODDEF
- {NULL, NULL} /* sentinel */