Re: [Python-Dev] [Python-checkins] r61577 - in python/trunk: Include/code.h Include/compile.h Include/parsetok.h Include/pythonrun.h Lib/__future__.py Lib/test/test_print.py Misc/ACKS Misc/NEWS Parser/parser.c Parser/parsetok.c Python/bltinmodule.c Python/future.c ...

This change breaks all the trunk buildbots: ====================================================================== ERROR: testCompileLibrary (test.test_compiler.CompilerTest) ---------------------------------------------------------------------- Traceback (most recent call last): File "S:\buildbots\python\trunk.nelson-windows\build\lib\test\test_compiler.py", line 52, in testCompileLibrary compiler.compile(buf, basename, "exec") File "S:\buildbots\python\trunk.nelson-windows\build\lib\compiler\pycodegen.py", line 64, in compile gen.compile() File "S:\buildbots\python\trunk.nelson-windows\build\lib\compiler\pycodegen.py", line 112, in compile gen = ModuleCodeGenerator(tree) File "S:\buildbots\python\trunk.nelson-windows\build\lib\compiler\pycodegen.py", line 1275, in __init__ self.futures = future.find_futures(tree) File "S:\buildbots\python\trunk.nelson-windows\build\lib\compiler\future.py", line 59, in find_futures walk(node, p1) File "S:\buildbots\python\trunk.nelson-windows\build\lib\compiler\visitor.py", line 106, in walk walker.preorder(tree, visitor) File "S:\buildbots\python\trunk.nelson-windows\build\lib\compiler\visitor.py", line 63, in preorder self.dispatch(tree, *args) # XXX *args make sense? File "S:\buildbots\python\trunk.nelson-windows\build\lib\compiler\visitor.py", line 57, in dispatch return meth(node, *args) File "S:\buildbots\python\trunk.nelson-windows\build\lib\compiler\future.py", line 27, in visitModule if not self.check_stmt(s): File "S:\buildbots\python\trunk.nelson-windows\build\lib\compiler\future.py", line 37, in check_stmt "future feature %s is not defined" % name SyntaxError: future feature print_function is not defined ________________________________________ From: python-checkins-bounces+tnelson=onresolve.com@python.org [python-checkins-bounces+tnelson=onresolve.com@python.org] On Behalf Of eric.smith [python-checkins@python.org] Sent: 18 March 2008 19:45 To: python-checkins@python.org Subject: [Python-checkins] r61577 - in python/trunk: Include/code.h Include/compile.h Include/parsetok.h Include/pythonrun.h Lib/__future__.py Lib/test/test_print.py Misc/ACKS Misc/NEWS Parser/parser.c Parser/parsetok.c Python/bltinmodule.c Python/future.c Pyth... Author: eric.smith Date: Wed Mar 19 00:45:49 2008 New Revision: 61577 Added: python/trunk/Lib/test/test_print.py Modified: python/trunk/Include/code.h python/trunk/Include/compile.h python/trunk/Include/parsetok.h python/trunk/Include/pythonrun.h python/trunk/Lib/__future__.py python/trunk/Misc/ACKS python/trunk/Misc/NEWS python/trunk/Parser/parser.c python/trunk/Parser/parsetok.c python/trunk/Python/bltinmodule.c python/trunk/Python/future.c python/trunk/Python/pythonrun.c Log: Backport of the print function, using a __future__ import. This work is substantially Anthony Baxter's, from issue 1633807. I just freshened it, made a few minor tweaks, and added the test cases. I also created issue 2412, which is to check for 2to3's behavior with the print function. I also added myself to ACKS. Modified: python/trunk/Include/code.h ============================================================================== --- python/trunk/Include/code.h (original) +++ python/trunk/Include/code.h Wed Mar 19 00:45:49 2008 @@ -48,11 +48,12 @@ #define CO_FUTURE_DIVISION 0x2000 #define CO_FUTURE_ABSOLUTE_IMPORT 0x4000 /* do absolute imports by default */ #define CO_FUTURE_WITH_STATEMENT 0x8000 +#define CO_FUTURE_PRINT_FUNCTION 0x10000 /* This should be defined if a future statement modifies the syntax. For example, when a keyword is added. */ -#if 0 +#if 1 #define PY_PARSER_REQUIRES_FUTURE_KEYWORD #endif Modified: python/trunk/Include/compile.h ============================================================================== --- python/trunk/Include/compile.h (original) +++ python/trunk/Include/compile.h Wed Mar 19 00:45:49 2008 @@ -24,6 +24,8 @@ #define FUTURE_DIVISION "division" #define FUTURE_ABSOLUTE_IMPORT "absolute_import" #define FUTURE_WITH_STATEMENT "with_statement" +#define FUTURE_PRINT_FUNCTION "print_function" + struct _mod; /* Declare the existence of this type */ PyAPI_FUNC(PyCodeObject *) PyAST_Compile(struct _mod *, const char *, Modified: python/trunk/Include/parsetok.h ============================================================================== --- python/trunk/Include/parsetok.h (original) +++ python/trunk/Include/parsetok.h Wed Mar 19 00:45:49 2008 @@ -27,6 +27,10 @@ #define PyPARSE_WITH_IS_KEYWORD 0x0003 #endif +#define PyPARSE_PRINT_IS_FUNCTION 0x0004 + + + PyAPI_FUNC(node *) PyParser_ParseString(const char *, grammar *, int, perrdetail *); PyAPI_FUNC(node *) PyParser_ParseFile (FILE *, const char *, grammar *, int, Modified: python/trunk/Include/pythonrun.h ============================================================================== --- python/trunk/Include/pythonrun.h (original) +++ python/trunk/Include/pythonrun.h Wed Mar 19 00:45:49 2008 @@ -8,7 +8,7 @@ #endif #define PyCF_MASK (CO_FUTURE_DIVISION | CO_FUTURE_ABSOLUTE_IMPORT | \ - CO_FUTURE_WITH_STATEMENT) + CO_FUTURE_WITH_STATEMENT|CO_FUTURE_PRINT_FUNCTION) #define PyCF_MASK_OBSOLETE (CO_NESTED) #define PyCF_SOURCE_IS_UTF8 0x0100 #define PyCF_DONT_IMPLY_DEDENT 0x0200 Modified: python/trunk/Lib/__future__.py ============================================================================== --- python/trunk/Lib/__future__.py (original) +++ python/trunk/Lib/__future__.py Wed Mar 19 00:45:49 2008 @@ -53,6 +53,7 @@ "division", "absolute_import", "with_statement", + "print_function", ] __all__ = ["all_feature_names"] + all_feature_names @@ -66,6 +67,7 @@ CO_FUTURE_DIVISION = 0x2000 # division CO_FUTURE_ABSOLUTE_IMPORT = 0x4000 # perform absolute imports by default CO_FUTURE_WITH_STATEMENT = 0x8000 # with statement +CO_FUTURE_PRINT_FUNCTION = 0x10000 # print function class _Feature: def __init__(self, optionalRelease, mandatoryRelease, compiler_flag): @@ -114,3 +116,7 @@ with_statement = _Feature((2, 5, 0, "alpha", 1), (2, 6, 0, "alpha", 0), CO_FUTURE_WITH_STATEMENT) + +print_function = _Feature((2, 6, 0, "alpha", 2), + (3, 0, 0, "alpha", 0), + CO_FUTURE_PRINT_FUNCTION) Added: python/trunk/Lib/test/test_print.py ============================================================================== --- (empty file) +++ python/trunk/Lib/test/test_print.py Wed Mar 19 00:45:49 2008 @@ -0,0 +1,129 @@ +"""Test correct operation of the print function. +""" + +from __future__ import print_function + +import unittest +from test import test_support + +import sys +try: + # 3.x + from io import StringIO +except ImportError: + # 2.x + from StringIO import StringIO + +from contextlib import contextmanager + +NotDefined = object() + +# A dispatch table all 8 combinations of providing +# sep, end, and file +# I use this machinery so that I'm not just passing default +# values to print, I'm eiher passing or not passing in the +# arguments +dispatch = { + (False, False, False): + lambda args, sep, end, file: print(*args), + (False, False, True): + lambda args, sep, end, file: print(file=file, *args), + (False, True, False): + lambda args, sep, end, file: print(end=end, *args), + (False, True, True): + lambda args, sep, end, file: print(end=end, file=file, *args), + (True, False, False): + lambda args, sep, end, file: print(sep=sep, *args), + (True, False, True): + lambda args, sep, end, file: print(sep=sep, file=file, *args), + (True, True, False): + lambda args, sep, end, file: print(sep=sep, end=end, *args), + (True, True, True): + lambda args, sep, end, file: print(sep=sep, end=end, file=file, *args), + } + +@contextmanager +def stdout_redirected(new_stdout): + save_stdout = sys.stdout + sys.stdout = new_stdout + try: + yield None + finally: + sys.stdout = save_stdout + +# Class used to test __str__ and print +class ClassWith__str__: + def __init__(self, x): + self.x = x + def __str__(self): + return self.x + +class TestPrint(unittest.TestCase): + def check(self, expected, args, + sep=NotDefined, end=NotDefined, file=NotDefined): + # Capture sys.stdout in a StringIO. Call print with args, + # and with sep, end, and file, if they're defined. Result + # must match expected. + + # Look up the actual function to call, based on if sep, end, and file + # are defined + fn = dispatch[(sep is not NotDefined, + end is not NotDefined, + file is not NotDefined)] + + t = StringIO() + with stdout_redirected(t): + fn(args, sep, end, file) + + self.assertEqual(t.getvalue(), expected) + + def test_print(self): + def x(expected, args, sep=NotDefined, end=NotDefined): + # Run the test 2 ways: not using file, and using + # file directed to a StringIO + + self.check(expected, args, sep=sep, end=end) + + # When writing to a file, stdout is expected to be empty + o = StringIO() + self.check('', args, sep=sep, end=end, file=o) + + # And o will contain the expected output + self.assertEqual(o.getvalue(), expected) + + x('\n', ()) + x('a\n', ('a',)) + x('None\n', (None,)) + x('1 2\n', (1, 2)) + x('1 2\n', (1, ' ', 2)) + x('1*2\n', (1, 2), sep='*') + x('1 s', (1, 's'), end='') + x('a\nb\n', ('a', 'b'), sep='\n') + x('1.01', (1.0, 1), sep='', end='') + x('1*a*1.3+', (1, 'a', 1.3), sep='*', end='+') + x('a\n\nb\n', ('a\n', 'b'), sep='\n') + x('\0+ +\0\n', ('\0', ' ', '\0'), sep='+') + + x('a\n b\n', ('a\n', 'b')) + x('a\n b\n', ('a\n', 'b'), sep=None) + x('a\n b\n', ('a\n', 'b'), end=None) + x('a\n b\n', ('a\n', 'b'), sep=None, end=None) + + x('*\n', (ClassWith__str__('*'),)) + x('abc 1\n', (ClassWith__str__('abc'), 1)) + + # 2.x unicode tests + x(u'1 2\n', ('1', u'2')) + x(u'u\1234\n', (u'u\1234',)) + x(u' abc 1\n', (' ', ClassWith__str__(u'abc'), 1)) + + # errors + self.assertRaises(TypeError, print, '', sep=3) + self.assertRaises(TypeError, print, '', end=3) + self.assertRaises(AttributeError, print, '', file='') + +def test_main(): + test_support.run_unittest(TestPrint) + +if __name__ == "__main__": + test_main() Modified: python/trunk/Misc/ACKS ============================================================================== --- python/trunk/Misc/ACKS (original) +++ python/trunk/Misc/ACKS Wed Mar 19 00:45:49 2008 @@ -622,6 +622,7 @@ J. Sipprell Kragen Sitaker Christopher Smith +Eric V. Smith Gregory P. Smith Rafal Smotrzyk Dirk Soede Modified: python/trunk/Misc/NEWS ============================================================================== --- python/trunk/Misc/NEWS (original) +++ python/trunk/Misc/NEWS Wed Mar 19 00:45:49 2008 @@ -12,6 +12,9 @@ Core and builtins ----------------- +- Issue 1745. Backport print function with: + from __future__ import print_function + - Issue 2332: add new attribute names for instance method objects. The two changes are: im_self -> __self__ and im_func -> __func__ Modified: python/trunk/Parser/parser.c ============================================================================== --- python/trunk/Parser/parser.c (original) +++ python/trunk/Parser/parser.c Wed Mar 19 00:45:49 2008 @@ -149,12 +149,10 @@ strcmp(l->lb_str, s) != 0) continue; #ifdef PY_PARSER_REQUIRES_FUTURE_KEYWORD - if (!(ps->p_flags & CO_FUTURE_WITH_STATEMENT)) { - if (s[0] == 'w' && strcmp(s, "with") == 0) - break; /* not a keyword yet */ - else if (s[0] == 'a' && strcmp(s, "as") == 0) - break; /* not a keyword yet */ - } + if (ps->p_flags & CO_FUTURE_PRINT_FUNCTION && + s[0] == 'p' && strcmp(s, "print") == 0) { + break; /* no longer a keyword */ + } #endif D(printf("It's a keyword\n")); return n - i; @@ -208,6 +206,10 @@ strcmp(STR(CHILD(cch, 0)), "with_statement") == 0) { ps->p_flags |= CO_FUTURE_WITH_STATEMENT; break; + } else if (NCH(cch) >= 1 && TYPE(CHILD(cch, 0)) == NAME && + strcmp(STR(CHILD(cch, 0)), "print_function") == 0) { + ps->p_flags |= CO_FUTURE_PRINT_FUNCTION; + break; } } } Modified: python/trunk/Parser/parsetok.c ============================================================================== --- python/trunk/Parser/parsetok.c (original) +++ python/trunk/Parser/parsetok.c Wed Mar 19 00:45:49 2008 @@ -123,8 +123,8 @@ return NULL; } #ifdef PY_PARSER_REQUIRES_FUTURE_KEYWORD - if (flags & PyPARSE_WITH_IS_KEYWORD) - ps->p_flags |= CO_FUTURE_WITH_STATEMENT; + if (flags & PyPARSE_PRINT_IS_FUNCTION) + ps->p_flags |= CO_FUTURE_PRINT_FUNCTION; #endif for (;;) { @@ -167,26 +167,6 @@ str[len] = '\0'; #ifdef PY_PARSER_REQUIRES_FUTURE_KEYWORD - /* This is only necessary to support the "as" warning, but - we don't want to warn about "as" in import statements. */ - if (type == NAME && - len == 6 && str[0] == 'i' && strcmp(str, "import") == 0) - handling_import = 1; - - /* Warn about with as NAME */ - if (type == NAME && - !(ps->p_flags & CO_FUTURE_WITH_STATEMENT)) { - if (len == 4 && str[0] == 'w' && strcmp(str, "with") == 0) - warn(with_msg, err_ret->filename, tok->lineno); - else if (!(handling_import || handling_with) && - len == 2 && str[0] == 'a' && - strcmp(str, "as") == 0) - warn(as_msg, err_ret->filename, tok->lineno); - } - else if (type == NAME && - (ps->p_flags & CO_FUTURE_WITH_STATEMENT) && - len == 4 && str[0] == 'w' && strcmp(str, "with") == 0) - handling_with = 1; #endif if (a >= tok->line_start) col_offset = a - tok->line_start; Modified: python/trunk/Python/bltinmodule.c ============================================================================== --- python/trunk/Python/bltinmodule.c (original) +++ python/trunk/Python/bltinmodule.c Wed Mar 19 00:45:49 2008 @@ -1486,6 +1486,78 @@ equivalent to (x**y) % z, but may be more efficient (e.g. for longs)."); +static PyObject * +builtin_print(PyObject *self, PyObject *args, PyObject *kwds) +{ + static char *kwlist[] = {"sep", "end", "file", 0}; + static PyObject *dummy_args; + PyObject *sep = NULL, *end = NULL, *file = NULL; + int i, err; + + if (dummy_args == NULL) { + if (!(dummy_args = PyTuple_New(0))) + return NULL; + } + if (!PyArg_ParseTupleAndKeywords(dummy_args, kwds, "|OOO:print", + kwlist, &sep, &end, &file)) + return NULL; + if (file == NULL || file == Py_None) { + file = PySys_GetObject("stdout"); + /* sys.stdout may be None when FILE* stdout isn't connected */ + if (file == Py_None) + Py_RETURN_NONE; + } + + if (sep && sep != Py_None && !PyString_Check(sep) && + !PyUnicode_Check(sep)) { + PyErr_Format(PyExc_TypeError, + "sep must be None, str or unicode, not %.200s", + sep->ob_type->tp_name); + return NULL; + } + if (end && end != Py_None && !PyString_Check(end) && + !PyUnicode_Check(end)) { + PyErr_Format(PyExc_TypeError, + "end must be None, str or unicode, not %.200s", + end->ob_type->tp_name); + return NULL; + } + + for (i = 0; i < PyTuple_Size(args); i++) { + if (i > 0) { + if (sep == NULL || sep == Py_None) + err = PyFile_WriteString(" ", file); + else + err = PyFile_WriteObject(sep, file, + Py_PRINT_RAW); + if (err) + return NULL; + } + err = PyFile_WriteObject(PyTuple_GetItem(args, i), file, + Py_PRINT_RAW); + if (err) + return NULL; + } + + if (end == NULL || end == Py_None) + err = PyFile_WriteString("\n", file); + else + err = PyFile_WriteObject(end, file, Py_PRINT_RAW); + if (err) + return NULL; + + Py_RETURN_NONE; +} + +PyDoc_STRVAR(print_doc, +"print(value, ..., sep=' ', end='\\n', file=sys.stdout)\n\ +\n\ +Prints the values to a stream, or to sys.stdout by default.\n\ +Optional keyword arguments:\n\ +file: a file-like object (stream); defaults to the current sys.stdout.\n\ +sep: string inserted between values, default a space.\n\ +end: string appended after the last value, default a newline."); + /* Return number of items in range (lo, hi, step), when arguments are * PyInt or PyLong objects. step > 0 required. Return a value < 0 if @@ -2424,6 +2496,7 @@ {"open", (PyCFunction)builtin_open, METH_VARARGS | METH_KEYWORDS, open_doc}, {"ord", builtin_ord, METH_O, ord_doc}, {"pow", builtin_pow, METH_VARARGS, pow_doc}, + {"print", (PyCFunction)builtin_print, METH_VARARGS | METH_KEYWORDS, print_doc}, {"range", builtin_range, METH_VARARGS, range_doc}, {"raw_input", builtin_raw_input, METH_VARARGS, raw_input_doc}, {"reduce", builtin_reduce, METH_VARARGS, reduce_doc}, Modified: python/trunk/Python/future.c ============================================================================== --- python/trunk/Python/future.c (original) +++ python/trunk/Python/future.c Wed Mar 19 00:45:49 2008 @@ -33,6 +33,8 @@ ff->ff_features |= CO_FUTURE_ABSOLUTE_IMPORT; } else if (strcmp(feature, FUTURE_WITH_STATEMENT) == 0) { ff->ff_features |= CO_FUTURE_WITH_STATEMENT; + } else if (strcmp(feature, FUTURE_PRINT_FUNCTION) == 0) { + ff->ff_features |= CO_FUTURE_PRINT_FUNCTION; } else if (strcmp(feature, "braces") == 0) { PyErr_SetString(PyExc_SyntaxError, "not a chance"); Modified: python/trunk/Python/pythonrun.c ============================================================================== --- python/trunk/Python/pythonrun.c (original) +++ python/trunk/Python/pythonrun.c Wed Mar 19 00:45:49 2008 @@ -738,18 +738,19 @@ } } +#if 0 /* compute parser flags based on compiler flags */ #define PARSER_FLAGS(flags) \ ((flags) ? ((((flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \ PyPARSE_DONT_IMPLY_DEDENT : 0)) : 0) - -#if 0 +#endif +#if 1 /* Keep an example of flags with future keyword support. */ #define PARSER_FLAGS(flags) \ ((flags) ? ((((flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \ PyPARSE_DONT_IMPLY_DEDENT : 0) \ - | ((flags)->cf_flags & CO_FUTURE_WITH_STATEMENT ? \ - PyPARSE_WITH_IS_KEYWORD : 0)) : 0) + | ((flags)->cf_flags & CO_FUTURE_PRINT_FUNCTION ? \ + PyPARSE_PRINT_IS_FUNCTION : 0)) : 0) #endif int _______________________________________________ Python-checkins mailing list Python-checkins@python.org http://mail.python.org/mailman/listinfo/python-checkins

Yes, I know, and I'm looking at it. It doesn't fail on my Linux or Mac OS X boxes. I'm trying to duplicate the problem. I'm going to try it on my Windows box when I get home in about an hour. I'll fix it tonight. I realize there's a beer riding on the buildbots being green! Eric. Trent Nelson wrote:
This change breaks all the trunk buildbots:
====================================================================== ERROR: testCompileLibrary (test.test_compiler.CompilerTest) ---------------------------------------------------------------------- Traceback (most recent call last): File "S:\buildbots\python\trunk.nelson-windows\build\lib\test\test_compiler.py", line 52, in testCompileLibrary compiler.compile(buf, basename, "exec") File "S:\buildbots\python\trunk.nelson-windows\build\lib\compiler\pycodegen.py", line 64, in compile gen.compile() File "S:\buildbots\python\trunk.nelson-windows\build\lib\compiler\pycodegen.py", line 112, in compile gen = ModuleCodeGenerator(tree) File "S:\buildbots\python\trunk.nelson-windows\build\lib\compiler\pycodegen.py", line 1275, in __init__ self.futures = future.find_futures(tree) File "S:\buildbots\python\trunk.nelson-windows\build\lib\compiler\future.py", line 59, in find_futures walk(node, p1) File "S:\buildbots\python\trunk.nelson-windows\build\lib\compiler\visitor.py", line 106, in walk walker.preorder(tree, visitor) File "S:\buildbots\python\trunk.nelson-windows\build\lib\compiler\visitor.py", line 63, in preorder self.dispatch(tree, *args) # XXX *args make sense? File "S:\buildbots\python\trunk.nelson-windows\build\lib\compiler\visitor.py", line 57, in dispatch return meth(node, *args) File "S:\buildbots\python\trunk.nelson-windows\build\lib\compiler\future.py", line 27, in visitModule if not self.check_stmt(s): File "S:\buildbots\python\trunk.nelson-windows\build\lib\compiler\future.py", line 37, in check_stmt "future feature %s is not defined" % name SyntaxError: future feature print_function is not defined
________________________________________ From: python-checkins-bounces+tnelson=onresolve.com@python.org [python-checkins-bounces+tnelson=onresolve.com@python.org] On Behalf Of eric.smith [python-checkins@python.org] Sent: 18 March 2008 19:45 To: python-checkins@python.org Subject: [Python-checkins] r61577 - in python/trunk: Include/code.h Include/compile.h Include/parsetok.h Include/pythonrun.h Lib/__future__.py Lib/test/test_print.py Misc/ACKS Misc/NEWS Parser/parser.c Parser/parsetok.c Python/bltinmodule.c Python/future.c Pyth...
Author: eric.smith Date: Wed Mar 19 00:45:49 2008 New Revision: 61577
Added: python/trunk/Lib/test/test_print.py Modified: python/trunk/Include/code.h python/trunk/Include/compile.h python/trunk/Include/parsetok.h python/trunk/Include/pythonrun.h python/trunk/Lib/__future__.py python/trunk/Misc/ACKS python/trunk/Misc/NEWS python/trunk/Parser/parser.c python/trunk/Parser/parsetok.c python/trunk/Python/bltinmodule.c python/trunk/Python/future.c python/trunk/Python/pythonrun.c Log: Backport of the print function, using a __future__ import. This work is substantially Anthony Baxter's, from issue 1633807. I just freshened it, made a few minor tweaks, and added the test cases. I also created issue 2412, which is to check for 2to3's behavior with the print function. I also added myself to ACKS.
Modified: python/trunk/Include/code.h ============================================================================== --- python/trunk/Include/code.h (original) +++ python/trunk/Include/code.h Wed Mar 19 00:45:49 2008 @@ -48,11 +48,12 @@ #define CO_FUTURE_DIVISION 0x2000 #define CO_FUTURE_ABSOLUTE_IMPORT 0x4000 /* do absolute imports by default */ #define CO_FUTURE_WITH_STATEMENT 0x8000 +#define CO_FUTURE_PRINT_FUNCTION 0x10000
/* This should be defined if a future statement modifies the syntax. For example, when a keyword is added. */ -#if 0 +#if 1 #define PY_PARSER_REQUIRES_FUTURE_KEYWORD #endif
Modified: python/trunk/Include/compile.h ============================================================================== --- python/trunk/Include/compile.h (original) +++ python/trunk/Include/compile.h Wed Mar 19 00:45:49 2008 @@ -24,6 +24,8 @@ #define FUTURE_DIVISION "division" #define FUTURE_ABSOLUTE_IMPORT "absolute_import" #define FUTURE_WITH_STATEMENT "with_statement" +#define FUTURE_PRINT_FUNCTION "print_function" +
struct _mod; /* Declare the existence of this type */ PyAPI_FUNC(PyCodeObject *) PyAST_Compile(struct _mod *, const char *,
Modified: python/trunk/Include/parsetok.h ============================================================================== --- python/trunk/Include/parsetok.h (original) +++ python/trunk/Include/parsetok.h Wed Mar 19 00:45:49 2008 @@ -27,6 +27,10 @@ #define PyPARSE_WITH_IS_KEYWORD 0x0003 #endif
+#define PyPARSE_PRINT_IS_FUNCTION 0x0004 + + + PyAPI_FUNC(node *) PyParser_ParseString(const char *, grammar *, int, perrdetail *); PyAPI_FUNC(node *) PyParser_ParseFile (FILE *, const char *, grammar *, int,
Modified: python/trunk/Include/pythonrun.h ============================================================================== --- python/trunk/Include/pythonrun.h (original) +++ python/trunk/Include/pythonrun.h Wed Mar 19 00:45:49 2008 @@ -8,7 +8,7 @@ #endif
#define PyCF_MASK (CO_FUTURE_DIVISION | CO_FUTURE_ABSOLUTE_IMPORT | \ - CO_FUTURE_WITH_STATEMENT) + CO_FUTURE_WITH_STATEMENT|CO_FUTURE_PRINT_FUNCTION) #define PyCF_MASK_OBSOLETE (CO_NESTED) #define PyCF_SOURCE_IS_UTF8 0x0100 #define PyCF_DONT_IMPLY_DEDENT 0x0200
Modified: python/trunk/Lib/__future__.py ============================================================================== --- python/trunk/Lib/__future__.py (original) +++ python/trunk/Lib/__future__.py Wed Mar 19 00:45:49 2008 @@ -53,6 +53,7 @@ "division", "absolute_import", "with_statement", + "print_function", ]
__all__ = ["all_feature_names"] + all_feature_names @@ -66,6 +67,7 @@ CO_FUTURE_DIVISION = 0x2000 # division CO_FUTURE_ABSOLUTE_IMPORT = 0x4000 # perform absolute imports by default CO_FUTURE_WITH_STATEMENT = 0x8000 # with statement +CO_FUTURE_PRINT_FUNCTION = 0x10000 # print function
class _Feature: def __init__(self, optionalRelease, mandatoryRelease, compiler_flag): @@ -114,3 +116,7 @@ with_statement = _Feature((2, 5, 0, "alpha", 1), (2, 6, 0, "alpha", 0), CO_FUTURE_WITH_STATEMENT) + +print_function = _Feature((2, 6, 0, "alpha", 2), + (3, 0, 0, "alpha", 0), + CO_FUTURE_PRINT_FUNCTION)
Added: python/trunk/Lib/test/test_print.py ============================================================================== --- (empty file) +++ python/trunk/Lib/test/test_print.py Wed Mar 19 00:45:49 2008 @@ -0,0 +1,129 @@ +"""Test correct operation of the print function. +""" + +from __future__ import print_function + +import unittest +from test import test_support + +import sys +try: + # 3.x + from io import StringIO +except ImportError: + # 2.x + from StringIO import StringIO + +from contextlib import contextmanager + +NotDefined = object() + +# A dispatch table all 8 combinations of providing +# sep, end, and file +# I use this machinery so that I'm not just passing default +# values to print, I'm eiher passing or not passing in the +# arguments +dispatch = { + (False, False, False): + lambda args, sep, end, file: print(*args), + (False, False, True): + lambda args, sep, end, file: print(file=file, *args), + (False, True, False): + lambda args, sep, end, file: print(end=end, *args), + (False, True, True): + lambda args, sep, end, file: print(end=end, file=file, *args), + (True, False, False): + lambda args, sep, end, file: print(sep=sep, *args), + (True, False, True): + lambda args, sep, end, file: print(sep=sep, file=file, *args), + (True, True, False): + lambda args, sep, end, file: print(sep=sep, end=end, *args), + (True, True, True): + lambda args, sep, end, file: print(sep=sep, end=end, file=file, *args), + } + +@contextmanager +def stdout_redirected(new_stdout): + save_stdout = sys.stdout + sys.stdout = new_stdout + try: + yield None + finally: + sys.stdout = save_stdout + +# Class used to test __str__ and print +class ClassWith__str__: + def __init__(self, x): + self.x = x + def __str__(self): + return self.x + +class TestPrint(unittest.TestCase): + def check(self, expected, args, + sep=NotDefined, end=NotDefined, file=NotDefined): + # Capture sys.stdout in a StringIO. Call print with args, + # and with sep, end, and file, if they're defined. Result + # must match expected. + + # Look up the actual function to call, based on if sep, end, and file + # are defined + fn = dispatch[(sep is not NotDefined, + end is not NotDefined, + file is not NotDefined)] + + t = StringIO() + with stdout_redirected(t): + fn(args, sep, end, file) + + self.assertEqual(t.getvalue(), expected) + + def test_print(self): + def x(expected, args, sep=NotDefined, end=NotDefined): + # Run the test 2 ways: not using file, and using + # file directed to a StringIO + + self.check(expected, args, sep=sep, end=end) + + # When writing to a file, stdout is expected to be empty + o = StringIO() + self.check('', args, sep=sep, end=end, file=o) + + # And o will contain the expected output + self.assertEqual(o.getvalue(), expected) + + x('\n', ()) + x('a\n', ('a',)) + x('None\n', (None,)) + x('1 2\n', (1, 2)) + x('1 2\n', (1, ' ', 2)) + x('1*2\n', (1, 2), sep='*') + x('1 s', (1, 's'), end='') + x('a\nb\n', ('a', 'b'), sep='\n') + x('1.01', (1.0, 1), sep='', end='') + x('1*a*1.3+', (1, 'a', 1.3), sep='*', end='+') + x('a\n\nb\n', ('a\n', 'b'), sep='\n') + x('\0+ +\0\n', ('\0', ' ', '\0'), sep='+') + + x('a\n b\n', ('a\n', 'b')) + x('a\n b\n', ('a\n', 'b'), sep=None) + x('a\n b\n', ('a\n', 'b'), end=None) + x('a\n b\n', ('a\n', 'b'), sep=None, end=None) + + x('*\n', (ClassWith__str__('*'),)) + x('abc 1\n', (ClassWith__str__('abc'), 1)) + + # 2.x unicode tests + x(u'1 2\n', ('1', u'2')) + x(u'u\1234\n', (u'u\1234',)) + x(u' abc 1\n', (' ', ClassWith__str__(u'abc'), 1)) + + # errors + self.assertRaises(TypeError, print, '', sep=3) + self.assertRaises(TypeError, print, '', end=3) + self.assertRaises(AttributeError, print, '', file='') + +def test_main(): + test_support.run_unittest(TestPrint) + +if __name__ == "__main__": + test_main()
Modified: python/trunk/Misc/ACKS ============================================================================== --- python/trunk/Misc/ACKS (original) +++ python/trunk/Misc/ACKS Wed Mar 19 00:45:49 2008 @@ -622,6 +622,7 @@ J. Sipprell Kragen Sitaker Christopher Smith +Eric V. Smith Gregory P. Smith Rafal Smotrzyk Dirk Soede
Modified: python/trunk/Misc/NEWS ============================================================================== --- python/trunk/Misc/NEWS (original) +++ python/trunk/Misc/NEWS Wed Mar 19 00:45:49 2008 @@ -12,6 +12,9 @@ Core and builtins -----------------
+- Issue 1745. Backport print function with: + from __future__ import print_function + - Issue 2332: add new attribute names for instance method objects. The two changes are: im_self -> __self__ and im_func -> __func__
Modified: python/trunk/Parser/parser.c ============================================================================== --- python/trunk/Parser/parser.c (original) +++ python/trunk/Parser/parser.c Wed Mar 19 00:45:49 2008 @@ -149,12 +149,10 @@ strcmp(l->lb_str, s) != 0) continue; #ifdef PY_PARSER_REQUIRES_FUTURE_KEYWORD - if (!(ps->p_flags & CO_FUTURE_WITH_STATEMENT)) { - if (s[0] == 'w' && strcmp(s, "with") == 0) - break; /* not a keyword yet */ - else if (s[0] == 'a' && strcmp(s, "as") == 0) - break; /* not a keyword yet */ - } + if (ps->p_flags & CO_FUTURE_PRINT_FUNCTION && + s[0] == 'p' && strcmp(s, "print") == 0) { + break; /* no longer a keyword */ + } #endif D(printf("It's a keyword\n")); return n - i; @@ -208,6 +206,10 @@ strcmp(STR(CHILD(cch, 0)), "with_statement") == 0) { ps->p_flags |= CO_FUTURE_WITH_STATEMENT; break; + } else if (NCH(cch) >= 1 && TYPE(CHILD(cch, 0)) == NAME && + strcmp(STR(CHILD(cch, 0)), "print_function") == 0) { + ps->p_flags |= CO_FUTURE_PRINT_FUNCTION; + break; } } }
Modified: python/trunk/Parser/parsetok.c ============================================================================== --- python/trunk/Parser/parsetok.c (original) +++ python/trunk/Parser/parsetok.c Wed Mar 19 00:45:49 2008 @@ -123,8 +123,8 @@ return NULL; } #ifdef PY_PARSER_REQUIRES_FUTURE_KEYWORD - if (flags & PyPARSE_WITH_IS_KEYWORD) - ps->p_flags |= CO_FUTURE_WITH_STATEMENT; + if (flags & PyPARSE_PRINT_IS_FUNCTION) + ps->p_flags |= CO_FUTURE_PRINT_FUNCTION; #endif
for (;;) { @@ -167,26 +167,6 @@ str[len] = '\0';
#ifdef PY_PARSER_REQUIRES_FUTURE_KEYWORD - /* This is only necessary to support the "as" warning, but - we don't want to warn about "as" in import statements. */ - if (type == NAME && - len == 6 && str[0] == 'i' && strcmp(str, "import") == 0) - handling_import = 1; - - /* Warn about with as NAME */ - if (type == NAME && - !(ps->p_flags & CO_FUTURE_WITH_STATEMENT)) { - if (len == 4 && str[0] == 'w' && strcmp(str, "with") == 0) - warn(with_msg, err_ret->filename, tok->lineno); - else if (!(handling_import || handling_with) && - len == 2 && str[0] == 'a' && - strcmp(str, "as") == 0) - warn(as_msg, err_ret->filename, tok->lineno); - } - else if (type == NAME && - (ps->p_flags & CO_FUTURE_WITH_STATEMENT) && - len == 4 && str[0] == 'w' && strcmp(str, "with") == 0) - handling_with = 1; #endif if (a >= tok->line_start) col_offset = a - tok->line_start;
Modified: python/trunk/Python/bltinmodule.c ============================================================================== --- python/trunk/Python/bltinmodule.c (original) +++ python/trunk/Python/bltinmodule.c Wed Mar 19 00:45:49 2008 @@ -1486,6 +1486,78 @@ equivalent to (x**y) % z, but may be more efficient (e.g. for longs).");
+static PyObject * +builtin_print(PyObject *self, PyObject *args, PyObject *kwds) +{ + static char *kwlist[] = {"sep", "end", "file", 0}; + static PyObject *dummy_args; + PyObject *sep = NULL, *end = NULL, *file = NULL; + int i, err; + + if (dummy_args == NULL) { + if (!(dummy_args = PyTuple_New(0))) + return NULL; + } + if (!PyArg_ParseTupleAndKeywords(dummy_args, kwds, "|OOO:print", + kwlist, &sep, &end, &file)) + return NULL; + if (file == NULL || file == Py_None) { + file = PySys_GetObject("stdout"); + /* sys.stdout may be None when FILE* stdout isn't connected */ + if (file == Py_None) + Py_RETURN_NONE; + } + + if (sep && sep != Py_None && !PyString_Check(sep) && + !PyUnicode_Check(sep)) { + PyErr_Format(PyExc_TypeError, + "sep must be None, str or unicode, not %.200s", + sep->ob_type->tp_name); + return NULL; + } + if (end && end != Py_None && !PyString_Check(end) && + !PyUnicode_Check(end)) { + PyErr_Format(PyExc_TypeError, + "end must be None, str or unicode, not %.200s", + end->ob_type->tp_name); + return NULL; + } + + for (i = 0; i < PyTuple_Size(args); i++) { + if (i > 0) { + if (sep == NULL || sep == Py_None) + err = PyFile_WriteString(" ", file); + else + err = PyFile_WriteObject(sep, file, + Py_PRINT_RAW); + if (err) + return NULL; + } + err = PyFile_WriteObject(PyTuple_GetItem(args, i), file, + Py_PRINT_RAW); + if (err) + return NULL; + } + + if (end == NULL || end == Py_None) + err = PyFile_WriteString("\n", file); + else + err = PyFile_WriteObject(end, file, Py_PRINT_RAW); + if (err) + return NULL; + + Py_RETURN_NONE; +} + +PyDoc_STRVAR(print_doc, +"print(value, ..., sep=' ', end='\\n', file=sys.stdout)\n\ +\n\ +Prints the values to a stream, or to sys.stdout by default.\n\ +Optional keyword arguments:\n\ +file: a file-like object (stream); defaults to the current sys.stdout.\n\ +sep: string inserted between values, default a space.\n\ +end: string appended after the last value, default a newline."); +
/* Return number of items in range (lo, hi, step), when arguments are * PyInt or PyLong objects. step > 0 required. Return a value < 0 if @@ -2424,6 +2496,7 @@ {"open", (PyCFunction)builtin_open, METH_VARARGS | METH_KEYWORDS, open_doc}, {"ord", builtin_ord, METH_O, ord_doc}, {"pow", builtin_pow, METH_VARARGS, pow_doc}, + {"print", (PyCFunction)builtin_print, METH_VARARGS | METH_KEYWORDS, print_doc}, {"range", builtin_range, METH_VARARGS, range_doc}, {"raw_input", builtin_raw_input, METH_VARARGS, raw_input_doc}, {"reduce", builtin_reduce, METH_VARARGS, reduce_doc},
Modified: python/trunk/Python/future.c ============================================================================== --- python/trunk/Python/future.c (original) +++ python/trunk/Python/future.c Wed Mar 19 00:45:49 2008 @@ -33,6 +33,8 @@ ff->ff_features |= CO_FUTURE_ABSOLUTE_IMPORT; } else if (strcmp(feature, FUTURE_WITH_STATEMENT) == 0) { ff->ff_features |= CO_FUTURE_WITH_STATEMENT; + } else if (strcmp(feature, FUTURE_PRINT_FUNCTION) == 0) { + ff->ff_features |= CO_FUTURE_PRINT_FUNCTION; } else if (strcmp(feature, "braces") == 0) { PyErr_SetString(PyExc_SyntaxError, "not a chance");
Modified: python/trunk/Python/pythonrun.c ============================================================================== --- python/trunk/Python/pythonrun.c (original) +++ python/trunk/Python/pythonrun.c Wed Mar 19 00:45:49 2008 @@ -738,18 +738,19 @@ } }
+#if 0 /* compute parser flags based on compiler flags */ #define PARSER_FLAGS(flags) \ ((flags) ? ((((flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \ PyPARSE_DONT_IMPLY_DEDENT : 0)) : 0) - -#if 0 +#endif +#if 1 /* Keep an example of flags with future keyword support. */ #define PARSER_FLAGS(flags) \ ((flags) ? ((((flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \ PyPARSE_DONT_IMPLY_DEDENT : 0) \ - | ((flags)->cf_flags & CO_FUTURE_WITH_STATEMENT ? \ - PyPARSE_WITH_IS_KEYWORD : 0)) : 0) + | ((flags)->cf_flags & CO_FUTURE_PRINT_FUNCTION ? \ + PyPARSE_PRINT_IS_FUNCTION : 0)) : 0) #endif
int _______________________________________________ Python-checkins mailing list Python-checkins@python.org http://mail.python.org/mailman/listinfo/python-checkins _______________________________________________ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/eric%2Bpython-dev%40truebl...

I see the problem. Without -ucompiler, test_compiler doesn't compile everything. I'll fix the breakage shortly. Apologies. Eric Smith wrote:
Yes, I know, and I'm looking at it. It doesn't fail on my Linux or Mac OS X boxes. I'm trying to duplicate the problem. I'm going to try it on my Windows box when I get home in about an hour. I'll fix it tonight.
I realize there's a beer riding on the buildbots being green!
Eric.
Trent Nelson wrote:
This change breaks all the trunk buildbots:
====================================================================== ERROR: testCompileLibrary (test.test_compiler.CompilerTest) ----------------------------------------------------------------------
participants (2)
-
Eric Smith
-
Trent Nelson