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: Bad interaction between ipaddress addresses and the bytes constructor
Type: crash Stage: resolved
Components: Versions:
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: benjamin.peterson, georg.brandl, ncoghlan, pmoody, python-dev
Priority: release blocker Keywords:

Created on 2012-08-05 08:03 by ncoghlan, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (3)
msg167474 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2012-08-05 08:03
I've been tinkering with the ipaddress module as I review Eli's documentation and have uncovered a *very* nasty interaction between ipaddress objects and the bytes constructor.

Specifically, if you pass an integer to bytes() it will attempt to allocate and zero-initialise a buffer of that size. It uses operator.index() to check for integers.

This creates a problem, because ipaddress objects currently implement __index__ so they can be treated as integers without an explicit cast.

This does very bad things if, say, you do "bytes(ipaddress.IPv4Address('192.168.0.1'))"

If I remove the __index__ implementation, then you can't call hex() directly on ipaddress objects anymore - you have to call hex(int(addr)) instead.

I'm a *lot* happier with that approach, and will be implementing it in a moment.
msg167475 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2012-08-05 08:20
New changeset 5abea8a43f19 by Nick Coghlan in branch 'default':
Close #15559: Implementing __index__ creates a nasty interaction with the bytes constructor. At least for 3.3, ipaddress objects must now be explicitly converted with int() and thus can't be passed directly to the hex() builtin.
http://hg.python.org/cpython/rev/5abea8a43f19
msg177787 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2012-12-19 22:27
I changed the precedence now, so __bytes__ is tried before __index__.
History
Date User Action Args
2022-04-11 14:57:33adminsetgithub: 59764
2012-12-19 22:27:52benjamin.petersonsetnosy: + benjamin.peterson
messages: + msg177787
2012-08-05 08:20:35python-devsetstatus: open -> closed

nosy: + python-dev
messages: + msg167475

resolution: fixed
stage: resolved
2012-08-05 08:03:13ncoghlancreate