Updated for hfsplus module, new gusi libs.
[python/dscho.git] / Objects / floatobject.c
blobea162dfde7b6ebd231ec07fd2babf037eb9dae82
2 /* Float object implementation */
4 /* XXX There should be overflow checks here, but it's hard to check
5 for any kind of float exception without losing portability. */
7 #include "Python.h"
9 #include <ctype.h>
11 #if !defined(__STDC__) && !defined(macintosh)
12 extern double fmod(double, double);
13 extern double pow(double, double);
14 #endif
16 #if defined(sun) && !defined(__SVR4)
17 /* On SunOS4.1 only libm.a exists. Make sure that references to all
18 needed math functions exist in the executable, so that dynamic
19 loading of mathmodule does not fail. */
20 double (*_Py_math_funcs_hack[])() = {
21 acos, asin, atan, atan2, ceil, cos, cosh, exp, fabs, floor,
22 fmod, log, log10, pow, sin, sinh, sqrt, tan, tanh
24 #endif
26 /* Special free list -- see comments for same code in intobject.c. */
27 #define BLOCK_SIZE 1000 /* 1K less typical malloc overhead */
28 #define BHEAD_SIZE 8 /* Enough for a 64-bit pointer */
29 #define N_FLOATOBJECTS ((BLOCK_SIZE - BHEAD_SIZE) / sizeof(PyFloatObject))
31 struct _floatblock {
32 struct _floatblock *next;
33 PyFloatObject objects[N_FLOATOBJECTS];
36 typedef struct _floatblock PyFloatBlock;
38 static PyFloatBlock *block_list = NULL;
39 static PyFloatObject *free_list = NULL;
41 static PyFloatObject *
42 fill_free_list(void)
44 PyFloatObject *p, *q;
45 /* XXX Float blocks escape the object heap. Use PyObject_MALLOC ??? */
46 p = (PyFloatObject *) PyMem_MALLOC(sizeof(PyFloatBlock));
47 if (p == NULL)
48 return (PyFloatObject *) PyErr_NoMemory();
49 ((PyFloatBlock *)p)->next = block_list;
50 block_list = (PyFloatBlock *)p;
51 p = &((PyFloatBlock *)p)->objects[0];
52 q = p + N_FLOATOBJECTS;
53 while (--q > p)
54 q->ob_type = (struct _typeobject *)(q-1);
55 q->ob_type = NULL;
56 return p + N_FLOATOBJECTS - 1;
59 PyObject *
60 PyFloat_FromDouble(double fval)
62 register PyFloatObject *op;
63 if (free_list == NULL) {
64 if ((free_list = fill_free_list()) == NULL)
65 return NULL;
67 /* PyObject_New is inlined */
68 op = free_list;
69 free_list = (PyFloatObject *)op->ob_type;
70 PyObject_INIT(op, &PyFloat_Type);
71 op->ob_fval = fval;
72 return (PyObject *) op;
75 /**************************************************************************
76 RED_FLAG 22-Sep-2000 tim
77 PyFloat_FromString's pend argument is braindead. Prior to this RED_FLAG,
79 1. If v was a regular string, *pend was set to point to its terminating
80 null byte. That's useless (the caller can find that without any
81 help from this function!).
83 2. If v was a Unicode string, or an object convertible to a character
84 buffer, *pend was set to point into stack trash (the auto temp
85 vector holding the character buffer). That was downright dangerous.
87 Since we can't change the interface of a public API function, pend is
88 still supported but now *officially* useless: if pend is not NULL,
89 *pend is set to NULL.
90 **************************************************************************/
91 PyObject *
92 PyFloat_FromString(PyObject *v, char **pend)
94 const char *s, *last, *end;
95 double x;
96 char buffer[256]; /* for errors */
97 #ifdef Py_USING_UNICODE
98 char s_buffer[256]; /* for objects convertible to a char buffer */
99 #endif
100 int len;
102 if (pend)
103 *pend = NULL;
104 if (PyString_Check(v)) {
105 s = PyString_AS_STRING(v);
106 len = PyString_GET_SIZE(v);
108 #ifdef Py_USING_UNICODE
109 else if (PyUnicode_Check(v)) {
110 if (PyUnicode_GET_SIZE(v) >= sizeof(s_buffer)) {
111 PyErr_SetString(PyExc_ValueError,
112 "Unicode float() literal too long to convert");
113 return NULL;
115 if (PyUnicode_EncodeDecimal(PyUnicode_AS_UNICODE(v),
116 PyUnicode_GET_SIZE(v),
117 s_buffer,
118 NULL))
119 return NULL;
120 s = s_buffer;
121 len = (int)strlen(s);
123 #endif
124 else if (PyObject_AsCharBuffer(v, &s, &len)) {
125 PyErr_SetString(PyExc_TypeError,
126 "float() needs a string argument");
127 return NULL;
130 last = s + len;
131 while (*s && isspace(Py_CHARMASK(*s)))
132 s++;
133 if (*s == '\0') {
134 PyErr_SetString(PyExc_ValueError, "empty string for float()");
135 return NULL;
137 /* We don't care about overflow or underflow. If the platform supports
138 * them, infinities and signed zeroes (on underflow) are fine.
139 * However, strtod can return 0 for denormalized numbers, where atof
140 * does not. So (alas!) we special-case a zero result. Note that
141 * whether strtod sets errno on underflow is not defined, so we can't
142 * key off errno.
144 PyFPE_START_PROTECT("strtod", return NULL)
145 x = strtod(s, (char **)&end);
146 PyFPE_END_PROTECT(x)
147 errno = 0;
148 /* Believe it or not, Solaris 2.6 can move end *beyond* the null
149 byte at the end of the string, when the input is inf(inity). */
150 if (end > last)
151 end = last;
152 if (end == s) {
153 PyOS_snprintf(buffer, sizeof(buffer),
154 "invalid literal for float(): %.200s", s);
155 PyErr_SetString(PyExc_ValueError, buffer);
156 return NULL;
158 /* Since end != s, the platform made *some* kind of sense out
159 of the input. Trust it. */
160 while (*end && isspace(Py_CHARMASK(*end)))
161 end++;
162 if (*end != '\0') {
163 PyOS_snprintf(buffer, sizeof(buffer),
164 "invalid literal for float(): %.200s", s);
165 PyErr_SetString(PyExc_ValueError, buffer);
166 return NULL;
168 else if (end != last) {
169 PyErr_SetString(PyExc_ValueError,
170 "null byte in argument for float()");
171 return NULL;
173 if (x == 0.0) {
174 /* See above -- may have been strtod being anal
175 about denorms. */
176 PyFPE_START_PROTECT("atof", return NULL)
177 x = atof(s);
178 PyFPE_END_PROTECT(x)
179 errno = 0; /* whether atof ever set errno is undefined */
181 return PyFloat_FromDouble(x);
184 static void
185 float_dealloc(PyFloatObject *op)
187 if (PyFloat_CheckExact(op)) {
188 op->ob_type = (struct _typeobject *)free_list;
189 free_list = op;
191 else
192 op->ob_type->tp_free((PyObject *)op);
195 double
196 PyFloat_AsDouble(PyObject *op)
198 PyNumberMethods *nb;
199 PyFloatObject *fo;
200 double val;
202 if (op && PyFloat_Check(op))
203 return PyFloat_AS_DOUBLE((PyFloatObject*) op);
205 if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL ||
206 nb->nb_float == NULL) {
207 PyErr_BadArgument();
208 return -1;
211 fo = (PyFloatObject*) (*nb->nb_float) (op);
212 if (fo == NULL)
213 return -1;
214 if (!PyFloat_Check(fo)) {
215 PyErr_SetString(PyExc_TypeError,
216 "nb_float should return float object");
217 return -1;
220 val = PyFloat_AS_DOUBLE(fo);
221 Py_DECREF(fo);
223 return val;
226 /* Methods */
228 static void
229 format_float(char *buf, size_t buflen, PyFloatObject *v, int precision)
231 register char *cp;
232 /* Subroutine for float_repr and float_print.
233 We want float numbers to be recognizable as such,
234 i.e., they should contain a decimal point or an exponent.
235 However, %g may print the number as an integer;
236 in such cases, we append ".0" to the string. */
238 assert(PyFloat_Check(v));
239 PyOS_snprintf(buf, buflen, "%.*g", precision, v->ob_fval);
240 cp = buf;
241 if (*cp == '-')
242 cp++;
243 for (; *cp != '\0'; cp++) {
244 /* Any non-digit means it's not an integer;
245 this takes care of NAN and INF as well. */
246 if (!isdigit(Py_CHARMASK(*cp)))
247 break;
249 if (*cp == '\0') {
250 *cp++ = '.';
251 *cp++ = '0';
252 *cp++ = '\0';
256 /* XXX PyFloat_AsStringEx should not be a public API function (for one
257 XXX thing, its signature passes a buffer without a length; for another,
258 XXX it isn't useful outside this file).
260 void
261 PyFloat_AsStringEx(char *buf, PyFloatObject *v, int precision)
263 format_float(buf, 100, v, precision);
266 /* Macro and helper that convert PyObject obj to a C double and store
267 the value in dbl; this replaces the functionality of the coercion
268 slot function */
270 #define CONVERT_TO_DOUBLE(obj, dbl) \
271 if (PyFloat_Check(obj)) \
272 dbl = PyFloat_AS_DOUBLE(obj); \
273 else if (convert_to_double(&(obj), &(dbl)) < 0) \
274 return obj;
276 static int
277 convert_to_double(PyObject **v, double *dbl)
279 register PyObject *obj = *v;
281 if (PyInt_Check(obj)) {
282 *dbl = (double)PyInt_AS_LONG(obj);
284 else if (PyLong_Check(obj)) {
285 *dbl = PyLong_AsDouble(obj);
286 if (*dbl == -1.0 && PyErr_Occurred()) {
287 *v = NULL;
288 return -1;
291 else {
292 Py_INCREF(Py_NotImplemented);
293 *v = Py_NotImplemented;
294 return -1;
296 return 0;
299 /* Precisions used by repr() and str(), respectively.
301 The repr() precision (17 significant decimal digits) is the minimal number
302 that is guaranteed to have enough precision so that if the number is read
303 back in the exact same binary value is recreated. This is true for IEEE
304 floating point by design, and also happens to work for all other modern
305 hardware.
307 The str() precision is chosen so that in most cases, the rounding noise
308 created by various operations is suppressed, while giving plenty of
309 precision for practical use.
313 #define PREC_REPR 17
314 #define PREC_STR 12
316 /* XXX PyFloat_AsString and PyFloat_AsReprString should be deprecated:
317 XXX they pass a char buffer without passing a length.
319 void
320 PyFloat_AsString(char *buf, PyFloatObject *v)
322 format_float(buf, 100, v, PREC_STR);
325 void
326 PyFloat_AsReprString(char *buf, PyFloatObject *v)
328 format_float(buf, 100, v, PREC_REPR);
331 /* ARGSUSED */
332 static int
333 float_print(PyFloatObject *v, FILE *fp, int flags)
335 char buf[100];
336 format_float(buf, sizeof(buf), v,
337 (flags & Py_PRINT_RAW) ? PREC_STR : PREC_REPR);
338 fputs(buf, fp);
339 return 0;
342 static PyObject *
343 float_repr(PyFloatObject *v)
345 char buf[100];
346 format_float(buf, sizeof(buf), v, PREC_REPR);
347 return PyString_FromString(buf);
350 static PyObject *
351 float_str(PyFloatObject *v)
353 char buf[100];
354 format_float(buf, sizeof(buf), v, PREC_STR);
355 return PyString_FromString(buf);
358 static int
359 float_compare(PyFloatObject *v, PyFloatObject *w)
361 double i = v->ob_fval;
362 double j = w->ob_fval;
363 return (i < j) ? -1 : (i > j) ? 1 : 0;
366 static long
367 float_hash(PyFloatObject *v)
369 return _Py_HashDouble(v->ob_fval);
372 static PyObject *
373 float_add(PyObject *v, PyObject *w)
375 double a,b;
376 CONVERT_TO_DOUBLE(v, a);
377 CONVERT_TO_DOUBLE(w, b);
378 PyFPE_START_PROTECT("add", return 0)
379 a = a + b;
380 PyFPE_END_PROTECT(a)
381 return PyFloat_FromDouble(a);
384 static PyObject *
385 float_sub(PyObject *v, PyObject *w)
387 double a,b;
388 CONVERT_TO_DOUBLE(v, a);
389 CONVERT_TO_DOUBLE(w, b);
390 PyFPE_START_PROTECT("subtract", return 0)
391 a = a - b;
392 PyFPE_END_PROTECT(a)
393 return PyFloat_FromDouble(a);
396 static PyObject *
397 float_mul(PyObject *v, PyObject *w)
399 double a,b;
400 CONVERT_TO_DOUBLE(v, a);
401 CONVERT_TO_DOUBLE(w, b);
402 PyFPE_START_PROTECT("multiply", return 0)
403 a = a * b;
404 PyFPE_END_PROTECT(a)
405 return PyFloat_FromDouble(a);
408 static PyObject *
409 float_div(PyObject *v, PyObject *w)
411 double a,b;
412 CONVERT_TO_DOUBLE(v, a);
413 CONVERT_TO_DOUBLE(w, b);
414 if (b == 0.0) {
415 PyErr_SetString(PyExc_ZeroDivisionError, "float division");
416 return NULL;
418 PyFPE_START_PROTECT("divide", return 0)
419 a = a / b;
420 PyFPE_END_PROTECT(a)
421 return PyFloat_FromDouble(a);
424 static PyObject *
425 float_classic_div(PyObject *v, PyObject *w)
427 double a,b;
428 CONVERT_TO_DOUBLE(v, a);
429 CONVERT_TO_DOUBLE(w, b);
430 if (Py_DivisionWarningFlag >= 2 &&
431 PyErr_Warn(PyExc_DeprecationWarning, "classic float division") < 0)
432 return NULL;
433 if (b == 0.0) {
434 PyErr_SetString(PyExc_ZeroDivisionError, "float division");
435 return NULL;
437 PyFPE_START_PROTECT("divide", return 0)
438 a = a / b;
439 PyFPE_END_PROTECT(a)
440 return PyFloat_FromDouble(a);
443 static PyObject *
444 float_rem(PyObject *v, PyObject *w)
446 double vx, wx;
447 double mod;
448 CONVERT_TO_DOUBLE(v, vx);
449 CONVERT_TO_DOUBLE(w, wx);
450 if (wx == 0.0) {
451 PyErr_SetString(PyExc_ZeroDivisionError, "float modulo");
452 return NULL;
454 PyFPE_START_PROTECT("modulo", return 0)
455 mod = fmod(vx, wx);
456 /* note: checking mod*wx < 0 is incorrect -- underflows to
457 0 if wx < sqrt(smallest nonzero double) */
458 if (mod && ((wx < 0) != (mod < 0))) {
459 mod += wx;
461 PyFPE_END_PROTECT(mod)
462 return PyFloat_FromDouble(mod);
465 static PyObject *
466 float_divmod(PyObject *v, PyObject *w)
468 double vx, wx;
469 double div, mod, floordiv;
470 CONVERT_TO_DOUBLE(v, vx);
471 CONVERT_TO_DOUBLE(w, wx);
472 if (wx == 0.0) {
473 PyErr_SetString(PyExc_ZeroDivisionError, "float divmod()");
474 return NULL;
476 PyFPE_START_PROTECT("divmod", return 0)
477 mod = fmod(vx, wx);
478 /* fmod is typically exact, so vx-mod is *mathematically* an
479 exact multiple of wx. But this is fp arithmetic, and fp
480 vx - mod is an approximation; the result is that div may
481 not be an exact integral value after the division, although
482 it will always be very close to one.
484 div = (vx - mod) / wx;
485 if (mod) {
486 /* ensure the remainder has the same sign as the denominator */
487 if ((wx < 0) != (mod < 0)) {
488 mod += wx;
489 div -= 1.0;
492 else {
493 /* the remainder is zero, and in the presence of signed zeroes
494 fmod returns different results across platforms; ensure
495 it has the same sign as the denominator; we'd like to do
496 "mod = wx * 0.0", but that may get optimized away */
497 mod *= mod; /* hide "mod = +0" from optimizer */
498 if (wx < 0.0)
499 mod = -mod;
501 /* snap quotient to nearest integral value */
502 if (div) {
503 floordiv = floor(div);
504 if (div - floordiv > 0.5)
505 floordiv += 1.0;
507 else {
508 /* div is zero - get the same sign as the true quotient */
509 div *= div; /* hide "div = +0" from optimizers */
510 floordiv = div * vx / wx; /* zero w/ sign of vx/wx */
512 PyFPE_END_PROTECT(floordiv)
513 return Py_BuildValue("(dd)", floordiv, mod);
516 static PyObject *
517 float_pow(PyObject *v, PyObject *w, PyObject *z)
519 double iv, iw, ix;
521 if ((PyObject *)z != Py_None) {
522 PyErr_SetString(PyExc_TypeError, "pow() 3rd argument not "
523 "allowed unless all arguments are integers");
524 return NULL;
527 CONVERT_TO_DOUBLE(v, iv);
528 CONVERT_TO_DOUBLE(w, iw);
530 /* Sort out special cases here instead of relying on pow() */
531 if (iw == 0) { /* v**0 is 1, even 0**0 */
532 PyFPE_START_PROTECT("pow", return NULL)
533 if ((PyObject *)z != Py_None) {
534 double iz;
535 CONVERT_TO_DOUBLE(z, iz);
536 ix = fmod(1.0, iz);
537 if (ix != 0 && iz < 0)
538 ix += iz;
540 else
541 ix = 1.0;
542 PyFPE_END_PROTECT(ix)
543 return PyFloat_FromDouble(ix);
545 if (iv == 0.0) { /* 0**w is error if w<0, else 1 */
546 if (iw < 0.0) {
547 PyErr_SetString(PyExc_ZeroDivisionError,
548 "0.0 cannot be raised to a negative power");
549 return NULL;
551 return PyFloat_FromDouble(0.0);
553 if (iv < 0.0 && iw != floor(iw)) {
554 PyErr_SetString(PyExc_ValueError,
555 "negative number cannot be raised to a fractional power");
556 return NULL;
558 errno = 0;
559 PyFPE_START_PROTECT("pow", return NULL)
560 ix = pow(iv, iw);
561 PyFPE_END_PROTECT(ix)
562 Py_SET_ERANGE_IF_OVERFLOW(ix);
563 if (errno != 0) {
564 /* XXX could it be another type of error? */
565 PyErr_SetFromErrno(PyExc_OverflowError);
566 return NULL;
568 return PyFloat_FromDouble(ix);
571 static PyObject *
572 float_int_div(PyObject *v, PyObject *w)
574 PyObject *t, *r;
576 t = float_divmod(v, w);
577 if (t != NULL) {
578 r = PyTuple_GET_ITEM(t, 0);
579 Py_INCREF(r);
580 Py_DECREF(t);
581 return r;
583 return NULL;
586 static PyObject *
587 float_neg(PyFloatObject *v)
589 return PyFloat_FromDouble(-v->ob_fval);
592 static PyObject *
593 float_pos(PyFloatObject *v)
595 if (PyFloat_CheckExact(v)) {
596 Py_INCREF(v);
597 return (PyObject *)v;
599 else
600 return PyFloat_FromDouble(v->ob_fval);
603 static PyObject *
604 float_abs(PyFloatObject *v)
606 return PyFloat_FromDouble(fabs(v->ob_fval));
609 static int
610 float_nonzero(PyFloatObject *v)
612 return v->ob_fval != 0.0;
615 static int
616 float_coerce(PyObject **pv, PyObject **pw)
618 if (PyInt_Check(*pw)) {
619 long x = PyInt_AsLong(*pw);
620 *pw = PyFloat_FromDouble((double)x);
621 Py_INCREF(*pv);
622 return 0;
624 else if (PyLong_Check(*pw)) {
625 *pw = PyFloat_FromDouble(PyLong_AsDouble(*pw));
626 Py_INCREF(*pv);
627 return 0;
629 else if (PyFloat_Check(*pw)) {
630 Py_INCREF(*pv);
631 Py_INCREF(*pw);
632 return 0;
634 return 1; /* Can't do it */
637 static PyObject *
638 float_int(PyObject *v)
640 double x = PyFloat_AsDouble(v);
641 double wholepart; /* integral portion of x, rounded toward 0 */
642 long aslong; /* (long)wholepart */
644 (void)modf(x, &wholepart);
645 #ifdef RISCOS
646 /* conversion from floating to integral type would raise exception */
647 if (wholepart>LONG_MAX || wholepart<LONG_MIN) {
648 PyErr_SetString(PyExc_OverflowError, "float too large to convert");
649 return NULL;
651 #endif
652 /* doubles may have more bits than longs, or vice versa; and casting
653 to long may yield gibberish in either case. What really matters
654 is whether converting back to double again reproduces what we
655 started with. */
656 aslong = (long)wholepart;
657 if ((double)aslong == wholepart)
658 return PyInt_FromLong(aslong);
659 PyErr_SetString(PyExc_OverflowError, "float too large to convert");
660 return NULL;
663 static PyObject *
664 float_long(PyObject *v)
666 double x = PyFloat_AsDouble(v);
667 return PyLong_FromDouble(x);
670 static PyObject *
671 float_float(PyObject *v)
673 Py_INCREF(v);
674 return v;
678 staticforward PyObject *
679 float_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
681 static PyObject *
682 float_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
684 PyObject *x = Py_False; /* Integer zero */
685 static char *kwlist[] = {"x", 0};
687 if (type != &PyFloat_Type)
688 return float_subtype_new(type, args, kwds); /* Wimp out */
689 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:float", kwlist, &x))
690 return NULL;
691 if (PyString_Check(x))
692 return PyFloat_FromString(x, NULL);
693 return PyNumber_Float(x);
696 /* Wimpy, slow approach to tp_new calls for subtypes of float:
697 first create a regular float from whatever arguments we got,
698 then allocate a subtype instance and initialize its ob_fval
699 from the regular float. The regular float is then thrown away.
701 static PyObject *
702 float_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
704 PyObject *tmp, *new;
706 assert(PyType_IsSubtype(type, &PyFloat_Type));
707 tmp = float_new(&PyFloat_Type, args, kwds);
708 if (tmp == NULL)
709 return NULL;
710 assert(PyFloat_CheckExact(tmp));
711 new = type->tp_alloc(type, 0);
712 if (new == NULL)
713 return NULL;
714 ((PyFloatObject *)new)->ob_fval = ((PyFloatObject *)tmp)->ob_fval;
715 Py_DECREF(tmp);
716 return new;
719 static char float_doc[] =
720 "float(x) -> floating point number\n\
722 Convert a string or number to a floating point number, if possible.";
725 static PyNumberMethods float_as_number = {
726 (binaryfunc)float_add, /*nb_add*/
727 (binaryfunc)float_sub, /*nb_subtract*/
728 (binaryfunc)float_mul, /*nb_multiply*/
729 (binaryfunc)float_classic_div, /*nb_divide*/
730 (binaryfunc)float_rem, /*nb_remainder*/
731 (binaryfunc)float_divmod, /*nb_divmod*/
732 (ternaryfunc)float_pow, /*nb_power*/
733 (unaryfunc)float_neg, /*nb_negative*/
734 (unaryfunc)float_pos, /*nb_positive*/
735 (unaryfunc)float_abs, /*nb_absolute*/
736 (inquiry)float_nonzero, /*nb_nonzero*/
737 0, /*nb_invert*/
738 0, /*nb_lshift*/
739 0, /*nb_rshift*/
740 0, /*nb_and*/
741 0, /*nb_xor*/
742 0, /*nb_or*/
743 (coercion)float_coerce, /*nb_coerce*/
744 (unaryfunc)float_int, /*nb_int*/
745 (unaryfunc)float_long, /*nb_long*/
746 (unaryfunc)float_float, /*nb_float*/
747 0, /* nb_oct */
748 0, /* nb_hex */
749 0, /* nb_inplace_add */
750 0, /* nb_inplace_subtract */
751 0, /* nb_inplace_multiply */
752 0, /* nb_inplace_divide */
753 0, /* nb_inplace_remainder */
754 0, /* nb_inplace_power */
755 0, /* nb_inplace_lshift */
756 0, /* nb_inplace_rshift */
757 0, /* nb_inplace_and */
758 0, /* nb_inplace_xor */
759 0, /* nb_inplace_or */
760 float_int_div, /* nb_floor_divide */
761 float_div, /* nb_true_divide */
762 0, /* nb_inplace_floor_divide */
763 0, /* nb_inplace_true_divide */
766 PyTypeObject PyFloat_Type = {
767 PyObject_HEAD_INIT(&PyType_Type)
769 "float",
770 sizeof(PyFloatObject),
772 (destructor)float_dealloc, /* tp_dealloc */
773 (printfunc)float_print, /* tp_print */
774 0, /* tp_getattr */
775 0, /* tp_setattr */
776 (cmpfunc)float_compare, /* tp_compare */
777 (reprfunc)float_repr, /* tp_repr */
778 &float_as_number, /* tp_as_number */
779 0, /* tp_as_sequence */
780 0, /* tp_as_mapping */
781 (hashfunc)float_hash, /* tp_hash */
782 0, /* tp_call */
783 (reprfunc)float_str, /* tp_str */
784 PyObject_GenericGetAttr, /* tp_getattro */
785 0, /* tp_setattro */
786 0, /* tp_as_buffer */
787 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES |
788 Py_TPFLAGS_BASETYPE, /* tp_flags */
789 float_doc, /* tp_doc */
790 0, /* tp_traverse */
791 0, /* tp_clear */
792 0, /* tp_richcompare */
793 0, /* tp_weaklistoffset */
794 0, /* tp_iter */
795 0, /* tp_iternext */
796 0, /* tp_methods */
797 0, /* tp_members */
798 0, /* tp_getset */
799 0, /* tp_base */
800 0, /* tp_dict */
801 0, /* tp_descr_get */
802 0, /* tp_descr_set */
803 0, /* tp_dictoffset */
804 0, /* tp_init */
805 0, /* tp_alloc */
806 float_new, /* tp_new */
809 void
810 PyFloat_Fini(void)
812 PyFloatObject *p;
813 PyFloatBlock *list, *next;
814 int i;
815 int bc, bf; /* block count, number of freed blocks */
816 int frem, fsum; /* remaining unfreed floats per block, total */
818 bc = 0;
819 bf = 0;
820 fsum = 0;
821 list = block_list;
822 block_list = NULL;
823 free_list = NULL;
824 while (list != NULL) {
825 bc++;
826 frem = 0;
827 for (i = 0, p = &list->objects[0];
828 i < N_FLOATOBJECTS;
829 i++, p++) {
830 if (PyFloat_CheckExact(p) && p->ob_refcnt != 0)
831 frem++;
833 next = list->next;
834 if (frem) {
835 list->next = block_list;
836 block_list = list;
837 for (i = 0, p = &list->objects[0];
838 i < N_FLOATOBJECTS;
839 i++, p++) {
840 if (!PyFloat_CheckExact(p) ||
841 p->ob_refcnt == 0) {
842 p->ob_type = (struct _typeobject *)
843 free_list;
844 free_list = p;
848 else {
849 PyMem_FREE(list); /* XXX PyObject_FREE ??? */
850 bf++;
852 fsum += frem;
853 list = next;
855 if (!Py_VerboseFlag)
856 return;
857 fprintf(stderr, "# cleanup floats");
858 if (!fsum) {
859 fprintf(stderr, "\n");
861 else {
862 fprintf(stderr,
863 ": %d unfreed float%s in %d out of %d block%s\n",
864 fsum, fsum == 1 ? "" : "s",
865 bc - bf, bc, bc == 1 ? "" : "s");
867 if (Py_VerboseFlag > 1) {
868 list = block_list;
869 while (list != NULL) {
870 for (i = 0, p = &list->objects[0];
871 i < N_FLOATOBJECTS;
872 i++, p++) {
873 if (PyFloat_CheckExact(p) &&
874 p->ob_refcnt != 0) {
875 char buf[100];
876 PyFloat_AsString(buf, p);
877 fprintf(stderr,
878 "# <float at %p, refcnt=%d, val=%s>\n",
879 p, p->ob_refcnt, buf);
882 list = list->next;