Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 590baa4

Browse files
committed
* import.c (get_module): pass .py filename to parse_file, not .pyc filename!
* 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.
1 parent 8732d6a commit 590baa4

8 files changed

Lines changed: 740 additions & 766 deletions

File tree

Grammar/Grammar

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
# Change log:
44

5+
# 30-Nov-93:
6+
# Removed lambda_input, added lambdef
7+
58
# 25-Oct-93:
69
# Added lambda_input
710

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

8385
# NB: compound_stmt in single_input is followed by extra NEWLINE!
8486
single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE
8587
file_input: (NEWLINE | stmt)* ENDMARKER
8688
eval_input: testlist NEWLINE* ENDMARKER
87-
lambda_input: varargslist ':' testlist NEWLINE* ENDMARKER
8889

8990
funcdef: 'def' NAME parameters ':' suite
9091
parameters: '(' [varargslist] ')'
@@ -134,7 +135,10 @@ shift_expr: arith_expr (('<<'|'>>') arith_expr)*
134135
arith_expr: term (('+'|'-') term)*
135136
term: factor (('*'|'/'|'%') factor)*
136137
factor: ('+'|'-'|'~') factor | atom trailer*
137-
atom: '(' [testlist] ')' | '[' [testlist] ']' | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING
138+
atom: '(' [testlist] ')' | '[' [testlist] ']' | '{' [dictmaker] '}' | '`' testlist '`' | lambdef | NAME | NUMBER | STRING
139+
# Note ambiguity in grammar: "lambda x: x[1]" could mean "(lambda x: x)[1]"
140+
# but the parser is eager so interprets it as "lambda x: (x[1])"...
141+
lambdef: 'lambda' [varargslist] ':' test
138142
trailer: '(' [testlist] ')' | '[' subscript ']' | '.' NAME
139143
subscript: test | [test] ':' [test]
140144
exprlist: expr (',' expr)* [',']

Include/compile.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,10 @@ extern typeobject Codetype;
5454

5555
/* Public interface */
5656
struct _node; /* Declare the existence of this type */
57-
codeobject *_compile PROTO((struct _node *, char *, int));
57+
codeobject *compile PROTO((struct _node *, char *));
5858
codeobject *newcodeobject
5959
PROTO((object *, object *, object *, object *, object *));
6060

61-
#define compile(n,f) (_compile((n),(f),0))
62-
6361
#ifdef __cplusplus
6462
}
6563
#endif

Include/graminit.h

Lines changed: 43 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,49 @@
11
#define single_input 256
22
#define file_input 257
33
#define eval_input 258
4-
#define lambda_input 259
5-
#define funcdef 260
6-
#define parameters 261
7-
#define varargslist 262
8-
#define fpdef 263
9-
#define fplist 264
10-
#define stmt 265
11-
#define simple_stmt 266
12-
#define small_stmt 267
13-
#define expr_stmt 268
14-
#define print_stmt 269
15-
#define del_stmt 270
16-
#define pass_stmt 271
17-
#define flow_stmt 272
18-
#define break_stmt 273
19-
#define continue_stmt 274
20-
#define return_stmt 275
21-
#define raise_stmt 276
22-
#define import_stmt 277
23-
#define global_stmt 278
24-
#define access_stmt 279
25-
#define accesstype 280
26-
#define exec_stmt 281
27-
#define compound_stmt 282
28-
#define if_stmt 283
29-
#define while_stmt 284
30-
#define for_stmt 285
31-
#define try_stmt 286
32-
#define except_clause 287
33-
#define suite 288
34-
#define test 289
35-
#define and_test 290
36-
#define not_test 291
37-
#define comparison 292
38-
#define comp_op 293
39-
#define expr 294
40-
#define xor_expr 295
41-
#define and_expr 296
42-
#define shift_expr 297
43-
#define arith_expr 298
44-
#define term 299
45-
#define factor 300
46-
#define atom 301
4+
#define funcdef 259
5+
#define parameters 260
6+
#define varargslist 261
7+
#define fpdef 262
8+
#define fplist 263
9+
#define stmt 264
10+
#define simple_stmt 265
11+
#define small_stmt 266
12+
#define expr_stmt 267
13+
#define print_stmt 268
14+
#define del_stmt 269
15+
#define pass_stmt 270
16+
#define flow_stmt 271
17+
#define break_stmt 272
18+
#define continue_stmt 273
19+
#define return_stmt 274
20+
#define raise_stmt 275
21+
#define import_stmt 276
22+
#define global_stmt 277
23+
#define access_stmt 278
24+
#define accesstype 279
25+
#define exec_stmt 280
26+
#define compound_stmt 281
27+
#define if_stmt 282
28+
#define while_stmt 283
29+
#define for_stmt 284
30+
#define try_stmt 285
31+
#define except_clause 286
32+
#define suite 287
33+
#define test 288
34+
#define and_test 289
35+
#define not_test 290
36+
#define comparison 291
37+
#define comp_op 292
38+
#define expr 293
39+
#define xor_expr 294
40+
#define and_expr 295
41+
#define shift_expr 296
42+
#define arith_expr 297
43+
#define term 298
44+
#define factor 299
45+
#define atom 300
46+
#define lambdef 301
4747
#define trailer 302
4848
#define subscript 303
4949
#define exprlist 304

Objects/funcobject.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,12 @@ func_repr(op)
100100
funcobject *op;
101101
{
102102
char buf[140];
103-
sprintf(buf, "<function %.100s at %lx>",
104-
getstringvalue(op->func_name),
105-
(long)op);
103+
if (op->func_name == None)
104+
sprintf(buf, "<anonymous function at %lx>", (long)op);
105+
else
106+
sprintf(buf, "<function %.100s at %lx>",
107+
getstringvalue(op->func_name),
108+
(long)op);
106109
return newstringobject(buf);
107110
}
108111

Python/bltinmodule.c

Lines changed: 3 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -79,23 +79,13 @@ builtin_filter(self, args)
7979
if (!getargs(args, "(OO)", &func, &seq))
8080
return NULL;
8181

82-
if (is_stringobject(func)) {
83-
if ((func = exec_eval(func, lambda_input)) == NULL)
84-
return NULL;
85-
}
86-
else {
87-
INCREF(func);
88-
}
89-
9082
if (is_stringobject(seq)) {
9183
object *r = filterstring(func, seq);
92-
DECREF(func);
9384
return r;
9485
}
9586

9687
if (is_tupleobject(seq)) {
9788
object *r = filtertuple(func, seq);
98-
DECREF(func);
9989
return r;
10090
}
10191

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

153-
DECREF(func);
154143
return result;
155144

156145
Fail_1:
157146
DECREF(result);
158147
Fail_2:
159-
DECREF(func);
160148
return NULL;
161149
}
162150

@@ -306,10 +294,10 @@ exec_eval(v, start)
306294
globals != NULL && !is_dictobject(globals) ||
307295
locals != NULL && !is_dictobject(locals)) {
308296
err_setstr(TypeError,
309-
"eval/lambda arguments must be (string|code)[,dict[,dict]]");
297+
"eval arguments must be (string|code)[,dict[,dict]]");
310298
return NULL;
311299
}
312-
/* XXX The following is only correct for eval(), not for lambda() */
300+
313301
if (is_codeobject(str))
314302
return eval_code((codeobject *) str, globals, locals,
315303
(object *)NULL, (object *)NULL);
@@ -318,7 +306,7 @@ exec_eval(v, start)
318306
err_setstr(ValueError, "embedded '\\0' in string arg");
319307
return NULL;
320308
}
321-
if (start == eval_input || start == lambda_input) {
309+
if (start == eval_input) {
322310
while (*s == ' ' || *s == '\t')
323311
s++;
324312
}
@@ -460,14 +448,6 @@ builtin_map(self, args)
460448
func = gettupleitem(args, 0);
461449
n = gettuplesize(args) - 1;
462450

463-
if (is_stringobject(func)) {
464-
if ((func = exec_eval(func, lambda_input)) == NULL)
465-
return NULL;
466-
}
467-
else {
468-
INCREF(func);
469-
}
470-
471451
if ((seqs = NEW(sequence, n)) == NULL) {
472452
err_nomem();
473453
goto Fail_2;
@@ -549,13 +529,11 @@ builtin_map(self, args)
549529
}
550530

551531
if (seqs) DEL(seqs);
552-
DECREF(func);
553532
return result;
554533

555534
Fail_1:
556535
DECREF(result);
557536
Fail_2:
558-
DECREF(func);
559537
if (seqs) DEL(seqs);
560538
return NULL;
561539
}
@@ -638,14 +616,6 @@ builtin_int(self, v)
638616
return (*nb->nb_int)(v);
639617
}
640618

641-
static object *
642-
builtin_lambda(self, v)
643-
object *self;
644-
object *v;
645-
{
646-
return exec_eval(v, lambda_input);
647-
}
648-
649619
static object *
650620
builtin_len(self, v)
651621
object *self;
@@ -977,14 +947,6 @@ builtin_reduce(self, args)
977947
return NULL;
978948
}
979949

980-
if (is_stringobject(func)) {
981-
if ((func = exec_eval(func, lambda_input)) == NULL)
982-
return NULL;
983-
}
984-
else {
985-
INCREF(func);
986-
}
987-
988950
if ((len = (*sqf->sq_length)(seq)) < 0)
989951
goto Fail_2;
990952

@@ -1025,7 +987,6 @@ builtin_reduce(self, args)
1025987
}
1026988

1027989
DECREF(args);
1028-
DECREF(func);
1029990

1030991
return result;
1031992

@@ -1035,7 +996,6 @@ builtin_reduce(self, args)
1035996
Fail_1:
1036997
DECREF(result);
1037998
Fail_2:
1038-
DECREF(func);
1039999
return NULL;
10401000
}
10411001

@@ -1133,7 +1093,6 @@ static struct methodlist builtin_methods[] = {
11331093
{"id", builtin_id},
11341094
{"input", builtin_input},
11351095
{"int", builtin_int},
1136-
{"lambda", builtin_lambda},
11371096
{"len", builtin_len},
11381097
{"long", builtin_long},
11391098
{"map", builtin_map},

0 commit comments

Comments
 (0)