Author: Philip Jenvey <pjen...@underboss.org> Branch: py3k Changeset: r64742:4042cddab652 Date: 2013-06-03 17:29 -0700 http://bitbucket.org/pypy/pypy/changeset/4042cddab652/
Log: o fix identifier handling around space.getname (use unicode throughout) o space.getname\W_TypeObject.get_module_type_name now return unicode o space.getrepr now accepts unicode diff --git a/pypy/goal/targetpypystandalone.py b/pypy/goal/targetpypystandalone.py --- a/pypy/goal/targetpypystandalone.py +++ b/pypy/goal/targetpypystandalone.py @@ -68,7 +68,7 @@ ## con.interact() except OperationError, e: debug("OperationError:") - debug(" operror-type: " + e.w_type.getname(space)) + debug(" operror-type: " + e.w_type.getname(space).encode('utf-8')) debug(" operror-value: " + space.str_w(space.str(e.get_w_value(space)))) return 1 finally: @@ -76,7 +76,7 @@ space.call_function(w_run_toplevel, w_call_finish_gateway) except OperationError, e: debug("OperationError:") - debug(" operror-type: " + e.w_type.getname(space)) + debug(" operror-type: " + e.w_type.getname(space).encode('utf-8')) debug(" operror-value: " + space.str_w(space.str(e.get_w_value(space)))) return 1 return exitcode @@ -117,7 +117,7 @@ except OperationError, e: if verbose: debug("OperationError:") - debug(" operror-type: " + e.w_type.getname(space)) + debug(" operror-type: " + e.w_type.getname(space).encode('utf-8')) debug(" operror-value: " + space.str_w(space.str(e.get_w_value(space)))) return 1 @@ -155,7 +155,7 @@ stmt.exec_code(space, w_globals, w_globals) except OperationError, e: debug("OperationError:") - debug(" operror-type: " + e.w_type.getname(space)) + debug(" operror-type: " + e.w_type.getname(space).encode('utf-8')) debug(" operror-value: " + space.str_w(space.str(e.get_w_value(space)))) return 1 return 0 diff --git a/pypy/interpreter/baseobjspace.py b/pypy/interpreter/baseobjspace.py --- a/pypy/interpreter/baseobjspace.py +++ b/pypy/interpreter/baseobjspace.py @@ -77,11 +77,12 @@ raise NotImplementedError("only for interp-level user subclasses " "from typedef.py") - def getname(self, space, default='?'): + def getname(self, space, default=u'?'): try: - return space.str_w(space.getattr(self, space.wrap('__name__'))) + return space.unicode_w(space.getattr(self, space.wrap('__name__'))) except OperationError, e: - if e.match(space, space.w_TypeError) or e.match(space, space.w_AttributeError): + if (e.match(space, space.w_TypeError) or + e.match(space, space.w_AttributeError)): return default raise @@ -104,10 +105,9 @@ w_id = space.rshift(w_id, w_4) return ''.join(addrstring) - def getrepr(self, space, info, moreinfo=''): - addrstring = self.getaddrstring(space) - return space.wrap("<%s at 0x%s%s>" % (info, addrstring, - moreinfo)) + def getrepr(self, space, info, moreinfo=u''): + addrstring = unicode(self.getaddrstring(space)) + return space.wrap(u"<%s at 0x%s%s>" % (info, addrstring, moreinfo)) def getslotvalue(self, index): raise NotImplementedError diff --git a/pypy/interpreter/error.py b/pypy/interpreter/error.py --- a/pypy/interpreter/error.py +++ b/pypy/interpreter/error.py @@ -414,15 +414,7 @@ elif fmt in 'NT': if fmt == 'T': value = space.type(value) - try: - w_name = space.getattr(value, space.wrap('__name__')) - except OperationError as e: - if not (e.match(space, space.w_TypeError) or - e.match(space, space.w_AttributeError)): - raise - result = u'?' - else: - result = space.unicode_w(w_name) + result = value.getname(space) else: result = unicode(value) lst[i + i + 1] = result @@ -450,7 +442,7 @@ %8 - The result of arg.decode('utf-8', 'strict') %N - The result of w_arg.getname(space) - %R - The result of space.str_w(space.repr(w_arg)) + %R - The result of space.unicode_w(space.repr(w_arg)) %T - The result of space.type(w_arg).getname(space) """ diff --git a/pypy/interpreter/function.py b/pypy/interpreter/function.py --- a/pypy/interpreter/function.py +++ b/pypy/interpreter/function.py @@ -245,7 +245,8 @@ return self.call_args(__args__) def descr_function_repr(self): - return self.getrepr(self.space, 'function %s' % (self.name,)) + return self.getrepr(self.space, u'function %s' % + (self.name.decode('utf-8'),)) # delicate _all = {'': None} @@ -390,7 +391,7 @@ self.w_doc = w_doc def fget_func_name(self, space): - return space.wrap(self.name) + return space.wrap(self.name.decode('utf-8')) def fset_func_name(self, space, w_name): try: @@ -492,7 +493,7 @@ return space.wrap(method) def __repr__(self): - return "bound method %s" % (self.w_function.getname(self.space),) + return u"bound method %s" % (self.w_function.getname(self.space),) def call_args(self, args): space = self.space @@ -509,8 +510,8 @@ name = self.w_function.getname(self.space) w_class = space.type(self.w_instance) typename = w_class.getname(self.space) - objrepr = space.str_w(space.repr(self.w_instance)) - s = '<bound method %s.%s of %s>' % (typename, name, objrepr) + objrepr = space.unicode_w(space.repr(self.w_instance)) + s = u'<bound method %s.%s of %s>' % (typename, name, objrepr) return space.wrap(s) def descr_method_getattribute(self, w_attr): diff --git a/pypy/interpreter/test/test_argument.py b/pypy/interpreter/test/test_argument.py --- a/pypy/interpreter/test/test_argument.py +++ b/pypy/interpreter/test/test_argument.py @@ -135,8 +135,8 @@ def type(self, obj): class Type: - def getname(self, space, default='?'): - return type(obj).__name__ + def getname(self, space, default=u'?'): + return unicode(type(obj).__name__) return Type() diff --git a/pypy/interpreter/test/test_compiler.py b/pypy/interpreter/test/test_compiler.py --- a/pypy/interpreter/test/test_compiler.py +++ b/pypy/interpreter/test/test_compiler.py @@ -844,6 +844,14 @@ c = compile('from os import 日本', '', 'exec') assert ('日本',) in c.co_consts + def test_class_nonascii(self): + """ + class 日本: + pass + assert 日本.__name__ == '日本' + assert '日本' in repr(日本) + """ + def test_cpython_issue2301(self): skip('XXX') try: diff --git a/pypy/interpreter/test/test_error.py b/pypy/interpreter/test/test_error.py --- a/pypy/interpreter/test/test_error.py +++ b/pypy/interpreter/test/test_error.py @@ -126,7 +126,7 @@ def test_new_exception(space): w_error = new_exception_class(space, '_socket.error') - assert w_error.getname(space) == 'error' + assert w_error.getname(space) == u'error' assert space.str_w(space.repr(w_error)) == "<class '_socket.error'>" operr = OperationError(w_error, space.wrap("message")) assert operr.match(space, w_error) diff --git a/pypy/interpreter/test/test_executioncontext.py b/pypy/interpreter/test/test_executioncontext.py --- a/pypy/interpreter/test/test_executioncontext.py +++ b/pypy/interpreter/test/test_executioncontext.py @@ -105,7 +105,7 @@ w_class = space.type(seen[0].w_instance) found = 'method %s of %s' % ( seen[0].w_function.name, - w_class.getname(space)) + w_class.getname(space).encode('utf-8')) else: assert isinstance(seen[0], Function) found = 'builtin %s' % seen[0].name diff --git a/pypy/interpreter/test/test_function.py b/pypy/interpreter/test/test_function.py --- a/pypy/interpreter/test/test_function.py +++ b/pypy/interpreter/test/test_function.py @@ -138,6 +138,14 @@ __name__ = "bar" assert f.__module__ == "foo"''') + def test_func_nonascii(self): + """ + def 日本(): + pass + assert repr(日本).startswith('<function 日本 at ') + assert 日本.__name__ == '日本' + """ + class AppTestFunction: def test_simple_call(self): diff --git a/pypy/module/__builtin__/interp_memoryview.py b/pypy/module/__builtin__/interp_memoryview.py --- a/pypy/module/__builtin__/interp_memoryview.py +++ b/pypy/module/__builtin__/interp_memoryview.py @@ -140,9 +140,9 @@ def descr_repr(self, space): if self.buf is None: - return self.getrepr(space, 'released memory') + return self.getrepr(space, u'released memory') else: - return self.getrepr(space, 'memory') + return self.getrepr(space, u'memory') def descr_release(self, space): self.buf = None diff --git a/pypy/module/_io/interp_bufferedio.py b/pypy/module/_io/interp_bufferedio.py --- a/pypy/module/_io/interp_bufferedio.py +++ b/pypy/module/_io/interp_bufferedio.py @@ -227,16 +227,16 @@ def repr_w(self, space): typename = space.type(self).getname(space) - module = space.str_w(space.type(self).get_module()) + module = space.unicode_w(space.type(self).get_module()) try: w_name = space.getattr(self, space.wrap("name")) except OperationError, e: if not e.match(space, space.w_AttributeError): raise - return space.wrap("<%s.%s>" % (module, typename,)) + return space.wrap(u"<%s.%s>" % (module, typename,)) else: - name_repr = space.str_w(space.repr(w_name)) - return space.wrap("<%s.%s name=%s>" % (module, typename, name_repr)) + name_repr = space.unicode_w(space.repr(w_name)) + return space.wrap(u"<%s.%s name=%s>" % (module, typename, name_repr)) # ______________________________________________ diff --git a/pypy/module/_lsprof/interp_lsprof.py b/pypy/module/_lsprof/interp_lsprof.py --- a/pypy/module/_lsprof/interp_lsprof.py +++ b/pypy/module/_lsprof/interp_lsprof.py @@ -201,27 +201,28 @@ w_realclass, _ = space.lookup_in_type_where(w_type, name) if isinstance(w_realclass, W_TypeObject): class_name = w_realclass.get_module_type_name() - return "{method '%s' of '%s' objects}" % (name, class_name) + return u"{method '%s' of '%s' objects}" % (name.decode('utf-8'), + class_name) @jit.elidable_promote() def create_spec_for_function(space, w_func): if w_func.w_module is None: - module = '' + module = u'' else: - module = space.str_w(w_func.w_module) - if module == 'builtins': - module = '' + module = space.unicode_w(w_func.w_module) + if module == u'builtins': + module = u'' else: - module += '.' - pre = 'built-in function ' if isinstance(w_func, BuiltinFunction) else '' - return '{%s%s%s}' % (pre, module, w_func.name) + module += u'.' + pre = u'built-in function ' if isinstance(w_func, BuiltinFunction) else u'' + return u'{%s%s%s}' % (pre, module, w_func.getname(space)) @jit.elidable_promote() def create_spec_for_object(space, w_obj): class_name = space.type(w_obj).getname(space) - return "{'%s' object}" % (class_name,) + return u"{'%s' object}" % (class_name,) def create_spec(space, w_arg): diff --git a/pypy/module/_multiprocessing/interp_connection.py b/pypy/module/_multiprocessing/interp_connection.py --- a/pypy/module/_multiprocessing/interp_connection.py +++ b/pypy/module/_multiprocessing/interp_connection.py @@ -64,8 +64,9 @@ return space.newbool(bool(self.flags & WRITABLE)) def _repr(self, space, handle): - conn_type = ["read-only", "write-only", "read-write"][self.flags - 1] - return space.wrap("<%s %s, handle %d>" % ( + index = self.flags - 1 + conn_type = [u"read-only", u"write-only", u"read-write"][index] + return space.wrap(u"<%s %s, handle %d>" % ( conn_type, space.type(self).getname(space), handle)) def descr_repr(self, space): diff --git a/pypy/module/_weakref/interp__weakref.py b/pypy/module/_weakref/interp__weakref.py --- a/pypy/module/_weakref/interp__weakref.py +++ b/pypy/module/_weakref/interp__weakref.py @@ -172,15 +172,15 @@ def descr__repr__(self, space): w_obj = self.dereference() if w_obj is None: - state = '; dead' + state = u'; dead' else: typename = space.type(w_obj).getname(space) - objname = w_obj.getname(space, '') + objname = w_obj.getname(space, u'') if objname: - state = "; to '%s' (%s)" % (typename, objname) + state = u"; to '%s' (%s)" % (typename, objname) else: - state = "; to '%s'" % (typename,) - return self.getrepr(space, self.typedef.name, state) + state = u"; to '%s'" % (typename,) + return self.getrepr(space, unicode(self.typedef.name), state) class W_Weakref(W_WeakrefBase): diff --git a/pypy/module/cppyy/test/test_zjit.py b/pypy/module/cppyy/test/test_zjit.py --- a/pypy/module/cppyy/test/test_zjit.py +++ b/pypy/module/cppyy/test/test_zjit.py @@ -61,7 +61,7 @@ self.name = name self.__name__ = name def getname(self, space, name): - return self.name + return unicode(self.name) class FakeBuffer(FakeBase): typedname = "buffer" def __init__(self, val): diff --git a/pypy/module/cpyext/classobject.py b/pypy/module/cpyext/classobject.py --- a/pypy/module/cpyext/classobject.py +++ b/pypy/module/cpyext/classobject.py @@ -28,9 +28,8 @@ return space.call_args(self.w_function, __args__) def descr_repr(self, space): - name = space.str_w( - space.getattr(self.w_function, space.wrap('__name__'))) - return self.getrepr(space, '<instancemethod %s>' % (name,)) + return self.getrepr(space, u'<instancemethod %s>' % + (space.getname(self.w_function),)) InstanceMethod.typedef = TypeDef("instancemethod", __new__ = interp2app(InstanceMethod.descr_new), diff --git a/pypy/module/cpyext/methodobject.py b/pypy/module/cpyext/methodobject.py --- a/pypy/module/cpyext/methodobject.py +++ b/pypy/module/cpyext/methodobject.py @@ -124,9 +124,9 @@ return self.space.unwrap(self.descr_method_repr()) def descr_method_repr(self): - return self.getrepr(self.space, - "built-in method '%s' of '%s' object" % - (self.name, self.w_objclass.getname(self.space))) + return self.getrepr( + self.space, u"built-in method '%s' of '%s' object" % + (self.name.decode('utf-8'), self.w_objclass.getname(self.space))) PyCFunction_Check, PyCFunction_CheckExact = build_type_checkers( "CFunction", W_PyCFunctionObject) @@ -143,9 +143,9 @@ return self.space.unwrap(self.descr_method_repr()) def descr_method_repr(self): - return self.getrepr(self.space, - "built-in method '%s' of '%s' object" % - (self.name, self.w_objclass.getname(self.space))) + return self.getrepr( + self.space, u"built-in method '%s' of '%s' object" % + (self.name.decode('utf-8'), self.w_objclass.getname(self.space))) class W_PyCWrapperObject(W_Root): @@ -173,8 +173,8 @@ return self.wrapper_func(space, w_self, w_args, self.func) def descr_method_repr(self): - return self.space.wrap("<slot wrapper '%s' of '%s' objects>" % - (self.method_name, + return self.space.wrap(u"<slot wrapper '%s' of '%s' objects>" % + (self.method_name.decode('utf-8'), self.w_objclass.getname(self.space))) def cwrapper_descr_call(space, w_self, __args__): diff --git a/pypy/module/cpyext/typeobject.py b/pypy/module/cpyext/typeobject.py --- a/pypy/module/cpyext/typeobject.py +++ b/pypy/module/cpyext/typeobject.py @@ -471,7 +471,7 @@ from pypy.module.cpyext.unicodeobject import _PyUnicode_AsString pto.c_tp_name = _PyUnicode_AsString(space, heaptype.c_ht_name) else: - pto.c_tp_name = rffi.str2charp(w_type.getname(space)) + pto.c_tp_name = rffi.str2charp(w_type.getname(space).encode('utf-8')) pto.c_tp_basicsize = -1 # hopefully this makes malloc bail out pto.c_tp_itemsize = 0 # uninitialized fields: diff --git a/pypy/module/exceptions/interp_exceptions.py b/pypy/module/exceptions/interp_exceptions.py --- a/pypy/module/exceptions/interp_exceptions.py +++ b/pypy/module/exceptions/interp_exceptions.py @@ -131,9 +131,10 @@ def descr_repr(self, space): if self.args_w: - args_repr = space.str_w(space.repr(space.newtuple(self.args_w))) + args_repr = space.unicode_w( + space.repr(space.newtuple(self.args_w))) else: - args_repr = "()" + args_repr = u"()" clsname = self.getclass(space).getname(space) return space.wrap(clsname + args_repr) @@ -556,7 +557,7 @@ values_w = space.fixedview(self.args_w[1]) w_tuple = space.newtuple(values_w + [self.w_lastlineno]) args_w = [self.args_w[0], w_tuple] - args_repr = space.str_w(space.repr(space.newtuple(args_w))) + args_repr = space.unicode_w(space.repr(space.newtuple(args_w))) clsname = self.getclass(space).getname(space) return space.wrap(clsname + args_repr) else: diff --git a/pypy/module/exceptions/test/test_exc.py b/pypy/module/exceptions/test/test_exc.py --- a/pypy/module/exceptions/test/test_exc.py +++ b/pypy/module/exceptions/test/test_exc.py @@ -261,3 +261,13 @@ assert e.errno == errno.ENOTDIR else: assert False, "Expected OSError" + + def test_nonascii_name(self): + """ + class 日本(Exception): + pass + assert '日本' in repr(日本) + class 日本2(SyntaxError): + pass + assert '日本2' in repr(日本2) + """ diff --git a/pypy/module/thread/os_lock.py b/pypy/module/thread/os_lock.py --- a/pypy/module/thread/os_lock.py +++ b/pypy/module/thread/os_lock.py @@ -172,7 +172,7 @@ def descr__repr__(self): typename = space.type(self).getname(space) - return space.wrap("<%s owner=%d count=%d>" % ( + return space.wrap(u"<%s owner=%d count=%d>" % ( typename, self.rlock_owner, self.rlock_count)) @unwrap_spec(blocking=bool, timeout=float) diff --git a/pypy/objspace/std/dictmultiobject.py b/pypy/objspace/std/dictmultiobject.py --- a/pypy/objspace/std/dictmultiobject.py +++ b/pypy/objspace/std/dictmultiobject.py @@ -1151,7 +1151,7 @@ w_self.w_dict = w_dict def descr_repr(self, space): - typename = space.type(self).getname(space).decode('utf-8') + typename = space.type(self).getname(space) w_seq = space.call_function(space.w_list, self) seq_repr = space.unicode_w(space.repr(w_seq)) return space.wrap(u"%s(%s)" % (typename, seq_repr)) diff --git a/pypy/objspace/std/listobject.py b/pypy/objspace/std/listobject.py --- a/pypy/objspace/std/listobject.py +++ b/pypy/objspace/std/listobject.py @@ -106,7 +106,7 @@ def _get_printable_location(w_type): return ('list__do_extend_from_iterable [w_type=%s]' % - w_type.getname(w_type.space)) + w_type.getname(w_type.space).encode('utf-8')) _do_extend_jitdriver = jit.JitDriver( diff --git a/pypy/objspace/std/objecttype.py b/pypy/objspace/std/objecttype.py --- a/pypy/objspace/std/objecttype.py +++ b/pypy/objspace/std/objecttype.py @@ -10,13 +10,13 @@ w_module = w_type.lookup("__module__") if w_module is not None: try: - modulename = space.str_w(w_module) + modulename = space.unicode_w(w_module) except OperationError, e: if not e.match(space, space.w_TypeError): raise else: - classname = '%s.%s' % (modulename, classname) - return w_obj.getrepr(space, '%s object' % (classname,)) + classname = u'%s.%s' % (modulename, classname) + return w_obj.getrepr(space, u'%s object' % (classname,)) def descr__str__(space, w_obj): w_type = space.type(w_obj) diff --git a/pypy/objspace/std/test/test_typeobject.py b/pypy/objspace/std/test/test_typeobject.py --- a/pypy/objspace/std/test/test_typeobject.py +++ b/pypy/objspace/std/test/test_typeobject.py @@ -683,9 +683,24 @@ assert d['A'].__module__ == 'builtins' # obscure, follows CPython assert repr(d['A']) == "<class 'A'>" - def test_repr_unicode(self): + def test_repr_nonascii(self): assert repr(type('日本', (), {})) == "<class '%s.日本'>" % __name__ + def test_name_nonascii(self): + assert type('日本', (), {}).__name__ == '日本' + + def test_errors_nonascii(self): + # Check some arbitrary error messages + Japan = type('日本', (), {}) + obj = Japan() + for f in hex, int, len, next, open, set, 'foo'.startswith: + try: + f(obj) + except TypeError as e: + assert '日本' in str(e) + else: + assert False, 'Expected TypeError' + def test_invalid_mro(self): class A(object): pass diff --git a/pypy/objspace/std/test/test_userobject.py b/pypy/objspace/std/test/test_userobject.py --- a/pypy/objspace/std/test/test_userobject.py +++ b/pypy/objspace/std/test/test_userobject.py @@ -1,3 +1,4 @@ +# encoding: utf-8 import py from pypy.interpreter import gateway from pypy.objspace.test import test_descriptor @@ -214,6 +215,11 @@ s = repr(Foo()) assert s.startswith('<a.b.c.Foo object at ') + def test_repr_nonascii(self): + Japan = type('日本', (), dict(__module__='日本国')) + s = repr(Japan()) + assert s.startswith('<日本国.日本 object at ') + def test_del_attr(self): class Foo(object): def __init__(self): diff --git a/pypy/objspace/std/typeobject.py b/pypy/objspace/std/typeobject.py --- a/pypy/objspace/std/typeobject.py +++ b/pypy/objspace/std/typeobject.py @@ -483,14 +483,14 @@ def get_module_type_name(w_self): space = w_self.space w_mod = w_self.get_module() - if not space.isinstance_w(w_mod, space.w_str): - mod = 'builtins' + if not space.isinstance_w(w_mod, space.w_unicode): + mod = u'builtins' else: - mod = space.str_w(w_mod) - if mod != 'builtins': - return '%s.%s' % (mod, w_self.name) + mod = space.unicode_w(w_mod) + if mod != u'builtins': + return u'%s.%s' % (mod, w_self.name.decode('utf-8')) else: - return w_self.name + return w_self.name.decode('utf-8') def add_subclass(w_self, w_subclass): space = w_self.space @@ -622,7 +622,7 @@ def descr_get__name__(space, w_type): w_type = _check(space, w_type) - return space.wrap(w_type.name) + return space.wrap(w_type.name.decode('utf-8')) def descr_set__name__(space, w_type, w_value): w_type = _check(space, w_type) @@ -1217,7 +1217,7 @@ cycle.reverse() names = [cls.getname(space) for cls in cycle] raise OperationError(space.w_TypeError, - space.wrap("cycle among base classes: " + ' < '.join(names))) + space.wrap(u"cycle among base classes: " + u' < '.join(names))) # ____________________________________________________________ diff --git a/pypy/tool/pytest/appsupport.py b/pypy/tool/pytest/appsupport.py --- a/pypy/tool/pytest/appsupport.py +++ b/pypy/tool/pytest/appsupport.py @@ -87,7 +87,7 @@ def __init__(self, space, operr): self.space = space self.operr = operr - self.typename = operr.w_type.getname(space) + self.typename = operr.w_type.getname(space).encode('utf-8') self.traceback = AppTraceback(space, self.operr.get_traceback()) debug_excs = getattr(operr, 'debug_excs', []) if debug_excs: _______________________________________________ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit