Skip to content

Improvements to ipaddress module #72047

Closed

Description

@MoritzS
mannequin
BPO 27860
Nosy @ncoghlan, @methane, @serhiy-storchaka, @zhangyangyu, @jacktose
PRs
  • bpo-27860: clean up ipaddress #12774
  • bpo-27860: ipaddress: use cached_property #12832
  • bpo-27860: ipaddress: fix Interface missed some attributes #12836
  • [3.7] bpo-27860: ipaddress: fix Interface constructor #14200
  • Files
  • ipaddress-improvement.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 = None
    closed_at = <Date 2019-06-19.11:48:12.761>
    created_at = <Date 2016-08-25.15:14:28.605>
    labels = ['3.7', '3.8', 'type-feature', 'library']
    title = 'Improvements to ipaddress module'
    updated_at = <Date 2019-06-19.11:48:12.760>
    user = 'https://bugs.python.org/moritzs'

    bugs.python.org fields:

    activity = <Date 2019-06-19.11:48:12.760>
    actor = 'methane'
    assignee = 'none'
    closed = True
    closed_date = <Date 2019-06-19.11:48:12.761>
    closer = 'methane'
    components = ['Library (Lib)']
    creation = <Date 2016-08-25.15:14:28.605>
    creator = 'moritzs'
    dependencies = []
    files = ['44223']
    hgrepos = []
    issue_num = 27860
    keywords = ['patch']
    message_count = 13.0
    messages = ['273655', '279600', '279607', '339845', '340240', '340259', '340307', '340308', '343349', '343537', '343671', '345983', '346038']
    nosy_count = 8.0
    nosy_names = ['ncoghlan', 'pmoody', 'methane', 'SilentGhost', 'serhiy.storchaka', 'moritzs', 'xiang.zhang', 'Jacktose']
    pr_nums = ['12774', '12832', '12836', '14200']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue27860'
    versions = ['Python 3.7', 'Python 3.8']

    Activity

    1. MoritzS commented on Aug 25, 2016

      MoritzSmannequin
      MannequinAuthor

      This patch fixes the following minor issues with the ipaddress module:

      • Removed unused property _BaseV4._valid_mask_octets
      • Removed unused methods _BaseV4._is_valid_netmask() and _BaseV4._is_hostmask()
      • Replaced several calls to superclass constructors by super()

      It also refactors the constructors of IPv4Network, IPv4Interface, IPv6Network, and IPv6Interface.
      They all now use the new method _get_addr_prefix_tuple() to parse the argument. It now matches the following sentence of the documentation of IPv4/6Interface:
      "The meaning of address is as in the constructor of IPv4Network".

      Additionally they now also accept a bytes or an IPv4/6Address object representing a netmask (or a hostmask [only IPv4]) as second element of an address/netmask tuple. This makes it easier to work with C-APIs that provide netmasks only as bytes object.

      This patch does not try to solve bpo-27683.

    2. added
      stdlib
      type-feature
      on Aug 25, 2016
    3. MoritzS commented on Oct 28, 2016

      MoritzSmannequin
      MannequinAuthor

      Any updates?

    4. ncoghlan commented on Oct 28, 2016

      @ncoghlan
      Contributor

      Peter, are you able to take a look at this or indicate you're happy for someone else to take it? (I relinquished my co-maintainer role for the ipaddress module a while back, so you're the only currently listed maintainer)

    5. methane commented on Apr 10, 2019

      @methane
      Member

      @MoritzS Would you create a pull request on GitHub?
      Or may I create a pull request for your patch?

    6. methane commented on Apr 15, 2019

      @methane
      Member

      New changeset 2430d53 by Inada Naoki in branch 'master':
      bpo-27860: use cached_property (GH-12832)
      2430d53

    7. methane commented on Apr 15, 2019

      @methane
      Member

      I am not owner of ipaddress module, so I don't know we should
      support more form of masks.

      On the other hand, IPv4Interface and IPv6Interface expose
      netmask and hostmask attributes when address is not bytes or int.

      These attributes are not documented. I assume it was added
      accidentally, when coping some code from Network classes.

      But there are test for these attributes.
      NetmaskTestMixin_v4.test_valid_netmask runs for IPv4Network and IPv4Interface.
      It checks these attributes when constructor argument is string.

      def test_valid_netmask(self):
      self.assertEqual(str(self.factory('192.0.2.0/255.255.255.0')),
      '192.0.2.0/24')
      for i in range(0, 33):
      # Generate and re-parse the CIDR format (trivial).
      net_str = '0.0.0.0/%d' % i
      net = self.factory(net_str)
      self.assertEqual(str(net), net_str)
      # Generate and re-parse the expanded netmask.
      self.assertEqual(
      str(self.factory('0.0.0.0/%s' % net.netmask)), net_str)
      # Zero prefix is treated as decimal.
      self.assertEqual(str(self.factory('0.0.0.0/0%d' % i)), net_str)
      # Generate and re-parse the expanded hostmask. The ambiguous
      # cases (/0 and /32) are treated as netmasks.
      if i in (32, 0):
      net_str = '0.0.0.0/%d' % (32 - i)
      self.assertEqual(
      str(self.factory('0.0.0.0/%s' % net.hostmask)), net_str)

      For safety, I added these attributes always, instead of remove them.

      PR-12836 is fixing it, and extracts methods for Network/Interface constructor
      (a part of PR-12774). It doesn't add additional mask form support.

    8. added
      3.8 (EOL)
      and removed
      3.7 (EOL)
      on Apr 15, 2019
    9. methane commented on Apr 15, 2019

      @methane
      Member

      New changeset 6fa84bd by Inada Naoki in branch 'master':
      bpo-27860: ipaddress: fix Interface missed some attributes (GH-12836)
      6fa84bd

    10. methane commented on Apr 15, 2019

      @methane
      Member

      I merged all cleanups.

      I don't merge accepting any IP representations as mask, because I'm not expert of this module.
      At least, I don't want to pass prefix/netmask by something like IPv4Network("0.0.255.255").

    11. SilentGhost commented on May 24, 2019

      SilentGhostmannequin
      Mannequin

      I was wondering why it was decided against backporting to 3.7? 6fa84bd fixes an actual bug bpo-35990 (string mask in tuple argument for IPv4Interfaces). Incidentally, there are also no tests for this behaviour.

      Just to note: both merged PRs lacked news entry.

    12. methane commented on May 26, 2019

      @methane
      Member

      I was wondering why it was decided against backporting to 3.7?

      Because I treats this is just a code cleanup.

      6fa84bd fixes an actual bug bpo-35990 (string mask in tuple argument for IPv4Interfaces).

      I didn't thought it was a bug.
      I don't know it is real bug which should be backported to 3.7 for now.

      Incidentally, there are also no tests for this behavior.

      Because I thought there are no change about public (documented) behavior.

      Just to note: both merged PRs lacked news entry.

      NEWS entry is not needed for code cleanup.

    13. jacktose commented on May 27, 2019

      jacktosemannequin
      Mannequin

      I for one have encountered bpo-35990 in my work and would appreciate a backport.

    14. methane commented on Jun 18, 2019

      @methane
      Member

      OK, I thought I improved only undocumented behavior, but it was documented and previous behavior didn't follow the document.
      I'll backport PR 12836.

    15. methane commented on Jun 19, 2019

      @methane
      Member

      New changeset f532fe5 by Inada Naoki in branch '3.7':
      bpo-27860: ipaddress: fix Interface constructor (GH-14200)
      f532fe5

    16. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      3.7 (EOL)3.8 (EOL)stdlibtype-feature

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions