static PyObject * strcaps_get_file(PyObject *self, PyObject *args) { // (int) fd / (str) path / (file) file PyObject *file; if (!PyArg_ParseTuple(args, "O", &file)) return NULL; cap_t caps; if (PyFile_Check(file)) caps = cap_get_fd(PyObject_AsFileDescriptor(file)); else if (PyInt_Check(file)) caps = cap_get_fd(PyInt_AsLong(file)); else if (PyString_Check(file)) caps = cap_get_file(PyString_AsString(file)); else if (PyUnicode_Check(file)) { PyObject *file_dec = PyUnicode_AsEncodedString( file, Py_FileSystemDefaultEncoding, "strict" ); if (file_dec == NULL) return NULL; caps = cap_get_file(PyString_AsString(file_dec)); Py_DECREF(file_dec); } else { PyErr_SetString( PyExc_TypeError, "Expecting file object, descriptor int or path string" ); return NULL; } size_t strcaps_len; char *strcaps; if (caps == NULL) { if (errno == ENODATA) { strcaps = "\0"; strcaps_len = 0; } else { PyErr_SetFromErrno(PyExc_OSError); return NULL; } } else strcaps = cap_to_text(caps, &strcaps_len); cap_free(caps); return Py_BuildValue("s#", strcaps, strcaps_len); }; // (str) caps
static write_state on_write_sendfile(struct ev_loop* mainloop, Request* request) { /* A sendfile response is split into two phases: * Phase A) sending HTTP headers * Phase B) sending the actual file contents */ if(request->current_chunk) { /* Phase A) -- current_chunk contains the HTTP headers */ if (do_send_chunk(request)) { // data left to send in the current chunk return not_yet_done; } else { assert(request->current_chunk == NULL); assert(request->current_chunk_p == 0); /* Transition to Phase B) -- abuse current_chunk_p to store the file fd */ request->current_chunk_p = PyObject_AsFileDescriptor(request->iterable); // don't stop yet, Phase B is still missing return not_yet_done; } } else { /* Phase B) -- current_chunk_p contains file fd */ if (do_sendfile(request)) { // Haven't reached the end of file yet return not_yet_done; } else { // Done with the file return done; } } }
static PyObject* run(PyObject* self, PyObject* args) { ServerInfo info; PyObject* socket; if(!PyArg_ParseTuple(args, "OO:server_run", &socket, &info.wsgi_app)) { return NULL; } info.sockfd = PyObject_AsFileDescriptor(socket); if (info.sockfd < 0) { return NULL; } info.host = NULL; if (PyObject_HasAttrString(socket, "getsockname")) { PyObject* sockname = PyObject_CallMethod(socket, "getsockname", NULL); if (sockname == NULL) { return NULL; } if (PyTuple_CheckExact(sockname) && PyTuple_GET_SIZE(sockname) == 2) { /* Standard (ipaddress, port) case */ info.host = PyTuple_GET_ITEM(sockname, 0); info.port = PyTuple_GET_ITEM(sockname, 1); } } _initialize_request_module(); server_run(&info); Py_RETURN_NONE; }
static PyObject *PyScript_io_add_watch(PyScript *self, PyObject *args, PyObject *kwds) { static char *kwlist[] = {"fd", "func", "data", "condition", NULL}; int fd = 0; PyObject *pyfd = NULL; PyObject *func = NULL; PyObject *data = NULL; int condition = G_IO_IN | G_IO_PRI; int ret; if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO|Oi", kwlist, &pyfd, &func, &data, &condition)) return NULL; fd = PyObject_AsFileDescriptor(pyfd); if (fd < 0) return NULL; if (!PyCallable_Check(func)) return PyErr_Format(PyExc_TypeError, "func not callable"); ret = pysource_io_add_watch_list(&self->sources, fd, condition, func, data); return PyInt_FromLong(ret); }
static PyObject * session_startup(SSH2_SessionObj *self, PyObject *args) { PyObject *sock; int ret; int fd; if (!PyArg_ParseTuple(args, "O:startup", &sock)) return NULL; if ((fd = PyObject_AsFileDescriptor(sock)) == -1) { PyErr_SetString(PyExc_ValueError, "argument must be a file descriptor"); return NULL; } Py_BEGIN_ALLOW_THREADS ret=libssh2_session_startup(self->session, fd); Py_END_ALLOW_THREADS CHECK_RETURN_CODE(ret, self) Py_DECREF(self->socket); Py_INCREF(sock); self->socket = sock; self->opened = 1; Py_RETURN_NONE; }
static PyObject * strcaps_set_file(PyObject *self, PyObject *args) { // (str) caps, (int) fd / (str) path / (file) file char *strcaps; PyObject *file; if (!PyArg_ParseTuple(args, "etO", Py_FileSystemDefaultEncoding, &strcaps, &file)) return NULL; cap_t caps; if ((caps = cap_from_text(strcaps)) == NULL) { PyErr_SetString(PyExc_ValueError, "Invalid capability specification"); PyMem_Free(strcaps); return NULL; } PyMem_Free(strcaps); int err; if (PyFile_Check(file)) err = cap_set_fd(PyObject_AsFileDescriptor(file), caps); else if (PyInt_Check(file)) err = cap_set_fd(PyInt_AsLong(file), caps); else if (PyString_Check(file)) err = cap_set_file(PyString_AsString(file), caps); else if (PyUnicode_Check(file)) { PyObject *file_dec = PyUnicode_AsEncodedString( file, Py_FileSystemDefaultEncoding, "strict" ); if (file_dec == NULL) return NULL; err = cap_set_file(PyString_AsString(file_dec), caps); Py_DECREF(file_dec); } else { PyErr_SetString( PyExc_TypeError, "Expecting file object, descriptor int or path string" ); cap_free(caps); return NULL; } cap_free(caps); if (err) { PyErr_SetFromErrno(PyExc_OSError); return NULL; } Py_RETURN_NONE; };
static response_status start_response_file(client_t *client) { PyObject *filelike; FileWrapperObject *filewrap; int ret,in_fd, size; struct stat info; filewrap = (FileWrapperObject *)client->response; filelike = filewrap->filelike; in_fd = PyObject_AsFileDescriptor(filelike); if (in_fd == -1) { PyErr_Clear(); DEBUG("can't get fd"); return STATUS_ERROR; } ret = write_headers(client, NULL, 0, 1); if(!client->content_length_set){ if (fstat(in_fd, &info) == -1){ PyErr_SetFromErrno(PyExc_IOError); /* write_error_log(__FILE__, __LINE__); */ call_error_logger(); return STATUS_ERROR; } size = info.st_size; client->content_length_set = 1; client->content_length = size; } return ret; }
static PyObject * poll_register(pollObject *self, PyObject *args) { PyObject *o, *key, *value; int fd, events = POLLIN | POLLPRI | POLLOUT; int err; if (!PyArg_ParseTuple(args, "O|i:register", &o, &events)) { return NULL; } fd = PyObject_AsFileDescriptor(o); if (fd == -1) return NULL; /* Add entry to the internal dictionary: the key is the file descriptor, and the value is the event mask. */ key = PyInt_FromLong(fd); if (key == NULL) return NULL; value = PyInt_FromLong(events); if (value == NULL) { Py_DECREF(key); return NULL; } err = PyDict_SetItem(self->dict, key, value); Py_DECREF(key); Py_DECREF(value); if (err < 0) return NULL; self->ufd_uptodate = 0; Py_INCREF(Py_None); return Py_None; }
/* Call with the basis file */ static PyObject* _librsync_new_patchmaker(PyObject* self, PyObject* args) { _librsync_PatchMakerObject* pm; PyObject *python_file; int fd; FILE *cfile; if (!PyArg_ParseTuple(args, "O:new_patchmaker", &python_file)) return NULL; fd = PyObject_AsFileDescriptor(python_file); if (fd == -1) { PyErr_SetString(PyExc_TypeError, "Need true file object"); return NULL; } Py_INCREF(python_file); pm = PyObject_New(_librsync_PatchMakerObject, &_librsync_PatchMakerType); if (pm == NULL) return NULL; pm->basis_file = python_file; cfile = fdopen(fd, "rb"); pm->patch_job = rs_patch_begin(rs_file_copy_cb, cfile); return (PyObject*)pm; }
static PyObject * poll_unregister(pollObject *self, PyObject *args) { PyObject *o, *key; int fd; if (!PyArg_ParseTuple(args, "O:unregister", &o)) { return NULL; } fd = PyObject_AsFileDescriptor( o ); if (fd == -1) return NULL; /* Check whether the fd is already in the array */ key = PyInt_FromLong(fd); if (key == NULL) return NULL; if (PyDict_DelItem(self->dict, key) == -1) { Py_DECREF(key); /* This will simply raise the KeyError set by PyDict_DelItem if the file descriptor isn't registered. */ return NULL; } Py_DECREF(key); self->ufd_uptodate = 0; Py_INCREF(Py_None); return Py_None; }
/* set the Io */ int set_Io(Io *self, PyObject *fd, int events) { int fdnum; #ifdef MS_WINDOWS if (!PyObject_TypeCheck(fd, PySocketModule.Sock_Type)) { PyErr_SetString(PyExc_TypeError, "only socket objects are supported " "in this configuration"); return -1; } #endif fdnum = PyObject_AsFileDescriptor(fd); if (fdnum == -1) { return -1; } #ifdef MS_WINDOWS fdnum = EV_WIN32_HANDLE_TO_FD(fdnum); if (fdnum == -1) { PyErr_SetFromWindowsErr(0); return -1; } #endif if (events & ~(EV_READ | EV_WRITE)) { PyErr_SetString(Error, "illegal event mask"); return -1; } ev_io_set(&self->io, fdnum, events); return 0; }
/** * \ingroup python_interface_filehandle * \brief Constructs a new file handle object from a Python object. * * \return 0 if everything was OK, 1 otherwise. An appropriate Python * exception is raised in this case. */ int igraphmodule_filehandle_init(igraphmodule_filehandle_t* handle, PyObject* object, char* mode) { #ifdef IGRAPH_PYTHON3 int fp; if (object == 0 || PyLong_Check(object)) { PyErr_SetString(PyExc_TypeError, "string or file-like object expected"); return 1; } #else if (object == 0 || (!PyBaseString_Check(object) && !PyFile_Check(object))) { PyErr_SetString(PyExc_TypeError, "string or file handle expected"); return 1; } #endif if (PyBaseString_Check(object)) { #ifdef IGRAPH_PYTHON3 handle->object = PyFile_FromObject(object, mode); #else handle->object = PyFile_FromString(PyString_AsString(object), mode); #endif if (handle->object == 0) return 1; } else { handle->object = object; Py_INCREF(handle->object); } /* At this stage, handle->object is something we can handle. * In Python 2, we get here only if object is a file object. In * Python 3, object can be anything, and PyFile_FromObject will * complain if the object cannot be converted to a file handle. */ #ifdef IGRAPH_PYTHON3 fp = PyObject_AsFileDescriptor(handle->object); if (fp == -1) { Py_DECREF(handle->object); return 1; } handle->fp = fdopen(fp, mode); if (handle->fp == 0) { Py_DECREF(handle->object); PyErr_SetString(PyExc_RuntimeError, "fdopen() failed unexpectedly"); return 1; } #else handle->fp = PyFile_AsFile(handle->object); if (handle->fp == 0) { Py_DECREF(handle->object); PyErr_SetString(PyExc_RuntimeError, "PyFile_AsFile() failed unexpectedly"); return 1; } #endif return 0; }
static int manage_raw_response(struct wsgi_request *wsgi_req) { int ret = 0; if (!wsgi_req->async_force_again) { ret = uwsgi_python_send_body(wsgi_req, (PyObject *) wsgi_req->async_result); if (ret == 0) { if (PyInt_Check((PyObject *) wsgi_req->async_result) || PyObject_HasAttrString((PyObject *) wsgi_req->async_result, "fileno")) { // is it a file ? int fd = PyObject_AsFileDescriptor((PyObject *) wsgi_req->async_result); if (fd >= 0) { wsgi_req->sendfile_fd = fd; uwsgi_response_sendfile_do(wsgi_req, fd, 0, 0); wsgi_req->sendfile_fd = -1; return UWSGI_OK; } } } } if (ret == 0) { if (!wsgi_req->async_placeholder) { wsgi_req->async_placeholder = PyObject_GetIter((PyObject *) wsgi_req->async_result); if (!wsgi_req->async_placeholder) return UWSGI_OK; } PyObject *pychunk = PyIter_Next((PyObject *) wsgi_req->async_placeholder); if (!pychunk) return UWSGI_OK; ret = uwsgi_python_send_body(wsgi_req, pychunk); if (ret == 0) { if (PyInt_Check(pychunk) || PyObject_HasAttrString(pychunk, "fileno")) { // is it a file ? int fd = PyObject_AsFileDescriptor(pychunk); if (fd >= 0) { wsgi_req->sendfile_fd = fd; uwsgi_response_sendfile_do(wsgi_req, fd, 0, 0); wsgi_req->sendfile_fd = -1; } } } Py_DECREF(pychunk); return UWSGI_AGAIN; } return UWSGI_OK; }
static int conv_descriptor(PyObject *object, int *target) { int fd = PyObject_AsFileDescriptor(object); if (fd < 0) return 0; *target = fd; return 1; }
static int file_conv(PyObject *object, int *fdp) { int fd = PyObject_AsFileDescriptor(object); if (fd < 0) return 0; *fdp = fd; return 1; }
FILE * libxml_PyFileGet(PyObject *f) { int fd, flags; FILE *res; const char *mode; fd = PyObject_AsFileDescriptor(f); if (!_PyVerify_fd(fd)) return(NULL); /* * Get the flags on the fd to understand how it was opened */ flags = fcntl(fd, F_GETFL, 0); switch (flags & O_ACCMODE) { case O_RDWR: if (flags & O_APPEND) mode = "a+"; else mode = "rw"; break; case O_RDONLY: if (flags & O_APPEND) mode = "r+"; else mode = "r"; break; case O_WRONLY: if (flags & O_APPEND) mode = "a"; else mode = "w"; break; default: return(NULL); } /* * the FILE struct gets a new fd, so that it can be closed * independently of the file descriptor given. The risk though is * lack of sync. So at the python level sync must be implemented * before and after a conversion took place. No way around it * in the Python3 infrastructure ! * The duplicated fd and FILE * will be released in the subsequent * call to libxml_PyFileRelease() which must be genrated accodingly */ fd = dup(fd); if (fd == -1) return(NULL); res = fdopen(fd, mode); if (res == NULL) { close(fd); return(NULL); } return(res); }
static int fdconv(PyObject* obj, void* p) { int fd; fd = PyObject_AsFileDescriptor(obj); if (fd >= 0) { *(int*)p = fd; return 1; } return 0; }
PyObject *py_uwsgi_sendfile(PyObject * self, PyObject * args) { struct wsgi_request *wsgi_req = current_wsgi_req(&uwsgi); if (!PyArg_ParseTuple(args, "Oi:uwsgi_sendfile", &wsgi_req->async_sendfile, &wsgi_req->sendfile_fd_chunk)) { return NULL; } #ifdef PYTHREE wsgi_req->sendfile_fd = PyObject_AsFileDescriptor(wsgi_req->async_sendfile); #else if (PyFile_Check((PyObject *)wsgi_req->async_sendfile)) { Py_INCREF((PyObject *)wsgi_req->async_sendfile); wsgi_req->sendfile_fd = PyObject_AsFileDescriptor(wsgi_req->async_sendfile); } #endif return PyTuple_New(0); }
static int rpmfd_init(rpmfdObject *s, PyObject *args, PyObject *kwds) { char *kwlist[] = { "obj", "mode", "flags", NULL }; const char *mode = "r"; const char *flags = "ufdio"; char *rpmio_mode = NULL; PyObject *fo = NULL; FD_t fd = NULL; int fdno; if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|ss", kwlist, &fo, &mode, &flags)) return -1; rpmio_mode = rstrscat(NULL, mode, ".", flags, NULL); if (PyBytes_Check(fo)) { fd = openPath(PyBytes_AsString(fo), rpmio_mode); } else if (PyUnicode_Check(fo)) { PyObject *enc = NULL; int rc; #if PY_MAJOR_VERSION >= 3 rc = PyUnicode_FSConverter(fo, &enc); #else rc = utf8FromPyObject(fo, &enc); #endif if (rc) { fd = openPath(PyBytes_AsString(enc), rpmio_mode); Py_DECREF(enc); } } else if (rpmfdObject_Check(fo)) { rpmfdObject *fdo = (rpmfdObject *)fo; fd = openFd(fdDup(Fileno(fdo->fd)), rpmio_mode); } else if ((fdno = PyObject_AsFileDescriptor(fo)) >= 0) { fd = openFd(fdDup(fdno), rpmio_mode); } else { PyErr_SetString(PyExc_TypeError, "path or file object expected"); } if (fd != NULL) { Fclose(s->fd); /* in case __init__ was called again */ free(s->mode); free(s->flags); s->fd = fd; s->mode = rstrdup(mode); s->flags = rstrdup(flags); } else { PyErr_SetString(PyExc_IOError, Fstrerror(fd)); } free(rpmio_mode); return (fd == NULL) ? -1 : 0; }
static int igraphmodule_i_filehandle_init_cpython_3(igraphmodule_filehandle_t* handle, PyObject* object, char* mode) { int fp; if (object == 0 || PyLong_Check(object)) { PyErr_SetString(PyExc_TypeError, "string or file-like object expected"); return 1; } handle->need_close = 0; handle->object = 0; if (PyBaseString_Check(object)) { /* We have received a string; we need to open the file denoted by this * string now and mark that we opened the file ourselves (so we need * to close it when igraphmodule_filehandle_destroy is invoked). */ handle->object = PyFile_FromObject(object, mode); if (handle->object == 0) { /* Could not open the file; just return an error code because an * exception was raised already */ return 1; } /* Remember that we need to close the file ourselves */ handle->need_close = 1; } else { /* This is probably a file-like object; store a reference for it and * we will handle it later */ handle->object = object; Py_INCREF(handle->object); } /* At this stage, handle->object is something we can handle. * We have to call PyObject_AsFileDescriptor instead * and then fdopen() it to get the corresponding FILE* object. */ fp = PyObject_AsFileDescriptor(handle->object); if (fp == -1) { igraphmodule_filehandle_destroy(handle); /* This already called Py_DECREF(handle->object), no need to call it */ return 1; } handle->fp = fdopen(fp, mode); if (handle->fp == 0) { igraphmodule_filehandle_destroy(handle); /* This already called Py_DECREF(handle->object), no need to call it */ PyErr_SetString(PyExc_RuntimeError, "fdopen() failed unexpectedly"); return 1; } return 0; }
FILE * pyfile_to_file(PyObject *pyfile, const char *mode) { #if PY_MAJOR_VERSION >= 3 int fd = PyObject_AsFileDescriptor(pyfile); if (fd < 0) { PyErr_Clear(); return NULL; } return fdopen(fd, mode); #else return PyFile_AsFile(pyfile); #endif }
void initioctl(void) { PyObject *zfs_ioctl = Py_InitModule("zfs.ioctl", zfsmethods); PyObject *zfs_util = PyImport_ImportModule("zfs.util"); PyObject *devfile; if (zfs_util == NULL) return; ZFSError = PyObject_GetAttrString(zfs_util, "ZFSError"); devfile = PyObject_GetAttrString(zfs_util, "dev"); zfsdevfd = PyObject_AsFileDescriptor(devfile); zfs_prop_init(); }
static bool Command_redirect_iostream(twopence_command_t *cmd, twopence_iofd_t dst, PyObject *object, twopence_buf_t **buf_ret) { if (object == NULL || PyByteArray_Check(object)) { twopence_buf_t *buffer; if (dst == TWOPENCE_STDIN && object == NULL) return true; /* Capture command output in a buffer */ buffer = twopence_command_alloc_buffer(cmd, dst, 65536); twopence_command_ostream_capture(cmd, dst, buffer); if (dst == TWOPENCE_STDIN) { unsigned int count = PyByteArray_Size(object); twopence_buf_ensure_tailroom(buffer, count); twopence_buf_append(buffer, PyByteArray_AsString(object), count); } if (buf_ret) *buf_ret = buffer; } else if (PyFile_Check(object)) { int fd = PyObject_AsFileDescriptor(object); if (fd < 0) { /* If this fails, we could also pull the content into a buffer and then send that */ PyErr_SetString(PyExc_TypeError, "unable to obtain file handle from File object"); return false; } /* We dup() the file descriptor so that we no longer have to worry * about what python does with its File object */ twopence_command_iostream_redirect(cmd, dst, dup(fd), true); } else if (object == Py_None) { /* Nothing */ } else { /* FIXME: we could check for a string type, and in that case interpret that as * the name of a file to write to. */ PyErr_SetString(PyExc_TypeError, "invalid type in stdio attribute"); return false; } return true; }
static int set_file_content_length(client_t *client, write_bucket *bucket) { struct stat info; int in_fd, ret = 0; size_t size = 0; Py_ssize_t valuelen = 0; FileWrapperObject *filewrap = NULL; PyObject *filelike = NULL, *length = NULL; char *value = NULL; filewrap = (FileWrapperObject *)client->response; filelike = filewrap->filelike; in_fd = PyObject_AsFileDescriptor(filelike); if (in_fd == -1) { call_error_logger(); return -1; } if (fstat(in_fd, &info) == -1){ PyErr_SetFromErrno(PyExc_IOError); return -1; } size = info.st_size; client->content_length_set = 1; client->content_length = size; DEBUG("set content length:%" PRIu64 , size); length = PyBytes_FromFormat("%zu", size); if (length == NULL) { return -1; } PyBytes_AsStringAndSize(length, &value, &valuelen); add_header(bucket, "Content-Length", 14, value, valuelen); ret = PyList_Append(bucket->temp1, length); if (ret == -1) { return -1; } Py_DECREF(length); return 1; }
static response_status process_sendfile(client_t *client) { PyObject *filelike = NULL; FileWrapperObject *filewrap = NULL; int in_fd, ret; filewrap = (FileWrapperObject *)client->response; filelike = filewrap->filelike; in_fd = PyObject_AsFileDescriptor(filelike); if (in_fd == -1) { PyErr_Clear(); return STATUS_OK; } while(client->content_length > client->write_bytes){ ret = write_sendfile(client->fd, in_fd, client->write_bytes, client->content_length); DEBUG("process_sendfile send %d", ret); switch (ret) { case 0: break; case -1: /* error */ if (errno == EAGAIN || errno == EWOULDBLOCK) { /* try again later */ //next DEBUG("process_sendfile EAGAIN %d", ret); return STATUS_SUSPEND; } else { /* fatal error */ client->keep_alive = 0; /* client->bad_request_code = 500; */ client->status_code = 500; //close return STATUS_ERROR; } default: client->write_bytes += ret; } } //all send //disable_cork(client); return close_response(client); }
int CheckFileWrapper(PyObject *obj) { FileWrapperObject *f; PyObject *filelike; int in_fd; if (obj->ob_type != &FileWrapperType){ return 0; } f = (FileWrapperObject *)obj; filelike = f->filelike; in_fd = PyObject_AsFileDescriptor(filelike); if (in_fd == -1) { PyErr_Clear(); return 0; } return 1; }
static int IO_init(libevwrapper_IO *self, PyObject *args, PyObject *kwds) { PyObject *socket; PyObject *callback; PyObject *loop; int io_flags = 0, fd = -1; struct ev_io *io = NULL; if (!PyArg_ParseTuple(args, "OiOO", &socket, &io_flags, &loop, &callback)) { return -1; } if (loop) { Py_INCREF(loop); self->loop = (libevwrapper_Loop *)loop; } if (callback) { if (!PyCallable_Check(callback)) { PyErr_SetString(PyExc_TypeError, "callback parameter must be callable"); Py_XDECREF(loop); return -1; } Py_INCREF(callback); self->callback = callback; } fd = PyObject_AsFileDescriptor(socket); if (fd == -1) { PyErr_SetString(PyExc_TypeError, "unable to get file descriptor from socket"); Py_XDECREF(callback); Py_XDECREF(loop); return -1; } io = &(self->io); ev_io_init(io, io_callback, fd, io_flags); self->io.data = self; return 0; }
static PyObject * setLogFile (PyObject * self, PyObject *arg) { FILE *fp; int fdno = PyObject_AsFileDescriptor(arg); if (fdno >= 0) { /* XXX we dont know the mode here.. guessing append for now */ fp = fdopen(fdno, "a"); if (fp == NULL) { PyErr_SetFromErrno(PyExc_IOError); return NULL; } } else if (arg == Py_None) { fp = NULL; } else { PyErr_SetString(PyExc_TypeError, "file object or None expected"); return NULL; } (void) rpmlogSetFile(fp); Py_RETURN_NONE; }
static PyObject *IORequest_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) { static char *kwlist[] = {"mode", "buf", "filelike", "offset", NULL}; long long offset; int bufflags, fd; short mode; PyObject *buf, *filelike; IORequest *rv; if (!PyArg_ParseTupleAndKeywords(args, kwargs, "hOOL", kwlist, &mode, &buf, &filelike, &offset)) return NULL; if (mode == IO_CMD_PREAD) bufflags = PyBUF_WRITABLE; else if (mode == IO_CMD_PWRITE) bufflags = PyBUF_SIMPLE; else { PyErr_SetString(PyExc_ValueError, "Invalid mode."); return NULL; } if ((fd = PyObject_AsFileDescriptor(filelike)) == -1) return NULL; if (!(rv = (IORequest*)type->tp_alloc(type, 0))) return NULL; rv->bufview.buf = NULL; if (PyObject_GetBuffer(buf, &rv->bufview, bufflags)) { Py_DECREF(rv); return NULL; } rv->iocb.aio_lio_opcode = mode; rv->iocb.aio_fildes = fd; rv->iocb.data = rv; rv->iocb.aio_reqprio = 0; rv->iocb.u.c.buf = rv->bufview.buf; rv->iocb.u.c.nbytes = rv->bufview.len; rv->iocb.u.c.offset = offset; rv->iocb.u.c.flags = IOCB_FLAG_RESFD; return (void*) rv; }
/* returns -1 and sets the Python exception if an error occurred, otherwise returns a number >= 0 */ static int seq2set(PyObject *seq, fd_set *set, pylist fd2obj[FD_SETSIZE + 1]) { int i; int max = -1; int index = 0; int len = -1; PyObject* fast_seq = NULL; PyObject* o = NULL; fd2obj[0].obj = (PyObject*)0; /* set list to zero size */ FD_ZERO(set); fast_seq=PySequence_Fast(seq, "arguments 1-3 must be sequences"); if (!fast_seq) return -1; len = PySequence_Fast_GET_SIZE(fast_seq); for (i = 0; i < len; i++) { SOCKET v; /* any intervening fileno() calls could decr this refcnt */ if (!(o = PySequence_Fast_GET_ITEM(fast_seq, i))) return -1; Py_INCREF(o); v = PyObject_AsFileDescriptor( o ); if (v == -1) goto finally; #if defined(_MSC_VER) max = 0; /* not used for Win32 */ #else /* !_MSC_VER */ if (v < 0 || v >= FD_SETSIZE) { PyErr_SetString(PyExc_ValueError, "filedescriptor out of range in select()"); goto finally; } if (v > max) max = v; #endif /* _MSC_VER */ FD_SET(v, set); /* add object and its file descriptor to the list */ if (index >= FD_SETSIZE) { PyErr_SetString(PyExc_ValueError, "too many file descriptors in select()"); goto finally; } fd2obj[index].obj = o; fd2obj[index].fd = v; fd2obj[index].sentinel = 0; fd2obj[++index].sentinel = -1; } Py_DECREF(fast_seq); return max+1; finally: Py_XDECREF(o); Py_DECREF(fast_seq); return -1; }