GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing github.com — gh-111489: Add PyList_FromArrayMoveRef() function by vstinner · Pull Request #112975 · python/cpython Conversation
|
|
I tried to clear array items (or set bytes to a pattern, such as PYMEM_DEADBYTE= PyObject *const * args = &PyTuple_GET_ITEM(callargs, 0); for (size_t i = 0; i < argcount; i++) { Py_INCREF(args[i]); } PyObject *u = _PyTuple_FromArraySteal(args, argcount); // ... check if u == NULL ... // ... code using callargs ... So I chose to leave array unchanged. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
Add new functions to create tuple and list from arrays: * PyTuple_FromArray(). * PyTuple_FromArrayMoveRef(). * PyList_FromArrayMoveRef(). Add tests on new functions.
7b20ab0 to
b12186e
Compare
|
I would prefer to declare array type as |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
I think it's confusing that the function consumes references when it succeeds but does not consume them when it fails. I would think that it should always consume references or never consume references. If it always consumes references, then one can write I think the only-sometimes-consuming was why there used to be a lot of error-handling mistakes in module initialization code with
I would think that we would want to replicate the always-consuming "other functions" (e.g. PyTuple_SetItem) style, not the only-sometimes-consuming PyModule_AddObject style. Is there a reason to prefer this style? |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
The Ref suffix is used in many new functions, would not it cause confusion?
What if use PyTuple_FromArrayMove() or PyTuple_FromArrayMoveOwnership()? Or maybe PyTuple_CopyFromArray() and PyTuple_MoveFromArray()?
Why there is no copying API for list?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| tuple. | ||
|
|
||
| * Return a new reference on success. | ||
| * Set an exception and return NULL on error. |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
| * Set an exception and return NULL on error. | |
| * Set an exception and return ``NULL`` on error. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| # Test PyList_FromArrayMoveRef() | ||
| list_fromarraymoveref = _testcapi.list_fromarraymoveref | ||
|
|
||
| lst = [object() for _ in range(5)] |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I would use the following example:
| lst = [object() for _ in range(5)] | |
| lst = [[i] for _ in range(5)] |
Better repr.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
First, I used list("abdef"), but all items are immortal and so tests didn't detect refcount bugs in my C code. I used object() to make sure that each object is unique.
I don't think that repr is important here, the test is not supposed to fail :-)
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| copy = list_fromarraymoveref(lst) | ||
| self.assertEqual(copy, lst) |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Test also with zero and negative size. with too large size (causing MemoryError or OverflowError), with array=NULL, and with NULLs in the array. We should know what cases raise exceptions, what cases crash and what cases are valid, and how errors are handled.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| if (tuple == NULL) { | ||
| for (Py_ssize_t i = 0; i < n; i++) { | ||
| Py_DECREF(src[i]); | ||
| for (Py_ssize_t i = 0; i < size; i++) { | ||
| Py_DECREF(array[i]); | ||
| } | ||
| return NULL; | ||
| } |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
It does not match the documentation.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
I suggest discussing the name at: capi-workgroup/api-evolution#25 |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
I only exposed what already existed. But yeah, when I wrote the PR, I also noticed that it seems like there is "a hole" in the API :-) I can add a "copy" API for list as well. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
I convert this PR to a draft until we agree on naming: capi-workgroup/api-evolution#25 |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
I created this PR to have a more concrete API to discuss the naming. I prefer to close the PR for now, until we agree on naming. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
There was an error while loading. Please reload this page.