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.

classification
Title: test_port_parameter_types fails with BytesWarning
Type: behavior Stage: resolved
Components: Tests Versions: Python 3.6, Python 3.5
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: berker.peksag, rhettinger, serhiy.storchaka, vstinner, yselivanov
Priority: normal Keywords:

Created on 2016-05-21 12:27 by berker.peksag, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (8)
msg265998 - (view) Author: Berker Peksag (berker.peksag) * (Python committer) Date: 2016-05-21 12:27
The relevant changeset is 1570e3855ce8. Here is a traceback from a random buildbot:

======================================================================
ERROR: test_port_parameter_types (test.test_asyncio.test_base_events.BaseEventTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/dje/cpython-buildarea/3.x.edelsohn-debian-z/build/Lib/test/test_asyncio/test_base_events.py", line 143, in test_port_parameter_types
    base_events._ipaddr_info('1.2.3.4', b'1', INET, STREAM, TCP))
BytesWarning: Comparison between bytes and string

http://buildbot.python.org/all/builders/s390x%20Debian%203.x/builds/1100/steps/test/logs/stdio
msg266017 - (view) Author: Yury Selivanov (yselivanov) * (Python committer) Date: 2016-05-21 19:40
I really can't figure out what's going on. I can't reproduce this, and the exception doesn't make any sense :(
msg266018 - (view) Author: Yury Selivanov (yselivanov) * (Python committer) Date: 2016-05-21 19:56
Alright, I think I know what's going on here -- this seems to be a bug of functools.lrucache.  Serhiy, would you be able to look into this?

Testcase:

test.py:

    import functools

    @functools.lru_cache()
    def foo(a):
        return

    foo('')
    foo(b'')

$ python3.5 -bb test.py
Traceback (most recent call last):
  File "test.py", line 8, in <module>
    foo(b'')
BytesWarning: Comparison between bytes and string
msg266022 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-05-21 20:37
The workaround is using lru_cache(typed=True).
msg266025 - (view) Author: Yury Selivanov (yselivanov) * (Python committer) Date: 2016-05-21 20:40
> The workaround is using lru_cache(typed=True).

Yes, seems to be working! Thanks.
msg266027 - (view) Author: Yury Selivanov (yselivanov) * (Python committer) Date: 2016-05-21 21:12
Berker, it should be fixed now in https://hg.python.org/cpython/rev/bdfb15aa3ac5 (sorry, I forgot to reference the issue in the commit message)
msg266029 - (view) Author: Berker Peksag (berker.peksag) * (Python committer) Date: 2016-05-21 21:17
Yes, buildbots look happy now. Thanks!
msg266030 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2016-05-21 21:21
On Python 3, comparison between str and bytes raises a BytesWarning exception if python3 is started with -bb. But sometimes, you really want to compare str with bytes. For example, os.get_exec_path() changes temporarely the BytesWarning warning for that!

    # {b'PATH': ...}.get('PATH') and {'PATH': ...}.get(b'PATH') emit a
    # BytesWarning when using python -b or python -bb: ignore the warning
    with warnings.catch_warnings():
        warnings.simplefilter("ignore", BytesWarning)
        ...
History
Date User Action Args
2022-04-11 14:58:31adminsetgithub: 71264
2016-05-21 21:21:17vstinnersetmessages: + msg266030
2016-05-21 21:17:26berker.peksagsetstatus: open -> closed
resolution: fixed
messages: + msg266029

stage: needs patch -> resolved
2016-05-21 21:12:13yselivanovsetmessages: + msg266027
2016-05-21 20:40:22yselivanovsetmessages: + msg266025
2016-05-21 20:37:53serhiy.storchakasetmessages: + msg266022
2016-05-21 19:56:01yselivanovsetnosy: + rhettinger, vstinner, serhiy.storchaka
messages: + msg266018
2016-05-21 19:40:44yselivanovsetmessages: + msg266017
2016-05-21 12:27:09berker.peksagcreate