Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit db4115f

Browse files
committed
Merged revisions 62039-62042 via svnmerge from
svn+ssh://[email protected]/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. ........
1 parent 9367c78 commit db4115f

6 files changed

Lines changed: 69 additions & 28 deletions

File tree

Include/Python-ast.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -542,5 +542,5 @@ keyword_ty _Py_keyword(identifier arg, expr_ty value, PyArena *arena);
542542
alias_ty _Py_alias(identifier name, identifier asname, PyArena *arena);
543543

544544
PyObject* PyAST_mod2obj(mod_ty t);
545-
mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena);
545+
mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena, int mode);
546546
int PyAST_Check(PyObject* obj);

Lib/test/test_compile.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,20 @@ def test_compile_ast(self):
427427
self.assert_(type(ast) == _ast.Module)
428428
co2 = compile(ast, '%s3' % fname, 'exec')
429429
self.assertEqual(co1, co2)
430+
# the code object's filename comes from the second compilation step
431+
self.assertEqual(co2.co_filename, '%s3' % fname)
432+
433+
# raise exception when node type doesn't match with compile mode
434+
co1 = compile('print(1)', '<string>', 'exec', _ast.PyCF_ONLY_AST)
435+
self.assertRaises(TypeError, compile, co1, '<ast>', 'eval')
436+
437+
# raise exception when node type is no start node
438+
self.assertRaises(TypeError, compile, _ast.If(), '<ast>', 'exec')
439+
440+
# raise exception when node has invalid children
441+
ast = _ast.Module()
442+
ast.body = [_ast.BoolOp()]
443+
self.assertRaises(TypeError, compile, ast, '<ast>', 'exec')
430444

431445

432446
def test_main():

Lib/test/test_socket.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,36 @@
1515
from weakref import proxy
1616
import signal
1717

18+
# Temporary hack to see why test_socket hangs on one buildbot
19+
if os.environ.get('COMPUTERNAME') == "GRAPE":
20+
def verbose_write(arg):
21+
print(arg, file=sys.__stdout__)
22+
else:
23+
def verbose_write(arg):
24+
pass
25+
1826
PORT = 50007
1927
HOST = 'localhost'
2028
MSG = b'Michael Gilfix was here\n'
2129

2230
class SocketTCPTest(unittest.TestCase):
2331

2432
def setUp(self):
33+
verbose_write(self)
2534
self.serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
2635
self.serv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
36+
verbose_write(str(self) + " socket created")
2737
global PORT
2838
PORT = test_support.bind_port(self.serv, HOST, PORT)
39+
verbose_write(str(self) + " start listening")
2940
self.serv.listen(1)
41+
verbose_write(str(self) + " started")
3042

3143
def tearDown(self):
44+
verbose_write(str(self) + " close")
3245
self.serv.close()
3346
self.serv = None
47+
verbose_write(str(self) + " done")
3448

3549
class SocketUDPTest(unittest.TestCase):
3650

Parser/asdl_c.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -934,13 +934,20 @@ class PartingShots(StaticVisitor):
934934
return ast2obj_mod(t);
935935
}
936936
937-
mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena)
937+
/* mode is 0 for "exec", 1 for "eval" and 2 for "single" input */
938+
mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena, int mode)
938939
{
939940
mod_ty res;
941+
PyObject *req_type[] = {(PyObject*)Module_type, (PyObject*)Expression_type,
942+
(PyObject*)Interactive_type};
943+
char *req_name[] = {"Module", "Expression", "Interactive"};
944+
assert(0 <= mode && mode <= 2);
945+
940946
init_types();
941-
if (!PyObject_IsInstance(ast, (PyObject*)mod_type)) {
942-
PyErr_SetString(PyExc_TypeError, "expected either Module, Interactive "
943-
"or Expression node");
947+
948+
if (!PyObject_IsInstance(ast, req_type[mode])) {
949+
PyErr_Format(PyExc_TypeError, "expected %s node, got %.400s",
950+
req_name[mode], Py_TYPE(ast)->tp_name);
944951
return NULL;
945952
}
946953
if (obj2ast_mod(ast, &res, arena) != 0)
@@ -997,8 +1004,8 @@ def main(srcfile):
9971004
)
9981005
c.visit(mod)
9991006
f.write("PyObject* PyAST_mod2obj(mod_ty t);\n")
1000-
print >>f, "mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena);"
1001-
print >>f, "int PyAST_Check(PyObject* obj);"
1007+
f.write("mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena, int mode);\n")
1008+
f.write("int PyAST_Check(PyObject* obj);\n")
10021009
f.close()
10031010

10041011
if SRC_DIR:

Python/Python-ast.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6415,13 +6415,20 @@ PyObject* PyAST_mod2obj(mod_ty t)
64156415
return ast2obj_mod(t);
64166416
}
64176417

6418-
mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena)
6418+
/* mode is 0 for "exec", 1 for "eval" and 2 for "single" input */
6419+
mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena, int mode)
64196420
{
64206421
mod_ty res;
6422+
PyObject *req_type[] = {(PyObject*)Module_type, (PyObject*)Expression_type,
6423+
(PyObject*)Interactive_type};
6424+
char *req_name[] = {"Module", "Expression", "Interactive"};
6425+
assert(0 <= mode && mode <= 2);
6426+
64216427
init_types();
6422-
if (!PyObject_IsInstance(ast, (PyObject*)mod_type)) {
6423-
PyErr_SetString(PyExc_TypeError, "expected either Module, Interactive "
6424-
"or Expression node");
6428+
6429+
if (!PyObject_IsInstance(ast, req_type[mode])) {
6430+
PyErr_Format(PyExc_TypeError, "expected %s node, got %.400s",
6431+
req_name[mode], Py_TYPE(ast)->tp_name);
64256432
return NULL;
64266433
}
64276434
if (obj2ast_mod(ast, &res, arena) != 0)

Python/bltinmodule.c

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -513,13 +513,14 @@ builtin_compile(PyObject *self, PyObject *args, PyObject *kwds)
513513
char *str;
514514
char *filename;
515515
char *startstr;
516-
int start;
516+
int mode = -1;
517517
int dont_inherit = 0;
518518
int supplied_flags = 0;
519519
PyCompilerFlags cf;
520520
PyObject *cmd;
521521
static char *kwlist[] = {"source", "filename", "mode", "flags",
522522
"dont_inherit", NULL};
523+
int start[] = {Py_file_input, Py_eval_input, Py_single_input};
523524

524525
if (!PyArg_ParseTupleAndKeywords(args, kwds, "Oss|ii:compile",
525526
kwlist, &cmd, &filename, &startstr,
@@ -541,6 +542,18 @@ builtin_compile(PyObject *self, PyObject *args, PyObject *kwds)
541542
PyEval_MergeCompilerFlags(&cf);
542543
}
543544

545+
if (strcmp(startstr, "exec") == 0)
546+
mode = 0;
547+
else if (strcmp(startstr, "eval") == 0)
548+
mode = 1;
549+
else if (strcmp(startstr, "single") == 0)
550+
mode = 2;
551+
else {
552+
PyErr_SetString(PyExc_ValueError,
553+
"compile() arg 3 must be 'exec', 'eval' or 'single'");
554+
return NULL;
555+
}
556+
544557
if (PyAST_Check(cmd)) {
545558
PyObject *result;
546559
if (supplied_flags & PyCF_ONLY_AST) {
@@ -552,7 +565,7 @@ builtin_compile(PyObject *self, PyObject *args, PyObject *kwds)
552565
mod_ty mod;
553566

554567
arena = PyArena_New();
555-
mod = PyAST_obj2mod(cmd, arena);
568+
mod = PyAST_obj2mod(cmd, arena, mode);
556569
if (mod == NULL) {
557570
PyArena_Free(arena);
558571
return NULL;
@@ -564,25 +577,11 @@ builtin_compile(PyObject *self, PyObject *args, PyObject *kwds)
564577
return result;
565578
}
566579

567-
/* XXX: is it possible to pass start to the PyAST_ branch? */
568-
if (strcmp(startstr, "exec") == 0)
569-
start = Py_file_input;
570-
else if (strcmp(startstr, "eval") == 0)
571-
start = Py_eval_input;
572-
else if (strcmp(startstr, "single") == 0)
573-
start = Py_single_input;
574-
else {
575-
PyErr_SetString(PyExc_ValueError,
576-
"compile() arg 3 must be 'exec'"
577-
"or 'eval' or 'single'");
578-
return NULL;
579-
}
580-
581580
str = source_as_string(cmd);
582581
if (str == NULL)
583582
return NULL;
584583

585-
return Py_CompileStringFlags(str, filename, start, &cf);
584+
return Py_CompileStringFlags(str, filename, start[mode], &cf);
586585
}
587586

588587
PyDoc_STRVAR(compile_doc,

0 commit comments

Comments
 (0)