summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorChristian Heimes <christian@cheimes.de>2008-01-19 16:21:02 (GMT)
committerChristian Heimes <christian@cheimes.de>2008-01-19 16:21:02 (GMT)
commit587c2bfedee6f6c5ee16b4e7eb5392d270a0c1a6 (patch)
tree72bedc48b7476a8d6251b2652b4fdd520dac02f2 /Lib
parentbd84a588e30e51c8a0bcca2a27aea5cb0e3817c7 (diff)
downloadcpython-587c2bfedee6f6c5ee16b4e7eb5392d270a0c1a6.zip
cpython-587c2bfedee6f6c5ee16b4e7eb5392d270a0c1a6.tar.gz
cpython-587c2bfedee6f6c5ee16b4e7eb5392d270a0c1a6.tar.bz2
Merged revisions 60053-60078 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk ........ r60054 | christian.heimes | 2008-01-18 20:12:56 +0100 (Fri, 18 Jan 2008) | 1 line Silence Coverity false alerts with CIDs #172, #183, #184 ........ r60057 | guido.van.rossum | 2008-01-18 21:56:30 +0100 (Fri, 18 Jan 2008) | 3 lines Fix an edge case whereby the __del__() method of a classic class could create a new weakref to the object. ........ r60058 | raymond.hettinger | 2008-01-18 22:14:58 +0100 (Fri, 18 Jan 2008) | 1 line Better variable name in an example. ........ r60063 | guido.van.rossum | 2008-01-19 00:05:40 +0100 (Sat, 19 Jan 2008) | 3 lines This got fixed for classic classes in r60057, and backported to 2.5.2 in 60056. ........ r60068 | jeffrey.yasskin | 2008-01-19 10:56:06 +0100 (Sat, 19 Jan 2008) | 4 lines Several tweaks: add construction from strings and .from_decimal(), change __init__ to __new__ to enforce immutability, and remove "rational." from repr and the parens from str. ........ r60069 | georg.brandl | 2008-01-19 11:11:27 +0100 (Sat, 19 Jan 2008) | 2 lines Fix markup. ........ r60070 | georg.brandl | 2008-01-19 11:16:09 +0100 (Sat, 19 Jan 2008) | 3 lines Amend curses docs by info how to write non-ascii characters. Thanks to Jeroen Ruigrok van der Werven. ........ r60071 | georg.brandl | 2008-01-19 11:18:07 +0100 (Sat, 19 Jan 2008) | 2 lines Indentation normalization. ........ r60073 | facundo.batista | 2008-01-19 13:32:27 +0100 (Sat, 19 Jan 2008) | 5 lines Fix issue #1822: MIMEMultipart.is_multipart() behaves correctly for a just-created (and empty) instance. Added tests for this. Thanks Jonathan Share. ........ r60074 | andrew.kuchling | 2008-01-19 14:33:20 +0100 (Sat, 19 Jan 2008) | 1 line Polish sentence ........ r60075 | christian.heimes | 2008-01-19 14:46:06 +0100 (Sat, 19 Jan 2008) | 1 line Added unit test to verify that threading.local doesn't cause ref leaks. It seems that the thread local storage always keeps the storage of the last stopped thread alive. Can anybody comment on it, please? ........ r60076 | christian.heimes | 2008-01-19 16:06:09 +0100 (Sat, 19 Jan 2008) | 1 line Update for threading.local test. ........ r60077 | andrew.kuchling | 2008-01-19 16:16:37 +0100 (Sat, 19 Jan 2008) | 1 line Polish sentence ........ r60078 | georg.brandl | 2008-01-19 16:22:16 +0100 (Sat, 19 Jan 2008) | 2 lines Fix typos. ........
Diffstat (limited to 'Lib')
-rw-r--r--Lib/email/mime/multipart.py6
-rw-r--r--Lib/email/test/test_email.py3
-rwxr-xr-xLib/rational.py76
-rw-r--r--Lib/test/crashers/weakref_in_del.py17
-rw-r--r--Lib/test/test_rational.py69
-rw-r--r--Lib/test/test_threading_local.py44
6 files changed, 182 insertions, 33 deletions
diff --git a/Lib/email/mime/multipart.py b/Lib/email/mime/multipart.py
index 5c8c9db..9661865 100644
--- a/Lib/email/mime/multipart.py
+++ b/Lib/email/mime/multipart.py
@@ -34,6 +34,12 @@ class MIMEMultipart(MIMEBase):
keyword arguments (or passed into the _params argument).
"""
MIMEBase.__init__(self, 'multipart', _subtype, **_params)
+
+ # Initialise _payload to an empty list as the Message superclass's
+ # implementation of is_multipart assumes that _payload is a list for
+ # multipart messages.
+ self._payload = []
+
if _subparts:
for p in _subparts:
self.attach(p)
diff --git a/Lib/email/test/test_email.py b/Lib/email/test/test_email.py
index c544004..1ca41e9 100644
--- a/Lib/email/test/test_email.py
+++ b/Lib/email/test/test_email.py
@@ -1892,6 +1892,9 @@ message 2
eq(msg.get_payload(0), text1)
eq(msg.get_payload(1), text2)
+ def test_default_multipart_constructor(self):
+ msg = MIMEMultipart()
+ self.assertTrue(msg.is_multipart())
# A general test of parser->model->generator idempotency. IOW, read a message
diff --git a/Lib/rational.py b/Lib/rational.py
index c913ec7..71ffff7 100755
--- a/Lib/rational.py
+++ b/Lib/rational.py
@@ -6,6 +6,7 @@
import math
import numbers
import operator
+import re
__all__ = ["Rational"]
@@ -75,6 +76,10 @@ def _binary_float_to_ratio(x):
return (top, 2 ** -e)
+_RATIONAL_FORMAT = re.compile(
+ r'^\s*(?P<sign>[-+]?)(?P<num>\d+)(?:/(?P<denom>\d+))?\s*$')
+
+
class Rational(RationalAbc):
"""This class implements rational numbers.
@@ -83,18 +88,41 @@ class Rational(RationalAbc):
and the denominator defaults to 1 so that Rational(3) == 3 and
Rational() == 0.
+ Rationals can also be constructed from strings of the form
+ '[-+]?[0-9]+(/[0-9]+)?', optionally surrounded by spaces.
+
"""
__slots__ = ('_numerator', '_denominator')
- def __init__(self, numerator=0, denominator=1):
- if (not isinstance(numerator, numbers.Integral) and
- isinstance(numerator, RationalAbc) and
- denominator == 1):
- # Handle copies from other rationals.
- other_rational = numerator
- numerator = other_rational.numerator
- denominator = other_rational.denominator
+ # We're immutable, so use __new__ not __init__
+ def __new__(cls, numerator=0, denominator=1):
+ """Constructs a Rational.
+
+ Takes a string, another Rational, or a numerator/denominator pair.
+
+ """
+ self = super(Rational, cls).__new__(cls)
+
+ if denominator == 1:
+ if isinstance(numerator, str):
+ # Handle construction from strings.
+ input = numerator
+ m = _RATIONAL_FORMAT.match(input)
+ if m is None:
+ raise ValueError('Invalid literal for Rational: ' + input)
+ numerator = int(m.group('num'))
+ # Default denominator to 1. That's the only optional group.
+ denominator = int(m.group('denom') or 1)
+ if m.group('sign') == '-':
+ numerator = -numerator
+
+ elif (not isinstance(numerator, numbers.Integral) and
+ isinstance(numerator, RationalAbc)):
+ # Handle copies from other rationals.
+ other_rational = numerator
+ numerator = other_rational.numerator
+ denominator = other_rational.denominator
if (not isinstance(numerator, numbers.Integral) or
not isinstance(denominator, numbers.Integral)):
@@ -107,10 +135,15 @@ class Rational(RationalAbc):
g = _gcd(numerator, denominator)
self._numerator = int(numerator // g)
self._denominator = int(denominator // g)
+ return self
@classmethod
def from_float(cls, f):
- """Converts a float to a rational number, exactly."""
+ """Converts a finite float to a rational number, exactly.
+
+ Beware that Rational.from_float(0.3) != Rational(3, 10).
+
+ """
if not isinstance(f, float):
raise TypeError("%s.from_float() only takes floats, not %r (%s)" %
(cls.__name__, f, type(f).__name__))
@@ -118,6 +151,26 @@ class Rational(RationalAbc):
raise TypeError("Cannot convert %r to %s." % (f, cls.__name__))
return cls(*_binary_float_to_ratio(f))
+ @classmethod
+ def from_decimal(cls, dec):
+ """Converts a finite Decimal instance to a rational number, exactly."""
+ from decimal import Decimal
+ if not isinstance(dec, Decimal):
+ raise TypeError(
+ "%s.from_decimal() only takes Decimals, not %r (%s)" %
+ (cls.__name__, dec, type(dec).__name__))
+ if not dec.is_finite():
+ # Catches infinities and nans.
+ raise TypeError("Cannot convert %s to %s." % (dec, cls.__name__))
+ sign, digits, exp = dec.as_tuple()
+ digits = int(''.join(map(str, digits)))
+ if sign:
+ digits = -digits
+ if exp >= 0:
+ return cls(digits * 10 ** exp)
+ else:
+ return cls(digits, 10 ** -exp)
+
@property
def numerator(a):
return a._numerator
@@ -128,15 +181,14 @@ class Rational(RationalAbc):
def __repr__(self):
"""repr(self)"""
- return ('rational.Rational(%r,%r)' %
- (self.numerator, self.denominator))
+ return ('Rational(%r,%r)' % (self.numerator, self.denominator))
def __str__(self):
"""str(self)"""
if self.denominator == 1:
return str(self.numerator)
else:
- return '(%s/%s)' % (self.numerator, self.denominator)
+ return '%s/%s' % (self.numerator, self.denominator)
def _operator_fallbacks(monomorphic_operator, fallback_operator):
"""Generates forward and reverse operators given a purely-rational
diff --git a/Lib/test/crashers/weakref_in_del.py b/Lib/test/crashers/weakref_in_del.py
deleted file mode 100644
index 2e9b186..0000000
--- a/Lib/test/crashers/weakref_in_del.py
+++ /dev/null
@@ -1,17 +0,0 @@
-import weakref
-
-# http://python.org/sf/1377858
-# Fixed for new-style classes in 2.5c1.
-
-ref = None
-
-def test_weakref_in_del():
- class Target():
- def __del__(self):
- global ref
- ref = weakref.ref(self)
-
- w = Target()
-
-if __name__ == '__main__':
- test_weakref_in_del()
diff --git a/Lib/test/test_rational.py b/Lib/test/test_rational.py
index 952a97f..e57adce 100644
--- a/Lib/test/test_rational.py
+++ b/Lib/test/test_rational.py
@@ -45,6 +45,44 @@ class RationalTest(unittest.TestCase):
self.assertRaises(TypeError, R, 1.5)
self.assertRaises(TypeError, R, 1.5 + 3j)
+ self.assertRaises(TypeError, R, R(1, 2), 3)
+ self.assertRaises(TypeError, R, "3/2", 3)
+
+ def testFromString(self):
+ self.assertEquals((5, 1), _components(R("5")))
+ self.assertEquals((3, 2), _components(R("3/2")))
+ self.assertEquals((3, 2), _components(R(" \n +3/2")))
+ self.assertEquals((-3, 2), _components(R("-3/2 ")))
+ self.assertEquals((3, 2), _components(R(" 03/02 \n ")))
+ self.assertEquals((3, 2), _components(R(" 03/02 \n ")))
+
+ self.assertRaisesMessage(
+ ZeroDivisionError, "Rational(3, 0)",
+ R, "3/0")
+ self.assertRaisesMessage(
+ ValueError, "Invalid literal for Rational: 3/",
+ R, "3/")
+ self.assertRaisesMessage(
+ ValueError, "Invalid literal for Rational: 3 /2",
+ R, "3 /2")
+ self.assertRaisesMessage(
+ # Denominators don't need a sign.
+ ValueError, "Invalid literal for Rational: 3/+2",
+ R, "3/+2")
+ self.assertRaisesMessage(
+ # Imitate float's parsing.
+ ValueError, "Invalid literal for Rational: + 3/2",
+ R, "+ 3/2")
+ self.assertRaisesMessage(
+ # Only parse fractions, not decimals.
+ ValueError, "Invalid literal for Rational: 3.2",
+ R, "3.2")
+
+ def testImmutable(self):
+ r = R(7, 3)
+ r.__init__(2, 15)
+ self.assertEquals((7, 3), _components(r))
+
def testFromFloat(self):
self.assertRaisesMessage(
TypeError, "Rational.from_float() only takes floats, not 3 (int)",
@@ -72,6 +110,31 @@ class RationalTest(unittest.TestCase):
TypeError, "Cannot convert nan to Rational.",
R.from_float, nan)
+ def testFromDecimal(self):
+ self.assertRaisesMessage(
+ TypeError,
+ "Rational.from_decimal() only takes Decimals, not 3 (int)",
+ R.from_decimal, 3)
+ self.assertEquals(R(0), R.from_decimal(Decimal("-0")))
+ self.assertEquals(R(5, 10), R.from_decimal(Decimal("0.5")))
+ self.assertEquals(R(5, 1000), R.from_decimal(Decimal("5e-3")))
+ self.assertEquals(R(5000), R.from_decimal(Decimal("5e3")))
+ self.assertEquals(1 - R(1, 10**30),
+ R.from_decimal(Decimal("0." + "9" * 30)))
+
+ self.assertRaisesMessage(
+ TypeError, "Cannot convert Infinity to Rational.",
+ R.from_decimal, Decimal("inf"))
+ self.assertRaisesMessage(
+ TypeError, "Cannot convert -Infinity to Rational.",
+ R.from_decimal, Decimal("-inf"))
+ self.assertRaisesMessage(
+ TypeError, "Cannot convert NaN to Rational.",
+ R.from_decimal, Decimal("nan"))
+ self.assertRaisesMessage(
+ TypeError, "Cannot convert sNaN to Rational.",
+ R.from_decimal, Decimal("snan"))
+
def testConversions(self):
self.assertTypedEquals(-1, trunc(R(-11, 10)))
self.assertTypedEquals(-2, math.floor(R(-11, 10)))
@@ -175,7 +238,7 @@ class RationalTest(unittest.TestCase):
self.assertTypedEquals(1.0 + 0j, (1.0 + 0j) ** R(1, 10))
def testMixingWithDecimal(self):
- """Decimal refuses mixed comparisons."""
+ # Decimal refuses mixed comparisons.
self.assertRaisesMessage(
TypeError,
"unsupported operand type(s) for +: 'Rational' and 'Decimal'",
@@ -238,8 +301,8 @@ class RationalTest(unittest.TestCase):
self.assertFalse(R(5, 2) == 2)
def testStringification(self):
- self.assertEquals("rational.Rational(7,3)", repr(R(7, 3)))
- self.assertEquals("(7/3)", str(R(7, 3)))
+ self.assertEquals("Rational(7,3)", repr(R(7, 3)))
+ self.assertEquals("7/3", str(R(7, 3)))
self.assertEquals("7", str(R(7, 1)))
def testHash(self):
diff --git a/Lib/test/test_threading_local.py b/Lib/test/test_threading_local.py
index 0aaedbc..b7dd538 100644
--- a/Lib/test/test_threading_local.py
+++ b/Lib/test/test_threading_local.py
@@ -1,9 +1,51 @@
import unittest
from doctest import DocTestSuite
from test import test_support
+import threading
+import weakref
+import gc
+
+class Weak(object):
+ pass
+
+def target(local, weaklist):
+ weak = Weak()
+ local.weak = weak
+ weaklist.append(weakref.ref(weak))
+
+class ThreadingLocalTest(unittest.TestCase):
+
+ def test_local_refs(self):
+ self._local_refs(20)
+ self._local_refs(50)
+ self._local_refs(100)
+
+ def _local_refs(self, n):
+ local = threading.local()
+ weaklist = []
+ for i in range(n):
+ t = threading.Thread(target=target, args=(local, weaklist))
+ t.start()
+ t.join()
+ del t
+
+ gc.collect()
+ self.assertEqual(len(weaklist), n)
+
+ # XXX threading.local keeps the local of the last stopped thread alive.
+ deadlist = [weak for weak in weaklist if weak() is None]
+ self.assertEqual(len(deadlist), n-1)
+
+ # Assignment to the same thread local frees it sometimes (!)
+ local.someothervar = None
+ gc.collect()
+ deadlist = [weak for weak in weaklist if weak() is None]
+ self.assert_(len(deadlist) in (n-1, n), (n, len(deadlist)))
def test_main():
- suite = DocTestSuite('_threading_local')
+ suite = unittest.TestSuite()
+ suite.addTest(DocTestSuite('_threading_local'))
+ suite.addTest(unittest.makeSuite(ThreadingLocalTest))
try:
from thread import _local