• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C++ PyBytes_AsString函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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



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

示例1: compile_source

/* Given a string buffer containing Python source code, compile it
   and return a code object as a new reference. */
static PyObject *
compile_source(PyObject *pathname, PyObject *source)
{
    PyObject *code, *fixed_source;

    fixed_source = normalize_line_endings(source);
    if (fixed_source == NULL) {
        return NULL;
    }

    code = Py_CompileStringObject(PyBytes_AsString(fixed_source),
                                  pathname, Py_file_input, NULL, -1);

    Py_DECREF(fixed_source);
    return code;
}
开发者ID:cpcloud,项目名称:cpython,代码行数:18,代码来源:zipimport.c


示例2: PyCode_Addr2Line

int
PyCode_Addr2Line(PyCodeObject *co, int addrq)
{
    Py_ssize_t size = PyBytes_Size(co->co_lnotab) / 2;
    unsigned char *p = (unsigned char*)PyBytes_AsString(co->co_lnotab);
    int line = co->co_firstlineno;
    int addr = 0;
    while (--size >= 0) {
        addr += *p++;
        if (addr > addrq)
            break;
        line += (signed char)*p;
        p++;
    }
    return line;
}
开发者ID:FFMG,项目名称:myoddweb.piger,代码行数:16,代码来源:codeobject.c


示例3: conv_pyobj_reg

static int
conv_pyobj_reg(PyObject *pyobj, unsigned long *reg)
{
#ifdef IS_PY3K
    if (PyLong_Check(pyobj)) {
#else
    if (PyLong_Check(pyobj) || PyInt_Check(pyobj)) {
#endif
        *reg = PyLong_AsUnsignedLong(pyobj);
    }
    else if (PyBool_Check(pyobj)) {
        *reg = (pyobj == Py_True) ? 1 : 0;
    }
    else if (PyBytes_Check(pyobj)) {
        *reg = (unsigned long)PyBytes_AsString(pyobj);
    }
    else {
        PyErr_SetString(PyExc_TypeError, "argument should be unsigned long, bool or bytes");
        return 0;
    }

    if (PyErr_Occurred()) {
        return 0;
    }

    return 1;
}

static int
build_regs(PyObject *args[NUM_REGS], regs_t *regs)
{
    unsigned long *regarray[NUM_REGS] = {&regs->rax, &regs->rbx, &regs->rcx, &regs->rdx, &regs->rsi, &regs->rdi};

    memset(regs, 0, sizeof(*regs));

    int i;
    for (i = 0; i < NUM_REGS; i++) {
        if (args[i] == NULL) {
            continue;
        }
        else if (!conv_pyobj_reg(args[i], regarray[i])) {
            return 0;
        }
    }

    return 1;
}
开发者ID:6f70,项目名称:pyvmcall,代码行数:47,代码来源:pyvmcall.c


示例4: pyobj2doc_pair

static inline void
pyobj2doc_pair(PyObject *key, PyObject *value, rapidjson::Document& doc)
{
    const char *key_string;
#ifdef PY3
    PyObject *utf8_item;
    utf8_item = PyUnicode_AsUTF8String(key);
    key_string = PyBytes_AsString(utf8_item);
#else
    key_string = PyString_AsString(key);
#endif
    rapidjson::Value s;
    s.SetString(key_string, doc.GetAllocator());
    rapidjson::Value _v;
    pyobj2doc(value, _v, doc);
    doc.AddMember(s, _v, doc.GetAllocator());
}
开发者ID:ionelmc,项目名称:pyrapidjson,代码行数:17,代码来源:_pyrapidjson.cpp


示例5: PyObject_CallFunction

//-------------------------------------------------------------------------------------
std::string Pickler::pickle(PyObject* pyobj, int8 protocol)
{
	PyObject* pyRet = PyObject_CallFunction(picklerMethod_, 
		const_cast<char*>("(Oi)"), pyobj, protocol);
	
	SCRIPT_ERROR_CHECK();
	
	if(pyRet)
	{
		std::string str;
		str.assign(PyBytes_AsString(pyRet), PyBytes_Size(pyRet));
		S_RELEASE(pyRet);
		return str;
	}
	
	return "";
}
开发者ID:321543223,项目名称:kbengine,代码行数:18,代码来源:pickler.cpp


示例6: PyUnicode_DecodeUTF8

//-------------------------------------------------------------------------------------
void TelnetHandler::processPythonCommand(std::string command)
{
	if(pTelnetServer_->pScript() == NULL || command.size() == 0)
		return;
	
	command += "\n";
	PyObject* pycmd = PyUnicode_DecodeUTF8(command.data(), command.size(), NULL);
	if(pycmd == NULL)
	{
		SCRIPT_ERROR_CHECK();
		return;
	}

	DEBUG_MSG(fmt::format("TelnetHandler::processPythonCommand: size({}), command={}.\n", 
		command.size(), command));

	std::string retbuf = "";
	PyObject* pycmd1 = PyUnicode_AsEncodedString(pycmd, "utf-8", NULL);

	pTelnetServer_->pScript()->run_simpleString(PyBytes_AsString(pycmd1), &retbuf);

	// 把返回值中的'\n'替換成'\r\n',以解决在vt100终端中显示不正确的问题
	std::string::size_type pos = 0;
	while ((pos = retbuf.find('\n', pos)) != std::string::npos)
	{
		if (retbuf[pos - 1] != '\r')
		{
			retbuf.insert(pos, "\r");
			pos++;
		}
		pos++;
	}

	if(retbuf.size() > 0)
	{
		// 将结果返回给客户端
		Network::Bundle* pBundle = Network::Bundle::createPoolObject();
		(*pBundle) << retbuf;
		pEndPoint_->send(pBundle);
		Network::Bundle::reclaimPoolObject(pBundle);
		sendEnter();
	}

	Py_DECREF(pycmd);
	Py_DECREF(pycmd1);
}
开发者ID:1564143452,项目名称:kbengine,代码行数:47,代码来源:telnet_handler.cpp


示例7: die

RPM_GNUC_NORETURN
static void die(PyObject *cb)
{
    char *pyfn = NULL;
    PyObject *r;

    if (PyErr_Occurred()) {
	PyErr_Print();
    }
    if ((r = PyObject_Repr(cb)) != NULL) { 
	pyfn = PyBytes_AsString(r);
    }
    fprintf(stderr, _("error: python callback %s failed, aborting!\n"), 
	    	      pyfn ? pyfn : "???");
    rpmdbCheckTerminate(1);
    exit(EXIT_FAILURE);
}
开发者ID:kaltsi,项目名称:rpm,代码行数:17,代码来源:rpmts-py.c


示例8: PyBytes_AsString

// The Python simpleEnc function.
static PyObject *simpleEnc_encode(PyObject *self, PyObject *args) {

    PyBytesObject *srcObj;
    uint8_t *src;
    int srcSize;
    int pos;
    uint32_t numBytes;
    uint32_t matchPos;

    if (!PyArg_ParseTuple(args, "Oi", &srcObj, &pos))
        return NULL;

    src = (uint8_t *) PyBytes_AsString((PyObject*) srcObj);
    srcSize = PySequence_Length((PyObject*) srcObj);
    numBytes = simpleEnc(src, srcSize, pos, &matchPos);
    return Py_BuildValue("ii", numBytes, matchPos);
}
开发者ID:Lyrositor,项目名称:Majora-Demonic-Utility,代码行数:18,代码来源:simpleEnc.c


示例9: getitem

PyObject *
getitem(_MetadataLocationObject *self, PyObject *pykey)
{
    char *key, *value;

    if (check_MetadataLocationStatus(self))
        return NULL;

    if (!PyUnicode_Check(pykey) && !PyBytes_Check(pykey)) {
        PyErr_SetString(PyExc_TypeError, "Unicode or bytes expected!");
        return NULL;
    }

    if (PyUnicode_Check(pykey)) {
        pykey = PyUnicode_AsUTF8String(pykey);
    }

    key = PyBytes_AsString(pykey);

    value = NULL;

    if (!strcmp(key, "primary")) {
        value = self->ml->pri_xml_href;
    } else if (!strcmp(key, "filelists")) {
        value = self->ml->fil_xml_href;
    } else if (!strcmp(key, "other")) {
        value = self->ml->oth_xml_href;
    } else if (!strcmp(key, "primary_db")) {
        value = self->ml->pri_sqlite_href;
    } else if (!strcmp(key, "filelists_db")) {
        value = self->ml->fil_sqlite_href;
    } else if (!strcmp(key, "other_db")) {
        value = self->ml->oth_sqlite_href;
    } else if (!strcmp(key, "group")) {
        value = self->ml->groupfile_href;
    } else if (!strcmp(key, "group_gz")) {
        value = self->ml->cgroupfile_href;
    } else if (!strcmp(key, "updateinfo")) {
        value = self->ml->updateinfo_href;
    }

    if (value)
        return PyUnicode_FromString(value);
    else
        Py_RETURN_NONE;
}
开发者ID:Conan-Kudo,项目名称:createrepo_c,代码行数:46,代码来源:locate_metadata-py.c


示例10: read_history_file

static PyObject *
read_history_file(PyObject *self, PyObject *args)
{
    PyObject *filename_obj = Py_None, *filename_bytes;
    if (!PyArg_ParseTuple(args, "|O:read_history_file", &filename_obj))
        return NULL;
    if (filename_obj != Py_None) {
        if (!PyUnicode_FSConverter(filename_obj, &filename_bytes))
            return NULL;
        errno = read_history(PyBytes_AsString(filename_bytes));
        Py_DECREF(filename_bytes);
    } else
        errno = read_history(NULL);
    if (errno)
        return PyErr_SetFromErrno(PyExc_IOError);
    Py_RETURN_NONE;
}
开发者ID:3lnc,项目名称:cpython,代码行数:17,代码来源:readline.c


示例11: PyDict_GetItemString

char *GetCharFromDict(PyObject * dict, const char *key)
{
	PyObject *o, *o2 = NULL;
	char *ps = NULL, *result = NULL;
	size_t length;

	o = PyDict_GetItemString(dict, key);
	if (o == NULL) {
		PyErr_Format(PyExc_ValueError, "Missing key in dictionary: %s",
			     key);
		return NULL;
	}

	if (PyUnicode_Check(o)) {
		o2 = PyUnicode_AsASCIIString(o);
		if (o2 == NULL) {
			return NULL;
		}
		ps = PyBytes_AsString(o2);
	}
#if PY_MAJOR_VERSION < 3
	else if (PyString_Check(o)) {
		ps = PyString_AsString(o);
	}
#endif


	if (ps == NULL) {
		PyErr_Format(PyExc_ValueError,
			     "Can not get string value for key %s", key);
		goto out;
	}
	length = strlen(ps) + 1;
	result = (char *)malloc(length);
	if (result == NULL) {
		PyErr_Format(PyExc_ValueError, "Failed to allocate memory!");
		goto out;
	}
	memcpy(result, ps, length);

out:
	if (o2 != NULL) {
		Py_DECREF(o2);
	}
	return result;
}
开发者ID:kleinsasserm,项目名称:python-gammu,代码行数:46,代码来源:base.c


示例12: Index__find

PyObject *
Index__find(Index *self, PyObject *py_path)
{
    char *path;
    size_t idx;
    int err;

    path = PyBytes_AsString(py_path);
    if (!path)
        return NULL;

    err = git_index_find(&idx, self->index, path);
    if (err < 0)
        return Error_set_str(err, path);

    return PyLong_FromSize_t(idx);
}
开发者ID:flowroute,项目名称:pygit2,代码行数:17,代码来源:index.c


示例13: _py_get_callable_name

const gchar *
_py_get_callable_name(PyObject *callable, gchar *buf, gsize buf_len)
{
  PyObject *name = PyObject_GetAttrString(callable, "__name__");

  if (name)
    {
      g_strlcpy(buf, PyBytes_AsString(name), buf_len);
    }
  else
    {
      PyErr_Clear();
      g_strlcpy(buf, "<unknown>", buf_len);
    }
  Py_XDECREF(name);
  return buf;
}
开发者ID:pzoleex,项目名称:syslog-ng,代码行数:17,代码来源:python-helpers.c


示例14: patches

static PyObject *
patches(PyObject *self, PyObject *args)
{
	PyObject *text, *bins, *result;
	struct flist *patch;
	const char *in;
	char *out;
	Py_ssize_t len, outlen, inlen;

	if (!PyArg_ParseTuple(args, "OO:mpatch", &text, &bins))
		return NULL;

	len = PyList_Size(bins);
	if (!len) {
		/* nothing to do */
		Py_INCREF(text);
		return text;
	}

	if (PyObject_AsCharBuffer(text, &in, &inlen))
		return NULL;

	patch = fold(bins, 0, len);
	if (!patch)
		return NULL;

	outlen = calcsize(inlen, patch);
	if (outlen < 0) {
		result = NULL;
		goto cleanup;
	}
	result = PyBytes_FromStringAndSize(NULL, outlen);
	if (!result) {
		result = NULL;
		goto cleanup;
	}
	out = PyBytes_AsString(result);
	if (!apply(out, in, inlen, patch)) {
		Py_DECREF(result);
		result = NULL;
	}
cleanup:
	lfree(patch);
	return result;
}
开发者ID:Distrotech,项目名称:mercurial,代码行数:45,代码来源:mpatch.c


示例15: trie_has_prefix

static PyObject *
trie_has_prefix(trieobject *mp, PyObject *py_prefix)
{
    const char *prefix;
    int has_prefix;
#ifdef IS_PY3K
    PyObject* bytes;
#endif

    /* Make sure prefix is a string. */
#ifdef IS_PY3K
    if(!PyUnicode_Check(py_prefix)) {
#else
    if(!PyString_Check(py_prefix)) {
#endif
        PyErr_SetString(PyExc_TypeError, "prefix must be a string");
        return NULL;
    }
#ifdef IS_PY3K
    bytes = PyUnicode_AsASCIIString(py_prefix);
    if(!bytes) {
        PyErr_SetString(PyExc_TypeError, "prefix must be an ASCII string");
        return NULL;
    }
    prefix = PyBytes_AsString(bytes);
#else
    prefix = PyString_AS_STRING(py_prefix);
#endif
    has_prefix = Trie_has_prefix(mp->trie, prefix);
#ifdef IS_PY3K
    Py_DECREF(bytes);
    return PyLong_FromLong((long)has_prefix);
#else
    return PyInt_FromLong((long)has_prefix);
#endif
}

static PyObject *
trie_has_prefix_onearg(trieobject *mp, PyObject *py_args)
{
    PyObject *py_arg;
    if(!PyArg_ParseTuple(py_args, "O", &py_arg))
        return NULL;
    return trie_has_prefix(mp, py_arg);
}
开发者ID:EverestAtWork,项目名称:biopython,代码行数:45,代码来源:triemodule.c


示例16: PySfPostFX_LoadFromMemory

static PyObject *
PySfPostFX_LoadFromMemory (PySfPostFX *self, PyObject *args)
{
	char *effect;
#ifdef IS_PY3K
	PyObject *string = PyUnicode_AsUTF8String(args);
	if (string == NULL)
		return NULL;
	effect = PyBytes_AsString(string);
#else
	effect = PyString_AsString(args);
#endif
	bool result = self->obj->LoadFromMemory(effect);
#ifdef IS_PY3K
	Py_DECREF(string);
#endif
	return PyBool_FromLong(result);
}
开发者ID:acgessler,项目名称:yiang,代码行数:18,代码来源:PostFX.cpp


示例17: FNFCIGETNEXTCABINET

static FNFCIGETNEXTCABINET(cb_getnextcabinet)
{
    if (pv) {
	PyObject *result = PyObject_CallMethod(pv, "getnextcabinet", "i", pccab->iCab);
	if (result == NULL)
	    return -1;
	if (!PyBytes_Check(result)) {
	    PyErr_Format(PyExc_TypeError, 
		"Incorrect return type %s from getnextcabinet",
		result->ob_type->tp_name);
	    Py_DECREF(result);
	    return FALSE;
	}
	strncpy(pccab->szCab, PyBytes_AsString(result), sizeof(pccab->szCab));
	return TRUE;
    }
    return FALSE;
}
开发者ID:cocoatomo,项目名称:CTPython,代码行数:18,代码来源:_msi.c


示例18: unicode_to_encoded_string

/* Returns a newly allocated string with the contents of the given unicode
   string object converted to CHARSET.  If an error occurs during the
   conversion, NULL will be returned and a python exception will be set.

   The caller is responsible for xfree'ing the string.  */
static gdb::unique_xmalloc_ptr<char>
unicode_to_encoded_string (PyObject *unicode_str, const char *charset)
{
  gdb::unique_xmalloc_ptr<char> result;

  /* Translate string to named charset.  */
  gdbpy_ref<> string (PyUnicode_AsEncodedString (unicode_str, charset, NULL));
  if (string == NULL)
    return NULL;

#ifdef IS_PY3K
  result.reset (xstrdup (PyBytes_AsString (string.get ())));
#else
  result.reset (xstrdup (PyString_AsString (string.get ())));
#endif

  return result;
}
开发者ID:Winter3un,项目名称:ctf_task,代码行数:23,代码来源:py-utils.c


示例19: getNameFromFile

const char* getNameFromFile(PyObject* value)
{
    const char* string = 0;
    PyObject *oname = PyObject_GetAttrString (value, "name");
    if (oname) {
        if (PyUnicode_Check (oname)) {
            string = PyUnicode_AsUTF8 (oname);
        }
        else if (PyBytes_Check (oname)) {
            string = PyBytes_AsString (oname);
        }
        Py_DECREF (oname);
    }

    if (!string)
        throw Base::TypeError("Unable to get filename");
    return string;
}
开发者ID:ickby,项目名称:FreeCAD_sf_master,代码行数:18,代码来源:PropertyFile.cpp


示例20: PyArray_SortkindConverter

/*NUMPY_API
 * Convert object to sort kind
 */
NPY_NO_EXPORT int
PyArray_SortkindConverter(PyObject *obj, NPY_SORTKIND *sortkind)
{
    char *str;
    PyObject *tmp = NULL;

    if (PyUnicode_Check(obj)) {
        obj = tmp = PyUnicode_AsASCIIString(obj);
        if (obj == NULL) {
            return NPY_FAIL;
        }
    }

    *sortkind = NPY_QUICKSORT;
    str = PyBytes_AsString(obj);
    if (!str) {
        Py_XDECREF(tmp);
        return NPY_FAIL;
    }
    if (strlen(str) < 1) {
        PyErr_SetString(PyExc_ValueError,
                        "Sort kind string must be at least length 1");
        Py_XDECREF(tmp);
        return NPY_FAIL;
    }
    if (str[0] == 'q' || str[0] == 'Q') {
        *sortkind = NPY_QUICKSORT;
    }
    else if (str[0] == 'h' || str[0] == 'H') {
        *sortkind = NPY_HEAPSORT;
    }
    else if (str[0] == 'm' || str[0] == 'M') {
        *sortkind = NPY_MERGESORT;
    }
    else {
        PyErr_Format(PyExc_ValueError,
                     "%s is an unrecognized kind of sort",
                     str);
        Py_XDECREF(tmp);
        return NPY_FAIL;
    }
    Py_XDECREF(tmp);
    return NPY_SUCCEED;
}
开发者ID:alphaitis,项目名称:numpy,代码行数:47,代码来源:conversion_utils.c



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C++ PyBytes_AsStringAndSize函数代码示例发布时间:2022-05-30
下一篇:
C++ PyBytes_AS_STRING函数代码示例发布时间:2022-05-30
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap