[pypy-commit] pypy py3k-qualname: Add __qualname__ attribute to Function

2014-07-27 Thread Ian Foote
Author: Ian Foote 
Branch: py3k-qualname
Changeset: r72559:d3fcc7dfcdee
Date: 2014-07-26 11:21 +0200
http://bitbucket.org/pypy/pypy/changeset/d3fcc7dfcdee/

Log:Add __qualname__ attribute to Function

Cheat by re-using __name__ implementation without adding extra
functionality

diff --git a/pypy/interpreter/function.py b/pypy/interpreter/function.py
--- a/pypy/interpreter/function.py
+++ b/pypy/interpreter/function.py
@@ -404,6 +404,12 @@
 "to a string object"))
 raise
 
+def fget_func_qualname(self, space):
+return self.fget_func_name(space)
+
+def fset_func_qualname(self, space, w_name):
+return self.fset_func_name(space, w_name)
+
 def fdel_func_doc(self, space):
 self.w_doc = space.w_None
 
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
@@ -146,6 +146,10 @@
 assert 日本.__name__ == '日本'
 """
 
+def test_qualname(self):
+def f(): pass
+assert hasattr(f, '__qualname__')
+
 
 class AppTestFunction:
 def test_simple_call(self):
diff --git a/pypy/interpreter/typedef.py b/pypy/interpreter/typedef.py
--- a/pypy/interpreter/typedef.py
+++ b/pypy/interpreter/typedef.py
@@ -807,6 +807,8 @@
   Function.fset_func_code)
 getset_func_name = GetSetProperty(Function.fget_func_name,
   Function.fset_func_name)
+getset_func_qualname = GetSetProperty(Function.fget_func_qualname,
+  Function.fset_func_qualname)
 getset_func_annotations = GetSetProperty(Function.fget_func_annotations,
 Function.fset_func_annotations,
 Function.fdel_func_annotations)
@@ -824,6 +826,7 @@
 __code__ = getset_func_code,
 __doc__ = getset_func_doc,
 __name__ = getset_func_name,
+__qualname__ = getset_func_qualname,
 __dict__ = getset_func_dict,
 __defaults__ = getset_func_defaults,
 __kwdefaults__ = getset_func_kwdefaults,
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy py3k-qualname: Failed attempt to update _make_function to add qualname to bytecode

2014-07-27 Thread Ian Foote
Author: Ian Foote 
Branch: py3k-qualname
Changeset: r72560:04e12b74c3cd
Date: 2014-07-26 17:22 +0200
http://bitbucket.org/pypy/pypy/changeset/04e12b74c3cd/

Log:Failed attempt to update _make_function to add qualname to bytecode

diff --git a/pypy/interpreter/astcompiler/assemble.py 
b/pypy/interpreter/astcompiler/assemble.py
--- a/pypy/interpreter/astcompiler/assemble.py
+++ b/pypy/interpreter/astcompiler/assemble.py
@@ -639,10 +639,10 @@
 return 1 - arg
 
 def _compute_MAKE_CLOSURE(arg):
-return -1 - _num_args(arg) - ((arg >> 16) & 0x)
+return -2 - _num_args(arg) - ((arg >> 16) & 0x)
 
 def _compute_MAKE_FUNCTION(arg):
-return -_num_args(arg) - ((arg >> 16) & 0x)
+return -1 -_num_args(arg) - ((arg >> 16) & 0x)
 
 def _compute_BUILD_SLICE(arg):
 if arg == 3:
diff --git a/pypy/interpreter/astcompiler/codegen.py 
b/pypy/interpreter/astcompiler/codegen.py
--- a/pypy/interpreter/astcompiler/codegen.py
+++ b/pypy/interpreter/astcompiler/codegen.py
@@ -282,9 +282,12 @@
 self.add_none_to_final_return = False
 mod.body.walkabout(self)
 
-def _make_function(self, code, num_defaults=0):
+def _make_function(self, code, num_defaults=0, w_qualname=None):
 """Emit the opcodes to turn a code object into a function."""
+if w_qualname is None:
+w_qualname = self.space.wrap(code.co_name.decode('utf-8'))
 code_index = self.add_const(code)
+qualname_index = self.add_const(w_qualname)
 if code.co_freevars:
 # Load cell and free vars to pass on.
 for free in code.co_freevars:
@@ -296,9 +299,11 @@
 self.emit_op_arg(ops.LOAD_CLOSURE, index)
 self.emit_op_arg(ops.BUILD_TUPLE, len(code.co_freevars))
 self.emit_op_arg(ops.LOAD_CONST, code_index)
+self.emit_op_arg(ops.LOAD_CONST, qualname_index)
 self.emit_op_arg(ops.MAKE_CLOSURE, num_defaults)
 else:
 self.emit_op_arg(ops.LOAD_CONST, code_index)
+self.emit_op_arg(ops.LOAD_CONST, qualname_index)
 self.emit_op_arg(ops.MAKE_FUNCTION, num_defaults)
 
 def _visit_kwonlydefaults(self, args):
@@ -597,7 +602,7 @@
 self.emit_op(ops.POP_TOP)
 if handler.name:
 ## generate the equivalent of:
-## 
+##
 ## try:
 ## # body
 ## except type as name:
@@ -1084,7 +1089,7 @@
 self._make_call(0,
 call.args, call.keywords,
 call.starargs, call.kwargs)
-
+
 def _call_has_no_star_args(self, call):
 return not call.starargs and not call.kwargs
 
diff --git a/pypy/interpreter/pycode.py b/pypy/interpreter/pycode.py
--- a/pypy/interpreter/pycode.py
+++ b/pypy/interpreter/pycode.py
@@ -35,7 +35,7 @@
 # different value for the highest 16 bits. Bump pypy_incremental_magic every
 # time you make pyc files incompatible
 
-pypy_incremental_magic = 48 # bump it by 16
+pypy_incremental_magic = 64 # bump it by 16
 assert pypy_incremental_magic % 16 == 0
 assert pypy_incremental_magic < 3000 # the magic number of Python 3. There are
  # no known magic numbers below this value
diff --git a/pypy/interpreter/pyopcode.py b/pypy/interpreter/pyopcode.py
--- a/pypy/interpreter/pyopcode.py
+++ b/pypy/interpreter/pyopcode.py
@@ -1205,6 +1205,7 @@
 @jit.unroll_safe
 def _make_function(self, oparg, freevars=None):
 space = self.space
+w_qualname = self.popvalue()
 w_codeobj = self.popvalue()
 codeobj = self.space.interp_w(PyCode, w_codeobj)
 if freevars is not None:
@@ -1236,7 +1237,7 @@
 
 @jit.unroll_safe
 def MAKE_CLOSURE(self, oparg, next_instr):
-w_freevarstuple = self.peekvalue(1)
+w_freevarstuple = self.peekvalue(2)
 freevars = [self.space.interp_w(Cell, cell)
 for cell in self.space.fixedview(w_freevarstuple)]
 self._make_function(oparg, freevars)
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
@@ -147,8 +147,11 @@
 """
 
 def test_qualname(self):
-def f(): pass
+def f():
+pass
 assert hasattr(f, '__qualname__')
+expected = 'AppTestFunctionIntrospection.test_qualname..f'
+assert f.__qualname__ == expected
 
 
 class AppTestFunction:
diff --git a/pypy/tool/importfun.py b/pypy/tool/importfun.py
--- a/pypy/tool/importfun.py
+++ b/pypy/tool/importfun.py
@@ -213,7 +213,7 @@
 if postop != _op_.LOAD_ATTR:
 break
 seenloadattr = True
-
+
   

[pypy-commit] pypy py3.3: Fix error message for converting surrogate to int

2014-07-27 Thread Ian Foote
Author: Ian Foote 
Branch: py3.3
Changeset: r72568:13703de458c9
Date: 2014-07-27 14:01 +0200
http://bitbucket.org/pypy/pypy/changeset/13703de458c9/

Log:Fix error message for converting surrogate to int

diff --git a/pypy/objspace/std/test/test_intobject.py 
b/pypy/objspace/std/test/test_intobject.py
--- a/pypy/objspace/std/test/test_intobject.py
+++ b/pypy/objspace/std/test/test_intobject.py
@@ -418,11 +418,11 @@
 return None
 inst = a()
 raises(TypeError, int, inst)
-assert inst.ar == True 
+assert inst.ar == True
 
 class b(object):
-pass 
-raises((AttributeError,TypeError), int, b()) 
+pass
+raises((AttributeError,TypeError), int, b())
 
 def test_special_long(self):
 class a(object):
@@ -504,6 +504,11 @@
 else:
 assert False, value
 
+def test_int_error_msg_surrogate(self):
+value = u'123\ud800'
+e = raises(ValueError, int, value)
+assert str(e.value) == "invalid literal for int() with base 10: %r" % 
value
+
 def test_fake_int_as_base(self):
 class MyInt(object):
 def __init__(self, x):
diff --git a/pypy/objspace/std/unicodeobject.py 
b/pypy/objspace/std/unicodeobject.py
--- a/pypy/objspace/std/unicodeobject.py
+++ b/pypy/objspace/std/unicodeobject.py
@@ -1166,7 +1166,7 @@
 except KeyError:
 pass
 result[i] = unichr(uchr)
-return unicodehelper.encode_utf8(space, u''.join(result))
+return unicodehelper.encode_utf8(space, u''.join(result), 
allow_surrogates=True)
 
 
 _repr_function, _ = make_unicode_escape_function(
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit