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.

Author josh.r
Recipients bkline, josh.r, terry.reedy, xtreak
Date 2019-09-07.02:56:39
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1567825000.04.0.340682620702.issue38003@roundup.psfhosted.org>
In-reply-to
Content
basestring in Python 2 means "thing that is logically text", because in Python 2, str can mean *either* logical text *or* binary data, and unicode is always logical text. str and unicode can kinda sorta interoperate on Python 2, so it can make sense to test for basestring if you're planning to use it as logical text; if you do 'foo' + u'bar', that's fine in Python 2. In Python 3, only str is logically text; b'foo' + 'bar' is completely illegal, so it doesn't make sense to convert it to recognize both bytes and str.

Your problem is that you're using basestring incorrectly in Python 2, and it happens to work only because Python 2 did a bad job of separating text and binary data. Your original example code should actually have been written in Python 2 as:


if isinstance(value, bytes):  # bytes is an alias of str, and only str, on 2.7
    value = value.decode(encoding)
elif not isinstance(value, unicode):
    some other code

which 2to3 would convert correctly (changing unicode to str, and leaving everything else untouched) because you actually tested what you meant to test to control the actions taken:

1. If it was binary data (which you interpret all Py2 strs to be), then it is decoded to text (Py2 unicode/Py3 str)
2. If it wasn't binary data and it wasn't text, you did something else

Point is, the converter is doing the right thing. You misunderstood the logical meaning of basestring, and wrote code that depended on your misinterpretation, that's all.

Your try/except to try to detect Python 3-ness was doomed from the start; you referenced basestring, and 2to3 (reasonably) converts that to str, which breaks your logic. You wrote cross-version code that can't be 2to3-ed because it's *already* Python 3 code; Python 3 code should never be subjected to 2to3, because it'll do dumb things (e.g. change print(1, 2) to print((1, 2))); it's 2to3, not 2or3to3 after all.
History
Date User Action Args
2019-09-07 02:56:40josh.rsetrecipients: + josh.r, terry.reedy, bkline, xtreak
2019-09-07 02:56:40josh.rsetmessageid: <1567825000.04.0.340682620702.issue38003@roundup.psfhosted.org>
2019-09-07 02:56:40josh.rlinkissue38003 messages
2019-09-07 02:56:39josh.rcreate