| 1 | from pypy.module.cpyext.test.test_cpyext import AppTestCpythonExtensionBase |
| 2 | from pypy.module.cpyext.test.test_api import BaseApiTest |
| 3 | from pypy.module.cpyext.boolobject import PyBool_FromLong |
| 4 | |
| 5 | class TestBoolObject(BaseApiTest): |
| 6 | def test_fromlong(self, space): |
| 7 | for i in range(-3, 3): |
| 8 | obj = PyBool_FromLong(space, i) |
| 9 | if i: |
| 10 | assert obj is space.w_True |
| 11 | else: |
| 12 | assert obj is space.w_False |
| 13 | |
| 14 | class AppTestBoolMacros(AppTestCpythonExtensionBase): |
| 15 | def test_macros(self): |
| 16 | module = self.import_extension('foo', [ |
| 17 | ("get_true", "METH_NOARGS", "Py_RETURN_TRUE;"), |
| 18 | ("get_false", "METH_NOARGS", "Py_RETURN_FALSE;"), |
| 19 | ]) |
| 20 | assert module.get_true() == True |
| 21 | assert module.get_false() == False |
| 22 | |
| 23 | def test_toint(self): |
| 24 | module = self.import_extension('foo', [ |
| 25 | ("to_int", "METH_O", |
| 26 | ''' |
| 27 | if (args->ob_type->tp_as_number && args->ob_type->tp_as_number->nb_int) { |
| 28 | return args->ob_type->tp_as_number->nb_int(args); |
| 29 | } |
| 30 | else { |
| 31 | PyErr_SetString(PyExc_TypeError,"cannot convert bool to int"); |
| 32 | return NULL; |
| 33 | } |
| 34 | '''), ]) |
| 35 | assert module.to_int(False) == 0 |
| 36 | assert module.to_int(True) == 1 |
| 37 | |
| 38 | def test_check(self): |
| 39 | module = self.import_extension('foo', [ |
| 40 | ("type_check", "METH_O", |
| 41 | ''' |
| 42 | return PyLong_FromLong(PyBool_Check(args)); |
| 43 | ''')]) |
| 44 | assert module.type_check(True) |
| 45 | assert module.type_check(False) |
| 46 | assert not module.type_check(None) |
| 47 | assert not module.type_check(1.0) |
| 48 |