本文整理汇总了C++中PyObject_AsFileDescriptor函数的典型用法代码示例。如果您正苦于以下问题:C++ PyObject_AsFileDescriptor函数的具体用法?C++ PyObject_AsFileDescriptor怎么用?C++ PyObject_AsFileDescriptor使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了PyObject_AsFileDescriptor函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: strcaps_get_file
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
示例2: on_write_sendfile
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;
}
}
}
示例3: run
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;
}
示例4: PyObject_AsFileDescriptor
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);
}
示例5: session_startup
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;
}
示例6: strcaps_set_file
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; };
示例7: start_response_file
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;
}
示例8: poll_register
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;
}
示例9: _librsync_new_patchmaker
/* 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;
}
示例10: poll_unregister
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;
}
示例11: set_Io
/* 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;
}
示例12: igraphmodule_filehandle_init
/**
* \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;
}
示例13: manage_raw_response
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;
}
示例14: conv_descriptor
static int
conv_descriptor(PyObject *object, int *target)
{
int fd = PyObject_AsFileDescriptor(object);
if (fd < 0)
return 0;
*target = fd;
return 1;
}
示例15: file_conv
static int file_conv(PyObject *object, int *fdp)
{
int fd = PyObject_AsFileDescriptor(object);
if (fd < 0)
return 0;
*fdp = fd;
return 1;
}