Skip to content

Commit

Permalink
* import.c (get_module): pass .py filename to parse_file, not .pyc fi…
Browse files Browse the repository at this point in the history
…lename!

* funcobject.c (func_repr): don't call getstringvalue(None) for anonymous
  functions.
* bltinmodule.c: removed lambda (which is now a built-in function);
  removed implied lambda for string arg to filter/map/reduce.
* Grammar, graminit.[ch], compile.[ch]: replaced lambda as built-in
  function by lambda as grammar entity: instead of "lambda('x: x+1')" you
  write "lambda x: x+1".
* Xtmodule.c (checkargdict): return 0, not NULL, for error.
  • Loading branch information
gvanrossum committed Nov 30, 1993
1 parent 8732d6a commit 590baa4
Show file tree
Hide file tree
Showing 8 changed files with 740 additions and 766 deletions.
12 changes: 8 additions & 4 deletions Grammar/Grammar
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

# Change log:

# 30-Nov-93:
# Removed lambda_input, added lambdef

# 25-Oct-93:
# Added lambda_input

Expand Down Expand Up @@ -77,14 +80,12 @@
# Start symbols for the grammar:
# single_input is a single interactive statement;
# file_input is a module or sequence of commands read from an input file;
# eval_input is the input for the eval() and input() functions;
# lambda_input is the input for the proposed lambda() function.
# eval_input is the input for the eval() and input() functions.

# NB: compound_stmt in single_input is followed by extra NEWLINE!
single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE
file_input: (NEWLINE | stmt)* ENDMARKER
eval_input: testlist NEWLINE* ENDMARKER
lambda_input: varargslist ':' testlist NEWLINE* ENDMARKER

funcdef: 'def' NAME parameters ':' suite
parameters: '(' [varargslist] ')'
Expand Down Expand Up @@ -134,7 +135,10 @@ shift_expr: arith_expr (('<<'|'>>') arith_expr)*
arith_expr: term (('+'|'-') term)*
term: factor (('*'|'/'|'%') factor)*
factor: ('+'|'-'|'~') factor | atom trailer*
atom: '(' [testlist] ')' | '[' [testlist] ']' | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING
atom: '(' [testlist] ')' | '[' [testlist] ']' | '{' [dictmaker] '}' | '`' testlist '`' | lambdef | NAME | NUMBER | STRING
# Note ambiguity in grammar: "lambda x: x[1]" could mean "(lambda x: x)[1]"
# but the parser is eager so interprets it as "lambda x: (x[1])"...
lambdef: 'lambda' [varargslist] ':' test
trailer: '(' [testlist] ')' | '[' subscript ']' | '.' NAME
subscript: test | [test] ':' [test]
exprlist: expr (',' expr)* [',']
Expand Down
4 changes: 1 addition & 3 deletions Include/compile.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,10 @@ extern typeobject Codetype;

/* Public interface */
struct _node; /* Declare the existence of this type */
codeobject *_compile PROTO((struct _node *, char *, int));
codeobject *compile PROTO((struct _node *, char *));
codeobject *newcodeobject
PROTO((object *, object *, object *, object *, object *));

#define compile(n,f) (_compile((n),(f),0))

#ifdef __cplusplus
}
#endif
Expand Down
86 changes: 43 additions & 43 deletions Include/graminit.h
Original file line number Diff line number Diff line change
@@ -1,49 +1,49 @@
#define single_input 256
#define file_input 257
#define eval_input 258
#define lambda_input 259
#define funcdef 260
#define parameters 261
#define varargslist 262
#define fpdef 263
#define fplist 264
#define stmt 265
#define simple_stmt 266
#define small_stmt 267
#define expr_stmt 268
#define print_stmt 269
#define del_stmt 270
#define pass_stmt 271
#define flow_stmt 272
#define break_stmt 273
#define continue_stmt 274
#define return_stmt 275
#define raise_stmt 276
#define import_stmt 277
#define global_stmt 278
#define access_stmt 279
#define accesstype 280
#define exec_stmt 281
#define compound_stmt 282
#define if_stmt 283
#define while_stmt 284
#define for_stmt 285
#define try_stmt 286
#define except_clause 287
#define suite 288
#define test 289
#define and_test 290
#define not_test 291
#define comparison 292
#define comp_op 293
#define expr 294
#define xor_expr 295
#define and_expr 296
#define shift_expr 297
#define arith_expr 298
#define term 299
#define factor 300
#define atom 301
#define funcdef 259
#define parameters 260
#define varargslist 261
#define fpdef 262
#define fplist 263
#define stmt 264
#define simple_stmt 265
#define small_stmt 266
#define expr_stmt 267
#define print_stmt 268
#define del_stmt 269
#define pass_stmt 270
#define flow_stmt 271
#define break_stmt 272
#define continue_stmt 273
#define return_stmt 274
#define raise_stmt 275
#define import_stmt 276
#define global_stmt 277
#define access_stmt 278
#define accesstype 279
#define exec_stmt 280
#define compound_stmt 281
#define if_stmt 282
#define while_stmt 283
#define for_stmt 284
#define try_stmt 285
#define except_clause 286
#define suite 287
#define test 288
#define and_test 289
#define not_test 290
#define comparison 291
#define comp_op 292
#define expr 293
#define xor_expr 294
#define and_expr 295
#define shift_expr 296
#define arith_expr 297
#define term 298
#define factor 299
#define atom 300
#define lambdef 301
#define trailer 302
#define subscript 303
#define exprlist 304
Expand Down
9 changes: 6 additions & 3 deletions Objects/funcobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,12 @@ func_repr(op)
funcobject *op;
{
char buf[140];
sprintf(buf, "<function %.100s at %lx>",
getstringvalue(op->func_name),
(long)op);
if (op->func_name == None)
sprintf(buf, "<anonymous function at %lx>", (long)op);
else
sprintf(buf, "<function %.100s at %lx>",
getstringvalue(op->func_name),
(long)op);
return newstringobject(buf);
}

Expand Down
47 changes: 3 additions & 44 deletions Python/bltinmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,23 +79,13 @@ builtin_filter(self, args)
if (!getargs(args, "(OO)", &func, &seq))
return NULL;

if (is_stringobject(func)) {
if ((func = exec_eval(func, lambda_input)) == NULL)
return NULL;
}
else {
INCREF(func);
}

if (is_stringobject(seq)) {
object *r = filterstring(func, seq);
DECREF(func);
return r;
}

if (is_tupleobject(seq)) {
object *r = filtertuple(func, seq);
DECREF(func);
return r;
}

Expand Down Expand Up @@ -150,13 +140,11 @@ builtin_filter(self, args)
if (setlistslice(result, j, len, NULL) < 0)
goto Fail_1;

DECREF(func);
return result;

Fail_1:
DECREF(result);
Fail_2:
DECREF(func);
return NULL;
}

Expand Down Expand Up @@ -306,10 +294,10 @@ exec_eval(v, start)
globals != NULL && !is_dictobject(globals) ||
locals != NULL && !is_dictobject(locals)) {
err_setstr(TypeError,
"eval/lambda arguments must be (string|code)[,dict[,dict]]");
"eval arguments must be (string|code)[,dict[,dict]]");
return NULL;
}
/* XXX The following is only correct for eval(), not for lambda() */

if (is_codeobject(str))
return eval_code((codeobject *) str, globals, locals,
(object *)NULL, (object *)NULL);
Expand All @@ -318,7 +306,7 @@ exec_eval(v, start)
err_setstr(ValueError, "embedded '\\0' in string arg");
return NULL;
}
if (start == eval_input || start == lambda_input) {
if (start == eval_input) {
while (*s == ' ' || *s == '\t')
s++;
}
Expand Down Expand Up @@ -460,14 +448,6 @@ builtin_map(self, args)
func = gettupleitem(args, 0);
n = gettuplesize(args) - 1;

if (is_stringobject(func)) {
if ((func = exec_eval(func, lambda_input)) == NULL)
return NULL;
}
else {
INCREF(func);
}

if ((seqs = NEW(sequence, n)) == NULL) {
err_nomem();
goto Fail_2;
Expand Down Expand Up @@ -549,13 +529,11 @@ builtin_map(self, args)
}

if (seqs) DEL(seqs);
DECREF(func);
return result;

Fail_1:
DECREF(result);
Fail_2:
DECREF(func);
if (seqs) DEL(seqs);
return NULL;
}
Expand Down Expand Up @@ -638,14 +616,6 @@ builtin_int(self, v)
return (*nb->nb_int)(v);
}

static object *
builtin_lambda(self, v)
object *self;
object *v;
{
return exec_eval(v, lambda_input);
}

static object *
builtin_len(self, v)
object *self;
Expand Down Expand Up @@ -977,14 +947,6 @@ builtin_reduce(self, args)
return NULL;
}

if (is_stringobject(func)) {
if ((func = exec_eval(func, lambda_input)) == NULL)
return NULL;
}
else {
INCREF(func);
}

if ((len = (*sqf->sq_length)(seq)) < 0)
goto Fail_2;

Expand Down Expand Up @@ -1025,7 +987,6 @@ builtin_reduce(self, args)
}

DECREF(args);
DECREF(func);

return result;

Expand All @@ -1035,7 +996,6 @@ builtin_reduce(self, args)
Fail_1:
DECREF(result);
Fail_2:
DECREF(func);
return NULL;
}

Expand Down Expand Up @@ -1133,7 +1093,6 @@ static struct methodlist builtin_methods[] = {
{"id", builtin_id},
{"input", builtin_input},
{"int", builtin_int},
{"lambda", builtin_lambda},
{"len", builtin_len},
{"long", builtin_long},
{"map", builtin_map},
Expand Down
Loading

0 comments on commit 590baa4

Please sign in to comment.