本文整理汇总了C++中PyBytes_AsStringAndSize函数的典型用法代码示例。如果您正苦于以下问题:C++ PyBytes_AsStringAndSize函数的具体用法?C++ PyBytes_AsStringAndSize怎么用?C++ PyBytes_AsStringAndSize使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了PyBytes_AsStringAndSize函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: set_string
int
set_string(
const char* propname,
PyObject* value,
char* dest,
Py_ssize_t maxlen) {
char* buffer;
Py_ssize_t len;
PyObject* ascii_obj = NULL;
int result = -1;
if (check_delete(propname, value)) {
return -1;
}
if (PyUnicode_Check(value)) {
ascii_obj = PyUnicode_AsASCIIString(value);
if (ascii_obj == NULL) {
goto end;
}
if (PyBytes_AsStringAndSize(ascii_obj, &buffer, &len) == -1) {
goto end;
}
} else if (PyBytes_Check(value)) {
if (PyBytes_AsStringAndSize(value, &buffer, &len) == -1) {
goto end;
}
} else {
PyErr_SetString(PyExc_TypeError, "value must be bytes or unicode");
goto end;
}
if (len > maxlen) {
PyErr_Format(
PyExc_ValueError,
"'%s' must be less than %u characters",
propname,
(unsigned int)maxlen);
goto end;
}
strncpy(dest, buffer, (size_t)maxlen);
result = 0;
end:
Py_XDECREF(ascii_obj);
return result;
}
示例2: py_hex_to_git_oid
size_t
py_hex_to_git_oid (PyObject *py_oid, git_oid *oid)
{
PyObject *py_hex;
int err;
char *hex;
Py_ssize_t len;
#if PY_MAJOR_VERSION == 2
/* Bytes (only supported in Python 2) */
if (PyBytes_Check(py_oid)) {
err = PyBytes_AsStringAndSize(py_oid, &hex, &len);
if (err)
return 0;
err = git_oid_fromstrn(oid, hex, len);
if (err < 0) {
PyErr_SetObject(Error_type(err), py_oid);
return 0;
}
return (size_t)len;
}
#endif
/* Unicode */
if (PyUnicode_Check(py_oid)) {
py_hex = PyUnicode_AsASCIIString(py_oid);
if (py_hex == NULL)
return 0;
err = PyBytes_AsStringAndSize(py_hex, &hex, &len);
if (err) {
Py_DECREF(py_hex);
return 0;
}
err = git_oid_fromstrn(oid, hex, len);
Py_DECREF(py_hex);
if (err < 0) {
PyErr_SetObject(Error_type(err), py_oid);
return 0;
}
return (size_t)len;
}
/* Type error */
PyErr_SetObject(PyExc_TypeError, py_oid);
return 0;
}
示例3: pytrap_send
static PyObject *
pytrap_send(PyObject *self, PyObject *args, PyObject *keywds)
{
uint32_t ifcidx;
PyObject *dataObj;
char *data;
Py_ssize_t data_size;
static char *kwlist[] = {"ifcidx", "data", NULL};
if (!PyArg_ParseTupleAndKeywords(args, keywds, "IO!", kwlist, &ifcidx, &PyBytes_Type, &dataObj)) {
return NULL;
}
PyBytes_AsStringAndSize(dataObj, &data, &data_size);
if (data_size > 0xFFFF) {
PyErr_SetString(TrapError, "Data length is out of range (0-65535)");
return NULL;
}
int ret = trap_send(ifcidx, data, (uint16_t) data_size);
if (ret == TRAP_E_TIMEOUT) {
PyErr_SetString(TimeoutError, "Timeout");
return NULL;
} else if (ret == TRAP_E_BAD_IFC_INDEX) {
PyErr_SetString(TrapError, "Bad index of IFC.");
return NULL;
}
Py_RETURN_NONE;
}
示例4: vm_set_mem
PyObject* vm_set_mem(VmMngr* self, PyObject* args)
{
PyObject *py_addr;
PyObject *py_buffer;
Py_ssize_t py_length;
char * buffer;
Py_ssize_t pysize;
uint64_t addr;
int ret;
if (!PyArg_ParseTuple(args, "OO", &py_addr, &py_buffer))
RAISE(PyExc_TypeError,"Cannot parse arguments");
PyGetInt_uint64_t(py_addr, addr);
if (!PyBytes_Check(py_buffer))
RAISE(PyExc_TypeError,"arg must be bytes");
pysize = PyBytes_Size(py_buffer);
if (pysize < 0) {
RAISE(PyExc_TypeError,"Python error");
}
PyBytes_AsStringAndSize(py_buffer, &buffer, &py_length);
ret = vm_write_mem(&self->vm_mngr, addr, buffer, pysize);
if (ret < 0)
RAISE(PyExc_TypeError, "Error in set_mem");
add_mem_write(&self->vm_mngr, addr, (size_t)pysize);
check_invalid_code_blocs(&self->vm_mngr);
Py_INCREF(Py_None);
return Py_None;
}
示例5: libvirt_charPtrSizeUnwrap
int libvirt_charPtrSizeUnwrap(PyObject *obj, char **str, Py_ssize_t *size)
{
int ret;
#if PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION <= 4
int isize;
#endif
*str = NULL;
*size = 0;
if (!obj) {
PyErr_SetString(PyExc_TypeError, "unexpected type");
return -1;
}
#if PY_MAJOR_VERSION > 2
ret = PyBytes_AsStringAndSize(obj, str, size);
#else
# if PY_MINOR_VERSION <= 4
ret = PyString_AsStringAndSize(obj, str, &isize);
*size = isize;
# else
ret = PyString_AsStringAndSize(obj, str, size);
# endif
#endif
return ret;
}
示例6: WebPEncodeRGB_wrapper
PyObject* WebPEncodeRGB_wrapper(PyObject* self, PyObject* args)
{
PyBytesObject *rgb_string;
int width;
int height;
int stride;
float quality_factor;
uint8_t *rgb;
uint8_t *output;
Py_ssize_t size;
size_t ret_size;
if (!PyArg_ParseTuple(args, "Siiif", &rgb_string, &width, &height, &stride, &quality_factor)) {
Py_INCREF(Py_None);
return Py_None;
}
PyBytes_AsStringAndSize((PyObject *) rgb_string, (char**)&rgb, &size);
if (stride * height > size) {
Py_INCREF(Py_None);
return Py_None;
}
ret_size = WebPEncodeRGB(rgb, width, height, stride, quality_factor, &output);
if (ret_size > 0) {
PyObject *ret = PyBytes_FromStringAndSize((char*)output, ret_size);
free(output);
return ret;
}
Py_INCREF(Py_None);
return Py_None;
}
示例7: if
static PyObject *py_ue_ihttp_request_set_content(ue_PyIHttpRequest *self, PyObject * args)
{
PyObject *py_obj;
if (!PyArg_ParseTuple(args, "O:set_content", &py_obj))
{
return NULL;
}
if (PyUnicode_Check(py_obj))
{
self->http_request->SetContentAsString(UTF8_TO_TCHAR(PyUnicode_AsUTF8(py_obj)));
}
else if (PyBytes_Check(py_obj))
{
char *buf = nullptr;
Py_ssize_t len = 0;
PyBytes_AsStringAndSize(py_obj, &buf, &len);
TArray<uint8> data;
data.Append((uint8 *)buf, len);
self->http_request->SetContent(data);
}
Py_INCREF(Py_None);
return Py_None;
}
示例8: PyString_AsStringAndSize
int
PyString_AsStringAndSize(PyObject *obj, char **buffer, Py_ssize_t *length)
{
PyObject *o;
int rv;
if (PyUnicode_Check(obj))
{
o = PyUnicode_AsEncodedString(obj, GetDatabaseEncodingName(), NULL);
errorCheck();
rv = PyBytes_AsStringAndSize(o, buffer, length);
Py_XDECREF(o);
return rv;
}
return PyBytes_AsStringAndSize(obj, buffer, length);
}
示例9: UnicodeResultProcessor_process
static PyObject *
UnicodeResultProcessor_process(UnicodeResultProcessor *self, PyObject *value)
{
const char *encoding, *errors;
char *str;
Py_ssize_t len;
if (value == Py_None)
Py_RETURN_NONE;
#if PY_MAJOR_VERSION >= 3
if (PyBytes_AsStringAndSize(value, &str, &len))
return NULL;
encoding = PyBytes_AS_STRING(self->encoding);
errors = PyBytes_AS_STRING(self->errors);
#else
if (PyString_AsStringAndSize(value, &str, &len))
return NULL;
encoding = PyString_AS_STRING(self->encoding);
errors = PyString_AS_STRING(self->errors);
#endif
return PyUnicode_Decode(str, len, encoding, errors);
}
示例10: is_socket_unix
static PyObject* is_socket_unix(PyObject *self, PyObject *args) {
int r;
int fd, type = 0, listening = -1;
char* path = NULL;
Py_ssize_t length = 0;
#if PY_MAJOR_VERSION >=3 && PY_MINOR_VERSION >= 1
_cleanup_Py_DECREF_ PyObject *_path = NULL;
if (!PyArg_ParseTuple(args, "i|iiO&:_is_socket_unix",
&fd, &type, &listening, Unicode_FSConverter, &_path))
return NULL;
if (_path) {
assert(PyBytes_Check(_path));
if (PyBytes_AsStringAndSize(_path, &path, &length))
return NULL;
}
#else
if (!PyArg_ParseTuple(args, "i|iiz#:_is_socket_unix",
&fd, &type, &listening, &path, &length))
return NULL;
#endif
r = sd_is_socket_unix(fd, type, listening, path, length);
if (set_error(r, path, NULL) < 0)
return NULL;
return PyBool_FromLong(r);
}
示例11: sophia_cursor_new
static PyObject *
sophia_cursor_new(SophiaDB *db, PyTypeObject *cursortype,
PyObject *args, PyObject *kwargs)
{
SophiaCursor *pcur;
int order = SPGTE;
char *begin = NULL;
PyObject *pbegin = NULL;
Py_ssize_t bsize = 0;
static char *keywords[] = {"start_key", "order", NULL};
ensure_is_opened(db, NULL);
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|Oi", keywords, &pbegin, &order)
|| (pbegin && pbegin != Py_None && PyBytes_AsStringAndSize(pbegin, &begin, &bsize) == -1))
return NULL;
pcur = PyObject_New(SophiaCursor, cursortype);
if (!pcur)
return NULL;
void *cursor = sp_cursor(db->db, order, begin, (size_t)bsize);
if (!cursor) {
PyObject_Del(pcur);
PyErr_SetString(SophiaError, sp_error(db->db));
return NULL;
}
Py_INCREF(db);
db->cursors++;
pcur->db = db;
pcur->cursor = cursor;
return (PyObject *)pcur;
}
示例12: sophia_db_get
static PyObject *
sophia_db_get(SophiaDB *db, PyObject *args)
{
char *key;
PyObject *pkey, *pvalue = NULL;
void *value;
Py_ssize_t ksize;
size_t vsize;
ensure_is_opened(db, NULL);
if (!PyArg_UnpackTuple(args, "get", 1, 2, &pkey, &pvalue)
|| PyBytes_AsStringAndSize(pkey, &key, &ksize) == -1)
return NULL;
int rv = sp_get(db->db, key, (size_t)ksize, &value, &vsize);
switch (rv) {
case 1:
pvalue = PyBytes_FromStringAndSize(value, (Py_ssize_t)vsize);
free(value);
return pvalue;
case 0:
if (pvalue)
return pvalue;
Py_RETURN_NONE;
default:
PyErr_SetString(SophiaError, sp_error(db->db));
return NULL;
}
}
示例13: vm_set_mem
PyObject *
vm_set_mem(JitCpu *self, PyObject *args) {
PyObject *py_addr;
PyObject *py_buffer;
Py_ssize_t py_length;
char *buffer;
Py_ssize_t pysize;
uint64_t addr;
int ret = 0x1337;
if (!PyArg_ParseTuple(args, "OO", &py_addr, &py_buffer))
return NULL;
PyGetInt_uint64_t(py_addr, addr);
if(!PyBytes_Check(py_buffer))
RAISE(PyExc_TypeError,"arg must be bytes");
pysize = PyBytes_Size(py_buffer);
if (pysize < 0) {
RAISE(PyExc_TypeError,"Python error");
}
PyBytes_AsStringAndSize(py_buffer, &buffer, &py_length);
ret = vm_write_mem(&(((VmMngr*)self->pyvm)->vm_mngr), addr, buffer, pysize);
if (ret < 0)
RAISE(PyExc_TypeError,"arg must be str");
Py_INCREF(Py_None);
return Py_None;
}
示例14:
/* exodus.codec.decode(big-endian bytes) -> list of instructions */
static PyObject *exodus_decode(PyObject *self, PyObject *args)
{
PyObject *arg, *insns, *insn;
uint8_t *mem;
Py_ssize_t sz, tsz;
if (!(arg = PyTuple_GetItem(args, 0)))
return NULL;
if (PyBytes_AsStringAndSize(arg, (char**)&mem, &sz) < 0)
return NULL;
if (!(insns = PyList_New(0)))
return NULL;
while (sz > 0) {
insn = do_decode(mem, sz, &tsz);
if (insn != NULL) {
if (PyList_Append(insns, insn) < 0) {
Py_DECREF(insns);
Py_DECREF(insn);
return NULL;
}
}
if (tsz > sz) tsz = sz; /* ??? */
sz -= tsz;
mem += tsz;
Py_DECREF(insn);
}
return insns;
}
示例15: PyBytes_AsStringAndSize
static PyObject *py_creds_set_utf16_password(PyObject *self, PyObject *args)
{
enum credentials_obtained obt = CRED_SPECIFIED;
int _obt = obt;
PyObject *newval = NULL;
DATA_BLOB blob = data_blob_null;
Py_ssize_t size = 0;
int result;
bool ok;
if (!PyArg_ParseTuple(args, "O|i", &newval, &_obt)) {
return NULL;
}
obt = _obt;
result = PyBytes_AsStringAndSize(newval, (char **)&blob.data, &size);
if (result != 0) {
PyErr_SetString(PyExc_RuntimeError, "Failed to convert passed value to Bytes");
return NULL;
}
blob.length = size;
ok = cli_credentials_set_utf16_password(PyCredentials_AsCliCredentials(self),
&blob, obt);
return PyBool_FromLong(ok);
}