Skip to content

Commit

Permalink
Refs gevent#38, fixed further ares.pyx encoding issue and tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
fantix committed Dec 6, 2012
1 parent 7d6e4b6 commit 56c691f
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
3 changes: 2 additions & 1 deletion gevent/ares.pyx
Expand Up @@ -35,6 +35,7 @@ cdef extern from "dnshelper.c":
struct ares_channeldata:
pass

object parse_h_name(hostent*)
object parse_h_aliases(hostent*)
object parse_h_addr_list(hostent*)
void* create_object_from_hostent(void*)
Expand Down Expand Up @@ -206,7 +207,7 @@ cdef void gevent_ares_host_callback(void *arg, int status, int timeouts, hostent
callback(result(None, gaierror(status, strerror(status))))
else:
try:
host_result = ares_host_result(host.h_addrtype, (host.h_name, parse_h_aliases(host), parse_h_addr_list(host)))
host_result = ares_host_result(host.h_addrtype, (parse_h_name(host), parse_h_aliases(host), parse_h_addr_list(host)))
except:
callback(result(None, sys.exc_info()[1]))
else:
Expand Down
15 changes: 12 additions & 3 deletions gevent/dnshelper.c
Expand Up @@ -14,7 +14,9 @@
#include "cares_pton.h"

#if PY_VERSION_HEX < 0x02060000
#define PyBytes_FromString PyString_FromString
#define PyUnicode_FromString PyString_FromString
#elif PY_MAJOR_VERSION < 3
#define PyUnicode_FromString PyBytes_FromString
#endif


Expand Down Expand Up @@ -49,7 +51,7 @@ gevent_append_addr(PyObject* list, int family, void* src, char* tmpbuf, size_t t
int status = -1;
PyObject* tmp;
if (ares_inet_ntop(family, src, tmpbuf, tmpsize)) {
tmp = PyBytes_FromString(tmpbuf);
tmp = PyUnicode_FromString(tmpbuf);
if (tmp) {
status = PyList_Append(list, tmp);
Py_DECREF(tmp);
Expand All @@ -59,6 +61,13 @@ gevent_append_addr(PyObject* list, int family, void* src, char* tmpbuf, size_t t
}


static PyObject *
parse_h_name(struct hostent *h)
{
return PyUnicode_FromString(h->h_name);
}


static PyObject*
parse_h_aliases(struct hostent *h)
{
Expand All @@ -72,7 +81,7 @@ parse_h_aliases(struct hostent *h)
for (pch = h->h_aliases; *pch != NULL; pch++) {
if (*pch != h->h_name && strcmp(*pch, h->h_name)) {
int status;
tmp = PyBytes_FromString(*pch);
tmp = PyUnicode_FromString(*pch);
if (tmp == NULL) {
break;
}
Expand Down
6 changes: 5 additions & 1 deletion gevent/resolver_ares.py
Expand Up @@ -3,7 +3,7 @@
import os
import sys
from _socket import getservbyname, getaddrinfo, gaierror, error
from gevent.hub import Waiter, get_hub, string_types, text_type, reraise
from gevent.hub import Waiter, get_hub, string_types, text_type, reraise, PY3, binary_type
from gevent.socket import AF_UNSPEC, AF_INET, AF_INET6, SOCK_STREAM, SOCK_DGRAM, SOCK_RAW, AI_NUMERICHOST, EAI_SERVICE, AI_PASSIVE
from gevent.ares import channel, InvalidIP

Expand Down Expand Up @@ -207,13 +207,17 @@ def _gethostbyaddr(self, ip_address):
if not result:
raise
_ip_address = result[0][-1][0]
if isinstance(_ip_address, text_type):
_ip_address = _ip_address.encode('ascii')
if _ip_address == ip_address:
raise
waiter.clear()
self.ares.gethostbyaddr(waiter, _ip_address)
return waiter.get()

def gethostbyaddr(self, ip_address):
if PY3 and isinstance(ip_address, binary_type):
ip_address = ip_address.decode('ascii')
ip_address = _resolve_special(ip_address, AF_UNSPEC)
while True:
ares = self.ares
Expand Down
2 changes: 2 additions & 0 deletions greentest/test__server.py
Expand Up @@ -295,11 +295,13 @@ def test_error_in_spawn(self):
self.init_server()
assert self.server.started
error = ExpectedError('test_error_in_spawn')

def error_spawn(*args):
try:
gevent.getcurrent().throw(error)
finally:
args = None

self.server._spawn = error_spawn
self.expect_one_error()
self.assertAcceptedConnectionError()
Expand Down

0 comments on commit 56c691f

Please sign in to comment.