Commit e98abcb2 authored by Marius Wachtler's avatar Marius Wachtler

Merge pull request #1040 from Daetalus/complex_improvements_2

Add missing complex attributes
parents 2526c94e fb612f83
......@@ -39,14 +39,10 @@ PyComplexObject represents a complex number with double-precision
real and imaginary parts.
*/
// Pyston change: this is not our object format
#if 0
typedef struct {
PyObject_HEAD
Py_complex cval;
} PyComplexObject;
#endif
typedef struct _PyComplexObject PyComplexObject;
// Pyston change: this is not a static object any more
// PyAPI_DATA(PyTypeObject) PyComplex_Type;
......
......@@ -215,8 +215,6 @@ c_abs(Py_complex z)
return result;
}
// Pyston changes: comment this out
#if 0
static PyObject *
complex_subtype_from_c_complex(PyTypeObject *type, Py_complex cval)
{
......@@ -356,14 +354,18 @@ PyComplex_AsCComplex(PyObject *op)
}
}
// Pyston changes: comment this out
#if 0
static void
complex_dealloc(PyObject *op)
{
op->ob_type->tp_free(op);
}
#endif
static PyObject *
// Pyston change: make no static
PyObject *
complex_format(PyComplexObject *v, int precision, char format_code)
{
PyObject *result = NULL;
......@@ -426,6 +428,8 @@ complex_format(PyComplexObject *v, int precision, char format_code)
return result;
}
// Pyston changes: comment this out
#if 0
static int
complex_print(PyComplexObject *v, FILE *fp, int flags)
{
......@@ -788,8 +792,10 @@ complex_coerce(PyObject **pv, PyObject **pw)
}
return 1; /* Can't do it */
}
#endif
static PyObject *
// Pyston change: make no static
PyObject *
complex_richcompare(PyObject *v, PyObject *w, int op)
{
PyObject *res;
......@@ -858,6 +864,8 @@ complex_richcompare(PyObject *v, PyObject *w, int op)
return Py_NotImplemented;
}
// Pyston change: comment this out
#if 0
static PyObject *
complex_int(PyObject *v)
{
......@@ -907,8 +915,10 @@ PyDoc_STRVAR(complex__format__doc,
"complex.__format__() -> str\n"
"\n"
"Convert to a string according to format_spec.");
#endif
static PyObject *
// Pyston changes: make no static
PyObject *
complex__format__(PyObject* self, PyObject* args)
{
PyObject *format_spec;
......@@ -952,7 +962,6 @@ PyDoc_STRVAR(complex_is_finite_doc,
"complex.is_finite() -> bool\n"
"\n"
"Returns True if the real and the imaginary part is finite.");
#endif
static PyMethodDef complex_methods[] = {
{"conjugate", (PyCFunction)complex_conjugate, METH_NOARGS,
......@@ -974,6 +983,7 @@ static PyMemberDef complex_members[] = {
"the imaginary part of a complex number"},
{0},
};
#endif
static PyObject *
complex_subtype_from_string(PyTypeObject *type, PyObject *v)
......@@ -1136,22 +1146,26 @@ complex_subtype_from_string(PyTypeObject *type, PyObject *v)
return NULL;
}
static PyObject *
complex_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
PyObject *r, *i, *tmp;
// Pyston changes: change the API, make it no static
// and let complex_new accept unpacked arguments
PyObject *
complex_new(PyTypeObject *type, PyObject *r, PyObject *i) {
PyObject *tmp;
PyNumberMethods *nbr, *nbi = NULL;
Py_complex cr, ci;
int own_r = 0;
int cr_is_complex = 0;
int ci_is_complex = 0;
static char *kwlist[] = {"real", "imag", 0};
r = Py_False;
i = NULL;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OO:complex", kwlist,
&r, &i))
return NULL;
/* Pyston changes: comment this out, pyston don't use tuple arg.
static char *kwlist[] = {"real", "imag", 0};
r = Py_False;
i = NULL;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OO:complex", kwlist,
&r, &i))
return NULL;
*/
/* Special-case for a single argument when type(arg) is complex. */
if (PyComplex_CheckExact(r) && i == NULL &&
......@@ -1269,6 +1283,8 @@ complex_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
return complex_subtype_from_doubles(type, cr.real, ci.real);
}
// Pyston changes: comment this out
#if 0
PyDoc_STRVAR(complex_doc,
"complex(real[, imag]) -> complex number\n"
"\n"
......
This diff is collapsed.
......@@ -390,6 +390,10 @@ public:
DEFAULT_CLASS_SIMPLE(complex_cls);
};
static_assert(sizeof(BoxedComplex) == sizeof(PyComplexObject), "");
static_assert(offsetof(BoxedComplex, real) == offsetof(PyComplexObject, cval.real), "");
static_assert(offsetof(BoxedComplex, imag) == offsetof(PyComplexObject, cval.imag), "");
class BoxedBool : public BoxedInt {
public:
BoxedBool(bool b) __attribute__((visibility("default"))) : BoxedInt(b ? 1 : 0) {}
......
......@@ -22,3 +22,49 @@ try:
complex(1, 1.0).real = 1
except TypeError, e:
print e
class C(complex):
pass
print(1j - C(1))
print(1j + C(1))
print(1j * C(1))
print(1j / C(1))
class C1(complex):
def __complex__(self):
return 1j
print(C1(1) + 1j)
print(C1(1) - 1j)
print(C1(1) * 1j)
print(C1(1) / 1j)
types = [int, float, long]
for _type in types:
try:
_type(1j)
except TypeError as e:
print(e.message)
data = ["-1j", "0j", "1j",
"5+5j", "5-5j",
"5", "5L", "5.5",
"\"5\"", "None",
]
operations = ["__mod__", "__rmod__",
"__divmod__", "__rdivmod__",
"__truediv__", "__rtruediv__",
"__floordiv__", "__rfloordiv__",
]
for x in data:
for y in data:
for operation in operations:
try:
print(eval("complex.{op}({arg1}, {arg2})".format(op=operation,
arg1=x,
arg2=y)))
except Exception as e:
print(e.message)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment