STINNER Victor added the comment:

To reduce the memory footprint, it's important that we reuse internal Unicode 
singletons. For the empty string singleton, you have two choices:

* if length is equal to zero, return the singleton
* create a string and then call unicode_result()

unicode_result() should be used when the output length is not easy to compute 
or when the code is complex.

Here is the code is very simple and the output length is obvious.

Serhiy: "In what circumstances _PyUnicode_FromASCII is called with size=0?"

I checked the current code: I'm unable to see any function which can call 
_PyUnicode_FromASCII() with size=0.


Xiang: "IMHO, _PyUnicode_FromASCII is a private API and could be used in other 
places in future. We should not rely on the caller to check and return the 
singleton empty string."

I agree. Xiang's patch is short and makes sense, so I vote +1 on it.

I let Serhiy decides if the patch should be merged or not.

If we decide to reject the patch, I suggest to add the following code at the 
beginning of _PyUnicode_FromASCII:

/* Detect missed optimization */
assert(size != 0);

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue28398>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to