summaryrefslogtreecommitdiff
path: root/Python
diff options
context:
space:
mode:
Diffstat (limited to 'Python')
-rw-r--r--Python/ast.c126
-rw-r--r--Python/ceval.c18
-rw-r--r--Python/codecs.c9
-rw-r--r--Python/compile.c135
-rw-r--r--Python/dynload_win.c2
-rw-r--r--Python/errors.c15
-rw-r--r--Python/future.c4
-rw-r--r--Python/getargs.c15
-rw-r--r--Python/getopt.c31
-rw-r--r--Python/import.c201
-rw-r--r--Python/mactoolboxglue.c3
-rw-r--r--Python/mystrtoul.c43
-rw-r--r--Python/pyarena.c8
-rw-r--r--Python/pystate.c57
-rw-r--r--Python/pystrtod.c7
-rw-r--r--Python/pythonrun.c49
-rw-r--r--Python/strtod.c2
-rw-r--r--Python/symtable.c16
-rw-r--r--Python/sysmodule.c90
-rw-r--r--Python/thread.c29
-rw-r--r--Python/thread_nt.h31
-rw-r--r--Python/thread_os2.h44
-rw-r--r--Python/thread_pthread.h77
23 files changed, 802 insertions, 210 deletions
diff --git a/Python/ast.c b/Python/ast.c
index f3e611b8e3..ca832aa5e1 100644
--- a/Python/ast.c
+++ b/Python/ast.c
@@ -339,7 +339,7 @@ set_context(expr_ty e, expr_context_ty ctx, const node *n)
/* The ast defines augmented store and load contexts, but the
implementation here doesn't actually use them. The code may be
a little more complex than necessary as a result. It also means
- that expressions in an augmented assignment have no context.
+ that expressions in an augmented assignment have a Store context.
Consider restructuring so that augmented assignment uses
set_context(), too.
*/
@@ -387,6 +387,9 @@ set_context(expr_ty e, expr_context_ty ctx, const node *n)
case GeneratorExp_kind:
expr_name = "generator expression";
break;
+ case Yield_kind:
+ expr_name = "yield expression";
+ break;
case ListComp_kind:
expr_name = "list comprehension";
break;
@@ -619,10 +622,10 @@ ast_for_arguments(struct compiling *c, const node *n)
}
args = (n_args ? asdl_seq_new(n_args, c->c_arena) : NULL);
if (!args && n_args)
- return NULL; /* Don't need to go to NULL; nothing allocated */
+ return NULL; /* Don't need to goto error; no objects allocated */
defaults = (n_defaults ? asdl_seq_new(n_defaults, c->c_arena) : NULL);
if (!defaults && n_defaults)
- goto error;
+ return NULL; /* Don't need to goto error; no objects allocated */
/* fpdef: NAME | '(' fplist ')'
fplist: fpdef (',' fpdef)* [',']
@@ -638,8 +641,11 @@ ast_for_arguments(struct compiling *c, const node *n)
anything other than EQUAL or a comma? */
/* XXX Should NCH(n) check be made a separate check? */
if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
- asdl_seq_SET(defaults, j++,
- ast_for_expr(c, CHILD(n, i + 2)));
+ expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
+ if (!expression)
+ goto error;
+ assert(defaults != NULL);
+ asdl_seq_SET(defaults, j++, expression);
i += 2;
found_default = 1;
}
@@ -1484,6 +1490,57 @@ ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
}
static expr_ty
+ast_for_factor(struct compiling *c, const node *n)
+{
+ node *pfactor, *ppower, *patom, *pnum;
+ expr_ty expression;
+
+ /* If the unary - operator is applied to a constant, don't generate
+ a UNARY_NEGATIVE opcode. Just store the approriate value as a
+ constant. The peephole optimizer already does something like
+ this but it doesn't handle the case where the constant is
+ (sys.maxint - 1). In that case, we want a PyIntObject, not a
+ PyLongObject.
+ */
+ if (TYPE(CHILD(n, 0)) == MINUS
+ && NCH(n) == 2
+ && TYPE((pfactor = CHILD(n, 1))) == factor
+ && NCH(pfactor) == 1
+ && TYPE((ppower = CHILD(pfactor, 0))) == power
+ && NCH(ppower) == 1
+ && TYPE((patom = CHILD(ppower, 0))) == atom
+ && TYPE((pnum = CHILD(patom, 0))) == NUMBER) {
+ char *s = PyObject_MALLOC(strlen(STR(pnum)) + 2);
+ if (s == NULL)
+ return NULL;
+ s[0] = '-';
+ strcpy(s + 1, STR(pnum));
+ PyObject_FREE(STR(pnum));
+ STR(pnum) = s;
+ return ast_for_atom(c, patom);
+ }
+
+ expression = ast_for_expr(c, CHILD(n, 1));
+ if (!expression)
+ return NULL;
+
+ switch (TYPE(CHILD(n, 0))) {
+ case PLUS:
+ return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
+ c->c_arena);
+ case MINUS:
+ return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
+ c->c_arena);
+ case TILDE:
+ return UnaryOp(Invert, expression, LINENO(n),
+ n->n_col_offset, c->c_arena);
+ }
+ PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
+ TYPE(CHILD(n, 0)));
+ return NULL;
+}
+
+static expr_ty
ast_for_power(struct compiling *c, const node *n)
{
/* power: atom trailer* ('**' factor)*
@@ -1662,30 +1719,12 @@ ast_for_expr(struct compiling *c, const node *n)
}
return Yield(exp, LINENO(n), n->n_col_offset, c->c_arena);
}
- case factor: {
- expr_ty expression;
-
+ case factor:
if (NCH(n) == 1) {
n = CHILD(n, 0);
goto loop;
}
-
- expression = ast_for_expr(c, CHILD(n, 1));
- if (!expression)
- return NULL;
-
- switch (TYPE(CHILD(n, 0))) {
- case PLUS:
- return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset, c->c_arena);
- case MINUS:
- return UnaryOp(USub, expression, LINENO(n), n->n_col_offset, c->c_arena);
- case TILDE:
- return UnaryOp(Invert, expression, LINENO(n), n->n_col_offset, c->c_arena);
- }
- PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
- TYPE(CHILD(n, 0)));
- break;
- }
+ return ast_for_factor(c, n);
case power:
return ast_for_power(c, n);
default:
@@ -1893,20 +1932,19 @@ ast_for_expr_stmt(struct compiling *c, const node *n)
operator_ty newoperator;
node *ch = CHILD(n, 0);
- if (TYPE(ch) == testlist)
- expr1 = ast_for_testlist(c, ch);
- else
- expr1 = Yield(ast_for_expr(c, CHILD(ch, 0)), LINENO(ch), n->n_col_offset,
- c->c_arena);
-
+ expr1 = ast_for_testlist(c, ch);
if (!expr1)
return NULL;
- /* TODO(jhylton): Figure out why set_context() can't be used here. */
+ /* TODO(nas): Remove duplicated error checks (set_context does it) */
switch (expr1->kind) {
case GeneratorExp_kind:
ast_error(ch, "augmented assignment to generator "
"expression not possible");
return NULL;
+ case Yield_kind:
+ ast_error(ch, "augmented assignment to yield "
+ "expression not possible");
+ return NULL;
case Name_kind: {
const char *var_name = PyString_AS_STRING(expr1->v.Name.id);
if (var_name[0] == 'N' && !strcmp(var_name, "None")) {
@@ -1923,12 +1961,13 @@ ast_for_expr_stmt(struct compiling *c, const node *n)
"assignment");
return NULL;
}
+ set_context(expr1, Store, ch);
ch = CHILD(n, 2);
if (TYPE(ch) == testlist)
expr2 = ast_for_testlist(c, ch);
else
- expr2 = Yield(ast_for_expr(c, ch), LINENO(ch), ch->n_col_offset, c->c_arena);
+ expr2 = ast_for_expr(c, ch);
if (!expr2)
return NULL;
@@ -2142,7 +2181,14 @@ alias_for_import_name(struct compiling *c, const node *n)
loop:
switch (TYPE(n)) {
case import_as_name:
- str = (NCH(n) == 3) ? NEW_IDENTIFIER(CHILD(n, 2)) : NULL;
+ str = NULL;
+ if (NCH(n) == 3) {
+ if (strcmp(STR(CHILD(n, 1)), "as") != 0) {
+ ast_error(n, "must use 'as' in import");
+ return NULL;
+ }
+ str = NEW_IDENTIFIER(CHILD(n, 2));
+ }
return alias(NEW_IDENTIFIER(CHILD(n, 0)), str, c->c_arena);
case dotted_as_name:
if (NCH(n) == 1) {
@@ -2151,6 +2197,10 @@ alias_for_import_name(struct compiling *c, const node *n)
}
else {
alias_ty a = alias_for_import_name(c, CHILD(n, 0));
+ if (strcmp(STR(CHILD(n, 1)), "as") != 0) {
+ ast_error(n, "must use 'as' in import");
+ return NULL;
+ }
assert(!a->asname);
a->asname = NEW_IDENTIFIER(CHILD(n, 2));
return a;
@@ -2621,6 +2671,7 @@ ast_for_for_stmt(struct compiling *c, const node *n)
asdl_seq *_target, *seq = NULL, *suite_seq;
expr_ty expression;
expr_ty target;
+ const node *node_target;
/* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
REQ(n, for_stmt);
@@ -2630,10 +2681,13 @@ ast_for_for_stmt(struct compiling *c, const node *n)
return NULL;
}
- _target = ast_for_exprlist(c, CHILD(n, 1), Store);
+ node_target = CHILD(n, 1);
+ _target = ast_for_exprlist(c, node_target, Store);
if (!_target)
return NULL;
- if (asdl_seq_LEN(_target) == 1)
+ /* Check the # of children rather than the length of _target, since
+ for x, in ... has 1 element in _target, but still requires a Tuple. */
+ if (NCH(node_target) == 1)
target = (expr_ty)asdl_seq_GET(_target, 0);
else
target = Tuple(_target, Store, LINENO(n), n->n_col_offset, c->c_arena);
diff --git a/Python/ceval.c b/Python/ceval.c
index f14e7daafe..9a9b98eb47 100644
--- a/Python/ceval.c
+++ b/Python/ceval.c
@@ -205,7 +205,7 @@ PyEval_GetCallStats(PyObject *self)
#ifdef WITH_THREAD
-#ifndef DONT_HAVE_ERRNO_H
+#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif
#include "pythread.h"
@@ -2740,7 +2740,7 @@ PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
/* Allocate and initialize storage for cell vars, and copy free
vars into frame. This isn't too efficient right now. */
if (PyTuple_GET_SIZE(co->co_cellvars)) {
- int i = 0, j = 0, nargs, found;
+ int i, j, nargs, found;
char *cellname, *argname;
PyObject *c;
@@ -3318,7 +3318,7 @@ int
PyEval_GetRestricted(void)
{
PyFrameObject *current_frame = PyEval_GetFrame();
- return current_frame == NULL ? 0 : current_frame->f_restricted;
+ return current_frame == NULL ? 0 : PyFrame_IsRestricted(current_frame);
}
int
@@ -4187,6 +4187,14 @@ string_concatenate(PyObject *v, PyObject *w,
{
/* This function implements 'variable += expr' when both arguments
are strings. */
+ Py_ssize_t v_len = PyString_GET_SIZE(v);
+ Py_ssize_t w_len = PyString_GET_SIZE(w);
+ Py_ssize_t new_len = v_len + w_len;
+ if (new_len < 0) {
+ PyErr_SetString(PyExc_OverflowError,
+ "strings are too large to concat");
+ return NULL;
+ }
if (v->ob_refcnt == 2) {
/* In the common case, there are 2 references to the value
@@ -4231,9 +4239,7 @@ string_concatenate(PyObject *v, PyObject *w,
/* Now we own the last reference to 'v', so we can resize it
* in-place.
*/
- Py_ssize_t v_len = PyString_GET_SIZE(v);
- Py_ssize_t w_len = PyString_GET_SIZE(w);
- if (_PyString_Resize(&v, v_len + w_len) != 0) {
+ if (_PyString_Resize(&v, new_len) != 0) {
/* XXX if _PyString_Resize() fails, 'v' has been
* deallocated so it cannot be put back into 'variable'.
* The MemoryError is raised when there is no value in
diff --git a/Python/codecs.c b/Python/codecs.c
index 046abe3507..4b0f4cb0d0 100644
--- a/Python/codecs.c
+++ b/Python/codecs.c
@@ -249,14 +249,17 @@ PyObject *codec_getstreamcodec(const char *encoding,
const char *errors,
const int index)
{
- PyObject *codecs, *streamcodec;
+ PyObject *codecs, *streamcodec, *codeccls;
codecs = _PyCodec_Lookup(encoding);
if (codecs == NULL)
return NULL;
- streamcodec = PyEval_CallFunction(
- PyTuple_GET_ITEM(codecs, index), "Os", stream, errors);
+ codeccls = PyTuple_GET_ITEM(codecs, index);
+ if (errors != NULL)
+ streamcodec = PyObject_CallFunction(codeccls, "Os", stream, errors);
+ else
+ streamcodec = PyObject_CallFunction(codeccls, "O", stream);
Py_DECREF(codecs);
return streamcodec;
}
diff --git a/Python/compile.c b/Python/compile.c
index 6d96006f31..4464882edf 100644
--- a/Python/compile.c
+++ b/Python/compile.c
@@ -143,7 +143,7 @@ struct compiler {
PyFutureFeatures *c_future; /* pointer to module's __future__ */
PyCompilerFlags *c_flags;
- int c_interactive;
+ int c_interactive; /* true if in interactive mode */
int c_nestlevel;
struct compiler_unit *u; /* compiler state for current block */
@@ -300,8 +300,11 @@ PyCodeObject *
PyNode_Compile(struct _node *n, const char *filename)
{
PyCodeObject *co = NULL;
+ mod_ty mod;
PyArena *arena = PyArena_New();
- mod_ty mod = PyAST_FromNode(n, NULL, filename, arena);
+ if (!arena)
+ return NULL;
+ mod = PyAST_FromNode(n, NULL, filename, arena);
if (mod)
co = PyAST_Compile(mod, filename, NULL, arena);
PyArena_Free(arena);
@@ -610,8 +613,10 @@ markblocks(unsigned char *code, int len)
unsigned int *blocks = (unsigned int *)PyMem_Malloc(len*sizeof(int));
int i,j, opcode, blockcnt = 0;
- if (blocks == NULL)
+ if (blocks == NULL) {
+ PyErr_NoMemory();
return NULL;
+ }
memset(blocks, 0, len*sizeof(int));
/* Mark labels in the first pass */
@@ -1066,14 +1071,14 @@ compiler_unit_free(struct compiler_unit *u)
PyObject_Free((void *)b);
b = next;
}
- Py_XDECREF(u->u_ste);
- Py_XDECREF(u->u_name);
- Py_XDECREF(u->u_consts);
- Py_XDECREF(u->u_names);
- Py_XDECREF(u->u_varnames);
- Py_XDECREF(u->u_freevars);
- Py_XDECREF(u->u_cellvars);
- Py_XDECREF(u->u_private);
+ Py_CLEAR(u->u_ste);
+ Py_CLEAR(u->u_name);
+ Py_CLEAR(u->u_consts);
+ Py_CLEAR(u->u_names);
+ Py_CLEAR(u->u_varnames);
+ Py_CLEAR(u->u_freevars);
+ Py_CLEAR(u->u_cellvars);
+ Py_CLEAR(u->u_private);
PyObject_Free(u);
}
@@ -1100,8 +1105,17 @@ compiler_enter_scope(struct compiler *c, identifier name, void *key,
u->u_name = name;
u->u_varnames = list2dict(u->u_ste->ste_varnames);
u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0);
+ if (!u->u_varnames || !u->u_cellvars) {
+ compiler_unit_free(u);
+ return 0;
+ }
+
u->u_freevars = dictbytype(u->u_ste->ste_symbols, FREE, DEF_FREE_CLASS,
PyDict_Size(u->u_cellvars));
+ if (!u->u_freevars) {
+ compiler_unit_free(u);
+ return 0;
+ }
u->u_blocks = NULL;
u->u_tmpname = 0;
@@ -1125,7 +1139,8 @@ compiler_enter_scope(struct compiler *c, identifier name, void *key,
/* Push the old compiler_unit on the stack. */
if (c->u) {
PyObject *wrapper = PyCObject_FromVoidPtr(c->u, NULL);
- if (PyList_Append(c->c_stack, wrapper) < 0) {
+ if (!wrapper || PyList_Append(c->c_stack, wrapper) < 0) {
+ Py_XDECREF(wrapper);
compiler_unit_free(u);
return 0;
}
@@ -1251,6 +1266,7 @@ compiler_next_instr(struct compiler *c, basicblock *b)
sizeof(struct instr) * DEFAULT_BLOCK_SIZE);
}
else if (b->b_iused == b->b_ialloc) {
+ struct instr *tmp;
size_t oldsize, newsize;
oldsize = b->b_ialloc * sizeof(struct instr);
newsize = oldsize << 1;
@@ -1259,10 +1275,13 @@ compiler_next_instr(struct compiler *c, basicblock *b)
return -1;
}
b->b_ialloc <<= 1;
- b->b_instr = (struct instr *)PyObject_Realloc(
+ tmp = (struct instr *)PyObject_Realloc(
(void *)b->b_instr, newsize);
- if (b->b_instr == NULL)
+ if (tmp == NULL) {
+ PyErr_NoMemory();
return -1;
+ }
+ b->b_instr = tmp;
memset((char *)b->b_instr + oldsize, 0, newsize - oldsize);
}
return b->b_iused++;
@@ -1769,7 +1788,8 @@ compiler_mod(struct compiler *c, mod_ty mod)
if (!module)
return NULL;
}
- if (!compiler_enter_scope(c, module, mod, 1))
+ /* Use 0 for firstlineno initially, will fixup in assemble(). */
+ if (!compiler_enter_scope(c, module, mod, 0))
return NULL;
switch (mod->kind) {
case Module_kind:
@@ -1963,11 +1983,8 @@ compiler_function(struct compiler *c, stmt_ty s)
n = asdl_seq_LEN(s->v.FunctionDef.body);
/* if there was a docstring, we need to skip the first statement */
for (i = docstring; i < n; i++) {
- stmt_ty s2 = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, i);
- if (i == 0 && s2->kind == Expr_kind &&
- s2->v.Expr.value->kind == Str_kind)
- continue;
- VISIT_IN_SCOPE(c, stmt, s2);
+ st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, i);
+ VISIT_IN_SCOPE(c, stmt, st);
}
co = assemble(c, 1);
compiler_exit_scope(c);
@@ -2190,6 +2207,10 @@ compiler_for(struct compiler *c, stmt_ty s)
VISIT(c, expr, s->v.For.iter);
ADDOP(c, GET_ITER);
compiler_use_next_block(c, start);
+ /* XXX(nnorwitz): is there a better way to handle this?
+ for loops are special, we want to be able to trace them
+ each time around, so we need to set an extra line number. */
+ c->u->u_lineno_set = false;
ADDOP_JREL(c, FOR_ITER, cleanup);
VISIT(c, expr, s->v.For.target);
VISIT_SEQ(c, stmt, s->v.For.body);
@@ -2708,11 +2729,13 @@ compiler_visit_stmt(struct compiler *c, stmt_ty s)
case Global_kind:
break;
case Expr_kind:
- VISIT(c, expr, s->v.Expr.value);
if (c->c_interactive && c->c_nestlevel <= 1) {
+ VISIT(c, expr, s->v.Expr.value);
ADDOP(c, PRINT_EXPR);
}
- else {
+ else if (s->v.Expr.value->kind != Str_kind &&
+ s->v.Expr.value->kind != Num_kind) {
+ VISIT(c, expr, s->v.Expr.value);
ADDOP(c, POP_TOP);
}
break;
@@ -2989,6 +3012,7 @@ compiler_boolop(struct compiler *c, expr_ty e)
return 0;
s = e->v.BoolOp.values;
n = asdl_seq_LEN(s) - 1;
+ assert(n >= 0);
for (i = 0; i < n; ++i) {
VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i));
ADDOP_JREL(c, jumpi, end);
@@ -3643,7 +3667,7 @@ compiler_augassign(struct compiler *c, stmt_ty s)
assert(s->kind == AugAssign_kind);
switch (e->kind) {
- case Attribute_kind:
+ case Attribute_kind:
auge = Attribute(e->v.Attribute.value, e->v.Attribute.attr,
AugLoad, e->lineno, e->col_offset, c->c_arena);
if (auge == NULL)
@@ -3666,7 +3690,8 @@ compiler_augassign(struct compiler *c, stmt_ty s)
VISIT(c, expr, auge);
break;
case Name_kind:
- VISIT(c, expr, s->v.AugAssign.target);
+ if (!compiler_nameop(c, e->v.Name.id, Load))
+ return 0;
VISIT(c, expr, s->v.AugAssign.value);
ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
return compiler_nameop(c, e->v.Name.id, Store);
@@ -3979,6 +4004,8 @@ stackdepth(struct compiler *c)
b->b_startdepth = INT_MIN;
entryblock = b;
}
+ if (!entryblock)
+ return 0;
return stackdepth_walk(c, entryblock, 0, 0);
}
@@ -4074,9 +4101,10 @@ corresponding to a bytecode address A should do something like this
In order for this to work, when the addr field increments by more than 255,
the line # increment in each pair generated must be 0 until the remaining addr
-increment is < 256. So, in the example above, com_set_lineno should not (as
-was actually done until 2.2) expand 300, 300 to 255, 255, 45, 45, but to
-255, 0, 45, 255, 0, 45.
+increment is < 256. So, in the example above, assemble_lnotab (it used
+to be called com_set_lineno) should not (as was actually done until 2.2)
+expand 300, 300 to 255, 255, 45, 45,
+ but to 255, 0, 45, 255, 0, 45.
*/
static int
@@ -4092,7 +4120,10 @@ assemble_lnotab(struct assembler *a, struct instr *i)
assert(d_bytecode >= 0);
assert(d_lineno >= 0);
- if (d_lineno == 0)
+ /* XXX(nnorwitz): is there a better way to handle this?
+ for loops are special, we want to be able to trace them
+ each time around, so we need to set an extra line number. */
+ if (d_lineno == 0 && i->i_opcode != FOR_ITER)
return 1;
if (d_bytecode > 255) {
@@ -4131,12 +4162,12 @@ assemble_lnotab(struct assembler *a, struct instr *i)
}
lnotab = (unsigned char *)
PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
- *lnotab++ = 255;
*lnotab++ = d_bytecode;
+ *lnotab++ = 255;
d_bytecode = 0;
for (j = 1; j < ncodes; j++) {
- *lnotab++ = 255;
*lnotab++ = 0;
+ *lnotab++ = 255;
}
d_lineno -= ncodes * 255;
a->a_lnotab_off += ncodes * 2;
@@ -4173,7 +4204,7 @@ static int
assemble_emit(struct assembler *a, struct instr *i)
{
int size, arg = 0, ext = 0;
- int len = PyString_GET_SIZE(a->a_bytecode);
+ Py_ssize_t len = PyString_GET_SIZE(a->a_bytecode);
char *code;
size = instrsize(i);
@@ -4397,6 +4428,41 @@ makecode(struct compiler *c, struct assembler *a)
return co;
}
+
+/* For debugging purposes only */
+#if 0
+static void
+dump_instr(const struct instr *i)
+{
+ const char *jrel = i->i_jrel ? "jrel " : "";
+ const char *jabs = i->i_jabs ? "jabs " : "";
+ char arg[128];
+
+ *arg = '\0';
+ if (i->i_hasarg)
+ sprintf(arg, "arg: %d ", i->i_oparg);
+
+ fprintf(stderr, "line: %d, opcode: %d %s%s%s\n",
+ i->i_lineno, i->i_opcode, arg, jabs, jrel);
+}
+
+static void
+dump_basicblock(const basicblock *b)
+{
+ const char *seen = b->b_seen ? "seen " : "";
+ const char *b_return = b->b_return ? "return " : "";
+ fprintf(stderr, "used: %d, depth: %d, offset: %d %s%s\n",
+ b->b_iused, b->b_startdepth, b->b_offset, seen, b_return);
+ if (b->b_instr) {
+ int i;
+ for (i = 0; i < b->b_iused; i++) {
+ fprintf(stderr, " [%02d] ", i);
+ dump_instr(b->b_instr + i);
+ }
+ }
+}
+#endif
+
static PyCodeObject *
assemble(struct compiler *c, int addNone)
{
@@ -4423,6 +4489,13 @@ assemble(struct compiler *c, int addNone)
entryblock = b;
}
+ /* Set firstlineno if it wasn't explicitly set. */
+ if (!c->u->u_firstlineno) {
+ if (entryblock && entryblock->b_instr)
+ c->u->u_firstlineno = entryblock->b_instr->i_lineno;
+ else
+ c->u->u_firstlineno = 1;
+ }
if (!assemble_init(&a, nblocks, c->u->u_firstlineno))
goto error;
dfs(c, entryblock, &a);
diff --git a/Python/dynload_win.c b/Python/dynload_win.c
index 36746e2f10..37d6d2ee38 100644
--- a/Python/dynload_win.c
+++ b/Python/dynload_win.c
@@ -2,7 +2,9 @@
/* Support for dynamic loading of extension modules */
#include <windows.h>
+#ifdef HAVE_DIRECT_H
#include <direct.h>
+#endif
#include <ctype.h>
#include "Python.h"
diff --git a/Python/errors.c b/Python/errors.c
index d99f261e18..48ed05c555 100644
--- a/Python/errors.c
+++ b/Python/errors.c
@@ -641,7 +641,7 @@ extern PyObject *PyModule_GetWarningsModule(void);
/* Function to issue a warning message; may raise an exception. */
int
-PyErr_Warn(PyObject *category, char *message)
+PyErr_WarnEx(PyObject *category, const char *message, Py_ssize_t stack_level)
{
PyObject *dict, *func = NULL;
PyObject *warnings_module = PyModule_GetWarningsModule();
@@ -659,7 +659,8 @@ PyErr_Warn(PyObject *category, char *message)
if (category == NULL)
category = PyExc_RuntimeWarning;
- res = PyObject_CallFunction(func, "sO", message, category);
+ res = PyObject_CallFunction(func, "sOn",
+ message, category, stack_level);
if (res == NULL)
return -1;
Py_DECREF(res);
@@ -667,6 +668,16 @@ PyErr_Warn(PyObject *category, char *message)
}
}
+/* PyErr_Warn is only for backwards compatability and will be removed.
+ Use PyErr_WarnEx instead. */
+
+#undef PyErr_Warn
+
+PyAPI_FUNC(int)
+PyErr_Warn(PyObject *category, char *message)
+{
+ return PyErr_WarnEx(category, message, 1);
+}
/* Warning with explicit origin */
int
diff --git a/Python/future.c b/Python/future.c
index d23fad6b8b..d6f11a4469 100644
--- a/Python/future.c
+++ b/Python/future.c
@@ -121,8 +121,10 @@ PyFuture_FromAST(mod_ty mod, const char *filename)
PyFutureFeatures *ff;
ff = (PyFutureFeatures *)PyObject_Malloc(sizeof(PyFutureFeatures));
- if (ff == NULL)
+ if (ff == NULL) {
+ PyErr_NoMemory();
return NULL;
+ }
ff->ff_features = 0;
ff->ff_lineno = -1;
diff --git a/Python/getargs.c b/Python/getargs.c
index 9637618fe7..8dc5e784f5 100644
--- a/Python/getargs.c
+++ b/Python/getargs.c
@@ -206,6 +206,9 @@ vgetargs1(PyObject *args, const char *format, va_list *p_va, int flags)
if (level == 0)
max++;
level++;
+ if (level >= 30)
+ Py_FatalError("too many tuple nesting levels "
+ "in argument format string");
break;
case ')':
if (level == 0)
@@ -351,8 +354,8 @@ seterror(int iarg, const char *msg, int *levels, const char *fname,
"argument %d", iarg);
i = 0;
p += strlen(p);
- while (levels[i] > 0 && (int)(p-buf) < 220) {
- PyOS_snprintf(p, sizeof(buf) - (buf - p),
+ while (levels[i] > 0 && i < 32 && (int)(p-buf) < 220) {
+ PyOS_snprintf(p, sizeof(buf) - (p - buf),
", item %d", levels[i]-1);
p += strlen(p);
i++;
@@ -439,6 +442,13 @@ converttuple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
char *msg;
PyObject *item;
item = PySequence_GetItem(arg, i);
+ if (item == NULL) {
+ PyErr_Clear();
+ levels[0] = i+1;
+ levels[1] = 0;
+ strncpy(msgbuf, "is not retrievable", bufsize);
+ return msgbuf;
+ }
msg = convertitem(item, &format, p_va, flags, levels+1,
msgbuf, bufsize, freelist);
/* PySequence_GetItem calls tp->sq_item, which INCREFs */
@@ -1508,6 +1518,7 @@ vgetargskeywords(PyObject *args, PyObject *keywords, const char *format,
else {
msg = skipitem(&format, p_va, flags);
if (msg) {
+ levels[0] = 0;
seterror(i+1, msg, levels, fname, message);
return cleanreturn(0, freelist);
}
diff --git a/Python/getopt.c b/Python/getopt.c
index 5429fac5ad..659efcfff8 100644
--- a/Python/getopt.c
+++ b/Python/getopt.c
@@ -24,6 +24,9 @@
* davegottner@delphi.com.
*---------------------------------------------------------------------------*/
+/* Modified to support --help and --version, as well as /? on Windows
+ * by Georg Brandl. */
+
#include <stdio.h>
#include <string.h>
@@ -43,8 +46,17 @@ int _PyOS_GetOpt(int argc, char **argv, char *optstring)
if (*opt_ptr == '\0') {
- if (_PyOS_optind >= argc || argv[_PyOS_optind][0] != '-' ||
- argv[_PyOS_optind][1] == '\0' /* lone dash */ )
+ if (_PyOS_optind >= argc)
+ return -1;
+#ifdef MS_WINDOWS
+ else if (strcmp(argv[_PyOS_optind], "/?") == 0) {
+ ++_PyOS_optind;
+ return 'h';
+ }
+#endif
+
+ else if (argv[_PyOS_optind][0] != '-' ||
+ argv[_PyOS_optind][1] == '\0' /* lone dash */ )
return -1;
else if (strcmp(argv[_PyOS_optind], "--") == 0) {
@@ -52,6 +64,17 @@ int _PyOS_GetOpt(int argc, char **argv, char *optstring)
return -1;
}
+ else if (strcmp(argv[_PyOS_optind], "--help") == 0) {
+ ++_PyOS_optind;
+ return 'h';
+ }
+
+ else if (strcmp(argv[_PyOS_optind], "--version") == 0) {
+ ++_PyOS_optind;
+ return 'V';
+ }
+
+
opt_ptr = &argv[_PyOS_optind++][1];
}
@@ -62,7 +85,7 @@ int _PyOS_GetOpt(int argc, char **argv, char *optstring)
if (_PyOS_opterr)
fprintf(stderr, "Unknown option: -%c\n", option);
- return '?';
+ return '_';
}
if (*(ptr + 1) == ':') {
@@ -76,7 +99,7 @@ int _PyOS_GetOpt(int argc, char **argv, char *optstring)
if (_PyOS_opterr)
fprintf(stderr,
"Argument expected for the -%c option\n", option);
- return '?';
+ return '_';
}
_PyOS_optarg = argv[_PyOS_optind++];
diff --git a/Python/import.c b/Python/import.c
index c14e8cd0f2..ef37e8b5b5 100644
--- a/Python/import.c
+++ b/Python/import.c
@@ -60,6 +60,10 @@ extern time_t PyOS_GetLastModificationTime(char *, FILE *);
Python 2.5a0: 62081 (ast-branch)
Python 2.5a0: 62091 (with)
Python 2.5a0: 62092 (changed WITH_CLEANUP opcode)
+ Python 2.5b3: 62101 (fix wrong code: for x, in ...)
+ Python 2.5b3: 62111 (fix wrong code: x += yield)
+ Python 2.5c1: 62121 (fix wrong lnotab with for loops and
+ storing constants that should have been removed)
Python 3000: 3000
.
*/
@@ -98,6 +102,8 @@ static const struct filedescr _PyImport_StandardFiletab[] = {
};
#endif
+static PyTypeObject NullImporterType; /* Forward reference */
+
/* Initialize things */
void
@@ -116,6 +122,8 @@ _PyImport_Init(void)
for (scan = _PyImport_StandardFiletab; scan->suffix != NULL; ++scan)
++countS;
filetab = PyMem_NEW(struct filedescr, countD + countS + 1);
+ if (filetab == NULL)
+ Py_FatalError("Can't initialize import file table.");
memcpy(filetab, _PyImport_DynLoadFiletab,
countD * sizeof(struct filedescr));
memcpy(filetab + countD, _PyImport_StandardFiletab,
@@ -153,6 +161,8 @@ _PyImportHooks_Init(void)
/* adding sys.path_hooks and sys.path_importer_cache, setting up
zipimport */
+ if (PyType_Ready(&NullImporterType) < 0)
+ goto error;
if (Py_VerboseFlag)
PySys_WriteStderr("# installing zipimport hook\n");
@@ -178,9 +188,11 @@ _PyImportHooks_Init(void)
if (err) {
error:
PyErr_Print();
- Py_FatalError("initializing sys.meta_path, sys.path_hooks or "
- "path_importer_cache failed");
+ Py_FatalError("initializing sys.meta_path, sys.path_hooks, "
+ "path_importer_cache, or NullImporter failed"
+ );
}
+
zimpimport = PyImport_ImportModule("zipimport");
if (zimpimport == NULL) {
PyErr_Clear(); /* No zip import module -- okay */
@@ -239,8 +251,11 @@ lock_import(void)
long me = PyThread_get_thread_ident();
if (me == -1)
return; /* Too bad */
- if (import_lock == NULL)
+ if (import_lock == NULL) {
import_lock = PyThread_allocate_lock();
+ if (import_lock == NULL)
+ return; /* Nothing much we can do. */
+ }
if (import_lock_thread == me) {
import_lock_level++;
return;
@@ -259,7 +274,7 @@ static int
unlock_import(void)
{
long me = PyThread_get_thread_ident();
- if (me == -1)
+ if (me == -1 || import_lock == NULL)
return 0; /* Too bad */
if (import_lock_thread != me)
return -1;
@@ -1053,9 +1068,18 @@ get_path_importer(PyObject *path_importer_cache, PyObject *path_hooks,
}
PyErr_Clear();
}
- if (importer == NULL)
- importer = Py_None;
- else if (importer != Py_None) {
+ if (importer == NULL) {
+ importer = PyObject_CallFunctionObjArgs(
+ (PyObject *)&NullImporterType, p, NULL
+ );
+ if (importer == NULL) {
+ if (PyErr_ExceptionMatches(PyExc_ImportError)) {
+ PyErr_Clear();
+ return Py_None;
+ }
+ }
+ }
+ if (importer != NULL) {
int err = PyDict_SetItem(path_importer_cache, p, importer);
Py_DECREF(importer);
if (err != 0)
@@ -1238,42 +1262,17 @@ find_module(char *fullname, char *subname, PyObject *path, char *buf,
importer = get_path_importer(path_importer_cache,
path_hooks, v);
- if (importer == NULL)
- return NULL;
- /* Note: importer is a borrowed reference */
- if (importer == Py_False) {
- /* Cached as not being a valid dir. */
+ if (importer == NULL) {
Py_XDECREF(copy);
- continue;
- }
- else if (importer == Py_True) {
- /* Cached as being a valid dir, so just
- * continue below. */
- }
- else if (importer == Py_None) {
- /* No importer was found, so it has to be a file.
- * Check if the directory is valid.
- * Note that the empty string is a valid path, but
- * not stat'able, hence the check for len. */
-#ifdef HAVE_STAT
- if (len && stat(buf, &statbuf) != 0) {
- /* Directory does not exist. */
- PyDict_SetItem(path_importer_cache,
- v, Py_False);
- Py_XDECREF(copy);
- continue;
- } else {
- PyDict_SetItem(path_importer_cache,
- v, Py_True);
- }
-#endif
+ return NULL;
}
- else {
- /* A real import hook importer was found. */
+ /* Note: importer is a borrowed reference */
+ if (importer != Py_None) {
PyObject *loader;
loader = PyObject_CallMethod(importer,
"find_module",
"s", fullname);
+ Py_XDECREF(copy);
if (loader == NULL)
return NULL; /* error */
if (loader != Py_None) {
@@ -1282,7 +1281,6 @@ find_module(char *fullname, char *subname, PyObject *path, char *buf,
return &importhookdescr;
}
Py_DECREF(loader);
- Py_XDECREF(copy);
continue;
}
}
@@ -1904,11 +1902,10 @@ PyImport_ImportFrozenModule(char *name)
if (co == NULL)
return -1;
if (!PyCode_Check(co)) {
- Py_DECREF(co);
PyErr_Format(PyExc_TypeError,
"frozen object %.200s is not a code object",
name);
- return -1;
+ goto err_return;
}
if (ispackage) {
/* Set __path__ to the package name */
@@ -1916,22 +1913,25 @@ PyImport_ImportFrozenModule(char *name)
int err;
m = PyImport_AddModule(name);
if (m == NULL)
- return -1;
+ goto err_return;
d = PyModule_GetDict(m);
s = PyString_InternFromString(name);
if (s == NULL)
- return -1;
+ goto err_return;
err = PyDict_SetItemString(d, "__path__", s);
Py_DECREF(s);
if (err != 0)
- return err;
+ goto err_return;
}
m = PyImport_ExecCodeModuleEx(name, co, "<frozen>");
- Py_DECREF(co);
if (m == NULL)
- return -1;
+ goto err_return;
+ Py_DECREF(co);
Py_DECREF(m);
return 1;
+err_return:
+ Py_DECREF(co);
+ return -1;
}
@@ -2926,11 +2926,120 @@ setint(PyObject *d, char *name, int value)
return err;
}
+typedef struct {
+ PyObject_HEAD
+} NullImporter;
+
+static int
+NullImporter_init(NullImporter *self, PyObject *args, PyObject *kwds)
+{
+ char *path;
+
+ if (!_PyArg_NoKeywords("NullImporter()", kwds))
+ return -1;
+
+ if (!PyArg_ParseTuple(args, "s:NullImporter",
+ &path))
+ return -1;
+
+ if (strlen(path) == 0) {
+ PyErr_SetString(PyExc_ImportError, "empty pathname");
+ return -1;
+ } else {
+#ifndef RISCOS
+ struct stat statbuf;
+ int rv;
+
+ rv = stat(path, &statbuf);
+ if (rv == 0) {
+ /* it exists */
+ if (S_ISDIR(statbuf.st_mode)) {
+ /* it's a directory */
+ PyErr_SetString(PyExc_ImportError,
+ "existing directory");
+ return -1;
+ }
+ }
+#else
+ if (object_exists(path)) {
+ /* it exists */
+ if (isdir(path)) {
+ /* it's a directory */
+ PyErr_SetString(PyExc_ImportError,
+ "existing directory");
+ return -1;
+ }
+ }
+#endif
+ }
+ return 0;
+}
+
+static PyObject *
+NullImporter_find_module(NullImporter *self, PyObject *args)
+{
+ Py_RETURN_NONE;
+}
+
+static PyMethodDef NullImporter_methods[] = {
+ {"find_module", (PyCFunction)NullImporter_find_module, METH_VARARGS,
+ "Always return None"
+ },
+ {NULL} /* Sentinel */
+};
+
+
+static PyTypeObject NullImporterType = {
+ PyObject_HEAD_INIT(NULL)
+ 0, /*ob_size*/
+ "imp.NullImporter", /*tp_name*/
+ sizeof(NullImporter), /*tp_basicsize*/
+ 0, /*tp_itemsize*/
+ 0, /*tp_dealloc*/
+ 0, /*tp_print*/
+ 0, /*tp_getattr*/
+ 0, /*tp_setattr*/
+ 0, /*tp_compare*/
+ 0, /*tp_repr*/
+ 0, /*tp_as_number*/
+ 0, /*tp_as_sequence*/
+ 0, /*tp_as_mapping*/
+ 0, /*tp_hash */
+ 0, /*tp_call*/
+ 0, /*tp_str*/
+ 0, /*tp_getattro*/
+ 0, /*tp_setattro*/
+ 0, /*tp_as_buffer*/
+ Py_TPFLAGS_DEFAULT, /*tp_flags*/
+ "Null importer object", /* tp_doc */
+ 0, /* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ 0, /* tp_iter */
+ 0, /* tp_iternext */
+ NullImporter_methods, /* tp_methods */
+ 0, /* tp_members */
+ 0, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ (initproc)NullImporter_init, /* tp_init */
+ 0, /* tp_alloc */
+ PyType_GenericNew /* tp_new */
+};
+
+
PyMODINIT_FUNC
initimp(void)
{
PyObject *m, *d;
+ if (PyType_Ready(&NullImporterType) < 0)
+ goto failure;
+
m = Py_InitModule4("imp", imp_methods, doc_imp,
NULL, PYTHON_API_VERSION);
if (m == NULL)
@@ -2948,6 +3057,8 @@ initimp(void)
if (setint(d, "PY_CODERESOURCE", PY_CODERESOURCE) < 0) goto failure;
if (setint(d, "IMP_HOOK", IMP_HOOK) < 0) goto failure;
+ Py_INCREF(&NullImporterType);
+ PyModule_AddObject(m, "NullImporter", (PyObject *)&NullImporterType);
failure:
;
}
diff --git a/Python/mactoolboxglue.c b/Python/mactoolboxglue.c
index 0aa2cfda37..26a13083f3 100644
--- a/Python/mactoolboxglue.c
+++ b/Python/mactoolboxglue.c
@@ -60,8 +60,9 @@ PyMac_StrError(int err)
strncpy(buf, input, sizeof(buf) - 1);
buf[sizeof(buf) - 1] = '\0';
}
+ Py_DECREF(rv);
}
-
+ Py_XDECREF(m);
return buf;
}
diff --git a/Python/mystrtoul.c b/Python/mystrtoul.c
index 380b37da90..0dda4be2ee 100644
--- a/Python/mystrtoul.c
+++ b/Python/mystrtoul.c
@@ -17,7 +17,7 @@
#include <ctype.h>
-#ifndef DONT_HAVE_ERRNO_H
+#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif
@@ -69,11 +69,22 @@ static unsigned long smallmax[] = {
* calculated by [int(math.floor(math.log(2**32, i))) for i in range(2, 37)].
* Note that this is pessimistic if sizeof(long) > 4.
*/
+#if SIZEOF_LONG == 4
static int digitlimit[] = {
0, 0, 32, 20, 16, 13, 12, 11, 10, 10, /* 0 - 9 */
9, 9, 8, 8, 8, 8, 8, 7, 7, 7, /* 10 - 19 */
7, 7, 7, 7, 6, 6, 6, 6, 6, 6, /* 20 - 29 */
6, 6, 6, 6, 6, 6, 6}; /* 30 - 36 */
+#elif SIZEOF_LONG == 8
+/* [int(math.floor(math.log(2**64, i))) for i in range(2, 37)] */
+static int digitlimit[] = {
+ 0, 0, 64, 40, 32, 27, 24, 22, 21, 20, /* 0 - 9 */
+ 19, 18, 17, 17, 16, 16, 16, 15, 15, 15, /* 10 - 19 */
+ 14, 14, 14, 14, 13, 13, 13, 13, 13, 13, /* 20 - 29 */
+ 13, 12, 12, 12, 12, 12, 12}; /* 30 - 36 */
+#else
+#error "Need table for SIZEOF_LONG"
+#endif
/*
** strtoul
@@ -184,10 +195,19 @@ overflowed:
return (unsigned long)-1;
}
+/* Checking for overflow in PyOS_strtol is a PITA since C doesn't define
+ * anything about what happens when a signed integer operation overflows,
+ * and some compilers think they're doing you a favor by being "clever"
+ * then. Python assumes a 2's-complement representation, so that the bit
+ * pattern for the largest postive signed long is LONG_MAX, and for
+ * the smallest negative signed long is LONG_MAX + 1.
+ */
+
long
PyOS_strtol(char *str, char **ptr, int base)
{
long result;
+ unsigned long uresult;
char sign;
while (*str && isspace(Py_CHARMASK(*str)))
@@ -197,17 +217,20 @@ PyOS_strtol(char *str, char **ptr, int base)
if (sign == '+' || sign == '-')
str++;
- result = (long) PyOS_strtoul(str, ptr, base);
+ uresult = PyOS_strtoul(str, ptr, base);
- /* Signal overflow if the result appears negative,
- except for the largest negative integer */
- if (result < 0 && !(sign == '-' && result == -result)) {
+ if (uresult <= (unsigned long)LONG_MAX) {
+ result = (long)uresult;
+ if (sign == '-')
+ result = -result;
+ }
+ else if (sign == '-' && uresult == (unsigned long)LONG_MAX + 1) {
+ assert(LONG_MIN == -LONG_MAX-1);
+ result = LONG_MIN;
+ }
+ else {
errno = ERANGE;
- result = 0x7fffffff;
+ result = LONG_MAX;
}
-
- if (sign == '-')
- result = -result;
-
return result;
}
diff --git a/Python/pyarena.c b/Python/pyarena.c
index f11a90546b..f4cc474f41 100644
--- a/Python/pyarena.c
+++ b/Python/pyarena.c
@@ -132,19 +132,19 @@ PyArena_New()
{
PyArena* arena = (PyArena *)malloc(sizeof(PyArena));
if (!arena)
- return NULL;
+ return (PyArena*)PyErr_NoMemory();
arena->a_head = block_new(DEFAULT_BLOCK_SIZE);
arena->a_cur = arena->a_head;
if (!arena->a_head) {
free((void *)arena);
- return NULL;
+ return (PyArena*)PyErr_NoMemory();
}
arena->a_objects = PyList_New(0);
if (!arena->a_objects) {
block_free(arena->a_head);
free((void *)arena);
- return NULL;
+ return (PyArena*)PyErr_NoMemory();
}
#if defined(Py_DEBUG)
arena->total_allocs = 0;
@@ -191,7 +191,7 @@ PyArena_Malloc(PyArena *arena, size_t size)
{
void *p = block_alloc(arena->a_cur, size);
if (!p)
- return NULL;
+ return PyErr_NoMemory();
#if defined(Py_DEBUG)
arena->total_allocs++;
arena->total_size += size;
diff --git a/Python/pystate.c b/Python/pystate.c
index b8f460ff8a..3fae85be88 100644
--- a/Python/pystate.c
+++ b/Python/pystate.c
@@ -63,6 +63,10 @@ PyInterpreterState_New(void)
if (interp != NULL) {
HEAD_INIT();
+#ifdef WITH_THREAD
+ if (head_mutex == NULL)
+ Py_FatalError("Can't initialize threads for interpreter");
+#endif
interp->modules = NULL;
interp->sysdict = NULL;
interp->builtins = NULL;
@@ -387,6 +391,53 @@ PyThreadState_Next(PyThreadState *tstate) {
return tstate->next;
}
+/* The implementation of sys._current_frames(). This is intended to be
+ called with the GIL held, as it will be when called via
+ sys._current_frames(). It's possible it would work fine even without
+ the GIL held, but haven't thought enough about that.
+*/
+PyObject *
+_PyThread_CurrentFrames(void)
+{
+ PyObject *result;
+ PyInterpreterState *i;
+
+ result = PyDict_New();
+ if (result == NULL)
+ return NULL;
+
+ /* for i in all interpreters:
+ * for t in all of i's thread states:
+ * if t's frame isn't NULL, map t's id to its frame
+ * Because these lists can mutute even when the GIL is held, we
+ * need to grab head_mutex for the duration.
+ */
+ HEAD_LOCK();
+ for (i = interp_head; i != NULL; i = i->next) {
+ PyThreadState *t;
+ for (t = i->tstate_head; t != NULL; t = t->next) {
+ PyObject *id;
+ int stat;
+ struct _frame *frame = t->frame;
+ if (frame == NULL)
+ continue;
+ id = PyInt_FromLong(t->thread_id);
+ if (id == NULL)
+ goto Fail;
+ stat = PyDict_SetItem(result, id, (PyObject *)frame);
+ Py_DECREF(id);
+ if (stat < 0)
+ goto Fail;
+ }
+ }
+ HEAD_UNLOCK();
+ return result;
+
+ Fail:
+ HEAD_UNLOCK();
+ Py_DECREF(result);
+ return NULL;
+}
/* Python "auto thread state" API. */
#ifdef WITH_THREAD
@@ -444,15 +495,15 @@ _PyGILState_NoteThreadState(PyThreadState* tstate)
/* If autoTLSkey is 0, this must be the very first threadstate created
in Py_Initialize(). Don't do anything for now (we'll be back here
when _PyGILState_Init is called). */
- if (!autoTLSkey)
+ if (!autoTLSkey)
return;
-
+
/* Stick the thread state for this thread in thread local storage.
The only situation where you can legitimately have more than one
thread state for an OS level thread is when there are multiple
interpreters, when:
-
+
a) You shouldn't really be using the PyGILState_ APIs anyway,
and:
diff --git a/Python/pystrtod.c b/Python/pystrtod.c
index e1c84ea03e..6c19b45fd2 100644
--- a/Python/pystrtod.c
+++ b/Python/pystrtod.c
@@ -90,6 +90,13 @@ PyOS_ascii_strtod(const char *nptr, char **endptr)
p++;
end = p;
}
+ else if (strncmp(p, decimal_point, decimal_point_len) == 0)
+ {
+ /* Python bug #1417699 */
+ *endptr = (char*)nptr;
+ errno = EINVAL;
+ return val;
+ }
/* For the other cases, we need not convert the decimal point */
}
diff --git a/Python/pythonrun.c b/Python/pythonrun.c
index 2dbcf75ac9..555c39d533 100644
--- a/Python/pythonrun.c
+++ b/Python/pythonrun.c
@@ -17,7 +17,9 @@
#include "eval.h"
#include "marshal.h"
+#ifdef HAVE_SIGNAL_H
#include <signal.h>
+#endif
#ifdef HAVE_LANGINFO_H
#include <locale.h>
@@ -738,6 +740,11 @@ PyRun_InteractiveOneFlags(FILE *fp, const char *filename, PyCompilerFlags *flags
ps2 = PyString_AsString(w);
}
arena = PyArena_New();
+ if (arena == NULL) {
+ Py_XDECREF(v);
+ Py_XDECREF(w);
+ return -1;
+ }
mod = PyParser_ASTFromFile(fp, filename,
Py_single_input, ps1, ps2,
flags, &errcode, arena);
@@ -1056,6 +1063,17 @@ PyErr_PrintEx(int set_sys_last_vars)
}
PyErr_Fetch(&exception2, &v2, &tb2);
PyErr_NormalizeException(&exception2, &v2, &tb2);
+ /* It should not be possible for exception2 or v2
+ to be NULL. However PyErr_Display() can't
+ tolerate NULLs, so just be safe. */
+ if (exception2 == NULL) {
+ exception2 = Py_None;
+ Py_INCREF(exception2);
+ }
+ if (v2 == NULL) {
+ v2 = Py_None;
+ Py_INCREF(v2);
+ }
if (Py_FlushLine())
PyErr_Clear();
fflush(stdout);
@@ -1063,8 +1081,8 @@ PyErr_PrintEx(int set_sys_last_vars)
PyErr_Display(exception2, v2, tb2);
PySys_WriteStderr("\nOriginal exception was:\n");
PyErr_Display(exception, v, tb);
- Py_XDECREF(exception2);
- Py_XDECREF(v2);
+ Py_DECREF(exception2);
+ Py_DECREF(v2);
Py_XDECREF(tb2);
}
Py_XDECREF(result);
@@ -1184,9 +1202,12 @@ PyRun_StringFlags(const char *str, int start, PyObject *globals,
PyObject *locals, PyCompilerFlags *flags)
{
PyObject *ret = NULL;
+ mod_ty mod;
PyArena *arena = PyArena_New();
- mod_ty mod = PyParser_ASTFromString(str, "<string>", start, flags,
- arena);
+ if (arena == NULL)
+ return NULL;
+
+ mod = PyParser_ASTFromString(str, "<string>", start, flags, arena);
if (mod != NULL)
ret = run_mod(mod, "<string>", globals, locals, flags, arena);
PyArena_Free(arena);
@@ -1198,9 +1219,13 @@ PyRun_FileExFlags(FILE *fp, const char *filename, int start, PyObject *globals,
PyObject *locals, int closeit, PyCompilerFlags *flags)
{
PyObject *ret;
+ mod_ty mod;
PyArena *arena = PyArena_New();
- mod_ty mod = PyParser_ASTFromFile(fp, filename, start, 0, 0,
- flags, NULL, arena);
+ if (arena == NULL)
+ return NULL;
+
+ mod = PyParser_ASTFromFile(fp, filename, start, 0, 0,
+ flags, NULL, arena);
if (mod == NULL) {
PyArena_Free(arena);
return NULL;
@@ -1263,8 +1288,12 @@ Py_CompileStringFlags(const char *str, const char *filename, int start,
PyCompilerFlags *flags)
{
PyCodeObject *co;
+ mod_ty mod;
PyArena *arena = PyArena_New();
- mod_ty mod = PyParser_ASTFromString(str, filename, start, flags, arena);
+ if (arena == NULL)
+ return NULL;
+
+ mod = PyParser_ASTFromString(str, filename, start, flags, arena);
if (mod == NULL) {
PyArena_Free(arena);
return NULL;
@@ -1283,8 +1312,12 @@ struct symtable *
Py_SymtableString(const char *str, const char *filename, int start)
{
struct symtable *st;
+ mod_ty mod;
PyArena *arena = PyArena_New();
- mod_ty mod = PyParser_ASTFromString(str, filename, start, NULL, arena);
+ if (arena == NULL)
+ return NULL;
+
+ mod = PyParser_ASTFromString(str, filename, start, NULL, arena);
if (mod == NULL) {
PyArena_Free(arena);
return NULL;
diff --git a/Python/strtod.c b/Python/strtod.c
index 7911a94590..5c084a4de0 100644
--- a/Python/strtod.c
+++ b/Python/strtod.c
@@ -54,7 +54,7 @@ static double HUGE = 1.7976931348623157e308;
extern double atof(const char *); /* Only called when result known to be ok */
-#ifndef DONT_HAVE_ERRNO_H
+#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif
extern int errno;
diff --git a/Python/symtable.c b/Python/symtable.c
index 1dc2a2ea74..439a24349d 100644
--- a/Python/symtable.c
+++ b/Python/symtable.c
@@ -221,8 +221,12 @@ PySymtable_Build(mod_ty mod, const char *filename, PyFutureFeatures *future)
return st;
st->st_filename = filename;
st->st_future = future;
- symtable_enter_block(st, GET_IDENTIFIER(top), ModuleBlock,
- (void *)mod, 0);
+ if (!symtable_enter_block(st, GET_IDENTIFIER(top), ModuleBlock,
+ (void *)mod, 0)) {
+ PySymtable_Free(st);
+ return NULL;
+ }
+
st->st_top = st->st_cur;
st->st_cur->ste_unoptimized = OPT_TOPLEVEL;
/* Any other top-level initialization? */
@@ -525,6 +529,8 @@ update_symbols(PyObject *symbols, PyObject *scope,
i = PyInt_AS_LONG(w);
flags |= (i << SCOPE_OFF);
u = PyInt_FromLong(flags);
+ if (!u)
+ return 0;
if (PyDict_SetItem(symbols, name, u) < 0) {
Py_DECREF(u);
return 0;
@@ -723,11 +729,13 @@ symtable_exit_block(struct symtable *st, void *ast)
{
Py_ssize_t end;
- Py_DECREF(st->st_cur);
+ Py_CLEAR(st->st_cur);
end = PyList_GET_SIZE(st->st_stack) - 1;
if (end >= 0) {
st->st_cur = (PySTEntryObject *)PyList_GET_ITEM(st->st_stack,
end);
+ if (st->st_cur == NULL)
+ return 0;
Py_INCREF(st->st_cur);
if (PySequence_DelItem(st->st_stack, end) < 0)
return 0;
@@ -749,6 +757,8 @@ symtable_enter_block(struct symtable *st, identifier name, _Py_block_ty block,
Py_DECREF(st->st_cur);
}
st->st_cur = PySTEntry_New(st, name, block, ast, lineno);
+ if (st->st_cur == NULL)
+ return 0;
if (name == GET_IDENTIFIER(top))
st->st_global = st->st_cur->ste_symbols;
if (prev) {
diff --git a/Python/sysmodule.c b/Python/sysmodule.c
index fe47fd19ba..a2df6691af 100644
--- a/Python/sysmodule.c
+++ b/Python/sysmodule.c
@@ -656,6 +656,21 @@ sys_getframe(PyObject *self, PyObject *args)
return (PyObject*)f;
}
+PyDoc_STRVAR(current_frames_doc,
+"_current_frames() -> dictionary\n\
+\n\
+Return a dictionary mapping each current thread T's thread id to T's\n\
+current stack frame.\n\
+\n\
+This function should be used for specialized purposes only."
+);
+
+static PyObject *
+sys_current_frames(PyObject *self, PyObject *noargs)
+{
+ return _PyThread_CurrentFrames();
+}
+
PyDoc_STRVAR(call_tracing_doc,
"call_tracing(func, args) -> object\n\
\n\
@@ -718,6 +733,8 @@ static PyMethodDef sys_methods[] = {
/* Might as well keep this in alphabetic order */
{"callstats", (PyCFunction)PyEval_GetCallStats, METH_NOARGS,
callstats_doc},
+ {"_current_frames", sys_current_frames, METH_NOARGS,
+ current_frames_doc},
{"displayhook", sys_displayhook, METH_O, displayhook_doc},
{"exc_info", sys_exc_info, METH_NOARGS, exc_info_doc},
{"exc_clear", sys_exc_clear, METH_NOARGS, exc_clear_doc},
@@ -1116,41 +1133,38 @@ _PySys_Init(void)
#elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_FINAL
s = "final";
#endif
- PyDict_SetItemString(sysdict, "version_info",
- v = Py_BuildValue("iiisi", PY_MAJOR_VERSION,
+
+#define SET_SYS_FROM_STRING(key, value) \
+ v = value; \
+ if (v != NULL) \
+ PyDict_SetItemString(sysdict, key, v); \
+ Py_XDECREF(v)
+
+ SET_SYS_FROM_STRING("version_info",
+ Py_BuildValue("iiisi", PY_MAJOR_VERSION,
PY_MINOR_VERSION,
PY_MICRO_VERSION, s,
PY_RELEASE_SERIAL));
- Py_XDECREF(v);
- PyDict_SetItemString(sysdict, "api_version",
- v = PyInt_FromLong(PYTHON_API_VERSION));
- Py_XDECREF(v);
- PyDict_SetItemString(sysdict, "copyright",
- v = PyString_FromString(Py_GetCopyright()));
- Py_XDECREF(v);
- PyDict_SetItemString(sysdict, "platform",
- v = PyString_FromString(Py_GetPlatform()));
- Py_XDECREF(v);
- PyDict_SetItemString(sysdict, "executable",
- v = PyString_FromString(Py_GetProgramFullPath()));
- Py_XDECREF(v);
- PyDict_SetItemString(sysdict, "prefix",
- v = PyString_FromString(Py_GetPrefix()));
- Py_XDECREF(v);
- PyDict_SetItemString(sysdict, "exec_prefix",
- v = PyString_FromString(Py_GetExecPrefix()));
- Py_XDECREF(v);
- PyDict_SetItemString(sysdict, "maxint",
- v = PyInt_FromLong(PyInt_GetMax()));
- Py_XDECREF(v);
+ SET_SYS_FROM_STRING("api_version",
+ PyInt_FromLong(PYTHON_API_VERSION));
+ SET_SYS_FROM_STRING("copyright",
+ PyString_FromString(Py_GetCopyright()));
+ SET_SYS_FROM_STRING("platform",
+ PyString_FromString(Py_GetPlatform()));
+ SET_SYS_FROM_STRING("executable",
+ PyString_FromString(Py_GetProgramFullPath()));
+ SET_SYS_FROM_STRING("prefix",
+ PyString_FromString(Py_GetPrefix()));
+ SET_SYS_FROM_STRING("exec_prefix",
+ PyString_FromString(Py_GetExecPrefix()));
+ SET_SYS_FROM_STRING("maxint",
+ PyInt_FromLong(PyInt_GetMax()));
#ifdef Py_USING_UNICODE
- PyDict_SetItemString(sysdict, "maxunicode",
- v = PyInt_FromLong(PyUnicode_GetMax()));
- Py_XDECREF(v);
+ SET_SYS_FROM_STRING("maxunicode",
+ PyInt_FromLong(PyUnicode_GetMax()));
#endif
- PyDict_SetItemString(sysdict, "builtin_module_names",
- v = list_builtin_module_names());
- Py_XDECREF(v);
+ SET_SYS_FROM_STRING("builtin_module_names",
+ list_builtin_module_names());
{
/* Assumes that longs are at least 2 bytes long.
Should be safe! */
@@ -1162,18 +1176,16 @@ _PySys_Init(void)
value = "big";
else
value = "little";
- PyDict_SetItemString(sysdict, "byteorder",
- v = PyString_FromString(value));
- Py_XDECREF(v);
+ SET_SYS_FROM_STRING("byteorder",
+ PyString_FromString(value));
}
#ifdef MS_COREDLL
- PyDict_SetItemString(sysdict, "dllhandle",
- v = PyLong_FromVoidPtr(PyWin_DLLhModule));
- Py_XDECREF(v);
- PyDict_SetItemString(sysdict, "winver",
- v = PyString_FromString(PyWin_DLLVersionString));
- Py_XDECREF(v);
+ SET_SYS_FROM_STRING("dllhandle",
+ PyLong_FromVoidPtr(PyWin_DLLhModule));
+ SET_SYS_FROM_STRING("winver",
+ PyString_FromString(PyWin_DLLVersionString));
#endif
+#undef SET_SYS_FROM_STRING
if (warnoptions == NULL) {
warnoptions = PyList_New(0);
}
diff --git a/Python/thread.c b/Python/thread.c
index c9356dcbfd..3a2c7af6ff 100644
--- a/Python/thread.c
+++ b/Python/thread.c
@@ -95,6 +95,11 @@ PyThread_init_thread(void)
PyThread__init_thread();
}
+/* Support for runtime thread stack size tuning.
+ A value of 0 means using the platform's default stack size
+ or the size specified by the THREAD_STACK_SIZE macro. */
+static size_t _pythread_stacksize = 0;
+
#ifdef SGI_THREADS
#include "thread_sgi.h"
#endif
@@ -150,6 +155,28 @@ PyThread_init_thread(void)
#endif
*/
+/* return the current thread stack size */
+size_t
+PyThread_get_stacksize(void)
+{
+ return _pythread_stacksize;
+}
+
+/* Only platforms defining a THREAD_SET_STACKSIZE() macro
+ in thread_<platform>.h support changing the stack size.
+ Return 0 if stack size is valid,
+ -1 if stack size value is invalid,
+ -2 if setting stack size is not supported. */
+int
+PyThread_set_stacksize(size_t size)
+{
+#if defined(THREAD_SET_STACKSIZE)
+ return THREAD_SET_STACKSIZE(size);
+#else
+ return -2;
+#endif
+}
+
#ifndef Py_HAVE_NATIVE_TLS
/* If the platform has not supplied a platform specific
TLS implementation, provide our own.
@@ -240,6 +267,8 @@ find_key(int key, void *value)
struct key *p;
long id = PyThread_get_thread_ident();
+ if (!keymutex)
+ return NULL;
PyThread_acquire_lock(keymutex, 1);
for (p = keyhead; p != NULL; p = p->next) {
if (p->id == id && p->key == key)
diff --git a/Python/thread_nt.h b/Python/thread_nt.h
index 0b7e84ece1..67f5ed5174 100644
--- a/Python/thread_nt.h
+++ b/Python/thread_nt.h
@@ -5,7 +5,9 @@
#include <windows.h>
#include <limits.h>
+#ifdef HAVE_PROCESS_H
#include <process.h>
+#endif
typedef struct NRMUTEX {
LONG owned ;
@@ -194,7 +196,7 @@ PyThread_start_new_thread(void (*func)(void *), void *arg)
if (obj.done == NULL)
return -1;
- rv = _beginthread(bootstrap, 0, &obj); /* use default stack size */
+ rv = _beginthread(bootstrap, _pythread_stacksize, &obj);
if (rv == (Py_uintptr_t)-1) {
/* I've seen errno == EAGAIN here, which means "there are
* too many threads".
@@ -333,3 +335,30 @@ PyThread_release_lock(PyThread_type_lock aLock)
if (!(aLock && LeaveNonRecursiveMutex((PNRMUTEX) aLock)))
dprintf(("%ld: Could not PyThread_release_lock(%p) error: %l\n", PyThread_get_thread_ident(), aLock, GetLastError()));
}
+
+/* minimum/maximum thread stack sizes supported */
+#define THREAD_MIN_STACKSIZE 0x8000 /* 32kB */
+#define THREAD_MAX_STACKSIZE 0x10000000 /* 256MB */
+
+/* set the thread stack size.
+ * Return 0 if size is valid, -1 otherwise.
+ */
+static int
+_pythread_nt_set_stacksize(size_t size)
+{
+ /* set to default */
+ if (size == 0) {
+ _pythread_stacksize = 0;
+ return 0;
+ }
+
+ /* valid range? */
+ if (size >= THREAD_MIN_STACKSIZE && size < THREAD_MAX_STACKSIZE) {
+ _pythread_stacksize = size;
+ return 0;
+ }
+
+ return -1;
+}
+
+#define THREAD_SET_STACKSIZE(x) _pythread_nt_set_stacksize(x)
diff --git a/Python/thread_os2.h b/Python/thread_os2.h
index 86e91c1e1a..12eeed51c4 100644
--- a/Python/thread_os2.h
+++ b/Python/thread_os2.h
@@ -14,10 +14,13 @@
long PyThread_get_thread_ident(void);
#endif
+/* default thread stack size of 64kB */
#if !defined(THREAD_STACK_SIZE)
#define THREAD_STACK_SIZE 0x10000
#endif
+#define OS2_STACKSIZE(x) (x ? x : THREAD_STACK_SIZE)
+
/*
* Initialization of the C package, should not be needed.
*/
@@ -32,18 +35,18 @@ PyThread__init_thread(void)
long
PyThread_start_new_thread(void (*func)(void *), void *arg)
{
- int aThread;
- int success = 0;
+ int thread_id;
- aThread = _beginthread(func, NULL, THREAD_STACK_SIZE, arg);
+ thread_id = _beginthread(func,
+ NULL,
+ OS2_STACKSIZE(_pythread_stacksize),
+ arg);
- if (aThread == -1) {
- success = -1;
- fprintf(stderr, "aThread failed == %d", aThread);
+ if (thread_id == -1) {
dprintf(("_beginthread failed. return %ld\n", errno));
}
- return success;
+ return thread_id;
}
long
@@ -275,3 +278,30 @@ PyThread_release_lock(PyThread_type_lock aLock)
DosExitCritSec();
#endif
}
+
+/* minimum/maximum thread stack sizes supported */
+#define THREAD_MIN_STACKSIZE 0x8000 /* 32kB */
+#define THREAD_MAX_STACKSIZE 0x2000000 /* 32MB */
+
+/* set the thread stack size.
+ * Return 0 if size is valid, -1 otherwise.
+ */
+static int
+_pythread_os2_set_stacksize(size_t size)
+{
+ /* set to default */
+ if (size == 0) {
+ _pythread_stacksize = 0;
+ return 0;
+ }
+
+ /* valid range? */
+ if (size >= THREAD_MIN_STACKSIZE && size < THREAD_MAX_STACKSIZE) {
+ _pythread_stacksize = size;
+ return 0;
+ }
+
+ return -1;
+}
+
+#define THREAD_SET_STACKSIZE(x) _pythread_os2_set_stacksize(x)
diff --git a/Python/thread_pthread.h b/Python/thread_pthread.h
index c29a61c809..60d2fb216e 100644
--- a/Python/thread_pthread.h
+++ b/Python/thread_pthread.h
@@ -12,6 +12,20 @@
#endif
#include <signal.h>
+/* The POSIX spec requires that use of pthread_attr_setstacksize
+ be conditional on _POSIX_THREAD_ATTR_STACKSIZE being defined. */
+#ifdef _POSIX_THREAD_ATTR_STACKSIZE
+#ifndef THREAD_STACK_SIZE
+#define THREAD_STACK_SIZE 0 /* use default stack size */
+#endif
+/* for safety, ensure a viable minimum stacksize */
+#define THREAD_STACK_MIN 0x8000 /* 32kB */
+#else /* !_POSIX_THREAD_ATTR_STACKSIZE */
+#ifdef THREAD_STACK_SIZE
+#error "THREAD_STACK_SIZE defined but _POSIX_THREAD_ATTR_STACKSIZE undefined"
+#endif
+#endif
+
/* The POSIX spec says that implementations supporting the sem_*
family of functions must indicate this by defining
_POSIX_SEMAPHORES. */
@@ -138,15 +152,27 @@ PyThread_start_new_thread(void (*func)(void *), void *arg)
#if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
pthread_attr_t attrs;
#endif
+#if defined(THREAD_STACK_SIZE)
+ size_t tss;
+#endif
+
dprintf(("PyThread_start_new_thread called\n"));
if (!initialized)
PyThread_init_thread();
#if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
- pthread_attr_init(&attrs);
+ if (pthread_attr_init(&attrs) != 0)
+ return -1;
#endif
-#ifdef THREAD_STACK_SIZE
- pthread_attr_setstacksize(&attrs, THREAD_STACK_SIZE);
+#if defined(THREAD_STACK_SIZE)
+ tss = (_pythread_stacksize != 0) ? _pythread_stacksize
+ : THREAD_STACK_SIZE;
+ if (tss != 0) {
+ if (pthread_attr_setstacksize(&attrs, tss) != 0) {
+ pthread_attr_destroy(&attrs);
+ return -1;
+ }
+ }
#endif
#if defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
pthread_attr_setscope(&attrs, PTHREAD_SCOPE_SYSTEM);
@@ -460,3 +486,48 @@ PyThread_release_lock(PyThread_type_lock lock)
}
#endif /* USE_SEMAPHORES */
+
+/* set the thread stack size.
+ * Return 0 if size is valid, -1 if size is invalid,
+ * -2 if setting stack size is not supported.
+ */
+static int
+_pythread_pthread_set_stacksize(size_t size)
+{
+#if defined(THREAD_STACK_SIZE)
+ pthread_attr_t attrs;
+ size_t tss_min;
+ int rc = 0;
+#endif
+
+ /* set to default */
+ if (size == 0) {
+ _pythread_stacksize = 0;
+ return 0;
+ }
+
+#if defined(THREAD_STACK_SIZE)
+#if defined(PTHREAD_STACK_MIN)
+ tss_min = PTHREAD_STACK_MIN > THREAD_STACK_MIN ? PTHREAD_STACK_MIN
+ : THREAD_STACK_MIN;
+#else
+ tss_min = THREAD_STACK_MIN;
+#endif
+ if (size >= tss_min) {
+ /* validate stack size by setting thread attribute */
+ if (pthread_attr_init(&attrs) == 0) {
+ rc = pthread_attr_setstacksize(&attrs, size);
+ pthread_attr_destroy(&attrs);
+ if (rc == 0) {
+ _pythread_stacksize = size;
+ return 0;
+ }
+ }
+ }
+ return -1;
+#else
+ return -2;
+#endif
+}
+
+#define THREAD_SET_STACKSIZE(x) _pythread_pthread_set_stacksize(x)