Skip to content

enable older ipaddr module, for compatibility with older versions of python #69

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion postgresql/test/test_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,10 @@
]

try:
import ipaddress
try:
import ipaddress
except ImportError:
import ipaddr as ipaddress

type_samples.extend([
('inet', [
Expand Down
60 changes: 42 additions & 18 deletions postgresql/types/io/pg_network.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,52 @@
from .. import INETOID, CIDROID, MACADDROID
from . import lib
import ipaddress
try:
import ipaddress
oid_to_type = {
MACADDROID : str,
INETOID: ipaddress._IPAddressBase,
CIDROID: ipaddress._BaseNetwork,
}
def inet_pack(ob, pack = lib.net_pack, Constructor = ipaddress.ip_address):
a = Constructor(ob)
return pack((a.version, None, a.packed))

oid_to_type = {
MACADDROID : str,
INETOID: ipaddress._IPAddressBase,
CIDROID: ipaddress._BaseNetwork,
}
def cidr_pack(ob, pack = lib.net_pack, Constructor = ipaddress.ip_network):
a = Constructor(ob)
return pack((a.version, a.prefixlen, a.network_address.packed))

def inet_unpack(data, unpack = lib.net_unpack, Constructor = ipaddress.ip_address):
version, mask, data = unpack(data)
return Constructor(data)

def cidr_unpack(data, unpack = lib.net_unpack, Constructor = ipaddress.ip_network):
version, mask, data = unpack(data)
return Constructor(data).supernet(new_prefix=mask)

except ImportError:
import ipaddr as ipaddress
oid_to_type = {
MACADDROID : str,
INETOID: ipaddress._IPAddrBase,
CIDROID: ipaddress._BaseNet,
}
def inet_pack(ob, pack = lib.net_pack, Constructor = ipaddress.IPAddress):
a = Constructor(ob)
return pack((a.version, None, a.packed))

def cidr_pack(ob, pack = lib.net_pack, Constructor = ipaddress.IPNetwork):
a = Constructor(ob)
return pack((a.version, a.prefixlen, a.network.packed))

def inet_pack(ob, pack = lib.net_pack, Constructor = ipaddress.ip_address):
a = Constructor(ob)
return pack((a.version, None, a.packed))
def inet_unpack(data, unpack = lib.net_unpack, Constructor = ipaddress.IPAddress):
version, mask, data = unpack(data)
return Constructor(data)

def cidr_pack(ob, pack = lib.net_pack, Constructor = ipaddress.ip_network):
a = Constructor(ob)
return pack((a.version, a.prefixlen, a.network_address.packed))
def cidr_unpack(data, unpack = lib.net_unpack, Constructor = ipaddress.IPNetwork):
version, mask, data = unpack(data)
return Constructor(data).supernet(new_prefix=mask)

def inet_unpack(data, unpack = lib.net_unpack, Constructor = ipaddress.ip_address):
version, mask, data = unpack(data)
return Constructor(data)

def cidr_unpack(data, unpack = lib.net_unpack, Constructor = ipaddress.ip_network):
version, mask, data = unpack(data)
return Constructor(data).supernet(new_prefix=mask)

oid_to_io = {
MACADDROID : (lib.macaddr_pack, lib.macaddr_unpack, str),
Expand Down