Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ipaddress.ip_{address,network,interface} raises TypeError instead of ValueError if given a tuple as address #90573

Closed
thomascellerier mannequin opened this issue Jan 17, 2022 · 2 comments
Labels
3.8 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@thomascellerier
Copy link
Mannequin

thomascellerier mannequin commented Jan 17, 2022

BPO 46415
Nosy @JelleZijlstra, @thomascellerier
PRs
  • bpo-46415: Use f-string for ValueError in ipaddress.ip_{address,network,interface} helper functions #30642
  • 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 2022-01-21.04:53:15.129>
    created_at = <Date 2022-01-17.15:21:20.696>
    labels = ['3.8', 'type-bug', 'library']
    title = 'ipaddress.ip_{address,network,interface} raises TypeError instead of ValueError if given a tuple as address'
    updated_at = <Date 2022-01-21.04:53:15.128>
    user = 'https://github.com/thomascellerier'

    bugs.python.org fields:

    activity = <Date 2022-01-21.04:53:15.128>
    actor = 'JelleZijlstra'
    assignee = 'none'
    closed = True
    closed_date = <Date 2022-01-21.04:53:15.129>
    closer = 'JelleZijlstra'
    components = ['Library (Lib)']
    creation = <Date 2022-01-17.15:21:20.696>
    creator = 'thomascellerier'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 46415
    keywords = ['patch']
    message_count = 2.0
    messages = ['410798', '411081']
    nosy_count = 2.0
    nosy_names = ['JelleZijlstra', 'thomascellerier']
    pr_nums = ['30642']
    priority = 'normal'
    resolution = 'duplicate'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue46415'
    versions = ['Python 3.8']

    @thomascellerier
    Copy link
    Mannequin Author

    thomascellerier mannequin commented Jan 17, 2022

    IPv*Network and IPv*Interface constructors accept a 2-tuple of (address description, netmask) as the address parameter.
    When the tuple-based address is used errors are not propagated correctly through the ipaddress.ip_* helper because of the %-formatting now expecting several arguments:

    In [7]: ipaddress.ip_network(("192.168.100.0", "fooo"))
    \---------------------------------------------------------------------------
    
    	TypeError                                 Traceback (most recent call last)
    	<ipython-input-7-7fc0bff07012> in <module>
    	----> 1 ipaddress.ip_network(("192.168.100.0", "fooo"))
    /usr/lib/python3.8/ipaddress.py in ip_network(address, strict)
         81         pass
         82
    ---\> 83     raise ValueError('%r does not appear to be an IPv4 or IPv6 network' %
         84                      address)
         85
    
    TypeError: not all arguments converted during string formatting
    

    Compared to:

    In [8]: ipaddress.IPv4Network(("192.168.100.0", "foo"))
    \---------------------------------------------------------------------------
    
    	NetmaskValueError                         Traceback (most recent call last)
    	<ipython-input-8-79078758e653> in <module>
    	----> 1 ipaddress.IPv4Network(("192.168.100.0", "foo"))
    /usr/lib/python3.8/ipaddress.py in \_\_init__(self, address, strict)
       1453
       1454         self.network_address = IPv4Address(addr)
    -\> 1455         self.netmask, self.\_prefixlen = self.\_make_netmask(mask)
       1456         packed = int(self.network_address)
       1457         if packed & int(self.netmask) != packed:
    
    /usr/lib/python3.8/ipaddress.py in \_make_netmask(cls, arg)
       1118                     # Check for a netmask or hostmask in dotted-quad form.
       1119                     # This may raise NetmaskValueError.
    -\> 1120                     prefixlen = cls.\_prefix_from_ip_string(arg)
       1121             netmask = IPv4Address(cls.\_ip_int_from_prefix(prefixlen))
       1122             cls.\_netmask_cache[arg] = netmask, prefixlen
    
    /usr/lib/python3.8/ipaddress.py in \_prefix_from_ip_string(cls, ip_str)
        516             ip_int = cls.\_ip_int_from_string(ip_str)
        517         except AddressValueError:
    --\> 518             cls.\_report_invalid_netmask(ip_str)
        519
        520         # Try matching a netmask (this would be /1\*0*/ as a bitwise regexp).
    
    /usr/lib/python3.8/ipaddress.py in \_report_invalid_netmask(cls, netmask_str)
        472     def \_report_invalid_netmask(cls, netmask_str):
        473         msg = '%r is not a valid netmask' % netmask_str
    --\> 474         raise NetmaskValueError(msg) from None
        475
        476     @classmethod
    
    NetmaskValueError: 'foo' is not a valid netmask
    

    @thomascellerier thomascellerier mannequin added 3.8 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Jan 17, 2022
    @thomascellerier thomascellerier mannequin changed the title ipaddress.ip_{address,network,interface} raise TypeError instead of ValueError if given a tuple as address ipaddress.ip_{address,network,interface} raises TypeError instead of ValueError if given a tuple as address Jan 17, 2022
    @thomascellerier thomascellerier mannequin changed the title ipaddress.ip_{address,network,interface} raise TypeError instead of ValueError if given a tuple as address ipaddress.ip_{address,network,interface} raises TypeError instead of ValueError if given a tuple as address Jan 17, 2022
    @JelleZijlstra
    Copy link
    Member

    Duplicate of bpo-46141.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.8 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant