コード例 #1
0
ファイル: _struct.c プロジェクト: grobe0ba/plan9front
static PyObject *
s_unpack(PyObject *self, PyObject *inputstr)
{
	char *start;
	Py_ssize_t len;
	PyObject *args=NULL, *result;
	PyStructObject *soself = (PyStructObject *)self;
	assert(PyStruct_Check(self));
	assert(soself->s_codes != NULL);
	if (inputstr == NULL)
		goto fail;
	if (PyString_Check(inputstr) &&
		PyString_GET_SIZE(inputstr) == soself->s_size) {
			return s_unpack_internal(soself, PyString_AS_STRING(inputstr));
	}
	args = PyTuple_Pack(1, inputstr);
	if (args == NULL)
		return NULL;
	if (!PyArg_ParseTuple(args, "s#:unpack", &start, &len))
		goto fail;
	if (soself->s_size != len)
		goto fail;
	result = s_unpack_internal(soself, start);
	Py_DECREF(args);
	return result;

fail:
	Py_XDECREF(args);
	PyErr_Format(StructError,
		"unpack requires a string argument of length %zd",
		soself->s_size);
	return NULL;
}
コード例 #2
0
ファイル: _struct.c プロジェクト: grobe0ba/plan9front
static int
s_init(PyObject *self, PyObject *args, PyObject *kwds)
{
	PyStructObject *soself = (PyStructObject *)self;
	PyObject *o_format = NULL;
	int ret = 0;
	static char *kwlist[] = {"format", 0};

	assert(PyStruct_Check(self));

	if (!PyArg_ParseTupleAndKeywords(args, kwds, "S:Struct", kwlist,
					 &o_format))
		return -1;

	Py_INCREF(o_format);
	Py_XDECREF(soself->s_format);
	soself->s_format = o_format;

	ret = prepare_s(soself);
	return ret;
}
コード例 #3
0
static PyObject *
s_unpack(PyObject *self, PyObject *input)
{
	Py_buffer vbuf;
	PyObject *result;
	PyStructObject *soself = (PyStructObject *)self;

	assert(PyStruct_Check(self));
	assert(soself->s_codes != NULL);
	if (PyObject_GetBuffer(input, &vbuf, PyBUF_SIMPLE) < 0)
		return NULL;
	if (vbuf.len != soself->s_size) {
		PyErr_Format(StructError,
			     "unpack requires a bytes argument of length %zd",
			     soself->s_size);
                PyBuffer_Release(&vbuf);
		return NULL;
	}
	result = s_unpack_internal(soself, vbuf.buf);
	PyBuffer_Release(&vbuf);
	return result;
}
コード例 #4
0
static int
s_init(PyObject *self, PyObject *args, PyObject *kwds)
{
	PyStructObject *soself = (PyStructObject *)self;
	PyObject *o_format = NULL;
	int ret = 0;
	static char *kwlist[] = {"format", 0};

	assert(PyStruct_Check(self));

	if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:Struct", kwlist,
					 &o_format))
		return -1;

	if (PyUnicode_Check(o_format)) {
		o_format = PyUnicode_AsASCIIString(o_format);
		if (o_format == NULL)
			return -1;
	}
	/* XXX support buffer interface, too */
	else {
		Py_INCREF(o_format);
	}

	if (!PyBytes_Check(o_format)) {
		Py_DECREF(o_format);
		PyErr_Format(PyExc_TypeError,
			     "Struct() argument 1 must be bytes, not %.200s",
			     Py_TYPE(o_format)->tp_name);
		return -1;
	}

	Py_CLEAR(soself->s_format);
	soself->s_format = o_format;

	ret = prepare_s(soself);
	return ret;
}