diff options
author | Nick Coghlan <ncoghlan@gmail.com> | 2010-11-21 03:44:04 (GMT) |
---|---|---|
committer | Nick Coghlan <ncoghlan@gmail.com> | 2010-11-21 03:44:04 (GMT) |
commit | e0f04659cd717a031b42dcfd43ca07824b24f9b2 (patch) | |
tree | 63fbd71f11a93bb398b5d1f6c2d91b116bc7d78e /Lib/test/test_inspect.py | |
parent | d3309df40b5335ebf225a359e5cb3b324801e221 (diff) | |
download | cpython-e0f04659cd717a031b42dcfd43ca07824b24f9b2.zip cpython-e0f04659cd717a031b42dcfd43ca07824b24f9b2.tar.gz cpython-e0f04659cd717a031b42dcfd43ca07824b24f9b2.tar.bz2 |
Issue #10220: Add inspect.getgeneratorstate(). Initial patch by Rodolpho Eckhardt
Diffstat (limited to 'Lib/test/test_inspect.py')
-rw-r--r-- | Lib/test/test_inspect.py | 47 |
1 files changed, 46 insertions, 1 deletions
diff --git a/Lib/test/test_inspect.py b/Lib/test/test_inspect.py index cba6f13..97c47ac 100644 --- a/Lib/test/test_inspect.py +++ b/Lib/test/test_inspect.py @@ -887,12 +887,57 @@ class TestGetattrStatic(unittest.TestCase): self.assertEqual(inspect.getattr_static(Something, 'foo'), 3) +class TestGetGeneratorState(unittest.TestCase): + + def setUp(self): + def number_generator(): + for number in range(5): + yield number + self.generator = number_generator() + + def _generatorstate(self): + return inspect.getgeneratorstate(self.generator) + + def test_created(self): + self.assertEqual(self._generatorstate(), inspect.GEN_CREATED) + + def test_suspended(self): + next(self.generator) + self.assertEqual(self._generatorstate(), inspect.GEN_SUSPENDED) + + def test_closed_after_exhaustion(self): + for i in self.generator: + pass + self.assertEqual(self._generatorstate(), inspect.GEN_CLOSED) + + def test_closed_after_immediate_exception(self): + with self.assertRaises(RuntimeError): + self.generator.throw(RuntimeError) + self.assertEqual(self._generatorstate(), inspect.GEN_CLOSED) + + def test_running(self): + # As mentioned on issue #10220, checking for the RUNNING state only + # makes sense inside the generator itself. + # The following generator checks for this by using the closure's + # reference to self and the generator state checking helper method + def running_check_generator(): + for number in range(5): + self.assertEqual(self._generatorstate(), inspect.GEN_RUNNING) + yield number + self.assertEqual(self._generatorstate(), inspect.GEN_RUNNING) + self.generator = running_check_generator() + # Running up to the first yield + next(self.generator) + # Running after the first yield + next(self.generator) + + def test_main(): run_unittest( TestDecorators, TestRetrievingSourceCode, TestOneliners, TestBuggyCases, TestInterpreterStack, TestClassesAndFunctions, TestPredicates, TestGetcallargsFunctions, TestGetcallargsMethods, - TestGetcallargsUnboundMethods, TestGetattrStatic + TestGetcallargsUnboundMethods, TestGetattrStatic, TestGetGeneratorState ) if __name__ == "__main__": |