Merged revisions 62914-62916,62918-62919,62921-62922,62924-62942,62944-62945,62947-62949 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r62914 | skip.montanaro | 2008-05-08 20:45:00 -0400 (Thu, 08 May 2008) | 4 lines
Add an example about using NamedTemporaryFile() to replace mktemp(). I'm
unclear whether the verbatim text should have been indented or by how much.
........
r62915 | benjamin.peterson | 2008-05-08 20:50:40 -0400 (Thu, 08 May 2008) | 2 lines
reindent example
........
r62927 | georg.brandl | 2008-05-09 02:09:25 -0400 (Fri, 09 May 2008) | 2 lines
#2788: add .hgignore file.
........
r62928 | georg.brandl | 2008-05-09 02:10:43 -0400 (Fri, 09 May 2008) | 2 lines
#2781: fix function name.
........
r62929 | georg.brandl | 2008-05-09 02:18:27 -0400 (Fri, 09 May 2008) | 2 lines
Add a sentence to basicConfig() that is in the docstring.
........
r62930 | georg.brandl | 2008-05-09 02:26:54 -0400 (Fri, 09 May 2008) | 2 lines
Add another link to colorsys docs.
........
r62931 | georg.brandl | 2008-05-09 02:36:07 -0400 (Fri, 09 May 2008) | 2 lines
Add Kodos as a re reference.
........
r62932 | georg.brandl | 2008-05-09 02:39:58 -0400 (Fri, 09 May 2008) | 2 lines
Add a note about using reload().
........
r62933 | andrew.kuchling | 2008-05-09 07:46:05 -0400 (Fri, 09 May 2008) | 3 lines
Update planned release date.
Uncomment PEP 370 section.
Add some module items
........
r62934 | christian.heimes | 2008-05-09 08:19:09 -0400 (Fri, 09 May 2008) | 1 line
Add --user option to build_ext
........
r62948 | mark.dickinson | 2008-05-09 13:54:23 -0400 (Fri, 09 May 2008) | 3 lines
Issue #2487. math.ldexp(x, n) raised OverflowError when n was large and
negative; fix to return an (appropriately signed) zero instead.
........
r62949 | martin.v.loewis | 2008-05-09 14:21:55 -0400 (Fri, 09 May 2008) | 1 line
Use the CHM file name that Sphinx assigns.
........
diff --git a/Modules/mathmodule.c b/Modules/mathmodule.c
index fffb630..d7aba68 100644
--- a/Modules/mathmodule.c
+++ b/Modules/mathmodule.c
@@ -425,23 +425,65 @@
math_ldexp(PyObject *self, PyObject *args)
{
double x, r;
- int exp;
- if (! PyArg_ParseTuple(args, "di:ldexp", &x, &exp))
+ PyObject *oexp;
+ long exp;
+ if (! PyArg_ParseTuple(args, "dO:ldexp", &x, &oexp))
return NULL;
- errno = 0;
- PyFPE_START_PROTECT("in math_ldexp", return 0)
- r = ldexp(x, exp);
- PyFPE_END_PROTECT(r)
- if (Py_IS_FINITE(x) && Py_IS_INFINITY(r))
- errno = ERANGE;
- /* Windows MSVC8 sets errno = EDOM on ldexp(NaN, i);
- we unset it to avoid raising a ValueError here. */
- if (errno == EDOM)
+
+ if (PyLong_Check(oexp)) {
+ /* on overflow, replace exponent with either LONG_MAX
+ or LONG_MIN, depending on the sign. */
+ exp = PyLong_AsLong(oexp);
+ if (exp == -1 && PyErr_Occurred()) {
+ if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
+ if (Py_SIZE(oexp) < 0) {
+ exp = LONG_MIN;
+ }
+ else {
+ exp = LONG_MAX;
+ }
+ PyErr_Clear();
+ }
+ else {
+ /* propagate any unexpected exception */
+ return NULL;
+ }
+ }
+ }
+ else if (PyLong_Check(oexp)) {
+ exp = PyLong_AS_LONG(oexp);
+ }
+ else {
+ PyErr_SetString(PyExc_TypeError,
+ "Expected an int or long as second argument "
+ "to ldexp.");
+ return NULL;
+ }
+
+ if (x == 0. || !Py_IS_FINITE(x)) {
+ /* NaNs, zeros and infinities are returned unchanged */
+ r = x;
errno = 0;
+ } else if (exp > INT_MAX) {
+ /* overflow */
+ r = copysign(Py_HUGE_VAL, x);
+ errno = ERANGE;
+ } else if (exp < INT_MIN) {
+ /* underflow to +-0 */
+ r = copysign(0., x);
+ errno = 0;
+ } else {
+ errno = 0;
+ PyFPE_START_PROTECT("in math_ldexp", return 0);
+ r = ldexp(x, (int)exp);
+ PyFPE_END_PROTECT(r);
+ if (Py_IS_INFINITY(r))
+ errno = ERANGE;
+ }
+
if (errno && is_error(r))
return NULL;
- else
- return PyFloat_FromDouble(r);
+ return PyFloat_FromDouble(r);
}
PyDoc_STRVAR(math_ldexp_doc,