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

Byte warning mode and b'' != '' #48238

Closed
tiran opened this issue Sep 28, 2008 · 12 comments
Closed

Byte warning mode and b'' != '' #48238

tiran opened this issue Sep 28, 2008 · 12 comments
Assignees
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) release-blocker type-bug An unexpected behavior, bug, or error

Comments

@tiran
Copy link
Member

tiran commented Sep 28, 2008

BPO 3988
Nosy @warsaw, @brettcannon, @pitrou, @vstinner, @tiran, @benjaminp
Files
  • bytes_ne_warning-3.patch: Raise a warning on b'' != '' and bytearray(b'') != ''
  • 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/warsaw'
    closed_at = <Date 2008-10-17.01:51:12.815>
    created_at = <Date 2008-09-28.11:32:56.051>
    labels = ['interpreter-core', 'type-bug', 'release-blocker']
    title = "Byte warning mode and b'' != ''"
    updated_at = <Date 2008-10-17.01:51:12.814>
    user = 'https://github.com/tiran'

    bugs.python.org fields:

    activity = <Date 2008-10-17.01:51:12.814>
    actor = 'barry'
    assignee = 'barry'
    closed = True
    closed_date = <Date 2008-10-17.01:51:12.815>
    closer = 'barry'
    components = ['Interpreter Core']
    creation = <Date 2008-09-28.11:32:56.051>
    creator = 'christian.heimes'
    dependencies = []
    files = ['11798']
    hgrepos = []
    issue_num = 3988
    keywords = ['patch']
    message_count = 12.0
    messages = ['73969', '73973', '73979', '73984', '73985', '73986', '73989', '73990', '73991', '74063', '74782', '74891']
    nosy_count = 6.0
    nosy_names = ['barry', 'brett.cannon', 'pitrou', 'vstinner', 'christian.heimes', 'benjamin.peterson']
    pr_nums = []
    priority = 'release blocker'
    resolution = None
    stage = None
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue3988'
    versions = ['Python 3.0']

    @tiran
    Copy link
    Member Author

    tiran commented Sep 28, 2008

    In byte warning mode (-b or -bb command line argument) b'' == '' raises
    an exception but b'' != '' doesn't.

    ./python -bb
    >>> b'' == ''
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    BytesWarning: Comparison between bytes and string
    >>> b'' != ''
    True

    I can't recall why I implemented the byte warning mode this way. But
    nowadays I think it could hide bugs in code like "while egg != ''" where
    egg is a byte instance.

    @tiran tiran added interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error labels Sep 28, 2008
    @benjaminp
    Copy link
    Contributor

    +1

    I was just confused by this fact yesterday. :)

    @pitrou
    Copy link
    Member

    pitrou commented Sep 28, 2008

    +1 as well. Lib/pty.py had a line like that ("while buf != ''") and I
    wondered why no exception was thrown with -bb while the variable was a
    bytes object.

    @vstinner
    Copy link
    Member

    Here is a patch for this issue.

    @tiran
    Copy link
    Member Author

    tiran commented Sep 28, 2008

    Thanks for the patch but you've missed a spot in bytearrayobject.c:

    Objects/bytearrayobject.c: if (Py_BytesWarningFlag && op == Py_EQ) {
    Objects/bytesobject.c: if (Py_BytesWarningFlag && (op == Py_EQ) &&

    @benjaminp
    Copy link
    Contributor

    It would also be nice to have tests. (in test_bytes)

    @vstinner
    Copy link
    Member

    @christian.heimes: Oops, i totally forget the bytearray() type. Here
    is a new patch.

    @vstinner
    Copy link
    Member

    I don't know how to activate BytesWarning as error (as "python3 -bb"
    does). Here is an patch for tests only working with "python3 -bb".

    @benjaminp
    Copy link
    Contributor

    warnings.simplefilter("always", BytesWarning) should do the trick.

    @brettcannon
    Copy link
    Member

    Setting as a deferred blocker since this is a 3.0 thing and not a 2.6 thing.

    @vstinner
    Copy link
    Member

    New patch including the test. The test doesn't fail anymore if -bb is
    not set (test is just ignored). See also issue bpo-4125 (force -bb option
    for runtests.sh).

    @warsaw
    Copy link
    Member

    warsaw commented Oct 17, 2008

    r66950

    @warsaw warsaw closed this as completed Oct 17, 2008
    @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) release-blocker type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    6 participants