Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

py3k-pep3137: issue warnings / errors on str(bytes()) and similar operations #45733

Closed
tiran opened this issue Nov 5, 2007 · 16 comments
Closed
Assignees
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@tiran
Copy link
Member

tiran commented Nov 5, 2007

BPO 1392
Nosy @gvanrossum, @tiran
Files
  • py3k-pep3137_remove_str_bytes.patch
  • py3k-pep3137_remove_str_bytes2.patch
  • py3k-pep3137_str_bytes_ascii.patch
  • py3k-pep3137_str_bytes_warning.patch
  • py3k-pep3137_str_bytes_warning2.patch
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/gvanrossum'
    closed_at = <Date 2007-11-06.11:53:53.748>
    created_at = <Date 2007-11-05.14:22:51.127>
    labels = ['interpreter-core', 'type-bug']
    title = 'py3k-pep3137: issue warnings / errors on str(bytes()) and similar operations'
    updated_at = <Date 2008-01-06.22:29:45.367>
    user = 'https://github.com/tiran'

    bugs.python.org fields:

    activity = <Date 2008-01-06.22:29:45.367>
    actor = 'admin'
    assignee = 'gvanrossum'
    closed = True
    closed_date = <Date 2007-11-06.11:53:53.748>
    closer = 'christian.heimes'
    components = ['Interpreter Core']
    creation = <Date 2007-11-05.14:22:51.127>
    creator = 'christian.heimes'
    dependencies = []
    files = ['8694', '8695', '8696', '8697', '8701']
    hgrepos = []
    issue_num = 1392
    keywords = ['patch']
    message_count = 16.0
    messages = ['57124', '57125', '57126', '57127', '57128', '57129', '57131', '57132', '57133', '57134', '57136', '57137', '57138', '57153', '57154', '57160']
    nosy_count = 2.0
    nosy_names = ['gvanrossum', 'christian.heimes']
    pr_nums = []
    priority = 'high'
    resolution = 'fixed'
    stage = None
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue1392'
    versions = ['Python 3.0']

    @tiran
    Copy link
    Member Author

    tiran commented Nov 5, 2007

    str(bytes()) == repr(bytes()) and str(buffer()) == repr(buffer()) is
    causing a bunch bugs which are extremely hard to understand. On several
    occasions I didn't understand the problem until I removed a str() call
    or made str(bytes()) and str(buffer()) raise an exception.

    @tiran tiran added interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error labels Nov 5, 2007
    @tiran
    Copy link
    Member Author

    tiran commented Nov 5, 2007

    Err, please remove the (reprfunc) cast and check the language of the
    error messages, too :)

    @tiran
    Copy link
    Member Author

    tiran commented Nov 5, 2007

    Here is an updated patch that fixes an error in httplib that was causing
    the xmlrpc test suite to block.

    @gvanrossum
    Copy link
    Member

    I'll look at the patches later, but we've gone over this before on the
    list. str() of *any* object needs to return *something*. Yes, it's
    unfortunate that this masks bugs in the transitional period, but it
    really is the best thing in the long run. We had other exceptional
    treatement for str vs. bytes (e.g. the comparison was raising TypeError
    for a while) and we had to kill that too.

    @tiran
    Copy link
    Member Author

    tiran commented Nov 5, 2007

    Guido van Rossum wrote:

    I'll look at the patches later, but we've gone over this before on the
    list. str() of *any* object needs to return *something*. Yes, it's
    unfortunate that this masks bugs in the transitional period, but it
    really is the best thing in the long run. We had other exceptional
    treatement for str vs. bytes (e.g. the comparison was raising TypeError
    for a while) and we had to kill that too.

    Can we agree to a compromise and make str(bytes()) return
    bytes().decode("ascii")? I think it's a sensible default behavior and
    catches the errors in the code I've seen so far.

    Christian

    @tiran
    Copy link
    Member Author

    tiran commented Nov 5, 2007

    New patch:

    static PyObject *
    string_str(PyObject *op)
    {
    	return PyObject_CallMethod(op, "decode", "s", "ascii");
    }

    @gvanrossum
    Copy link
    Member

    No -- making str(b) return b.decode("ascii") brings back the same issues
    we had with mindlessly mixing PyString and PyUnicode in 2.x. That was a
    major disaster.

    @gvanrossum
    Copy link
    Member

    A compromise I could live with: add a command line option that makes it
    a warning.

    @tiran
    Copy link
    Member Author

    tiran commented Nov 5, 2007

    Guido van Rossum wrote:

    Guido van Rossum added the comment:

    A compromise I could live with: add a command line option that makes it
    a warning.

    Good idea. I can live with it, too. :) How do you like

    -b: issue warnings about str(bytes_instance) and str(buffer_instance)
    (-bb: issue errors)

    Christian

    @tiran
    Copy link
    Member Author

    tiran commented Nov 5, 2007

    I've made a patch with -b -> warning and -bb > type error. I'm not happy
    with the warning message. Maybe you've a better idea for it.

    @gvanrossum
    Copy link
    Member

    The error message is fine, though you could drop the word "Calling"
    without loss of information. ;-) Also, most warnings don't seem to use
    a trailing period.

    Perhaps you could expand this to also add a trap in the comparison
    function when Py{String,Bytes} is compared to PyUnicode.

    @tiran
    Copy link
    Member Author

    tiran commented Nov 5, 2007

    Guido van Rossum wrote:

    The error message is fine, though you could drop the word "Calling"
    without loss of information. ;-) Also, most warnings don't seem to use
    a trailing period.

    Perhaps you could expand this to also add a trap in the comparison
    function when Py{String,Bytes} is compared to PyUnicode.

    It's sounds like a reasonable idea. But first I'm going to hit the down
    with my gf and meet some friends. Cu later!

    PS: In my humble opinion Amaury Forgeot d'Arc deserves an entry in the
    Misc/ACKS list for his patches.

    @gvanrossum
    Copy link
    Member

    It's sounds like a reasonable idea. But first I'm going to hit the down
    with my gf and meet some friends. Cu later!

    No! You're not allowed to have a life! :-)

    (And no hacking drunk, either. :-)

    PS: In my humble opinion Amaury Forgeot d'Arc deserves an entry in the
    Misc/ACKS list for his patches.

    Agreed -- done!

    @tiran
    Copy link
    Member Author

    tiran commented Nov 6, 2007

    Pfff! I've written my best code after having one or two beers :)

    Updates:

    • Added class BytesWarning(Warning)
    • Added -b/-bb command line args and Py_BytesWarningFlag
    • issue PyErr_WarnEx(PyExc_BytesWarning) when Py_BytesWarningFlag is set.
    • Added warning filter for BytesWarning that raises an exception if
      Py_BytesWarningFlag > 1

    Open questions:

    • the warning filter isn't set when the initializer cannot load the
      warning module (e.g. frozen apps). W/o the warning module Python is
      screwed anyway and frozen apps would never add the -b argument.
    • the warnings are only enabled with -b. They can't be enabled by
      modifying the warnings.filters. Is this fine with you?

    @gvanrossum
    Copy link
    Member

    Updates:

    • Added class BytesWarning(Warning)
    • Added -b/-bb command line args and Py_BytesWarningFlag
    • issue PyErr_WarnEx(PyExc_BytesWarning) when Py_BytesWarningFlag is set.
    • Added warning filter for BytesWarning that raises an exception if
      Py_BytesWarningFlag > 1

    Open questions:

    • the warning filter isn't set when the initializer cannot load the
      warning module (e.g. frozen apps). W/o the warning module Python is
      screwed anyway and frozen apps would never add the -b argument.
    • the warnings are only enabled with -b. They can't be enabled by
      modifying the warnings.filters. Is this fine with you?

    Yes, this is is all fine with me. Feel free to submit overnight.

    @tiran
    Copy link
    Member Author

    tiran commented Nov 6, 2007

    Applied to py3k-pep3137 in r 58876

    @tiran tiran closed this as completed Nov 6, 2007
    @tiran tiran changed the title py3k-pep3137: str(bytes()) and str(buffer()) should raise TypeError patch py3k-pep3137: issue warnings / errors on str(bytes()) and similar operations Nov 6, 2007
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants