本文整理汇总了C++中PyBytes_AS_STRING函数的典型用法代码示例。如果您正苦于以下问题:C++ PyBytes_AS_STRING函数的具体用法?C++ PyBytes_AS_STRING怎么用?C++ PyBytes_AS_STRING使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了PyBytes_AS_STRING函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: escape_encode
static PyObject *
escape_encode(PyObject *self,
PyObject *args)
{
PyObject *str;
Py_ssize_t size;
Py_ssize_t newsize;
const char *errors = NULL;
PyObject *v;
if (!PyArg_ParseTuple(args, "O!|z:escape_encode",
&PyBytes_Type, &str, &errors))
return NULL;
size = PyBytes_GET_SIZE(str);
if (size > PY_SSIZE_T_MAX / 4) {
PyErr_SetString(PyExc_OverflowError,
"string is too large to encode");
return NULL;
}
newsize = 4*size;
v = PyBytes_FromStringAndSize(NULL, newsize);
if (v == NULL) {
return NULL;
}
else {
Py_ssize_t i;
char c;
char *p = PyBytes_AS_STRING(v);
for (i = 0; i < size; i++) {
/* There's at least enough room for a hex escape */
assert(newsize - (p - PyBytes_AS_STRING(v)) >= 4);
c = PyBytes_AS_STRING(str)[i];
if (c == '\'' || c == '\\')
*p++ = '\\', *p++ = c;
else if (c == '\t')
*p++ = '\\', *p++ = 't';
else if (c == '\n')
*p++ = '\\', *p++ = 'n';
else if (c == '\r')
*p++ = '\\', *p++ = 'r';
else if (c < ' ' || c >= 0x7f) {
*p++ = '\\';
*p++ = 'x';
*p++ = Py_hexdigits[(c & 0xf0) >> 4];
*p++ = Py_hexdigits[c & 0xf];
}
else
*p++ = c;
}
示例2: while
void get_arg::get_arg_base::finished() {
// are there more keywords than we used?
if(UNLIKELY(kwds && kcount < PyDict_Size(kwds))) {
PyObject *key;
Py_ssize_t pos = 0;
while(PyDict_Next(kwds,&pos,&key,NULL)){
if(!PYSTR(Check)(key)) {
PyErr_SetString(PyExc_TypeError,"keywords must be strings");
throw py_error_set();
}
#if PY_MAJOR_VERSION >= 3
#if PY_MINOR_VERSION >= 3
const char *kstr = PyUnicode_AsUTF8(key);
if(!kstr) throw py_error_set();
#else
PyObject *kstr_obj = PyUnicode_AsUTF8String(key);
if(!kstr_obj) throw py_error_set();
struct deleter {
PyObject *ptr;
deleter(PyObject *ptr) : ptr(ptr) {}
~deleter() { Py_DECREF(ptr); }
} _(kstr_obj);
const char *kstr = PyBytes_AS_STRING(kstr_obj);
#endif
#else
const char *kstr = PyBytes_AS_STRING(key);
#endif
if(names) {
for(const char **name = names; *name; ++name) {
if(strcmp(kstr,*name) == 0) goto match;
}
}
PyErr_Format(PyExc_TypeError,"'%s' is an invalid keyword argument for %s%s",
kstr,
fname ? fname : "this function",
fname ? "()" : "");
throw py_error_set();
match:
;
}
// should never reach here
assert(false);
}
}
示例3: unshare_buffer
/* Internal routine for detaching the shared buffer of BytesIO objects.
The caller should ensure that the 'size' argument is non-negative and
not lesser than self->string_size. Returns 0 on success, -1 otherwise. */
static int
unshare_buffer(bytesio *self, size_t size)
{
PyObject *new_buf;
assert(SHARED_BUF(self));
assert(self->exports == 0);
assert(size >= (size_t)self->string_size);
new_buf = PyBytes_FromStringAndSize(NULL, size);
if (new_buf == NULL)
return -1;
memcpy(PyBytes_AS_STRING(new_buf), PyBytes_AS_STRING(self->buf),
self->string_size);
Py_SETREF(self->buf, new_buf);
return 0;
}
示例4: 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;
}
示例5: PyBlosc_decompress
static PyObject *
PyBlosc_decompress(PyObject *self, PyObject *args)
{
PyObject *result_str;
void *input, *output;
size_t nbytes, cbytes;
if (!PyArg_ParseTuple(args, "s#:decompress", &input, &cbytes))
return NULL;
/* fetch the uncompressed size into nbytes */
if (!get_nbytes(input, cbytes, &nbytes))
return NULL;
/* Book memory for the result */
if (!(result_str = PyBytes_FromStringAndSize(NULL, (Py_ssize_t)nbytes)))
return NULL;
output = PyBytes_AS_STRING(result_str);
/* do decompression */
if (!decompress_helper(input, nbytes, output)){
Py_XDECREF(result_str);
return NULL;
}
return result_str;
}
示例6: Billiard_recv
static PyObject *
Billiard_recv(PyObject *self, PyObject *args)
{
HANDLE handle;
int size, nread;
PyObject *buf;
if (!PyArg_ParseTuple(args, F_HANDLE "i:recv" , &handle, &size))
return NULL;
buf = PyBytes_FromStringAndSize(NULL, size);
if (!buf)
return NULL;
Py_BEGIN_ALLOW_THREADS
nread = recv((SOCKET) handle, PyBytes_AS_STRING(buf), size, 0);
Py_END_ALLOW_THREADS
if (nread < 0) {
Py_DECREF(buf);
return PyErr_SetExcFromWindowsErr(PyExc_IOError, WSAGetLastError());
}
_PyBytes_Resize(&buf, nread);
return buf;
}
示例7: py_replace_history
remove history item given by its position");
static PyObject *
py_replace_history(PyObject *self, PyObject *args)
{
int entry_number;
PyObject *line;
PyObject *encoded;
HIST_ENTRY *old_entry;
if (!PyArg_ParseTuple(args, "iU:replace_history_item", &entry_number,
&line)) {
return NULL;
}
if (entry_number < 0) {
PyErr_SetString(PyExc_ValueError,
"History index cannot be negative");
return NULL;
}
encoded = encode(line);
if (encoded == NULL) {
return NULL;
}
old_entry = replace_history_entry(entry_number, PyBytes_AS_STRING(encoded), (void *)NULL);
Py_DECREF(encoded);
if (!old_entry) {
PyErr_Format(PyExc_ValueError,
"No history item at position %d",
entry_number);
return NULL;
}
/* free memory allocated for the old history entry */
_py_free_history_entry(old_entry);
Py_RETURN_NONE;
}
示例8: MGLTextureArray_read
PyObject * MGLTextureArray_read(MGLTextureArray * self, PyObject * args) {
int alignment;
int args_ok = PyArg_ParseTuple(
args,
"I",
&alignment
);
if (!args_ok) {
return 0;
}
if (alignment != 1 && alignment != 2 && alignment != 4 && alignment != 8) {
MGLError_Set("the alignment must be 1, 2, 4 or 8");
return 0;
}
int expected_size = self->width * self->components * self->data_type->size;
expected_size = (expected_size + alignment - 1) / alignment * alignment;
expected_size = expected_size * self->height * self->layers;
PyObject * result = PyBytes_FromStringAndSize(0, expected_size);
char * data = PyBytes_AS_STRING(result);
int pixel_type = self->data_type->gl_type;
int base_format = self->data_type->base_format[self->components];
const GLMethods & gl = self->context->gl;
gl.ActiveTexture(GL_TEXTURE0 + self->context->default_texture_unit);
gl.BindTexture(GL_TEXTURE_2D_ARRAY, self->texture_obj);
gl.PixelStorei(GL_PACK_ALIGNMENT, alignment);
gl.PixelStorei(GL_UNPACK_ALIGNMENT, alignment);
// To determine the required size of pixels, use glGetTexLevelParameter to determine
// the dimensions of the internal texture image, then scale the required number of pixels
// by the storage required for each pixel, based on format and type. Be sure to take the
// pixel storage parameters into account, especially GL_PACK_ALIGNMENT.
// int pack = 0;
// gl.GetIntegerv(GL_PACK_ALIGNMENT, &pack);
// printf("GL_PACK_ALIGNMENT: %d\n", pack);
// glGetTexLevelParameter with argument GL_TEXTURE_WIDTH
// glGetTexLevelParameter with argument GL_TEXTURE_HEIGHT
// glGetTexLevelParameter with argument GL_TEXTURE_INTERNAL_FORMAT
// int level_width = 0;
// int level_height = 0;
// gl.GetTexLevelParameteriv(GL_TEXTURE_2D_ARRAY, 0, GL_TEXTURE_WIDTH, &level_width);
// gl.GetTexLevelParameteriv(GL_TEXTURE_2D_ARRAY, 0, GL_TEXTURE_HEIGHT, &level_height);
// printf("level_width: %d\n", level_width);
// printf("level_height: %d\n", level_height);
gl.GetTexImage(GL_TEXTURE_2D_ARRAY, 0, base_format, pixel_type, data);
return result;
}
示例9: __Pyx__PyObject_Ord
static long __Pyx__PyObject_Ord(PyObject* c) {
Py_ssize_t size;
if (PyBytes_Check(c)) {
size = PyBytes_GET_SIZE(c);
if (likely(size == 1)) {
return (unsigned char) PyBytes_AS_STRING(c)[0];
}
#if PY_MAJOR_VERSION < 3
} else if (PyUnicode_Check(c)) {
return (long)__Pyx_PyUnicode_AsPy_UCS4(c);
#endif
#if (!CYTHON_COMPILING_IN_PYPY) || (defined(PyByteArray_AS_STRING) && defined(PyByteArray_GET_SIZE))
} else if (PyByteArray_Check(c)) {
size = PyByteArray_GET_SIZE(c);
if (likely(size == 1)) {
return (unsigned char) PyByteArray_AS_STRING(c)[0];
}
#endif
} else {
// FIXME: support character buffers - but CPython doesn't support them either
PyErr_Format(PyExc_TypeError,
"ord() expected string of length 1, but %.200s found", c->ob_type->tp_name);
return (long)(Py_UCS4)-1;
}
PyErr_Format(PyExc_TypeError,
"ord() expected a character, but string of length %zd found", size);
return (long)(Py_UCS4)-1;
}
示例10: on_completion
static char *
on_completion(const char *text, int state)
{
char *result = NULL;
if (readlinestate_global->completer != NULL) {
PyObject *r = NULL, *t;
PyGILState_STATE gilstate = PyGILState_Ensure();
rl_attempted_completion_over = 1;
t = decode(text);
r = PyObject_CallFunction(readlinestate_global->completer, "Ni", t, state);
if (r == NULL)
goto error;
if (r == Py_None) {
result = NULL;
}
else {
PyObject *encoded = encode(r);
if (encoded == NULL)
goto error;
result = strdup(PyBytes_AS_STRING(encoded));
Py_DECREF(encoded);
}
Py_DECREF(r);
goto done;
error:
PyErr_Clear();
Py_XDECREF(r);
done:
PyGILState_Release(gilstate);
return result;
}
return result;
}
示例11: _lzma__encode_filter_properties_impl
static PyObject *
_lzma__encode_filter_properties_impl(PyModuleDef *module, lzma_filter filter)
/*[clinic end generated code: output=b5fe690acd6b61d1 input=d4c64f1b557c77d4]*/
{
lzma_ret lzret;
uint32_t encoded_size;
PyObject *result = NULL;
lzret = lzma_properties_size(&encoded_size, &filter);
if (catch_lzma_error(lzret))
goto error;
result = PyBytes_FromStringAndSize(NULL, encoded_size);
if (result == NULL)
goto error;
lzret = lzma_properties_encode(
&filter, (uint8_t *)PyBytes_AS_STRING(result));
if (catch_lzma_error(lzret))
goto error;
return result;
error:
Py_XDECREF(result);
return NULL;
}
示例12: 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;
}
示例13: compress_helper
static PyObject *
compress_helper(void * input, size_t nbytes, size_t typesize,
int clevel, int shuffle, char *cname){
int cbytes;
PyObject *output = NULL;
/* Alloc memory for compression */
if (!(output = PyBytes_FromStringAndSize(NULL, nbytes+BLOSC_MAX_OVERHEAD)))
return NULL;
/* Select compressor */
if (blosc_set_compressor(cname) < 0) {
/* The compressor is not available (should never happen here) */
blosc_error(-1, "compressor not available");
return NULL;
}
/* Compress */
cbytes = blosc_compress(clevel, shuffle, typesize, nbytes,
input, PyBytes_AS_STRING(output),
nbytes+BLOSC_MAX_OVERHEAD);
if (cbytes < 0) {
blosc_error(cbytes, "while compressing data");
Py_XDECREF(output);
return NULL;
}
/* Attempt to resize, if it's much smaller, a copy is required. */
if (_PyBytes_Resize(&output, cbytes) < 0){
/* the memory exception will have been set, hopefully */
return NULL;
}
return output;
}
示例14: automaton_search_iter_new
static PyObject*
automaton_search_iter_new(
Automaton* automaton,
PyObject* object,
int start,
int end
) {
AutomatonSearchIter* iter;
iter = (AutomatonSearchIter*)PyObject_New(AutomatonSearchIter, &automaton_search_iter_type);
if (iter == NULL)
return NULL;
iter->automaton = automaton;
iter->version = automaton->version;
iter->object = object;
#ifdef AHOCORASICK_UNICODE
iter->data = PyUnicode_AS_UNICODE(object);
#else
iter->data = (uint8_t*)PyBytes_AS_STRING(object);
#endif
iter->state = automaton->root;
iter->output= NULL;
iter->shift = 0;
iter->index = start - 1; // -1 because first instruction in next() increments index
iter->end = end;
Py_INCREF(iter->automaton);
Py_INCREF(iter->object);
return (PyObject*)iter;
}
示例15: _Py_fopen
FILE*
_Py_fopen(PyObject *path, const char *mode)
{
#ifdef MS_WINDOWS
wchar_t *wpath;
wchar_t wmode[10];
int usize;
if (!PyUnicode_Check(path)) {
PyErr_Format(PyExc_TypeError,
"str file path expected under Windows, got %R",
Py_TYPE(path));
return NULL;
}
wpath = PyUnicode_AsUnicode(path);
if (wpath == NULL)
return NULL;
usize = MultiByteToWideChar(CP_ACP, 0, mode, -1, wmode, sizeof(wmode));
if (usize == 0)
return NULL;
return _wfopen(wpath, wmode);
#else
FILE *f;
PyObject *bytes;
if (!PyUnicode_FSConverter(path, &bytes))
return NULL;
f = fopen(PyBytes_AS_STRING(bytes), mode);
Py_DECREF(bytes);
return f;
#endif
}