@@ -107,12 +107,7 @@ def emit_function(node: typing.Union[ast.AsyncFunctionDef, ast.FunctionDef, ast.
107
107
new_ctx .bc .append (Instr ('LOAD_CONST' , None ))
108
108
new_ctx .bc .append (Instr ('RETURN_VALUE' ))
109
109
110
- try :
111
- inner_code = new_ctx .bc .to_code ()
112
- except RuntimeError :
113
- print (new_ctx .bc .filename )
114
- dump_bytecode (new_ctx .bc )
115
- raise
110
+ inner_code = new_ctx .bc .to_code ()
116
111
parent_ctx .bc .append (Instr ('LOAD_CONST' , inner_code , lineno = node .lineno ))
117
112
118
113
# when it comes to nested, the name is not generated correctly now.
@@ -195,64 +190,45 @@ def py_emit(node: ast.ClassDef, ctx: Context):
195
190
name = node .name
196
191
parent_ctx : Context = ctx .parent
197
192
198
- ctx .bc .name = f'{ parent_ctx .bc .name } .{ name } ' if parent_ctx .bc .name else name
199
-
200
193
for decorator in getattr (node , 'decorator_list' , ()):
201
194
py_emit (decorator , parent_ctx )
202
195
196
+ parent_ctx .bc .append (Instr ('LOAD_BUILD_CLASS' ))
197
+ ctx .bc .name = f'{ parent_ctx .bc .name } .{ name } ' if parent_ctx .bc .name else name
198
+
203
199
head = node .body
204
200
if isinstance (head , ast .Expr ) and isinstance (head .value , ast .Str ):
205
201
ctx .bc .docstring = head .value .s
206
202
207
- for each in node .body :
208
- py_emit (each , ctx )
209
-
210
203
ctx .bc .argcount = 0
211
204
ctx .bc .kwonlyarbgcount = 0
212
- ctx .bc .argnames = ['.yapypy.args' , '.yapypy.kwargs' ]
213
-
214
- make_function_flags = 0
215
- ctx .bc .flags |= CompilerFlags .VARARGS
216
- ctx .bc .flags |= CompilerFlags .VARKEYWORDS
217
-
218
- if ctx .sym_tb .freevars :
219
- make_function_flags |= 0x08
220
- ctx .load_closure (lineno = node .lineno )
221
-
205
+ ctx .load_closure (lineno = node .lineno )
222
206
ctx .bc .extend ([
223
207
LOAD_GLOBAL ('__name__' ),
224
- STORE_FAST ('__module__' ),
208
+ STORE_NAME ('__module__' ),
225
209
LOAD_CONST (ctx .bc .name ),
226
- STORE_FAST ('__qualname__' ),
227
- LOAD_FAST ('.yapypy.kwargs' ),
228
- LOAD_ATTR ('get' ),
229
- LOAD_CONST ('metaclass' ),
230
- LOAD_GLOBAL ('.yapypy.type' ),
231
- CALL_FUNCTION (2 ), # get metaclass
232
- LOAD_CONST (name ),
233
- LOAD_FAST ('.yapypy.args' ),
234
- LOAD_GLOBAL ('.yapypy.locals' ),
235
- CALL_FUNCTION (0 ), # get locals
236
- DUP_TOP (),
237
- LOAD_ATTR ('pop' ),
238
- DUP_TOP (),
239
- LOAD_CONST ('.yapypy.args' ),
240
- CALL_FUNCTION (1 ),
241
- POP_TOP (),
242
- LOAD_CONST ('.yapypy.kwargs' ),
243
- CALL_FUNCTION (1 ),
244
- POP_TOP (),
245
- CALL_FUNCTION (3 , lineno = node .lineno ), # create new type
210
+ STORE_NAME ('__qualname__' ),
211
+ ])
212
+ for each in node .body :
213
+ py_emit (each , ctx )
214
+
215
+ # https://docs.python.org/3/reference/datamodel.html#creating-the-class-object
216
+ ctx .bc .extend ([
217
+ Instr ('LOAD_CLOSURE' , CellVar ('__class__' )),
218
+ STORE_NAME ('__classcell__' ),
219
+ LOAD_CONST (None ),
220
+ RETURN_VALUE ()
246
221
])
247
222
248
- ctx .bc .append (Instr ('RETURN_VALUE' ))
249
223
inner_code = ctx .bc .to_code ()
250
224
251
225
parent_ctx .bc .append (LOAD_CONST (inner_code , lineno = lineno ))
252
226
# when it comes to nested, the name is not generated correctly now.
253
227
parent_ctx .bc .append (LOAD_CONST (name , lineno = lineno ))
254
228
255
- parent_ctx .bc .append (MAKE_FUNCTION (make_function_flags , lineno = lineno ))
229
+ parent_ctx .bc .append (MAKE_FUNCTION (0x08 , lineno = lineno ))
230
+
231
+ parent_ctx .bc .extend ([LOAD_CONST (name ), BUILD_TUPLE (2 )])
256
232
257
233
# *args
258
234
if node .bases :
@@ -262,6 +238,8 @@ def py_emit(node: ast.ClassDef, ctx: Context):
262
238
else :
263
239
parent_ctx .bc .append (LOAD_CONST (()))
264
240
241
+ parent_ctx .bc .append (Instr ('BUILD_TUPLE_UNPACK_WITH_CALL' , 2 ))
242
+
265
243
# **kwargs
266
244
if node .keywords :
267
245
keys , values = zip (* [(ast .Str (
@@ -274,9 +252,7 @@ def py_emit(node: ast.ClassDef, ctx: Context):
274
252
py_emit (ex_dict , parent_ctx )
275
253
else :
276
254
parent_ctx .bc .append (BUILD_MAP (0 ))
277
-
278
255
parent_ctx .bc .append (CALL_FUNCTION_EX (1 ))
279
-
280
256
parent_ctx .bc .extend (
281
257
[CALL_FUNCTION (1 , lineno = lineno )] * len (getattr (node , 'decorator_list' , ())))
282
258
0 commit comments