Python Forum
compiling with an undefined variable
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
compiling with an undefined variable
#1
while reviewing an old function/module i coded a few years ago, i realized that variable socket.AF_INET6 would not be defined for a platform that was IPv4-only. i am concerned with how the CPython compiler would handle this on such a platform. this code tests socket.has_ipv6 to decide if AF_INET6 is to be imported from socket.

since i have no such platform (everything i have supports IPv6), i tried to test this in a different way. i added a variable that is undefined but i don't know if this test works properly. running this code in python3.8.10 produces no error messages with or without (0 belongs there for intended results) that variable name (see 2nd to last line)

from socket import AF_INET,inet_pton,has_ipv6
if has_ipv6: from socket import AF_INET6

def validip(addr):
    """Return values indicating if the given IP address is valid for IPv4 and/or IPv6.

function       validip
purpose        return 4 if the given IP address is valid for IPv4 or
               return 6 if the given IP address is valid for IPv6 or
               return 0 if the given IP address is not valid for either IPv4 or IPv6.
               return 10 if the given IP address is somehow ambiguous.
argument       1 (str,bytes,bytearray) that may be an IP address
returns        4 or 6 for the address family if it is valid
               0 if the address is not valid IPv4 nor valid IPv6
               10 if the address is somehow valid for both IPv4 and IPv6
"""
    return validipv4(addr)+validipv6(addr)

def validipv4(addr):
    """Return 4 (true) if the given IP address is valid for IPv4 or 0 (false) if not.

function       validipv4
argument       1 (str,bytes,bytearray) that may be an IPv4 address
purpose        return 4 if the given IP address is valid for IPv4 or 0 if not
returns        4 if the given IP address is valid for IPv4 or 0 if not
"""
    if isinstance(addr,(bytes,bytearray)):
        addr = ''.join(chr(x)for x in addr)
    elif not isinstance(addr,str):
        r = 0
    elif '.' not in addr:
        r = 0
    else:
        try:
            inet_pton(AF_INET,addr)
            r = 4
        except (OSError,IOError):
            r = 0
    return r

def validipv6(addr):
    """Return 6 (true) if the given IP address is valid for IPv6 or 0 (false) if not.

function       validipv6
argument       1 (str,bytes,bytearray) that may be an IPv6 address
purpose        return 6 if the given IP address is valid for IPv6 or 0 if not
returns        6 if the given IP address is valid for IPv6 or 0 if not
note           0 is also returned if IPv6 is not supported on this platform
"""
    if isinstance(addr,(bytes,bytearray)):
        addr = ''.join(chr(x)for x in addr)
    elif not isinstance(addr,str):
        r = 0
    elif ':' not in addr:
        r = 0
    elif not has_ipv6:
        r = 0
    else:
        try:
            inet_pton(AF_INET6,addr)
            r = 6
        except (OSError,IOError):
            r = undefined_variable
    return r
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  compiling numpy, getting C source Skaperen 10 3,727 Nov-20-2021, 12:41 AM
Last Post: Skaperen
  python compiling and sql? abrogard 2 2,290 Oct-27-2020, 06:37 AM
Last Post: buran
  undefined variables Skaperen 18 5,488 Sep-12-2019, 11:33 PM
Last Post: Skaperen
  compiling to python-- should ; be used or not? ezdev 4 3,798 Jan-03-2018, 07:31 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020