
import gc
import unittest
import sys

from traits.has_traits import HasTraits


def _create_subclass():
    class Subclass(HasTraits):
        pass
    return Subclass


class TestRegression(unittest.TestCase):

    def test_subclasses_weakref(self):
        """ Make sure that dynamically created subclasses are not held
        strongly by HasTraits.
        """
        previous_subclasses = HasTraits.__subclasses__()
        _create_subclass()
        _create_subclass()
        _create_subclass()
        _create_subclass()
        gc.collect()
        self.assertEqual(previous_subclasses, HasTraits.__subclasses__())


import gc
import unittest
import sys

from traits.has_traits import HasTraits


def _create_subclass():
    class Subclass(HasTraits):
        pass
    return Subclass


class TestRegression(unittest.TestCase):

    def test_subclasses_weakref(self):
        """ Make sure that dynamically created subclasses are not held
        strongly by HasTraits.
        """
        previous_subclasses = HasTraits.__subclasses__()
        _create_subclass()
        _create_subclass()
        _create_subclass()
        _create_subclass()
        gc.collect()
        self.assertEqual(previous_subclasses, HasTraits.__subclasses__())

    def test_leaked_property_tuple(self):
        """ the property ctrait constructor shouldn't leak a tuple. """
        class A(HasTraits):
            prop = Property()
        a = A()
        self.assertEqual(sys.getrefcount(a.trait('prop').property()), 1)


if __name__ == '__main__':
    unittest.main()

Add a test case for the tuple leak in ctraits property.
Add a test case for the tuple leak in ctraits property.

Courtesy Robert Kern.

  
  import gc
  import unittest
  import sys
  
  from traits.has_traits import HasTraits
  
  
  def _create_subclass():
      class Subclass(HasTraits):
          pass
      return Subclass
  
  
  class TestRegression(unittest.TestCase):
  
      def test_subclasses_weakref(self):
          """ Make sure that dynamically created subclasses are not held
          strongly by HasTraits.
          """
          previous_subclasses = HasTraits.__subclasses__()
          _create_subclass()
          _create_subclass()
          _create_subclass()
          _create_subclass()
          gc.collect()
          self.assertEqual(previous_subclasses, HasTraits.__subclasses__())
  
+     def test_leaked_property_tuple(self):
+         """ the property ctrait constructor shouldn't leak a tuple. """
+         class A(HasTraits):
+             prop = Property()
+         a = A()
+         self.assertEqual(sys.getrefcount(a.trait('prop').property()), 1)
+ 
+ 
+ if __name__ == '__main__':
+     unittest.main()
+ 
Add a test case for the tuple leak in ctraits property.
## Code Before:

import gc
import unittest
import sys

from traits.has_traits import HasTraits


def _create_subclass():
    class Subclass(HasTraits):
        pass
    return Subclass


class TestRegression(unittest.TestCase):

    def test_subclasses_weakref(self):
        """ Make sure that dynamically created subclasses are not held
        strongly by HasTraits.
        """
        previous_subclasses = HasTraits.__subclasses__()
        _create_subclass()
        _create_subclass()
        _create_subclass()
        _create_subclass()
        gc.collect()
        self.assertEqual(previous_subclasses, HasTraits.__subclasses__())

## Instruction:
Add a test case for the tuple leak in ctraits property.
## Code After:

import gc
import unittest
import sys

from traits.has_traits import HasTraits


def _create_subclass():
    class Subclass(HasTraits):
        pass
    return Subclass


class TestRegression(unittest.TestCase):

    def test_subclasses_weakref(self):
        """ Make sure that dynamically created subclasses are not held
        strongly by HasTraits.
        """
        previous_subclasses = HasTraits.__subclasses__()
        _create_subclass()
        _create_subclass()
        _create_subclass()
        _create_subclass()
        gc.collect()
        self.assertEqual(previous_subclasses, HasTraits.__subclasses__())

    def test_leaked_property_tuple(self):
        """ the property ctrait constructor shouldn't leak a tuple. """
        class A(HasTraits):
            prop = Property()
        a = A()
        self.assertEqual(sys.getrefcount(a.trait('prop').property()), 1)


if __name__ == '__main__':
    unittest.main()
