On 2020-01-07 20:37, Rui Hu wrote:
I see! That definitely helps. What about a call to 
PyObject_CallMethodObjArgs()? Under the hood, is it really the same as a first 
call to PyObject_GetAttr() to get an attribute which is followed by another 
call to PyObject_Call()? I read Objects/call.c and seems to me this is the 
case. I'm asking because with Py_VaBuildValue() the only to simulate 
Py_CallMethodObjArgs() is the two-way call described above.

It returns a new reference because it's returning a result, which might be a newly-created object.

Consider, say, PyList_GetItem. The object it returns is in the list, so doing an incref would be wasteful (you're not expecting the object to disappear unexpectedly). It's a borrowed reference, and that's good enough.

Now consider PyUnicode_Substring. The result is a substring, so some copying must take place. A new string is created and returned. It returns a new reference.

Except that it doesn't always create a new string.

If the substring happens to include all of the characters, there's no need to make a copy, because strings are immutable. It'll just do an incref and return the original string, and your code will be none the wiser.
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/GJWEAN6LLTCDGS4S5SP3PL3C24KCBJER/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to