-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
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
Avoid creating incomplete/invalid objects #36
Comments
These are actually two independent requirements. It's easy to construct an API for creating container objects incrementally that keeps the object out of GC tracking while it's being built. This is independent of the question whether the object has been created in one go or incrementally before it is added to the GC tracking. |
Yes. This guideline should disallow both:
That could be solved with one-shot creation API, and/or a "builder" approach that makes the user do all the steps, in the proper order. (And if those approaches can't be fast enough, it's time for an exception -- see #4.) |
In Python 3.13 alpha1, I modified PyList_SET_ITEM() to restrict index to I dislike PyList_New() + PyList_SET_ITEM() API since the list is immediately tracked by the GC and so calling gc.get_objects() can expose an invalid list object (ex: calling With python/cpython#111480 change,
Well, python/cpython#111480 was not about designing a correct API, but to migrate Python 3.12 code to Python 3.13. Previously, |
For list, we need an API to transfer objects from a C array to a list without the expensive INCREF/DECREF dance: make |
I created an issue for that: python/cpython#111489 |
I added I'm working on a similar API to create a Python int object from an array of digits, |
I added `PyUnicodeWriter` API to solve this problem for Unicode strings
Why does it need a PyUnicodeWriter_Discard() and not just the normal decref?
|
PyUnicodeWriter allocates a structure on the heap memory. It's not a Python object. |
This is unfortunate (creating a string this way now requires two heap allocations) but if we were to use a stack-allocated writer object instead, it's hard to imagine how this API could ever be promoted to the stable ABI, and it would still require a separate API call (to reveal the created object in its final form). So I've made peace with this design. |
I created python/cpython#121710: [C API] Add PyBytesWriter API. |
From capi-workgroup/problems#56
Related to #20 (Disallow mutating immutable objects)
New API should not allow creation of incomplete/invalid objects.
In particular, a
traverse
function must be safe to call right after an object is tracked with the GC.The text was updated successfully, but these errors were encountered: