]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Merged revisions 62039-62042 via svnmerge from
authorNeal Norwitz <nnorwitz@gmail.com>
Mon, 31 Mar 2008 04:20:05 +0000 (04:20 +0000)
committerNeal Norwitz <nnorwitz@gmail.com>
Mon, 31 Mar 2008 04:20:05 +0000 (04:20 +0000)
svn+ssh://pythondev@svn.python.org/python/trunk

........
  r62039 | georg.brandl | 2008-03-29 06:24:23 -0700 (Sat, 29 Mar 2008) | 3 lines

  Properly check for consistency with the third argument of
  compile() when compiling an AST node.
........
  r62040 | amaury.forgeotdarc | 2008-03-29 06:47:05 -0700 (Sat, 29 Mar 2008) | 5 lines

  The buildbot "x86 W2k8 trunk" seems to hang in test_socket.
  http://www.python.org/dev/buildbot/trunk/x86%20W2k8%20trunk/builds/255/step-test/0

  Temporarily increase verbosity of this test.
........
  r62042 | amaury.forgeotdarc | 2008-03-29 07:53:05 -0700 (Sat, 29 Mar 2008) | 3 lines

  Still investigating on the hanging test_socket.
  the test itself doesn't do anything on windows, focus on setUp and tearDown.
........

Include/Python-ast.h
Lib/test/test_compile.py
Lib/test/test_socket.py
Parser/asdl_c.py
Python/Python-ast.c
Python/bltinmodule.c

index c018ddd933838d6559ff83f57541a1fa81673e99..6653b087ca01036a126273ed8e9f4b2ea5bfc1ed 100644 (file)
@@ -542,5 +542,5 @@ keyword_ty _Py_keyword(identifier arg, expr_ty value, PyArena *arena);
 alias_ty _Py_alias(identifier name, identifier asname, PyArena *arena);
 
 PyObject* PyAST_mod2obj(mod_ty t);
-mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena);
+mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena, int mode);
 int PyAST_Check(PyObject* obj);
index 376369bf566c753510a3dc176bf737959168923b..5a069d36073028b75d5b0ef882078a24353c5595 100644 (file)
@@ -427,6 +427,20 @@ if 1:
             self.assert_(type(ast) == _ast.Module)
             co2 = compile(ast, '%s3' % fname, 'exec')
             self.assertEqual(co1, co2)
+            # the code object's filename comes from the second compilation step
+            self.assertEqual(co2.co_filename, '%s3' % fname)
+
+        # raise exception when node type doesn't match with compile mode
+        co1 = compile('print(1)', '<string>', 'exec', _ast.PyCF_ONLY_AST)
+        self.assertRaises(TypeError, compile, co1, '<ast>', 'eval')
+
+        # raise exception when node type is no start node
+        self.assertRaises(TypeError, compile, _ast.If(), '<ast>', 'exec')
+
+        # raise exception when node has invalid children
+        ast = _ast.Module()
+        ast.body = [_ast.BoolOp()]
+        self.assertRaises(TypeError, compile, ast, '<ast>', 'exec')
 
 
 def test_main():
index 2bec373ed965debece65b4d556d86f0fb97ae0b5..f4abffaa35b56ddf1c7bcc94950a0f683106efaa 100644 (file)
@@ -15,6 +15,14 @@ import array
 from weakref import proxy
 import signal
 
+# Temporary hack to see why test_socket hangs on one buildbot
+if os.environ.get('COMPUTERNAME') == "GRAPE":
+    def verbose_write(arg):
+        print(arg, file=sys.__stdout__)
+else:
+    def verbose_write(arg):
+        pass
+
 PORT = 50007
 HOST = 'localhost'
 MSG = b'Michael Gilfix was here\n'
@@ -22,15 +30,21 @@ MSG = b'Michael Gilfix was here\n'
 class SocketTCPTest(unittest.TestCase):
 
     def setUp(self):
+        verbose_write(self)
         self.serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
         self.serv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
+        verbose_write(str(self) + " socket created")
         global PORT
         PORT = test_support.bind_port(self.serv, HOST, PORT)
+        verbose_write(str(self) + " start listening")
         self.serv.listen(1)
+        verbose_write(str(self) + " started")
 
     def tearDown(self):
+        verbose_write(str(self) + " close")
         self.serv.close()
         self.serv = None
+        verbose_write(str(self) + " done")
 
 class SocketUDPTest(unittest.TestCase):
 
index 94e04b1c02124381643a7d2db5134f21688ee707..1b5e18fd65179d14bbb3c909fb25ed00aee13b22 100755 (executable)
@@ -934,13 +934,20 @@ PyObject* PyAST_mod2obj(mod_ty t)
     return ast2obj_mod(t);
 }
 
-mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena)
+/* mode is 0 for "exec", 1 for "eval" and 2 for "single" input */
+mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena, int mode)
 {
     mod_ty res;
+    PyObject *req_type[] = {(PyObject*)Module_type, (PyObject*)Expression_type,
+                            (PyObject*)Interactive_type};
+    char *req_name[] = {"Module", "Expression", "Interactive"};
+    assert(0 <= mode && mode <= 2);
+
     init_types();
-    if (!PyObject_IsInstance(ast, (PyObject*)mod_type)) {
-        PyErr_SetString(PyExc_TypeError, "expected either Module, Interactive "
-                        "or Expression node");
+
+    if (!PyObject_IsInstance(ast, req_type[mode])) {
+        PyErr_Format(PyExc_TypeError, "expected %s node, got %.400s",
+                     req_name[mode], Py_TYPE(ast)->tp_name);
         return NULL;
     }
     if (obj2ast_mod(ast, &res, arena) != 0)
@@ -997,8 +1004,8 @@ def main(srcfile):
                             )
         c.visit(mod)
         f.write("PyObject* PyAST_mod2obj(mod_ty t);\n")
-        print >>f, "mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena);"
-        print >>f, "int PyAST_Check(PyObject* obj);"
+        f.write("mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena, int mode);\n")
+        f.write("int PyAST_Check(PyObject* obj);\n")
         f.close()
 
     if SRC_DIR:
index 464d28d34b1146ec3d2e1a6ae69a6a1205585a18..9f8558181722900b350b0e999c7f53e5319258b0 100644 (file)
@@ -6415,13 +6415,20 @@ PyObject* PyAST_mod2obj(mod_ty t)
     return ast2obj_mod(t);
 }
 
-mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena)
+/* mode is 0 for "exec", 1 for "eval" and 2 for "single" input */
+mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena, int mode)
 {
     mod_ty res;
+    PyObject *req_type[] = {(PyObject*)Module_type, (PyObject*)Expression_type,
+                            (PyObject*)Interactive_type};
+    char *req_name[] = {"Module", "Expression", "Interactive"};
+    assert(0 <= mode && mode <= 2);
+
     init_types();
-    if (!PyObject_IsInstance(ast, (PyObject*)mod_type)) {
-        PyErr_SetString(PyExc_TypeError, "expected either Module, Interactive "
-                        "or Expression node");
+
+    if (!PyObject_IsInstance(ast, req_type[mode])) {
+        PyErr_Format(PyExc_TypeError, "expected %s node, got %.400s",
+                     req_name[mode], Py_TYPE(ast)->tp_name);
         return NULL;
     }
     if (obj2ast_mod(ast, &res, arena) != 0)
index ccfce068b43e2176abedb8202b7a17c274da4a74..199ebc087de8bab89b143112b961ec78b78a2241 100644 (file)
@@ -513,13 +513,14 @@ builtin_compile(PyObject *self, PyObject *args, PyObject *kwds)
        char *str;
        char *filename;
        char *startstr;
-       int start;
+       int mode = -1;
        int dont_inherit = 0;
        int supplied_flags = 0;
        PyCompilerFlags cf;
        PyObject *cmd;
        static char *kwlist[] = {"source", "filename", "mode", "flags",
                                 "dont_inherit", NULL};
+       int start[] = {Py_file_input, Py_eval_input, Py_single_input};
 
        if (!PyArg_ParseTupleAndKeywords(args, kwds, "Oss|ii:compile",
                                         kwlist, &cmd, &filename, &startstr,
@@ -541,6 +542,18 @@ builtin_compile(PyObject *self, PyObject *args, PyObject *kwds)
                PyEval_MergeCompilerFlags(&cf);
        }
 
+       if (strcmp(startstr, "exec") == 0)
+               mode = 0;
+       else if (strcmp(startstr, "eval") == 0)
+               mode = 1;
+       else if (strcmp(startstr, "single") == 0)
+               mode = 2;
+       else {
+               PyErr_SetString(PyExc_ValueError,
+                               "compile() arg 3 must be 'exec', 'eval' or 'single'");
+               return NULL;
+       }
+
        if (PyAST_Check(cmd)) {
                PyObject *result;
                if (supplied_flags & PyCF_ONLY_AST) {
@@ -552,7 +565,7 @@ builtin_compile(PyObject *self, PyObject *args, PyObject *kwds)
                        mod_ty mod;
 
                        arena = PyArena_New();
-                       mod = PyAST_obj2mod(cmd, arena);
+                       mod = PyAST_obj2mod(cmd, arena, mode);
                        if (mod == NULL) {
                                PyArena_Free(arena);
                                return NULL;
@@ -564,25 +577,11 @@ builtin_compile(PyObject *self, PyObject *args, PyObject *kwds)
                return result;
        }
 
-       /* XXX: is it possible to pass start to the PyAST_ branch? */
-       if (strcmp(startstr, "exec") == 0)
-               start = Py_file_input;
-       else if (strcmp(startstr, "eval") == 0)
-               start = Py_eval_input;
-       else if (strcmp(startstr, "single") == 0)
-               start = Py_single_input;
-       else {
-               PyErr_SetString(PyExc_ValueError,
-                               "compile() arg 3 must be 'exec'"
-                               "or 'eval' or 'single'");
-               return NULL;
-       }
-
        str = source_as_string(cmd);
        if (str == NULL)
                return NULL;
 
-       return Py_CompileStringFlags(str, filename, start, &cf);
+       return Py_CompileStringFlags(str, filename, start[mode], &cf);
 }
 
 PyDoc_STRVAR(compile_doc,