当前位置: 首页>>代码示例>>C++>>正文


C++ PyBytes_GET_SIZE函数代码示例

本文整理汇总了C++中PyBytes_GET_SIZE函数的典型用法代码示例。如果您正苦于以下问题:C++ PyBytes_GET_SIZE函数的具体用法?C++ PyBytes_GET_SIZE怎么用?C++ PyBytes_GET_SIZE使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了PyBytes_GET_SIZE函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: complex__format__

static PyObject *
complex__format__(PyObject* self, PyObject* args)
{
    PyObject *format_spec;

    if (!PyArg_ParseTuple(args, "O:__format__", &format_spec))
        return NULL;
    if (PyBytes_Check(format_spec))
        return _PyComplex_FormatAdvanced(self,
                                         PyBytes_AS_STRING(format_spec),
                                         PyBytes_GET_SIZE(format_spec));
    if (PyUnicode_Check(format_spec)) {
        /* Convert format_spec to a str */
        PyObject *result;
        PyObject *str_spec = PyObject_Str(format_spec);

        if (str_spec == NULL)
            return NULL;

        result = _PyComplex_FormatAdvanced(self,
                                           PyBytes_AS_STRING(str_spec),
                                           PyBytes_GET_SIZE(str_spec));

        Py_DECREF(str_spec);
        return result;
    }
    PyErr_SetString(PyExc_TypeError, "__format__ requires str or unicode");
    return NULL;
}
开发者ID:youseatao,项目名称:Python-2.7.8,代码行数:29,代码来源:complexobject.c

示例2: p_memcpy

static uint32_t
p_memcpy(char *dst, struct p_place *p, uint32_t amount)
{
	struct p_list *pl = p->list;
	uint32_t offset = p->offset;
	uint32_t amount_left = amount;
	char *src;
	Py_ssize_t chunk_size;

	/* Nothing to read */
	if (pl == NULL)
		return(0);

	src = (PyBytes_AS_STRING(pl->data) + offset);
	chunk_size = PyBytes_GET_SIZE(pl->data) - offset;

	while (amount_left > 0)
	{
		uint32_t this_read =
			chunk_size < amount_left ? chunk_size : amount_left;

		memcpy(dst, src, this_read);
		dst = dst + this_read;
		amount_left = amount_left - this_read;

		pl = pl->next;
		if (pl == NULL)
			break;

		src = PyBytes_AS_STRING(pl->data);
		chunk_size = PyBytes_GET_SIZE(pl->data);
	}

	return(amount - amount_left);
}
开发者ID:wchorolque,项目名称:py-postgresql,代码行数:35,代码来源:buffer.c

示例3: cxBuffer_FromObject

//-----------------------------------------------------------------------------
// cxBuffer_FromObject()
//   Populate the string buffer from a unicode object.
//-----------------------------------------------------------------------------
static int cxBuffer_FromObject(
    udt_Buffer *buf,                    // buffer to fill
    PyObject *obj,                      // object (string or Unicode object)
    const char *encoding)               // encoding to use, if applicable
{
    if (!obj)
        return cxBuffer_Init(buf);
    if (encoding && PyUnicode_Check(obj)) {
        buf->obj = PyUnicode_AsEncodedString(obj, encoding, NULL);
        if (!buf->obj)
            return -1;
        buf->ptr = PyBytes_AS_STRING(buf->obj);
        buf->size = PyBytes_GET_SIZE(buf->obj);
        buf->numCharacters = PyUnicode_GET_SIZE(obj);
    } else if (PyBytes_Check(obj)) {
        Py_INCREF(obj);
        buf->obj = obj;
        buf->ptr = PyBytes_AS_STRING(buf->obj);
        buf->size = buf->numCharacters = PyBytes_GET_SIZE(buf->obj);
#if PY_MAJOR_VERSION < 3
    } else if (PyBuffer_Check(obj)) {
        if (PyObject_AsReadBuffer(obj, &buf->ptr, &buf->size) < 0)
            return -1;
        Py_INCREF(obj);
        buf->obj = obj;
        buf->numCharacters = buf->size;
#endif
    } else {
        PyErr_SetString(PyExc_TypeError, CXORA_TYPE_ERROR);
        return -1;
    }
    return 0;
}
开发者ID:15580056814,项目名称:hue,代码行数:37,代码来源:Buffer.c

示例4: p_at_least

/*
 * p_at_least - whether the position has at least given number of bytes.
 */
static char
p_at_least(struct p_place *p, uint32_t amount)
{
	int32_t current = 0;
	struct p_list *pl;

	pl = p->list;
	if (pl)
		current += PyBytes_GET_SIZE(pl->data) - p->offset;

	if (current >= amount)
		return((char) 1);

	if (pl)
	{
		for (pl = pl->next; pl != NULL; pl = pl->next)
		{
			current += PyBytes_GET_SIZE(pl->data);
			if (current >= amount)
				return((char) 1);
		}
	}

	return((char) 0);
}
开发者ID:wchorolque,项目名称:py-postgresql,代码行数:28,代码来源:buffer.c

示例5: strxor_function

static PyObject *
strxor_function(PyObject *self, PyObject *args)
{
    PyObject *a, *b, *retval;
    Py_ssize_t len_a, len_b;

    if (!PyArg_ParseTuple(args, "SS", &a, &b))
        return NULL;

    len_a = PyBytes_GET_SIZE(a);
    len_b = PyBytes_GET_SIZE(b);

    assert(len_a >= 0);
    assert(len_b >= 0);

    if (len_a != len_b) {
        PyErr_SetString(PyExc_ValueError, "length of both strings must be equal");
        return NULL;
    }

    /* Create return string */
    retval = PyBytes_FromStringAndSize(NULL, len_a);
    if (!retval) {
        return NULL;
    }

    /* retval := a ^ b */
    xor_strings(PyBytes_AS_STRING(retval), PyBytes_AS_STRING(a), PyBytes_AS_STRING(b), len_a);

    return retval;
}
开发者ID:monnerat,项目名称:iseriespython,代码行数:31,代码来源:strxor.c

示例6: decompress

static PyObject *
decompress(Decompressor *d, uint8_t *data, size_t len)
{
    size_t data_size = 0;
    PyObject *result;

    result = PyBytes_FromStringAndSize(NULL, INITIAL_BUFFER_SIZE);
    if (result == NULL)
        return NULL;
    d->lzs.next_in = data;
    d->lzs.avail_in = len;
    d->lzs.next_out = (uint8_t *)PyBytes_AS_STRING(result);
    d->lzs.avail_out = PyBytes_GET_SIZE(result);
    for (;;) {
        lzma_ret lzret;

        Py_BEGIN_ALLOW_THREADS
        lzret = lzma_code(&d->lzs, LZMA_RUN);
        data_size = (char *)d->lzs.next_out - PyBytes_AS_STRING(result);
        Py_END_ALLOW_THREADS
        if (catch_lzma_error(lzret))
            goto error;
        if (lzret == LZMA_GET_CHECK || lzret == LZMA_NO_CHECK)
            d->check = lzma_get_check(&d->lzs);
        if (lzret == LZMA_STREAM_END) {
            d->eof = 1;
            if (d->lzs.avail_in > 0) {
                Py_CLEAR(d->unused_data);
                d->unused_data = PyBytes_FromStringAndSize(
                        (char *)d->lzs.next_in, d->lzs.avail_in);
                if (d->unused_data == NULL)
                    goto error;
            }
            break;
        } else if (d->lzs.avail_in == 0) {
            break;
        } else if (d->lzs.avail_out == 0) {
            if (grow_buffer(&result) == -1)
                goto error;
            d->lzs.next_out = (uint8_t *)PyBytes_AS_STRING(result) + data_size;
            d->lzs.avail_out = PyBytes_GET_SIZE(result) - data_size;
        }
    }
    if (data_size != (size_t)PyBytes_GET_SIZE(result))
        if (_PyBytes_Resize(&result, data_size) == -1)
            goto error;
    return result;

error:
    Py_XDECREF(result);
    return NULL;
}
开发者ID:Alex9029,项目名称:cpython,代码行数:52,代码来源:_lzmamodule.c

示例7: ghash_function

static PyObject *
ghash_function(PyObject *self, PyObject *args)
{
    PyObject *data, *y, *exp_h;
    PyObject *retval = NULL;
    Py_ssize_t len_data, len_y, len_exp_h;

    if (!PyArg_ParseTuple(args, "SSS", &data, &y, &exp_h)) {
        goto out;
    }

    len_data = PyBytes_GET_SIZE(data);
    len_y = PyBytes_GET_SIZE(y);
    len_exp_h = PyBytes_GET_SIZE(exp_h);

    if (len_data%16!=0) {
        PyErr_SetString(PyExc_ValueError, "Length of data must be a multiple of 16 bytes.");
        goto out;
    }

    if (len_y!=16) {
        PyErr_SetString(PyExc_ValueError, "Length of y must be 16 bytes.");
        goto out;
    }

    if (len_exp_h!=sizeof(t_key_tables) && len_exp_h!=16) {
        PyErr_SetString(PyExc_ValueError, "Length of expanded key is incorrect.");
        goto out;
    }

    /* Create return string */
    retval = PyBytes_FromStringAndSize(NULL, 16);
    if (!retval) {
        goto out;
    }

    Py_BEGIN_ALLOW_THREADS;

#define PyBytes_Buffer(a)   (uint8_t*)PyBytes_AS_STRING(a)

    ghash(  PyBytes_Buffer(retval), PyBytes_Buffer(data), len_data,
            PyBytes_Buffer(y),
            PyBytes_Buffer(exp_h), len_exp_h );

#undef PyBytes_Buffer

     Py_END_ALLOW_THREADS;

out:
    return retval;
}
开发者ID:Legrandin,项目名称:pycrypto,代码行数:51,代码来源:galois.c

示例8: source_as_string

// from Python/bltinmodule.c
static const char *
source_as_string(PyObject *cmd, const char *funcname, const char *what, PyCompilerFlags *cf, PyObject **cmd_copy)
{
    const char *str;
    Py_ssize_t size;
    Py_buffer view;

    *cmd_copy = NULL;
    if (PyUnicode_Check(cmd)) {
        cf->cf_flags |= PyCF_IGNORE_COOKIE;
        str = PyUnicode_AsUTF8AndSize(cmd, &size);
        if (str == NULL)
            return NULL;
    }
    else if (PyBytes_Check(cmd)) {
        str = PyBytes_AS_STRING(cmd);
        size = PyBytes_GET_SIZE(cmd);
    }
    else if (PyByteArray_Check(cmd)) {
        str = PyByteArray_AS_STRING(cmd);
        size = PyByteArray_GET_SIZE(cmd);
    }
    else if (PyObject_GetBuffer(cmd, &view, PyBUF_SIMPLE) == 0) {
        /* Copy to NUL-terminated buffer. */
        *cmd_copy = PyBytes_FromStringAndSize(
            (const char *)view.buf, view.len);
        PyBuffer_Release(&view);
        if (*cmd_copy == NULL) {
            return NULL;
        }
        str = PyBytes_AS_STRING(*cmd_copy);
        size = PyBytes_GET_SIZE(*cmd_copy);
    }
    else {
        PyErr_Format(PyExc_TypeError,
          "%s() arg 1 must be a %s object",
          funcname, what);
        return NULL;
    }

    if (strlen(str) != (size_t)size) {
        PyErr_SetString(PyExc_ValueError,
                        "source code string cannot contain null bytes");
        Py_CLEAR(*cmd_copy);
        return NULL;
    }
    return str;
}
开发者ID:Michael0x2a,项目名称:typed_ast,代码行数:49,代码来源:typed_ast.c

示例9: BYTES_ADD_INCREMENTAL

NUITKA_MAY_BE_UNUSED static bool BYTES_ADD_INCREMENTAL(PyObject **operand1, PyObject *operand2) {
    assert(PyBytes_CheckExact(*operand1));
    assert(PyBytes_CheckExact(operand2));

    // Buffer
    Py_buffer wb;
    wb.len = -1;

    if (PyObject_GetBuffer(operand2, &wb, PyBUF_SIMPLE) != 0) {
        PyErr_Format(PyExc_TypeError, "can't concat %s to %s", Py_TYPE(operand2)->tp_name, Py_TYPE(*operand1)->tp_name);

        return false;
    }

    Py_ssize_t oldsize = PyBytes_GET_SIZE(*operand1);

    if (oldsize > PY_SSIZE_T_MAX - wb.len) {
        PyErr_NoMemory();
        PyBuffer_Release(&wb);
        return false;
    }
    if (_PyBytes_Resize(operand1, oldsize + wb.len) < 0) {
        PyBuffer_Release(&wb);
        return false;
    }

    memcpy(PyBytes_AS_STRING(*operand1) + oldsize, wb.buf, wb.len);
    PyBuffer_Release(&wb);
    return true;
}
开发者ID:kayhayen,项目名称:Nuitka,代码行数:30,代码来源:HelpersOperationBinaryInplaceAdd.c

示例10: PyUnicode_CopyPreferredEncoding

int
PyUnicode_CopyPreferredEncoding(char *buffer, Py_ssize_t buffer_size)
{
	PyObject *u = NULL;
	PyObject *b = NULL;
	Py_ssize_t len;

#if (PY_MAJOR_VERSION >= 3)
	u = PyUnicode_GetPreferredEncoding();
	if (u == NULL)
		goto error;
	b = PyUnicode_AsASCIIString(u);
#else
	b = PyUnicode_GetPreferredEncoding();
#endif
	if (b == NULL)
		goto error;

	len = PyBytes_GET_SIZE(b);
	if (len >= buffer_size)
		len = buffer_size - 1;

	strncpy(buffer, PyBytes_AS_STRING(b), len);
	buffer[len] = '\0';

	Py_XDECREF(u);
	Py_DECREF(b);
	return 1;
  error:
	Py_XDECREF(u);
	Py_XDECREF(b);
	return 0;
}
开发者ID:stefanholek,项目名称:rl,代码行数:33,代码来源:unicode.c

示例11: strxor_c_function

static PyObject *
strxor_c_function(PyObject *self, PyObject *args)
{
    PyObject *s, *retval;
    int c;
    Py_ssize_t length;

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

    if ((c < 0) || (c > 255)) {
        PyErr_SetString(PyExc_ValueError, "c must be in range(256)");
        return NULL;
    }

    length = PyBytes_GET_SIZE(s);
    assert(length >= 0);

    /* Create return string */
    retval = PyBytes_FromStringAndSize(NULL, length);
    if (!retval) {
        return NULL;
    }

    /* retval := a ^ chr(c)*length */
    xor_string_with_char(PyBytes_AS_STRING(retval), PyBytes_AS_STRING(s), (char) c, length);

    return retval;
}
开发者ID:monnerat,项目名称:iseriespython,代码行数:29,代码来源:strxor.c

示例12: Signature_tp_iter

static PyObject *
Signature_tp_iter(PyObject *self)
{
    SignatureIter *iter = PyObject_New(SignatureIter, &SignatureIterType);
    PyObject *self_as_bytes;

    if (!iter) return NULL;

#ifdef PY3
    self_as_bytes = PyUnicode_AsUTF8String(self);
    if (!self_as_bytes) {
        Py_CLEAR(iter);
        return NULL;
    }
#else
    self_as_bytes = self;
    Py_INCREF(self_as_bytes);
#endif

    if (PyBytes_GET_SIZE(self_as_bytes) > 0) {
        iter->bytes = self_as_bytes;
        dbus_signature_iter_init(&(iter->iter),
                                 PyBytes_AS_STRING(self_as_bytes));
    }
    else {
        /* this is a null string, make a null iterator */
        iter->bytes = NULL;
        Py_CLEAR(self_as_bytes);
    }
    return (PyObject *)iter;
}
开发者ID:lichinka,项目名称:dbus-python,代码行数:31,代码来源:signature.c

示例13: _generate_regex

static int
_generate_regex(PyObject *s, void *vg)
{
    int i, count;
    gen_state_t *g = (gen_state_t *)vg;
    regex_pt_t *p;
    const char *pystr;
    unsigned int pystrlen;

    DBGN(D_GEN, "-> regex has %d parts, each generating: [", SYMOBJ(s)->data.regex.n_parts);
    for (i = 0; i < SYMOBJ(s)->data.regex.n_parts; i++) {
        p = SYMOBJ(s)->data.regex.parts + i;
        if (gen_state_hit_limit(g) || gen_state_hit_depth(g))
            count = p->min_count;
        else
            count = rnd(rnd(p->max_count - p->min_count + 1)) + p->min_count;
        if (i)
            PDBGN(D_GEN, ",");
        PDBGN(D_GEN, "%d", count);
        pystr = PyBytes_AS_STRING(p->charset);
        pystrlen = PyBytes_GET_SIZE(p->charset);
        for ( ;count > 0; count--) {
            // TODO: unicode
            if (gen_state_write(g, &pystr[rnd(pystrlen)], 1))
                return -1;
        }
    }
    PDBGN(D_GEN, "]\n");
    return 0;
}
开发者ID:blackberry,项目名称:ALF,代码行数:30,代码来源:symbol.c

示例14: dbm_contains

static int
dbm_contains(PyObject *self, PyObject *arg)
{
    dbmobject *dp = (dbmobject *)self;
    datum key, val;
    Py_ssize_t size;

    if ((dp)->di_dbm == NULL) {
        PyErr_SetString(DbmError,
                        "DBM object has already been closed");
         return -1;
    }
    if (PyUnicode_Check(arg)) {
        key.dptr = PyUnicode_AsUTF8AndSize(arg, &size);
        key.dsize = size;
        if (key.dptr == NULL)
            return -1;
    }
    else if (!PyBytes_Check(arg)) {
        PyErr_Format(PyExc_TypeError,
                     "dbm key must be bytes or string, not %.100s",
                     arg->ob_type->tp_name);
        return -1;
    }
    else {
        key.dptr = PyBytes_AS_STRING(arg);
        key.dsize = PyBytes_GET_SIZE(arg);
    }
    val = dbm_fetch(dp->di_dbm, key);
    return val.dptr != NULL;
}
开发者ID:Martiusweb,项目名称:cpython,代码行数:31,代码来源:_dbmmodule.c

示例15: fixIndex

PyObjectHandle LuaToPythonConverter::convertToUnicode(lua_State* L, int index) {
  fixIndex(L, index);

  if (auto ref = getOpaqueRef(L, index)) {
    // Must convert to a Python unicode object.
    if (PyBytes_Check(ref->obj.get())) {
      char* data = PyBytes_AS_STRING(ref->obj.get());
      Py_ssize_t len = PyBytes_GET_SIZE(ref->obj.get());
      PyObjectHandle obj(PyUnicode_DecodeUTF8(data, len, "strict"));
      checkPythonError(obj, L, "convertToUnicode(bytes)");
      return obj;
    } else if (PyUnicode_Check(ref->obj.get())) {
      return ref->obj;
    } else {
      luaL_error(L, "neither bytes nor unicode");
    }
  }

  int type = lua_type(L, index);
  if (type != LUA_TSTRING) {
    luaL_error(L, "Invalid type for convertToUnicode: %d", type);
  }

  size_t len;
  const char* str = lua_tolstring(L, index, &len);
  PyObjectHandle obj(PyUnicode_DecodeUTF8(str, len, "strict"));
  checkPythonError(obj, L, "convertToUnicdoe");
  return obj;
}
开发者ID:SalemAmeen,项目名称:fblualib,代码行数:29,代码来源:LuaToPython.cpp


注:本文中的PyBytes_GET_SIZE函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。