Beispiel #1
0
/*
 * Return disk mounted partitions as a list of tuples including device,
 * mount point and filesystem type.
 */
static PyObject *
psutil_disk_partitions(PyObject *self, PyObject *args)
{
    FILE *file;
    struct mnttab mt;
    PyObject *py_retlist = PyList_New(0);
    PyObject *py_tuple = NULL;

    if (py_retlist == NULL)
        return NULL;

    file = fopen(MNTTAB, "rb");
    if (file == NULL) {
        PyErr_SetFromErrno(PyExc_OSError);
        goto error;
    }

    while (getmntent(file, &mt) == 0) {
        py_tuple = Py_BuildValue(
                       "(ssss)",
                       mt.mnt_special,   // device
                       mt.mnt_mountp,    // mount point
                       mt.mnt_fstype,    // fs type
                       mt.mnt_mntopts);  // options
        if (py_tuple == NULL)
            goto error;
        if (PyList_Append(py_retlist, py_tuple))
            goto error;
        Py_DECREF(py_tuple);

    }
    fclose(file);
    return py_retlist;

error:
    Py_XDECREF(py_tuple);
    Py_DECREF(py_retlist);
    if (file != NULL)
        fclose(file);
    return NULL;
}
Beispiel #2
0
static int
dbm_ass_sub(dbmobject *dp, PyObject *v, PyObject *w)
{
    datum krec, drec;

    if (!PyArg_Parse(v, "s#", &krec.dptr, &krec.dsize) ) {
        PyErr_SetString(PyExc_TypeError,
                        "gdbm mappings have string indices only");
        return -1;
    }
    if (dp->di_dbm == NULL) {
        PyErr_SetString(DbmError,
                        "GDBM object has already been closed"); 
        return -1; 
    }
    dp->di_size = -1;
    if (w == NULL) {
        if (gdbm_delete(dp->di_dbm, krec) < 0) {
            PyErr_SetString(PyExc_KeyError,
                            PyString_AS_STRING((PyStringObject *)v));
            return -1;
        }
    }
    else {
        if (!PyArg_Parse(w, "s#", &drec.dptr, &drec.dsize)) {
            PyErr_SetString(PyExc_TypeError,
                            "gdbm mappings have string elements only");
            return -1;
        }
        errno = 0;
        if (gdbm_store(dp->di_dbm, krec, drec, GDBM_REPLACE) < 0) {
            if (errno != 0)
                PyErr_SetFromErrno(DbmError);
            else
                PyErr_SetString(DbmError,
                                gdbm_strerror(gdbm_errno));
            return -1;
        }
    }
    return 0;
}
Beispiel #3
0
/*
 * Return process pathname executable.
 * Thanks to Robert N. M. Watson:
 * http://fxr.googlebit.com/source/usr.bin/procstat/procstat_bin.c?v=8-CURRENT
 */
PyObject *
psutil_proc_exe(PyObject *self, PyObject *args) {
    long pid;
    char pathname[PATH_MAX];
    int error;
    int mib[4];
    int ret;
    size_t size;
    const char *encoding_errs;

    if (! PyArg_ParseTuple(args, "l", &pid))
        return NULL;

    mib[0] = CTL_KERN;
    mib[1] = KERN_PROC;
    mib[2] = KERN_PROC_PATHNAME;
    mib[3] = pid;

    size = sizeof(pathname);
    error = sysctl(mib, 4, pathname, &size, NULL, 0);
    if (error == -1) {
        PyErr_SetFromErrno(PyExc_OSError);
        return NULL;
    }
    if (size == 0 || strlen(pathname) == 0) {
        ret = psutil_pid_exists(pid);
        if (ret == -1)
            return NULL;
        else if (ret == 0)
            return NoSuchProcess();
        else
            strcpy(pathname, "");
    }

#if PY_MAJOR_VERSION >= 3
    return PyUnicode_DecodeFSDefault(pathname);
#else
    return Py_BuildValue("s", pathname);
#endif

}
Beispiel #4
0
/*
 * Return NIC MTU. References:
 * http://www.i-scream.org/libstatgrab/
 */
static PyObject *
psutil_net_if_mtu(PyObject *self, PyObject *args) {
    char *nic_name;
    int sock = 0;
    int ret;
#ifdef PSUTIL_SUNOS10
    struct lifreq lifr;
#else
    struct ifreq ifr;
#endif

    if (! PyArg_ParseTuple(args, "s", &nic_name))
        return NULL;

    sock = socket(AF_INET, SOCK_DGRAM, 0);
    if (sock == -1)
        goto error;

#ifdef PSUTIL_SUNOS10
    strncpy(lifr.lifr_name, nic_name, sizeof(lifr.lifr_name));
    ret = ioctl(sock, SIOCGIFMTU, &lifr);
#else
    strncpy(ifr.ifr_name, nic_name, sizeof(ifr.ifr_name));
    ret = ioctl(sock, SIOCGIFMTU, &ifr);
#endif
    if (ret == -1)
        goto error;
    close(sock);

#ifdef PSUTIL_SUNOS10
    return Py_BuildValue("i", lifr.lifr_mtu);
#else
    return Py_BuildValue("i", ifr.ifr_mtu);
#endif

error:
    if (sock != 0)
        close(sock);
    PyErr_SetFromErrno(PyExc_OSError);
    return NULL;
}
Beispiel #5
0
static PyObject *bits_disk_write(PyObject *self, PyObject *args)
{
    PyObject *pyfile;
    grub_file_t file;
    grub_disk_addr_t sector;
    const char *data;
    unsigned offset;
    int length;

    if (!PyArg_ParseTuple(args, "O!KIs#:disk_write", &PyFile_Type, &pyfile, &sector, &offset, &data, &length))
        return NULL;

    file = PyFile_AsFile(pyfile);
    if (!file->device->disk)
        return PyErr_Format(PyExc_RuntimeError, "Can't get disk device from non-disk-backed file");

    if (grub_disk_write(file->device->disk, sector, offset, length, data) != GRUB_ERR_NONE)
        return PyErr_SetFromErrno(PyExc_IOError);

    return Py_BuildValue("");
}
Beispiel #6
0
static PyObject *py_socket_write(pytalloc_Object *self, PyObject *args)
{
	char *buffer;
	int len;
	int *sock;
	int status;

	if (!PyArg_ParseTuple(args, "s#:write", &buffer, &len)) {
		return NULL;
	}

	sock = pytalloc_get_ptr(self);

	status = swrap_send(*sock, buffer, len, 0);
	if (status < 0) {
		PyErr_SetFromErrno(py_socket_error);
		return NULL;
	}

	Py_RETURN_NONE;
}
Beispiel #7
0
static PyObject *
method_sendfile(PyObject *self, PyObject *args)
{
    int out_fd, in_fd;
    off_t offset;
    size_t count;
    ssize_t sts;

    if (!PyArg_ParseTuple(args, "iiLk", &out_fd, &in_fd, &offset, &count))
        return NULL;
	 
    Py_BEGIN_ALLOW_THREADS;
    sts = sendfile(out_fd, in_fd, (off_t *) &offset, (ssize_t) count);
    Py_END_ALLOW_THREADS;
    if (sts == -1) {
        PyErr_SetFromErrno(PyExc_OSError);
        return NULL;
    } else {
        return Py_BuildValue("Lk", offset, sts);
    }
}
Beispiel #8
0
PyObject *
psutil_proc_cwd(PyObject *self, PyObject *args) {
    // Reference:
    // http://anoncvs.spacehopper.org/openbsd-src/tree/bin/ps/print.c#n179
    long pid;
    struct kinfo_proc kp;
    char path[MAXPATHLEN];
    size_t pathlen = sizeof path;

    if (! PyArg_ParseTuple(args, "l", &pid))
        return NULL;
    if (psutil_kinfo_proc(pid, &kp) == -1)
        return NULL;

    int name[] = { CTL_KERN, KERN_PROC_CWD, pid };
    if (sysctl(name, 3, path, &pathlen, NULL, 0) != 0) {
        PyErr_SetFromErrno(PyExc_OSError);
        return NULL;
    }
    return Py_BuildValue("s", path);
}
Beispiel #9
0
static PyObject *
time_clock_settime(PyObject *self, PyObject *args)
{
    int clk_id;
    PyObject *obj;
    struct timespec tp;
    int ret;

    if (!PyArg_ParseTuple(args, "iO:clock_settime", &clk_id, &obj))
        return NULL;

    if (_PyTime_ObjectToTimespec(obj, &tp.tv_sec, &tp.tv_nsec) == -1)
        return NULL;

    ret = clock_settime((clockid_t)clk_id, &tp);
    if (ret != 0) {
        PyErr_SetFromErrno(PyExc_IOError);
        return NULL;
    }
    Py_RETURN_NONE;
}
Beispiel #10
0
/*
 * Return a Python tuple representing user, kernel and idle CPU times
 */
static PyObject*
get_system_cpu_times(PyObject* self, PyObject* args)
{
    long cpu_time[CPUSTATES];
    size_t size;

    size = sizeof(cpu_time);

    if (sysctlbyname("kern.cp_time", &cpu_time, &size, NULL, 0) == -1) {
        PyErr_SetFromErrno(0);
        return NULL;
    }

    return Py_BuildValue("(ddddd)",
                         (double)cpu_time[CP_USER] / CLOCKS_PER_SEC,
                         (double)cpu_time[CP_NICE] / CLOCKS_PER_SEC,
                         (double)cpu_time[CP_SYS] / CLOCKS_PER_SEC,
                         (double)cpu_time[CP_IDLE] / CLOCKS_PER_SEC,
                         (double)cpu_time[CP_INTR] / CLOCKS_PER_SEC
    );
}
static PyObject *
SMBus_read_block_data(SMBus *self, PyObject *args)
{
	int addr, cmd;
	union i2c_smbus_data data;

	if (!PyArg_ParseTuple(args, "ii:read_block_data", &addr, &cmd))
		return NULL;

	SMBus_SET_ADDR(self, addr);

	/* save a bit of code by calling the access function directly */
	if (i2c_smbus_access(self->fd, I2C_SMBUS_READ, (__u8)cmd,
				I2C_SMBUS_BLOCK_DATA, &data)) {
		PyErr_SetFromErrno(PyExc_IOError);
		return NULL;
	}

	/* first byte of the block contains (remaining) data length */
	return SMBus_buf_to_list(&data.block[1], data.block[0]);
}
static PyObject *
read_init_file(EditLineObject *self, PyObject *args)
{
    PyObject *filename_obj = Py_None, *filename_bytes;
    
    if (!PyArg_ParseTuple(args, "|O:read_init_file", &filename_obj))
        return NULL;
    
    if (filename_obj == Py_None)
	return NULL;

    if (!PyUnicode_FSConverter(filename_obj, &filename_bytes))
	return NULL;

    errno = el_source(self->el, PyBytes_AsString(filename_bytes));

    Py_DECREF(filename_bytes);
    if (errno)
        return PyErr_SetFromErrno(PyExc_IOError);
    Py_RETURN_NONE;
}
static PyObject *
oss_write(oss_audio_t *self, PyObject *args)
{
    char *cp;
    int rv, size;

    if (!PyArg_ParseTuple(args, "s#:write", &cp, &size)) {
        return NULL;
    }

    Py_BEGIN_ALLOW_THREADS
    rv = write(self->fd, cp, size);
    Py_END_ALLOW_THREADS

    if (rv == -1) {
        return PyErr_SetFromErrno(PyExc_IOError);
    } else {
        self->ocount += rv;
    }
    return PyInt_FromLong(rv);
}
Beispiel #14
0
static PyObject *
time_gmtime(PyObject *self, PyObject *args)
{
    time_t when;
    struct tm buf, *local;

    if (!parse_time_t_args(args, "|O:gmtime", &when))
        return NULL;

    errno = 0;
    local = gmtime(&when);
    if (local == NULL) {
#ifdef EINVAL
        if (errno == 0)
            errno = EINVAL;
#endif
        return PyErr_SetFromErrno(PyExc_OSError);
    }
    buf = *local;
    return tmtotuple(&buf);
}
Beispiel #15
0
static PyObject * m_timerfd_create(PyObject *self, PyObject *args, PyObject *kwargs)
{
    char *kw[] = {"clockid", "flags", NULL};
    long clockid = 0, flags = 0;
    int t_res;
    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ll", kw, &clockid, &flags))
        return NULL;

    if (clockid == 0) {
        clockid = CLOCK_MONOTONIC;
    }

    t_res = timerfd_create(clockid, flags);
    if (t_res < 1) {
        return PyErr_SetFromErrno(PyExc_IOError);
        /* PyErr_SetString(ErrorObject, "timerfd_create system call faild."); */
        /* return NULL; */
    }

    return (PyObject *)PyLong_FromLong(t_res);
}//m_timerfd_create()
Beispiel #16
0
/* Return information about a file.

   On POSIX, use fstat().

   On Windows, use GetFileType() and GetFileInformationByHandle() which support
   files larger than 2 GiB.  fstat() may fail with EOVERFLOW on files larger
   than 2 GiB because the file size type is a signed 32-bit integer: see issue
   #23152.

   Raise an exception and return -1 on error. On Windows, set the last Windows
   error on error. On POSIX, set errno on error. Fill status and return 0 on
   success.

   Release the GIL to call GetFileType() and GetFileInformationByHandle(), or
   to call fstat(). The caller must hold the GIL. */
int
_Py_fstat(int fd, struct _Py_stat_struct *status)
{
    int res;

    assert(PyGILState_Check());

    Py_BEGIN_ALLOW_THREADS
    res = _Py_fstat_noraise(fd, status);
    Py_END_ALLOW_THREADS

    if (res != 0) {
#ifdef MS_WINDOWS
        PyErr_SetFromWindowsErr(0);
#else
        PyErr_SetFromErrno(PyExc_OSError);
#endif
        return -1;
    }
    return 0;
}
Beispiel #17
0
static PyObject *m_timerfd_expired(PyObject *self, PyObject *args)
{
    long fd = 0;
    uint64_t buf = 0;

    if (!PyArg_ParseTuple(args, "l", &fd)) {
        PyErr_SetString(
            PyExc_TypeError,
            "fd is not a valid type or the argument is missing");
        return NULL;
    }

    if (read(fd, &buf, sizeof(buf)) < 0) {
        if (errno != EAGAIN) {
            return PyErr_SetFromErrno(PyExc_IOError);
        }
        return PyLong_FromUnsignedLong(0);
    }

    return PyLong_FromUnsignedLong(buf);
}//m_timerfd_expired()
Beispiel #18
0
static int
SpiDev_set_bits_per_word(SpiDevObject *self, PyObject *val, void *closure)
{
	uint8_t bits;

	if (val == NULL) {
		PyErr_SetString(PyExc_TypeError,
			"Cannot delete attribute");
		return -1;
	}
#if PY_MAJOR_VERSION < 3
	if (PyInt_Check(val)) {
		bits = PyInt_AS_LONG(val);
	} else
#endif
	{
		if (PyLong_Check(val)) {
			bits = PyLong_AS_LONG(val);
		} else {
			PyErr_SetString(PyExc_TypeError,
				"The bits_per_word attribute must be an integer");
			return -1;
		}
	}

		if (bits < 8 || bits > 16) {
		PyErr_SetString(PyExc_TypeError,
			"invalid bits_per_word (8 to 16)");
		return -1;
	}

	if (self->bits_per_word != bits) {
		if (ioctl(self->fd, SPI_IOC_WR_BITS_PER_WORD, &bits) == -1) {
			PyErr_SetFromErrno(PyExc_IOError);
			return -1;
		}
		self->bits_per_word = bits;
	}
	return 0;
}
Beispiel #19
0
PyObject *
shm_attach(SharedMemory *self, void *address, int shmat_flags) {
    DPRINTF("attaching memory @ address %p with id %d using flags 0x%x\n",
             address, self->id, shmat_flags);

    self->address = shmat(self->id, address, shmat_flags);

    if ((void *)-1 == self->address) {
        self->address = NULL;
        switch (errno) {
            case EACCES:
                PyErr_SetString(pPermissionsException, "No permission to attach");
            break;

            case ENOMEM:
                PyErr_SetString(PyExc_MemoryError, "Not enough memory");
            break;

            case EINVAL:
                PyErr_SetString(PyExc_ValueError, "Invalid id, address, or flags");
            break;

            default:
                PyErr_SetFromErrno(PyExc_OSError);
            break;
        }

        goto error_return;
    }
    else {
        // memory was attached successfully
        self->read_only = (shmat_flags & SHM_RDONLY) ? 1 : 0;
        DPRINTF("set memory's internal read_only flag to %d\n", self->read_only);
    }

    Py_RETURN_NONE;

    error_return:
    return NULL;
}
Beispiel #20
0
static PyObject *
upload_effect(PyObject *self, PyObject *args)
{
    int fd, ret;
    PyObject* effect_data;
    ret = PyArg_ParseTuple(args, "iO", &fd, &effect_data);
    if (!ret) return NULL;

    void* data = PyBytes_AsString(effect_data);
    struct ff_effect effect = {};
    memmove(&effect, data, sizeof(struct ff_effect));

    // print_ff_effect(&effect);

    ret = ioctl(fd, EVIOCSFF, &effect);
    if (ret != 0) {
        PyErr_SetFromErrno(PyExc_IOError);
        return NULL;
    }

    return Py_BuildValue("i", effect.id);
}
Beispiel #21
0
static PyObject *
dski_channel_flush(PyObject *self, PyObject *args)
{
	struct dski_ioc_channel_ctrl chan_ctrl;
	int fd, ret, channel_id;

	if (!PyArg_ParseTuple(args, "O&i", file_conv, &fd, &channel_id))
		return NULL;

	chan_ctrl.channel_id = channel_id;

	Py_BEGIN_ALLOW_THREADS
	ret = ioctl(fd, DSKI_CHANNEL_FLUSH, &chan_ctrl);
	Py_END_ALLOW_THREADS
	if (ret < 0) {
		PyErr_SetFromErrno(PyExc_IOError);
		return NULL;
	}

	Py_INCREF(Py_None);
	return Py_None;
}
Beispiel #22
0
/* firstkey */
static PyObject *db_firstkey(nmdbobject *db, PyObject *args)
{
	unsigned char *key;
	size_t ksize;
	ssize_t rv;
	PyObject *r;

	if (!PyArg_ParseTuple(args, "")) {
		return NULL;
	}

	/* ksize is enough to hold the any value */
	ksize = 128 * 1024;
	key = malloc(ksize);
	if (key== NULL)
		return PyErr_NoMemory();

	Py_BEGIN_ALLOW_THREADS
	rv = nmdb_firstkey(db->db, key, ksize);
	Py_END_ALLOW_THREADS

	if (rv <= -2) {
		/* FIXME: define a better exception */
		r = PyErr_SetFromErrno(PyExc_IOError);
	} else if (rv == -1) {
		/* No first key or unsupported, handled in the high-level
		 * module. */
		r = PyLong_FromLong(-1);
	} else {
#ifdef PYTHON3
		r = PyBytes_FromStringAndSize((char *) key, rv);
#elif PYTHON2
		r = PyString_FromStringAndSize((char *) key, rv);
#endif
	}

	free(key);
	return r;
}
Beispiel #23
0
static PyObject *
oss_mixer_get(oss_mixer_t *self, PyObject *args)
{
    int channel, volume;

    if (!_is_fd_valid(self->fd))
        return NULL;

    /* Can't use _do_ioctl_1 because of encoded arg thingy. */
    if (!PyArg_ParseTuple(args, "i:get", &channel))
        return NULL;

    if (channel < 0 || channel > SOUND_MIXER_NRDEVICES) {
        PyErr_SetString(OSSAudioError, "Invalid mixer channel specified.");
        return NULL;
    }

    if (ioctl(self->fd, MIXER_READ(channel), &volume) == -1)
        return PyErr_SetFromErrno(PyExc_IOError);

    return Py_BuildValue("(ii)", volume & 0xff, (volume & 0xff00) >> 8);
}
Beispiel #24
0
static PyObject *
multiprocessing_sendfd(PyObject *self, PyObject *args)
{
    int conn, fd, res;
    struct iovec dummy_iov;
    char dummy_char;
    struct msghdr msg;
    struct cmsghdr *cmsg;
    union {
        struct cmsghdr hdr;
        unsigned char buf[CMSG_SPACE(sizeof(int))];
    } cmsgbuf;

    if (!PyArg_ParseTuple(args, "ii", &conn, &fd))
        return NULL;

    dummy_iov.iov_base = &dummy_char;
    dummy_iov.iov_len = 1;

    memset(&msg, 0, sizeof(msg));
    msg.msg_control = &cmsgbuf.buf;
    msg.msg_controllen = sizeof(cmsgbuf.buf);
    msg.msg_iov = &dummy_iov;
    msg.msg_iovlen = 1;

    cmsg = CMSG_FIRSTHDR(&msg);
    cmsg->cmsg_len = CMSG_LEN(sizeof(int));
    cmsg->cmsg_level = SOL_SOCKET;
    cmsg->cmsg_type = SCM_RIGHTS;
    * (int *) CMSG_DATA(cmsg) = fd;

    Py_BEGIN_ALLOW_THREADS
    res = sendmsg(conn, &msg, 0);
    Py_END_ALLOW_THREADS

    if (res < 0)
        return PyErr_SetFromErrno(PyExc_OSError);
    Py_RETURN_NONE;
}
Beispiel #25
0
static PyObject *
stdprinter_write(PyStdPrinter_Object *self, PyObject *args)
{
    char *c;
    Py_ssize_t n;

    if (self->fd < 0) {
        /* fd might be invalid on Windows
         * I can't raise an exception here. It may lead to an
         * unlimited recursion in the case stderr is invalid.
         */
        Py_RETURN_NONE;
    }

    if (!PyArg_ParseTuple(args, "s", &c)) {
        return NULL;
    }
    n = strlen(c);

    Py_BEGIN_ALLOW_THREADS
    errno = 0;
#if defined(MS_WIN64) || defined(MS_WINDOWS)
    if (n > INT_MAX)
        n = INT_MAX;
    n = write(self->fd, c, (int)n);
#else
    n = write(self->fd, c, n);
#endif
    Py_END_ALLOW_THREADS

    if (n < 0) {
        if (errno == EAGAIN)
            Py_RETURN_NONE;
        PyErr_SetFromErrno(PyExc_IOError);
        return NULL;
    }

    return PyLong_FromSsize_t(n);
}
Beispiel #26
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;
}
Beispiel #27
0
static PyObject *
SpiDev_readbytes(SpiDevObject *self, PyObject *args)
{
	uint8_t	rxbuf[SPIDEV_MAXPATH];
	int		status, len, ii;
	PyObject	*list;

	if (!PyArg_ParseTuple(args, "i:read", &len))
		return NULL;

	/* read at least 1 byte, no more than SPIDEV_MAXPATH */
	if (len < 1)
		len = 1;
	else if (len > sizeof(rxbuf))
		len = sizeof(rxbuf);

	memset(rxbuf, 0, sizeof rxbuf);
	status = read(self->fd, &rxbuf[0], len);

	if (status < 0) {
		PyErr_SetFromErrno(PyExc_IOError);
		return NULL;
	}

	if (status != len) {
		perror("short read");
		return NULL;
	}

	list = PyList_New(len);

	for (ii = 0; ii < len; ii++) {
		PyObject *val = Py_BuildValue("l", (long)rxbuf[ii]);
		PyList_SET_ITEM(list, ii, val);
	}

	Py_INCREF(list);
	return list;
}
static PyObject *
SMBus_write_i2c_block_data(SMBus *self, PyObject *args)
{
	int addr, cmd;
	union i2c_smbus_data data;

	if (!PyArg_ParseTuple(args, "iiO&:write_i2c_block_data", &addr, &cmd,
			SMBus_list_to_data, &data))
		return NULL;

	SMBus_SET_ADDR(self, addr);

	/* save a bit of code by calling the access function directly */
	if (i2c_smbus_access(self->fd, I2C_SMBUS_WRITE, (__u8)cmd,
				I2C_SMBUS_I2C_BLOCK_BROKEN, &data)) {
		PyErr_SetFromErrno(PyExc_IOError);
		return NULL;
	}

	Py_INCREF(Py_None);
	return Py_None;
}
PyObject *
PyGccRtl_str(struct PyGccRtl * self)
{
    /*
      We use print_rtl_single() which takes a FILE*, so
      we need an fmemopen buffer to write to:
    */
    char buf[2048]; /* FIXME */
    FILE *f;

    buf[0] = '\0';
    f = fmemopen(buf, sizeof(buf), "w");
    if (!f) {
        return PyErr_SetFromErrno(PyExc_IOError);
    }

    print_rtl_single (f, self->insn.inner);

    fclose(f);

    return PyGccString_FromString(buf);
}
Beispiel #30
0
/**
 * Open SPI device with given configuration
 * 
 * @param self
 * @param args
 * @param kwargs
 * @return none
 */
static PyObject* py_open(PyObject* self, PyObject* args, PyObject* kwargs){

    int ret;
    char *device;
    spi_config_t config = {0};

    /* Set default values */
    config.mode = 0;
    config.bits_per_word = 8;
    config.speed = 100000;
    config.delay = 0;

    /* Define keywords */
    static char *kwlist [] = {
        "device", "mode", "bits_per_word", "speed", "delay", NULL
    };

    /* Parse arguments */
    if(!PyArg_ParseTupleAndKeywords(
        args, kwargs, "s|iiii", kwlist,
        &device,
        &config.mode,
        &config.bits_per_word,
        &config.speed,
        &config.delay)){
        return NULL;
    }

    /* Open the device */
    ret = spi_open(device, config);

    if(ret < 0 ){
        return PyErr_SetFromErrno(PyExc_IOError);
    }else{
        fd = ret;
    }

    Py_RETURN_NONE;
}