summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
Diffstat (limited to 'Lib')
-rwxr-xr-xLib/pydoc.py1
-rwxr-xr-xLib/rational.py56
-rwxr-xr-xLib/test/regrtest.py3
-rw-r--r--Lib/test/test_builtin.py21
-rw-r--r--Lib/test/test_resource.py3
-rw-r--r--Lib/test/test_xmlrpc.py31
6 files changed, 44 insertions, 71 deletions
diff --git a/Lib/pydoc.py b/Lib/pydoc.py
index 755d51a..0fdbb90 100755
--- a/Lib/pydoc.py
+++ b/Lib/pydoc.py
@@ -1203,7 +1203,6 @@ class TextDoc(Doc):
else:
tag = "inherited from %s" % classname(thisclass,
object.__module__)
- filter(lambda t: not t[0].startswith('_'), attrs)
# Sort attrs by name.
attrs.sort()
diff --git a/Lib/rational.py b/Lib/rational.py
index da75ab1..8de8f23 100755
--- a/Lib/rational.py
+++ b/Lib/rational.py
@@ -24,60 +24,6 @@ def gcd(a, b):
return a
-def _binary_float_to_ratio(x):
- """x -> (top, bot), a pair of ints s.t. x = top/bot.
-
- The conversion is done exactly, without rounding.
- bot > 0 guaranteed.
- Some form of binary fp is assumed.
- Pass NaNs or infinities at your own risk.
-
- >>> _binary_float_to_ratio(10.0)
- (10, 1)
- >>> _binary_float_to_ratio(0.0)
- (0, 1)
- >>> _binary_float_to_ratio(-.25)
- (-1, 4)
- """
- # XXX Move this to floatobject.c with a name like
- # float.as_integer_ratio()
-
- if x == 0:
- return 0, 1
- f, e = math.frexp(x)
- signbit = 1
- if f < 0:
- f = -f
- signbit = -1
- assert 0.5 <= f < 1.0
- # x = signbit * f * 2**e exactly
-
- # Suck up CHUNK bits at a time; 28 is enough so that we suck
- # up all bits in 2 iterations for all known binary double-
- # precision formats, and small enough to fit in an int.
- CHUNK = 28
- top = 0
- # invariant: x = signbit * (top + f) * 2**e exactly
- while f:
- f = math.ldexp(f, CHUNK)
- digit = trunc(f)
- assert digit >> CHUNK == 0
- top = (top << CHUNK) | digit
- f = f - digit
- assert 0.0 <= f < 1.0
- e = e - CHUNK
- assert top
-
- # Add in the sign bit.
- top = signbit * top
-
- # now x = top * 2**e exactly; fold in 2**e
- if e>0:
- return (top * 2**e, 1)
- else:
- return (top, 2 ** -e)
-
-
_RATIONAL_FORMAT = re.compile(
r'^\s*(?P<sign>[-+]?)(?P<num>\d+)'
r'(?:/(?P<denom>\d+)|\.(?P<decimal>\d+))?\s*$')
@@ -162,7 +108,7 @@ class Rational(RationalAbc):
(cls.__name__, f, type(f).__name__))
if math.isnan(f) or math.isinf(f):
raise TypeError("Cannot convert %r to %s." % (f, cls.__name__))
- return cls(*_binary_float_to_ratio(f))
+ return cls(*f.as_integer_ratio())
@classmethod
def from_decimal(cls, dec):
diff --git a/Lib/test/regrtest.py b/Lib/test/regrtest.py
index 790d769..5934e6b 100755
--- a/Lib/test/regrtest.py
+++ b/Lib/test/regrtest.py
@@ -752,6 +752,9 @@ def dash_R_cleanup(fs, ps, pic, abcs):
sys.path_importer_cache.clear()
sys.path_importer_cache.update(pic)
+ # clear type cache
+ sys._cleartypecache()
+
# Clear ABC registries, restoring previously saved ABC registries.
for abc in [getattr(_abcoll, a) for a in _abcoll.__all__]:
if not issubclass(abc, _Abstract):
diff --git a/Lib/test/test_builtin.py b/Lib/test/test_builtin.py
index e6ded81..92c1d00 100644
--- a/Lib/test/test_builtin.py
+++ b/Lib/test/test_builtin.py
@@ -5,7 +5,7 @@ from test.test_support import fcmp, TESTFN, unlink, run_unittest, \
run_with_locale
from operator import neg
-import sys, warnings, random, UserDict, io
+import sys, warnings, random, UserDict, io, rational
warnings.filterwarnings("ignore", "hex../oct.. of negative int",
FutureWarning, __name__)
warnings.filterwarnings("ignore", "integer argument expected",
@@ -592,6 +592,25 @@ class BuiltinTest(unittest.TestCase):
# make sure we can take a subclass of str as a format spec
self.assertEqual(format(0, C('10')), ' 0')
+ def test_floatasratio(self):
+ R = rational.Rational
+ self.assertEqual(R(0, 1),
+ R(*float(0.0).as_integer_ratio()))
+ self.assertEqual(R(5, 2),
+ R(*float(2.5).as_integer_ratio()))
+ self.assertEqual(R(1, 2),
+ R(*float(0.5).as_integer_ratio()))
+ self.assertEqual(R(4728779608739021, 2251799813685248),
+ R(*float(2.1).as_integer_ratio()))
+ self.assertEqual(R(-4728779608739021, 2251799813685248),
+ R(*float(-2.1).as_integer_ratio()))
+ self.assertEqual(R(-2100, 1),
+ R(*float(-2100.0).as_integer_ratio()))
+
+ self.assertRaises(OverflowError, float('inf').as_integer_ratio)
+ self.assertRaises(OverflowError, float('-inf').as_integer_ratio)
+ self.assertRaises(ValueError, float('nan').as_integer_ratio)
+
def test_getattr(self):
import sys
self.assert_(getattr(sys, 'stdout') is sys.stdout)
diff --git a/Lib/test/test_resource.py b/Lib/test/test_resource.py
index 43ff372..86440b5 100644
--- a/Lib/test/test_resource.py
+++ b/Lib/test/test_resource.py
@@ -56,13 +56,12 @@ class ResourceTest(unittest.TestCase):
f.flush()
# On some systems (e.g., Ubuntu on hppa) the flush()
# doesn't always cause the exception, but the close()
- # does eventually. Try closing several times in
+ # does eventually. Try flushing several times in
# an attempt to ensure the file is really synced and
# the exception raised.
for i in range(5):
time.sleep(.1)
f.flush()
- f.close()
except IOError:
if not limit_set:
raise
diff --git a/Lib/test/test_xmlrpc.py b/Lib/test/test_xmlrpc.py
index 28cbf0c..092be51 100644
--- a/Lib/test/test_xmlrpc.py
+++ b/Lib/test/test_xmlrpc.py
@@ -312,10 +312,17 @@ def is_unavailable_exception(e):
given by operations on non-blocking sockets.'''
# sometimes we get a -1 error code and/or empty headers
- if e.errcode == -1 or e.headers is None:
+ try:
+ if e.errcode == -1 or e.headers is None:
+ return True
+ exc_mess = e.headers.get('X-exception')
+ except AttributeError:
+ # Ignore socket.errors here.
+ exc_mess = str(e)
+
+ if exc_mess and 'temporarily unavailable' in exc_mess.lower():
return True
-
class SimpleServerTestCase(unittest.TestCase):
def setUp(self):
# enable traceback reporting
@@ -349,7 +356,7 @@ class SimpleServerTestCase(unittest.TestCase):
# ignore failures due to non-blocking socket 'unavailable' errors
if not is_unavailable_exception(e):
# protocol error; provide additional information in test output
- self.fail("%s\n%s" % (e, e.headers))
+ self.fail("%s\n%s" % (e, getattr(e, "headers", "")))
# [ch] The test 404 is causing lots of false alarms.
def XXXtest_404(self):
@@ -375,7 +382,7 @@ class SimpleServerTestCase(unittest.TestCase):
# ignore failures due to non-blocking socket 'unavailable' errors
if not is_unavailable_exception(e):
# protocol error; provide additional information in test output
- self.fail("%s\n%s" % (e, e.headers))
+ self.fail("%s\n%s" % (e, getattr(e, "headers", "")))
def test_introspection2(self):
@@ -388,7 +395,7 @@ class SimpleServerTestCase(unittest.TestCase):
# ignore failures due to non-blocking socket 'unavailable' errors
if not is_unavailable_exception(e):
# protocol error; provide additional information in test output
- self.fail("%s\n%s" % (e, e.headers))
+ self.fail("%s\n%s" % (e, getattr(e, "headers", "")))
def test_introspection3(self):
try:
@@ -400,7 +407,7 @@ class SimpleServerTestCase(unittest.TestCase):
# ignore failures due to non-blocking socket 'unavailable' errors
if not is_unavailable_exception(e):
# protocol error; provide additional information in test output
- self.fail("%s\n%s" % (e, e.headers))
+ self.fail("%s\n%s" % (e, getattr(e, "headers", "")))
def test_introspection4(self):
# the SimpleXMLRPCServer doesn't support signatures, but
@@ -413,7 +420,7 @@ class SimpleServerTestCase(unittest.TestCase):
# ignore failures due to non-blocking socket 'unavailable' errors
if not is_unavailable_exception(e):
# protocol error; provide additional information in test output
- self.fail("%s\n%s" % (e, e.headers))
+ self.fail("%s\n%s" % (e, getattr(e, "headers", "")))
def test_multicall(self):
try:
@@ -430,7 +437,7 @@ class SimpleServerTestCase(unittest.TestCase):
# ignore failures due to non-blocking socket 'unavailable' errors
if not is_unavailable_exception(e):
# protocol error; provide additional information in test output
- self.fail("%s\n%s" % (e, e.headers))
+ self.fail("%s\n%s" % (e, getattr(e, "headers", "")))
def test_non_existing_multicall(self):
try:
@@ -451,7 +458,7 @@ class SimpleServerTestCase(unittest.TestCase):
# ignore failures due to non-blocking socket 'unavailable' errors
if not is_unavailable_exception(e):
# protocol error; provide additional information in test output
- self.fail("%s\n%s" % (e, e.headers))
+ self.fail("%s\n%s" % (e, getattr(e, "headers", "")))
# This is a contrived way to make a failure occur on the server side
# in order to test the _send_traceback_header flag on the server
@@ -498,7 +505,7 @@ class FailingServerTestCase(unittest.TestCase):
# ignore failures due to non-blocking socket 'unavailable' errors
if not is_unavailable_exception(e):
# protocol error; provide additional information in test output
- self.fail("%s\n%s" % (e, e.headers))
+ self.fail("%s\n%s" % (e, getattr(e, "headers", "")))
def test_fail_no_info(self):
# use the broken message class
@@ -509,7 +516,7 @@ class FailingServerTestCase(unittest.TestCase):
p.pow(6,8)
except (xmlrpclib.ProtocolError, socket.error) as e:
# ignore failures due to non-blocking socket 'unavailable' errors
- if not is_unavailable_exception(e):
+ if not is_unavailable_exception(e) and hasattr(e, "headers"):
# The two server-side error headers shouldn't be sent back in this case
self.assertTrue(e.headers.get("X-exception") is None)
self.assertTrue(e.headers.get("X-traceback") is None)
@@ -529,7 +536,7 @@ class FailingServerTestCase(unittest.TestCase):
p.pow(6,8)
except (xmlrpclib.ProtocolError, socket.error) as e:
# ignore failures due to non-blocking socket 'unavailable' errors
- if not is_unavailable_exception(e):
+ if not is_unavailable_exception(e) and hasattr(e, "headers"):
# We should get error info in the response
expected_err = "invalid literal for int() with base 10: 'I am broken'"
self.assertEqual(e.headers.get("x-exception"), expected_err)