本文整理汇总了C++中PyUnicode_GET_LENGTH函数的典型用法代码示例。如果您正苦于以下问题:C++ PyUnicode_GET_LENGTH函数的具体用法?C++ PyUnicode_GET_LENGTH怎么用?C++ PyUnicode_GET_LENGTH使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了PyUnicode_GET_LENGTH函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C++代码示例。
示例1: zipimport_zipimporter_get_data_impl
static PyObject *
zipimport_zipimporter_get_data_impl(ZipImporter *self, PyObject *path)
/*[clinic end generated code: output=65dc506aaa268436 input=fa6428b74843c4ae]*/
{
PyObject *key;
PyObject *toc_entry;
Py_ssize_t path_start, path_len, len;
if (self->archive == NULL) {
PyErr_SetString(PyExc_ValueError,
"zipimporter.__init__() wasn't called");
return NULL;
}
#ifdef ALTSEP
path = _PyObject_CallMethodId((PyObject *)&PyUnicode_Type, &PyId_replace,
"OCC", path, ALTSEP, SEP);
if (!path)
return NULL;
#else
Py_INCREF(path);
#endif
if (PyUnicode_READY(path) == -1)
goto error;
path_len = PyUnicode_GET_LENGTH(path);
len = PyUnicode_GET_LENGTH(self->archive);
path_start = 0;
if (PyUnicode_Tailmatch(path, self->archive, 0, len, -1)
&& PyUnicode_READ_CHAR(path, len) == SEP) {
path_start = len + 1;
}
key = PyUnicode_Substring(path, path_start, path_len);
if (key == NULL)
goto error;
toc_entry = PyDict_GetItem(self->files, key);
if (toc_entry == NULL) {
PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, key);
Py_DECREF(key);
goto error;
}
Py_DECREF(key);
Py_DECREF(path);
return get_data(self->archive, toc_entry);
error:
Py_DECREF(path);
return NULL;
}
开发者ID:1st1,项目名称:cpython,代码行数:50,代码来源:zipimport.c
示例2: zipimporter_get_data
static PyObject *
zipimporter_get_data(PyObject *obj, PyObject *args)
{
ZipImporter *self = (ZipImporter *)obj;
PyObject *path, *key;
#ifdef ALTSEP
_Py_IDENTIFIER(replace);
#endif
PyObject *toc_entry;
Py_ssize_t path_start, path_len, len;
if (!PyArg_ParseTuple(args, "U:zipimporter.get_data", &path))
return NULL;
#ifdef ALTSEP
path = _PyObject_CallMethodId(path, &PyId_replace, "CC", ALTSEP, SEP);
if (!path)
return NULL;
#else
Py_INCREF(path);
#endif
if (PyUnicode_READY(path) == -1)
goto error;
path_len = PyUnicode_GET_LENGTH(path);
len = PyUnicode_GET_LENGTH(self->archive);
path_start = 0;
if (PyUnicode_Tailmatch(path, self->archive, 0, len, -1)
&& PyUnicode_READ_CHAR(path, len) == SEP) {
path_start = len + 1;
}
key = PyUnicode_Substring(path, path_start, path_len);
if (key == NULL)
goto error;
toc_entry = PyDict_GetItem(self->files, key);
if (toc_entry == NULL) {
PyErr_SetFromErrnoWithFilenameObject(PyExc_IOError, key);
Py_DECREF(key);
goto error;
}
Py_DECREF(key);
Py_DECREF(path);
return get_data(self->archive, toc_entry);
error:
Py_DECREF(path);
return NULL;
}
开发者ID:certik,项目名称:python-3.3,代码行数:49,代码来源:zipimport.c
示例3: asciistr_getbuffer
static int
asciistr_getbuffer(PyObject *exporter, Py_buffer *view, int flags)
{
return (PyBuffer_FillInfo(view, exporter, PyUnicode_DATA(exporter),
PyUnicode_GET_LENGTH(exporter), 1, flags));
}
开发者ID:ncoghlan,项目名称:asciicompat,代码行数:7,代码来源:asciicompat.c
示例4: qpycore_PyObject_AsQString
// Convert a Python Unicode object to a QString.
QString qpycore_PyObject_AsQString(PyObject *obj)
{
#if defined(PYQT_PEP_393)
SIP_SSIZE_T len = PyUnicode_GET_LENGTH(obj);
switch (PyUnicode_KIND(obj))
{
case PyUnicode_1BYTE_KIND:
return QString::fromLatin1((char *)PyUnicode_1BYTE_DATA(obj), len);
case PyUnicode_2BYTE_KIND:
// The (QChar *) cast should be safe.
return QString((QChar *)PyUnicode_2BYTE_DATA(obj), len);
case PyUnicode_4BYTE_KIND:
return QString::fromUcs4(PyUnicode_4BYTE_DATA(obj), len);
}
return QString();
#elif defined(Py_UNICODE_WIDE)
return QString::fromUcs4((const uint *)PyUnicode_AS_UNICODE(obj),
PyUnicode_GET_SIZE(obj));
#else
return QString::fromUtf16((const ushort *)PyUnicode_AS_UNICODE(obj),
PyUnicode_GET_SIZE(obj));
#endif
}
开发者ID:HunterChen,项目名称:pyqt5,代码行数:28,代码来源:qpycore_qstring.cpp
示例5: Reader_iternext
static PyObject *
Reader_iternext(ReaderObj *self)
{
PyObject *fields = NULL;
Py_UCS4 c;
Py_ssize_t pos, linelen;
unsigned int kind;
void *data;
PyObject *lineobj;
if (parse_reset(self) < 0)
return NULL;
do {
lineobj = PyIter_Next(self->input_iter);
if (lineobj == NULL) {
/* End of input OR exception */
if (!PyErr_Occurred() && self->field_len != 0)
PyErr_Format(error_obj,
"newline inside string");
return NULL;
}
if (!PyUnicode_Check(lineobj)) {
PyErr_Format(error_obj,
"iterator should return strings, "
"not %.200s "
"(did you open the file in text mode?)",
lineobj->ob_type->tp_name
);
Py_DECREF(lineobj);
return NULL;
}
++self->line_num;
kind = PyUnicode_KIND(lineobj);
data = PyUnicode_DATA(lineobj);
pos = 0;
linelen = PyUnicode_GET_LENGTH(lineobj);
while (linelen--) {
c = PyUnicode_READ(kind, data, pos);
if (c == '\0') {
Py_DECREF(lineobj);
PyErr_Format(error_obj,
"line contains NULL byte");
goto err;
}
if (parse_process_char(self, c) < 0) {
Py_DECREF(lineobj);
goto err;
}
pos++;
}
Py_DECREF(lineobj);
if (parse_process_char(self, 0) < 0)
goto err;
} while (self->state != START_RECORD);
fields = self->fields;
self->fields = NULL;
err:
return fields;
}
开发者ID:Naddiseo,项目名称:cpython,代码行数:60,代码来源:_csv.c
示例6: join_append
static int
join_append(WriterObj *self, PyObject *field, int *quoted, int quote_empty)
{
unsigned int field_kind = -1;
void *field_data = NULL;
Py_ssize_t field_len = 0;
Py_ssize_t rec_len;
if (field != NULL) {
field_kind = PyUnicode_KIND(field);
field_data = PyUnicode_DATA(field);
field_len = PyUnicode_GET_LENGTH(field);
}
rec_len = join_append_data(self, field_kind, field_data, field_len,
quote_empty, quoted, 0);
if (rec_len < 0)
return 0;
/* grow record buffer if necessary */
if (!join_check_rec_size(self, rec_len))
return 0;
self->rec_len = join_append_data(self, field_kind, field_data, field_len,
quote_empty, quoted, 1);
self->num_fields++;
return 1;
}
开发者ID:Naddiseo,项目名称:cpython,代码行数:28,代码来源:_csv.c
示例7: qpycore_PyObject_AsQString
// Convert a Python Unicode object to a QString.
QString qpycore_PyObject_AsQString(PyObject *obj)
{
#if defined(PYQT_PEP_393)
SIP_SSIZE_T len = PyUnicode_GET_LENGTH(obj);
switch (PyUnicode_KIND(obj))
{
case PyUnicode_1BYTE_KIND:
return QString::fromLatin1((char *)PyUnicode_1BYTE_DATA(obj), len);
case PyUnicode_2BYTE_KIND:
// The (QChar *) cast should be safe.
return QString((QChar *)PyUnicode_2BYTE_DATA(obj), len);
case PyUnicode_4BYTE_KIND:
#if QT_VERSION >= 0x040200
return QString::fromUcs4(PyUnicode_4BYTE_DATA(obj), len);
#else
// Note that this code doesn't handle code points greater than 0xffff
// very well.
QString qstr;
Py_UCS4 *ucode = PyUnicode_4BYTE_DATA(obj);
for (SIP_SSIZE_T i = 0; i < len; ++i)
qstr.append((uint)ucode[i]);
return qstr;
#endif
}
return QString();
#elif defined(Py_UNICODE_WIDE)
#if QT_VERSION >= 0x040200
return QString::fromUcs4((const uint *)PyUnicode_AS_UNICODE(obj),
PyUnicode_GET_SIZE(obj));
#else
// Note that this code doesn't handle code points greater than 0xffff very
// well.
QString qstr;
Py_UNICODE *ucode = PyUnicode_AS_UNICODE(obj);
SIP_SSIZE_T len = PyUnicode_GET_SIZE(obj);
for (SIP_SSIZE_T i = 0; i < len; ++i)
qstr.append((uint)ucode[i]);
return qstr;
#endif
#else
return QString::fromUtf16((const ushort *)PyUnicode_AS_UNICODE(obj),
PyUnicode_GET_SIZE(obj));
#endif
}
开发者ID:EntityFXCode,项目名称:arsenalsuite,代码行数:57,代码来源:qpycore_qstring.cpp
示例8: 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, format_spec, 0,
PyUnicode_GET_LENGTH(format_spec));
}
开发者ID:Naddiseo,项目名称:cpython,代码行数:10,代码来源:complexobject.c
示例9: _str_shared
static int
_str_shared(PyObject *obj, _PyCrossInterpreterData *data)
{
struct _shared_str_data *shared = PyMem_NEW(struct _shared_str_data, 1);
shared->kind = PyUnicode_KIND(obj);
shared->buffer = PyUnicode_DATA(obj);
shared->len = PyUnicode_GET_LENGTH(obj) - 1;
data->data = (void *)shared;
data->obj = obj; // Will be "released" (decref'ed) when data released.
data->new_object = _new_str_object;
data->free = PyMem_Free;
return 0;
}
开发者ID:arizvisa,项目名称:cpython,代码行数:13,代码来源:pystate.c
示例10: syslog_get_argv
static PyObject *
syslog_get_argv(void)
{
/* Figure out what to use for as the program "ident" for openlog().
* This swallows exceptions and continues rather than failing out,
* because the syslog module can still be used because openlog(3)
* is optional.
*/
Py_ssize_t argv_len, scriptlen;
PyObject *scriptobj;
Py_ssize_t slash;
PyObject *argv = PySys_GetObject("argv");
if (argv == NULL) {
return(NULL);
}
argv_len = PyList_Size(argv);
if (argv_len == -1) {
PyErr_Clear();
return(NULL);
}
if (argv_len == 0) {
return(NULL);
}
scriptobj = PyList_GetItem(argv, 0);
if (!PyUnicode_Check(scriptobj)) {
return(NULL);
}
scriptlen = PyUnicode_GET_LENGTH(scriptobj);
if (scriptlen == 0) {
return(NULL);
}
slash = PyUnicode_FindChar(scriptobj, SEP, 0, scriptlen, -1);
if (slash == -2)
return NULL;
if (slash != -1) {
return PyUnicode_Substring(scriptobj, slash, scriptlen);
} else {
Py_INCREF(scriptobj);
return(scriptobj);
}
return(NULL);
}
开发者ID:IgnusIndia,项目名称:pythonexperiment,代码行数:48,代码来源:syslogmodule.c
示例11: all_name_chars
/* all_name_chars(s): true iff s matches [a-zA-Z0-9_]* */
static int
all_name_chars(PyObject *o)
{
const unsigned char *s, *e;
if (!PyUnicode_IS_ASCII(o))
return 0;
s = PyUnicode_1BYTE_DATA(o);
e = s + PyUnicode_GET_LENGTH(o);
for (; s != e; s++) {
if (!Py_ISALNUM(*s) && *s != '_')
return 0;
}
return 1;
}
开发者ID:Apoorvadabhere,项目名称:cpython,代码行数:17,代码来源:codeobject.c
示例12: __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;
}
开发者ID:hfeeki,项目名称:numba,代码行数:44,代码来源:type_conversion.c
示例13: complex__format__
static PyObject *
complex__format__(PyObject* self, PyObject* args)
{
PyObject *format_spec;
_PyUnicodeWriter writer;
int ret;
if (!PyArg_ParseTuple(args, "U:__format__", &format_spec))
return NULL;
_PyUnicodeWriter_Init(&writer);
ret = _PyComplex_FormatAdvancedWriter(
&writer,
self,
format_spec, 0, PyUnicode_GET_LENGTH(format_spec));
if (ret == -1) {
_PyUnicodeWriter_Dealloc(&writer);
return NULL;
}
return _PyUnicodeWriter_Finish(&writer);
}
开发者ID:1st1,项目名称:cpython,代码行数:21,代码来源:complexobject.c
示例14: join_append_lineterminator
static int
join_append_lineterminator(WriterObj *self)
{
Py_ssize_t terminator_len, i;
unsigned int term_kind;
void *term_data;
terminator_len = PyUnicode_GET_LENGTH(self->dialect->lineterminator);
if (terminator_len == -1)
return 0;
/* grow record buffer if necessary */
if (!join_check_rec_size(self, self->rec_len + terminator_len))
return 0;
term_kind = PyUnicode_KIND(self->dialect->lineterminator);
term_data = PyUnicode_DATA(self->dialect->lineterminator);
for (i = 0; i < terminator_len; i++)
self->rec[self->rec_len + i] = PyUnicode_READ(term_kind, term_data, i);
self->rec_len += terminator_len;
return 1;
}
开发者ID:Naddiseo,项目名称:cpython,代码行数:23,代码来源:_csv.c
示例15: PyUnicode_INDEX
Py_ssize_t
PyUnicode_INDEX(const char *text, Py_ssize_t index)
{
PyObject *u;
Py_ssize_t i;
/* Prevent excessive micro allocations */
char buffer[256];
size_t buffer_size = Py_ARRAY_LENGTH(buffer);
char *s = buffer;
size_t l;
/* Short-circuit */
if (index == 0)
return 0;
l = strlen(text);
if (index > l)
index = l;
if (index >= buffer_size) {
s = PyMem_RawMalloc(index+1);
if (s == NULL)
return -1;
}
strncpy(s, text, index);
s[index] = '\0';
u = PyUnicode_DECODE(s);
if (s != buffer)
PyMem_RawFree(s);
if (u == NULL)
return -1;
i = PyUnicode_GET_LENGTH(u);
Py_DECREF(u);
return i;
}
开发者ID:stefanholek,项目名称:rl,代码行数:36,代码来源:unicode.c
示例16: namespace_repr
static PyObject *
namespace_repr(PyObject *ns)
{
int i, loop_error = 0;
PyObject *pairs = NULL, *d = NULL, *keys = NULL, *keys_iter = NULL;
PyObject *key;
PyObject *separator, *pairsrepr, *repr = NULL;
const char * name;
name = (Py_TYPE(ns) == &_PyNamespace_Type) ? "namespace"
: ns->ob_type->tp_name;
i = Py_ReprEnter(ns);
if (i != 0) {
return i > 0 ? PyUnicode_FromFormat("%s(...)", name) : NULL;
}
pairs = PyList_New(0);
if (pairs == NULL)
goto error;
d = ((_PyNamespaceObject *)ns)->ns_dict;
assert(d != NULL);
Py_INCREF(d);
keys = PyDict_Keys(d);
if (keys == NULL)
goto error;
if (PyList_Sort(keys) != 0)
goto error;
keys_iter = PyObject_GetIter(keys);
if (keys_iter == NULL)
goto error;
while ((key = PyIter_Next(keys_iter)) != NULL) {
if (PyUnicode_Check(key) && PyUnicode_GET_LENGTH(key) > 0) {
PyObject *value, *item;
value = PyDict_GetItem(d, key);
assert(value != NULL);
item = PyUnicode_FromFormat("%S=%R", key, value);
if (item == NULL) {
loop_error = 1;
}
else {
loop_error = PyList_Append(pairs, item);
Py_DECREF(item);
}
}
Py_DECREF(key);
if (loop_error)
goto error;
}
separator = PyUnicode_FromString(", ");
if (separator == NULL)
goto error;
pairsrepr = PyUnicode_Join(separator, pairs);
Py_DECREF(separator);
if (pairsrepr == NULL)
goto error;
repr = PyUnicode_FromFormat("%s(%S)", name, pairsrepr);
Py_DECREF(pairsrepr);
error:
Py_XDECREF(pairs);
Py_XDECREF(d);
Py_XDECREF(keys);
Py_XDECREF(keys_iter);
Py_ReprLeave(ns);
return repr;
}
开发者ID:ARK4579,项目名称:cpython,代码行数:78,代码来源:namespaceobject.c
示例17: setup_context
/* Returns 0 on error (no new refs), 1 on success */
static int
setup_context(Py_ssize_t stack_level, PyObject **filename, int *lineno,
PyObject **module, PyObject **registry)
{
PyObject *globals;
/* Setup globals and lineno. */
PyFrameObject *f = PyThreadState_GET()->frame;
// Stack level comparisons to Python code is off by one as there is no
// warnings-related stack level to avoid.
if (stack_level <= 0 || is_internal_frame(f)) {
while (--stack_level > 0 && f != NULL) {
f = f->f_back;
}
}
else {
while (--stack_level > 0 && f != NULL) {
f = next_external_frame(f);
}
}
if (f == NULL) {
globals = PyThreadState_Get()->interp->sysdict;
*lineno = 1;
}
else {
globals = f->f_globals;
*lineno = PyFrame_GetLineNumber(f);
}
*module = NULL;
/* Setup registry. */
assert(globals != NULL);
assert(PyDict_Check(globals));
*registry = PyDict_GetItemString(globals, "__warningregistry__");
if (*registry == NULL) {
int rc;
*registry = PyDict_New();
if (*registry == NULL)
return 0;
rc = PyDict_SetItemString(globals, "__warningregistry__", *registry);
if (rc < 0)
goto handle_error;
}
else
Py_INCREF(*registry);
/* Setup module. */
*module = PyDict_GetItemString(globals, "__name__");
if (*module == NULL) {
*module = PyUnicode_FromString("<string>");
if (*module == NULL)
goto handle_error;
}
else
Py_INCREF(*module);
/* Setup filename. */
*filename = PyDict_GetItemString(globals, "__file__");
if (*filename != NULL && PyUnicode_Check(*filename)) {
Py_ssize_t len;
int kind;
void *data;
if (PyUnicode_READY(*filename))
goto handle_error;
len = PyUnicode_GetLength(*filename);
kind = PyUnicode_KIND(*filename);
data = PyUnicode_DATA(*filename);
#define ascii_lower(c) ((c <= 127) ? Py_TOLOWER(c) : 0)
/* if filename.lower().endswith(".pyc"): */
if (len >= 4 &&
PyUnicode_READ(kind, data, len-4) == '.' &&
ascii_lower(PyUnicode_READ(kind, data, len-3)) == 'p' &&
ascii_lower(PyUnicode_READ(kind, data, len-2)) == 'y' &&
ascii_lower(PyUnicode_READ(kind, data, len-1)) == 'c')
{
*filename = PyUnicode_Substring(*filename, 0,
PyUnicode_GET_LENGTH(*filename)-1);
if (*filename == NULL)
goto handle_error;
}
else
Py_INCREF(*filename);
}
else {
*filename = NULL;
if (*module != Py_None && PyUnicode_CompareWithASCIIString(*module, "__main__") == 0) {
PyObject *argv = _PySys_GetObjectId(&PyId_argv);
/* PyList_Check() is needed because sys.argv is set to None during
Python finalization */
if (argv != NULL && PyList_Check(argv) && PyList_Size(argv) > 0) {
int is_true;
*filename = PyList_GetItem(argv, 0);
//.........这里部分代码省略.........
开发者ID:PiJoules,项目名称:cpython-modified,代码行数:101,代码来源:_warnings.c
示例18: show_warning
static void
show_warning(PyObject *filename, int lineno, PyObject *text,
PyObject *category, PyObject *sourceline)
{
PyObject *f_stderr;
PyObject *name;
char lineno_str[128];
_Py_IDENTIFIER(__name__);
PyOS_snprintf(lineno_str, sizeof(lineno_str), ":%d: ", lineno);
name = _PyObject_GetAttrId(category, &PyId___name__);
if (name == NULL) /* XXX Can an object lack a '__name__' attribute? */
goto error;
f_stderr = _PySys_GetObjectId(&PyId_stderr);
if (f_stderr == NULL) {
fprintf(stderr, "lost sys.stderr\n");
goto error;
}
/* Print "filename:lineno: category: text\n" */
if (PyFile_WriteObject(filename, f_stderr, Py_PRINT_RAW) < 0)
goto error;
if (PyFile_WriteString(lineno_str, f_stderr) < 0)
goto error;
if (PyFile_WriteObject(name, f_stderr, Py_PRINT_RAW) < 0)
goto error;
if (PyFile_WriteString(": ", f_stderr) < 0)
goto error;
if (PyFile_WriteObject(text, f_stderr, Py_PRINT_RAW) < 0)
goto error;
if (PyFile_WriteString("\n", f_stderr) < 0)
goto error;
Py_CLEAR(name);
/* Print " source_line\n" */
if (sourceline) {
int kind;
void *data;
Py_ssize_t i, len;
Py_UCS4 ch;
PyObject *truncated;
if (PyUnicode_READY(sourceline) < 1)
goto error;
kind = PyUnicode_KIND(sourceline);
data = PyUnicode_DATA(sourceline);
len = PyUnicode_GET_LENGTH(sourceline);
for (i=0; i<len; i++) {
ch = PyUnicode_READ(kind, data, i);
if (ch != ' ' && ch != '\t' && ch != '\014')
break;
}
truncated = PyUnicode_Substring(sourceline, i, len);
if (truncated == NULL)
goto error;
PyFile_WriteObject(sourceline, f_stderr, Py_PRINT_RAW);
Py_DECREF(truncated);
PyFile_WriteString("\n", f_stderr);
}
else {
_Py_DisplaySourceLine(f_stderr, filename, lineno, 2);
}
error:
Py_XDECREF(name);
PyErr_Clear();
}
开发者ID:PiJoules,项目名称:cpython-modified,代码行数:72,代码来源:_warnings.c
示例19: format_long_internal
//.........这里部分代码省略.........
these are characters that don't get formatted, only copied
into the output string. We do this for 'c' formatting,
because the characters are likely to be non-digits. */
n_remainder = 1;
}
else {
int base;
int leading_chars_to_skip = 0; /* Number of characters added by
PyNumber_ToBase that we want to
skip over. */
/* Compute the base and how many characters will be added by
PyNumber_ToBase */
switch (format->type) {
case 'b':
base = 2;
leading_chars_to_skip = 2; /* 0b */
break;
case 'o':
base = 8;
leading_chars_to_skip = 2; /* 0o */
break;
case 'x':
case 'X':
base = 16;
leading_chars_to_skip = 2; /* 0x */
break;
default: /* shouldn't be needed, but stops a compiler warning */
case 'd':
case 'n':
base = 10;
break;
}
if (format->sign != '+' && format->sign != ' '
&& format->width == -1
&& format->type != 'X' && format->type != 'n'
&& !format->thousands_separators
&& PyLong_CheckExact(value))
{
/* Fast path */
return _PyLong_FormatWriter(writer, value, base, format->alternate);
}
/* The number of prefix chars is the same as the leading
chars to skip */
if (format->alternate)
n_prefix = leading_chars_to_skip;
/* Do the hard part, converting to a string in a given base */
tmp = _PyLong_Format(value, base);
if (tmp == NULL || PyUnicode_READY(tmp) == -1)
goto done;
inumeric_chars = 0;
n_digits = PyUnicode_GET_LENGTH(tmp);
prefix = inumeric_chars;
/* Is a sign character present in the output? If so, remember it
and skip it */
if (PyUnicode_READ_CHAR(tmp, inumeric_chars) == '-') {
sign_char = '-';
++prefix;
++leading_chars_to_skip;
}
/* Skip over the leading chars (0x, 0b, etc.) */
n_digits -= leading_chars_to_skip;
inumeric_chars += leading_chars_to_skip;
}
/* Determine the grouping, separator, and decimal point, if any. */
if (get_locale_info(format->type == 'n' ? LT_CURRENT_LOCALE :
(format->thousands_separators ?
LT_DEFAULT_LOCALE :
LT_NO_LOCALE),
&locale) == -1)
goto done;
/* Calculate how much memory we'll need. */
n_total = calc_number_widths(&spec, n_prefix, sign_char, tmp, inumeric_chars,
inumeric_chars + n_digits, n_remainder, 0,
&locale, format, &maxchar);
/* Allocate the memory. */
if (_PyUnicodeWriter_Prepare(writer, n_total, maxchar) == -1)
goto done;
/* Populate the memory. */
result = fill_number(writer, &spec,
tmp, inumeric_chars, inumeric_chars + n_digits,
tmp, prefix, format->fill_char,
&locale, format->type == 'X');
done:
Py_XDECREF(tmp);
free_locale_info(&locale);
return result;
}
开发者ID:1564143452,项目名称:kbengine,代码行数:101,代码来源:formatter_unicode.c
示例20: format_string_internal
static int
format_string_internal(PyObject *value, const InternalFormatSpec *format,
_PyUnicodeWriter *writer)
{
Py_ssize_t lpad;
Py_ssize_t rpad;
Py_ssize_t total;
Py_ssize_t len;
int result = -1;
Py_UCS4 maxchar;
assert(PyUnicode_IS_READY(value));
len = PyUnicode_GET_LENGTH(value);
/* sign is not allowed on strings */
if (format->sign != '\0') {
PyErr_SetString(PyExc_ValueError,
"Sign not allowed in string format specifier");
goto done;
}
/* alternate is not allowed on strings */
if (format->alternate) {
PyErr_SetString(PyExc_ValueError,
"Alternate form (#) not allowed in string format "
"specifier");
goto done;
}
/* '=' alignment not allowed on strings */
if (format->align == '=') {
PyErr_SetString(PyExc_ValueError,
"'=' alignment not allowed "
"in string format specifier");
goto done;
}
if ((format->width == -1 || format->width <= len)
&& (format->precision == -1 || format->precision >= len)) {
/* Fast path */
return _PyUnicodeWriter_WriteStr(writer, value);
}
/* if precision is specified, output no more that format.precision
characters */
if (format->precision >= 0 && len >= format->precision) {
len = format->precision;
}
calc_padding(len, format->width, format->align, &lpad, &rpad, &total);
maxchar = writer->maxchar;
if (lpad != 0 || rpad != 0)
maxchar = Py_MAX(maxchar, format->fill_char);
if (PyUnicode_MAX_CHAR_VALUE(value) > maxchar) {
Py_UCS4 valmaxchar = _PyUnicode_FindMaxChar(value, 0, len);
maxchar = Py_MAX(maxchar, valmaxchar);
}
/* allocate the resulting string */
if (_PyUnicodeWriter_Prepare(writer, total, maxchar) == -1)
goto done;
/* Write into that space. First the padding. */
result = fill_padding(writer, len, format->fill_char, lpad, rpad);
if (result == -1)
goto done;
/* Then the source string. */
if (len) {
_PyUnicode_FastCopyCharacters(writer->buffer, writer->pos,
value, 0, len);
}
writer->pos += (len + rpad);
result = 0;
done:
return result;
}
开发者ID:1564143452,项目名称:kbengine,代码行数:79,代码来源:formatter_unicode.c
注:本文中的PyUnicode_GET_LENGTH函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论