本文整理汇总了C++中PyUnicode_GET_SIZE函数的典型用法代码示例。如果您正苦于以下问题:C++ PyUnicode_GET_SIZE函数的具体用法?C++ PyUnicode_GET_SIZE怎么用?C++ PyUnicode_GET_SIZE使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了PyUnicode_GET_SIZE函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: 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;
}
示例2: 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];
}
}
示例3: 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);
}
示例4: __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;
}
示例5: _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;
}
示例6: complex_subtype_from_string
static PyObject *
complex_subtype_from_string(PyTypeObject *type, PyObject *v)
{
const char *s;
PyObject *result = NULL;
#ifdef Py_USING_UNICODE
char *s_buffer = NULL;
#endif
Py_ssize_t len;
if (PyString_Check(v)) {
s = PyString_AS_STRING(v);
len = PyString_GET_SIZE(v);
}
#ifdef Py_USING_UNICODE
else if (PyUnicode_Check(v)) {
s_buffer = (char *)PyMem_MALLOC(PyUnicode_GET_SIZE(v)+1);
if (s_buffer == NULL)
return PyErr_NoMemory();
if (PyUnicode_EncodeDecimal(PyUnicode_AS_UNICODE(v),
PyUnicode_GET_SIZE(v),
s_buffer,
NULL))
goto exit;
s = s_buffer;
len = strlen(s);
}
#endif
else {
PyErr_SetString(PyExc_TypeError,
"complex() arg is not a string");
return NULL;
}
result = _Py_string_to_number_with_underscores(s, len, "complex", v, type,
complex_from_string_inner);
exit:
#ifdef Py_USING_UNICODE
if (s_buffer)
PyMem_FREE(s_buffer);
#endif
return result;
}
示例7: PyUnicode_EncodeUTF8
static void *PyUnicodeToUTF8(JSOBJ _obj, JSONTypeContext *tc, void *outValue, size_t *_outLen)
{
PyObject *obj = (PyObject *) _obj;
PyObject *newObj = PyUnicode_EncodeUTF8 (PyUnicode_AS_UNICODE(obj), PyUnicode_GET_SIZE(obj), NULL);
GET_TC(tc)->newObj = newObj;
*_outLen = PyString_GET_SIZE(newObj);
return PyString_AS_STRING(newObj);
}
示例8: AsObj
static Tcl_Obj*
AsObj(PyObject *value)
{
Tcl_Obj *result;
if (PyString_Check(value))
return Tcl_NewStringObj(PyString_AS_STRING(value),
PyString_GET_SIZE(value));
else if (PyInt_Check(value))
return Tcl_NewLongObj(PyInt_AS_LONG(value));
else if (PyFloat_Check(value))
return Tcl_NewDoubleObj(PyFloat_AS_DOUBLE(value));
else if (PyTuple_Check(value)) {
Tcl_Obj **argv = (Tcl_Obj**)
ckalloc(PyTuple_Size(value)*sizeof(Tcl_Obj*));
int i;
if(!argv)
return 0;
for(i=0;i<PyTuple_Size(value);i++)
argv[i] = AsObj(PyTuple_GetItem(value,i));
result = Tcl_NewListObj(PyTuple_Size(value), argv);
ckfree(FREECAST argv);
return result;
}
else if (PyUnicode_Check(value)) {
#if TKMAJORMINOR <= 8001
/* In Tcl 8.1 we must use UTF-8 */
PyObject* utf8 = PyUnicode_AsUTF8String(value);
if (!utf8)
return 0;
result = Tcl_NewStringObj(PyString_AS_STRING(utf8),
PyString_GET_SIZE(utf8));
Py_DECREF(utf8);
return result;
#else /* TKMAJORMINOR > 8001 */
/* In Tcl 8.2 and later, use Tcl_NewUnicodeObj() */
if (sizeof(Py_UNICODE) != sizeof(Tcl_UniChar)) {
/* XXX Should really test this at compile time */
PyErr_SetString(PyExc_SystemError,
"Py_UNICODE and Tcl_UniChar differ in size");
return 0;
}
return Tcl_NewUnicodeObj(PyUnicode_AS_UNICODE(value),
PyUnicode_GET_SIZE(value));
#endif /* TKMAJORMINOR > 8001 */
}
else {
PyObject *v = PyObject_Str(value);
if (!v)
return 0;
result = AsObj(v);
Py_DECREF(v);
return result;
}
}
示例9: PyWinObject_AsWCHAR
// String conversions
// Convert a Python object to a WCHAR - allow embedded NULLs, None, etc.
BOOL PyWinObject_AsWCHAR(PyObject *stringObject, WCHAR **pResult, BOOL bNoneOK /*= FALSE*/,DWORD *pResultLen /*= NULL*/)
{
BOOL rc = TRUE;
int resultLen = 0;
#if (PY_VERSION_HEX < 0x03000000)
// Do NOT accept 'bytes' object when a plain 'WCHAR' is needed on py3k.
if (PyString_Check(stringObject)) {
int size=PyString_Size(stringObject);
const char *buf = PyString_AsString(stringObject);
if (buf==NULL) return FALSE;
/* We assume that we dont need more 'wide characters' for the result
then the number of bytes in the input. Often we
will need less, as the input may contain multi-byte chars, but we
should never need more
*/
*pResult = (LPWSTR)PyMem_Malloc((size+1)*sizeof(WCHAR));
if (*pResult==NULL) {
PyErr_SetString(PyExc_MemoryError, "No memory for wide string buffer");
return FALSE;
}
/* convert and get the final character size */
resultLen = MultiByteToWideChar(CP_ACP, 0, buf, size, *pResult, size);
/* terminate the string */
(*pResult)[resultLen] = L'\0';
}
else
#endif // py3k
if (PyUnicode_Check(stringObject))
{
resultLen = PyUnicode_GET_SIZE(stringObject);
size_t cb = sizeof(WCHAR) * (resultLen+1);
*pResult = (WCHAR *)PyMem_Malloc(cb);
if (*pResult==NULL) {
PyErr_SetString(PyExc_MemoryError, "Allocating WCHAR array");
return FALSE;
}
// copy the value, including embedded NULLs
memcpy(*pResult, PyUnicode_AsUnicode(stringObject), cb);
}
else if (stringObject == Py_None) {
if (bNoneOK) {
*pResult = NULL;
} else {
PyErr_SetString(PyExc_TypeError, "None is not a valid string in this context");
rc = FALSE;
}
} else {
const char *tp_name = stringObject && stringObject->ob_type ? stringObject->ob_type->tp_name : "<NULL!!>";
PyErr_Format(PyExc_TypeError, "Objects of type '%s' can not be converted to Unicode.", tp_name);
rc = FALSE;
}
if (rc && pResultLen) *pResultLen = resultLen;
return rc;
}
示例10: add_to_data
static int add_to_data(ligolw_Tokenizer *tokenizer, PyObject *unicode)
{
Py_ssize_t n = PyUnicode_GET_SIZE(unicode);
if(n) {
if(tokenizer->length - tokenizer->data + n > tokenizer->allocation) {
/*
* convert pointers to integer offsets
*/
ptrdiff_t pos = tokenizer->pos - tokenizer->data;
ptrdiff_t length = tokenizer->length - tokenizer->data;
/*
* increase buffer size, adding 1 to leave room for
* the null terminator
*/
Py_UNICODE *old_data = tokenizer->data;
tokenizer->data = realloc(tokenizer->data, (tokenizer->allocation + n + 1) * sizeof(*tokenizer->data));
if(!tokenizer->data) {
/*
* memory failure, restore pointer and exit
*/
tokenizer->data = old_data;
return -1;
}
tokenizer->allocation += n;
/*
* convert integer offsets back to pointers
*/
tokenizer->pos = &tokenizer->data[pos];
tokenizer->length = &tokenizer->data[length];
}
/*
* copy data from unicode into buffer, appending null
* terminator
*/
memcpy(tokenizer->length, PyUnicode_AsUnicode(unicode), n * sizeof(*tokenizer->length));
tokenizer->length += n;
*tokenizer->length = 0;
}
/*
* success
*/
return 0;
}
示例11: complex__format__
static PyObject *
complex__format__(PyObject* self, PyObject* args)
{
PyObject *format_spec;
if (!PyArg_ParseTuple(args, "U:__format__", &format_spec))
return NULL;
return _PyComplex_FormatAdvanced(self,
PyUnicode_AS_UNICODE(format_spec),
PyUnicode_GET_SIZE(format_spec));
}
示例12: PyUnicode_AS_UNICODE
EXPORT UnicodeString &PyObject_AsUnicodeString(PyObject *object,
const char *encoding,
const char *mode,
UnicodeString &string)
{
if (PyUnicode_Check(object))
{
if (sizeof(Py_UNICODE) == sizeof(UChar))
string.setTo((const UChar *) PyUnicode_AS_UNICODE(object),
(int32_t) PyUnicode_GET_SIZE(object));
else
{
int32_t len = (int32_t) PyUnicode_GET_SIZE(object);
Py_UNICODE *pchars = PyUnicode_AS_UNICODE(object);
UChar *chars = new UChar[len * 3];
UErrorCode status = U_ZERO_ERROR;
int32_t dstLen;
u_strFromUTF32(chars, len * 3, &dstLen,
(const UChar32 *) pchars, len, &status);
if (U_FAILURE(status))
{
delete[] chars;
throw ICUException(status);
}
string.setTo((const UChar *) chars, (int32_t) dstLen);
delete[] chars;
}
}
else if (PyBytes_Check(object))
PyBytes_AsUnicodeString(object, encoding, mode, string);
else
{
PyErr_SetObject(PyExc_TypeError, object);
throw ICUException();
}
return string;
}
示例13: Splitter_split
static PyObject *
Splitter_split(Splitter *self, PyObject *args)
{
PyObject *doc;
char *encoding = "iso-8859-15";
Py_XDECREF(self->list);
self->list = PyList_New(0);
if (! (PyArg_ParseTuple(args,"O|s",&doc, &encoding))) return NULL;
if (PyBytes_Check(doc)) {
if (strlen(encoding) == 0 || !strcmp(encoding,"ascii"))
splitString(self, doc);
else {
PyObject *doc1;
if (! (doc1 = PyUnicode_FromEncodedObject(doc, encoding, "strict"))) {
PyErr_SetString(PyExc_UnicodeError,"unicode conversion failed (maybe wrong encoding parameter)");
return NULL;
}
splitUnicodeString(self, doc1);
Py_XDECREF(doc1);
}
} else if (PyUnicode_Check(doc)) {
PyObject *doc1; // create a *real* copy since we need to modify the string
doc1 = PyUnicode_FromUnicode(NULL, PyUnicode_GET_SIZE(doc));
Py_UNICODE_COPY(PyUnicode_AS_UNICODE(doc1),
PyUnicode_AS_UNICODE(doc),
PyUnicode_GET_SIZE(doc));
splitUnicodeString(self, doc1);
Py_DECREF(doc1);
} else {
PyErr_SetString(PyExc_TypeError, "first argument must be string or unicode");
return NULL;
}
Py_XINCREF(self->list);
return self->list;
}
示例14: len
Py_ssize_t len(PyObject* str)
{
if (str == Py_None)
return 0;
if (PyUnicode_Check(str))
return PyUnicode_GET_SIZE(str);
if (PyBytes_Check(str))
return PyBytes_GET_SIZE(str);
return 0;
}
示例15: unquote_internal_unicode
static PyObject *
unquote_internal_unicode(PyObject *string, int plus)
{
PyObject *result;
Py_UNICODE *su, *ru;
Py_ssize_t j, slen, tlen, sentinel;
if (!PyUnicode_CheckExact(string)) {
if (!(string = PyObject_Unicode(string)))
return NULL;
}
else
Py_INCREF(string);
su = PyUnicode_AS_UNICODE(string);
slen = tlen = PyUnicode_GET_SIZE(string);
for (j=0, sentinel=slen-2; j<sentinel; ++j) {
if ( WTF_IS_LATIN1(su[j]) && (su[j] & 0xFF) == '%'
&& WTF_IS_LATIN1(su[j+1]) && WTF_IS_HEX_DIGIT(su[j+1])
&& WTF_IS_LATIN1(su[j+2]) && WTF_IS_HEX_DIGIT(su[j+2])) {
tlen -= 2;
j += 2;
}
}
if (slen == tlen && !plus) /* shortcut: nothing to unquote */
return string;
if (!(result = PyUnicode_FromUnicode(NULL, tlen)))
goto done;
ru = PyUnicode_AS_UNICODE(result);
for (j=0, sentinel=slen-2; j<slen; ++j) {
if ( j < sentinel && WTF_IS_LATIN1(su[j]) && (su[j] & 0xFF) == '%'
&& WTF_IS_LATIN1(su[j+1]) && WTF_IS_HEX_DIGIT(su[j+1])
&& WTF_IS_LATIN1(su[j+2]) && WTF_IS_HEX_DIGIT(su[j+2])) {
*ru++ = (WTF_HEX_VALUE(su[j+1]) << 4)
+ (WTF_HEX_VALUE(su[j+2]));
j += 2;
}
else if (plus && su[j] == (unsigned char)'+') {
*ru++ = (unsigned char)' ';
}
else {
*ru++ = su[j];
}
}
done:
Py_DECREF(string);
return result;
}