Skip to content

Commit

Permalink
The BDFL has retired! Long live the FLUFL (Friendly Language Uncle Fo…
Browse files Browse the repository at this point in the history
…r Life)!
  • Loading branch information
brettcannon committed Apr 1, 2009
1 parent 4ed72ac commit e3944a5
Show file tree
Hide file tree
Showing 13 changed files with 168 additions and 121 deletions.
2 changes: 1 addition & 1 deletion Grammar/Grammar
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ or_test: and_test ('or' and_test)*
and_test: not_test ('and' not_test)*
not_test: 'not' not_test | comparison
comparison: star_expr (comp_op star_expr)*
comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is'|'is' 'not'
comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not'
star_expr: ['*'] expr
expr: xor_expr ('|' xor_expr)*
xor_expr: and_expr ('^' and_expr)*
Expand Down
4 changes: 3 additions & 1 deletion Include/code.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,12 @@ typedef struct {
#define CO_FUTURE_UNICODE_LITERALS 0x20000
#endif

#define CO_FUTURE_BARRY_AS_BDFL 0x40000

/* This should be defined if a future statement modifies the syntax.
For example, when a keyword is added.
*/
/* #define PY_PARSER_REQUIRES_FUTURE_KEYWORD */
#define PY_PARSER_REQUIRES_FUTURE_KEYWORD

#define CO_MAXBLOCKS 20 /* Max static block nesting within a function */

Expand Down
1 change: 1 addition & 0 deletions Include/compile.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ typedef struct {
#define FUTURE_WITH_STATEMENT "with_statement"
#define FUTURE_PRINT_FUNCTION "print_function"
#define FUTURE_UNICODE_LITERALS "unicode_literals"
#define FUTURE_BARRY_AS_BDFL "barry_as_FLUFL"

struct _mod; /* Declare the existence of this type */
PyAPI_FUNC(PyCodeObject *) PyAST_Compile(struct _mod *, const char *,
Expand Down
1 change: 1 addition & 0 deletions Include/parsetok.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ typedef struct {
#endif

#define PyPARSE_IGNORE_COOKIE 0x0010
#define PyPARSE_BARRY_AS_BDFL 0x0020

PyAPI_FUNC(node *) PyParser_ParseString(const char *, grammar *, int,
perrdetail *);
Expand Down
2 changes: 1 addition & 1 deletion Include/pythonrun.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
extern "C" {
#endif

#define PyCF_MASK 0
#define PyCF_MASK CO_FUTURE_BARRY_AS_BDFL
#define PyCF_MASK_OBSOLETE 0
#define PyCF_SOURCE_IS_UTF8 0x0100
#define PyCF_DONT_IMPLY_DEDENT 0x0200
Expand Down
5 changes: 5 additions & 0 deletions Lib/__future__.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
CO_FUTURE_WITH_STATEMENT = 0x8000 # with statement
CO_FUTURE_PRINT_FUNCTION = 0x10000 # print function
CO_FUTURE_UNICODE_LITERALS = 0x20000 # unicode string literals
CO_FUTURE_BARRY_AS_BDFL = 0x40000

class _Feature:
def __init__(self, optionalRelease, mandatoryRelease, compiler_flag):
Expand Down Expand Up @@ -126,3 +127,7 @@ def __repr__(self):
unicode_literals = _Feature((2, 6, 0, "alpha", 2),
(3, 0, 0, "alpha", 0),
CO_FUTURE_UNICODE_LITERALS)

barry_as_FLUFL = _Feature((3, 1, 0, "alpha", 2),
(3, 9, 0, "alpha", 0),
CO_FUTURE_BARRY_AS_BDFL)
27 changes: 27 additions & 0 deletions Lib/test/test_flufl.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import __future__
import unittest

class FLUFLTests(unittest.TestCase):

def test_barry_as_bdfl(self):
code = "from __future__ import barry_as_FLUFL; 2 {0} 3"
compile(code.format('<>'), '<BDFL test>', 'exec',
__future__.CO_FUTURE_BARRY_AS_BDFL)
self.assertRaises(SyntaxError, compile, code.format('!='),
'<FLUFL test>', 'exec',
__future__.CO_FUTURE_BARRY_AS_BDFL)

def test_guido_as_bdfl(self):
code = '2 {0} 3'
compile(code.format('!='), '<BDFL test>', 'exec')
self.assertRaises(SyntaxError, compile, code.format('<>'),
'<FLUFL test>', 'exec')


def test_main():
from test.support import run_unittest
run_unittest(FLUFLTests)


if __name__ == '__main__':
test_main()
8 changes: 8 additions & 0 deletions Parser/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,15 @@ classify(parser_state *ps, int type, char *str)
strcmp(l->lb_str, s) != 0)
continue;
#ifdef PY_PARSER_REQUIRES_FUTURE_KEYWORD
#if 0
/* Leaving this in as an example */
if (!(ps->p_flags & CO_FUTURE_WITH_STATEMENT)) {
if (s[0] == 'w' && strcmp(s, "with") == 0)
break; /* not a keyword yet */
else if (s[0] == 'a' && strcmp(s, "as") == 0)
break; /* not a keyword yet */
}
#endif
#endif
D(printf("It's a keyword\n"));
return n - i;
Expand All @@ -178,6 +180,7 @@ classify(parser_state *ps, int type, char *str)
}

#ifdef PY_PARSER_REQUIRES_FUTURE_KEYWORD
#if 0
/* Leaving this in as an example */
static void
future_hack(parser_state *ps)
Expand Down Expand Up @@ -218,6 +221,7 @@ future_hack(parser_state *ps)
}
}
}
#endif
#endif /* future keyword */

int
Expand Down Expand Up @@ -278,10 +282,12 @@ PyParser_AddToken(register parser_state *ps, register int type, char *str,
d->d_name,
ps->p_stack.s_top->s_state));
#ifdef PY_PARSER_REQUIRES_FUTURE_KEYWORD
#if 0
if (d->d_name[0] == 'i' &&
strcmp(d->d_name,
"import_stmt") == 0)
future_hack(ps);
#endif
#endif
s_pop(&ps->p_stack);
if (s_empty(&ps->p_stack)) {
Expand All @@ -296,9 +302,11 @@ PyParser_AddToken(register parser_state *ps, register int type, char *str,

if (s->s_accept) {
#ifdef PY_PARSER_REQUIRES_FUTURE_KEYWORD
#if 0
if (d->d_name[0] == 'i' &&
strcmp(d->d_name, "import_stmt") == 0)
future_hack(ps);
#endif
#endif
/* Pop this dfa and try again */
s_pop(&ps->p_stack);
Expand Down
38 changes: 17 additions & 21 deletions Parser/parsetok.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ PyParser_ParseFileFlagsEx(FILE *fp, const char *filename,
}

#ifdef PY_PARSER_REQUIRES_FUTURE_KEYWORD
#if 0
static char with_msg[] =
"%s:%d: Warning: 'with' will become a reserved keyword in Python 2.6\n";

Expand All @@ -114,6 +115,7 @@ warn(const char *msg, const char *filename, int lineno)
PySys_WriteStderr(msg, filename, lineno);
}
#endif
#endif

/* Parse input coming from the given tokenizer structure.
Return error code. */
Expand All @@ -133,8 +135,8 @@ parsetok(struct tok_state *tok, grammar *g, int start, perrdetail *err_ret,
return NULL;
}
#ifdef PY_PARSER_REQUIRES_FUTURE_KEYWORD
if (*flags & PyPARSE_WITH_IS_KEYWORD)
ps->p_flags |= CO_FUTURE_WITH_STATEMENT;
if (*flags & PyPARSE_BARRY_AS_BDFL)
ps->p_flags |= CO_FUTURE_BARRY_AS_BDFL;
#endif

for (;;) {
Expand Down Expand Up @@ -177,26 +179,20 @@ parsetok(struct tok_state *tok, grammar *g, int start, perrdetail *err_ret,
str[len] = '\0';

#ifdef PY_PARSER_REQUIRES_FUTURE_KEYWORD
/* This is only necessary to support the "as" warning, but
we don't want to warn about "as" in import statements. */
if (type == NAME &&
len == 6 && str[0] == 'i' && strcmp(str, "import") == 0)
handling_import = 1;

/* Warn about with as NAME */
if (type == NAME &&
!(ps->p_flags & CO_FUTURE_WITH_STATEMENT)) {
if (len == 4 && str[0] == 'w' && strcmp(str, "with") == 0)
warn(with_msg, err_ret->filename, tok->lineno);
else if (!(handling_import || handling_with) &&
len == 2 && str[0] == 'a' &&
strcmp(str, "as") == 0)
warn(as_msg, err_ret->filename, tok->lineno);
if (type == NOTEQUAL) {
if (!(ps->p_flags & CO_FUTURE_BARRY_AS_BDFL) &&
strcmp(str, "!=")) {
err_ret->error = E_SYNTAX;
break;
}
else if ((ps->p_flags & CO_FUTURE_BARRY_AS_BDFL) &&
strcmp(str, "<>")) {
err_ret->text = "with Barry as BDFL, use '<>' "
"instead of '!='";
err_ret->error = E_SYNTAX;
break;
}
}
else if (type == NAME &&
(ps->p_flags & CO_FUTURE_WITH_STATEMENT) &&
len == 4 && str[0] == 'w' && strcmp(str, "with") == 0)
handling_with = 1;
#endif
if (a >= tok->line_start)
col_offset = a - tok->line_start;
Expand Down
1 change: 1 addition & 0 deletions Parser/tokenizer.c
Original file line number Diff line number Diff line change
Expand Up @@ -1040,6 +1040,7 @@ PyToken_TwoChars(int c1, int c2)
break;
case '<':
switch (c2) {
case '>': return NOTEQUAL;
case '=': return LESSEQUAL;
case '<': return LEFTSHIFT;
}
Expand Down
2 changes: 2 additions & 0 deletions Python/future.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ future_check_features(PyFutureFeatures *ff, stmt_ty s, const char *filename)
continue;
} else if (strcmp(feature, FUTURE_UNICODE_LITERALS) == 0) {
continue;
} else if (strcmp(feature, FUTURE_BARRY_AS_BDFL) == 0) {
ff->ff_features |= CO_FUTURE_BARRY_AS_BDFL;
} else if (strcmp(feature, "braces") == 0) {
PyErr_SetString(PyExc_SyntaxError,
"not a chance");
Expand Down
Loading

0 comments on commit e3944a5

Please sign in to comment.