GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • [Python-Dev] Documenting types outside of typing
  • [ date ]
  • [ thread ]
  • [ subject ]
  • [ author ]

This view

  • Original page
  • New address

GeezerVison is showing mail.python.org — [Python-Dev] When val=b'', but val == b'' returns False - bytes initialization

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

Antoine Pitrou solipsis at pitrou.net
Wed Dec 27 12:51:15 EST 2017

  • Previous message (by thread): [Python-Dev] When val=b'', but val == b'' returns False - bytes initialization
  • Next message (by thread): [Python-Dev] Documenting types outside of typing
  • Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]

On Wed, 27 Dec 2017 17:28:41 +0100
Antoine Pitrou <solipsis at pitrou.net> wrote:
> On Wed, 27 Dec 2017 14:19:16 +0000
> Jonathan Underwood <jonathan.underwood at gmail.com> wrote:
> > 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;  
> 
> I wouldn't call it "expected", but bytes objects are supposed to be
> NULL-terminated internally, so your code technically creates an invalid
> bytes object.  The NULL-terminated constraint may be relied on by some
> code, for example when the string gets passed to a third-party C
> function.

Note this is really happening because you're allocating a 20-long bytes
object and then shortening it to 0 bytes.  PyBytes_FromStringAndSize()
already stored a NULL byte in the 21st place.

Regards

Antoine.



  • Previous message (by thread): [Python-Dev] When val=b'', but val == b'' returns False - bytes initialization
  • Next message (by thread): [Python-Dev] Documenting types outside of typing
  • Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]

More information about the Python-Dev mailing list
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.