Author: Brian Kearns <[email protected]>
Branch:
Changeset: r71203:b72c31dbc037
Date: 2014-05-02 13:35 -0400
http://bitbucket.org/pypy/pypy/changeset/b72c31dbc037/
Log: support __future__ flags in gateway appdef from func obj
diff --git a/pypy/interpreter/gateway.py b/pypy/interpreter/gateway.py
--- a/pypy/interpreter/gateway.py
+++ b/pypy/interpreter/gateway.py
@@ -1074,7 +1074,9 @@
return x+y
''')
"""
+ prefix = ""
if not isinstance(source, str):
+ flags = source.__code__.co_flags
source = py.std.inspect.getsource(source).lstrip()
while source.startswith(('@py.test.mark.', '@pytest.mark.')):
# these decorators are known to return the same function
@@ -1083,12 +1085,21 @@
source = source[source.find('\n') + 1:].lstrip()
assert source.startswith("def "), "can only transform functions"
source = source[4:]
+ import __future__
+ if flags & __future__.CO_FUTURE_DIVISION:
+ prefix += "from __future__ import division\n"
+ if flags & __future__.CO_FUTURE_ABSOLUTE_IMPORT:
+ prefix += "from __future__ import absolute_import\n"
+ if flags & __future__.CO_FUTURE_PRINT_FUNCTION:
+ prefix += "from __future__ import print_function\n"
+ if flags & __future__.CO_FUTURE_UNICODE_LITERALS:
+ prefix += "from __future__ import unicode_literals\n"
p = source.find('(')
assert p >= 0
funcname = source[:p].strip()
source = source[p:]
assert source.strip()
- funcsource = "def %s%s\n" % (funcname, source)
+ funcsource = prefix + "def %s%s\n" % (funcname, source)
#for debugging of wrong source code: py.std.parser.suite(funcsource)
a = applevel(funcsource, filename=filename)
return a.interphook(funcname)
diff --git a/pypy/interpreter/test/test_gateway.py
b/pypy/interpreter/test/test_gateway.py
--- a/pypy/interpreter/test/test_gateway.py
+++ b/pypy/interpreter/test/test_gateway.py
@@ -1,18 +1,20 @@
-
# -*- coding: utf-8 -*-
+from __future__ import division, print_function # for test_app2interp_future
from pypy.interpreter import gateway, argument
from pypy.interpreter.gateway import ObjSpace, W_Root, WrappedDefault
from pypy.interpreter.signature import Signature
import py
import sys
+
class FakeFunc(object):
def __init__(self, space, name):
self.space = space
self.name = name
self.defs_w = []
+
class TestBuiltinCode:
def test_signature(self):
def c(space, w_x, w_y, hello_w):
@@ -89,8 +91,8 @@
w_result = code.funcrun(FakeFunc(self.space, "c"), args)
assert self.space.eq_w(w_result, w(1020))
+
class TestGateway:
-
def test_app2interp(self):
w = self.space.wrap
def app_g3(a, b):
@@ -117,6 +119,14 @@
args = gateway.Arguments(self.space, [w(6)], ['hello', 'world'],
[w(7), w(8)])
assert self.space.int_w(gg(self.space, w(3), args)) == 213
+ def test_app2interp_future(self):
+ w = self.space.wrap
+ def app_g3(a, b):
+ print(end='')
+ return a / b
+ g3 = gateway.app2interp_temp(app_g3)
+ assert self.space.eq_w(g3(self.space, w(1), w(4),), w(0.25))
+
def test_interp2app(self):
space = self.space
w = space.wrap
@@ -616,7 +626,7 @@
w_app_f = self.space.wrap(app_f)
assert isinstance(w_app_f.code, gateway.BuiltinCode2)
-
+
called = []
fastcall_2 = w_app_f.code.fastcall_2
def witness_fastcall_2(space, w_func, w_a, w_b):
@@ -736,7 +746,6 @@
class TestPassThroughArguments:
-
def test_pass_trough_arguments0(self):
space = self.space
@@ -834,7 +843,6 @@
class AppTestKeywordsToBuiltinSanity(object):
-
def test_type(self):
class X(object):
def __init__(self, **kw):
@@ -873,4 +881,3 @@
d.update(**{clash: 33})
dict.update(d, **{clash: 33})
-
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit