Add functions PyUnicode_Append() and PyUnicode_AppendAndDel() that mirror

PyString_Concat() and PyString_ConcatAndDel() (the name PyUnicode_Concat()
was already taken).

Change PyObject_Repr() to always return a unicode object.

Update all repr implementations to return unicode objects.

Add a function PyObject_ReprStr8() that calls PyObject_Repr() and converts
the result to an 8bit string.

Use PyObject_ReprStr8() where using PyObject_Repr() can't be done
straightforward.
This commit is contained in:
Walter Dörwald 2007-05-18 17:15:44 +00:00
parent 14176a56d3
commit 1ab8330827
49 changed files with 385 additions and 255 deletions

View file

@ -372,6 +372,7 @@ PyAPI_FUNC(int) PyObject_Print(PyObject *, FILE *, int);
PyAPI_FUNC(void) _Py_BreakPoint(void); PyAPI_FUNC(void) _Py_BreakPoint(void);
PyAPI_FUNC(void) _PyObject_Dump(PyObject *); PyAPI_FUNC(void) _PyObject_Dump(PyObject *);
PyAPI_FUNC(PyObject *) PyObject_Repr(PyObject *); PyAPI_FUNC(PyObject *) PyObject_Repr(PyObject *);
PyAPI_FUNC(PyObject *) PyObject_ReprStr8(PyObject *);
PyAPI_FUNC(PyObject *) _PyObject_Str(PyObject *); PyAPI_FUNC(PyObject *) _PyObject_Str(PyObject *);
PyAPI_FUNC(PyObject *) PyObject_Str(PyObject *); PyAPI_FUNC(PyObject *) PyObject_Str(PyObject *);
PyAPI_FUNC(PyObject *) PyObject_Unicode(PyObject *); PyAPI_FUNC(PyObject *) PyObject_Unicode(PyObject *);
@ -418,7 +419,7 @@ PyAPI_FUNC(long) _Py_HashDouble(double);
PyAPI_FUNC(long) _Py_HashPointer(void*); PyAPI_FUNC(long) _Py_HashPointer(void*);
/* Helper for passing objects to printf and the like */ /* Helper for passing objects to printf and the like */
#define PyObject_REPR(obj) PyString_AS_STRING(PyObject_Repr(obj)) #define PyObject_REPR(obj) PyString_AS_STRING(PyObject_ReprStr8(obj))
/* Flag bits for printing: */ /* Flag bits for printing: */
#define Py_PRINT_RAW 1 /* No string quotes etc. */ #define Py_PRINT_RAW 1 /* No string quotes etc. */

View file

@ -145,6 +145,8 @@ typedef PY_UNICODE_TYPE Py_UNICODE;
# define PyUnicode_AsWideChar PyUnicodeUCS2_AsWideChar # define PyUnicode_AsWideChar PyUnicodeUCS2_AsWideChar
# define PyUnicode_Compare PyUnicodeUCS2_Compare # define PyUnicode_Compare PyUnicodeUCS2_Compare
# define PyUnicode_Concat PyUnicodeUCS2_Concat # define PyUnicode_Concat PyUnicodeUCS2_Concat
# define PyUnicode_Append PyUnicodeUCS2_Append
# define PyUnicode_AppendAndDel PyUnicodeUCS2_AppendAndDel
# define PyUnicode_Contains PyUnicodeUCS2_Contains # define PyUnicode_Contains PyUnicodeUCS2_Contains
# define PyUnicode_Count PyUnicodeUCS2_Count # define PyUnicode_Count PyUnicodeUCS2_Count
# define PyUnicode_Decode PyUnicodeUCS2_Decode # define PyUnicode_Decode PyUnicodeUCS2_Decode
@ -227,6 +229,8 @@ typedef PY_UNICODE_TYPE Py_UNICODE;
# define PyUnicode_AsWideChar PyUnicodeUCS4_AsWideChar # define PyUnicode_AsWideChar PyUnicodeUCS4_AsWideChar
# define PyUnicode_Compare PyUnicodeUCS4_Compare # define PyUnicode_Compare PyUnicodeUCS4_Compare
# define PyUnicode_Concat PyUnicodeUCS4_Concat # define PyUnicode_Concat PyUnicodeUCS4_Concat
# define PyUnicode_Append PyUnicodeUCS4_Append
# define PyUnicode_AppendAndDel PyUnicodeUCS4_AppendAndDel
# define PyUnicode_Contains PyUnicodeUCS4_Contains # define PyUnicode_Contains PyUnicodeUCS4_Contains
# define PyUnicode_Count PyUnicodeUCS4_Count # define PyUnicode_Count PyUnicodeUCS4_Count
# define PyUnicode_Decode PyUnicodeUCS4_Decode # define PyUnicode_Decode PyUnicodeUCS4_Decode
@ -1020,6 +1024,22 @@ PyAPI_FUNC(PyObject*) PyUnicode_Concat(
PyObject *right /* Right string */ PyObject *right /* Right string */
); );
/* Concat two strings and put the result in *pleft
(sets *pleft to NULL on error) */
PyAPI_FUNC(void) PyUnicode_Append(
PyObject **pleft, /* Pointer to left string */
PyObject *right /* Right string */
);
/* Concat two strings, put the result in *pleft and drop the right object
(sets *pleft to NULL on error) */
PyAPI_FUNC(void) PyUnicode_AppendAndDel(
PyObject **pleft, /* Pointer to left string */
PyObject *right /* Right string */
);
/* Split a string giving a list of Unicode strings. /* Split a string giving a list of Unicode strings.
If sep is NULL, splitting will be done at all whitespace If sep is NULL, splitting will be done at all whitespace

View file

@ -392,7 +392,7 @@ static PyObject * CFTypeRefObj_repr(CFTypeRefObject *self)
{ {
char buf[100]; char buf[100];
sprintf(buf, "<CFTypeRef type-%d object at 0x%8.8x for 0x%8.8x>", (int)CFGetTypeID(self->ob_itself), (unsigned)self, (unsigned)self->ob_itself); sprintf(buf, "<CFTypeRef type-%d object at 0x%8.8x for 0x%8.8x>", (int)CFGetTypeID(self->ob_itself), (unsigned)self, (unsigned)self->ob_itself);
return PyString_FromString(buf); return PyUnicode_FromString(buf);
} }
static int CFTypeRefObj_hash(CFTypeRefObject *self) static int CFTypeRefObj_hash(CFTypeRefObject *self)
@ -596,7 +596,7 @@ static PyObject * CFArrayRefObj_repr(CFArrayRefObject *self)
{ {
char buf[100]; char buf[100];
sprintf(buf, "<CFArrayRef object at 0x%8.8x for 0x%8.8x>", (unsigned)self, (unsigned)self->ob_itself); sprintf(buf, "<CFArrayRef object at 0x%8.8x for 0x%8.8x>", (unsigned)self, (unsigned)self->ob_itself);
return PyString_FromString(buf); return PyUnicode_FromString(buf);
} }
static int CFArrayRefObj_hash(CFArrayRefObject *self) static int CFArrayRefObj_hash(CFArrayRefObject *self)
@ -836,7 +836,7 @@ static PyObject * CFMutableArrayRefObj_repr(CFMutableArrayRefObject *self)
{ {
char buf[100]; char buf[100];
sprintf(buf, "<CFMutableArrayRef object at 0x%8.8x for 0x%8.8x>", (unsigned)self, (unsigned)self->ob_itself); sprintf(buf, "<CFMutableArrayRef object at 0x%8.8x for 0x%8.8x>", (unsigned)self, (unsigned)self->ob_itself);
return PyString_FromString(buf); return PyUnicode_FromString(buf);
} }
static int CFMutableArrayRefObj_hash(CFMutableArrayRefObject *self) static int CFMutableArrayRefObj_hash(CFMutableArrayRefObject *self)
@ -1029,7 +1029,7 @@ static PyObject * CFDictionaryRefObj_repr(CFDictionaryRefObject *self)
{ {
char buf[100]; char buf[100];
sprintf(buf, "<CFDictionaryRef object at 0x%8.8x for 0x%8.8x>", (unsigned)self, (unsigned)self->ob_itself); sprintf(buf, "<CFDictionaryRef object at 0x%8.8x for 0x%8.8x>", (unsigned)self, (unsigned)self->ob_itself);
return PyString_FromString(buf); return PyUnicode_FromString(buf);
} }
static int CFDictionaryRefObj_hash(CFDictionaryRefObject *self) static int CFDictionaryRefObj_hash(CFDictionaryRefObject *self)
@ -1206,7 +1206,7 @@ static PyObject * CFMutableDictionaryRefObj_repr(CFMutableDictionaryRefObject *s
{ {
char buf[100]; char buf[100];
sprintf(buf, "<CFMutableDictionaryRef object at 0x%8.8x for 0x%8.8x>", (unsigned)self, (unsigned)self->ob_itself); sprintf(buf, "<CFMutableDictionaryRef object at 0x%8.8x for 0x%8.8x>", (unsigned)self, (unsigned)self->ob_itself);
return PyString_FromString(buf); return PyUnicode_FromString(buf);
} }
static int CFMutableDictionaryRefObj_hash(CFMutableDictionaryRefObject *self) static int CFMutableDictionaryRefObj_hash(CFMutableDictionaryRefObject *self)
@ -1437,7 +1437,7 @@ static PyObject * CFDataRefObj_repr(CFDataRefObject *self)
{ {
char buf[100]; char buf[100];
sprintf(buf, "<CFDataRef object at 0x%8.8x for 0x%8.8x>", (unsigned)self, (unsigned)self->ob_itself); sprintf(buf, "<CFDataRef object at 0x%8.8x for 0x%8.8x>", (unsigned)self, (unsigned)self->ob_itself);
return PyString_FromString(buf); return PyUnicode_FromString(buf);
} }
static int CFDataRefObj_hash(CFDataRefObject *self) static int CFDataRefObj_hash(CFDataRefObject *self)
@ -1702,7 +1702,7 @@ static PyObject * CFMutableDataRefObj_repr(CFMutableDataRefObject *self)
{ {
char buf[100]; char buf[100];
sprintf(buf, "<CFMutableDataRef object at 0x%8.8x for 0x%8.8x>", (unsigned)self, (unsigned)self->ob_itself); sprintf(buf, "<CFMutableDataRef object at 0x%8.8x for 0x%8.8x>", (unsigned)self, (unsigned)self->ob_itself);
return PyString_FromString(buf); return PyUnicode_FromString(buf);
} }
static int CFMutableDataRefObj_hash(CFMutableDataRefObject *self) static int CFMutableDataRefObj_hash(CFMutableDataRefObject *self)
@ -2444,7 +2444,7 @@ static PyObject * CFStringRefObj_repr(CFStringRefObject *self)
{ {
char buf[100]; char buf[100];
sprintf(buf, "<CFStringRef object at 0x%8.8x for 0x%8.8x>", (unsigned)self, (unsigned)self->ob_itself); sprintf(buf, "<CFStringRef object at 0x%8.8x for 0x%8.8x>", (unsigned)self, (unsigned)self->ob_itself);
return PyString_FromString(buf); return PyUnicode_FromString(buf);
} }
static int CFStringRefObj_hash(CFStringRefObject *self) static int CFStringRefObj_hash(CFStringRefObject *self)
@ -2832,7 +2832,7 @@ static PyObject * CFMutableStringRefObj_repr(CFMutableStringRefObject *self)
{ {
char buf[100]; char buf[100];
sprintf(buf, "<CFMutableStringRef object at 0x%8.8x for 0x%8.8x>", (unsigned)self, (unsigned)self->ob_itself); sprintf(buf, "<CFMutableStringRef object at 0x%8.8x for 0x%8.8x>", (unsigned)self, (unsigned)self->ob_itself);
return PyString_FromString(buf); return PyUnicode_FromString(buf);
} }
static int CFMutableStringRefObj_hash(CFMutableStringRefObject *self) static int CFMutableStringRefObj_hash(CFMutableStringRefObject *self)
@ -3484,7 +3484,7 @@ static PyObject * CFURLRefObj_repr(CFURLRefObject *self)
{ {
char buf[100]; char buf[100];
sprintf(buf, "<CFURL object at 0x%8.8x for 0x%8.8x>", (unsigned)self, (unsigned)self->ob_itself); sprintf(buf, "<CFURL object at 0x%8.8x for 0x%8.8x>", (unsigned)self, (unsigned)self->ob_itself);
return PyString_FromString(buf); return PyUnicode_FromString(buf);
} }
static int CFURLRefObj_hash(CFURLRefObject *self) static int CFURLRefObj_hash(CFURLRefObject *self)

View file

@ -1393,7 +1393,7 @@ static PyObject * FSSpec_repr(FSSpecObject *self)
self->ob_itself.vRefNum, self->ob_itself.vRefNum,
self->ob_itself.parID, self->ob_itself.parID,
self->ob_itself.name[0], self->ob_itself.name+1); self->ob_itself.name[0], self->ob_itself.name+1);
return PyString_FromString(buf); return PyUnicode_FromString(buf);
} }
#define FSSpec_hash NULL #define FSSpec_hash NULL

View file

@ -2580,7 +2580,7 @@ static PyObject * WinObj_repr(WindowObject *self)
{ {
char buf[100]; char buf[100];
sprintf(buf, "<Window object at 0x%8.8x for 0x%8.8x>", (unsigned)self, (unsigned)self->ob_itself); sprintf(buf, "<Window object at 0x%8.8x for 0x%8.8x>", (unsigned)self, (unsigned)self->ob_itself);
return PyString_FromString(buf); return PyUnicode_FromString(buf);
} }
static int WinObj_hash(WindowObject *self) static int WinObj_hash(WindowObject *self)

View file

@ -161,27 +161,63 @@ static PyObject *
escape_encode(PyObject *self, escape_encode(PyObject *self,
PyObject *args) PyObject *args)
{ {
static const char *hexdigits = "0123456789abcdef";
PyObject *str; PyObject *str;
Py_ssize_t size;
Py_ssize_t newsize;
const char *errors = NULL; const char *errors = NULL;
char *buf; PyObject *v;
Py_ssize_t len;
if (!PyArg_ParseTuple(args, "O!|z:escape_encode", if (!PyArg_ParseTuple(args, "O!|z:escape_encode",
&PyString_Type, &str, &errors)) &PyString_Type, &str, &errors))
return NULL; return NULL;
str = PyString_Repr(str, 0); size = PyUnicode_GET_SIZE(str);
if (!str) newsize = 4*size;
return NULL; if (newsize > PY_SSIZE_T_MAX || newsize / 4 != size) {
PyErr_SetString(PyExc_OverflowError,
"string is too large to encode");
return NULL;
}
v = PyBytes_FromStringAndSize(NULL, newsize);
/* The string will be quoted. Unquote, similar to unicode-escape. */ if (v == NULL) {
buf = PyString_AS_STRING (str);
len = PyString_GET_SIZE (str);
memmove(buf, buf+1, len-2);
if (_PyString_Resize(&str, len-2) < 0)
return NULL; return NULL;
}
else {
register Py_ssize_t i;
register char c;
register char *p = PyBytes_AS_STRING(v);
return codec_tuple(str, PyString_Size(str)); 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 = PyString_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++ = hexdigits[(c & 0xf0) >> 4];
*p++ = hexdigits[c & 0xf];
}
else
*p++ = c;
}
*p = '\0';
if (PyBytes_Resize(v, (p - PyBytes_AS_STRING(v)))) {
Py_DECREF(v);
return NULL;
}
}
return codec_tuple(v, PyBytes_Size(v));
} }
/* --- Decoder ------------------------------------------------------------ */ /* --- Decoder ------------------------------------------------------------ */

View file

@ -611,14 +611,14 @@ PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
static PyObject * static PyObject *
deque_repr(PyObject *deque) deque_repr(PyObject *deque)
{ {
PyObject *aslist, *result, *fmt; PyObject *aslist, *result;
int i; int i;
i = Py_ReprEnter(deque); i = Py_ReprEnter(deque);
if (i != 0) { if (i != 0) {
if (i < 0) if (i < 0)
return NULL; return NULL;
return PyString_FromString("[...]"); return PyUnicode_FromString("[...]");
} }
aslist = PySequence_List(deque); aslist = PySequence_List(deque);
@ -627,14 +627,14 @@ deque_repr(PyObject *deque)
return NULL; return NULL;
} }
fmt = PyString_FromString("deque(%r)"); result = PyUnicode_FromString("deque(");
if (fmt == NULL) { if (result == NULL) {
Py_DECREF(aslist); Py_DECREF(aslist);
Py_ReprLeave(deque); Py_ReprLeave(deque);
return NULL; return NULL;
} }
result = PyString_Format(fmt, aslist); PyUnicode_AppendAndDel(&result, PyObject_Repr(aslist));
Py_DECREF(fmt); PyUnicode_AppendAndDel(&result, PyUnicode_FromString(")"));
Py_DECREF(aslist); Py_DECREF(aslist);
Py_ReprLeave(deque); Py_ReprLeave(deque);
return result; return result;
@ -1215,18 +1215,18 @@ defdict_repr(defdictobject *dd)
if (baserepr == NULL) if (baserepr == NULL)
return NULL; return NULL;
if (dd->default_factory == NULL) if (dd->default_factory == NULL)
defrepr = PyString_FromString("None"); defrepr = PyUnicode_FromString("None");
else else
defrepr = PyObject_Repr(dd->default_factory); defrepr = PyObject_Repr(dd->default_factory);
if (defrepr == NULL) { if (defrepr == NULL) {
Py_DECREF(baserepr); Py_DECREF(baserepr);
return NULL; return NULL;
} }
result = PyString_FromFormat("defaultdict(%s, %s)", result = PyUnicode_FromString("defaultdict(");
PyString_AS_STRING(defrepr), PyUnicode_AppendAndDel(&result, defrepr);
PyString_AS_STRING(baserepr)); PyUnicode_AppendAndDel(&result, PyUnicode_FromString(", "));
Py_DECREF(defrepr); PyUnicode_AppendAndDel(&result, baserepr);
Py_DECREF(baserepr); PyUnicode_AppendAndDel(&result, PyUnicode_FromString(")"));
return result; return result;
} }

View file

@ -3446,12 +3446,12 @@ CFuncPtr_repr(CFuncPtrObject *self)
{ {
#ifdef MS_WIN32 #ifdef MS_WIN32
if (self->index) if (self->index)
return PyString_FromFormat("<COM method offset %d: %s at %p>", return PyUnicode_FromFormat("<COM method offset %d: %s at %p>",
self->index - 0x1000, self->index - 0x1000,
self->ob_type->tp_name, self->ob_type->tp_name,
self); self);
#endif #endif
return PyString_FromFormat("<%s object at %p>", return PyUnicode_FromFormat("<%s object at %p>",
self->ob_type->tp_name, self->ob_type->tp_name,
self); self);
} }
@ -4081,12 +4081,12 @@ Simple_repr(CDataObject *self)
static PyObject *format; static PyObject *format;
if (self->ob_type->tp_base != &Simple_Type) { if (self->ob_type->tp_base != &Simple_Type) {
return PyString_FromFormat("<%s object at %p>", return PyUnicode_FromFormat("<%s object at %p>",
self->ob_type->tp_name, self); self->ob_type->tp_name, self);
} }
if (format == NULL) { if (format == NULL) {
format = PyString_FromString("%s(%r)"); format = PyUnicode_FromString("%s(%r)");
if (format == NULL) if (format == NULL)
return NULL; return NULL;
} }
@ -4095,7 +4095,7 @@ Simple_repr(CDataObject *self)
if (val == NULL) if (val == NULL)
return NULL; return NULL;
name = PyString_FromString(self->ob_type->tp_name); name = PyUnicode_FromString(self->ob_type->tp_name);
if (name == NULL) { if (name == NULL) {
Py_DECREF(val); Py_DECREF(val);
return NULL; return NULL;
@ -4107,7 +4107,7 @@ Simple_repr(CDataObject *self)
if (args == NULL) if (args == NULL)
return NULL; return NULL;
result = PyString_Format(format, args); result = PyUnicode_Format(format, args);
Py_DECREF(args); Py_DECREF(args);
return result; return result;
} }

View file

@ -370,7 +370,7 @@ PyCArg_repr(PyCArgObject *self)
self->tag, (long)self); self->tag, (long)self);
break; break;
} }
return PyString_FromString(buffer); return PyUnicode_FromString(buffer);
} }
static PyMemberDef PyCArgType_members[] = { static PyMemberDef PyCArgType_members[] = {

View file

@ -275,7 +275,7 @@ CField_repr(CFieldObject *self)
name = ((PyTypeObject *)self->proto)->tp_name; name = ((PyTypeObject *)self->proto)->tp_name;
if (bits) if (bits)
result = PyString_FromFormat( result = PyUnicode_FromFormat(
#if (PY_VERSION_HEX < 0x02050000) #if (PY_VERSION_HEX < 0x02050000)
"<Field type=%s, ofs=%d:%d, bits=%d>", "<Field type=%s, ofs=%d:%d, bits=%d>",
#else #else
@ -283,7 +283,7 @@ CField_repr(CFieldObject *self)
#endif #endif
name, self->offset, size, bits); name, self->offset, size, bits);
else else
result = PyString_FromFormat( result = PyUnicode_FromFormat(
#if (PY_VERSION_HEX < 0x02050000) #if (PY_VERSION_HEX < 0x02050000)
"<Field type=%s, ofs=%d, size=%d>", "<Field type=%s, ofs=%d, size=%d>",
#else #else

View file

@ -1121,12 +1121,12 @@ element_repr(ElementObject* self)
PyObject* repr; PyObject* repr;
char buffer[100]; char buffer[100];
repr = PyString_FromString("<Element "); repr = PyUnicode_FromString("<Element ");
PyString_ConcatAndDel(&repr, PyObject_Repr(self->tag)); PyUnicode_AppendAndDel(&repr, PyObject_Repr(self->tag));
sprintf(buffer, " at %p>", self); sprintf(buffer, " at %p>", self);
PyString_ConcatAndDel(&repr, PyString_FromString(buffer)); PyUnicode_AppendAndDel(&repr, PyUnicode_FromString(buffer));
return repr; return repr;
} }

View file

@ -580,9 +580,9 @@ static PyObject *
fileio_repr(PyFileIOObject *self) fileio_repr(PyFileIOObject *self)
{ {
if (self->fd < 0) if (self->fd < 0)
return PyString_FromFormat("_fileio._FileIO(-1)"); return PyUnicode_FromFormat("_fileio._FileIO(-1)");
return PyString_FromFormat("_fileio._FileIO(%d, '%s')", return PyUnicode_FromFormat("_fileio._FileIO(%d, '%s')",
self->fd, mode_string(self)); self->fd, mode_string(self));
} }

View file

@ -221,7 +221,7 @@ EVP_repr(PyObject *self)
char buf[100]; char buf[100];
PyOS_snprintf(buf, sizeof(buf), "<%s HASH object @ %p>", PyOS_snprintf(buf, sizeof(buf), "<%s HASH object @ %p>",
PyString_AsString(((EVPobject *)self)->name), self); PyString_AsString(((EVPobject *)self)->name), self);
return PyString_FromString(buf); return PyUnicode_FromString(buf);
} }
#if HASH_OBJ_CONSTRUCTOR #if HASH_OBJ_CONSTRUCTOR

View file

@ -812,7 +812,7 @@ PyTclObject_repr(PyTclObject *self)
char buf[50]; char buf[50];
PyOS_snprintf(buf, 50, "<%s object at %p>", PyOS_snprintf(buf, 50, "<%s object at %p>",
self->value->typePtr->name, self->value); self->value->typePtr->name, self->value);
return PyString_FromString(buf); return PyUnicode_FromString(buf);
} }
static int static int
@ -2375,7 +2375,7 @@ Tktt_Repr(PyObject *self)
PyOS_snprintf(buf, sizeof(buf), "<tktimertoken at %p%s>", v, PyOS_snprintf(buf, sizeof(buf), "<tktimertoken at %p%s>", v,
v->func == NULL ? ", handler deleted" : ""); v->func == NULL ? ", handler deleted" : "");
return PyString_FromString(buf); return PyUnicode_FromString(buf);
} }
static PyObject * static PyObject *

View file

@ -1574,7 +1574,7 @@ array_repr(arrayobject *a)
typecode = a->ob_descr->typecode; typecode = a->ob_descr->typecode;
if (len == 0) { if (len == 0) {
PyOS_snprintf(buf, sizeof(buf), "array('%c')", typecode); PyOS_snprintf(buf, sizeof(buf), "array('%c')", typecode);
return PyString_FromString(buf); return PyUnicode_FromString(buf);
} }
if (typecode == 'c') if (typecode == 'c')
@ -1587,9 +1587,9 @@ array_repr(arrayobject *a)
Py_XDECREF(v); Py_XDECREF(v);
PyOS_snprintf(buf, sizeof(buf), "array('%c', ", typecode); PyOS_snprintf(buf, sizeof(buf), "array('%c', ", typecode);
s = PyString_FromString(buf); s = PyUnicode_FromString(buf);
PyString_ConcatAndDel(&s, t); PyUnicode_AppendAndDel(&s, t);
PyString_ConcatAndDel(&s, PyString_FromString(")")); PyUnicode_AppendAndDel(&s, PyUnicode_FromString(")"));
return s; return s;
} }

View file

@ -1122,7 +1122,7 @@ save_long(Picklerobject *self, PyObject *args)
/* proto < 2: write the repr and newline. This is quadratic-time /* proto < 2: write the repr and newline. This is quadratic-time
* (in the number of digits), in both directions. * (in the number of digits), in both directions.
*/ */
if (!( repr = PyObject_Repr(args))) if (!( repr = PyObject_ReprStr8(args)))
goto finally; goto finally;
if ((size = PyString_Size(repr)) < 0) if ((size = PyString_Size(repr)) < 0)
@ -1189,7 +1189,7 @@ save_string(Picklerobject *self, PyObject *args, int doput)
static char string = STRING; static char string = STRING;
if (!( repr = PyObject_Repr(args))) if (!( repr = PyObject_ReprStr8(args)))
return -1; return -1;
if ((len = PyString_Size(repr)) < 0) if ((len = PyString_Size(repr)) < 0)

View file

@ -1044,27 +1044,27 @@ append_keyword_tzinfo(PyObject *repr, PyObject *tzinfo)
{ {
PyObject *temp; PyObject *temp;
assert(PyString_Check(repr)); assert(PyUnicode_Check(repr));
assert(tzinfo); assert(tzinfo);
if (tzinfo == Py_None) if (tzinfo == Py_None)
return repr; return repr;
/* Get rid of the trailing ')'. */ /* Get rid of the trailing ')'. */
assert(PyString_AsString(repr)[PyString_Size(repr)-1] == ')'); assert(PyUnicode_AS_UNICODE(repr)[PyUnicode_GET_SIZE(repr)-1] == ')');
temp = PyString_FromStringAndSize(PyString_AsString(repr), temp = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(repr),
PyString_Size(repr) - 1); PyUnicode_GET_SIZE(repr) - 1);
Py_DECREF(repr); Py_DECREF(repr);
if (temp == NULL) if (temp == NULL)
return NULL; return NULL;
repr = temp; repr = temp;
/* Append ", tzinfo=". */ /* Append ", tzinfo=". */
PyString_ConcatAndDel(&repr, PyString_FromString(", tzinfo=")); PyUnicode_AppendAndDel(&repr, PyUnicode_FromString(", tzinfo="));
/* Append repr(tzinfo). */ /* Append repr(tzinfo). */
PyString_ConcatAndDel(&repr, PyObject_Repr(tzinfo)); PyUnicode_AppendAndDel(&repr, PyObject_Repr(tzinfo));
/* Add a closing paren. */ /* Add a closing paren. */
PyString_ConcatAndDel(&repr, PyString_FromString(")")); PyUnicode_AppendAndDel(&repr, PyUnicode_FromString(")"));
return repr; return repr;
} }
@ -1972,18 +1972,18 @@ static PyObject *
delta_repr(PyDateTime_Delta *self) delta_repr(PyDateTime_Delta *self)
{ {
if (GET_TD_MICROSECONDS(self) != 0) if (GET_TD_MICROSECONDS(self) != 0)
return PyString_FromFormat("%s(%d, %d, %d)", return PyUnicode_FromFormat("%s(%d, %d, %d)",
self->ob_type->tp_name, self->ob_type->tp_name,
GET_TD_DAYS(self), GET_TD_DAYS(self),
GET_TD_SECONDS(self), GET_TD_SECONDS(self),
GET_TD_MICROSECONDS(self)); GET_TD_MICROSECONDS(self));
if (GET_TD_SECONDS(self) != 0) if (GET_TD_SECONDS(self) != 0)
return PyString_FromFormat("%s(%d, %d)", return PyUnicode_FromFormat("%s(%d, %d)",
self->ob_type->tp_name, self->ob_type->tp_name,
GET_TD_DAYS(self), GET_TD_DAYS(self),
GET_TD_SECONDS(self)); GET_TD_SECONDS(self));
return PyString_FromFormat("%s(%d)", return PyUnicode_FromFormat("%s(%d)",
self->ob_type->tp_name, self->ob_type->tp_name,
GET_TD_DAYS(self)); GET_TD_DAYS(self));
} }
@ -2410,7 +2410,7 @@ date_repr(PyDateTime_Date *self)
type_name, type_name,
GET_YEAR(self), GET_MONTH(self), GET_DAY(self)); GET_YEAR(self), GET_MONTH(self), GET_DAY(self));
return PyString_FromString(buffer); return PyUnicode_FromString(buffer);
} }
static PyObject * static PyObject *
@ -3131,7 +3131,7 @@ time_repr(PyDateTime_Time *self)
else else
PyOS_snprintf(buffer, sizeof(buffer), PyOS_snprintf(buffer, sizeof(buffer),
"%s(%d, %d)", type_name, h, m); "%s(%d, %d)", type_name, h, m);
result = PyString_FromString(buffer); result = PyUnicode_FromString(buffer);
if (result != NULL && HASTZINFO(self)) if (result != NULL && HASTZINFO(self))
result = append_keyword_tzinfo(result, self->tzinfo); result = append_keyword_tzinfo(result, self->tzinfo);
return result; return result;
@ -4043,7 +4043,7 @@ datetime_repr(PyDateTime_DateTime *self)
GET_YEAR(self), GET_MONTH(self), GET_DAY(self), GET_YEAR(self), GET_MONTH(self), GET_DAY(self),
DATE_GET_HOUR(self), DATE_GET_MINUTE(self)); DATE_GET_HOUR(self), DATE_GET_MINUTE(self));
} }
baserepr = PyString_FromString(buffer); baserepr = PyUnicode_FromString(buffer);
if (baserepr == NULL || ! HASTZINFO(self)) if (baserepr == NULL || ! HASTZINFO(self))
return baserepr; return baserepr;
return append_keyword_tzinfo(baserepr, self->tzinfo); return append_keyword_tzinfo(baserepr, self->tzinfo);

View file

@ -2084,7 +2084,7 @@ count_next(countobject *lz)
static PyObject * static PyObject *
count_repr(countobject *lz) count_repr(countobject *lz)
{ {
return PyString_FromFormat("count(%zd)", lz->cnt); return PyUnicode_FromFormat("count(%zd)", lz->cnt);
} }
PyDoc_STRVAR(count_doc, PyDoc_STRVAR(count_doc,
@ -2396,11 +2396,11 @@ repeat_repr(repeatobject *ro)
return NULL; return NULL;
if (ro->cnt == -1) if (ro->cnt == -1)
result = PyString_FromFormat("repeat(%s)", result = PyUnicode_FromFormat("repeat(%U)",
PyString_AS_STRING(objrepr)); objrepr);
else else
result = PyString_FromFormat("repeat(%s, %zd)", result = PyUnicode_FromFormat("repeat(%U, %zd)",
PyString_AS_STRING(objrepr), ro->cnt); objrepr, ro->cnt);
Py_DECREF(objrepr); Py_DECREF(objrepr);
return result; return result;
} }

View file

@ -2865,7 +2865,7 @@ sock_repr(PySocketSockObject *s)
(long)s->sock_fd, s->sock_family, (long)s->sock_fd, s->sock_family,
s->sock_type, s->sock_type,
s->sock_proto); s->sock_proto);
return PyString_FromString(buf); return PyUnicode_FromString(buf);
} }

View file

@ -203,7 +203,7 @@ zipimporter_repr(ZipImporter *self)
PyOS_snprintf(buf, sizeof(buf), PyOS_snprintf(buf, sizeof(buf),
"<zipimporter object \"%.300s\">", "<zipimporter object \"%.300s\">",
archive); archive);
return PyString_FromString(buf); return PyUnicode_FromString(buf);
} }
/* return fullname.split(".")[-1] */ /* return fullname.split(".")[-1] */

View file

@ -24,10 +24,10 @@ bool_repr(PyObject *self)
if (self == Py_True) if (self == Py_True)
s = true_str ? true_str : s = true_str ? true_str :
(true_str = PyString_InternFromString("True")); (true_str = PyUnicode_FromString("True"));
else else
s = false_str ? false_str : s = false_str ? false_str :
(false_str = PyString_InternFromString("False")); (false_str = PyUnicode_FromString("False"));
Py_XINCREF(s); Py_XINCREF(s);
return s; return s;
} }

View file

@ -319,13 +319,13 @@ buffer_repr(PyBufferObject *self)
const char *status = self->b_readonly ? "read-only" : "read-write"; const char *status = self->b_readonly ? "read-only" : "read-write";
if ( self->b_base == NULL ) if ( self->b_base == NULL )
return PyString_FromFormat("<%s buffer ptr %p, size %zd at %p>", return PyUnicode_FromFormat("<%s buffer ptr %p, size %zd at %p>",
status, status,
self->b_ptr, self->b_ptr,
self->b_size, self->b_size,
self); self);
else else
return PyString_FromFormat( return PyUnicode_FromFormat(
"<%s buffer for %p, size %zd, offset %zd at %p>", "<%s buffer for %p, size %zd, offset %zd at %p>",
status, status,
self->b_base, self->b_base,

View file

@ -815,6 +815,7 @@ bytes_init(PyBytesObject *self, PyObject *args, PyObject *kwds)
static PyObject * static PyObject *
bytes_repr(PyBytesObject *self) bytes_repr(PyBytesObject *self)
{ {
static const char *hexdigits = "0123456789abcdef";
size_t newsize = 3 + 4 * self->ob_size; size_t newsize = 3 + 4 * self->ob_size;
PyObject *v; PyObject *v;
if (newsize > PY_SSIZE_T_MAX || newsize / 4 != self->ob_size) { if (newsize > PY_SSIZE_T_MAX || newsize / 4 != self->ob_size) {
@ -822,23 +823,23 @@ bytes_repr(PyBytesObject *self)
"bytes object is too large to make repr"); "bytes object is too large to make repr");
return NULL; return NULL;
} }
v = PyString_FromStringAndSize((char *)NULL, newsize); v = PyUnicode_FromUnicode(NULL, newsize);
if (v == NULL) { if (v == NULL) {
return NULL; return NULL;
} }
else { else {
register Py_ssize_t i; register Py_ssize_t i;
register char c; register Py_UNICODE c;
register char *p; register Py_UNICODE *p;
int quote = '\''; int quote = '\'';
p = PyString_AS_STRING(v); p = PyUnicode_AS_UNICODE(v);
*p++ = 'b'; *p++ = 'b';
*p++ = quote; *p++ = quote;
for (i = 0; i < self->ob_size; i++) { for (i = 0; i < self->ob_size; i++) {
/* There's at least enough room for a hex escape /* There's at least enough room for a hex escape
and a closing quote. */ and a closing quote. */
assert(newsize - (p - PyString_AS_STRING(v)) >= 5); assert(newsize - (p - PyUnicode_AS_UNICODE(v)) >= 5);
c = self->ob_bytes[i]; c = self->ob_bytes[i];
if (c == quote || c == '\\') if (c == quote || c == '\\')
*p++ = '\\', *p++ = c; *p++ = '\\', *p++ = c;
@ -851,20 +852,21 @@ bytes_repr(PyBytesObject *self)
else if (c == 0) else if (c == 0)
*p++ = '\\', *p++ = 'x', *p++ = '0', *p++ = '0'; *p++ = '\\', *p++ = 'x', *p++ = '0', *p++ = '0';
else if (c < ' ' || c >= 0x7f) { else if (c < ' ' || c >= 0x7f) {
/* For performance, we don't want to call *p++ = '\\';
PyOS_snprintf here (extra layers of *p++ = 'x';
function call). */ *p++ = hexdigits[(c & 0xf0) >> 4];
sprintf(p, "\\x%02x", c & 0xff); *p++ = hexdigits[c & 0xf];
p += 4;
} }
else else
*p++ = c; *p++ = c;
} }
assert(newsize - (p - PyString_AS_STRING(v)) >= 1); assert(newsize - (p - PyUnicode_AS_UNICODE(v)) >= 1);
*p++ = quote; *p++ = quote;
*p = '\0'; *p = '\0';
_PyString_Resize( if (PyUnicode_Resize(&v, (p - PyUnicode_AS_UNICODE(v)))) {
&v, (p - PyString_AS_STRING(v))); Py_DECREF(v);
return NULL;
}
return v; return v;
} }
} }

View file

@ -53,9 +53,9 @@ static PyObject *
cell_repr(PyCellObject *op) cell_repr(PyCellObject *op)
{ {
if (op->ob_ref == NULL) if (op->ob_ref == NULL)
return PyString_FromFormat("<cell at %p: empty>", op); return PyUnicode_FromFormat("<cell at %p: empty>", op);
return PyString_FromFormat("<cell at %p: %.80s object at %p>", return PyUnicode_FromFormat("<cell at %p: %.80s object at %p>",
op, op->ob_ref->ob_type->tp_name, op, op->ob_ref->ob_type->tp_name,
op->ob_ref); op->ob_ref);
} }

View file

@ -258,23 +258,15 @@ method_repr(PyMethodObject *a)
sklassname = PyString_AS_STRING(klassname); sklassname = PyString_AS_STRING(klassname);
} }
if (self == NULL) if (self == NULL)
result = PyString_FromFormat("<unbound method %s.%s>", result = PyUnicode_FromFormat("<unbound method %s.%s>",
sklassname, sfuncname); sklassname, sfuncname);
else { else {
result = PyUnicode_FromFormat("<bound method %s.%s of ",
sklassname, sfuncname);
/* XXX Shouldn't use repr() here! */ /* XXX Shouldn't use repr() here! */
PyObject *selfrepr = PyObject_Repr(self); PyUnicode_AppendAndDel(&result, PyObject_Repr(self));
if (selfrepr == NULL) PyUnicode_AppendAndDel(&result, PyUnicode_FromString(">"));
goto fail;
if (!PyString_Check(selfrepr)) {
Py_DECREF(selfrepr);
goto fail;
}
result = PyString_FromFormat("<bound method %s.%s of %s>",
sklassname, sfuncname,
PyString_AS_STRING(selfrepr));
Py_DECREF(selfrepr);
} }
fail:
Py_XDECREF(funcname); Py_XDECREF(funcname);
Py_XDECREF(klassname); Py_XDECREF(klassname);
return result; return result;

View file

@ -300,7 +300,7 @@ code_repr(PyCodeObject *co)
PyOS_snprintf(buf, sizeof(buf), PyOS_snprintf(buf, sizeof(buf),
"<code object %.100s at %p, file \"%.300s\", line %d>", "<code object %.100s at %p, file \"%.300s\", line %d>",
name, co, filename, lineno); name, co, filename, lineno);
return PyString_FromString(buf); return PyUnicode_FromString(buf);
} }
static PyObject * static PyObject *

View file

@ -342,7 +342,7 @@ complex_repr(PyComplexObject *v)
{ {
char buf[100]; char buf[100];
complex_to_buf(buf, sizeof(buf), v, PREC_REPR); complex_to_buf(buf, sizeof(buf), v, PREC_REPR);
return PyString_FromString(buf); return PyUnicode_FromString(buf);
} }
static PyObject * static PyObject *

View file

@ -24,7 +24,7 @@ descr_name(PyDescrObject *descr)
static PyObject * static PyObject *
descr_repr(PyDescrObject *descr, char *format) descr_repr(PyDescrObject *descr, char *format)
{ {
return PyString_FromFormat(format, descr_name(descr), return PyUnicode_FromFormat(format, descr_name(descr),
descr->d_type->tp_name); descr->d_type->tp_name);
} }
@ -917,7 +917,7 @@ wrapper_hash(wrapperobject *wp)
static PyObject * static PyObject *
wrapper_repr(wrapperobject *wp) wrapper_repr(wrapperobject *wp)
{ {
return PyString_FromFormat("<method-wrapper '%s' of %s object at %p>", return PyUnicode_FromFormat("<method-wrapper '%s' of %s object at %p>",
wp->descr->d_base->name, wp->descr->d_base->name,
wp->self->ob_type->tp_name, wp->self->ob_type->tp_name,
wp->self); wp->self);

View file

@ -941,11 +941,11 @@ dict_repr(dictobject *mp)
i = Py_ReprEnter((PyObject *)mp); i = Py_ReprEnter((PyObject *)mp);
if (i != 0) { if (i != 0) {
return i > 0 ? PyString_FromString("{...}") : NULL; return i > 0 ? PyUnicode_FromString("{...}") : NULL;
} }
if (mp->ma_used == 0) { if (mp->ma_used == 0) {
result = PyString_FromString("{}"); result = PyUnicode_FromString("{}");
goto Done; goto Done;
} }
@ -953,7 +953,7 @@ dict_repr(dictobject *mp)
if (pieces == NULL) if (pieces == NULL)
goto Done; goto Done;
colon = PyString_FromString(": "); colon = PyUnicode_FromString(": ");
if (colon == NULL) if (colon == NULL)
goto Done; goto Done;
@ -965,8 +965,8 @@ dict_repr(dictobject *mp)
/* Prevent repr from deleting value during key format. */ /* Prevent repr from deleting value during key format. */
Py_INCREF(value); Py_INCREF(value);
s = PyObject_Repr(key); s = PyObject_Repr(key);
PyString_Concat(&s, colon); PyUnicode_Append(&s, colon);
PyString_ConcatAndDel(&s, PyObject_Repr(value)); PyUnicode_AppendAndDel(&s, PyObject_Repr(value));
Py_DECREF(value); Py_DECREF(value);
if (s == NULL) if (s == NULL)
goto Done; goto Done;
@ -978,29 +978,29 @@ dict_repr(dictobject *mp)
/* Add "{}" decorations to the first and last items. */ /* Add "{}" decorations to the first and last items. */
assert(PyList_GET_SIZE(pieces) > 0); assert(PyList_GET_SIZE(pieces) > 0);
s = PyString_FromString("{"); s = PyUnicode_FromString("{");
if (s == NULL) if (s == NULL)
goto Done; goto Done;
temp = PyList_GET_ITEM(pieces, 0); temp = PyList_GET_ITEM(pieces, 0);
PyString_ConcatAndDel(&s, temp); PyUnicode_AppendAndDel(&s, temp);
PyList_SET_ITEM(pieces, 0, s); PyList_SET_ITEM(pieces, 0, s);
if (s == NULL) if (s == NULL)
goto Done; goto Done;
s = PyString_FromString("}"); s = PyUnicode_FromString("}");
if (s == NULL) if (s == NULL)
goto Done; goto Done;
temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1); temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1);
PyString_ConcatAndDel(&temp, s); PyUnicode_AppendAndDel(&temp, s);
PyList_SET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1, temp); PyList_SET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1, temp);
if (temp == NULL) if (temp == NULL)
goto Done; goto Done;
/* Paste them all together with ", " between. */ /* Paste them all together with ", " between. */
s = PyString_FromString(", "); s = PyUnicode_FromString(", ");
if (s == NULL) if (s == NULL)
goto Done; goto Done;
result = _PyString_Join(s, pieces); result = PyUnicode_Join(s, pieces);
Py_DECREF(s); Py_DECREF(s);
Done: Done:

View file

@ -111,13 +111,13 @@ BaseException_repr(PyBaseExceptionObject *self)
dot = strrchr(name, '.'); dot = strrchr(name, '.');
if (dot != NULL) name = dot+1; if (dot != NULL) name = dot+1;
repr = PyString_FromString(name); repr = PyUnicode_FromString(name);
if (!repr) { if (!repr) {
Py_DECREF(repr_suffix); Py_DECREF(repr_suffix);
return NULL; return NULL;
} }
PyString_ConcatAndDel(&repr, repr_suffix); PyUnicode_AppendAndDel(&repr, repr_suffix);
return repr; return repr;
} }
@ -529,7 +529,7 @@ EnvironmentError_str(PyEnvironmentErrorObject *self)
if (!fmt) if (!fmt)
return NULL; return NULL;
repr = PyObject_Repr(self->filename); repr = PyObject_ReprStr8(self->filename);
if (!repr) { if (!repr) {
Py_DECREF(fmt); Py_DECREF(fmt);
return NULL; return NULL;
@ -760,7 +760,7 @@ WindowsError_str(PyWindowsErrorObject *self)
if (!fmt) if (!fmt)
return NULL; return NULL;
repr = PyObject_Repr(self->filename); repr = PyObject_ReprStr8(self->filename);
if (!repr) { if (!repr) {
Py_DECREF(fmt); Py_DECREF(fmt);
return NULL; return NULL;
@ -1134,7 +1134,7 @@ KeyError_str(PyBaseExceptionObject *self)
If args is anything else, use the default BaseException__str__(). If args is anything else, use the default BaseException__str__().
*/ */
if (PyTuple_GET_SIZE(self->args) == 1) { if (PyTuple_GET_SIZE(self->args) == 1) {
return PyObject_Repr(PyTuple_GET_ITEM(self->args, 0)); return PyObject_ReprStr8(PyTuple_GET_ITEM(self->args, 0));
} }
return BaseException_str(self); return BaseException_str(self);
} }

View file

@ -406,7 +406,7 @@ file_repr(PyFileObject *f)
PyObject *ret = NULL; PyObject *ret = NULL;
PyObject *name = PyUnicode_AsUnicodeEscapeString(f->f_name); PyObject *name = PyUnicode_AsUnicodeEscapeString(f->f_name);
const char *name_str = name ? PyString_AsString(name) : "?"; const char *name_str = name ? PyString_AsString(name) : "?";
ret = PyString_FromFormat("<%s file u'%s', mode '%s' at %p>", ret = PyUnicode_FromFormat("<%s file u'%s', mode '%s' at %p>",
f->f_fp == NULL ? "closed" : "open", f->f_fp == NULL ? "closed" : "open",
name_str, name_str,
PyString_AsString(f->f_mode), PyString_AsString(f->f_mode),
@ -414,7 +414,7 @@ file_repr(PyFileObject *f)
Py_XDECREF(name); Py_XDECREF(name);
return ret; return ret;
} else { } else {
return PyString_FromFormat("<%s file '%s', mode '%s' at %p>", return PyUnicode_FromFormat("<%s file '%s', mode '%s' at %p>",
f->f_fp == NULL ? "closed" : "open", f->f_fp == NULL ? "closed" : "open",
PyString_AsString(f->f_name), PyString_AsString(f->f_name),
PyString_AsString(f->f_mode), PyString_AsString(f->f_mode),
@ -2142,7 +2142,7 @@ PyFile_WriteObject(PyObject *v, PyObject *f, int flags)
value = PyObject_Str(v); value = PyObject_Str(v);
} }
else else
value = PyObject_Repr(v); value = PyObject_ReprStr8(v);
if (value == NULL) { if (value == NULL) {
Py_DECREF(writer); Py_DECREF(writer);
return -1; return -1;

View file

@ -302,7 +302,7 @@ float_repr(PyFloatObject *v)
{ {
char buf[100]; char buf[100];
format_float(buf, sizeof(buf), v, PREC_REPR); format_float(buf, sizeof(buf), v, PREC_REPR);
return PyString_FromString(buf); return PyUnicode_FromString(buf);
} }
static PyObject * static PyObject *

View file

@ -574,7 +574,7 @@ func_dealloc(PyFunctionObject *op)
static PyObject* static PyObject*
func_repr(PyFunctionObject *op) func_repr(PyFunctionObject *op)
{ {
return PyString_FromFormat("<function %s at %p>", return PyUnicode_FromFormat("<function %s at %p>",
PyString_AsString(op->func_name), PyString_AsString(op->func_name),
op); op);
} }

View file

@ -369,7 +369,7 @@ PyInt_FromString(char *s, char **pend, int base)
sobj = PyString_FromStringAndSize(s, slen); sobj = PyString_FromStringAndSize(s, slen);
if (sobj == NULL) if (sobj == NULL)
return NULL; return NULL;
srepr = PyObject_Repr(sobj); srepr = PyObject_ReprStr8(sobj);
Py_DECREF(sobj); Py_DECREF(sobj);
if (srepr == NULL) if (srepr == NULL)
return NULL; return NULL;
@ -433,7 +433,7 @@ int_repr(PyIntObject *v)
{ {
char buf[64]; char buf[64];
PyOS_snprintf(buf, sizeof(buf), "%ld", v->ob_ival); PyOS_snprintf(buf, sizeof(buf), "%ld", v->ob_ival);
return PyString_FromString(buf); return PyUnicode_FromString(buf);
} }
static int static int
@ -972,7 +972,7 @@ int_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
/* create a repr() of the input string, /* create a repr() of the input string,
* just like PyInt_FromString does */ * just like PyInt_FromString does */
PyObject *srepr; PyObject *srepr;
srepr = PyObject_Repr(x); srepr = PyObject_ReprStr8(x);
if (srepr == NULL) if (srepr == NULL)
return NULL; return NULL;
PyErr_Format(PyExc_ValueError, PyErr_Format(PyExc_ValueError,

View file

@ -308,11 +308,11 @@ list_repr(PyListObject *v)
i = Py_ReprEnter((PyObject*)v); i = Py_ReprEnter((PyObject*)v);
if (i != 0) { if (i != 0) {
return i > 0 ? PyString_FromString("[...]") : NULL; return i > 0 ? PyUnicode_FromString("[...]") : NULL;
} }
if (v->ob_size == 0) { if (v->ob_size == 0) {
result = PyString_FromString("[]"); result = PyUnicode_FromString("[]");
goto Done; goto Done;
} }
@ -335,29 +335,29 @@ list_repr(PyListObject *v)
/* Add "[]" decorations to the first and last items. */ /* Add "[]" decorations to the first and last items. */
assert(PyList_GET_SIZE(pieces) > 0); assert(PyList_GET_SIZE(pieces) > 0);
s = PyString_FromString("["); s = PyUnicode_FromString("[");
if (s == NULL) if (s == NULL)
goto Done; goto Done;
temp = PyList_GET_ITEM(pieces, 0); temp = PyList_GET_ITEM(pieces, 0);
PyString_ConcatAndDel(&s, temp); PyUnicode_AppendAndDel(&s, temp);
PyList_SET_ITEM(pieces, 0, s); PyList_SET_ITEM(pieces, 0, s);
if (s == NULL) if (s == NULL)
goto Done; goto Done;
s = PyString_FromString("]"); s = PyUnicode_FromString("]");
if (s == NULL) if (s == NULL)
goto Done; goto Done;
temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1); temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1);
PyString_ConcatAndDel(&temp, s); PyUnicode_AppendAndDel(&temp, s);
PyList_SET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1, temp); PyList_SET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1, temp);
if (temp == NULL) if (temp == NULL)
goto Done; goto Done;
/* Paste them all together with ", " between. */ /* Paste them all together with ", " between. */
s = PyString_FromString(", "); s = PyUnicode_FromString(", ");
if (s == NULL) if (s == NULL)
goto Done; goto Done;
result = _PyString_Join(s, pieces); result = PyUnicode_Join(s, pieces);
Py_DECREF(s); Py_DECREF(s);
Done: Done:

View file

@ -1430,10 +1430,10 @@ static PyObject *
long_format(PyObject *aa, int base) long_format(PyObject *aa, int base)
{ {
register PyLongObject *a = (PyLongObject *)aa; register PyLongObject *a = (PyLongObject *)aa;
PyStringObject *str; PyObject *str;
Py_ssize_t i, j, sz; Py_ssize_t i, j, sz;
Py_ssize_t size_a; Py_ssize_t size_a;
char *p; Py_UNICODE *p;
int bits; int bits;
char sign = '\0'; char sign = '\0';
@ -1459,10 +1459,10 @@ long_format(PyObject *aa, int base)
"int is too large to format"); "int is too large to format");
return NULL; return NULL;
} }
str = (PyStringObject *) PyString_FromStringAndSize((char *)0, sz); str = PyUnicode_FromUnicode(NULL, sz);
if (str == NULL) if (str == NULL)
return NULL; return NULL;
p = PyString_AS_STRING(str) + sz; p = PyUnicode_AS_UNICODE(str) + sz;
*p = '\0'; *p = '\0';
if (a->ob_size < 0) if (a->ob_size < 0)
sign = '-'; sign = '-';
@ -1486,7 +1486,7 @@ long_format(PyObject *aa, int base)
do { do {
char cdigit = (char)(accum & (base - 1)); char cdigit = (char)(accum & (base - 1));
cdigit += (cdigit < 10) ? '0' : 'a'-10; cdigit += (cdigit < 10) ? '0' : 'a'-10;
assert(p > PyString_AS_STRING(str)); assert(p > PyUnicode_AS_UNICODE(str));
*--p = cdigit; *--p = cdigit;
accumbits -= basebits; accumbits -= basebits;
accum >>= basebits; accum >>= basebits;
@ -1538,7 +1538,7 @@ long_format(PyObject *aa, int base)
do { do {
digit nextrem = (digit)(rem / base); digit nextrem = (digit)(rem / base);
char c = (char)(rem - nextrem * base); char c = (char)(rem - nextrem * base);
assert(p > PyString_AS_STRING(str)); assert(p > PyUnicode_AS_UNICODE(str));
c += (c < 10) ? '0' : 'a'-10; c += (c < 10) ? '0' : 'a'-10;
*--p = c; *--p = c;
rem = nextrem; rem = nextrem;
@ -1567,14 +1567,16 @@ long_format(PyObject *aa, int base)
} }
if (sign) if (sign)
*--p = sign; *--p = sign;
if (p != PyString_AS_STRING(str)) { if (p != PyUnicode_AS_UNICODE(str)) {
char *q = PyString_AS_STRING(str); Py_UNICODE *q = PyUnicode_AS_UNICODE(str);
assert(p > q); assert(p > q);
do { do {
} while ((*q++ = *p++) != '\0'); } while ((*q++ = *p++) != '\0');
q--; q--;
_PyString_Resize((PyObject **)&str, if (PyUnicode_Resize(&str, (Py_ssize_t) (q - PyUnicode_AS_UNICODE(str)))) {
(Py_ssize_t) (q - PyString_AS_STRING(str))); Py_DECREF(str);
return NULL;
}
} }
return (PyObject *)str; return (PyObject *)str;
} }
@ -1928,7 +1930,7 @@ digit beyond the first.
strobj = PyString_FromStringAndSize(orig_str, slen); strobj = PyString_FromStringAndSize(orig_str, slen);
if (strobj == NULL) if (strobj == NULL)
return NULL; return NULL;
strrepr = PyObject_Repr(strobj); strrepr = PyObject_ReprStr8(strobj);
Py_DECREF(strobj); Py_DECREF(strobj);
if (strrepr == NULL) if (strrepr == NULL)
return NULL; return NULL;
@ -3525,7 +3527,7 @@ long_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
/* create a repr() of the input string, /* create a repr() of the input string,
* just like PyLong_FromString does. */ * just like PyLong_FromString does. */
PyObject *srepr; PyObject *srepr;
srepr = PyObject_Repr(x); srepr = PyObject_ReprStr8(x);
if (srepr == NULL) if (srepr == NULL)
return NULL; return NULL;
PyErr_Format(PyExc_ValueError, PyErr_Format(PyExc_ValueError,

View file

@ -184,9 +184,9 @@ static PyObject *
meth_repr(PyCFunctionObject *m) meth_repr(PyCFunctionObject *m)
{ {
if (m->m_self == NULL) if (m->m_self == NULL)
return PyString_FromFormat("<built-in function %s>", return PyUnicode_FromFormat("<built-in function %s>",
m->m_ml->ml_name); m->m_ml->ml_name);
return PyString_FromFormat("<built-in method %s of %s object at %p>", return PyUnicode_FromFormat("<built-in method %s of %s object at %p>",
m->m_ml->ml_name, m->m_ml->ml_name,
m->m_self->ob_type->tp_name, m->m_self->ob_type->tp_name,
m->m_self); m->m_self);

View file

@ -198,9 +198,9 @@ module_repr(PyModuleObject *m)
filename = PyModule_GetFilename((PyObject *)m); filename = PyModule_GetFilename((PyObject *)m);
if (filename == NULL) { if (filename == NULL) {
PyErr_Clear(); PyErr_Clear();
return PyString_FromFormat("<module '%s' (built-in)>", name); return PyUnicode_FromFormat("<module '%s' (built-in)>", name);
} }
return PyString_FromFormat("<module '%s' from '%s'>", name, filename); return PyUnicode_FromFormat("<module '%s' from '%s'>", name, filename);
} }
/* We only need a traverse function, no clear function: If the module /* We only need a traverse function, no clear function: If the module

View file

@ -284,7 +284,7 @@ internal_print(PyObject *op, FILE *fp, int flags, int nesting)
if (flags & Py_PRINT_RAW) if (flags & Py_PRINT_RAW)
s = PyObject_Str(op); s = PyObject_Str(op);
else else
s = PyObject_Repr(op); s = PyObject_ReprStr8(op);
if (s == NULL) if (s == NULL)
ret = -1; ret = -1;
else { else {
@ -343,6 +343,7 @@ _PyObject_Dump(PyObject* op)
PyObject * PyObject *
PyObject_Repr(PyObject *v) PyObject_Repr(PyObject *v)
{ {
PyObject *ress, *resu;
if (PyErr_CheckSignals()) if (PyErr_CheckSignals())
return NULL; return NULL;
#ifdef USE_STACKCHECK #ifdef USE_STACKCHECK
@ -352,28 +353,47 @@ PyObject_Repr(PyObject *v)
} }
#endif #endif
if (v == NULL) if (v == NULL)
return PyString_FromString("<NULL>"); return PyUnicode_FromString("<NULL>");
else if (v->ob_type->tp_repr == NULL) else if (v->ob_type->tp_repr == NULL)
return PyString_FromFormat("<%s object at %p>", return PyUnicode_FromFormat("<%s object at %p>", v->ob_type->tp_name, v);
v->ob_type->tp_name, v);
else { else {
PyObject *res; ress = (*v->ob_type->tp_repr)(v);
res = (*v->ob_type->tp_repr)(v); if (!ress)
if (res == NULL)
return NULL; return NULL;
if (PyUnicode_Check(res)) if (PyUnicode_Check(ress))
return res; return ress;
if (!PyString_Check(res)) { if (!PyString_Check(ress)) {
PyErr_Format(PyExc_TypeError, PyErr_Format(PyExc_TypeError,
"__repr__ returned non-string (type %.200s)", "__repr__ returned non-string (type %.200s)",
res->ob_type->tp_name); ress->ob_type->tp_name);
Py_DECREF(res); Py_DECREF(ress);
return NULL; return NULL;
} }
return res; resu = PyUnicode_FromObject(ress);
Py_DECREF(ress);
return resu;
} }
} }
PyObject *
PyObject_ReprStr8(PyObject *v)
{
PyObject *resu = PyObject_Repr(v);
if (resu) {
PyObject *resb = PyUnicode_AsEncodedString(resu, NULL, NULL);
Py_DECREF(resu);
if (resb) {
PyObject *ress = PyString_FromStringAndSize(
PyBytes_AS_STRING(resb),
PyBytes_GET_SIZE(resb)
);
Py_DECREF(resb);
return ress;
}
}
return NULL;
}
PyObject * PyObject *
_PyObject_Str(PyObject *v) _PyObject_Str(PyObject *v)
{ {
@ -1509,7 +1529,7 @@ so there is exactly one (which is indestructible, by the way).
static PyObject * static PyObject *
none_repr(PyObject *op) none_repr(PyObject *op)
{ {
return PyString_FromString("None"); return PyUnicode_FromString("None");
} }
/* ARGUSED */ /* ARGUSED */
@ -1551,7 +1571,7 @@ PyObject _Py_NoneStruct = {
static PyObject * static PyObject *
NotImplemented_repr(PyObject *op) NotImplemented_repr(PyObject *op)
{ {
return PyString_FromString("NotImplemented"); return PyUnicode_FromString("NotImplemented");
} }
static PyTypeObject PyNotImplemented_Type = { static PyTypeObject PyNotImplemented_Type = {

View file

@ -261,19 +261,19 @@ range_repr(rangeobject *r)
} }
if (istart == 0 && istep == 1) if (istart == 0 && istep == 1)
result = PyString_FromFormat("range(%s)", result = PyUnicode_FromFormat("range(%s)",
PyString_AS_STRING(stop_str)); PyString_AS_STRING(stop_str));
else if (istep == 1) { else if (istep == 1) {
if (start_str) if (start_str)
result = PyString_FromFormat("range(%s, %s)", result = PyUnicode_FromFormat("range(%s, %s)",
PyString_AS_STRING(start_str), PyString_AS_STRING(start_str),
PyString_AS_STRING(stop_str)); PyString_AS_STRING(stop_str));
} }
else if (start_str && step_str) else if (start_str && step_str)
result = PyString_FromFormat("range(%s, %s, %s)", result = PyUnicode_FromFormat("range(%s, %s, %s)",
PyString_AS_STRING(start_str), PyString_AS_STRING(start_str),
PyString_AS_STRING(stop_str), PyString_AS_STRING(stop_str),
PyString_AS_STRING(step_str)); PyString_AS_STRING(step_str));
/* else result is NULL and an error should already be set. */ /* else result is NULL and an error should already be set. */
Py_XDECREF(start_str); Py_XDECREF(start_str);

View file

@ -613,18 +613,21 @@ static PyObject *
set_repr(PySetObject *so) set_repr(PySetObject *so)
{ {
PyObject *keys, *result=NULL, *listrepr; PyObject *keys, *result=NULL, *listrepr;
int newsize;
Py_UNICODE *u;
const char *s;
int status = Py_ReprEnter((PyObject*)so); int status = Py_ReprEnter((PyObject*)so);
if (status != 0) { if (status != 0) {
if (status < 0) if (status < 0)
return NULL; return NULL;
return PyString_FromFormat("%s(...)", so->ob_type->tp_name); return PyUnicode_FromFormat("%s(...)", so->ob_type->tp_name);
} }
/* shortcut for the empty set */ /* shortcut for the empty set */
if (!so->used) { if (!so->used) {
Py_ReprLeave((PyObject*)so); Py_ReprLeave((PyObject*)so);
return PyString_FromFormat("%s()", so->ob_type->tp_name); return PyUnicode_FromFormat("%s()", so->ob_type->tp_name);
} }
keys = PySequence_List((PyObject *)so); keys = PySequence_List((PyObject *)so);
@ -635,14 +638,28 @@ set_repr(PySetObject *so)
if (listrepr == NULL) if (listrepr == NULL)
goto done; goto done;
if (so->ob_type == &PySet_Type) { newsize = PyUnicode_GET_SIZE(listrepr);
char *s = PyString_AS_STRING(listrepr); if (so->ob_type != &PySet_Type)
s += 1; newsize += strlen(so->ob_type->tp_name)+2;
s[strlen(s)-1] = 0; result = PyUnicode_FromUnicode(NULL, newsize);
result = PyString_FromFormat("{%s}", s); if (result) {
} else { u = PyUnicode_AS_UNICODE(result);
result = PyString_FromFormat("%s(%s)", so->ob_type->tp_name, if (so->ob_type != &PySet_Type) {
PyString_AS_STRING(listrepr)); for (s = so->ob_type->tp_name; *s;)
*u++ = *s++;
*u++ = '(';
Py_UNICODE_COPY(u, PyUnicode_AS_UNICODE(listrepr),
PyUnicode_GET_SIZE(listrepr));
u += PyUnicode_GET_SIZE(listrepr);
*u++ = ')';
} else {
*u++ = '{';
/* Omit the brackets from the listrepr */
Py_UNICODE_COPY(u, PyUnicode_AS_UNICODE(listrepr)+1,
PyUnicode_GET_SIZE(listrepr)-2);
u += PyUnicode_GET_SIZE(listrepr)-2;
*u++ = '}';
}
} }
Py_DECREF(listrepr); Py_DECREF(listrepr);
done: done:

View file

@ -19,7 +19,7 @@ this type and there is exactly one in existence.
static PyObject * static PyObject *
ellipsis_repr(PyObject *op) ellipsis_repr(PyObject *op)
{ {
return PyString_FromString("Ellipsis"); return PyUnicode_FromString("Ellipsis");
} }
static PyTypeObject PyEllipsis_Type = { static PyTypeObject PyEllipsis_Type = {
@ -228,14 +228,14 @@ slice_repr(PySliceObject *r)
{ {
PyObject *s, *comma; PyObject *s, *comma;
s = PyString_FromString("slice("); s = PyUnicode_FromString("slice(");
comma = PyString_FromString(", "); comma = PyUnicode_FromString(", ");
PyString_ConcatAndDel(&s, PyObject_Repr(r->start)); PyUnicode_AppendAndDel(&s, PyObject_Repr(r->start));
PyString_Concat(&s, comma); PyUnicode_Append(&s, comma);
PyString_ConcatAndDel(&s, PyObject_Repr(r->stop)); PyUnicode_AppendAndDel(&s, PyObject_Repr(r->stop));
PyString_Concat(&s, comma); PyUnicode_Append(&s, comma);
PyString_ConcatAndDel(&s, PyObject_Repr(r->step)); PyUnicode_AppendAndDel(&s, PyObject_Repr(r->step));
PyString_ConcatAndDel(&s, PyString_FromString(")")); PyUnicode_AppendAndDel(&s, PyUnicode_FromString(")"));
Py_DECREF(comma); Py_DECREF(comma);
return s; return s;
} }

View file

@ -822,36 +822,46 @@ string_print(PyStringObject *op, FILE *fp, int flags)
PyObject * PyObject *
PyString_Repr(PyObject *obj, int smartquotes) PyString_Repr(PyObject *obj, int smartquotes)
{ {
static const char *hexdigits = "0123456789abcdef";
register PyStringObject* op = (PyStringObject*) obj; register PyStringObject* op = (PyStringObject*) obj;
Py_ssize_t length = PyUnicode_GET_SIZE(op);
size_t newsize = 2 + 4 * op->ob_size; size_t newsize = 2 + 4 * op->ob_size;
PyObject *v; PyObject *v;
if (newsize > PY_SSIZE_T_MAX || newsize / 4 != op->ob_size) { if (newsize > PY_SSIZE_T_MAX || newsize / 4 != op->ob_size) {
PyErr_SetString(PyExc_OverflowError, PyErr_SetString(PyExc_OverflowError,
"string is too large to make repr"); "string is too large to make repr");
} }
v = PyString_FromStringAndSize((char *)NULL, newsize); v = PyUnicode_FromUnicode(NULL, newsize);
if (v == NULL) { if (v == NULL) {
return NULL; return NULL;
} }
else { else {
register Py_ssize_t i; register Py_ssize_t i;
register char c; register Py_UNICODE c;
register char *p; register Py_UNICODE *p = PyUnicode_AS_UNICODE(v);
int quote; int quote;
/* figure out which quote to use; single is preferred */ /* figure out which quote to use; single is preferred */
quote = '\''; quote = '\'';
if (smartquotes && if (smartquotes) {
memchr(op->ob_sval, '\'', op->ob_size) && Py_UNICODE *test;
!memchr(op->ob_sval, '"', op->ob_size)) for (test = p; test < p+length; ++test) {
quote = '"'; if (*test == '"') {
quote = '\''; /* switch back to single quote */
goto decided;
}
else if (*test == '\'')
quote = '"';
}
decided:
;
}
p = PyString_AS_STRING(v);
*p++ = quote; *p++ = quote;
for (i = 0; i < op->ob_size; i++) { for (i = 0; i < op->ob_size; i++) {
/* There's at least enough room for a hex escape /* There's at least enough room for a hex escape
and a closing quote. */ and a closing quote. */
assert(newsize - (p - PyString_AS_STRING(v)) >= 5); assert(newsize - (p - PyUnicode_AS_UNICODE(v)) >= 5);
c = op->ob_sval[i]; c = op->ob_sval[i];
if (c == quote || c == '\\') if (c == quote || c == '\\')
*p++ = '\\', *p++ = c; *p++ = '\\', *p++ = c;
@ -862,20 +872,21 @@ PyString_Repr(PyObject *obj, int smartquotes)
else if (c == '\r') else if (c == '\r')
*p++ = '\\', *p++ = 'r'; *p++ = '\\', *p++ = 'r';
else if (c < ' ' || c >= 0x7f) { else if (c < ' ' || c >= 0x7f) {
/* For performance, we don't want to call *p++ = '\\';
PyOS_snprintf here (extra layers of *p++ = 'x';
function call). */ *p++ = hexdigits[(c & 0xf0) >> 4];
sprintf(p, "\\x%02x", c & 0xff); *p++ = hexdigits[c & 0xf];
p += 4;
} }
else else
*p++ = c; *p++ = c;
} }
assert(newsize - (p - PyString_AS_STRING(v)) >= 1); assert(newsize - (p - PyUnicode_AS_UNICODE(v)) >= 1);
*p++ = quote; *p++ = quote;
*p = '\0'; *p = '\0';
_PyString_Resize( if (PyUnicode_Resize(&v, (p - PyUnicode_AS_UNICODE(v)))) {
&v, (p - PyString_AS_STRING(v))); Py_DECREF(v);
return NULL;
}
return v; return v;
} }
} }
@ -4613,7 +4624,7 @@ PyString_Format(PyObject *format, PyObject *args)
/* Fall through */ /* Fall through */
case 'r': case 'r':
if (c == 'r') if (c == 'r')
temp = PyObject_Repr(v); temp = PyObject_ReprStr8(v);
if (temp == NULL) if (temp == NULL)
goto error; goto error;
if (!PyString_Check(temp)) { if (!PyString_Check(temp)) {

View file

@ -210,7 +210,7 @@ tuplerepr(PyTupleObject *v)
n = v->ob_size; n = v->ob_size;
if (n == 0) if (n == 0)
return PyString_FromString("()"); return PyUnicode_FromString("()");
pieces = PyTuple_New(n); pieces = PyTuple_New(n);
if (pieces == NULL) if (pieces == NULL)
@ -226,29 +226,29 @@ tuplerepr(PyTupleObject *v)
/* Add "()" decorations to the first and last items. */ /* Add "()" decorations to the first and last items. */
assert(n > 0); assert(n > 0);
s = PyString_FromString("("); s = PyUnicode_FromString("(");
if (s == NULL) if (s == NULL)
goto Done; goto Done;
temp = PyTuple_GET_ITEM(pieces, 0); temp = PyTuple_GET_ITEM(pieces, 0);
PyString_ConcatAndDel(&s, temp); PyUnicode_AppendAndDel(&s, temp);
PyTuple_SET_ITEM(pieces, 0, s); PyTuple_SET_ITEM(pieces, 0, s);
if (s == NULL) if (s == NULL)
goto Done; goto Done;
s = PyString_FromString(n == 1 ? ",)" : ")"); s = PyUnicode_FromString(n == 1 ? ",)" : ")");
if (s == NULL) if (s == NULL)
goto Done; goto Done;
temp = PyTuple_GET_ITEM(pieces, n-1); temp = PyTuple_GET_ITEM(pieces, n-1);
PyString_ConcatAndDel(&temp, s); PyUnicode_AppendAndDel(&temp, s);
PyTuple_SET_ITEM(pieces, n-1, temp); PyTuple_SET_ITEM(pieces, n-1, temp);
if (temp == NULL) if (temp == NULL)
goto Done; goto Done;
/* Paste them all together with ", " between. */ /* Paste them all together with ", " between. */
s = PyString_FromString(", "); s = PyUnicode_FromString(", ");
if (s == NULL) if (s == NULL)
goto Done; goto Done;
result = _PyString_Join(s, pieces); result = PyUnicode_Join(s, pieces);
Py_DECREF(s); Py_DECREF(s);
Done: Done:

View file

@ -385,13 +385,13 @@ type_repr(PyTypeObject *type)
kind = "type"; kind = "type";
if (mod != NULL && strcmp(PyString_AS_STRING(mod), "__builtin__")) { if (mod != NULL && strcmp(PyString_AS_STRING(mod), "__builtin__")) {
rtn = PyString_FromFormat("<%s '%s.%s'>", rtn = PyUnicode_FromFormat("<%s '%s.%s'>",
kind, kind,
PyString_AS_STRING(mod), PyString_AS_STRING(mod),
PyString_AS_STRING(name)); PyString_AS_STRING(name));
} }
else else
rtn = PyString_FromFormat("<%s '%s'>", kind, type->tp_name); rtn = PyUnicode_FromFormat("<%s '%s'>", kind, type->tp_name);
Py_XDECREF(mod); Py_XDECREF(mod);
Py_DECREF(name); Py_DECREF(name);
@ -1006,7 +1006,7 @@ class_name(PyObject *cls)
if (name == NULL) { if (name == NULL) {
PyErr_Clear(); PyErr_Clear();
Py_XDECREF(name); Py_XDECREF(name);
name = PyObject_Repr(cls); name = PyObject_ReprStr8(cls);
} }
if (name == NULL) if (name == NULL)
return NULL; return NULL;
@ -2481,12 +2481,12 @@ object_repr(PyObject *self)
if (name == NULL) if (name == NULL)
return NULL; return NULL;
if (mod != NULL && strcmp(PyString_AS_STRING(mod), "__builtin__")) if (mod != NULL && strcmp(PyString_AS_STRING(mod), "__builtin__"))
rtn = PyString_FromFormat("<%s.%s object at %p>", rtn = PyUnicode_FromFormat("<%s.%s object at %p>",
PyString_AS_STRING(mod), PyString_AS_STRING(mod),
PyString_AS_STRING(name), PyString_AS_STRING(name),
self); self);
else else
rtn = PyString_FromFormat("<%s object at %p>", rtn = PyUnicode_FromFormat("<%s object at %p>",
type->tp_name, self); type->tp_name, self);
Py_XDECREF(mod); Py_XDECREF(mod);
Py_DECREF(name); Py_DECREF(name);
@ -4645,7 +4645,7 @@ slot_tp_repr(PyObject *self)
return res; return res;
} }
PyErr_Clear(); PyErr_Clear();
return PyString_FromFormat("<%s object at %p>", return PyUnicode_FromFormat("<%s object at %p>",
self->ob_type->tp_name, self); self->ob_type->tp_name, self);
} }
@ -4662,8 +4662,14 @@ slot_tp_str(PyObject *self)
return res; return res;
} }
else { else {
PyObject *ress;
PyErr_Clear(); PyErr_Clear();
return slot_tp_repr(self); res = slot_tp_repr(self);
if (!res)
return NULL;
ress = _PyUnicode_AsDefaultEncodedString(res, NULL);
Py_DECREF(res);
return ress;
} }
} }
@ -5692,12 +5698,12 @@ super_repr(PyObject *self)
superobject *su = (superobject *)self; superobject *su = (superobject *)self;
if (su->obj_type) if (su->obj_type)
return PyString_FromFormat( return PyUnicode_FromFormat(
"<super: <class '%s'>, <%s object>>", "<super: <class '%s'>, <%s object>>",
su->type ? su->type->tp_name : "NULL", su->type ? su->type->tp_name : "NULL",
su->obj_type->tp_name); su->obj_type->tp_name);
else else
return PyString_FromFormat( return PyUnicode_FromFormat(
"<super: <class '%s'>, NULL>", "<super: <class '%s'>, NULL>",
su->type ? su->type->tp_name : "NULL"); su->type ? su->type->tp_name : "NULL");
} }

View file

@ -5854,6 +5854,29 @@ onError:
return NULL; return NULL;
} }
void
PyUnicode_Append(PyObject **pleft, PyObject *right)
{
PyObject *new;
if (*pleft == NULL)
return;
if (right == NULL || !PyUnicode_Check(*pleft)) {
Py_DECREF(*pleft);
*pleft = NULL;
return;
}
new = PyUnicode_Concat(*pleft, right);
Py_DECREF(*pleft);
*pleft = new;
}
void
PyUnicode_AppendAndDel(PyObject **pleft, PyObject *right)
{
PyUnicode_Append(pleft, right);
Py_XDECREF(right);
}
PyDoc_STRVAR(count__doc__, PyDoc_STRVAR(count__doc__,
"S.count(sub[, start[, end]]) -> int\n\ "S.count(sub[, start[, end]]) -> int\n\
\n\ \n\
@ -6749,7 +6772,7 @@ static
PyObject *unicode_repr(PyObject *unicode) PyObject *unicode_repr(PyObject *unicode)
{ {
PyObject *repr; PyObject *repr;
char *p; Py_UNICODE *p;
Py_UNICODE *s = PyUnicode_AS_UNICODE(unicode); Py_UNICODE *s = PyUnicode_AS_UNICODE(unicode);
Py_ssize_t size = PyUnicode_GET_SIZE(unicode); Py_ssize_t size = PyUnicode_GET_SIZE(unicode);
@ -6771,7 +6794,7 @@ PyObject *unicode_repr(PyObject *unicode)
escape. escape.
*/ */
repr = PyString_FromStringAndSize(NULL, repr = PyUnicode_FromUnicode(NULL,
2 /* quotes */ 2 /* quotes */
#ifdef Py_UNICODE_WIDE #ifdef Py_UNICODE_WIDE
+ 10*size + 10*size
@ -6782,7 +6805,7 @@ PyObject *unicode_repr(PyObject *unicode)
if (repr == NULL) if (repr == NULL)
return NULL; return NULL;
p = PyString_AS_STRING(repr); p = PyUnicode_AS_UNICODE(repr);
/* Add quote */ /* Add quote */
*p++ = (findchar(s, size, '\'') && *p++ = (findchar(s, size, '\'') &&
@ -6791,9 +6814,9 @@ PyObject *unicode_repr(PyObject *unicode)
Py_UNICODE ch = *s++; Py_UNICODE ch = *s++;
/* Escape quotes and backslashes */ /* Escape quotes and backslashes */
if ((ch == (Py_UNICODE) PyString_AS_STRING(repr)[0]) || (ch == '\\')) { if ((ch == PyUnicode_AS_UNICODE(repr)[0]) || (ch == '\\')) {
*p++ = '\\'; *p++ = '\\';
*p++ = (char) ch; *p++ = ch;
continue; continue;
} }
@ -6877,10 +6900,10 @@ PyObject *unicode_repr(PyObject *unicode)
*p++ = (char) ch; *p++ = (char) ch;
} }
/* Add quote */ /* Add quote */
*p++ = PyString_AS_STRING(repr)[0]; *p++ = PyUnicode_AS_UNICODE(repr)[0];
*p = '\0'; *p = '\0';
_PyString_Resize(&repr, p - PyString_AS_STRING(repr)); _PyUnicode_Resize(&repr, p - PyUnicode_AS_UNICODE(repr));
return repr; return repr;
} }

View file

@ -177,7 +177,7 @@ weakref_repr(PyWeakReference *self)
name); name);
Py_XDECREF(nameobj); Py_XDECREF(nameobj);
} }
return PyString_FromString(buffer); return PyUnicode_FromString(buffer);
} }
/* Weak references only support equality, not ordering. Two weak references /* Weak references only support equality, not ordering. Two weak references
@ -451,7 +451,7 @@ proxy_repr(PyWeakReference *proxy)
"<weakproxy at %p to %.100s at %p>", proxy, "<weakproxy at %p to %.100s at %p>", proxy,
PyWeakref_GET_OBJECT(proxy)->ob_type->tp_name, PyWeakref_GET_OBJECT(proxy)->ob_type->tp_name,
PyWeakref_GET_OBJECT(proxy)); PyWeakref_GET_OBJECT(proxy));
return PyString_FromString(buf); return PyUnicode_FromString(buf);
} }

View file

@ -54,7 +54,7 @@ PyErr_SetObject(PyObject *exception, PyObject *value)
{ {
if (exception != NULL && if (exception != NULL &&
!PyExceptionClass_Check(exception)) { !PyExceptionClass_Check(exception)) {
PyObject *excstr = PyObject_Repr(exception); PyObject *excstr = PyObject_ReprStr8(exception);
PyErr_Format(PyExc_SystemError, PyErr_Format(PyExc_SystemError,
"exception %s not a BaseException subclass", "exception %s not a BaseException subclass",
PyString_AS_STRING(excstr)); PyString_AS_STRING(excstr));

View file

@ -94,7 +94,7 @@ ste_repr(PySTEntryObject *ste)
"<symtable entry %.100s(%ld), line %d>", "<symtable entry %.100s(%ld), line %d>",
PyString_AS_STRING(ste->ste_name), PyString_AS_STRING(ste->ste_name),
PyInt_AS_LONG(ste->ste_id), ste->ste_lineno); PyInt_AS_LONG(ste->ste_id), ste->ste_lineno);
return PyString_FromString(buf); return PyUnicode_FromString(buf);
} }
static void static void