Commit Graph

138 Commits

Author SHA1 Message Date
Tim Peters 307fa78107 SF bug #513866: Float/long comparison anomaly.
When an integer is compared to a float now, the int isn't coerced to float.
This avoids spurious overflow exceptions and insane results.  This should
compute correct results, without raising spurious exceptions, in all cases
now -- although I expect that what happens when an int/long is compared to
a NaN is still a platform accident.

Note that we had potential problems here even with "short" ints, on boxes
where sizeof(long)==8.  There's #ifdef'ed code here to handle that, but
I can't test it as intended.  I tested it by changing the #ifdef to
trigger on my 32-bit box instead.

I suppose this is a bugfix candidate, but I won't backport it.  It's
long-winded (for speed) and messy (because the problem is messy).  Note
that this also depends on a previous 2.4 patch that introduced
_Py_SwappedOp[] as an extern.
2004-09-23 08:06:40 +00:00
Neal Norwitz 739a8f86d6 Fix a couple of signed/unsigned comparison warnings 2004-07-08 01:55:58 +00:00
Martin v. Löwis 737ea82a5a Patch #774665: Make Python LC_NUMERIC agnostic. 2004-06-08 18:52:54 +00:00
Michael W. Hudson 08678a1055 Remove float_compare as per
[ 899109 ] 1==float('nan')

which can now finally be closed, I think.
2004-05-26 17:36:12 +00:00
Michael W. Hudson 6bee23cdc3 Oops, didn't mean to commit the removal of float_compare! 2004-02-26 13:16:03 +00:00
Michael W. Hudson 957f9774b6 Pass a variable that actually exists to PyFPE_END_PROTECT in
float_richcompare.  Reported on c.l.py by Helmut Jarausch.
2004-02-26 12:33:09 +00:00
Michael W. Hudson d3b33b5f6f "Fix" (for certain configurations of the planets, including
recent gcc on Linux/x86)

[ 899109 ] 1==float('nan')

by implementing rich comparisons for floats.

Seems to make comparisons involving NaNs somewhat less surprising
when the underlying C compiler actually implements C99 semantics.
2004-02-19 19:35:22 +00:00
Skip Montanaro ce59c04127 Remove support for SunOS 4.
Remove BAD_EXEC_PROTOYPE (leftover from IRIX 4 demolition).
2004-01-17 14:19:44 +00:00
Jack Jansen eddc1449ba Getting rid of all the code inside #ifdef macintosh too. 2003-11-20 01:44:59 +00:00
Raymond Hettinger f466793fcc SF patch 703666: Several objects don't decref tmp on failure in subtype_new
Submitted By: Christopher A. Craig

Fillin some missing decrefs.
2003-06-28 20:04:25 +00:00
Tim Peters e87568dd9a SF bug 705231: Assertion failed, python aborts.
float_pow():  Don't let the platform pow() raise -1.0 to an integer power
anymore; at least glibc gets it wrong in some cases.  Note that
math.pow() will continue to deliver wrong (but platform-native) results
in such cases.
2003-05-24 20:18:24 +00:00
Tim Peters f1ed934278 _PyFloat_Pack4(): Removed needless call of floor(). 2003-03-21 17:10:03 +00:00
Tim Peters 9905b943f7 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
refactoring to get all the duplicates of this delicate code out of the
cPickle and struct modules.
2003-03-20 20:53:32 +00:00
Guido van Rossum 5d9113d8be Implement appropriate __getnewargs__ for all immutable subclassable builtin
types.  The special handling for these can now be removed from save_newobj().
Add some testing for this.

Also add support for setting the 'fast' flag on the Python Pickler class,
which suppresses use of the memo.
2003-01-29 17:58:45 +00:00
Neal Norwitz abcb0c03ad Fix SF bug# 676155, RuntimeWarning with tp_compare
Check return value of PyLong_AsDouble(), it can return an error.
2003-01-28 19:21:24 +00:00
Tim Peters 7d791240c0 float_int(): Some systems raise an exception if a double is cast to
long but the double is too big to fit in a long.  Prevent that.  This
closes some recent bug or patch on SF, but SF is down now so I can't
say which.

Bugfix candidate.
2002-11-21 22:26:37 +00:00
Walter Dörwald f171540ab8 Change int() so that passing a string, unicode, float or long argument
that is outside the integer range no longer raises OverflowError, but
returns a long object instead.

This fixes SF bug http://www.python.org/sf/635115
2002-11-19 20:49:15 +00:00
Neil Schemenauer 2c77e90804 Improve exception message raised by PyFloat_AsDouble if the object does not
have a nb_float slot.  This matches what PyInt_AsLong does.
2002-11-18 16:06:21 +00:00
Guido van Rossum e3a8e7ed1d Call me anal, but there was a particular phrase that was speading to
comments everywhere that bugged me: /* Foo is inlined */ instead of
/* Inline Foo */.  Somehow the "is inlined" phrase always confused me
for half a second (thinking, "No it isn't" until I added the missing
"here").  The new phrase is hopefully unambiguous.
2002-08-19 19:26:42 +00:00
Jeremy Hylton 938ace69a0 staticforward bites the dust.
The staticforward define was needed to support certain broken C
compilers (notably SCO ODT 3.0, perhaps early AIX as well) botched the
static keyword when it was used with a forward declaration of a static
initialized structure.  Standard C allows the forward declaration with
static, and we've decided to stop catering to broken C compilers.  (In
fact, we expect that the compilers are all fixed eight years later.)

I'm leaving staticforward and statichere defined in object.h as
static.  This is only for backwards compatibility with C extensions
that might still use it.

XXX I haven't updated the documentation.
2002-07-17 16:30:39 +00:00
Martin v. Löwis 14f8b4cfcb Patch #568124: Add doc string macros. 2002-06-13 20:33:02 +00:00
Skip Montanaro 71390a9a94 clarify message when raising TypeError to indicate that float() accepts
strings or numbers
2002-05-02 13:03:22 +00:00
Tim Peters dc5a508761 SF bug 525705: [2.2] underflow raise OverflowException.
Another year in the quest to out-guess random C behavior.

Added macros Py_ADJUST_ERANGE1(X) and Py_ADJUST_ERANGE2(X, Y).  The latter
is useful for functions with complex results.  Two corrections to errno-
after-libm-call are attempted:

1. If the platform set errno to ERANGE due to underflow, clear errno.
   Some unknown subset of libm versions and link options do this.  It's
   allowed by C89, but I never figured anyone would do it.

2. If the platform did not set errno but overflow occurred, force
   errno to ERANGE.  C89 required setting errno to ERANGE, but C99
   doesn't.  Some unknown subset of libm versions and link options do
   it the C99 way now.

Bugfix candidate, but hold off until some Linux people actually try it,
with and without -lieee.  I'll send a help plea to Python-Dev.
2002-03-09 04:58:24 +00:00
Tim Peters 77d8a4fc91 float_floor_div: An expression like 3.//1j crashed the interpreter, or
delivered bizarre results.  Check float_divmod for a Py_NotImplemented
return and pass it along (instead of treating Py_NotImplemented as a
2-tuple).
CONVERT_TO_DOUBLE:  Added comments; this macro is obscure.
2001-12-11 20:31:34 +00:00
Tim Peters 63a3571e17 float_int_div(): For clarity, move this closer to the other float
division functions, and rename to float_floor_div.
2001-12-11 19:57:24 +00:00
Tim Peters 97019e4110 PyFloat_AsStringEx(): This function takes an output char* but doesn't
pass the buffer length.  Stop using it.  It should be deprecated, but too
late in the release cycle to do that now.
New static format_float() does the same thing but requires passing the
buffer length too.  Use it instead.
2001-11-28 22:43:45 +00:00
Barry Warsaw af8aef9ee2 PyFloat_FromString(): Conversion of sprintf() to PyOS_snprintf() for
buffer overrun avoidance.
2001-11-28 20:52:21 +00:00
Tim Peters 4e8ab5db38 float_divmod(): the code wasn't sick enough to stop the MS optimizer
from optimizing away mod's sign adjustment when mod == 0; so it got
the intended result only in the debug build.
2001-11-01 23:59:56 +00:00
Tim Peters d2e40d6691 SF bug #477221: abs and divmod act oddly with -0.0
Try to ensure that divmod(-0.0, 1.0) -> (-0.0, +0.0) across platforms.
It always did on Windows, and still does.  It didn't on Linux.  Alas,
there's no platform-independent way to write a test case for this.
Bugfix candidate.
2001-11-01 23:12:27 +00:00
Tim Peters faf0cd21ed float_abs() again: Guido pointed out that this could screw up in the
presence of NaNs.  So pass the issue on to the platform libm fabs();
after all, fabs() is a std C function because you can't implement it
correctly in portable C89.
2001-11-01 21:51:15 +00:00
Tim Peters d2364e8e2d SF bug #477221: abs and divmod act oddly with -0.0.
Partial fix.
float_abs():  ensure abs(-0.0) returns +0.0.
Bugfix candidate.
2001-11-01 20:09:42 +00:00
Guido van Rossum e2ae77b8b8 SF patch #474590 -- RISC OS support 2001-10-24 20:42:55 +00:00
Guido van Rossum 9475a2310d Enable GC for new-style instances. This touches lots of files, since
many types were subclassable but had a xxx_dealloc function that
called PyObject_DEL(self) directly instead of deferring to
self->ob_type->tp_free(self).  It is permissible to set tp_free in the
type object directly to _PyObject_Del, for non-GC types, or to
_PyObject_GC_Del, for GC types.  Still, PyObject_DEL was a tad faster,
so I'm fearing that our pystone rating is going down again.  I'm not
sure if doing something like

void xxx_dealloc(PyObject *self)
{
	if (PyXxxCheckExact(self))
		PyObject_DEL(self);
	else
		self->ob_type->tp_free(self);
}

is any faster than always calling the else branch, so I haven't
attempted that -- however those types whose own dealloc is fancier
(int, float, unicode) do use this pattern.
2001-10-05 20:51:39 +00:00
Guido van Rossum 1952e388ca Add additional coercion support for "self subtypes" to int, long,
float (compare the recent checkin to complex).  Added tests for these.
2001-09-19 01:25:16 +00:00
Tim Peters 2400fa4ad1 Again perhaps the end of [#460020] bug or feature: unicode() and subclasses.
Inhibited complex unary plus optimization when applied to a complex subtype.
Added PyComplex_CheckExact macro.  Some comments and minor code fiddling.
2001-09-12 19:12:49 +00:00
Tim Peters 0280cf79a7 More bug 460020: when F is a subclass of float, disable the unary plus
optimization (+F(whatever)).
2001-09-11 21:53:35 +00:00
Guido van Rossum dea6ef9bfd Replace a few places where X->ob_type was compared to &PyXXX_Type with
calls to PyXXX_CheckExact(X).
2001-09-11 16:13:52 +00:00
Tim Peters 97f4a33e12 Better error msg for 3-arg pow with a float argument. 2001-09-05 23:49:24 +00:00
Tim Peters a40c793d06 Rework the way we try to check for libm overflow, given that C99 no longer
requires that errno ever get set, and it looks like glibc is already
playing that game.  New rules:

+ Never use HUGE_VAL.  Use the new Py_HUGE_VAL instead.

+ Never believe errno.  If overflow is the only thing you're interested in,
  use the new Py_OVERFLOWED(x) macro.  If you're interested in any libm
  errors, use the new Py_SET_ERANGE_IF_OVERFLOW(x) macro, which attempts
  to set errno the way C89 said it worked.

Unfortunately, none of these are reliable, but they work on Windows and I
*expect* under glibc too.
2001-09-05 22:36:56 +00:00
Tim Peters 4c483c4d8e Make the error msgs in our pow() implementations consistent. 2001-09-05 06:24:58 +00:00
Tim Peters 9fffa3eea3 Raise OverflowError when appropriate on long->float conversion. Most of
the fiddling is simply due to that no caller of PyLong_AsDouble ever
checked for failure (so that's fixing old bugs).  PyLong_AsDouble is much
faster for big inputs now too, but that's more of a happy consequence
than a design goal.
2001-09-04 05:14:19 +00:00
Guido van Rossum 1832de4bc0 PEP 238 documented -Qwarn as warning only for classic int or long
division, and this makes sense.  Add -Qwarnall to warn for all
classic divisions, as required by the fixdiv.py tool.
2001-09-04 03:51:09 +00:00
Tim Peters 32f453eaa4 New restriction on pow(x, y, z): If z is not None, x and y must be of
integer types, and y must be >= 0.  See discussion at
http://sf.net/tracker/index.php?func=detail&aid=457066&group_id=5470&atid=105470
2001-09-03 08:35:41 +00:00
Guido van Rossum 393661d15f Add warning mode for classic division, almost exactly as specified in
PEP 238.  Changes:

- add a new flag variable Py_DivisionWarningFlag, declared in
  pydebug.h, defined in object.c, set in main.c, and used in
  {int,long,float,complex}object.c.  When this flag is set, the
  classic division operator issues a DeprecationWarning message.

- add a new API PyRun_SimpleStringFlags() to match
  PyRun_SimpleString().  The main() function calls this so that
  commands run with -c can also benefit from -Dnew.

- While I was at it, I changed the usage message in main() somewhat:
  alphabetized the options, split it in *four* parts to fit in under
  512 bytes (not that I still believe this is necessary -- doc strings
  elsewhere are much longer), and perhaps most visibly, don't display
  the full list of options on each command line error.  Instead, the
  full list is only displayed when -h is used, and otherwise a brief
  reminder of -h is displayed.  When -h is used, write to stdout so
  that you can do `python -h | more'.

Notes:

- I don't want to use the -W option to control whether the classic
  division warning is issued or not, because the machinery to decide
  whether to display the warning or not is very expensive (it involves
  calling into the warnings.py module).  You can use -Werror to turn
  the warnings into exceptions though.

- The -Dnew option doesn't select future division for all of the
  program -- only for the __main__ module.  I don't know if I'll ever
  change this -- it would require changes to the .pyc file magic
  number to do it right, and a more global notion of compiler flags.

- You can usefully combine -Dwarn and -Dnew: this gives the __main__
  module new division, and warns about classic division everywhere
  else.
2001-08-31 17:40:15 +00:00
Guido van Rossum d93dce1699 Fix typo: double semicolons. 2001-08-30 03:09:31 +00:00
Guido van Rossum bef1417f9f Make int, long and float subclassable.
This uses a slightly wimpy and wasteful approach, but it works. :-)
2001-08-29 15:47:46 +00:00
Tim Peters 96685bfbf0 float_pow: Put *all* of the burden on the libm pow in normal
cases.
powu:  Deleted.

This started with a nonsensical error msg:

>>> x = -1.
>>> import sys
>>> x**(-sys.maxint-1L)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
ValueError: negative number cannot be raised to a fractional power
>>>

The special-casing in float_pow was simply wrong in this case (there's
not even anything peculiar about these inputs), and I don't see any point
to it in *any* case:  a decent libm pow should have worst-case error under
1 ULP, so in particular should deliver the exact result whenever the exact
result is representable (else its error is at least 1 ULP).  Thus our
special fiddling for integral values "shouldn't" buy anything in accuracy,
and, to the contrary, repeated multiplication is less accurate than a
decent pow when the true result isn't exactly representable.  So just
letting pow() do its job here (we may not be able to trust libm x-platform
in exceptional cases, but these are normal cases).
2001-08-23 22:31:37 +00:00
Martin v. Löwis 339d0f720e Patch #445762: Support --disable-unicode
- Do not compile unicodeobject, unicodectype, and unicodedata if Unicode is disabled
- check for Py_USING_UNICODE in all places that use Unicode functions
- disables unicode literals, and the builtin functions
- add the types.StringTypes list
- remove Unicode literals from most tests.
2001-08-17 18:39:25 +00:00
Guido van Rossum 4668b000a1 Implement PEP 238 in its (almost) full glory.
This introduces:

- A new operator // that means floor division (the kind of division
  where 1/2 is 0).

- The "future division" statement ("from __future__ import division)
  which changes the meaning of the / operator to implement "true
  division" (where 1/2 is 0.5).

- New overloadable operators __truediv__ and __floordiv__.

- New slots in the PyNumberMethods struct for true and floor division,
  new abstract APIs for them, new opcodes, and so on.

I emphasize that without the future division statement, the semantics
of / will remain unchanged until Python 3.0.

Not yet implemented are warnings (default off) when / is used with int
or long arguments.

This has been on display since 7/31 as SF patch #443474.

Flames to /dev/null.
2001-08-08 05:00:18 +00:00
Tim Peters 6d6c1a35e0 Merge of descr-branch back into trunk. 2001-08-02 04:15:00 +00:00