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

Fixing some byte-to-string conversion warnings #60049

Closed
eng793 mannequin opened this issue Sep 2, 2012 · 7 comments
Closed

Fixing some byte-to-string conversion warnings #60049

eng793 mannequin opened this issue Sep 2, 2012 · 7 comments
Assignees
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@eng793
Copy link
Mannequin

eng793 mannequin commented Sep 2, 2012

BPO 15845
Nosy @pitrou, @benjaminp, @ezio-melotti, @serhiy-storchaka
Files
  • os_glob_bytes.patch: 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/serhiy-storchaka'
    closed_at = <Date 2013-01-08.09:43:37.892>
    created_at = <Date 2012-09-02.02:19:56.305>
    labels = ['type-bug', 'library']
    title = 'Fixing some byte-to-string conversion warnings'
    updated_at = <Date 2013-01-08.09:43:37.891>
    user = 'https://bugs.python.org/eng793'

    bugs.python.org fields:

    activity = <Date 2013-01-08.09:43:37.891>
    actor = 'serhiy.storchaka'
    assignee = 'serhiy.storchaka'
    closed = True
    closed_date = <Date 2013-01-08.09:43:37.892>
    closer = 'serhiy.storchaka'
    components = ['Library (Lib)']
    creation = <Date 2012-09-02.02:19:56.305>
    creator = 'eng793'
    dependencies = []
    files = ['27092']
    hgrepos = []
    issue_num = 15845
    keywords = ['patch']
    message_count = 7.0
    messages = ['169683', '169735', '170868', '177608', '179278', '179325', '179326']
    nosy_count = 6.0
    nosy_names = ['pitrou', 'benjamin.peterson', 'ezio.melotti', 'python-dev', 'serhiy.storchaka', 'eng793']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue15845'
    versions = ['Python 3.2', 'Python 3.3', 'Python 3.4']

    @eng793
    Copy link
    Mannequin Author

    eng793 mannequin commented Sep 2, 2012

    This is related to bpo-15826.

    When run with the -b option, some glob.py and os.py functions give warnings due to byte-to-string conversions:

    amoura@amoura-laptop:/cpython$ ./python -b -c "import glob; glob.glob(b'cover*/glob.cover')"
    /home/amoura/cpython/Lib/glob.py:64: BytesWarning: Comparison between bytes and string
    if basename == '':
    amoura@amoura-laptop:
    /cpython$ ./python -b -c "import os; os.makedirs(b'tst/making/dirs')"
    /home/amoura/cpython/Lib/os.py:266: BytesWarning: Comparison between bytes and string
    if tail == cdir: # xxx/newdir/. exists if xxx/newdir exists

    The attached patch fixes this.

    There is a rather more mysterious phenomenon with exceptions (which is triggered by test_exceptions for ImportException, but it happens for any Exception class):

    >>> e = Exception(b'aaa')
    [60596 refs]
    >>> e.args[0]
    b'aaa'
    [60601 refs]
    >>> str(e)
    __main__:1: BytesWarning: str() on a bytes instance
    "b'aaa'"
    [60615 refs]
    >>> e.args[0]
    b'aaa'
    [60615 refs]
    >>> str(e)
    "b'aaa'"
    [60615 refs]
    >>> e.args[0]
    b'aaa'
    [60615 refs]

    In other words, if a bytes argument is given to the exception, the first call to str triggers the warning, but further calls don't. Is this worth pursuing?

    @eng793 eng793 mannequin added type-feature A feature request or enhancement stdlib Python modules in the Lib dir labels Sep 2, 2012
    @benjaminp
    Copy link
    Contributor

    Not repeating warnings from the same place is the default warning behavior. You can get all of them by passing -Wall.

    @ezio-melotti
    Copy link
    Member

    • if basename == '':
      + if len(basename) == 0:

    This should be "if not basename:"

    -        if tail == curdir:
    +        cdir = curdir
    +        if isinstance(tail, bytes):
    +            cdir = bytes(curdir, 'ASCII')
    +        if tail == cdir:

    This will raise an error if curdir is a non-ascii str, so, unless the same error was already raised later in the code, this is backward incompatible.

    @serhiy-storchaka
    Copy link
    Member

    The first bug fixed in bpo-16696.

    @serhiy-storchaka serhiy-storchaka added type-bug An unexpected behavior, bug, or error and removed type-feature A feature request or enhancement labels Dec 16, 2012
    @serhiy-storchaka serhiy-storchaka self-assigned this Jan 7, 2013
    @serhiy-storchaka
    Copy link
    Member

    This will raise an error if curdir is a non-ascii str, so, unless the same error was already raised later in the code, this is backward incompatible.

    On all supported platforms curdir is a ascii str (':' on Mac Classic, '.' on all other). The same idiom used in glob module.

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Jan 8, 2013

    New changeset 9458a516f769 by Serhiy Storchaka in branch '3.2':
    Issue bpo-15845: Fix comparison between bytes and string.
    http://hg.python.org/cpython/rev/9458a516f769

    New changeset f6cf2985348a by Serhiy Storchaka in branch '3.3':
    Issue bpo-15845: Fix comparison between bytes and string.
    http://hg.python.org/cpython/rev/f6cf2985348a

    New changeset 51e60d9ee389 by Serhiy Storchaka in branch 'default':
    Issue bpo-15845: Fix comparison between bytes and string.
    http://hg.python.org/cpython/rev/51e60d9ee389

    @serhiy-storchaka
    Copy link
    Member

    Fixed. Thank your for report and patch, Alessandro.

    @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
    stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants