| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| import enum | ||
| import unittest | ||
| from test.support import import_helper | ||
|
|
||
| _testlimitedcapi = import_helper.import_module('_testlimitedcapi') | ||
|
|
||
|
|
||
| class Constant(enum.IntEnum): | ||
| Py_CONSTANT_NONE = 0 | ||
| Py_CONSTANT_FALSE = 1 | ||
| Py_CONSTANT_TRUE = 2 | ||
| Py_CONSTANT_ELLIPSIS = 3 | ||
| Py_CONSTANT_NOT_IMPLEMENTED = 4 | ||
| Py_CONSTANT_ZERO = 5 | ||
| Py_CONSTANT_ONE = 6 | ||
| Py_CONSTANT_EMPTY_STR = 7 | ||
| Py_CONSTANT_EMPTY_BYTES = 8 | ||
| Py_CONSTANT_EMPTY_TUPLE = 9 | ||
|
|
||
| INVALID_CONSTANT = Py_CONSTANT_EMPTY_TUPLE + 1 | ||
|
|
||
|
|
||
| class CAPITest(unittest.TestCase): | ||
| def check_get_constant(self, get_constant): | ||
| self.assertIs(get_constant(Constant.Py_CONSTANT_NONE), None) | ||
| self.assertIs(get_constant(Constant.Py_CONSTANT_FALSE), False) | ||
| self.assertIs(get_constant(Constant.Py_CONSTANT_TRUE), True) | ||
| self.assertIs(get_constant(Constant.Py_CONSTANT_ELLIPSIS), Ellipsis) | ||
| self.assertIs(get_constant(Constant.Py_CONSTANT_NOT_IMPLEMENTED), NotImplemented) | ||
|
|
||
| for constant_id, constant_type, value in ( | ||
| (Constant.Py_CONSTANT_ZERO, int, 0), | ||
| (Constant.Py_CONSTANT_ONE, int, 1), | ||
| (Constant.Py_CONSTANT_EMPTY_STR, str, ""), | ||
| (Constant.Py_CONSTANT_EMPTY_BYTES, bytes, b""), | ||
| (Constant.Py_CONSTANT_EMPTY_TUPLE, tuple, ()), | ||
| ): | ||
| with self.subTest(constant_id=constant_id): | ||
| obj = get_constant(constant_id) | ||
| self.assertEqual(type(obj), constant_type, obj) | ||
| self.assertEqual(obj, value) | ||
|
|
||
| with self.assertRaises(SystemError): | ||
| get_constant(Constant.INVALID_CONSTANT) | ||
|
|
||
| def test_get_constant(self): | ||
| self.check_get_constant(_testlimitedcapi.get_constant) | ||
|
|
||
| def test_get_constant_borrowed(self): | ||
| self.check_get_constant(_testlimitedcapi.get_constant_borrowed) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| Add :c:func:`Py_GetConstant` and :c:func:`Py_GetConstantBorrowed` functions to | ||
| get constants. For example, ``Py_GetConstant(Py_CONSTANT_ZERO)`` returns a | ||
| :term:`strong reference` to the constant zero. Patch by Victor Stinner. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| In the limited C API version 3.13, getting ``Py_None``, ``Py_False``, | ||
| ``Py_True``, ``Py_Ellipsis`` and ``Py_NotImplemented`` singletons is now | ||
| implemented as function calls at the stable ABI level to hide implementation | ||
| details. Getting these constants still return borrowed references. Patch by | ||
| Victor Stinner. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| // Need limited C API version 3.13 for Py_GetConstant() | ||
| #include "pyconfig.h" // Py_GIL_DISABLED | ||
| #if !defined(Py_GIL_DISABLED) && !defined(Py_LIMITED_API ) | ||
| # define Py_LIMITED_API 0x030d0000 | ||
| #endif | ||
|
|
||
| #include "parts.h" | ||
| #include "util.h" | ||
|
|
||
|
|
||
| /* Test Py_GetConstant() */ | ||
| static PyObject * | ||
| get_constant(PyObject *Py_UNUSED(module), PyObject *args) | ||
| { | ||
| int constant_id; | ||
| if (!PyArg_ParseTuple(args, "i", &constant_id)) { | ||
| return NULL; | ||
| } | ||
|
|
||
| PyObject *obj = Py_GetConstant(constant_id); | ||
| if (obj == NULL) { | ||
| assert(PyErr_Occurred()); | ||
| return NULL; | ||
| } | ||
| return obj; | ||
| } | ||
|
|
||
|
|
||
| /* Test Py_GetConstantBorrowed() */ | ||
| static PyObject * | ||
| get_constant_borrowed(PyObject *Py_UNUSED(module), PyObject *args) | ||
| { | ||
| int constant_id; | ||
| if (!PyArg_ParseTuple(args, "i", &constant_id)) { | ||
| return NULL; | ||
| } | ||
|
|
||
| PyObject *obj = Py_GetConstantBorrowed(constant_id); | ||
| if (obj == NULL) { | ||
| assert(PyErr_Occurred()); | ||
| return NULL; | ||
| } | ||
| return Py_NewRef(obj); | ||
| } | ||
|
|
||
|
|
||
| /* Test constants */ | ||
| static PyObject * | ||
| test_constants(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args)) | ||
| { | ||
| // Test that implementation of constants in the limited C API: | ||
| // check that the C code compiles. | ||
| // | ||
| // Test also that constants and Py_GetConstant() return the same | ||
| // objects. | ||
| assert(Py_None == Py_GetConstant(Py_CONSTANT_NONE)); | ||
| assert(Py_False == Py_GetConstant(Py_CONSTANT_FALSE)); | ||
| assert(Py_True == Py_GetConstant(Py_CONSTANT_TRUE)); | ||
| assert(Py_Ellipsis == Py_GetConstant(Py_CONSTANT_ELLIPSIS)); | ||
| assert(Py_NotImplemented == Py_GetConstant(Py_CONSTANT_NOT_IMPLEMENTED)); | ||
| // Other constants are tested in test_capi.test_object | ||
| Py_RETURN_NONE; | ||
| } | ||
|
|
||
| static PyMethodDef test_methods[] = { | ||
| {"get_constant", get_constant, METH_VARARGS}, | ||
| {"get_constant_borrowed", get_constant_borrowed, METH_VARARGS}, | ||
| {"test_constants", test_constants, METH_NOARGS}, | ||
| {NULL}, | ||
| }; | ||
|
|
||
| int | ||
| _PyTestLimitedCAPI_Init_Object(PyObject *m) | ||
| { | ||
| if (PyModule_AddFunctions(m, test_methods) < 0) { | ||
| return -1; | ||
| } | ||
|
|
||
| return 0; | ||
| } |
| Back | FazBrowse Home | New Git URL |
Uh oh!
There was an error while loading. Please reload this page.