gh-103092: Convert some _ctypes metatypes to heap types#113620
Conversation
erlend-aasland
left a comment
There was a problem hiding this comment.
Thanks, this is a good start. First, we should add some tests to Lib/test/test_ctypes/, though, to make sure we're not breaking anything as we proceed:
- test the inheritance hierarchy for
_ctypestypes - test immutability for
_ctypestypes - test disallow instantiation for relevant
_ctypestypes
Also, this PR introduces reference leaks; check the traverse/clear/free slots for each new heap type:
$ ./python.exe -m test -R : test_ctypes (clinic/real-warnings *$%)
Using random seed: 4031286472
Raised RLIMIT_NOFILE: 256 -> 1024
0:00:00 load avg: 3.13 Run 1 test sequentially
0:00:00 load avg: 3.13 [1/1] test_ctypes
beginning 9 repetitions
123456789
.........
test_ctypes leaked [494, 494, 494, 494] references, sum=1976
test_ctypes leaked [314, 313, 313, 313] memory blocks, sum=1253
test_ctypes failed (reference leak)
== Tests result: FAILURE ==
1 test failed:
test_ctypes
Total duration: 6.4 sec
Total tests: run=432 skipped=29
Total test files: run=1/1 failed=1
Result: FAILURE_ctypes metatypes to heap types
Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com>
Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com>
Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com>
Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com>
Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com>
Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com>
Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com>
|
|
I'd really like if we could add tests before proceeding with this; we should do this in a separate PR. |
|
Hi @erlend-aasland, thanks for the review, I'm currently adding the tests. |
|
Sorry, I might not have been clear. Do you mean to add tests in another pull request, and then process this and subsequent pull requests to see if the changes maintain compatibility as before? |
Yes, I would prefer to add the tests in a separate PR first. Then we can proceed with this. |
|
Got it, working on this. |
| PyCArrayType_traverse(PyTypeObject *self, visitproc visit, void *arg) | ||
| { | ||
| Py_VISIT(Py_TYPE(self)); | ||
| return PyType_Type.tp_traverse((PyObject *)self, visit, arg); |
There was a problem hiding this comment.
PyCArrayType_Type and PyCSimpleType_Type can use CDataType_clear/traverse, as they now have the GC protocol. If we care about the overhead of unnecessary visits to StgDict->proto (non-container), a comment would be nice?
There was a problem hiding this comment.
I checked the CDataType_clear/traverse, and found that the only difference to current implementation, is that the CDataType version will try to convert self to StgDictObject and clear / visit StgDictObject's own fields.
Since PyCArrayType_Type and PyCSimpleType_Type are not StgDictObject, so I think re-use it is not nessasry, and may leads confusion.
There was a problem hiding this comment.
Putting your comments into those functions will be helpful when converting the PyCStgDict type. Please check me (a bit different):
1) PyCStructType_Type
2) UnionType_Type
3) PyCPointerType_Type
4) PyCArrayType_Type
5) PyCSimpleType_Type
They are not StgDictObject, but their dict objects in the tp_dict slot are replaced with StgDictObject by using Py_SETREF(). Previously, (4)(5) did not have Py_TPFLAGS_HAVE_GC, since the stgdict->proto member is a non-container type (e.g. str type: "h","i").
Do you think (1)(2)(3) are StgDictObject? They use CDataType's traverse and clear functions.
There was a problem hiding this comment.
Off-topic: StgDictObject itself (self->tp_dict) is handled by PyType_Type.tp_*(self). However, current PyCStgDict_Type does not have Py_TPFLAGS_HAVE_GC, so at least a container proto needs to be visited in CDataType_traverse(), taking account for the cache?/clone? of StgDictObject as well?
There was a problem hiding this comment.
Ah, sorry, I made a mistake in my first comment. I've understood your idea, and the codes have been updated. Thank you for pointing it out!
|
Looks good to me. |
|
|
The buildbot fails with "No space left on device" while building. |
…onGH-113620) Co-authored-by: Erlend E. Aasland <erlend@python.org>
…onGH-113620) Co-authored-by: Erlend E. Aasland <erlend@python.org>
…onGH-113620) Co-authored-by: Erlend E. Aasland <erlend@python.org>
Make the metatypes in
_ctypesinto heap types.