diff --git a/Lib/ctypes/test/test_structures.py b/Lib/ctypes/test/test_structures.py --- a/Lib/ctypes/test/test_structures.py +++ b/Lib/ctypes/test/test_structures.py @@ -315,16 +315,11 @@ class StructureTestCase(unittest.TestCas cls, msg = self.get_except(Person, b"Someone", (1, 2)) self.assertEqual(cls, RuntimeError) self.assertEqual(msg, - "(Phone) : " - "expected string, int found") + '(Phone) TypeError: expected string, int found') cls, msg = self.get_except(Person, b"Someone", (b"a", b"b", b"c")) self.assertEqual(cls, RuntimeError) - if issubclass(Exception, object): - self.assertEqual(msg, - "(Phone) : too many initializers") - else: - self.assertEqual(msg, "(Phone) TypeError: too many initializers") + self.assertEqual(msg, "(Phone) TypeError: too many initializers") def test_huge_field_name(self): # issue12881: segfault with large structure field names diff --git a/Lib/optparse.py b/Lib/optparse.py --- a/Lib/optparse.py +++ b/Lib/optparse.py @@ -671,7 +671,7 @@ class Option: elif not isinstance(self.choices, (tuple, list)): raise OptionError( "choices must be a list of strings ('%s' supplied)" - % str(type(self.choices)).split("'")[1], self) + % type(self.choices), self) elif self.choices is not None: raise OptionError( "must not supply choices for type %r" % self.type, self) diff --git a/Lib/test/test_descrtut.py b/Lib/test/test_descrtut.py --- a/Lib/test/test_descrtut.py +++ b/Lib/test/test_descrtut.py @@ -37,16 +37,16 @@ test_1 = """ Here's the new type at work: >>> print(defaultdict) # show our type - + defaultdict >>> print(type(defaultdict)) # its metatype - + type >>> a = defaultdict(default=0.0) # create an instance >>> print(a) # show the instance {} >>> print(type(a)) # show its type - + defaultdict >>> print(a.__class__) # show its class - + defaultdict >>> print(type(a) is a.__class__) # its type is its class True >>> a[1] = 3.25 # modify the instance @@ -258,21 +258,21 @@ implicit first argument that is the *cla ... print("classmethod", cls, y) >>> C.foo(1) - classmethod 1 + classmethod C 1 >>> c = C() >>> c.foo(1) - classmethod 1 + classmethod C 1 >>> class D(C): ... pass >>> D.foo(1) - classmethod 1 + classmethod D 1 >>> d = D() >>> d.foo(1) - classmethod 1 + classmethod D 1 -This prints "classmethod __main__.D 1" both times; in other words, the +This prints "classmethod D 1" both times; in other words, the class passed as the first argument of foo() is the class involved in the call, not the class involved in the definition of foo(). @@ -286,11 +286,11 @@ But notice this: >>> E.foo(1) E.foo() called - classmethod 1 + classmethod C 1 >>> e = E() >>> e.foo(1) E.foo() called - classmethod 1 + classmethod C 1 In this example, the call to C.foo() from E.foo() will see class C as its first argument, not class E. This is to be expected, since the call diff --git a/Lib/test/test_threading.py b/Lib/test/test_threading.py --- a/Lib/test/test_threading.py +++ b/Lib/test/test_threading.py @@ -331,7 +331,7 @@ class ThreadTests(BaseTestCase): sleep(1) # As a non-daemon thread we SHOULD wake up and nothing # should be torn down yet - print("Woke up, sleep function is:", sleep) + print("Woke up, sleep function is: %r" % sleep) threading.Thread(target=child).start() raise SystemExit diff --git a/Lib/test/test_xmlrpc.py b/Lib/test/test_xmlrpc.py --- a/Lib/test/test_xmlrpc.py +++ b/Lib/test/test_xmlrpc.py @@ -649,8 +649,7 @@ class SimpleServerTestCase(BaseServerTes self.assertEqual(result.results[0]['faultCode'], 1) self.assertEqual(result.results[0]['faultString'], - ':method "this_is_not_exists" ' - 'is not supported') + 'Exception:method "this_is_not_exists" is not supported') except (xmlrpclib.ProtocolError, socket.error) as e: # ignore failures due to non-blocking socket 'unavailable' errors if not is_unavailable_exception(e): diff --git a/Objects/funcobject.c b/Objects/funcobject.c --- a/Objects/funcobject.c +++ b/Objects/funcobject.c @@ -610,6 +610,12 @@ func_repr(PyFunctionObject *op) op->func_qualname, op); } +static PyObject* +func_str(PyFunctionObject *op) +{ + return PyUnicode_FromFormat("%U", op->func_qualname); +} + static int func_traverse(PyFunctionObject *f, visitproc visit, void *arg) { @@ -706,7 +712,7 @@ PyTypeObject PyFunction_Type = { 0, /* tp_as_mapping */ 0, /* tp_hash */ function_call, /* tp_call */ - 0, /* tp_str */ + (reprfunc)func_str, /* tp_str */ PyObject_GenericGetAttr, /* tp_getattro */ PyObject_GenericSetAttr, /* tp_setattro */ 0, /* tp_as_buffer */ diff --git a/Objects/methodobject.c b/Objects/methodobject.c --- a/Objects/methodobject.c +++ b/Objects/methodobject.c @@ -198,6 +198,17 @@ meth_repr(PyCFunctionObject *m) } static PyObject * +meth_str(PyCFunctionObject *m) +{ + if (m->m_self == NULL || PyModule_Check(m->m_self)) + return PyUnicode_FromFormat("%s", m->m_ml->ml_name); + return PyUnicode_FromFormat("", + m->m_ml->ml_name, + m->m_self->ob_type->tp_name, + m->m_self); +} + +static PyObject * meth_richcompare(PyObject *self, PyObject *other, int op) { PyCFunctionObject *a, *b; @@ -260,11 +271,11 @@ PyTypeObject PyCFunction_Type = { 0, /* tp_as_mapping */ (hashfunc)meth_hash, /* tp_hash */ PyCFunction_Call, /* tp_call */ - 0, /* tp_str */ + (reprfunc)meth_str, /* tp_str */ PyObject_GenericGetAttr, /* tp_getattro */ 0, /* tp_setattro */ 0, /* tp_as_buffer */ - Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ + Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ 0, /* tp_doc */ (traverseproc)meth_traverse, /* tp_traverse */ 0, /* tp_clear */ diff --git a/Objects/moduleobject.c b/Objects/moduleobject.c --- a/Objects/moduleobject.c +++ b/Objects/moduleobject.c @@ -388,6 +388,27 @@ module_repr(PyModuleObject *m) return repr; } +static PyObject * +module_str(PyModuleObject *m) +{ + PyObject *name, *str; + + name = PyModule_GetNameObject((PyObject *)m); + if (name == NULL) { + /* XXX PyUnicode_FromStringAndSize is documented as part of + the legacy API */ + PyErr_Clear(); + name = PyUnicode_FromStringAndSize("?", 1); + if (name == NULL) /* */ + return NULL; + } + + str = PyUnicode_FromFormat("%U", name); + + Py_DECREF(name); + return str; +} + static int module_traverse(PyModuleObject *m, visitproc visit, void *arg) { @@ -464,7 +485,7 @@ PyTypeObject PyModule_Type = { 0, /* tp_as_mapping */ 0, /* tp_hash */ 0, /* tp_call */ - 0, /* tp_str */ + (reprfunc)module_str, /* tp_str */ PyObject_GenericGetAttr, /* tp_getattro */ PyObject_GenericSetAttr, /* tp_setattro */ 0, /* tp_as_buffer */ diff --git a/Objects/typeobject.c b/Objects/typeobject.c --- a/Objects/typeobject.c +++ b/Objects/typeobject.c @@ -700,6 +700,21 @@ type_repr(PyTypeObject *type) } static PyObject * +type_str(PyTypeObject *type) +{ + PyObject *name, *rtn; + + name = type_qualname(type, NULL); + if (name == NULL) + return NULL; + + rtn = PyUnicode_FromFormat("%U", name); + + Py_DECREF(name); + return rtn; +} + +static PyObject * type_call(PyTypeObject *type, PyObject *args, PyObject *kwds) { PyObject *obj; @@ -2835,7 +2850,7 @@ PyTypeObject PyType_Type = { 0, /* tp_as_mapping */ 0, /* tp_hash */ (ternaryfunc)type_call, /* tp_call */ - 0, /* tp_str */ + (reprfunc)type_str, /* tp_str */ (getattrofunc)type_getattro, /* tp_getattro */ (setattrofunc)type_setattro, /* tp_setattro */ 0, /* tp_as_buffer */