summaryrefslogtreecommitdiff
path: root/Python
diff options
context:
space:
mode:
Diffstat (limited to 'Python')
-rw-r--r--Python/Python-ast.c297
-rw-r--r--Python/ast.c65
-rw-r--r--Python/compile.c182
-rw-r--r--Python/import.c18
-rw-r--r--Python/marshal.c35
-rw-r--r--Python/pythonrun.c2
-rw-r--r--Python/symtable.c29
-rw-r--r--Python/thread_pthread.h12
8 files changed, 330 insertions, 310 deletions
diff --git a/Python/Python-ast.c b/Python/Python-ast.c
index 6b1ea3cbab..b597626463 100644
--- a/Python/Python-ast.c
+++ b/Python/Python-ast.c
@@ -2,7 +2,7 @@
/*
- __version__ 0daa6ba25d9b.
+ __version__ e0e663132363.
This module must be committed separately after each AST grammar change;
The __version__ number is set to the revision number of the commit
@@ -95,8 +95,7 @@ static char *If_fields[]={
};
static PyTypeObject *With_type;
static char *With_fields[]={
- "context_expr",
- "optional_vars",
+ "items",
"body",
};
static PyTypeObject *Raise_type;
@@ -104,15 +103,11 @@ static char *Raise_fields[]={
"exc",
"cause",
};
-static PyTypeObject *TryExcept_type;
-static char *TryExcept_fields[]={
+static PyTypeObject *Try_type;
+static char *Try_fields[]={
"body",
"handlers",
"orelse",
-};
-static PyTypeObject *TryFinally_type;
-static char *TryFinally_fields[]={
- "body",
"finalbody",
};
static PyTypeObject *Assert_type;
@@ -392,6 +387,12 @@ static char *alias_fields[]={
"name",
"asname",
};
+static PyTypeObject *withitem_type;
+static PyObject* ast2obj_withitem(void*);
+static char *withitem_fields[]={
+ "context_expr",
+ "optional_vars",
+};
static int
@@ -680,15 +681,12 @@ static int init_types(void)
if (!While_type) return 0;
If_type = make_type("If", stmt_type, If_fields, 3);
if (!If_type) return 0;
- With_type = make_type("With", stmt_type, With_fields, 3);
+ With_type = make_type("With", stmt_type, With_fields, 2);
if (!With_type) return 0;
Raise_type = make_type("Raise", stmt_type, Raise_fields, 2);
if (!Raise_type) return 0;
- TryExcept_type = make_type("TryExcept", stmt_type, TryExcept_fields, 3);
- if (!TryExcept_type) return 0;
- TryFinally_type = make_type("TryFinally", stmt_type, TryFinally_fields,
- 2);
- if (!TryFinally_type) return 0;
+ Try_type = make_type("Try", stmt_type, Try_fields, 4);
+ if (!Try_type) return 0;
Assert_type = make_type("Assert", stmt_type, Assert_fields, 2);
if (!Assert_type) return 0;
Import_type = make_type("Import", stmt_type, Import_fields, 1);
@@ -938,6 +936,8 @@ static int init_types(void)
if (!keyword_type) return 0;
alias_type = make_type("alias", &AST_type, alias_fields, 2);
if (!alias_type) return 0;
+ withitem_type = make_type("withitem", &AST_type, withitem_fields, 2);
+ if (!withitem_type) return 0;
initialized = 1;
return 1;
}
@@ -960,6 +960,7 @@ static int obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena);
static int obj2ast_arg(PyObject* obj, arg_ty* out, PyArena* arena);
static int obj2ast_keyword(PyObject* obj, keyword_ty* out, PyArena* arena);
static int obj2ast_alias(PyObject* obj, alias_ty* out, PyArena* arena);
+static int obj2ast_withitem(PyObject* obj, withitem_ty* out, PyArena* arena);
mod_ty
Module(asdl_seq * body, PyArena *arena)
@@ -1225,21 +1226,15 @@ If(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno, int
}
stmt_ty
-With(expr_ty context_expr, expr_ty optional_vars, asdl_seq * body, int lineno,
- int col_offset, PyArena *arena)
+With(asdl_seq * items, asdl_seq * body, int lineno, int col_offset, PyArena
+ *arena)
{
stmt_ty p;
- if (!context_expr) {
- PyErr_SetString(PyExc_ValueError,
- "field context_expr is required for With");
- return NULL;
- }
p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
if (!p)
return NULL;
p->kind = With_kind;
- p->v.With.context_expr = context_expr;
- p->v.With.optional_vars = optional_vars;
+ p->v.With.items = items;
p->v.With.body = body;
p->lineno = lineno;
p->col_offset = col_offset;
@@ -1262,33 +1257,18 @@ Raise(expr_ty exc, expr_ty cause, int lineno, int col_offset, PyArena *arena)
}
stmt_ty
-TryExcept(asdl_seq * body, asdl_seq * handlers, asdl_seq * orelse, int lineno,
- int col_offset, PyArena *arena)
+Try(asdl_seq * body, asdl_seq * handlers, asdl_seq * orelse, asdl_seq *
+ finalbody, int lineno, int col_offset, PyArena *arena)
{
stmt_ty p;
p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
if (!p)
return NULL;
- p->kind = TryExcept_kind;
- p->v.TryExcept.body = body;
- p->v.TryExcept.handlers = handlers;
- p->v.TryExcept.orelse = orelse;
- p->lineno = lineno;
- p->col_offset = col_offset;
- return p;
-}
-
-stmt_ty
-TryFinally(asdl_seq * body, asdl_seq * finalbody, int lineno, int col_offset,
- PyArena *arena)
-{
- stmt_ty p;
- p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
- if (!p)
- return NULL;
- p->kind = TryFinally_kind;
- p->v.TryFinally.body = body;
- p->v.TryFinally.finalbody = finalbody;
+ p->kind = Try_kind;
+ p->v.Try.body = body;
+ p->v.Try.handlers = handlers;
+ p->v.Try.orelse = orelse;
+ p->v.Try.finalbody = finalbody;
p->lineno = lineno;
p->col_offset = col_offset;
return p;
@@ -2135,6 +2115,23 @@ alias(identifier name, identifier asname, PyArena *arena)
return p;
}
+withitem_ty
+withitem(expr_ty context_expr, expr_ty optional_vars, PyArena *arena)
+{
+ withitem_ty p;
+ if (!context_expr) {
+ PyErr_SetString(PyExc_ValueError,
+ "field context_expr is required for withitem");
+ return NULL;
+ }
+ p = (withitem_ty)PyArena_Malloc(arena, sizeof(*p));
+ if (!p)
+ return NULL;
+ p->context_expr = context_expr;
+ p->optional_vars = optional_vars;
+ return p;
+}
+
PyObject*
ast2obj_mod(void* _o)
@@ -2390,15 +2387,9 @@ ast2obj_stmt(void* _o)
case With_kind:
result = PyType_GenericNew(With_type, NULL, NULL);
if (!result) goto failed;
- value = ast2obj_expr(o->v.With.context_expr);
+ value = ast2obj_list(o->v.With.items, ast2obj_withitem);
if (!value) goto failed;
- if (PyObject_SetAttrString(result, "context_expr", value) == -1)
- goto failed;
- Py_DECREF(value);
- value = ast2obj_expr(o->v.With.optional_vars);
- if (!value) goto failed;
- if (PyObject_SetAttrString(result, "optional_vars", value) ==
- -1)
+ if (PyObject_SetAttrString(result, "items", value) == -1)
goto failed;
Py_DECREF(value);
value = ast2obj_list(o->v.With.body, ast2obj_stmt);
@@ -2421,35 +2412,25 @@ ast2obj_stmt(void* _o)
goto failed;
Py_DECREF(value);
break;
- case TryExcept_kind:
- result = PyType_GenericNew(TryExcept_type, NULL, NULL);
+ case Try_kind:
+ result = PyType_GenericNew(Try_type, NULL, NULL);
if (!result) goto failed;
- value = ast2obj_list(o->v.TryExcept.body, ast2obj_stmt);
+ value = ast2obj_list(o->v.Try.body, ast2obj_stmt);
if (!value) goto failed;
if (PyObject_SetAttrString(result, "body", value) == -1)
goto failed;
Py_DECREF(value);
- value = ast2obj_list(o->v.TryExcept.handlers,
- ast2obj_excepthandler);
+ value = ast2obj_list(o->v.Try.handlers, ast2obj_excepthandler);
if (!value) goto failed;
if (PyObject_SetAttrString(result, "handlers", value) == -1)
goto failed;
Py_DECREF(value);
- value = ast2obj_list(o->v.TryExcept.orelse, ast2obj_stmt);
+ value = ast2obj_list(o->v.Try.orelse, ast2obj_stmt);
if (!value) goto failed;
if (PyObject_SetAttrString(result, "orelse", value) == -1)
goto failed;
Py_DECREF(value);
- break;
- case TryFinally_kind:
- result = PyType_GenericNew(TryFinally_type, NULL, NULL);
- if (!result) goto failed;
- value = ast2obj_list(o->v.TryFinally.body, ast2obj_stmt);
- if (!value) goto failed;
- if (PyObject_SetAttrString(result, "body", value) == -1)
- goto failed;
- Py_DECREF(value);
- value = ast2obj_list(o->v.TryFinally.finalbody, ast2obj_stmt);
+ value = ast2obj_list(o->v.Try.finalbody, ast2obj_stmt);
if (!value) goto failed;
if (PyObject_SetAttrString(result, "finalbody", value) == -1)
goto failed;
@@ -3370,6 +3351,35 @@ failed:
return NULL;
}
+PyObject*
+ast2obj_withitem(void* _o)
+{
+ withitem_ty o = (withitem_ty)_o;
+ PyObject *result = NULL, *value = NULL;
+ if (!o) {
+ Py_INCREF(Py_None);
+ return Py_None;
+ }
+
+ result = PyType_GenericNew(withitem_type, NULL, NULL);
+ if (!result) return NULL;
+ value = ast2obj_expr(o->context_expr);
+ if (!value) goto failed;
+ if (PyObject_SetAttrString(result, "context_expr", value) == -1)
+ goto failed;
+ Py_DECREF(value);
+ value = ast2obj_expr(o->optional_vars);
+ if (!value) goto failed;
+ if (PyObject_SetAttrString(result, "optional_vars", value) == -1)
+ goto failed;
+ Py_DECREF(value);
+ return result;
+failed:
+ Py_XDECREF(value);
+ Py_XDECREF(result);
+ return NULL;
+}
+
int
obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena)
@@ -4210,33 +4220,34 @@ obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)
return 1;
}
if (isinstance) {
- expr_ty context_expr;
- expr_ty optional_vars;
+ asdl_seq* items;
asdl_seq* body;
- if (PyObject_HasAttrString(obj, "context_expr")) {
+ if (PyObject_HasAttrString(obj, "items")) {
int res;
- tmp = PyObject_GetAttrString(obj, "context_expr");
+ Py_ssize_t len;
+ Py_ssize_t i;
+ tmp = PyObject_GetAttrString(obj, "items");
if (tmp == NULL) goto failed;
- res = obj2ast_expr(tmp, &context_expr, arena);
- if (res != 0) goto failed;
+ if (!PyList_Check(tmp)) {
+ PyErr_Format(PyExc_TypeError, "With field \"items\" must be a list, not a %.200s", tmp->ob_type->tp_name);
+ goto failed;
+ }
+ len = PyList_GET_SIZE(tmp);
+ items = asdl_seq_new(len, arena);
+ if (items == NULL) goto failed;
+ for (i = 0; i < len; i++) {
+ withitem_ty value;
+ res = obj2ast_withitem(PyList_GET_ITEM(tmp, i), &value, arena);
+ if (res != 0) goto failed;
+ asdl_seq_SET(items, i, value);
+ }
Py_XDECREF(tmp);
tmp = NULL;
} else {
- PyErr_SetString(PyExc_TypeError, "required field \"context_expr\" missing from With");
+ PyErr_SetString(PyExc_TypeError, "required field \"items\" missing from With");
return 1;
}
- if (PyObject_HasAttrString(obj, "optional_vars")) {
- int res;
- tmp = PyObject_GetAttrString(obj, "optional_vars");
- if (tmp == NULL) goto failed;
- res = obj2ast_expr(tmp, &optional_vars, arena);
- if (res != 0) goto failed;
- Py_XDECREF(tmp);
- tmp = NULL;
- } else {
- optional_vars = NULL;
- }
if (PyObject_HasAttrString(obj, "body")) {
int res;
Py_ssize_t len;
@@ -4262,8 +4273,7 @@ obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)
PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from With");
return 1;
}
- *out = With(context_expr, optional_vars, body, lineno,
- col_offset, arena);
+ *out = With(items, body, lineno, col_offset, arena);
if (*out == NULL) goto failed;
return 0;
}
@@ -4301,7 +4311,7 @@ obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)
if (*out == NULL) goto failed;
return 0;
}
- isinstance = PyObject_IsInstance(obj, (PyObject*)TryExcept_type);
+ isinstance = PyObject_IsInstance(obj, (PyObject*)Try_type);
if (isinstance == -1) {
return 1;
}
@@ -4309,6 +4319,7 @@ obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)
asdl_seq* body;
asdl_seq* handlers;
asdl_seq* orelse;
+ asdl_seq* finalbody;
if (PyObject_HasAttrString(obj, "body")) {
int res;
@@ -4317,7 +4328,7 @@ obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)
tmp = PyObject_GetAttrString(obj, "body");
if (tmp == NULL) goto failed;
if (!PyList_Check(tmp)) {
- PyErr_Format(PyExc_TypeError, "TryExcept field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
+ PyErr_Format(PyExc_TypeError, "Try field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
goto failed;
}
len = PyList_GET_SIZE(tmp);
@@ -4332,7 +4343,7 @@ obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)
Py_XDECREF(tmp);
tmp = NULL;
} else {
- PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from TryExcept");
+ PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Try");
return 1;
}
if (PyObject_HasAttrString(obj, "handlers")) {
@@ -4342,7 +4353,7 @@ obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)
tmp = PyObject_GetAttrString(obj, "handlers");
if (tmp == NULL) goto failed;
if (!PyList_Check(tmp)) {
- PyErr_Format(PyExc_TypeError, "TryExcept field \"handlers\" must be a list, not a %.200s", tmp->ob_type->tp_name);
+ PyErr_Format(PyExc_TypeError, "Try field \"handlers\" must be a list, not a %.200s", tmp->ob_type->tp_name);
goto failed;
}
len = PyList_GET_SIZE(tmp);
@@ -4357,7 +4368,7 @@ obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)
Py_XDECREF(tmp);
tmp = NULL;
} else {
- PyErr_SetString(PyExc_TypeError, "required field \"handlers\" missing from TryExcept");
+ PyErr_SetString(PyExc_TypeError, "required field \"handlers\" missing from Try");
return 1;
}
if (PyObject_HasAttrString(obj, "orelse")) {
@@ -4367,7 +4378,7 @@ obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)
tmp = PyObject_GetAttrString(obj, "orelse");
if (tmp == NULL) goto failed;
if (!PyList_Check(tmp)) {
- PyErr_Format(PyExc_TypeError, "TryExcept field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
+ PyErr_Format(PyExc_TypeError, "Try field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
goto failed;
}
len = PyList_GET_SIZE(tmp);
@@ -4382,45 +4393,7 @@ obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)
Py_XDECREF(tmp);
tmp = NULL;
} else {
- PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from TryExcept");
- return 1;
- }
- *out = TryExcept(body, handlers, orelse, lineno, col_offset,
- arena);
- if (*out == NULL) goto failed;
- return 0;
- }
- isinstance = PyObject_IsInstance(obj, (PyObject*)TryFinally_type);
- if (isinstance == -1) {
- return 1;
- }
- if (isinstance) {
- asdl_seq* body;
- asdl_seq* finalbody;
-
- if (PyObject_HasAttrString(obj, "body")) {
- int res;
- Py_ssize_t len;
- Py_ssize_t i;
- tmp = PyObject_GetAttrString(obj, "body");
- if (tmp == NULL) goto failed;
- if (!PyList_Check(tmp)) {
- PyErr_Format(PyExc_TypeError, "TryFinally field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
- goto failed;
- }
- len = PyList_GET_SIZE(tmp);
- body = asdl_seq_new(len, arena);
- if (body == NULL) goto failed;
- for (i = 0; i < len; i++) {
- stmt_ty value;
- res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
- if (res != 0) goto failed;
- asdl_seq_SET(body, i, value);
- }
- Py_XDECREF(tmp);
- tmp = NULL;
- } else {
- PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from TryFinally");
+ PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from Try");
return 1;
}
if (PyObject_HasAttrString(obj, "finalbody")) {
@@ -4430,7 +4403,7 @@ obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)
tmp = PyObject_GetAttrString(obj, "finalbody");
if (tmp == NULL) goto failed;
if (!PyList_Check(tmp)) {
- PyErr_Format(PyExc_TypeError, "TryFinally field \"finalbody\" must be a list, not a %.200s", tmp->ob_type->tp_name);
+ PyErr_Format(PyExc_TypeError, "Try field \"finalbody\" must be a list, not a %.200s", tmp->ob_type->tp_name);
goto failed;
}
len = PyList_GET_SIZE(tmp);
@@ -4445,10 +4418,11 @@ obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)
Py_XDECREF(tmp);
tmp = NULL;
} else {
- PyErr_SetString(PyExc_TypeError, "required field \"finalbody\" missing from TryFinally");
+ PyErr_SetString(PyExc_TypeError, "required field \"finalbody\" missing from Try");
return 1;
}
- *out = TryFinally(body, finalbody, lineno, col_offset, arena);
+ *out = Try(body, handlers, orelse, finalbody, lineno,
+ col_offset, arena);
if (*out == NULL) goto failed;
return 0;
}
@@ -6723,6 +6697,43 @@ failed:
return 1;
}
+int
+obj2ast_withitem(PyObject* obj, withitem_ty* out, PyArena* arena)
+{
+ PyObject* tmp = NULL;
+ expr_ty context_expr;
+ expr_ty optional_vars;
+
+ if (PyObject_HasAttrString(obj, "context_expr")) {
+ int res;
+ tmp = PyObject_GetAttrString(obj, "context_expr");
+ if (tmp == NULL) goto failed;
+ res = obj2ast_expr(tmp, &context_expr, arena);
+ if (res != 0) goto failed;
+ Py_XDECREF(tmp);
+ tmp = NULL;
+ } else {
+ PyErr_SetString(PyExc_TypeError, "required field \"context_expr\" missing from withitem");
+ return 1;
+ }
+ if (PyObject_HasAttrString(obj, "optional_vars")) {
+ int res;
+ tmp = PyObject_GetAttrString(obj, "optional_vars");
+ if (tmp == NULL) goto failed;
+ res = obj2ast_expr(tmp, &optional_vars, arena);
+ if (res != 0) goto failed;
+ Py_XDECREF(tmp);
+ tmp = NULL;
+ } else {
+ optional_vars = NULL;
+ }
+ *out = withitem(context_expr, optional_vars, arena);
+ return 0;
+failed:
+ Py_XDECREF(tmp);
+ return 1;
+}
+
static struct PyModuleDef _astmodule = {
PyModuleDef_HEAD_INIT, "_ast"
@@ -6739,7 +6750,7 @@ PyInit__ast(void)
NULL;
if (PyModule_AddIntConstant(m, "PyCF_ONLY_AST", PyCF_ONLY_AST) < 0)
return NULL;
- if (PyModule_AddStringConstant(m, "__version__", "0daa6ba25d9b") < 0)
+ if (PyModule_AddStringConstant(m, "__version__", "e0e663132363") < 0)
return NULL;
if (PyDict_SetItemString(d, "mod", (PyObject*)mod_type) < 0) return
NULL;
@@ -6774,10 +6785,8 @@ PyInit__ast(void)
NULL;
if (PyDict_SetItemString(d, "Raise", (PyObject*)Raise_type) < 0) return
NULL;
- if (PyDict_SetItemString(d, "TryExcept", (PyObject*)TryExcept_type) <
- 0) return NULL;
- if (PyDict_SetItemString(d, "TryFinally", (PyObject*)TryFinally_type) <
- 0) return NULL;
+ if (PyDict_SetItemString(d, "Try", (PyObject*)Try_type) < 0) return
+ NULL;
if (PyDict_SetItemString(d, "Assert", (PyObject*)Assert_type) < 0)
return NULL;
if (PyDict_SetItemString(d, "Import", (PyObject*)Import_type) < 0)
@@ -6940,6 +6949,8 @@ PyInit__ast(void)
return NULL;
if (PyDict_SetItemString(d, "alias", (PyObject*)alias_type) < 0) return
NULL;
+ if (PyDict_SetItemString(d, "withitem", (PyObject*)withitem_type) < 0)
+ return NULL;
return m;
}
diff --git a/Python/ast.c b/Python/ast.c
index 5b12da80f2..485b7d64d2 100644
--- a/Python/ast.c
+++ b/Python/ast.c
@@ -2893,7 +2893,7 @@ ast_for_try_stmt(struct compiling *c, const node *n)
{
const int nch = NCH(n);
int n_except = (nch - 3)/3;
- asdl_seq *body, *orelse = NULL, *finally = NULL;
+ asdl_seq *body, *handlers = NULL, *orelse = NULL, *finally = NULL;
REQ(n, try_stmt);
@@ -2934,9 +2934,8 @@ ast_for_try_stmt(struct compiling *c, const node *n)
if (n_except > 0) {
int i;
- stmt_ty except_st;
/* process except statements to create a try ... except */
- asdl_seq *handlers = asdl_seq_new(n_except, c->c_arena);
+ handlers = asdl_seq_new(n_except, c->c_arena);
if (handlers == NULL)
return NULL;
@@ -2947,28 +2946,15 @@ ast_for_try_stmt(struct compiling *c, const node *n)
return NULL;
asdl_seq_SET(handlers, i, e);
}
-
- except_st = TryExcept(body, handlers, orelse, LINENO(n),
- n->n_col_offset, c->c_arena);
- if (!finally)
- return except_st;
-
- /* if a 'finally' is present too, we nest the TryExcept within a
- TryFinally to emulate try ... except ... finally */
- body = asdl_seq_new(1, c->c_arena);
- if (body == NULL)
- return NULL;
- asdl_seq_SET(body, 0, except_st);
}
- /* must be a try ... finally (except clauses are in body, if any exist) */
- assert(finally != NULL);
- return TryFinally(body, finally, LINENO(n), n->n_col_offset, c->c_arena);
+ assert(finally != NULL || asdl_seq_LEN(handlers));
+ return Try(body, handlers, orelse, finally, LINENO(n), n->n_col_offset, c->c_arena);
}
/* with_item: test ['as' expr] */
-static stmt_ty
-ast_for_with_item(struct compiling *c, const node *n, asdl_seq *content)
+static withitem_ty
+ast_for_with_item(struct compiling *c, const node *n)
{
expr_ty context_expr, optional_vars = NULL;
@@ -2987,43 +2973,32 @@ ast_for_with_item(struct compiling *c, const node *n, asdl_seq *content)
}
}
- return With(context_expr, optional_vars, content, LINENO(n),
- n->n_col_offset, c->c_arena);
+ return withitem(context_expr, optional_vars, c->c_arena);
}
/* with_stmt: 'with' with_item (',' with_item)* ':' suite */
static stmt_ty
ast_for_with_stmt(struct compiling *c, const node *n)
{
- int i;
- stmt_ty ret;
- asdl_seq *inner;
+ int i, n_items;
+ asdl_seq *items, *body;
REQ(n, with_stmt);
- /* process the with items inside-out */
- i = NCH(n) - 1;
- /* the suite of the innermost with item is the suite of the with stmt */
- inner = ast_for_suite(c, CHILD(n, i));
- if (!inner)
- return NULL;
-
- for (;;) {
- i -= 2;
- ret = ast_for_with_item(c, CHILD(n, i), inner);
- if (!ret)
- return NULL;
- /* was this the last item? */
- if (i == 1)
- break;
- /* if not, wrap the result so far in a new sequence */
- inner = asdl_seq_new(1, c->c_arena);
- if (!inner)
+ n_items = (NCH(n) - 2) / 2;
+ items = asdl_seq_new(n_items, c->c_arena);
+ for (i = 1; i < NCH(n) - 2; i += 2) {
+ withitem_ty item = ast_for_with_item(c, CHILD(n, i));
+ if (!item)
return NULL;
- asdl_seq_SET(inner, 0, ret);
+ asdl_seq_SET(items, (i - 1) / 2, item);
}
- return ret;
+ body = ast_for_suite(c, CHILD(n, NCH(n) - 1));
+ if (!body)
+ return NULL;
+
+ return With(items, body, LINENO(n), n->n_col_offset, c->c_arena);
}
static stmt_ty
diff --git a/Python/compile.c b/Python/compile.c
index 53f5a12cc3..b655c2523a 100644
--- a/Python/compile.c
+++ b/Python/compile.c
@@ -135,6 +135,7 @@ managed by compiler_enter_scope() and compiler_exit_scope().
struct compiler {
const char *c_filename;
+ PyObject *c_filename_obj;
struct symtable *c_st;
PyFutureFeatures *c_future; /* pointer to module's __future__ */
PyCompilerFlags *c_flags;
@@ -178,12 +179,13 @@ static int compiler_in_loop(struct compiler *);
static int inplace_binop(struct compiler *, operator_ty);
static int expr_constant(struct compiler *, expr_ty);
-static int compiler_with(struct compiler *, stmt_ty);
+static int compiler_with(struct compiler *, stmt_ty, int);
static int compiler_call_helper(struct compiler *c, int n,
asdl_seq *args,
asdl_seq *keywords,
expr_ty starargs,
expr_ty kwargs);
+static int compiler_try_except(struct compiler *, stmt_ty);
static PyCodeObject *assemble(struct compiler *, int addNone);
static PyObject *__doc__;
@@ -272,6 +274,9 @@ PyAST_CompileEx(mod_ty mod, const char *filename, PyCompilerFlags *flags,
if (!compiler_init(&c))
return NULL;
c.c_filename = filename;
+ c.c_filename_obj = PyUnicode_DecodeFSDefault(filename);
+ if (!c.c_filename_obj)
+ goto finally;
c.c_arena = arena;
c.c_future = PyFuture_FromAST(mod, filename);
if (c.c_future == NULL)
@@ -324,6 +329,8 @@ compiler_free(struct compiler *c)
PySymtable_Free(c->c_st);
if (c->c_future)
PyObject_Free(c->c_future);
+ if (c->c_filename_obj)
+ Py_DECREF(c->c_filename_obj);
Py_DECREF(c->c_stack);
}
@@ -1892,7 +1899,13 @@ compiler_try_finally(struct compiler *c, stmt_ty s)
compiler_use_next_block(c, body);
if (!compiler_push_fblock(c, FINALLY_TRY, body))
return 0;
- VISIT_SEQ(c, stmt, s->v.TryFinally.body);
+ if (s->v.Try.handlers && asdl_seq_LEN(s->v.Try.handlers)) {
+ if (!compiler_try_except(c, s))
+ return 0;
+ }
+ else {
+ VISIT_SEQ(c, stmt, s->v.Try.body);
+ }
ADDOP(c, POP_BLOCK);
compiler_pop_fblock(c, FINALLY_TRY, body);
@@ -1900,7 +1913,7 @@ compiler_try_finally(struct compiler *c, stmt_ty s)
compiler_use_next_block(c, end);
if (!compiler_push_fblock(c, FINALLY_END, end))
return 0;
- VISIT_SEQ(c, stmt, s->v.TryFinally.finalbody);
+ VISIT_SEQ(c, stmt, s->v.Try.finalbody);
ADDOP(c, END_FINALLY);
compiler_pop_fblock(c, FINALLY_END, end);
@@ -1954,15 +1967,15 @@ compiler_try_except(struct compiler *c, stmt_ty s)
compiler_use_next_block(c, body);
if (!compiler_push_fblock(c, EXCEPT, body))
return 0;
- VISIT_SEQ(c, stmt, s->v.TryExcept.body);
+ VISIT_SEQ(c, stmt, s->v.Try.body);
ADDOP(c, POP_BLOCK);
compiler_pop_fblock(c, EXCEPT, body);
ADDOP_JREL(c, JUMP_FORWARD, orelse);
- n = asdl_seq_LEN(s->v.TryExcept.handlers);
+ n = asdl_seq_LEN(s->v.Try.handlers);
compiler_use_next_block(c, except);
for (i = 0; i < n; i++) {
excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
- s->v.TryExcept.handlers, i);
+ s->v.Try.handlers, i);
if (!handler->v.ExceptHandler.type && i < n-1)
return compiler_error(c, "default 'except:' must be last");
c->u->u_lineno_set = 0;
@@ -1979,82 +1992,91 @@ compiler_try_except(struct compiler *c, stmt_ty s)
}
ADDOP(c, POP_TOP);
if (handler->v.ExceptHandler.name) {
- basicblock *cleanup_end, *cleanup_body;
+ basicblock *cleanup_end, *cleanup_body;
- cleanup_end = compiler_new_block(c);
- cleanup_body = compiler_new_block(c);
- if(!(cleanup_end || cleanup_body))
- return 0;
-
- compiler_nameop(c, handler->v.ExceptHandler.name, Store);
- ADDOP(c, POP_TOP);
+ cleanup_end = compiler_new_block(c);
+ cleanup_body = compiler_new_block(c);
+ if (!(cleanup_end || cleanup_body))
+ return 0;
- /*
- try:
- # body
- except type as name:
- try:
- # body
- finally:
- name = None
- del name
- */
+ compiler_nameop(c, handler->v.ExceptHandler.name, Store);
+ ADDOP(c, POP_TOP);
- /* second try: */
- ADDOP_JREL(c, SETUP_FINALLY, cleanup_end);
- compiler_use_next_block(c, cleanup_body);
- if (!compiler_push_fblock(c, FINALLY_TRY, cleanup_body))
- return 0;
+ /*
+ try:
+ # body
+ except type as name:
+ try:
+ # body
+ finally:
+ name = None
+ del name
+ */
+
+ /* second try: */
+ ADDOP_JREL(c, SETUP_FINALLY, cleanup_end);
+ compiler_use_next_block(c, cleanup_body);
+ if (!compiler_push_fblock(c, FINALLY_TRY, cleanup_body))
+ return 0;
- /* second # body */
- VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
- ADDOP(c, POP_BLOCK);
- ADDOP(c, POP_EXCEPT);
- compiler_pop_fblock(c, FINALLY_TRY, cleanup_body);
+ /* second # body */
+ VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
+ ADDOP(c, POP_BLOCK);
+ ADDOP(c, POP_EXCEPT);
+ compiler_pop_fblock(c, FINALLY_TRY, cleanup_body);
- /* finally: */
- ADDOP_O(c, LOAD_CONST, Py_None, consts);
- compiler_use_next_block(c, cleanup_end);
- if (!compiler_push_fblock(c, FINALLY_END, cleanup_end))
- return 0;
+ /* finally: */
+ ADDOP_O(c, LOAD_CONST, Py_None, consts);
+ compiler_use_next_block(c, cleanup_end);
+ if (!compiler_push_fblock(c, FINALLY_END, cleanup_end))
+ return 0;
- /* name = None */
- ADDOP_O(c, LOAD_CONST, Py_None, consts);
- compiler_nameop(c, handler->v.ExceptHandler.name, Store);
+ /* name = None */
+ ADDOP_O(c, LOAD_CONST, Py_None, consts);
+ compiler_nameop(c, handler->v.ExceptHandler.name, Store);
- /* del name */
- compiler_nameop(c, handler->v.ExceptHandler.name, Del);
+ /* del name */
+ compiler_nameop(c, handler->v.ExceptHandler.name, Del);
- ADDOP(c, END_FINALLY);
- compiler_pop_fblock(c, FINALLY_END, cleanup_end);
+ ADDOP(c, END_FINALLY);
+ compiler_pop_fblock(c, FINALLY_END, cleanup_end);
}
else {
- basicblock *cleanup_body;
+ basicblock *cleanup_body;
- cleanup_body = compiler_new_block(c);
- if(!cleanup_body)
- return 0;
+ cleanup_body = compiler_new_block(c);
+ if (!cleanup_body)
+ return 0;
ADDOP(c, POP_TOP);
- ADDOP(c, POP_TOP);
- compiler_use_next_block(c, cleanup_body);
- if (!compiler_push_fblock(c, FINALLY_TRY, cleanup_body))
- return 0;
+ ADDOP(c, POP_TOP);
+ compiler_use_next_block(c, cleanup_body);
+ if (!compiler_push_fblock(c, FINALLY_TRY, cleanup_body))
+ return 0;
VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
- ADDOP(c, POP_EXCEPT);
- compiler_pop_fblock(c, FINALLY_TRY, cleanup_body);
+ ADDOP(c, POP_EXCEPT);
+ compiler_pop_fblock(c, FINALLY_TRY, cleanup_body);
}
ADDOP_JREL(c, JUMP_FORWARD, end);
compiler_use_next_block(c, except);
}
ADDOP(c, END_FINALLY);
compiler_use_next_block(c, orelse);
- VISIT_SEQ(c, stmt, s->v.TryExcept.orelse);
+ VISIT_SEQ(c, stmt, s->v.Try.orelse);
compiler_use_next_block(c, end);
return 1;
}
static int
+compiler_try(struct compiler *c, stmt_ty s) {
+ if (s->v.Try.finalbody && asdl_seq_LEN(s->v.Try.finalbody))
+ return compiler_try_finally(c, s);
+ else
+ return compiler_try_except(c, s);
+}
+
+
+static int
compiler_import_as(struct compiler *c, identifier name, identifier asname)
{
/* The IMPORT_NAME opcode was already generated. This function
@@ -2301,10 +2323,8 @@ compiler_visit_stmt(struct compiler *c, stmt_ty s)
}
ADDOP_I(c, RAISE_VARARGS, n);
break;
- case TryExcept_kind:
- return compiler_try_except(c, s);
- case TryFinally_kind:
- return compiler_try_finally(c, s);
+ case Try_kind:
+ return compiler_try(c, s);
case Assert_kind:
return compiler_assert(c, s);
case Import_kind:
@@ -2335,7 +2355,7 @@ compiler_visit_stmt(struct compiler *c, stmt_ty s)
case Continue_kind:
return compiler_continue(c);
case With_kind:
- return compiler_with(c, s);
+ return compiler_with(c, s, 0);
}
return 1;
}
@@ -3062,9 +3082,10 @@ expr_constant(struct compiler *c, expr_ty e)
exit(*exc)
*/
static int
-compiler_with(struct compiler *c, stmt_ty s)
+compiler_with(struct compiler *c, stmt_ty s, int pos)
{
basicblock *block, *finally;
+ withitem_ty item = asdl_seq_GET(s->v.With.items, pos);
assert(s->kind == With_kind);
@@ -3074,7 +3095,7 @@ compiler_with(struct compiler *c, stmt_ty s)
return 0;
/* Evaluate EXPR */
- VISIT(c, expr, s->v.With.context_expr);
+ VISIT(c, expr, item->context_expr);
ADDOP_JREL(c, SETUP_WITH, finally);
/* SETUP_WITH pushes a finally block. */
@@ -3083,16 +3104,20 @@ compiler_with(struct compiler *c, stmt_ty s)
return 0;
}
- if (s->v.With.optional_vars) {
- VISIT(c, expr, s->v.With.optional_vars);
+ if (item->optional_vars) {
+ VISIT(c, expr, item->optional_vars);
}
else {
/* Discard result from context.__enter__() */
ADDOP(c, POP_TOP);
}
- /* BLOCK code */
- VISIT_SEQ(c, stmt, s->v.With.body);
+ pos++;
+ if (pos == asdl_seq_LEN(s->v.With.items))
+ /* BLOCK code */
+ VISIT_SEQ(c, stmt, s->v.With.body)
+ else if (!compiler_with(c, s, pos))
+ return 0;
/* End of try block; start the finally block */
ADDOP(c, POP_BLOCK);
@@ -3361,7 +3386,7 @@ compiler_in_loop(struct compiler *c) {
static int
compiler_error(struct compiler *c, const char *errstr)
{
- PyObject *loc, *filename;
+ PyObject *loc;
PyObject *u = NULL, *v = NULL;
loc = PyErr_ProgramText(c->c_filename, c->u->u_lineno);
@@ -3369,16 +3394,7 @@ compiler_error(struct compiler *c, const char *errstr)
Py_INCREF(Py_None);
loc = Py_None;
}
- if (c->c_filename != NULL) {
- filename = PyUnicode_DecodeFSDefault(c->c_filename);
- if (!filename)
- goto exit;
- }
- else {
- Py_INCREF(Py_None);
- filename = Py_None;
- }
- u = Py_BuildValue("(NiiO)", filename, c->u->u_lineno,
+ u = Py_BuildValue("(OiiO)", c->c_filename_obj, c->u->u_lineno,
c->u->u_col_offset, loc);
if (!u)
goto exit;
@@ -3927,7 +3943,6 @@ makecode(struct compiler *c, struct assembler *a)
PyObject *consts = NULL;
PyObject *names = NULL;
PyObject *varnames = NULL;
- PyObject *filename = NULL;
PyObject *name = NULL;
PyObject *freevars = NULL;
PyObject *cellvars = NULL;
@@ -3951,10 +3966,6 @@ makecode(struct compiler *c, struct assembler *a)
freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_Size(cellvars));
if (!freevars)
goto error;
- filename = PyUnicode_DecodeFSDefault(c->c_filename);
- if (!filename)
- goto error;
-
nlocals = PyDict_Size(c->u->u_varnames);
flags = compute_code_flags(c);
if (flags < 0)
@@ -3974,14 +3985,13 @@ makecode(struct compiler *c, struct assembler *a)
nlocals, stackdepth(c), flags,
bytecode, consts, names, varnames,
freevars, cellvars,
- filename, c->u->u_name,
+ c->c_filename_obj, c->u->u_name,
c->u->u_firstlineno,
a->a_lnotab);
error:
Py_XDECREF(consts);
Py_XDECREF(names);
Py_XDECREF(varnames);
- Py_XDECREF(filename);
Py_XDECREF(name);
Py_XDECREF(freevars);
Py_XDECREF(cellvars);
diff --git a/Python/import.c b/Python/import.c
index 5360d57c51..aaf649f858 100644
--- a/Python/import.c
+++ b/Python/import.c
@@ -110,8 +110,12 @@ typedef unsigned short mode_t;
TAG and PYC_TAG_UNICODE must change for each major Python release. The magic
number will take care of any bytecode changes that occur during development.
*/
+#define QUOTE(arg) #arg
+#define STRIFY(name) QUOTE(name)
+#define MAJOR STRIFY(PY_MAJOR_VERSION)
+#define MINOR STRIFY(PY_MINOR_VERSION)
#define MAGIC (3180 | ((long)'\r'<<16) | ((long)'\n'<<24))
-#define TAG "cpython-32"
+#define TAG "cpython-" MAJOR MINOR;
#define CACHEDIR "__pycache__"
static const Py_UNICODE CACHEDIR_UNICODE[] = {
'_', '_', 'p', 'y', 'c', 'a', 'c', 'h', 'e', '_', '_', '\0'};
@@ -119,7 +123,11 @@ static const Py_UNICODE CACHEDIR_UNICODE[] = {
static long pyc_magic = MAGIC;
static const char *pyc_tag = TAG;
static const Py_UNICODE PYC_TAG_UNICODE[] = {
- 'c', 'p', 'y', 't', 'h', 'o', 'n', '-', '3', '2', '\0'};
+ 'c', 'p', 'y', 't', 'h', 'o', 'n', '-', PY_MAJOR_VERSION + 48, PY_MINOR_VERSION + 48, '\0'};
+#undef QUOTE
+#undef STRIFY
+#undef MAJOR
+#undef MINOR
/* See _PyImport_FixupExtensionObject() below */
static PyObject *extensions = NULL;
@@ -1733,7 +1741,6 @@ find_module_path(PyObject *fullname, PyObject *name, PyObject *path,
Py_UNICODE buf[MAXPATHLEN+1];
Py_ssize_t buflen = MAXPATHLEN+1;
PyObject *path_unicode, *filename;
- const Py_UNICODE *base;
Py_ssize_t len;
struct stat statbuf;
static struct filedescr fd_package = {"", "", PKG_DIRECTORY};
@@ -1751,7 +1758,6 @@ find_module_path(PyObject *fullname, PyObject *name, PyObject *path,
else
return 0;
- base = PyUnicode_AS_UNICODE(path_unicode);
len = PyUnicode_GET_SIZE(path_unicode);
if (len + 2 + PyUnicode_GET_SIZE(name) + MAXSUFFIXSIZE >= buflen) {
Py_DECREF(path_unicode);
@@ -2275,12 +2281,10 @@ case_ok(PyObject *filename, Py_ssize_t prefix_delta, PyObject *name)
static int
find_init_module(PyObject *directory)
{
- size_t len;
struct stat statbuf;
PyObject *filename;
int match;
- len = PyUnicode_GET_SIZE(directory);
filename = PyUnicode_FromFormat("%U%c__init__.py", directory, SEP);
if (filename == NULL)
return -1;
@@ -2818,7 +2822,7 @@ PyImport_ImportModuleLevelObject(PyObject *name, PyObject *globals,
}
PyObject *
-PyImport_ImportModuleLevel(char *name, PyObject *globals, PyObject *locals,
+PyImport_ImportModuleLevel(const char *name, PyObject *globals, PyObject *locals,
PyObject *fromlist, int level)
{
PyObject *nameobj, *mod;
diff --git a/Python/marshal.c b/Python/marshal.c
index 73d4f374cd..7b327ade01 100644
--- a/Python/marshal.c
+++ b/Python/marshal.c
@@ -58,9 +58,9 @@ typedef struct {
int depth;
/* If fp == NULL, the following are valid: */
PyObject *str;
+ PyObject *current_filename;
char *ptr;
char *end;
- PyObject *strings; /* dict on marshal, list on unmarshal */
int version;
} WFILE;
@@ -444,7 +444,6 @@ PyMarshal_WriteLongToFile(long x, FILE *fp, int version)
wf.fp = fp;
wf.error = WFERR_OK;
wf.depth = 0;
- wf.strings = NULL;
wf.version = version;
w_long(x, &wf);
}
@@ -456,10 +455,8 @@ PyMarshal_WriteObjectToFile(PyObject *x, FILE *fp, int version)
wf.fp = fp;
wf.error = WFERR_OK;
wf.depth = 0;
- wf.strings = (version > 0) ? PyDict_New() : NULL;
wf.version = version;
w_object(x, &wf);
- Py_XDECREF(wf.strings);
}
typedef WFILE RFILE; /* Same struct with different invariants */
@@ -980,6 +977,18 @@ r_object(RFILE *p)
filename = r_object(p);
if (filename == NULL)
goto code_error;
+ if (PyUnicode_CheckExact(filename)) {
+ if (p->current_filename != NULL) {
+ if (!PyUnicode_Compare(filename, p->current_filename)) {
+ Py_DECREF(filename);
+ Py_INCREF(p->current_filename);
+ filename = p->current_filename;
+ }
+ }
+ else {
+ p->current_filename = filename;
+ }
+ }
name = r_object(p);
if (name == NULL)
goto code_error;
@@ -1041,7 +1050,7 @@ PyMarshal_ReadShortFromFile(FILE *fp)
RFILE rf;
assert(fp);
rf.fp = fp;
- rf.strings = NULL;
+ rf.current_filename = NULL;
rf.end = rf.ptr = NULL;
return r_short(&rf);
}
@@ -1051,7 +1060,7 @@ PyMarshal_ReadLongFromFile(FILE *fp)
{
RFILE rf;
rf.fp = fp;
- rf.strings = NULL;
+ rf.current_filename = NULL;
rf.ptr = rf.end = NULL;
return r_long(&rf);
}
@@ -1112,11 +1121,10 @@ PyMarshal_ReadObjectFromFile(FILE *fp)
RFILE rf;
PyObject *result;
rf.fp = fp;
- rf.strings = PyList_New(0);
+ rf.current_filename = NULL;
rf.depth = 0;
rf.ptr = rf.end = NULL;
result = r_object(&rf);
- Py_DECREF(rf.strings);
return result;
}
@@ -1126,12 +1134,11 @@ PyMarshal_ReadObjectFromString(char *str, Py_ssize_t len)
RFILE rf;
PyObject *result;
rf.fp = NULL;
+ rf.current_filename = NULL;
rf.ptr = str;
rf.end = str + len;
- rf.strings = PyList_New(0);
rf.depth = 0;
result = r_object(&rf);
- Py_DECREF(rf.strings);
return result;
}
@@ -1150,9 +1157,7 @@ PyMarshal_WriteObjectToString(PyObject *x, int version)
wf.error = WFERR_OK;
wf.depth = 0;
wf.version = version;
- wf.strings = (version > 0) ? PyDict_New() : NULL;
w_object(x, &wf);
- Py_XDECREF(wf.strings);
if (wf.str != NULL) {
char *base = PyBytes_AS_STRING((PyBytesObject *)wf.str);
if (wf.ptr - base > PY_SSIZE_T_MAX) {
@@ -1226,6 +1231,7 @@ marshal_load(PyObject *self, PyObject *f)
if (data == NULL)
return NULL;
rf.fp = NULL;
+ rf.current_filename = NULL;
if (PyBytes_Check(data)) {
rf.ptr = PyBytes_AS_STRING(data);
rf.end = rf.ptr + PyBytes_GET_SIZE(data);
@@ -1242,10 +1248,8 @@ marshal_load(PyObject *self, PyObject *f)
Py_DECREF(data);
return NULL;
}
- rf.strings = PyList_New(0);
rf.depth = 0;
result = read_object(&rf);
- Py_DECREF(rf.strings);
Py_DECREF(data);
return result;
}
@@ -1296,12 +1300,11 @@ marshal_loads(PyObject *self, PyObject *args)
s = p.buf;
n = p.len;
rf.fp = NULL;
+ rf.current_filename = NULL;
rf.ptr = s;
rf.end = s + n;
- rf.strings = PyList_New(0);
rf.depth = 0;
result = read_object(&rf);
- Py_DECREF(rf.strings);
PyBuffer_Release(&p);
return result;
}
diff --git a/Python/pythonrun.c b/Python/pythonrun.c
index b55dc5b201..232d7befa0 100644
--- a/Python/pythonrun.c
+++ b/Python/pythonrun.c
@@ -1593,7 +1593,7 @@ print_exception(PyObject *f, PyObject *value)
moduleName = PyObject_GetAttrString(type, "__module__");
if (moduleName == NULL || !PyUnicode_Check(moduleName))
{
- Py_DECREF(moduleName);
+ Py_XDECREF(moduleName);
err = PyFile_WriteString("<unknown>", f);
}
else {
diff --git a/Python/symtable.c b/Python/symtable.c
index 8040665b58..e31a2ebb39 100644
--- a/Python/symtable.c
+++ b/Python/symtable.c
@@ -185,6 +185,7 @@ static int symtable_visit_params(struct symtable *st, asdl_seq *args);
static int symtable_visit_argannotations(struct symtable *st, asdl_seq *args);
static int symtable_implicit_arg(struct symtable *st, int pos);
static int symtable_visit_annotations(struct symtable *st, stmt_ty s);
+static int symtable_visit_withitem(struct symtable *st, withitem_ty item);
static identifier top = NULL, lambda = NULL, genexpr = NULL,
@@ -1210,14 +1211,11 @@ symtable_visit_stmt(struct symtable *st, stmt_ty s)
}
}
break;
- case TryExcept_kind:
- VISIT_SEQ(st, stmt, s->v.TryExcept.body);
- VISIT_SEQ(st, stmt, s->v.TryExcept.orelse);
- VISIT_SEQ(st, excepthandler, s->v.TryExcept.handlers);
- break;
- case TryFinally_kind:
- VISIT_SEQ(st, stmt, s->v.TryFinally.body);
- VISIT_SEQ(st, stmt, s->v.TryFinally.finalbody);
+ case Try_kind:
+ VISIT_SEQ(st, stmt, s->v.Try.body);
+ VISIT_SEQ(st, stmt, s->v.Try.orelse);
+ VISIT_SEQ(st, excepthandler, s->v.Try.handlers);
+ VISIT_SEQ(st, stmt, s->v.Try.finalbody);
break;
case Assert_kind:
VISIT(st, expr, s->v.Assert.test);
@@ -1305,10 +1303,7 @@ symtable_visit_stmt(struct symtable *st, stmt_ty s)
/* nothing to do here */
break;
case With_kind:
- VISIT(st, expr, s->v.With.context_expr);
- if (s->v.With.optional_vars) {
- VISIT(st, expr, s->v.With.optional_vars);
- }
+ VISIT_SEQ(st, withitem, s->v.With.items);
VISIT_SEQ(st, stmt, s->v.With.body);
break;
}
@@ -1540,6 +1535,16 @@ symtable_visit_excepthandler(struct symtable *st, excepthandler_ty eh)
return 1;
}
+static int
+symtable_visit_withitem(struct symtable *st, withitem_ty item)
+{
+ VISIT(st, expr, item->context_expr);
+ if (item->optional_vars) {
+ VISIT(st, expr, item->optional_vars);
+ }
+ return 1;
+}
+
static int
symtable_visit_alias(struct symtable *st, alias_ty a)
diff --git a/Python/thread_pthread.h b/Python/thread_pthread.h
index 40ebaf6964..09a0887bd4 100644
--- a/Python/thread_pthread.h
+++ b/Python/thread_pthread.h
@@ -18,6 +18,18 @@
#ifndef THREAD_STACK_SIZE
#define THREAD_STACK_SIZE 0 /* use default stack size */
#endif
+
+#if (defined(__APPLE__) || defined(__FreeBSD__)) && defined(THREAD_STACK_SIZE) && THREAD_STACK_SIZE == 0
+ /* The default stack size for new threads on OSX is small enough that
+ * we'll get hard crashes instead of 'maximum recursion depth exceeded'
+ * exceptions.
+ *
+ * The default stack size below is the minimal stack size where a
+ * simple recursive function doesn't cause a hard crash.
+ */
+#undef THREAD_STACK_SIZE
+#define THREAD_STACK_SIZE 0x400000
+#endif
/* for safety, ensure a viable minimum stacksize */
#define THREAD_STACK_MIN 0x8000 /* 32kB */
#else /* !_POSIX_THREAD_ATTR_STACKSIZE */