A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://mail.python.org/pipermail/python-dev/2017-December/151464.html below:

[Python-Dev] When val=b'', but val == b'' returns False

[Python-Dev] When val=b'', but val == b'' returns False - bytes initializationJonathan Underwood jonathan.underwood at gmail.com
Wed Dec 27 09:19:16 EST 2017
Hello,

I am not sure if this is expected behaviour, or a bug.

In a C extension module, if I create and return an empty bytes object like this:

    val = PyBytes_FromStringAndSize (NULL, 20);
    Py_SIZE(val) = 0;

Then from the Python interpreter's perspective:

    isinstance(val, bytes) returns True
    print(val) returns b''
    print(repr(val)) returns b''

BUT val == b'' returns False.


On the other hand, initializing the underlying memory:

    val = PyBytes_FromStringAndSize (NULL, 20);
    PyBytes_AS_STRING (val);
    c[0] = '\0';
    Py_SIZE(val) = 0;

Then, from the Python interpreter, val == b'' returns True, as expected.

So, my question is: is this the expected behaviour, or a bug? I was
slightly surprised to have to initialize the storage. On the other
hand, I can perhaps also see it might be expected, since the docs do
say that PyBytes_FromStringAndSize will not initialize the underlying
storage.

Please cc me on any replies - am not subscribed to the list.

Many thanks,
Jonathan
More information about the Python-Dev mailing list

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4