PHP 8.2.30
Preview: test_extension_interface.py Size: 3.74 KB
/opt/hc_python/lib64/python3.12/site-packages/greenlet/tests/test_extension_interface.py

from __future__ import print_function
from __future__ import absolute_import

import sys

import greenlet
from . import _test_extension
from . import TestCase

# pylint:disable=c-extension-no-member

class CAPITests(TestCase):
    def test_switch(self):
        self.assertEqual(
            50, _test_extension.test_switch(greenlet.greenlet(lambda: 50)))

    def test_switch_kwargs(self):
        def adder(x, y):
            return x * y
        g = greenlet.greenlet(adder)
        self.assertEqual(6, _test_extension.test_switch_kwargs(g, x=3, y=2))

    def test_setparent(self):
        # pylint:disable=disallowed-name
        def foo():
            def bar():
                greenlet.getcurrent().parent.switch()

                # This final switch should go back to the main greenlet, since
                # the test_setparent() function in the C extension should have
                # reparented this greenlet.
                greenlet.getcurrent().parent.switch()
                raise AssertionError("Should never have reached this code")
            child = greenlet.greenlet(bar)
            child.switch()
            greenlet.getcurrent().parent.switch(child)
            greenlet.getcurrent().parent.throw(
                AssertionError("Should never reach this code"))
        foo_child = greenlet.greenlet(foo).switch()
        self.assertEqual(None, _test_extension.test_setparent(foo_child))

    def test_getcurrent(self):
        _test_extension.test_getcurrent()

    def test_new_greenlet(self):
        self.assertEqual(-15, _test_extension.test_new_greenlet(lambda: -15))

    def test_raise_greenlet_dead(self):
        self.assertRaises(
            greenlet.GreenletExit, _test_extension.test_raise_dead_greenlet)

    def test_raise_greenlet_error(self):
        self.assertRaises(
            greenlet.error, _test_extension.test_raise_greenlet_error)

    def test_throw(self):
        seen = []

        def foo():         # pylint:disable=disallowed-name
            try:
                greenlet.getcurrent().parent.switch()
            except ValueError:
                seen.append(sys.exc_info()[1])
            except greenlet.GreenletExit:
                raise AssertionError
        g = greenlet.greenlet(foo)
        g.switch()
        _test_extension.test_throw(g)
        self.assertEqual(len(seen), 1)
        self.assertTrue(
            isinstance(seen[0], ValueError),
            "ValueError was not raised in foo()")
        self.assertEqual(
            str(seen[0]),
            'take that sucka!',
            "message doesn't match")

    def test_non_traceback_param(self):
        with self.assertRaises(TypeError) as exc:
            _test_extension.test_throw_exact(
                greenlet.getcurrent(),
                Exception,
                Exception(),
                self
            )
        self.assertEqual(str(exc.exception),
                         "throw() third argument must be a traceback object")

    def test_instance_of_wrong_type(self):
        with self.assertRaises(TypeError) as exc:
            _test_extension.test_throw_exact(
                greenlet.getcurrent(),
                Exception(),
                BaseException(),
                None,
            )

        self.assertEqual(str(exc.exception),
                         "instance exception may not have a separate value")

    def test_not_throwable(self):
        with self.assertRaises(TypeError) as exc:
            _test_extension.test_throw_exact(
                greenlet.getcurrent(),
                "abc",
                None,
                None,
            )
        self.assertEqual(str(exc.exception),
                         "exceptions must be classes, or instances, not str")


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

Directory Contents

Dirs: 1 × Files: 27

Name Size Perms Modified Actions
- drwxr-xr-x 2025-12-03 08:02:54
Edit Download
1.23 KB lrw-r--r-- 2025-12-03 08:02:54
Edit Download
985 B lrw-r--r-- 2025-12-03 08:02:54
Edit Download
1.92 KB lrw-r--r-- 2025-12-03 08:02:54
Edit Download
524 B lrw-r--r-- 2025-12-03 08:02:54
Edit Download
956 B lrw-r--r-- 2025-12-03 08:02:54
Edit Download
1.25 KB lrw-r--r-- 2025-12-03 08:02:54
Edit Download
817 B lrw-r--r-- 2025-12-03 08:02:54
Edit Download
12.32 KB lrw-r--r-- 2025-12-03 08:02:54
Edit Download
10.29 KB lrw-r--r-- 2025-12-03 08:02:54
Edit Download
2.67 KB lrw-r--r-- 2025-12-03 08:02:54
Edit Download
3.74 KB lrw-r--r-- 2025-12-03 08:02:54
Edit Download
2.85 KB lrw-r--r-- 2025-12-03 08:02:54
Edit Download
1.21 KB lrw-r--r-- 2025-12-03 08:02:54
Edit Download
3.63 KB lrw-r--r-- 2025-12-03 08:02:54
Edit Download
46.87 KB lrw-r--r-- 2025-12-03 08:02:54
Edit Download
7.76 KB lrw-r--r-- 2025-12-03 08:02:54
Edit Download
17.65 KB lrw-r--r-- 2025-12-03 08:02:54
Edit Download
446 B lrw-r--r-- 2025-12-03 08:02:54
Edit Download
3.63 KB lrw-r--r-- 2025-12-03 08:02:54
Edit Download
8.39 KB lrw-r--r-- 2025-12-03 08:02:54
Edit Download
1.31 KB lrw-r--r-- 2025-12-03 08:02:54
Edit Download
883 B lrw-r--r-- 2025-12-03 08:02:54
Edit Download
5.64 KB lrw-r--r-- 2025-12-03 08:02:54
Edit Download
16.85 KB lrwxr-xr-x 2025-12-03 08:02:54
Edit Download
6.41 KB lrw-r--r-- 2025-12-03 08:02:54
Edit Download
57.02 KB lrwxr-xr-x 2025-12-03 08:02:54
Edit Download
9.51 KB lrw-r--r-- 2025-12-03 08:02:54
Edit Download

If ZipArchive is unavailable, a .tar will be created (no compression).