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

Skip to content

Commit 52cc1d8

Browse files
committed
Implement PEP 3115 -- new metaclass syntax and semantics.
The compiler package hasn't been updated yet; test_compiler.py fails. Otherwise all tests seem to be passing now. There are no occurrences of __metaclass__ left in the standard library. Docs have not been updated.
1 parent ef17c16 commit 52cc1d8

25 files changed

Lines changed: 603 additions & 236 deletions

Grammar/Grammar

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ exprlist: expr (',' expr)* [',']
119119
testlist: test (',' test)* [',']
120120
dictsetmaker: (test ':' test (',' test ':' test)* [',']) | (test (',' test)* [','])
121121

122-
classdef: 'class' NAME ['(' [testlist] ')'] ':' suite
122+
classdef: 'class' NAME ['(' [arglist] ')'] ':' suite
123123

124124
arglist: (argument ',')* (argument [',']| '*' test [',' '**' test] | '**' test)
125125
argument: test [gen_for] | test '=' test # Really [keyword '='] test

Include/Python-ast.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ struct _stmt {
8282
struct {
8383
identifier name;
8484
asdl_seq *bases;
85+
asdl_seq *keywords;
86+
expr_ty starargs;
87+
expr_ty kwargs;
8588
asdl_seq *body;
8689
} ClassDef;
8790

@@ -380,8 +383,9 @@ mod_ty _Py_Suite(asdl_seq * body, PyArena *arena);
380383
stmt_ty _Py_FunctionDef(identifier name, arguments_ty args, asdl_seq * body,
381384
asdl_seq * decorators, expr_ty returns, int lineno, int
382385
col_offset, PyArena *arena);
383-
#define ClassDef(a0, a1, a2, a3, a4, a5) _Py_ClassDef(a0, a1, a2, a3, a4, a5)
384-
stmt_ty _Py_ClassDef(identifier name, asdl_seq * bases, asdl_seq * body, int
386+
#define ClassDef(a0, a1, a2, a3, a4, a5, a6, a7, a8) _Py_ClassDef(a0, a1, a2, a3, a4, a5, a6, a7, a8)
387+
stmt_ty _Py_ClassDef(identifier name, asdl_seq * bases, asdl_seq * keywords,
388+
expr_ty starargs, expr_ty kwargs, asdl_seq * body, int
385389
lineno, int col_offset, PyArena *arena);
386390
#define Return(a0, a1, a2, a3) _Py_Return(a0, a1, a2, a3)
387391
stmt_ty _Py_Return(expr_ty value, int lineno, int col_offset, PyArena *arena);

Include/opcode.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,9 @@ extern "C" {
5959
#define BINARY_OR 66
6060
#define INPLACE_POWER 67
6161
#define GET_ITER 68
62-
62+
#define STORE_LOCALS 69
6363
#define PRINT_EXPR 70
64+
#define LOAD_BUILD_CLASS 71
6465

6566
#define INPLACE_LSHIFT 75
6667
#define INPLACE_RSHIFT 76
@@ -69,14 +70,13 @@ extern "C" {
6970
#define INPLACE_OR 79
7071
#define BREAK_LOOP 80
7172
#define WITH_CLEANUP 81
72-
#define LOAD_LOCALS 82
73+
7374
#define RETURN_VALUE 83
7475
#define IMPORT_STAR 84
7576
#define MAKE_BYTES 85
7677
#define YIELD_VALUE 86
7778
#define POP_BLOCK 87
7879
#define END_FINALLY 88
79-
#define BUILD_CLASS 89
8080

8181
#define HAVE_ARGUMENT 90 /* Opcodes from here have an argument: */
8282

@@ -120,10 +120,10 @@ extern "C" {
120120
#define RAISE_VARARGS 130 /* Number of raise arguments (1, 2 or 3) */
121121
/* CALL_FUNCTION_XXX opcodes defined below depend on this definition */
122122
#define CALL_FUNCTION 131 /* #args + (#kwargs<<8) */
123-
#define MAKE_FUNCTION 132 /* #defaults */
123+
#define MAKE_FUNCTION 132 /* #defaults + #kwdefaults<<8 + #annotations<<16 */
124124
#define BUILD_SLICE 133 /* Number of items */
125125

126-
#define MAKE_CLOSURE 134 /* #free vars */
126+
#define MAKE_CLOSURE 134 /* same as MAKE_FUNCTION */
127127
#define LOAD_CLOSURE 135 /* Load free variable from closure */
128128
#define LOAD_DEREF 136 /* Load and dereference from closure cell */
129129
#define STORE_DEREF 137 /* Store into cell */

Lib/build_class.py

Whitespace-only changes.

Lib/ctypes/_endian.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,16 @@ def __setattr__(self, attrname, value):
4242

4343
LittleEndianStructure = Structure
4444

45-
class BigEndianStructure(Structure):
45+
class BigEndianStructure(Structure, metaclass=_swapped_meta):
4646
"""Structure with big endian byte order"""
47-
__metaclass__ = _swapped_meta
4847
_swappedbytes_ = None
4948

5049
elif sys.byteorder == "big":
5150
_OTHER_ENDIAN = "__ctype_le__"
5251

5352
BigEndianStructure = Structure
54-
class LittleEndianStructure(Structure):
53+
class LittleEndianStructure(Structure, metaclass=_swapped_meta):
5554
"""Structure with little endian byte order"""
56-
__metaclass__ = _swapped_meta
5755
_swappedbytes_ = None
5856

5957
else:

Lib/opcode.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,10 @@ def jabs_op(name, op):
9898
def_op('BINARY_OR', 66)
9999
def_op('INPLACE_POWER', 67)
100100
def_op('GET_ITER', 68)
101+
def_op('STORE_LOCALS', 69)
101102

102103
def_op('PRINT_EXPR', 70)
104+
def_op('LOAD_BUILD_CLASS', 71)
103105

104106
def_op('INPLACE_LSHIFT', 75)
105107
def_op('INPLACE_RSHIFT', 76)
@@ -108,14 +110,13 @@ def jabs_op(name, op):
108110
def_op('INPLACE_OR', 79)
109111
def_op('BREAK_LOOP', 80)
110112
def_op('WITH_CLEANUP', 81)
111-
def_op('LOAD_LOCALS', 82)
113+
112114
def_op('RETURN_VALUE', 83)
113115
def_op('IMPORT_STAR', 84)
114116
def_op('MAKE_BYTES', 85)
115117
def_op('YIELD_VALUE', 86)
116118
def_op('POP_BLOCK', 87)
117119
def_op('END_FINALLY', 88)
118-
def_op('BUILD_CLASS', 89)
119120

120121
HAVE_ARGUMENT = 90 # Opcodes from here have an argument:
121122

Lib/string.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,8 @@ def __init__(cls, name, bases, dct):
119119
cls.pattern = _re.compile(pattern, _re.IGNORECASE | _re.VERBOSE)
120120

121121

122-
class Template:
122+
class Template(metaclass=_TemplateMetaclass):
123123
"""A string class for supporting $-substitutions."""
124-
__metaclass__ = _TemplateMetaclass
125124

126125
delimiter = '$'
127126
idpattern = r'[_a-z][_a-z0-9]*'

Lib/test/crashers/modify_dict_attr.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@ class Y(object):
77
class type_with_modifiable_dict(Y, type):
88
pass
99

10-
class MyClass(object):
10+
class MyClass(object, metaclass=type_with_modifiable_dict):
1111
"""This class has its __dict__ attribute completely exposed:
1212
user code can read, reassign and even delete it.
1313
"""
14-
__metaclass__ = type_with_modifiable_dict
1514

1615

1716
if __name__ == '__main__':

Lib/test/leakers/test_selftype.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
def leak():
77
class T(type):
88
pass
9-
class U(type):
10-
__metaclass__ = T
9+
class U(type, metaclass=T):
10+
pass
1111
U.__class__ = U
1212
del U
1313
gc.collect(); gc.collect(); gc.collect()

Lib/test/pickletester.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ def __getinitargs__(self):
8888
class metaclass(type):
8989
pass
9090

91-
class use_metaclass(object):
92-
__metaclass__ = metaclass
91+
class use_metaclass(object, metaclass=metaclass):
92+
pass
9393

9494
# DATA0 .. DATA2 are the pickles we expect under the various protocols, for
9595
# the object returned by create_data().

0 commit comments

Comments
 (0)