镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Bytes, strings, and unicode? #30

Open

Description

@ltratt

I see this warning in the standard library:

/tmp/pygrate2/Lib/codecs.py:506: Py3xWarning: The first string is 'str' while the second is 'unicode': mixed bytes, str and unicode operands cannot be used in string concatenation in Python 3.x: convert the operand(s) so that they are the same type.
  self.charbuffer += newchars

Since Python3 doesn't have the unicode type per se, I'm wondering if this warning is quite right? As I understand the situation (and my memory is a bit fuzzy...):

  • Python2 has the types str and unicode but not a type bytes. The bytes() function returns strs.
  • Python3 has the types bytes and str but not unicode.

If I've got that (roughly) correct, I wonder whether the warning at the top of this issue is correct and/or as accurate as it could be? I think that str + unicode in Python2 will always map to str + str in Python3? Or are there certain types of strings/unicode strings in Python2 that will map to bytes in Python3?

Activity

  1. nanjekyejoannah commented on May 22, 2023

    @nanjekyejoannah
    Collaborator

    Yes, you are correct, the u prefix just exists for backward compatibility, i.e, no-op per se, its not supported in semantics to say unicode = str, that is to say no Python 3 code uses the u prefix or is expected to. Any code still using the u prefix is most likely assuming the genuine existence of the unicode type as was the case in Python 2. I think in short supporting u in python 3 is not a feature, just exists for convenience. If we look at it as a supported solely for compatibility, the warning is correct, if we assume it is a feature the warning needs fixing.

  2. ltratt commented on May 22, 2023

    @ltratt
    MemberAuthor

    I'm not sure this is entirely about u"blah" strings, but I might be wrong. The warning we're giving is really saying the + operator won't work as expected in Python3. In one sense, I suppose that's true, but I think it's not very useful, because Python3 won't have let a user construct a unicode string by definition.

    Now that I think about it, I'm not even sure if the user can do much about this warning? Can they convert their string type(s) in a way that will make their code forward compatible?

  3. nanjekyejoannah commented on May 22, 2023

    @nanjekyejoannah
    Collaborator

    By making the strings involved in the + operation of the same type. str + str or bytes + bytes , if we took u to be a feature u'str' + 'str' shouldnt lead to a warning.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions