view Python/future.c @ 50076:c7abfab4dcdb 3.0

Merged revisions 67525-67527,67529,67548-67549,67551-67552,67555,67578,67586,67593-67594,67596,67605-67606,67609,67612,67616-67617,67632,67635,67638 via svnmerge from svn+ssh://svn.python.org/python/branches/py3k ........ r67525 | georg.brandl | 2008-12-04 19:18:16 +0100 (Thu, 04 Dec 2008) | 2 lines #4527: fix "unicode or string". ........ r67526 | georg.brandl | 2008-12-04 19:19:41 +0100 (Thu, 04 Dec 2008) | 2 lines #4526: fix typo. ........ r67527 | georg.brandl | 2008-12-04 19:21:46 +0100 (Thu, 04 Dec 2008) | 2 lines #4521: fix getcwdu() vs getcwdb(). ........ r67529 | georg.brandl | 2008-12-04 19:28:38 +0100 (Thu, 04 Dec 2008) | 2 lines #4513: remove traces of zip() docstring from when it was izip(). ........ r67548 | guido.van.rossum | 2008-12-05 06:49:12 +0100 (Fri, 05 Dec 2008) | 3 lines Tweak verbiage and markup for relative/absolute import. Backport candidate. (I guess *all* changes to this file should be backported to the 3.0 branch.) ........ r67549 | martin.v.loewis | 2008-12-05 08:20:46 +0100 (Fri, 05 Dec 2008) | 2 lines Send mails to python-checkins now. ........ r67551 | georg.brandl | 2008-12-05 08:32:56 +0100 (Fri, 05 Dec 2008) | 2 lines #4355: refer to urlencode in the correct module. ........ r67552 | georg.brandl | 2008-12-05 08:45:54 +0100 (Fri, 05 Dec 2008) | 2 lines #4401: Re-add os.extsep. ........ r67555 | georg.brandl | 2008-12-05 08:57:51 +0100 (Fri, 05 Dec 2008) | 2 lines #4427: fix arguments for "y" BuildValue type. ........ r67578 | georg.brandl | 2008-12-05 16:12:15 +0100 (Fri, 05 Dec 2008) | 2 lines #4504, #4505: Update noddy examples in "Extending & Embedding". ........ r67586 | georg.brandl | 2008-12-05 16:55:41 +0100 (Fri, 05 Dec 2008) | 2 lines Manually merged r67583: move __import__ doc to the bottom. ........ r67593 | georg.brandl | 2008-12-05 19:04:41 +0100 (Fri, 05 Dec 2008) | 2 lines #4550: fix 2.x syntax in webservers howto. ........ r67594 | georg.brandl | 2008-12-05 19:06:58 +0100 (Fri, 05 Dec 2008) | 2 lines #4549: Mention nonlocal statement in tutorial section about scoping. ........ r67596 | georg.brandl | 2008-12-05 19:31:51 +0100 (Fri, 05 Dec 2008) | 2 lines Use :samp: role. ........ r67605 | georg.brandl | 2008-12-06 09:12:11 +0100 (Sat, 06 Dec 2008) | 2 lines #4557: remove 'c' typecode from array docs and add a note in whatsnew. ........ r67606 | georg.brandl | 2008-12-06 09:14:46 +0100 (Sat, 06 Dec 2008) | 2 lines cmp() is not really gone. ........ r67609 | georg.brandl | 2008-12-06 15:28:56 +0100 (Sat, 06 Dec 2008) | 2 lines #4562: fix zip() examples. ........ r67612 | skip.montanaro | 2008-12-06 18:25:02 +0100 (Sat, 06 Dec 2008) | 2 lines issue 4483 - _dbm build failures on systems with gdbm_compat lib. ........ r67616 | benjamin.peterson | 2008-12-06 19:32:13 +0100 (Sat, 06 Dec 2008) | 1 line there are only four asdl builtin types in py3k ........ r67617 | benjamin.peterson | 2008-12-06 19:33:20 +0100 (Sat, 06 Dec 2008) | 1 line update version number ........ r67632 | georg.brandl | 2008-12-07 15:09:20 +0100 (Sun, 07 Dec 2008) | 2 lines #4576: fix ob_type access. ........ r67635 | benjamin.peterson | 2008-12-07 15:58:03 +0100 (Sun, 07 Dec 2008) | 1 line document how to deal with binary standard streams ........ r67638 | benjamin.peterson | 2008-12-07 16:07:02 +0100 (Sun, 07 Dec 2008) | 1 line remove extraneous 'text' ........
author Georg Brandl <georg@python.org>
date Sun, 07 Dec 2008 15:52:13 +0000
parents bd0e9a9cd283
children 0530aadff696
line wrap: on
line source

#include "Python.h"
#include "Python-ast.h"
#include "node.h"
#include "token.h"
#include "graminit.h"
#include "code.h"
#include "compile.h"
#include "symtable.h"

#define UNDEFINED_FUTURE_FEATURE "future feature %.100s is not defined"
#define ERR_LATE_FUTURE \
"from __future__ imports must occur at the beginning of the file"

static int
future_check_features(PyFutureFeatures *ff, stmt_ty s, const char *filename)
{
	int i;
	asdl_seq *names;

	assert(s->kind == ImportFrom_kind);

	names = s->v.ImportFrom.names;
	for (i = 0; i < asdl_seq_LEN(names); i++) {
                alias_ty name = (alias_ty)asdl_seq_GET(names, i);
		const char *feature = _PyUnicode_AsString(name->name);
		if (!feature)
			return 0;
		if (strcmp(feature, FUTURE_NESTED_SCOPES) == 0) {
			continue;
		} else if (strcmp(feature, FUTURE_GENERATORS) == 0) {
			continue;
		} else if (strcmp(feature, FUTURE_DIVISION) == 0) {
			continue;
		} else if (strcmp(feature, FUTURE_ABSOLUTE_IMPORT) == 0) {
			continue;
		} else if (strcmp(feature, FUTURE_WITH_STATEMENT) == 0) {
			continue;
		} else if (strcmp(feature, FUTURE_PRINT_FUNCTION) == 0) {
			continue;
		} else if (strcmp(feature, FUTURE_UNICODE_LITERALS) == 0) {
			continue;
		} else if (strcmp(feature, "braces") == 0) {
			PyErr_SetString(PyExc_SyntaxError,
					"not a chance");
			PyErr_SyntaxLocation(filename, s->lineno);
			return 0;
		} else {
			PyErr_Format(PyExc_SyntaxError,
				     UNDEFINED_FUTURE_FEATURE, feature);
			PyErr_SyntaxLocation(filename, s->lineno);
			return 0;
		}
	}
	return 1;
}

static int
future_parse(PyFutureFeatures *ff, mod_ty mod, const char *filename)
{
	int i, found_docstring = 0, done = 0, prev_line = 0;

	static PyObject *future;
	if (!future) {
		future = PyUnicode_InternFromString("__future__");
		if (!future)
			return 0;
	}

	if (!(mod->kind == Module_kind || mod->kind == Interactive_kind))
		return 1;

	/* A subsequent pass will detect future imports that don't
	   appear at the beginning of the file.  There's one case,
	   however, that is easier to handle here: A series of imports
	   joined by semi-colons, where the first import is a future
	   statement but some subsequent import has the future form
	   but is preceded by a regular import.
	*/
	   

	for (i = 0; i < asdl_seq_LEN(mod->v.Module.body); i++) {
		stmt_ty s = (stmt_ty)asdl_seq_GET(mod->v.Module.body, i);

		if (done && s->lineno > prev_line)
			return 1;
		prev_line = s->lineno;

		/* The tests below will return from this function unless it is
		   still possible to find a future statement.  The only things
		   that can precede a future statement are another future
		   statement and a doc string.
		*/

		if (s->kind == ImportFrom_kind) {
			if (s->v.ImportFrom.module == future) {
				if (done) {
					PyErr_SetString(PyExc_SyntaxError,
							ERR_LATE_FUTURE);
					PyErr_SyntaxLocation(filename, 
							     s->lineno);
					return 0;
				}
				if (!future_check_features(ff, s, filename))
					return 0;
				ff->ff_lineno = s->lineno;
			}
			else
				done = 1;
		}
		else if (s->kind == Expr_kind && !found_docstring) {
			expr_ty e = s->v.Expr.value;
			if (e->kind != Str_kind)
				done = 1;
			else
				found_docstring = 1;
		}
		else
			done = 1;
	}
	return 1;
}


PyFutureFeatures *
PyFuture_FromAST(mod_ty mod, const char *filename)
{
	PyFutureFeatures *ff;

	ff = (PyFutureFeatures *)PyObject_Malloc(sizeof(PyFutureFeatures));
	if (ff == NULL) {
		PyErr_NoMemory();
		return NULL;
	}
	ff->ff_features = 0;
	ff->ff_lineno = -1;

	if (!future_parse(ff, mod, filename)) {
		PyObject_Free(ff);
		return NULL;
	}
	return ff;
}