This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: Return new string for single subclass joins (Bug #1001011)
Type: Stage:
Components: Interpreter Core Versions: Python 2.4
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: ncoghlan, rhettinger, tim.peters
Priority: normal Keywords: patch

Created on 2004-08-11 08:03 by ncoghlan, last changed 2022-04-11 14:56 by admin. This issue is now closed.

File name Uploaded Description Edit
str_join2.diff ncoghlan, 2004-08-11 22:52 Fix that preserves optimisation for the two builtin classes
Messages (8)
msg46667 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2004-08-11 08:03
At present, str.join contains a shortcut that returns a
reference to the existing string for a sequence that
contains a single item.

This can cause misbehaviour when subclasses of str are
involved (see the bug report for details).

The attached patch removes the shortcut, and adds a
test for the correct behaviour.
msg46668 - (view) Author: Tim Peters (tim.peters) * (Python committer) Date: 2004-08-11 12:52
Logged In: YES 

This is an important optimization for "true strings".  It was 
indeed the intent that internal optimization for immutable 
types not be applied to subclass instances, so it's good to fix 
that.  It's not good to lose the optimization when it's safe.
msg46669 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2004-08-11 22:17
Logged In: YES 

OK. I think I can do something with PyString_CheckExact that
will do what you'd prefer.
msg46670 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2004-08-11 22:52
Logged In: YES 

New version of patch attached which uses PyString_CheckExact
and PyUnicode_CheckExact to determine when the optimisation
is safe, and falls through to the general case when it isn't.

Should this optimisation be applied to the unicode join as well?
msg46671 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2004-08-11 22:53
Logged In: YES 

Oh - the test looks explicitly for the optimised behaviour
for a true string (to make sure this version preserved that
behaviour). Is that a good idea, or not?
msg46672 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2004-08-19 22:17
Logged In: YES 

The patch looks good, it compiles okay, and the test_suite

If Tim has no further issues, I will apply it.
msg46673 - (view) Author: Tim Peters (tim.peters) * (Python committer) Date: 2004-08-19 22:28
Logged In: YES 

Unassigned myself -- can't make time for this now.
msg46674 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2004-08-23 23:26
Logged In: YES 

Applied as:
   Objects/stringobject.c 2.225
   Lib/test/ 1.26

Thanks for the perfect patch.
Date User Action Args
2022-04-11 14:56:06adminsetgithub: 40742
2004-08-11 08:03:32ncoghlancreate