]> granicus.if.org Git - python/commitdiff
Issue 4998: __slots__ on Fractions was useless.
authorRaymond Hettinger <python@rcn.com>
Tue, 20 Jan 2009 20:34:19 +0000 (20:34 +0000)
committerRaymond Hettinger <python@rcn.com>
Tue, 20 Jan 2009 20:34:19 +0000 (20:34 +0000)
Lib/numbers.py
Lib/test/test_fractions.py
Misc/NEWS

index fa59fd8e7de93d68a37021269165d27c51649b01..540e7d59107a17fa84f9acfd687f7bf49dbd55d2 100644 (file)
@@ -17,6 +17,7 @@ class Number(object):
     caring what kind, use isinstance(x, Number).
     """
     __metaclass__ = ABCMeta
+    __slots__ = ()
 
     # Concrete numeric types must provide their own hash implementation
     __hash__ = None
@@ -41,6 +42,8 @@ class Complex(Number):
     type as described below.
     """
 
+    __slots__ = ()
+
     @abstractmethod
     def __complex__(self):
         """Return a builtin complex instance. Called for complex(self)."""
@@ -172,6 +175,8 @@ class Real(Complex):
     Real also provides defaults for the derived operations.
     """
 
+    __slots__ = ()
+
     @abstractmethod
     def __float__(self):
         """Any Real can be converted to a native float object.
@@ -265,6 +270,8 @@ Real.register(float)
 class Rational(Real):
     """.numerator and .denominator should be in lowest terms."""
 
+    __slots__ = ()
+
     @abstractproperty
     def numerator(self):
         raise NotImplementedError
@@ -288,6 +295,8 @@ class Rational(Real):
 class Integral(Rational):
     """Integral adds a conversion to long and the bit-string operations."""
 
+    __slots__ = ()
+
     @abstractmethod
     def __long__(self):
         """long(self)"""
index a6c3c32cb09e271dd83b8655b33740950f9fab55..0509f92ccbcedb058480f30adf1ce6c835498ac5 100644 (file)
@@ -394,6 +394,11 @@ class FractionTest(unittest.TestCase):
         self.assertEqual(id(r), id(copy(r)))
         self.assertEqual(id(r), id(deepcopy(r)))
 
+    def test_slots(self):
+        # Issue 4998
+        r = F(13, 7)
+        self.assertRaises(AttributeError, setattr, r, 'a', 10)
+
 def test_main():
     run_unittest(FractionTest, GcdTest)
 
index f8bfa005e96616415b2aa66cd66cc71d073f040c..9d234bdc2f748bd4842f348711345ce4e228af50 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -145,6 +145,10 @@ Core and Builtins
 Library
 -------
 
+- Issue #4998: The memory saving effect of __slots__ had been lost on Fractions
+  which inherited from numbers.py which did not have __slots__ defined.  The
+  numbers heirarchy now has its own __slots__ declarations.
+
 - Issue #3321: _multiprocessing.Connection() doesn't check handle; added checks
   for *nix machines for negative handles and large int handles. Without this check
   it is possible to segfault the interpreter.