summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorChristian Heimes <christian@cheimes.de>2008-01-14 18:49:24 (GMT)
committerChristian Heimes <christian@cheimes.de>2008-01-14 18:49:24 (GMT)
commitd32ed6f5117a68037e9cb7bcfd8d0f8d1611f442 (patch)
tree9e2893323fe2a217827f43ee53e92f8de9b1cf99 /Objects
parent1acde190b2676ecfa45d754667df36d6b9c9cc7e (diff)
downloadcpython-d32ed6f5117a68037e9cb7bcfd8d0f8d1611f442.zip
cpython-d32ed6f5117a68037e9cb7bcfd8d0f8d1611f442.tar.gz
cpython-d32ed6f5117a68037e9cb7bcfd8d0f8d1611f442.tar.bz2
Merged revisions 59933-59951 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk ........ r59935 | raymond.hettinger | 2008-01-13 07:15:15 +0100 (Sun, 13 Jan 2008) | 1 line Named tuple is a concept, not a specific type. ........ r59936 | raymond.hettinger | 2008-01-13 07:18:07 +0100 (Sun, 13 Jan 2008) | 1 line Fix spelling. ........ r59937 | georg.brandl | 2008-01-13 10:36:18 +0100 (Sun, 13 Jan 2008) | 2 lines Clarify the effect of text mode. ........ r59938 | thomas.heller | 2008-01-13 12:19:43 +0100 (Sun, 13 Jan 2008) | 1 line Make Modules/socketobject.c compile for Windows again. ........ r59939 | ka-ping.yee | 2008-01-13 12:25:13 +0100 (Sun, 13 Jan 2008) | 9 lines Check in the patch proposed by Ben Hayden (benjhayden) for issue #1550: help('modules') broken by several 3rd party libraries. Tested with Python build: trunk:54235:59936M -- the reported error occurs with Django installed (or with any __init__.py present on the path that raises an exception), and such errors indeed go away when this change is applied. ........ r59940 | georg.brandl | 2008-01-13 16:04:05 +0100 (Sun, 13 Jan 2008) | 2 lines Back out r59931 - test_ctypes fails with it. ........ r59943 | amaury.forgeotdarc | 2008-01-14 01:22:44 +0100 (Mon, 14 Jan 2008) | 6 lines As discussed in issue 1700288: ctypes takes some liberties when creating python types: it modifies the types' __dict__ directly, bypassing all the machinery of type objects which deal with special methods. And this broke recent optimisations of method lookup. Now we try to modify the type with more "official" functions. ........ r59944 | amaury.forgeotdarc | 2008-01-14 01:29:41 +0100 (Mon, 14 Jan 2008) | 5 lines Re-apply patch #1700288 (first applied in r59931, rolled back in r59940) now that ctypes uses a more supported method to create types: Method cache optimization, by Armin Rigo, ported to 2.6 by Kevin Jacobs. ........ r59946 | amaury.forgeotdarc | 2008-01-14 02:07:27 +0100 (Mon, 14 Jan 2008) | 4 lines ?Why did my tests not notice this before? Slots inheritance is very different from OO inheritance. This code lead to infinite recursion on classes derived from StructType. ........ r59947 | christian.heimes | 2008-01-14 04:33:52 +0100 (Mon, 14 Jan 2008) | 1 line Added new an better structseq representation. E.g. repr(time.gmtime(0)) now returns 'time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=1, tm_isdst=0)' instead of '(1970, 1, 1, 0, 0, 0, 3, 1, 0)'. The feature is part of #1816: sys.flags ........ r59948 | christian.heimes | 2008-01-14 04:35:38 +0100 (Mon, 14 Jan 2008) | 1 line I missed the most important file ........ r59949 | christian.heimes | 2008-01-14 04:42:48 +0100 (Mon, 14 Jan 2008) | 1 line Applied patch #1816: sys.flags patch ........ r59950 | christian.heimes | 2008-01-14 05:13:37 +0100 (Mon, 14 Jan 2008) | 2 lines Now that I've learnt about structseq objects I felt like converting sys.float_info to a structseq. It's readonly and help(sys.float_info) explains the attributes nicely. ........ r59951 | christian.heimes | 2008-01-14 07:06:19 +0100 (Mon, 14 Jan 2008) | 1 line Added more comments to the new structseq repr code and implemented several of Neal's suggestions. ........
Diffstat (limited to 'Objects')
-rw-r--r--Objects/floatobject.c106
-rw-r--r--Objects/structseq.c82
2 files changed, 154 insertions, 34 deletions
diff --git a/Objects/floatobject.c b/Objects/floatobject.c
index 0989415..a832c5d 100644
--- a/Objects/floatobject.c
+++ b/Objects/floatobject.c
@@ -5,6 +5,7 @@
for any kind of float exception without losing portability. */
#include "Python.h"
+#include "structseq.h"
#include "formatter_unicode.h"
@@ -61,39 +62,86 @@ PyFloat_GetMin(void)
return DBL_MIN;
}
+static PyTypeObject FloatInfoType;
+
+PyDoc_STRVAR(floatinfo__doc__,
+"sys.floatinfo\n\
+\n\
+A structseq holding information about the float type. It contains low level\n\
+information about the precision and internal representation. Please study\n\
+your system's :file:`float.h` for more information.");
+
+static PyStructSequence_Field floatinfo_fields[] = {
+ {"max", "DBL_MAX -- maximum representable finite float"},
+ {"max_exp", "DBL_MAX_EXP -- maximum int e such that radix**(e-1) "
+ "is representable"},
+ {"max_10_exp", "DBL_MAX_10_EXP -- maximum int e such that 10**e "
+ "is representable"},
+ {"min", "DBL_MIN -- Minimum positive normalizer float"},
+ {"min_exp", "DBL_MIN_EXP -- minimum int e such that radix**(e-1) "
+ "is a normalized float"},
+ {"min_10_exp", "DBL_MIN_10_EXP -- minimum int e such that 10**e is "
+ "a normalized"},
+ {"dig", "DBL_DIG -- digits"},
+ {"mant_dig", "DBL_MANT_DIG -- mantissa digits"},
+ {"epsilon", "DBL_EPSILON -- Difference between 1 and the next "
+ "representable float"},
+ {"radix", "FLT_RADIX -- radix of exponent"},
+ {"rounds", "FLT_ROUNDS -- addition rounds"},
+ {0}
+};
+
+static PyStructSequence_Desc floatinfo_desc = {
+ "sys.floatinfo", /* name */
+ floatinfo__doc__, /* doc */
+ floatinfo_fields, /* fields */
+ 11
+};
+
PyObject *
PyFloat_GetInfo(void)
{
- PyObject *d, *tmp;
-
-#define SET_FLOAT_CONST(d, key, const) \
- tmp = PyFloat_FromDouble(const); \
- if (tmp == NULL) return NULL; \
- if (PyDict_SetItemString(d, key, tmp)) return NULL; \
- Py_DECREF(tmp)
-#define SET_INT_CONST(d, key, const) \
- tmp = PyLong_FromLong(const); \
- if (tmp == NULL) return NULL; \
- if (PyDict_SetItemString(d, key, tmp)) return NULL; \
- Py_DECREF(tmp)
-
- d = PyDict_New();
-
- SET_FLOAT_CONST(d, "max", DBL_MAX);
- SET_INT_CONST(d, "max_exp", DBL_MAX_EXP);
- SET_INT_CONST(d, "max_10_exp", DBL_MAX_10_EXP);
- SET_FLOAT_CONST(d, "min", DBL_MIN);
- SET_INT_CONST(d, "min_exp", DBL_MIN_EXP);
- SET_INT_CONST(d, "min_10_exp", DBL_MIN_10_EXP);
- SET_INT_CONST(d, "dig", DBL_DIG);
- SET_INT_CONST(d, "mant_dig", DBL_MANT_DIG);
- SET_FLOAT_CONST(d, "epsilon", DBL_EPSILON);
- SET_INT_CONST(d, "radix", FLT_RADIX);
- SET_INT_CONST(d, "rounds", FLT_ROUNDS);
-
- return d;
-}
+ static PyObject* floatinfo;
+ int pos = 0;
+ if (floatinfo != NULL) {
+ Py_INCREF(floatinfo);
+ return floatinfo;
+ }
+ PyStructSequence_InitType(&FloatInfoType, &floatinfo_desc);
+
+ floatinfo = PyStructSequence_New(&FloatInfoType);
+ if (floatinfo == NULL) {
+ return NULL;
+ }
+
+#define SetIntFlag(flag) \
+ PyStructSequence_SET_ITEM(floatinfo, pos++, PyLong_FromLong(flag))
+#define SetDblFlag(flag) \
+ PyStructSequence_SET_ITEM(floatinfo, pos++, PyFloat_FromDouble(flag))
+
+ SetDblFlag(DBL_MAX);
+ SetIntFlag(DBL_MAX_EXP);
+ SetIntFlag(DBL_MAX_10_EXP);
+ SetDblFlag(DBL_MIN);
+ SetIntFlag(DBL_MIN_EXP);
+ SetIntFlag(DBL_MIN_10_EXP);
+ SetIntFlag(DBL_DIG);
+ SetIntFlag(DBL_MANT_DIG);
+ SetDblFlag(DBL_EPSILON);
+ SetIntFlag(FLT_RADIX);
+ SetIntFlag(FLT_ROUNDS);
+#undef SetIntFlag
+#undef SetDblFlag
+
+ if (PyErr_Occurred()) {
+ Py_CLEAR(floatinfo);
+ return NULL;
+ }
+
+ Py_INCREF(floatinfo);
+ return floatinfo;
+}
PyObject *
PyFloat_FromDouble(double fval)
diff --git a/Objects/structseq.c b/Objects/structseq.c
index 96026fc..56e885c 100644
--- a/Objects/structseq.c
+++ b/Objects/structseq.c
@@ -30,7 +30,7 @@ PyObject *
PyStructSequence_New(PyTypeObject *type)
{
PyStructSequence *obj;
-
+
obj = PyObject_New(PyStructSequence, type);
Py_SIZE(obj) = VISIBLE_SIZE_TP(type);
@@ -230,11 +230,83 @@ make_tuple(PyStructSequence *obj)
static PyObject *
structseq_repr(PyStructSequence *obj)
{
- PyObject *tup, *str;
- tup = make_tuple(obj);
- str = PyObject_Repr(tup);
+ /* buffer and type size were chosen well considered. */
+#define REPR_BUFFER_SIZE 512
+#define TYPE_MAXSIZE 100
+
+ PyObject *tup;
+ PyTypeObject *typ = Py_TYPE(obj);
+ int i, removelast = 0;
+ Py_ssize_t len;
+ char buf[REPR_BUFFER_SIZE];
+ char *endofbuf, *pbuf = buf;
+
+ /* pointer to end of writeable buffer; safes space for "...)\0" */
+ endofbuf= &buf[REPR_BUFFER_SIZE-5];
+
+ if ((tup = make_tuple(obj)) == NULL) {
+ return NULL;
+ }
+
+ /* "typename(", limited to TYPE_MAXSIZE */
+ len = strlen(typ->tp_name) > TYPE_MAXSIZE ? TYPE_MAXSIZE :
+ strlen(typ->tp_name);
+ strncpy(pbuf, typ->tp_name, len);
+ pbuf += len;
+ *pbuf++ = '(';
+
+ for (i=0; i < VISIBLE_SIZE(obj); i++) {
+ PyObject *val, *repr;
+ char *cname, *crepr;
+
+ cname = typ->tp_members[i].name;
+
+ val = PyTuple_GetItem(tup, i);
+ if (cname == NULL || val == NULL) {
+ return NULL;
+ }
+ repr = PyObject_Repr(val);
+ if (repr == NULL) {
+ Py_DECREF(tup);
+ return NULL;
+ }
+ crepr = PyUnicode_AsString(repr);
+ if (crepr == NULL) {
+ Py_DECREF(tup);
+ Py_DECREF(repr);
+ return NULL;
+ }
+
+ /* + 3: keep space for "=" and ", " */
+ len = strlen(cname) + strlen(crepr) + 3;
+ if ((pbuf+len) <= endofbuf) {
+ strcpy(pbuf, cname);
+ pbuf += strlen(cname);
+ *pbuf++ = '=';
+ strcpy(pbuf, crepr);
+ pbuf += strlen(crepr);
+ *pbuf++ = ',';
+ *pbuf++ = ' ';
+ removelast = 1;
+ Py_DECREF(repr);
+ }
+ else {
+ strcpy(pbuf, "...");
+ pbuf += 3;
+ removelast = 0;
+ Py_DECREF(repr);
+ break;
+ }
+ }
Py_DECREF(tup);
- return str;
+ if (removelast) {
+ /* overwrite last ", " */
+ pbuf-=2;
+ }
+ *pbuf++ = ')';
+ *pbuf = '\0';
+
+ return PyUnicode_FromString(buf);
}
static PyObject *