本文整理汇总了C++中PyBytes_Size函数的典型用法代码示例。如果您正苦于以下问题:C++ PyBytes_Size函数的具体用法?C++ PyBytes_Size怎么用?C++ PyBytes_Size使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了PyBytes_Size函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: PyUnicode_AsASCIIString
static char *GetString( PyObject *value ) {
/*
* Name:
* GetString
* Purpose:
* Get a pointer to a null terminated string from a PyObject.
* It should be freed by the caller.
* Stolen from pyast
*/
char *result = NULL;
if( value && value != Py_None ) {
#ifdef USE_PY3K
PyObject *bytes = PyUnicode_AsASCIIString(value);
#else
PyObject *bytes = value;
#endif
if( bytes ) {
size_t nbytes = PyBytes_Size( bytes );
const char * bytestr = PyBytes_AS_STRING(bytes);
result = malloc( (nbytes+1) * sizeof(*result));
strcpy( result, bytestr );
#ifdef USE_PY3K
Py_DECREF(bytes);
#endif
}
}
return result;
}
示例2: OnSysRead
size_t OnSysRead(void *buffer, size_t bufsize)
{
if (bufsize == 0)
return 0;
wxPyThreadBlocker blocker;
PyObject* arglist = Py_BuildValue("(i)", bufsize);
PyObject* result = PyEval_CallObject(m_read, arglist);
Py_DECREF(arglist);
size_t o = 0;
if ((result != NULL) && PyBytes_Check(result)) {
o = PyBytes_Size(result);
if (o == 0)
m_lasterror = wxSTREAM_EOF;
if (o > bufsize)
o = bufsize;
memcpy((char*)buffer, PyBytes_AsString(result), o); // strings only, not unicode...
Py_DECREF(result);
}
else
m_lasterror = wxSTREAM_READ_ERROR;
return o;
}
示例3: match
/* Given the contents of a .py[co] file in a buffer, unmarshal the data
and return the code object. Return None if it the magic word doesn't
match (we do this instead of raising an exception as we fall back
to .py if available and we don't want to mask other errors).
Returns a new reference. */
static PyObject *
unmarshal_code(char *pathname, PyObject *data)
{
PyObject *code;
char *buf = PyBytes_AsString(data);
Py_ssize_t size = PyBytes_Size(data);
if (size <= 9) {
PyErr_SetString(PyExc_TypeError,
"bad pyc data");
return NULL;
}
if (get_long((unsigned char *)buf) != PyImport_GetMagicNumber()) {
if (Py_VerboseFlag)
PySys_WriteStderr("# %s has bad magic\n",
pathname);
Py_INCREF(Py_None);
return NULL;
}
code = PyMarshal_ReadObjectFromString(buf + 8, size - 8);
if (code == NULL)
return NULL;
if (!PyCode_Check(code)) {
Py_DECREF(code);
PyErr_Format(PyExc_TypeError,
"compiled module %.200s is not a code object",
pathname);
return NULL;
}
return code;
}
示例4: Py_INCREF
static PyObject *dash_getpowhash(PyObject *self, PyObject *args)
{
char *output;
PyObject *value;
#if PY_MAJOR_VERSION >= 3
PyBytesObject *input;
#else
PyStringObject *input;
#endif
if (!PyArg_ParseTuple(args, "S", &input))
return NULL;
Py_INCREF(input);
output = PyMem_Malloc(32);
#if PY_MAJOR_VERSION >= 3
dash_hash((char *)PyBytes_AsString((PyObject*) input), (int)PyBytes_Size((PyObject*) input), output);
#else
dash_hash((char *)PyString_AsString((PyObject*) input), (int)PyString_Size((PyObject*) input), output);
#endif
Py_DECREF(input);
#if PY_MAJOR_VERSION >= 3
value = Py_BuildValue("y#", output, 32);
#else
value = Py_BuildValue("s#", output, 32);
#endif
PyMem_Free(output);
return value;
}
示例5: Rlite_init
static int Rlite_init(hirlite_RliteObject *self, PyObject *args, PyObject *kwds) {
static char *kwlist[] = { "path", "encoding", NULL };
PyObject *encodingObj = NULL;
char *path = ":memory:";
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|sO", kwlist, &path, &encodingObj))
return -1;
if (encodingObj) {
PyObject *encbytes;
char *encstr;
Py_ssize_t enclen;
if (PyUnicode_Check(encodingObj))
encbytes = PyUnicode_AsASCIIString(encodingObj);
else
encbytes = PyObject_Bytes(encodingObj);
if (encbytes == NULL)
return -1;
enclen = PyBytes_Size(encbytes);
encstr = PyBytes_AsString(encbytes);
self->encoding = (char*)malloc(enclen+1);
memcpy(self->encoding, encstr, enclen);
self->encoding[enclen] = '\0';
Py_DECREF(encbytes);
}
self->context = rliteConnect(path, 0);
return 0;
}
示例6: chunker_fill
static int
chunker_fill(Chunker *c)
{
size_t n;
memmove(c->data, c->data + c->last, c->position + c->remaining - c->last);
c->position -= c->last;
c->last = 0;
n = c->buf_size - c->position - c->remaining;
if(c->eof || n == 0) {
return 1;
}
PyObject *data = PyObject_CallMethod(c->fd, "read", "i", n);
if(!data) {
return 0;
}
n = PyBytes_Size(data);
if(n) {
memcpy(c->data + c->position + c->remaining, PyBytes_AsString(data), n);
c->remaining += n;
c->bytes_read += n;
}
else {
c->eof = 1;
}
Py_DECREF(data);
return 1;
}
示例7: chunker_fill
static int
chunker_fill(Chunker *c)
{
ssize_t n;
size_t offset, length;
PyObject *data;
memmove(c->data, c->data + c->last, c->position + c->remaining - c->last);
c->position -= c->last;
c->last = 0;
n = c->buf_size - c->position - c->remaining;
if(c->eof || n == 0) {
return 1;
}
if(c->fh >= 0) {
offset = c->bytes_read;
// if we have a os-level file descriptor, use os-level API
n = read(c->fh, c->data + c->position + c->remaining, n);
if(n > 0) {
c->remaining += n;
c->bytes_read += n;
}
else
if(n == 0) {
c->eof = 1;
}
else {
// some error happened
return 0;
}
length = c->bytes_read - offset;
#if ( _XOPEN_SOURCE >= 600 || _POSIX_C_SOURCE >= 200112L )
// We tell the OS that we do not need the data that we just have read any
// more (that it maybe has in the cache). This avoids that we spoil the
// complete cache with data that we only read once and (due to cache
// size limit) kick out data from the cache that might be still useful
// for the OS or other processes.
if (length > 0) {
posix_fadvise(c->fh, (off_t) offset, (off_t) length, POSIX_FADV_DONTNEED);
}
#endif
}
else {
// no os-level file descriptor, use Python file object API
data = PyObject_CallMethod(c->fd, "read", "i", n);
if(!data) {
return 0;
}
n = PyBytes_Size(data);
if(n) {
memcpy(c->data + c->position + c->remaining, PyBytes_AsString(data), n);
c->remaining += n;
c->bytes_read += n;
}
else {
c->eof = 1;
}
Py_DECREF(data);
}
return 1;
}
示例8: mmap_ass_item
static int
mmap_ass_item(mmap_object *self, Py_ssize_t i, PyObject *v)
{
const char *buf;
CHECK_VALID(-1);
if (i < 0 || i >= self->size) {
PyErr_SetString(PyExc_IndexError, "mmap index out of range");
return -1;
}
if (v == NULL) {
PyErr_SetString(PyExc_TypeError,
"mmap object doesn't support item deletion");
return -1;
}
if (! (PyBytes_Check(v) && PyBytes_Size(v)==1) ) {
PyErr_SetString(PyExc_IndexError,
"mmap assignment must be length-1 bytes()");
return -1;
}
if (!is_writable(self))
return -1;
buf = PyBytes_AsString(v);
self->data[i] = buf[0];
return 0;
}
示例9: 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;
}
示例10: init_locale_info
// Initialize the global decimal character and thousands separator character, used when parsing decimal
// objects.
//
static void init_locale_info()
{
Object module(PyImport_ImportModule("locale"));
if (!module)
{
PyErr_Clear();
return;
}
Object ldict(PyObject_CallMethod(module, "localeconv", 0));
if (!ldict)
{
PyErr_Clear();
return;
}
PyObject* value = PyDict_GetItemString(ldict, "decimal_point");
if (value)
{
if (PyBytes_Check(value) && PyBytes_Size(value) == 1)
chDecimal = (Py_UNICODE)PyBytes_AS_STRING(value)[0];
if (PyUnicode_Check(value) && PyUnicode_GET_SIZE(value) == 1)
chDecimal = PyUnicode_AS_UNICODE(value)[0];
}
}
示例11: 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;
}
示例12: setstate
static void setstate(IM& im, const object & state) {
if(!PyBytes_Check(state.ptr()))
throw std::invalid_argument("Failed to unpickle, unexpected type!");
const int N = im.W()*im.H()*im.C()*sizeof(value_type);
if( PyBytes_Size(state.ptr()) != N )
throw std::invalid_argument("Failed to unpickle, unexpected size!");
memcpy( im.data(), PyBytes_AS_STRING(state.ptr()), N );
}
示例13: np_char
static int
np_char(char *p, PyObject *v, const formatdef *f)
{
if (!PyBytes_Check(v) || PyBytes_Size(v) != 1) {
PyErr_SetString(StructError,
"char format requires a bytes object of length 1");
return -1;
}
*p = *PyBytes_AsString(v);
return 0;
}
示例14: PyUnicode_FromString
static PyObject *_internal_stream_load(PyObject *args, unsigned int blocking)
{
PyObject *decoder = NULL;
PyObject *stream = NULL;
PyObject *buffer = NULL;
PyObject *result = NULL;
#ifdef IS_PYTHON3
PyObject *bufferstring = NULL;
#endif
if (!PyArg_ParseTuple(args, "O", &stream)) {
goto bad_type;
}
if (__read == NULL) {
__read = PyUnicode_FromString("read");
}
if (!PyObject_HasAttr(stream, __read)) {
goto bad_type;
}
buffer = PyObject_CallMethodObjArgs(stream, __read, NULL);
if (!buffer)
return NULL;
#ifdef IS_PYTHON3
bufferstring = PyUnicode_AsUTF8String(buffer);
if (!bufferstring)
return NULL;
#endif
decoder = PyObject_Call((PyObject *)(&YajlDecoderType), NULL, NULL);
if (decoder == NULL) {
return NULL;
}
#ifdef IS_PYTHON3
result = _internal_decode((_YajlDecoder *)decoder, PyBytes_AsString(bufferstring),
PyBytes_Size(bufferstring));
Py_XDECREF(bufferstring);
#else
result = _internal_decode((_YajlDecoder *)decoder, PyString_AsString(buffer),
PyString_Size(buffer));
#endif
Py_XDECREF(decoder);
Py_XDECREF(buffer);
return result;
bad_type:
PyErr_SetObject(PyExc_TypeError, PyUnicode_FromString("Must pass a single stream object"));
return NULL;
}
示例15: vm_add_memory_page
PyObject* vm_add_memory_page(VmMngr* self, PyObject* args)
{
PyObject *addr;
PyObject *access;
PyObject *item_str;
PyObject *name=NULL;
uint64_t buf_size;
size_t buf_size_st;
char* buf_data;
Py_ssize_t length;
uint64_t page_addr;
uint64_t page_access;
const char *name_ptr;
struct memory_page_node * mpn;
if (!PyArg_ParseTuple(args, "OOO|O", &addr, &access, &item_str, &name))
RAISE(PyExc_TypeError,"Cannot parse arguments");
PyGetInt_uint64_t(addr, page_addr);
PyGetInt_uint64_t(access, page_access);
if(!PyBytes_Check(item_str))
RAISE(PyExc_TypeError,"arg must be bytes");
buf_size = PyBytes_Size(item_str);
PyBytes_AsStringAndSize(item_str, &buf_data, &length);
if (name == NULL) {
name_ptr = (char*)"";
} else {
PyGetStr(name_ptr, name);
}
mpn = create_memory_page_node(page_addr, (unsigned int)buf_size, (unsigned int)page_access, name_ptr);
if (mpn == NULL)
RAISE(PyExc_TypeError,"cannot create page");
if (is_mpn_in_tab(&self->vm_mngr, mpn)) {
free(mpn->ad_hp);
free(mpn);
RAISE(PyExc_TypeError,"known page in memory");
}
if (buf_size > SIZE_MAX) {
fprintf(stderr, "Size too big\n");
exit(EXIT_FAILURE);
}
buf_size_st = (size_t) buf_size;
memcpy(mpn->ad_hp, buf_data, buf_size_st);
add_memory_page(&self->vm_mngr, mpn);
Py_INCREF(Py_None);
return Py_None;
}