[Python-3000-checkins] r56652 - python/branches/py3k-struni/Objects/floatobject.c python/branches/py3k-struni/Objects/longobject.c

guido.van.rossum python-3000-checkins at python.org
Wed Aug 1 20:08:09 CEST 2007


Author: guido.van.rossum
Date: Wed Aug  1 20:08:08 2007
New Revision: 56652

Modified:
   python/branches/py3k-struni/Objects/floatobject.c
   python/branches/py3k-struni/Objects/longobject.c
Log:
Changes to long and float by Jeffrey Jasskin to conform to PEP 3141.
In particular, add trivial implementations of .real, .imag and .conjugate()
to both, and add .numerator and .denominator to long.
Also some small optimizations (e.g. remove long_pos in favor of long_long).


Modified: python/branches/py3k-struni/Objects/floatobject.c
==============================================================================
--- python/branches/py3k-struni/Objects/floatobject.c	(original)
+++ python/branches/py3k-struni/Objects/floatobject.c	Wed Aug  1 20:08:08 2007
@@ -742,17 +742,6 @@
 }
 
 static PyObject *
-float_pos(PyFloatObject *v)
-{
-	if (PyFloat_CheckExact(v)) {
-		Py_INCREF(v);
-		return (PyObject *)v;
-	}
-	else
-		return PyFloat_FromDouble(v->ob_fval);
-}
-
-static PyObject *
 float_abs(PyFloatObject *v)
 {
 	return PyFloat_FromDouble(fabs(v->ob_fval));
@@ -989,7 +978,15 @@
 "Overrides the automatic determination of C-level floating point type.\n"
 "This affects how floats are converted to and from binary strings.");
 
+static PyObject *
+float_getzero(PyObject *v, void *closure)
+{
+	return PyFloat_FromDouble(0.0);
+}
+
 static PyMethodDef float_methods[] = {
+  	{"conjugate",	(PyCFunction)float_float,	METH_NOARGS,
+	 "Returns self, the complex conjugate of any float."},
 	{"__getnewargs__",	(PyCFunction)float_getnewargs,	METH_NOARGS},
 	{"__getformat__",	(PyCFunction)float_getformat,	
 	 METH_O|METH_CLASS,		float_getformat_doc},
@@ -998,6 +995,18 @@
 	{NULL,		NULL}		/* sentinel */
 };
 
+static PyGetSetDef float_getset[] = {
+    {"real", 
+     (getter)float_float, (setter)NULL,
+     "the real part of a complex number",
+     NULL},
+    {"imag", 
+     (getter)float_getzero, (setter)NULL,
+     "the imaginary part of a complex number",
+     NULL},
+    {NULL}  /* Sentinel */
+};
+
 PyDoc_STRVAR(float_doc,
 "float(x) -> floating point number\n\
 \n\
@@ -1012,7 +1021,7 @@
 	float_divmod, 	/*nb_divmod*/
 	float_pow, 	/*nb_power*/
 	(unaryfunc)float_neg, /*nb_negative*/
-	(unaryfunc)float_pos, /*nb_positive*/
+	(unaryfunc)float_float, /*nb_positive*/
 	(unaryfunc)float_abs, /*nb_absolute*/
 	(inquiry)float_bool, /*nb_bool*/
 	0,		/*nb_invert*/
@@ -1073,7 +1082,7 @@
 	0,					/* tp_iternext */
 	float_methods,				/* tp_methods */
 	0,					/* tp_members */
-	0,					/* tp_getset */
+	float_getset,				/* tp_getset */
 	0,					/* tp_base */
 	0,					/* tp_dict */
 	0,					/* tp_descr_get */

Modified: python/branches/py3k-struni/Objects/longobject.c
==============================================================================
--- python/branches/py3k-struni/Objects/longobject.c	(original)
+++ python/branches/py3k-struni/Objects/longobject.c	Wed Aug  1 20:08:08 2007
@@ -1985,7 +1985,7 @@
 /* forward */
 static PyLongObject *x_divrem
 	(PyLongObject *, PyLongObject *, PyLongObject **);
-static PyObject *long_pos(PyLongObject *);
+static PyObject *long_long(PyObject *v);
 static int long_divrem(PyLongObject *, PyLongObject *,
 	PyLongObject **, PyLongObject **);
 
@@ -3181,17 +3181,6 @@
 }
 
 static PyObject *
-long_pos(PyLongObject *v)
-{
-	if (PyLong_CheckExact(v)) {
-		Py_INCREF(v);
-		return (PyObject *)v;
-	}
-	else
-		return _PyLong_Copy(v);
-}
-
-static PyObject *
 long_neg(PyLongObject *v)
 {
 	PyLongObject *z;
@@ -3209,7 +3198,7 @@
 	if (Py_Size(v) < 0)
 		return long_neg(v);
 	else
-		return long_pos(v);
+		return long_long((PyObject *)v);
 }
 
 static int
@@ -3496,12 +3485,6 @@
 }
 
 static PyObject *
-long_int(PyObject *v)
-{
-	return long_long(v);
-}
-
-static PyObject *
 long_float(PyObject *v)
 {
 	double result;
@@ -3607,11 +3590,38 @@
 	return Py_BuildValue("(N)", _PyLong_Copy(v));
 }
 
+static PyObject *
+long_getN(PyLongObject *v, void *context) {
+	return PyLong_FromLong((intptr_t)context);
+}
+
 static PyMethodDef long_methods[] = {
+	{"conjugate",	(PyCFunction)long_long,	METH_NOARGS,
+	 "Returns self, the complex conjugate of any int."},
 	{"__getnewargs__",	(PyCFunction)long_getnewargs,	METH_NOARGS},
 	{NULL,		NULL}		/* sentinel */
 };
 
+static PyGetSetDef long_getset[] = {
+    {"real", 
+     (getter)long_long, (setter)NULL,
+     "the real part of a complex number",
+     NULL},
+    {"imag", 
+     (getter)long_getN, (setter)NULL,
+     "the imaginary part of a complex number",
+     (void*)0},
+    {"numerator", 
+     (getter)long_long, (setter)NULL,
+     "the numerator of a rational number in lowest terms",
+     NULL},
+    {"denominator", 
+     (getter)long_getN, (setter)NULL,
+     "the denominator of a rational number in lowest terms",
+     (void*)1},
+    {NULL}  /* Sentinel */
+};
+
 PyDoc_STRVAR(long_doc,
 "int(x[, base]) -> integer\n\
 \n\
@@ -3629,7 +3639,7 @@
 			long_divmod,	/*nb_divmod*/
 			long_pow,	/*nb_power*/
 	(unaryfunc) 	long_neg,	/*nb_negative*/
-	(unaryfunc) 	long_pos,	/*tp_positive*/
+	(unaryfunc) 	long_long,	/*tp_positive*/
 	(unaryfunc) 	long_abs,	/*tp_absolute*/
 	(inquiry)	long_bool,	/*tp_bool*/
 	(unaryfunc)	long_invert,	/*nb_invert*/
@@ -3639,7 +3649,7 @@
 			long_xor,	/*nb_xor*/
 			long_or,	/*nb_or*/
 			0,		/*nb_coerce*/
-			long_int,	/*nb_int*/
+			long_long,	/*nb_int*/
 			long_long,	/*nb_long*/
 			long_float,	/*nb_float*/
 			0,		/*nb_oct*/ /* not used */
@@ -3694,7 +3704,7 @@
 	0,					/* tp_iternext */
 	long_methods,				/* tp_methods */
 	0,					/* tp_members */
-	0,					/* tp_getset */
+	long_getset,				/* tp_getset */
 	0,					/* tp_base */
 	0,					/* tp_dict */
 	0,					/* tp_descr_get */


More information about the Python-3000-checkins mailing list