本文整理汇总了C++中PyUnicode_AS_UNICODE函数的典型用法代码示例。如果您正苦于以下问题:C++ PyUnicode_AS_UNICODE函数的具体用法?C++ PyUnicode_AS_UNICODE怎么用?C++ PyUnicode_AS_UNICODE使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了PyUnicode_AS_UNICODE函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: u_set
/* u - a single wchar_t character */
static PyObject *
u_set(void *ptr, PyObject *value, Py_ssize_t size)
{
Py_ssize_t len;
if (PyBytes_Check(value)) {
value = PyUnicode_FromEncodedObject(value,
_ctypes_conversion_encoding,
_ctypes_conversion_errors);
if (!value)
return NULL;
} else if (!PyUnicode_Check(value)) {
PyErr_Format(PyExc_TypeError,
"unicode string expected instead of %s instance",
value->ob_type->tp_name);
return NULL;
} else
Py_INCREF(value);
len = PyUnicode_GET_SIZE(value);
if (len != 1) {
Py_DECREF(value);
PyErr_SetString(PyExc_TypeError,
"one character unicode string expected");
return NULL;
}
*(wchar_t *)ptr = PyUnicode_AS_UNICODE(value)[0];
Py_DECREF(value);
_RET(value);
}
示例2: U_get
/* U - a unicode string */
static PyObject *
U_get(void *ptr, Py_ssize_t size)
{
PyObject *result;
Py_ssize_t len;
Py_UNICODE *p;
size /= sizeof(wchar_t); /* we count character units here, not bytes */
result = PyUnicode_FromWideChar((wchar_t *)ptr, size);
if (!result)
return NULL;
/* We need 'result' to be able to count the characters with wcslen,
since ptr may not be NUL terminated. If the length is smaller (if
it was actually NUL terminated, we construct a new one and throw
away the result.
*/
/* chop off at the first NUL character, if any. */
p = PyUnicode_AS_UNICODE(result);
for (len = 0; len < size; ++len)
if (!p[len])
break;
if (len < size) {
PyObject *ob = PyUnicode_FromWideChar((wchar_t *)ptr, len);
Py_DECREF(result);
return ob;
}
return result;
}
示例3: Py_INCREF
EXPORT PyObject *PyUnicode_FromUnicodeString(const UnicodeString *string)
{
if (!string)
{
Py_INCREF(Py_None);
return Py_None;
}
else if (sizeof(Py_UNICODE) == sizeof(UChar))
return PyUnicode_FromUnicode((const Py_UNICODE *) string->getBuffer(),
(int) string->length());
else
{
int len = string->length();
PyObject *u = PyUnicode_FromUnicode(NULL, len);
if (u)
{
Py_UNICODE *pchars = PyUnicode_AS_UNICODE(u);
const UChar *chars = string->getBuffer();
for (int i = 0; i < len; i++)
pchars[i] = chars[i];
}
return u;
}
}
示例4: 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;
}
示例5: PyObject_Unicode
PyObject *
PyObject_Unicode(PyObject *v)
{
PyObject *res;
PyObject *func;
PyObject *str;
static PyObject *unicodestr;
if (v == NULL) {
res = PyString_FromString("<NULL>");
if (res == NULL)
return NULL;
str = PyUnicode_FromEncodedObject(res, NULL, "strict");
Py_DECREF(res);
return str;
} else if (PyUnicode_CheckExact(v)) {
Py_INCREF(v);
return v;
}
/* XXX As soon as we have a tp_unicode slot, we should
check this before trying the __unicode__
method. */
if (unicodestr == NULL) {
unicodestr= PyString_InternFromString("__unicode__");
if (unicodestr == NULL)
return NULL;
}
func = PyObject_GetAttr(v, unicodestr);
if (func != NULL) {
res = PyEval_CallObject(func, (PyObject *)NULL);
Py_DECREF(func);
}
else {
PyErr_Clear();
if (PyUnicode_Check(v)) {
/* For a Unicode subtype that's didn't overwrite __unicode__,
return a true Unicode object with the same data. */
return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(v),
PyUnicode_GET_SIZE(v));
}
if (PyString_CheckExact(v)) {
Py_INCREF(v);
res = v;
}
else {
if (v->ob_type->tp_str != NULL)
res = (*v->ob_type->tp_str)(v);
else
res = PyObject_Repr(v);
}
}
if (res == NULL)
return NULL;
if (!PyUnicode_Check(res)) {
str = PyUnicode_FromEncodedObject(res, NULL, "strict");
Py_DECREF(res);
res = str;
}
return res;
}
示例6: TRACE_IN
JCharString JPyString::asJCharString(PyObject* obj)
{
PyObject* torelease = NULL;
TRACE_IN("JPyString::asJCharString");
if (PyString_Check(obj))
{
PY_CHECK( obj = PyUnicode_FromObject(obj) );
torelease = obj;
}
Py_UNICODE* val = PyUnicode_AS_UNICODE(obj);
Py_ssize_t length = JPyObject::length(obj);
JCharString res(length);
for (int i = 0; val[i] != 0; i++)
{
res[i] = (jchar)val[i];
}
if (torelease != NULL)
{
Py_DECREF(torelease);
}
return res;
TRACE_OUT;
}
示例7: BlockLocator_new
BlockLocator *
BlockLocator_new(PyUnicodeObject* codestr)
{
BlockLocator *self;
#ifdef DEBUG
fprintf(stderr, "%s\n", __PRETTY_FUNCTION__);
#endif
self = PyMem_New(BlockLocator, 1);
if (self) {
memset(self, 0, sizeof(BlockLocator));
Py_INCREF(codestr);
self->py_codestr = codestr;
self->codestr = PyUnicode_AS_UNICODE(codestr);
self->codestr_sz = PyUnicode_GetSize((PyObject*)codestr);
self->codestr_ptr = self->codestr;
self->lineno = 1;
self->par = 0;
self->instr = 0;
self->depth = 0;
self->skip = 0;
self->init = self->codestr;
self->lose = self->codestr;
self->start = NULL;
self->end = NULL;
#ifdef DEBUG
fprintf(stderr, "\tScss BlockLocator object created (%ld units)!\n", self->codestr_sz);
#endif
}
return self;
}
示例8: _mktime_u
static PyObject*
_mktime_u(PyObject* self, PyObject* arg) {
Py_UNICODE *src = PyUnicode_AS_UNICODE(arg), *s = src, digitBuffer[32], *d = digitBuffer, *dEnd = digitBuffer + 32, *bPtr;
size_t tbuf[] = {0, 0, 0, 0, 0, 0}, timestamp = 0;
PROCESS_MKTIME;
}
示例9: __init__
static int __init__(PyObject *self, PyObject *args, PyObject *kwds)
{
ligolw_Tokenizer *tokenizer = (ligolw_Tokenizer *) self;
PyObject *arg;
if(!PyArg_ParseTuple(args, "U", &arg))
return -1;
if(PyUnicode_GET_SIZE(arg) != 1) {
PyErr_SetString(PyExc_ValueError, "len(delimiter) != 1");
return -1;
}
tokenizer->delimiter = *PyUnicode_AS_UNICODE(arg);
tokenizer->quote_characters = default_quote_characters;
tokenizer->escape_character = '\\';
tokenizer->types = malloc(1 * sizeof(*tokenizer->types));
tokenizer->types_length = &tokenizer->types[1];
tokenizer->types[0] = (PyObject *) &PyUnicode_Type;
Py_INCREF(tokenizer->types[0]);
tokenizer->type = tokenizer->types;
tokenizer->allocation = 0;
tokenizer->data = NULL;
tokenizer->length = tokenizer->data;
tokenizer->pos = tokenizer->data;
return 0;
}
示例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: PyCodec_ReplaceErrors
PyObject* PyCodec_ReplaceErrors(PyObject* exc) noexcept {
PyObject* restuple;
Py_ssize_t start;
Py_ssize_t end;
Py_ssize_t i;
if (PyObject_IsInstance(exc, PyExc_UnicodeEncodeError)) {
PyObject* res;
Py_UNICODE* p;
if (PyUnicodeEncodeError_GetStart(exc, &start))
return NULL;
if (PyUnicodeEncodeError_GetEnd(exc, &end))
return NULL;
res = PyUnicode_FromUnicode(NULL, end - start);
if (res == NULL)
return NULL;
for (p = PyUnicode_AS_UNICODE(res), i = start; i < end; ++p, ++i)
*p = '?';
restuple = Py_BuildValue("(On)", res, end);
Py_DECREF(res);
return restuple;
} else if (PyObject_IsInstance(exc, PyExc_UnicodeDecodeError)) {
Py_UNICODE res = Py_UNICODE_REPLACEMENT_CHARACTER;
if (PyUnicodeDecodeError_GetEnd(exc, &end))
return NULL;
return Py_BuildValue("(u#n)", &res, (Py_ssize_t)1, end);
} else if (PyObject_IsInstance(exc, PyExc_UnicodeTranslateError)) {
PyObject* res;
Py_UNICODE* p;
if (PyUnicodeTranslateError_GetStart(exc, &start))
return NULL;
if (PyUnicodeTranslateError_GetEnd(exc, &end))
return NULL;
res = PyUnicode_FromUnicode(NULL, end - start);
if (res == NULL)
return NULL;
for (p = PyUnicode_AS_UNICODE(res), i = start; i < end; ++p, ++i)
*p = Py_UNICODE_REPLACEMENT_CHARACTER;
restuple = Py_BuildValue("(On)", res, end);
Py_DECREF(res);
return restuple;
} else {
wrong_exception_type(exc);
return NULL;
}
}
示例12: Connect
static bool Connect(PyObject* pConnectString, HDBC hdbc, bool fAnsi)
{
// This should have been checked by the global connect function.
I(PyString_Check(pConnectString) || PyUnicode_Check(pConnectString));
const int cchMax = 600;
if (PySequence_Length(pConnectString) >= cchMax)
{
PyErr_SetString(PyExc_TypeError, "connection string too long");
return false;
}
// The driver manager determines if the app is a Unicode app based on whether we call SQLDriverConnectA or
// SQLDriverConnectW. Some drivers, notably Microsoft Access/Jet, change their behavior based on this, so we try
// the Unicode version first. (The Access driver only supports Unicode text, but SQLDescribeCol returns SQL_CHAR
// instead of SQL_WCHAR if we connect with the ANSI version. Obviously this causes lots of errors since we believe
// what it tells us (SQL_CHAR).)
// Python supports only UCS-2 and UCS-4, so we shouldn't need to worry about receiving surrogate pairs. However,
// Windows does use UCS-16, so it is possible something would be misinterpreted as one. We may need to examine
// this more.
SQLRETURN ret;
if (!fAnsi)
{
SQLWCHAR szConnectW[cchMax];
if (PyUnicode_Check(pConnectString))
{
Py_UNICODE* p = PyUnicode_AS_UNICODE(pConnectString);
for (int i = 0, c = PyUnicode_GET_SIZE(pConnectString); i <= c; i++)
szConnectW[i] = (wchar_t)p[i];
}
else
{
const char* p = PyString_AS_STRING(pConnectString);
for (int i = 0, c = PyString_GET_SIZE(pConnectString); i <= c; i++)
szConnectW[i] = (wchar_t)p[i];
}
Py_BEGIN_ALLOW_THREADS
ret = SQLDriverConnectW(hdbc, 0, szConnectW, SQL_NTS, 0, 0, 0, SQL_DRIVER_NOPROMPT);
Py_END_ALLOW_THREADS
if (SQL_SUCCEEDED(ret))
return true;
// The Unicode function failed. If the error is that the driver doesn't have a Unicode version (IM001), continue
// to the ANSI version.
PyObject* error = GetErrorFromHandle("SQLDriverConnectW", hdbc, SQL_NULL_HANDLE);
if (!HasSqlState(error, "IM001"))
{
RaiseErrorFromException(error);
return false;
}
Py_XDECREF(error);
}
示例13: __Numba_PyObject_AsPy_UCS4
static NUMBA_INLINE Py_UCS4 __Numba_PyObject_AsPy_UCS4(PyObject* x) {
long ival;
if (PyUnicode_Check(x)) {
Py_ssize_t length;
#if CYTHON_PEP393_ENABLED
length = PyUnicode_GET_LENGTH(x);
if (likely(length == 1)) {
return PyUnicode_READ_CHAR(x, 0);
}
#else
length = PyUnicode_GET_SIZE(x);
if (likely(length == 1)) {
return PyUnicode_AS_UNICODE(x)[0];
}
#if Py_UNICODE_SIZE == 2
else if (PyUnicode_GET_SIZE(x) == 2) {
Py_UCS4 high_val = PyUnicode_AS_UNICODE(x)[0];
if (high_val >= 0xD800 && high_val <= 0xDBFF) {
Py_UCS4 low_val = PyUnicode_AS_UNICODE(x)[1];
if (low_val >= 0xDC00 && low_val <= 0xDFFF) {
return 0x10000 + (((high_val & ((1<<10)-1)) << 10) | (low_val & ((1<<10)-1)));
}
}
}
#endif
#endif
PyErr_Format(PyExc_ValueError,
"only single character unicode strings can be converted to Py_UCS4, "
"got length %" CYTHON_FORMAT_SSIZE_T "d", length);
return (Py_UCS4)-1;
}
ival = __Numba_PyInt_AsLong(x);
if (unlikely(ival < 0)) {
if (!PyErr_Occurred())
PyErr_SetString(PyExc_OverflowError,
"cannot convert negative value to Py_UCS4");
return (Py_UCS4)-1;
} else if (unlikely(ival > 1114111)) {
PyErr_SetString(PyExc_OverflowError,
"value too large to convert to Py_UCS4");
return (Py_UCS4)-1;
}
return (Py_UCS4)ival;
}
示例14: MakeConnectionString
static PyObject* MakeConnectionString(PyObject* existing, PyObject* parts)
{
// Creates a connection string from an optional existing connection string plus a dictionary of keyword value
// pairs.
//
// existing
// Optional Unicode connection string we will be appending to. Used when a partial connection string is passed
// in, followed by keyword parameters:
//
// connect("driver={x};database={y}", user='z')
//
// parts
// A dictionary of text keywords and text values that will be appended.
I(PyUnicode_Check(existing));
Py_ssize_t length = 0; // length in *characters*
if (existing)
length = Text_Size(existing) + 1; // + 1 to add a trailing semicolon
Py_ssize_t pos = 0;
PyObject* key = 0;
PyObject* value = 0;
while (PyDict_Next(parts, &pos, &key, &value))
{
length += Text_Size(key) + 1 + Text_Size(value) + 1; // key=value;
}
PyObject* result = PyUnicode_FromUnicode(0, length);
if (!result)
return 0;
Py_UNICODE* buffer = PyUnicode_AS_UNICODE(result);
Py_ssize_t offset = 0;
if (existing)
{
offset += TextCopyToUnicode(&buffer[offset], existing);
buffer[offset++] = (Py_UNICODE)';';
}
pos = 0;
while (PyDict_Next(parts, &pos, &key, &value))
{
offset += TextCopyToUnicode(&buffer[offset], key);
buffer[offset++] = (Py_UNICODE)'=';
offset += TextCopyToUnicode(&buffer[offset], value);
buffer[offset++] = (Py_UNICODE)';';
}
I(offset == length);
return result;
}
示例15: _intNew
static Box* _intNew(Box* val, Box* base) {
if (isSubclass(val->cls, int_cls)) {
RELEASE_ASSERT(!base, "");
BoxedInt* n = static_cast<BoxedInt*>(val);
if (val->cls == int_cls)
return n;
return new BoxedInt(n->n);
} else if (isSubclass(val->cls, str_cls)) {
int base_n;
if (!base)
base_n = 10;
else {
RELEASE_ASSERT(base->cls == int_cls, "");
base_n = static_cast<BoxedInt*>(base)->n;
}
BoxedString* s = static_cast<BoxedString*>(val);
RELEASE_ASSERT(s->size() == strlen(s->data()), "");
Box* r = PyInt_FromString(s->data(), NULL, base_n);
if (!r)
throwCAPIException();
return r;
} else if (isSubclass(val->cls, unicode_cls)) {
int base_n;
if (!base)
base_n = 10;
else {
RELEASE_ASSERT(base->cls == int_cls, "");
base_n = static_cast<BoxedInt*>(base)->n;
}
Box* r = PyInt_FromUnicode(PyUnicode_AS_UNICODE(val), PyUnicode_GET_SIZE(val), base_n);
if (!r)
throwCAPIException();
return r;
} else if (val->cls == float_cls) {
RELEASE_ASSERT(!base, "");
double d = static_cast<BoxedFloat*>(val)->d;
return new BoxedInt(d);
} else {
RELEASE_ASSERT(!base, "");
static const std::string int_str("__int__");
Box* r = callattr(val, &int_str, CallattrFlags({.cls_only = true, .null_on_nonexistent = true }),
ArgPassSpec(0), NULL, NULL, NULL, NULL, NULL);
if (!r) {
fprintf(stderr, "TypeError: int() argument must be a string or a number, not '%s'\n", getTypeName(val));
raiseExcHelper(TypeError, "");
}
if (!isSubclass(r->cls, int_cls) && !isSubclass(r->cls, long_cls)) {
raiseExcHelper(TypeError, "__int__ returned non-int (type %s)", r->cls->tp_name);
}
return r;
}