Commit 1a507dd9 authored by Alexey Borzenkov's avatar Alexey Borzenkov

Fix c-ares on Windows + other fixes

parent 1f8b3a79
/* Copyright 1998 by the Massachusetts Institute of Technology.
*
* Permission to use, copy, modify, and distribute this
* software and its documentation for any purpose and without
* fee is hereby granted, provided that the above copyright
* notice appear in all copies and that both that copyright
* notice and this permission notice appear in supporting
* documentation, and that the name of M.I.T. not be used in
* advertising or publicity pertaining to distribution of the
* software without specific, written prior permission.
* M.I.T. makes no representations about the suitability of
* this software for any purpose. It is provided "as is"
* without express or implied warranty.
*/
#include "ares_setup.h"
#ifdef HAVE_LIMITS_H
# include <limits.h>
#endif
#include "ares.h"
#include "ares_private.h"
#ifndef HAVE_WRITEV
ssize_t ares_writev(ares_socket_t s, const struct iovec *iov, int iovcnt)
{
char *buffer, *bp;
int i;
size_t bytes = 0;
ssize_t result;
/* Validate iovcnt */
if (iovcnt <= 0)
{
SET_ERRNO(EINVAL);
return (-1);
}
/* Validate and find the sum of the iov_len values in the iov array */
for (i = 0; i < iovcnt; i++)
{
if (iov[i].iov_len > INT_MAX - bytes)
{
SET_ERRNO(EINVAL);
return (-1);
}
bytes += iov[i].iov_len;
}
if (bytes == 0)
return (0);
/* Allocate a temporary buffer to hold the data */
buffer = malloc(bytes);
if (!buffer)
{
SET_ERRNO(ENOMEM);
return (-1);
}
/* Copy the data into buffer */
for (bp = buffer, i = 0; i < iovcnt; ++i)
{
memcpy (bp, iov[i].iov_base, iov[i].iov_len);
bp += iov[i].iov_len;
}
/* Send buffer contents */
result = swrite(s, buffer, bytes);
free(buffer);
return (result);
}
#endif
......@@ -448,7 +448,7 @@ define(WATCHER, `WATCHER_BASE($1)
ACTIVE($1)')
define(INIT, `def __init__(self, loop loop$2):
define(INIT, `def __cinit__(self, loop loop$2):
libev.ev_$1_init(&self._watcher, <void *>gevent_callback_$1$3)
self.loop = loop
self._incref = 0')
......@@ -498,22 +498,39 @@ cdef public class io(watcher) [object PyGeventIOObject, type PyGeventIO_Type]:
WATCHER(io)
def __init__(self, loop loop, int fd, int events):
cdef int _initialized # for __dealloc__, whether io is initialized
def __cinit__(self, loop loop, int fd, int events):
IFDEF_WINDOWS()
fd = _open_osfhandle(fd)
ENDIF()
libev.ev_io_init(&self._watcher, <void *>gevent_callback_io, fd, events)
self._initialized = 1
self.loop = loop
self._incref = 0
def __dealloc__(self):
IFDEF_WINDOWS()
if self._initialized:
libev.close(self._watcher.fd)
ENDIF()
property fd:
def __get__(self):
return self._watcher.fd
cdef int fd = self._watcher.fd
IFDEF_WINDOWS()
fd = libev._get_osfhandle(fd)
ENDIF()
return fd
def __set__(self, int fd):
if libev.ev_is_active(&self._watcher):
raise AttributeError("'io' watcher attribute 'fd' is read-only while watcher is active")
IFDEF_WINDOWS()
fd = _open_osfhandle(fd) # careful, might raise
libev.close(self._watcher.fd) # close the old one
ENDIF()
libev.ev_io_init(&self._watcher, <void *>gevent_callback_io, fd, self._watcher.events)
property events:
......
......@@ -131,3 +131,5 @@ cdef extern from "libev.h":
# not from libev, but it's there to avoid collisions with gevent.core._open_osfhandle
int _open_osfhandle(int, int)
int _get_osfhandle(int)
void close(int)
......@@ -59,6 +59,8 @@ if os.path.exists('libev'):
def need_configure_ares():
if sys.platform == 'win32':
return False
if 'Generated from ares_build.h.in by configure' not in read('c-ares/ares_build.h'):
return True
if not os.path.exists('c-ares/ares_config.h'):
......@@ -96,12 +98,13 @@ if ares_embed:
ARES.sources += sorted(glob('c-ares/*.c'))
if sys.platform == 'win32':
ARES.libraries += ['advapi32']
ARES.define_macros += [('CARES_STATICLIB', '')]
else:
ARES.configure = configure_ares
ARES.define_macros += [('HAVE_CONFIG_H', '')]
ARES.define_macros += [('CARES_EMBED', '')]
if sys.platform != 'darwin':
ARES.libraries += ['rt']
ARES.define_macros += [('CARES_EMBED', '')]
def need_update(destination, *source):
......@@ -221,9 +224,6 @@ def read(name):
ext_modules = [CORE, ARES]
if sys.platform in ('win32'):
# XXX currently does not work
ext_modules.remove(ARES)
warnings = []
def warn(message):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment