Skip to content

Commit

Permalink
greendns: fix getaddrinfo parameter name for Python 3
Browse files Browse the repository at this point in the history
  • Loading branch information
tuokri committed Sep 17, 2023
1 parent dd2f0ea commit 3466872
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Contributors
* Victor Stinner
* Samuel Merritt
* Eric Urban
* Tuomo Kriikkula

Linden Lab Contributors
-----------------------
Expand Down
32 changes: 23 additions & 9 deletions eventlet/support/greendns.py
Original file line number Diff line number Diff line change
Expand Up @@ -538,14 +538,7 @@ def _getaddrinfo_lookup(host, family, flags):
return str(answer.qname), addrs


def getaddrinfo(host, port, family=0, socktype=0, proto=0, flags=0):
"""Replacement for Python's socket.getaddrinfo
This does the A and AAAA lookups asynchronously after which it
calls the OS' getaddrinfo(3) using the AI_NUMERICHOST flag. This
flag ensures getaddrinfo(3) does not use the network itself and
allows us to respect all the other arguments like the native OS.
"""
def _getaddrinfo_impl(host, port, family=0, type=0, proto=0, flags=0):
if isinstance(host, six.string_types):
host = host.encode('idna').decode('ascii')
elif isinstance(host, six.binary_type):
Expand All @@ -561,7 +554,7 @@ def getaddrinfo(host, port, family=0, socktype=0, proto=0, flags=0):
for addr in addrs:
try:
ai = socket.getaddrinfo(addr, port, family,
socktype, proto, aiflags)
type, proto, aiflags)
except socket.error as e:
if flags & socket.AI_ADDRCONFIG:
err = e
Expand All @@ -580,6 +573,27 @@ def getaddrinfo(host, port, family=0, socktype=0, proto=0, flags=0):
return res


def getaddrinfo(host, port, family=0, socktype=0, proto=0, flags=0):
"""Replacement for Python's socket.getaddrinfo
This does the A and AAAA lookups asynchronously after which it
calls the OS' getaddrinfo(3) using the AI_NUMERICHOST flag. This
flag ensures getaddrinfo(3) does not use the network itself and
allows us to respect all the other arguments like the native OS.
"""
return _getaddrinfo_impl(host, port, family, socktype, proto, flags)


if six.PY3:
_doc = getaddrinfo.__doc__

def getaddrinfo(host, port, family=0, type=0, proto=0, flags=0):
return _getaddrinfo_impl(host, port, family, type, proto, flags)

getaddrinfo.__doc__ = _doc
del _doc


def gethostbyname(hostname):
"""Replacement for Python's socket.gethostbyname"""
if is_ipv4_addr(hostname):
Expand Down
7 changes: 7 additions & 0 deletions tests/greendns_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"""Tests for the eventlet.support.greendns module"""

import os
import six
import socket
import tempfile
import time
Expand Down Expand Up @@ -794,6 +795,12 @@ def getaddrinfo(addr, port, family, socktype, proto, aiflags):
except socket.error as e:
assert e.errno == socket.EAI_ADDRFAMILY

def test_getaddrinfo_py2_py3_type_parameter(self):
if six.PY3:
greendns.getaddrinfo('localhost', None, type=0)
else:
greendns.getaddrinfo('localhost', None, socktype=0)


class TestIsIpAddr(tests.LimitedTestCase):

Expand Down

0 comments on commit 3466872

Please sign in to comment.