Source

pypy / pypy / interpreter / function.py

Full commit
Armin Rigo 6a00154 







Alexander Hesse f86b274 
Armin Rigo 1641ad7 
Armin Rigo 6a00154 
Ludovic Aubry 881d964 
Samuele Pedroni 4107e6f 
Alexander Hesse f86b274 

Armin Rigo 6a00154 
Maciej Fijalkows… b5a2e0b 

Carl Friedrich B… 382f399 
Carl Friedrich B… 404b795 



Alex Gaynor ddca225 
Armin Rigo 6a00154 



Gintautas Miliau… 63644f8 
Carl Friedrich B… 404b795 
Armin Rigo b90ea3b 

Carl Friedrich B… 29789b6 
Alex Gaynor dff2f4c 

Carl Friedrich B… 404b795 

Armin Rigo b4acd9a 
Armin Rigo 6a00154 

Armin Rigo 2e33c32 
Armin Rigo 6a00154 


Armin Rigo b4acd9a 
Michael Hudson-D… 3f0ca0e 
Ludovic Aubry 9b8a7a5 
Armin Rigo 6a00154 



Armin Rigo 96124ba 


Armin Rigo d5b188d 
Armin Rigo 6a00154 

Alex Gaynor e91ea9c 
Carl Friedrich B… 2ef5591 
Maciej Fijalkows… b5a2e0b 


Carl Friedrich B… 2ef5591 
Armin Rigo d6449de 

Carl Friedrich B… 404b795 


Carl Friedrich B… b1ffa11 
Carl Friedrich B… 404b795 
Alex Gaynor e91ea9c 
Samuele Pedroni d1a0dea 
Maciej Fijalkows… b5a2e0b 

Alex Gaynor e91ea9c 
Armin Rigo d6449de 
Maciej Fijalkows… b5a2e0b 



Alex Gaynor e91ea9c 
Maciej Fijalkows… b5a2e0b 







Alex Gaynor e91ea9c 
Maciej Fijalkows… b5a2e0b 


Alex Gaynor e91ea9c 
Maciej Fijalkows… b5a2e0b 

Alex Gaynor b494ff6 
Alex Gaynor e91ea9c 
Maciej Fijalkows… b5a2e0b 

Carl Friedrich B… 29789b6 
Maciej Fijalkows… b5a2e0b 

Armin Rigo a1843da 
Alex Gaynor e91ea9c 
Samuele Pedroni ee9f801 
Maciej Fijalkows… b5a2e0b 



Samuele Pedroni d1a0dea 

Armin Rigo 2e72773 
Maciej Fijalkows… b5a2e0b 

Alex Gaynor e91ea9c 
Samuele Pedroni aa90f76 
Armin Rigo 42c400c 





Alex Gaynor e91ea9c 
Maciej Fijalkows… b5a2e0b 



















Alex Gaynor b494ff6 
Maciej Fijalkows… b5a2e0b 
Carl Friedrich B… 389ff86 
Alex Gaynor b494ff6 

Armin Rigo b4acd9a 
Samuele Pedroni ee9f801 
Carl Friedrich B… 389ff86 
Alex Gaynor b494ff6 
Samuele Pedroni ee9f801 
Maciej Fijalkows… b5a2e0b 


Samuele Pedroni 4107e6f 
Alex Gaynor e91ea9c 
Samuele Pedroni aa90f76 
Samuele Pedroni 4107e6f 
Samuele Pedroni d1a0dea 
Carl Friedrich B… 329014d 
Carl Friedrich B… 389ff86 
Maciej Fijalkows… b5a2e0b 

Carl Friedrich B… 29789b6 
Maciej Fijalkows… b5a2e0b 

Armin Rigo a1843da 
Alex Gaynor e91ea9c 
jonathanslenders 73c4b9e 
jonathanslenders ee63ef5 
jonathanslenders 73c4b9e 

























Carl Friedrich B… 389ff86 
Carl Friedrich B… 329014d 
Carl Friedrich B… 389ff86 


Carl Friedrich B… 29789b6 
Carl Friedrich B… 389ff86 

Armin Rigo a1843da 
Alex Gaynor e91ea9c 
Armin Rigo b4acd9a 
Alex Gaynor b494ff6 
Carl Friedrich B… 389ff86 

Armin Rigo a1843da 
Carl Friedrich B… 389ff86 
Alex Gaynor e91ea9c 
Samuele Pedroni d1a0dea 
Amaury Forgeot d… 93ae975 
Michael Hudson-D… 3f0ca0e 
Amaury Forgeot d… 93ae975 
Armin Rigo 6a00154 

Armin Rigo 0bb5dc2 
Alex Gaynor b494ff6 



Armin Rigo 6a00154 

Alex Gaynor e91ea9c 
Amaury Forgeot d… 70846a8 
Armin Rigo 6e86cee 
Samuele Pedroni e8c5e3c 

Maciej Fijalkows… c7b80d2 
Samuele Pedroni e8c5e3c 


Maciej Fijalkows… c7b80d2 
Armin Rigo 8e5d92f 
Samuele Pedroni e8c5e3c 

Samuele Pedroni b0c4859 



Maciej Fijalkows… c7b80d2 
Samuele Pedroni e8c5e3c 






Samuele Pedroni b0c4859 
Samuele Pedroni e8c5e3c 
Samuele Pedroni a909c65 
Gintautas Miliau… 63644f8 
Armin Rigo 6e86cee 
Samuele Pedroni e8c5e3c 
Armin Rigo 9bf1994 
Samuele Pedroni e8c5e3c 

Armin Rigo 6a00154 


Armin Rigo 0a04459 


Armin Rigo 4ac21ef 
Alex Gaynor e91ea9c 
Armin Rigo 4ac21ef 

Maciej Fijalkows… c4813b2 
Armin Rigo 4ac21ef 



Benjamin Peterso… 3cda850 
Armin Rigo b8e9360 



Benjamin Peterso… e945b46 
Armin Rigo 4ac21ef 

Benjamin Peterso… e945b46 


Armin Rigo 4ac21ef 



Eric van Riet Pa… 10587a6 
Armin Rigo 4ac21ef 
Eric van Riet Pa… 5b338ef 
Eric van Riet Pa… 2479a9c 

Armin Rigo 4ac21ef 




Alex Gaynor e91ea9c 
Eric van Riet Pa… 5b338ef 
Eric van Riet Pa… 2479a9c 
Armin Rigo 7481ec9 



Armin Rigo 4ac21ef 











Christian Tismer 1cc6ee7 




Armin Rigo 4ac21ef 
Eric van Riet Pa… 5b338ef 
Armin Rigo 4ac21ef 
Armin Rigo 7481ec9 
Armin Rigo b4acd9a 
Armin Rigo 4ac21ef 
Christian Tismer 1cc6ee7 
Eric van Riet Pa… 2479a9c 
Christian Tismer 1cc6ee7 




Benjamin Peterso… fd70166 
Alex Gaynor b494ff6 
Benjamin Peterso… fd70166 




Christian Tismer 1cc6ee7 


Armin Rigo 4ac21ef 

Samuele Pedroni fb4685a 


Christian Tismer 1cc6ee7 

Armin Rigo 4ac21ef 








Armin Rigo b4acd9a 
Christian Tismer 1cc6ee7 
Eric van Riet Pa… 5b338ef 
Amaury Forgeot d… fe81f52 
Armin Rigo b4acd9a 
Alex Gaynor b494ff6 
Maciej Fijalkows… 34bc061 
Alex Gaynor b494ff6 
Armin Rigo 2a5b0f7 
Armin Rigo 6a00154 
Carl Friedrich B… 913a2af 
Gintautas Miliau… 63644f8 
Amaury Forgeot d… fe81f52 
Maciej Fijalkows… b5a2e0b 
Armin Rigo b4acd9a 
Maciej Fijalkows… b5a2e0b 
Carl Friedrich B… 913a2af 
Maciej Fijalkows… b5a2e0b 
Armin Rigo b4acd9a 
Ludovic Aubry 881d964 
Amaury Forgeot d… fe81f52 
Armin Rigo b4acd9a 
Armin Rigo 6a00154 
Amaury Forgeot d… fe81f52 
Armin Rigo 6a00154 
Armin Rigo 2e33c32 
Armin Rigo 6a00154 

Amaury Forgeot d… fe81f52 
Armin Rigo 6a00154 

Amaury Forgeot d… fe81f52 
Anders Qvist 3e6ba3d 

Amaury Forgeot d… fe81f52 
Anders Qvist 3e6ba3d 









Amaury Forgeot d… fe81f52 
Armin Rigo 6a00154 

Amaury Forgeot d… fe81f52 
Ludovic Aubry 9b8a7a5 

Alex Gaynor b494ff6 
Ludovic Aubry 9b8a7a5 


Gintautas Miliau… 63644f8 
Amaury Forgeot d… fe81f52 
Armin Rigo 8250bfc 
Gintautas Miliau… 63644f8 
Amaury Forgeot d… fe81f52 
Ludovic Aubry 9b8a7a5 

Amaury Forgeot d… fe81f52 
Ludovic Aubry 881d964 

Amaury Forgeot d… fe81f52 
Samuele Pedroni c662cb4 
Carl Friedrich B… 404b795 


Armin Rigo 6e86cee 
Samuele Pedroni c662cb4 



Armin Rigo 1641ad7 


Armin Rigo 5b2403f 
Ludovic Aubry 881d964 
Gintautas Miliau… 63644f8 
Amaury Forgeot d… fe81f52 
Christian Tismer a9b8292 


Christian Tismer 3973fb7 
Ludovic Aubry 881d964 

Maciej Fijalkows… 5ef114d 
Armin Rigo f62fa02 
Armin Rigo 9bfce73 
Armin Rigo f62fa02 

Maciej Fijalkows… c7b80d2 
Maciej Fijalkows… 5ef114d 
Armin Rigo f62fa02 






Gintautas Miliau… 63644f8 
Armin Rigo 6a00154 
Armin Rigo b90ea3b 
Armin Rigo 6a00154 




Maciej Fijalkows… c7b80d2 

Armin Rigo 044e2b0 
Gintautas Miliau… 63644f8 
Maciej Fijalkows… c7b80d2 

Benjamin Peterso… b9564d5 
Ludovic Aubry 881d964 
Maciej Fijalkows… c7b80d2 
Benjamin Peterso… b9564d5 

Armin Rigo 1e283c8 

Ludovic Aubry 881d964 

Armin Rigo 6a00154 




Armin Rigo 815cd34 
Armin Rigo 6a00154 

Samuele Pedroni a106532 
Armin Rigo 6a00154 

Maciej Fijalkows… b5a2e0b 






Armin Rigo 6a00154 
Amaury Forgeot d… 44cfeaf 

Maciej Fijalkows… b5a2e0b 
Amaury Forgeot d… 44cfeaf 


Maciej Fijalkows… b5a2e0b 

Armin Rigo 6a00154 
Amaury Forgeot d… 44cfeaf 

Maciej Fijalkows… b5a2e0b 
Amaury Forgeot d… 44cfeaf 




Armin Rigo 1641ad7 

Samuele Pedroni a106532 
Armin Rigo 6a00154 






Armin Rigo 044e2b0 
Christian Tismer c8cfd80 
Amaury Forgeot d… 89f238f 

Armin Rigo f62fa02 


Armin Rigo 6a00154 



Gintautas Miliau… 63644f8 
holger krekel f5169e5 
Armin Rigo 815cd34 
Gintautas Miliau… 63644f8 




Armin Rigo 815cd34 
Gintautas Miliau… 63644f8 
Adrien Di Mascio af0d75b 
Armin Rigo 902025d 
holger krekel f5169e5 
Armin Rigo 724a924 
Carl Friedrich B… 239180d 

holger krekel f5169e5 
Armin Rigo 6a00154 

Armin Rigo ed6dd81 








Armin Rigo 6a00154 
Armin Rigo 29db480 



Alex Gaynor e91ea9c 
Armin Rigo c2aab86 





Maciej Fijalkows… a0a2487 
Armin Rigo c2aab86 
Armin Rigo 29db480 

Armin Rigo c2aab86 






Eric van Riet Pa… 10587a6 

Michael Hudson-D… 6b9bae5 
Eric van Riet Pa… 10587a6 




Michael Hudson-D… 6b9bae5 







Eric van Riet Pa… 10587a6 



Alex Gaynor e91ea9c 
Armin Rigo 6a00154 
Armin Rigo 0354e02 
Armin Rigo 72ff127 
Armin Rigo 6a00154 




Armin Rigo 9bfce73 
Armin Rigo 6a00154 
Armin Rigo 6476a28 
Carl Friedrich B… 2d89c6b 



Maciej Fijalkows… 7036f80 
Armin Rigo 0354e02 

Armin Rigo 72ff127 
Armin Rigo 0354e02 




Maciej Fijalkows… 5ef114d 
Armin Rigo 0354e02 


Amaury Forgeot d… fe018de 



Armin Rigo 0354e02 
Carl Friedrich B… 404b795 


Armin Rigo 6476a28 
Carl Friedrich B… 404b795 
Armin Rigo 6476a28 



Armin Rigo b4acd9a 
Armin Rigo 6476a28 



Amaury Forgeot d… fe5a0f5 


Armin Rigo 0a04459 


Armin Rigo d5b188d 








  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
"""
Function objects.

In PyPy there is no difference between built-in and user-defined function
objects; the difference lies in the code object found in their func_code
attribute.
"""

from rpython.rlib.unroll import unrolling_iterable
from pypy.interpreter.error import OperationError, operationerrfmt
from pypy.interpreter.baseobjspace import Wrappable
from pypy.interpreter.eval import Code
from pypy.interpreter.argument import Arguments
from rpython.rlib import jit
from rpython.rlib.debug import make_sure_not_resized

funccallunrolling = unrolling_iterable(range(4))

@jit.elidable_promote()
def _get_immutable_code(func):
    assert not func.can_change_code
    return func.code


class Function(Wrappable):
    """A function is a code object captured with some environment:
    an object space, a dictionary of globals, default arguments,
    and an arbitrary 'closure' passed to the code object."""

    can_change_code = True
    _immutable_fields_ = ['code?',
                          'w_func_globals?',
                          'closure?[*]',
                          'defs_w?[*]',
                          'name?']

    def __init__(self, space, code, w_globals=None, defs_w=[], closure=None,
                 forcename=None):
        self.space = space
        self.name = forcename or code.co_name
        self.w_doc = None   # lazily read from code.getdocstring()
        self.code = code       # Code instance
        self.w_func_globals = w_globals  # the globals dictionary
        self.closure   = closure    # normally, list of Cell instances or None
        self.defs_w = defs_w
        self.w_func_dict = None # filled out below if needed
        self.w_module = None

    def __repr__(self):
        # return "function %s.%s" % (self.space, self.name)
        # maybe we want this shorter:
        name = getattr(self, 'name', None)
        if not isinstance(name, str):
            name = '?'
        return "<%s %s>" % (self.__class__.__name__, name)

    def call_args(self, args):
        # delegate activation to code
        return self.getcode().funcrun(self, args)

    def call_obj_args(self, w_obj, args):
        # delegate activation to code
        return self.getcode().funcrun_obj(self, w_obj, args)

    def getcode(self):
        if jit.we_are_jitted():
            if not self.can_change_code:
                return _get_immutable_code(self)
            return jit.promote(self.code)
        return self.code

    def funccall(self, *args_w): # speed hack
        from pypy.interpreter import gateway
        from pypy.interpreter.pycode import PyCode

        code = self.getcode() # hook for the jit
        nargs = len(args_w)
        fast_natural_arity = code.fast_natural_arity
        if nargs == fast_natural_arity:
            if nargs == 0:
                assert isinstance(code, gateway.BuiltinCode0)
                return code.fastcall_0(self.space, self)
            elif nargs == 1:
                assert isinstance(code, gateway.BuiltinCode1)
                return code.fastcall_1(self.space, self, args_w[0])
            elif nargs == 2:
                assert isinstance(code, gateway.BuiltinCode2)
                return code.fastcall_2(self.space, self, args_w[0], args_w[1])
            elif nargs == 3:
                assert isinstance(code, gateway.BuiltinCode3)
                return code.fastcall_3(self.space, self, args_w[0],
                                       args_w[1], args_w[2])
            elif nargs == 4:
                assert isinstance(code, gateway.BuiltinCode4)
                return code.fastcall_4(self.space, self, args_w[0],
                                       args_w[1], args_w[2], args_w[3])
        elif (nargs | PyCode.FLATPYCALL) == fast_natural_arity:
            assert isinstance(code, PyCode)
            if nargs < 5:
                new_frame = self.space.createframe(code, self.w_func_globals,
                                                   self)
                for i in funccallunrolling:
                    if i < nargs:
                        new_frame.locals_stack_w[i] = args_w[i]
                return new_frame.run()
        elif nargs >= 1 and fast_natural_arity == Code.PASSTHROUGHARGS1:
            assert isinstance(code, gateway.BuiltinCodePassThroughArguments1)
            return code.funcrun_obj(self, args_w[0],
                                    Arguments(self.space,
                                              list(args_w[1:])))
        return self.call_args(Arguments(self.space, list(args_w)))

    def funccall_valuestack(self, nargs, frame): # speed hack
        from pypy.interpreter import gateway
        from pypy.interpreter.pycode import PyCode

        code = self.getcode() # hook for the jit
        #
        if (jit.we_are_jitted() and code is self.space._code_of_sys_exc_info
                                and nargs == 0):
            from pypy.module.sys.vm import exc_info_direct
            return exc_info_direct(self.space, frame)
        #
        fast_natural_arity = code.fast_natural_arity
        if nargs == fast_natural_arity:
            if nargs == 0:
                assert isinstance(code, gateway.BuiltinCode0)
                return code.fastcall_0(self.space, self)
            elif nargs == 1:
                assert isinstance(code, gateway.BuiltinCode1)
                return code.fastcall_1(self.space, self, frame.peekvalue(0))
            elif nargs == 2:
                assert isinstance(code, gateway.BuiltinCode2)
                return code.fastcall_2(self.space, self, frame.peekvalue(1),
                                       frame.peekvalue(0))
            elif nargs == 3:
                assert isinstance(code, gateway.BuiltinCode3)
                return code.fastcall_3(self.space, self, frame.peekvalue(2),
                                       frame.peekvalue(1), frame.peekvalue(0))
            elif nargs == 4:
                assert isinstance(code, gateway.BuiltinCode4)
                return code.fastcall_4(self.space, self, frame.peekvalue(3),
                                       frame.peekvalue(2), frame.peekvalue(1),
                                        frame.peekvalue(0))
        elif (nargs | Code.FLATPYCALL) == fast_natural_arity:
            assert isinstance(code, PyCode)
            return self._flat_pycall(code, nargs, frame)
        elif fast_natural_arity & Code.FLATPYCALL:
            natural_arity = fast_natural_arity & 0xff
            if natural_arity > nargs >= natural_arity - len(self.defs_w):
                assert isinstance(code, PyCode)
                return self._flat_pycall_defaults(code, nargs, frame,
                                                  natural_arity - nargs)
        elif fast_natural_arity == Code.PASSTHROUGHARGS1 and nargs >= 1:
            assert isinstance(code, gateway.BuiltinCodePassThroughArguments1)
            w_obj = frame.peekvalue(nargs-1)
            args = frame.make_arguments(nargs-1)
            return code.funcrun_obj(self, w_obj, args)

        args = frame.make_arguments(nargs)
        return self.call_args(args)

    @jit.unroll_safe
    def _flat_pycall(self, code, nargs, frame):
        # code is a PyCode
        new_frame = self.space.createframe(code, self.w_func_globals,
                                                   self)
        for i in xrange(nargs):
            w_arg = frame.peekvalue(nargs-1-i)
            new_frame.locals_stack_w[i] = w_arg

        result = new_frame.run()
        result = self._wrap_await_result(result)
        return result

    def _wrap_await_result(self, result):
        """
        Python-extension for the 'await' keyword. Wrap the last iteration of an
        await-generator through executioncontext.w_await_schedular_func.
        """
        # Does the code object of this function contain the CO_AWAIT_GENERATOR
        # flag? (Set by the ast.)
        from pypy.interpreter.pycode import PyCode
        from pypy.interpreter.astcompiler.consts import CO_GENERATOR, CO_AWAIT_GENERATOR
        is_await_generator = (self.getcode().co_flags & CO_AWAIT_GENERATOR)

        # If so, and if an I/O schedular is set, get the result through the
        # I/O schedular function.
        if is_await_generator:
            context = self.space.getexecutioncontext()
            if context.w_await_schedular_func:
                schedular = context.w_await_schedular_func
                assert isinstance(schedular, Function)
                result = schedular.funccall(result)
            else:
                raise OperationError(self.space.w_TypeError,
                    self.space.wrap("Calling an asyncronous function (with an 'await'-keyword) while no I/O schedular has been set")
                )
        return result

    @jit.unroll_safe
    def _flat_pycall_defaults(self, code, nargs, frame, defs_to_load):
        # code is a PyCode
        new_frame = self.space.createframe(code, self.w_func_globals,
                                                   self)
        for i in xrange(nargs):
            w_arg = frame.peekvalue(nargs-1-i)
            new_frame.locals_stack_w[i] = w_arg

        ndefs = len(self.defs_w)
        start = ndefs - defs_to_load
        i = nargs
        for j in xrange(start, ndefs):
            new_frame.locals_stack_w[i] = self.defs_w[j]
            i += 1
        return new_frame.run()

    def getdict(self, space):
        if self.w_func_dict is None:
            self.w_func_dict = space.newdict(instance=True)
        return self.w_func_dict

    def setdict(self, space, w_dict):
        if not space.isinstance_w(w_dict, space.w_dict):
            raise OperationError(space.w_TypeError,
                space.wrap("setting function's dictionary to a non-dict")
            )
        self.w_func_dict = w_dict

    def descr_function__new__(space, w_subtype, w_code, w_globals,
                              w_name=None, w_argdefs=None, w_closure=None):
        code = space.interp_w(Code, w_code)
        if not space.is_true(space.isinstance(w_globals, space.w_dict)):
            raise OperationError(space.w_TypeError, space.wrap("expected dict"))
        if not space.is_none(w_name):
            name = space.str_w(w_name)
        else:
            name = None
        if not space.is_none(w_argdefs):
            defs_w = space.fixedview(w_argdefs)
        else:
            defs_w = []
        nfreevars = 0
        from pypy.interpreter.pycode import PyCode
        if isinstance(code, PyCode):
            nfreevars = len(code.co_freevars)
        if space.is_none(w_closure) and nfreevars == 0:
            closure = None
        elif not space.is_w(space.type(w_closure), space.w_tuple):
            raise OperationError(space.w_TypeError, space.wrap("invalid closure"))
        else:
            from pypy.interpreter.nestedscope import Cell
            closure_w = space.unpackiterable(w_closure)
            n = len(closure_w)
            if nfreevars == 0:
                raise OperationError(space.w_ValueError, space.wrap("no closure needed"))
            elif nfreevars != n:
                raise OperationError(space.w_ValueError, space.wrap("closure is wrong size"))
            closure = [space.interp_w(Cell, w_cell) for w_cell in closure_w]
        func = space.allocate_instance(Function, w_subtype)
        Function.__init__(func, space, code, w_globals, defs_w, closure, name)
        return space.wrap(func)

    def descr_function_call(self, __args__):
        return self.call_args(__args__)

    def descr_function_repr(self):
        return self.getrepr(self.space, 'function %s' % (self.name,))


    # delicate
    _all = {'': None}

    def _cleanup_(self):
        from pypy.interpreter.gateway import BuiltinCode
        if isinstance(self.code, BuiltinCode):
            # we have been seen by other means so rtyping should not choke
            # on us
            identifier = self.code.identifier
            previous = Function._all.get(identifier, self)
            assert previous is self, (
                "duplicate function ids with identifier=%r: %r and %r" % (
                identifier, previous, self))
            self.add_to_table()
        return False

    def add_to_table(self):
        Function._all[self.code.identifier] = self

    def find(identifier):
        return Function._all[identifier]
    find = staticmethod(find)

    def descr_function__reduce__(self, space):
        from pypy.interpreter.gateway import BuiltinCode
        from pypy.interpreter.mixedmodule import MixedModule
        w_mod    = space.getbuiltinmodule('_pickle_support')
        mod      = space.interp_w(MixedModule, w_mod)
        code = self.code
        if isinstance(code, BuiltinCode):
            new_inst = mod.get('builtin_function')
            return space.newtuple([new_inst,
                                   space.newtuple([space.wrap(code.identifier)])])

        new_inst = mod.get('func_new')
        w        = space.wrap
        if self.closure is None:
            w_closure = space.w_None
        else:
            w_closure = space.newtuple([w(cell) for cell in self.closure])
        if self.w_doc is None:
            w_doc = space.w_None
        else:
            w_doc = self.w_doc
        if self.w_func_globals is None:
            w_func_globals = space.w_None
        else:
            w_func_globals = self.w_func_globals
        if self.w_func_dict is None:
            w_func_dict = space.w_None
        else:
            w_func_dict = self.w_func_dict

        nt = space.newtuple
        tup_base = []
        tup_state = [
            w(self.name),
            w_doc,
            w(self.code),
            w_func_globals,
            w_closure,
            nt(self.defs_w),
            w_func_dict,
            self.w_module,
        ]
        return nt([new_inst, nt(tup_base), nt(tup_state)])

    def descr_function__setstate__(self, space, w_args):
        from pypy.interpreter.pycode import PyCode
        args_w = space.unpackiterable(w_args)
        try:
            (w_name, w_doc, w_code, w_func_globals, w_closure, w_defs,
             w_func_dict, w_module) = args_w
        except ValueError:
            # wrong args
            raise OperationError(space.w_ValueError,
                         space.wrap("Wrong arguments to function.__setstate__"))

        self.space = space
        self.name = space.str_w(w_name)
        self.code = space.interp_w(Code, w_code)
        if not space.is_w(w_closure, space.w_None):
            from pypy.interpreter.nestedscope import Cell
            closure_w = space.unpackiterable(w_closure)
            self.closure = [space.interp_w(Cell, w_cell) for w_cell in closure_w]
        else:
            self.closure = None
        if space.is_w(w_doc, space.w_None):
            w_doc = None
        self.w_doc = w_doc
        if space.is_w(w_func_globals, space.w_None):
            w_func_globals = None
        self.w_func_globals = w_func_globals
        if space.is_w(w_func_dict, space.w_None):
            w_func_dict = None
        self.w_func_dict = w_func_dict
        self.defs_w = space.fixedview(w_defs)
        self.w_module = w_module

    def fget_func_defaults(self, space):
        values_w = self.defs_w
        # the `None in values_w` check here is to ensure that interp-level
        # functions with a default of None do not get their defaults
        # exposed at applevel
        if not values_w or None in values_w:
            return space.w_None
        return space.newtuple(values_w)

    def fset_func_defaults(self, space, w_defaults):
        if space.is_w(w_defaults, space.w_None):
            self.defs_w = []
            return
        if not space.is_true(space.isinstance(w_defaults, space.w_tuple)):
            raise OperationError( space.w_TypeError, space.wrap("func_defaults must be set to a tuple object or None") )
        self.defs_w = space.fixedview(w_defaults)

    def fdel_func_defaults(self, space):
        self.defs_w = []

    def fget_func_doc(self, space):
        if self.w_doc is None:
            self.w_doc = self.code.getdocstring(space)
        return self.w_doc

    def fset_func_doc(self, space, w_doc):
        self.w_doc = w_doc

    def fget_func_name(self, space):
        return space.wrap(self.name)

    def fset_func_name(self, space, w_name):
        try:
            self.name = space.str_w(w_name)
        except OperationError, e:
            if e.match(space, space.w_TypeError):
                raise OperationError(space.w_TypeError,
                                     space.wrap("func_name must be set "
                                                "to a string object"))
            raise


    def fdel_func_doc(self, space):
        self.w_doc = space.w_None

    def fget___module__(self, space):
        if self.w_module is None:
            if self.w_func_globals is not None and not space.is_w(self.w_func_globals, space.w_None):
                self.w_module = space.call_method(self.w_func_globals, "get", space.wrap("__name__"))
            else:
                self.w_module = space.w_None
        return self.w_module

    def fset___module__(self, space, w_module):
        self.w_module = w_module

    def fdel___module__(self, space):
        self.w_module = space.w_None

    def fget_func_code(self, space):
        return space.wrap(self.code)

    def fset_func_code(self, space, w_code):
        from pypy.interpreter.pycode import PyCode
        if not self.can_change_code:
            raise OperationError(space.w_TypeError,
                    space.wrap("Cannot change code attribute of builtin functions"))
        code = space.interp_w(Code, w_code)
        closure_len = 0
        if self.closure:
            closure_len = len(self.closure)
        if isinstance(code, PyCode) and closure_len != len(code.co_freevars):
            raise operationerrfmt(space.w_ValueError,
                "%s() requires a code object with %d free vars, not %d",
                self.name, closure_len, len(code.co_freevars))
        self.fget_func_doc(space)    # see test_issue1293
        self.code = code

    def fget_func_closure(self, space):
        if self.closure is not None:
            w_res = space.newtuple( [ space.wrap(i) for i in self.closure ] )
        else:
            w_res = space.w_None
        return w_res


def descr_function_get(space, w_function, w_obj, w_cls=None):
    """functionobject.__get__(obj[, type]) -> method"""
    # this is not defined as a method on Function because it's generally
    # useful logic: w_function can be any callable.  It is used by Method too.
    asking_for_bound = (space.is_none(w_cls) or
                        not space.is_w(w_obj, space.w_None) or
                        space.is_w(w_cls, space.type(space.w_None)))
    if asking_for_bound:
        return space.wrap(Method(space, w_function, w_obj, w_cls))
    else:
        return space.wrap(Method(space, w_function, None, w_cls))


class Method(Wrappable):
    """A method is a function bound to a specific instance or class."""
    _immutable_fields_ = ['w_function', 'w_instance', 'w_class']

    def __init__(self, space, w_function, w_instance, w_class):
        self.space = space
        self.w_function = w_function
        self.w_instance = w_instance   # or None
        if w_class is None:
            w_class = space.w_None
        self.w_class = w_class         # possibly space.w_None

    def descr_method__new__(space, w_subtype, w_function, w_instance,
                            w_class=None):
        if space.is_w(w_instance, space.w_None):
            w_instance = None
        if w_instance is None and space.is_none(w_class):
            raise OperationError(space.w_TypeError,
                                 space.wrap("unbound methods must have class"))
        method = space.allocate_instance(Method, w_subtype)
        Method.__init__(method, space, w_function, w_instance, w_class)
        return space.wrap(method)

    def __repr__(self):
        if self.w_instance:
            pre = "bound"
        else:
            pre = "unbound"
        return "%s method %s" % (pre, self.w_function.getname(self.space))

    def call_args(self, args):
        space = self.space
        if self.w_instance is not None:
            # bound method
            return space.call_obj_args(self.w_function, self.w_instance, args)

        # unbound method
        w_firstarg = args.firstarg()
        if w_firstarg is not None and (
                space.abstract_isinstance_w(w_firstarg, self.w_class)):
            pass  # ok
        else:
            myname = self.getname(space, "")
            clsdescr = self.w_class.getname(space, "")
            if clsdescr:
                clsdescr += " instance"
            else:
                clsdescr = "instance"
            if w_firstarg is None:
                instdescr = "nothing"
            else:
                instname = space.abstract_getclass(w_firstarg).getname(space,
                                                                       "")
                if instname:
                    instdescr = instname + " instance"
                else:
                    instdescr = "instance"
            msg = ("unbound method %s() must be called with %s "
                   "as first argument (got %s instead)")
            raise operationerrfmt(space.w_TypeError, msg,
                                  myname, clsdescr, instdescr)
        return space.call_args(self.w_function, args)

    def descr_method_get(self, w_obj, w_cls=None):
        space = self.space
        if self.w_instance is not None:
            return space.wrap(self)    # already bound
        else:
            # only allow binding to a more specific class than before
            if (w_cls is not None and
                not space.is_w(w_cls, space.w_None) and
                not space.abstract_issubclass_w(w_cls, self.w_class,
                                                allow_override=True)):
                return space.wrap(self)    # subclass test failed
            else:
                return descr_function_get(space, self.w_function, w_obj, w_cls)

    def descr_method_call(self, __args__):
        return self.call_args(__args__)

    def descr_method_repr(self):
        space = self.space
        name = self.w_function.getname(self.space)
        # XXX do we handle all cases sanely here?
        if space.is_w(self.w_class, space.w_None):
            w_class = space.type(self.w_instance)
        else:
            w_class = self.w_class
        typename = w_class.getname(self.space)
        if self.w_instance is None:
            s = "<unbound method %s.%s>" % (typename, name)
            return space.wrap(s)
        else:
            objrepr = space.str_w(space.repr(self.w_instance))
            s = '<bound method %s.%s of %s>' % (typename, name, objrepr)
            return space.wrap(s)

    def descr_method_getattribute(self, w_attr):
        space = self.space
        if space.str_w(w_attr) != '__doc__':
            try:
                return space.call_method(space.w_object, '__getattribute__',
                                         space.wrap(self), w_attr)
            except OperationError, e:
                if not e.match(space, space.w_AttributeError):
                    raise
        # fall-back to the attribute of the underlying 'im_func'
        return space.getattr(self.w_function, w_attr)

    def descr_method_eq(self, w_other):
        space = self.space
        other = space.interpclass_w(w_other)
        if not isinstance(other, Method):
            return space.w_NotImplemented
        if self.w_instance is None:
            if other.w_instance is not None:
                return space.w_False
        else:
            if other.w_instance is None:
                return space.w_False
            if not space.eq_w(self.w_instance, other.w_instance):
                return space.w_False
        return space.eq(self.w_function, other.w_function)

    def descr_method_hash(self):
        space = self.space
        w_result = space.hash(self.w_function)
        if self.w_instance is not None:
            w_result = space.xor(w_result, space.hash(self.w_instance))
        return w_result

    def descr_method__reduce__(self, space):
        from pypy.interpreter.mixedmodule import MixedModule
        from pypy.interpreter.gateway import BuiltinCode
        w_mod    = space.getbuiltinmodule('_pickle_support')
        mod      = space.interp_w(MixedModule, w_mod)
        new_inst = mod.get('method_new')
        w        = space.wrap
        w_instance = self.w_instance or space.w_None
        function = space.interpclass_w(self.w_function)
        if isinstance(function, Function) and isinstance(function.code, BuiltinCode):
            new_inst = mod.get('builtin_method_new')
            if space.is_w(w_instance, space.w_None):
                tup = [self.w_class, space.wrap(function.name)]
            else:
                tup = [w_instance, space.wrap(function.name)]
        elif space.is_w( self.w_class, space.w_None ):
            tup = [self.w_function, w_instance]
        else:
            tup = [self.w_function, w_instance, self.w_class]
        return space.newtuple([new_inst, space.newtuple(tup)])

class StaticMethod(Wrappable):
    """The staticmethod objects."""
    _immutable_fields_ = ['w_function']

    def __init__(self, w_function):
        self.w_function = w_function

    def descr_staticmethod_get(self, w_obj, w_cls=None):
        """staticmethod(x).__get__(obj[, type]) -> x"""
        return self.w_function

    def descr_staticmethod__new__(space, w_subtype, w_function):
        instance = space.allocate_instance(StaticMethod, w_subtype)
        instance.__init__(w_function)
        return space.wrap(instance)

class ClassMethod(Wrappable):
    """The classmethod objects."""
    _immutable_fields_ = ['w_function']

    def __init__(self, w_function):
        self.w_function = w_function

    def descr_classmethod_get(self, space, w_obj, w_klass=None):
        if space.is_none(w_klass):
            w_klass = space.type(w_obj)
        return space.wrap(Method(space, self.w_function, w_klass, space.w_None))

    def descr_classmethod__new__(space, w_subtype, w_function):
        instance = space.allocate_instance(ClassMethod, w_subtype)
        instance.__init__(w_function)
        return space.wrap(instance)

class FunctionWithFixedCode(Function):
    can_change_code = False

class BuiltinFunction(Function):
    can_change_code = False

    def __init__(self, func):
        assert isinstance(func, Function)
        Function.__init__(self, func.space, func.code, func.w_func_globals,
                          func.defs_w, func.closure, func.name)
        self.w_doc = func.w_doc
        self.w_func_dict = func.w_func_dict
        self.w_module = func.w_module

    def descr_builtinfunction__new__(space, w_subtype):
        raise OperationError(space.w_TypeError,
                     space.wrap("cannot create 'builtin_function' instances"))

    def descr_function_repr(self):
        return self.space.wrap('<built-in function %s>' % (self.name,))

def is_builtin_code(w_func):
    from pypy.interpreter.gateway import BuiltinCode
    if isinstance(w_func, Method):
        w_func = w_func.w_function
    if isinstance(w_func, Function):
        code = w_func.getcode()
    else:
        code = None
    return isinstance(code, BuiltinCode)