File: code_tracing.py

package info (click to toggle)
python-enaml 0.19.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,284 kB
  • sloc: python: 31,443; cpp: 4,499; makefile: 140; javascript: 68; lisp: 53; sh: 20
file content (933 lines) | stat: -rw-r--r-- 37,715 bytes parent folder | download
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
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
# ------------------------------------------------------------------------------
# Copyright (c) 2013-2025, Nucleic Development Team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file LICENSE, distributed with this software.
# ------------------------------------------------------------------------------
from types import CodeType

import bytecode as bc
from ..compat import PY311, PY312, PY313, PY314


class CodeTracer(object):
    """A base class for implementing code tracers.

    This class defines the interface for a code tracer object, which is
    an object which can be passed as the first argument to a code object
    which has been transformed to enable tracing. Methods on the tracer
    are called with relevant arguments from the Python stack when that
    particular code segment is executing. The return value of a tracer
    method is ignored; exceptions are propagated.

    """

    __slots__ = ()

    def dynamic_load(self, obj, attr, value):
        """Called when an dynamic attribute is loaded.

        This method is called by the dynamic scope when it loads an
        attribute from some object in the object hierarchy due to the
        execution of the LOAD_NAME opcode.

        Parameters
        ----------
        obj : Object
            The Enaml object which owns the dynamically scoped attr.

        attr : str
            The name of the attribute which was loaded.

        value : object
            The value which was loaded.

        """
        pass

    def load_attr(self, obj, attr):
        """Called before the LOAD_ATTR opcode is executed.

        Parameters
        ----------
        obj : object
            The object which owns the attribute.

        attr : string
            The attribute being loaded.

        """
        pass

    def call_function(self, func, argtuple, argspec):
        """Called before the CALL opcode is executed.

        The CALL is only used for function call with only positional
        arguments in Python 3.6+ so argspec is actually just the argument
        number but is kept for backward compatibility.

        Parameters
        ----------
        func : object
            The object being called.

        argtuple : tuple
            The argument tuple from the stack.

        argspec : int
            Len of the argtuple kept for backward compatibility.

        """
        pass

    def binary_subscr(self, obj, idx):
        """Called before the BINARY_SUBSCR opcode is executed.

        Parameters
        ----------
        obj : object
            The object being indexed.

        idx : object
            The index.

        """
        pass

    def get_iter(self, obj):
        """Called before the GET_ITER opcode is executed.

        Parameters
        ----------
        obj : object
            The object which should return an iterator.

        """
        pass

    def return_value(self, value):
        """Called before the RETURN_VALUE opcode is executed.

        Parameters
        ----------
        value : object
            The value that will be returned from the code object.

        """
        pass


class CodeInverter(object):
    """A base class for implementing code inverters.

    This class defines the interface for a code inverter object, which is
    an object which can be passed as the first argument to a code object
    which has been transformed to enable inversion. The methods on the
    inverter are called with relevant arguments from the Python stack
    when that particular code segment is executing. The return values of
    a tracer method is ignored; exceptions are propagated.

    The default behavior of an inverter is to raise. Implementations
    must provide their own code in order to enable inversion.

    """

    __slots__ = ()

    def fail(self):
        """Called by handlers to raise an inversion exception."""
        raise RuntimeError("can't assign to expression")

    def load_name(self, name, value):
        """Called before the LOAD_NAME opcode is executed.

        This method should perform a STORE_NAME operation.

        Parameters
        ----------
        name : string
            The name being loaded.

        value : object
            The value to store.

        """
        self.fail()

    def load_attr(self, obj, attr, value):
        """Called before the LOAD_ATTR opcode is executed.

        This method should perform a STORE_ATTR operation.

        Parameters
        ----------
        obj : object
            The object which owns the attribute.

        attr : string
            The attribute being loaded.

        value : object
            The value to store

        """
        self.fail()

    def call_function(self, func, argtuple, argspec, value):
        """Called before the CALL_FUNCTION opcode is executed.

        The CALL_FUNCTION is only used for function call with only positional
        arguments in Python 3.6+ so argspec is actually just the argument
        number but is kept for backward compatibility.

        This method should perform an appropriate store operation.

        Parameters
        ----------
        func : object
            The object being called.

        argtuple : tuple
            The argument tuple from the stack.

        argspec : int
            Len of the argtuple kept for backward compatibility.

        value : object
            The value to store.

        Notes
        -----
        The semantics of the arguments is identical to the method
        `call_function` on the `CodeTracer` type.

        """
        self.fail()

    def binary_subscr(self, obj, idx, value):
        """Called before the BINARY_SUBSCR opcode is executed.

        This method should perform a STORE_SUBSCR operation.

        Parameters
        ----------
        obj : object
            The object being indexed.

        idx : object
            The index.

        value : object
            The value to store.

        """
        self.fail()


if PY313:

    def call_tracer_load_attr(tracer_op: str, i_arg: int, Instr=bc.Instr):
        return [  # obj
            Instr(tracer_op, "_[tracer]"),  # obj -> tracer
            Instr("LOAD_ATTR", (False, "load_attr")),  # obj -> tracefunc
            Instr("PUSH_NULL"),  # obj -> tracefunc -> null
            Instr("COPY", 3),  # obj -> tracefunc -> null -> obj
            Instr("LOAD_CONST", i_arg),  # obj -> tracefunc -> null -> obj -> attr
            Instr("CALL", 0x0002),  # obj -> retval
            Instr("POP_TOP"),  # obj
        ]

    def call_tracer_call_function(tracer_op: str, i_arg: int, Instr=bc.Instr):
        # n_stack_args = i_arg
        return [  # func -> null -> arg(0) -> arg(1) -> ... -> arg(n-1)
            Instr("BUILD_TUPLE", i_arg),  # func -> null -> argtuple
            Instr(tracer_op, "_[tracer]"),  # func -> null -> argtuple -> tracer
            Instr(
                "LOAD_ATTR", (False, "call_function")
            ),  # func -> null -> argtuple -> tracefunc
            Instr("PUSH_NULL"),  # func -> null -> argtuple -> tracefunc -> null
            Instr("COPY", 5),  # func -> null -> argtuple -> tracefunc -> null -> func
            Instr(
                "COPY", 4
            ),  # func -> null -> argtuple -> tracefunc -> null -> func -> argtuple
            Instr(
                "LOAD_CONST", i_arg
            ),  # func -> null -> argtuple -> tracefunc -> null -> func -> argtuple -> argspec
            Instr("CALL", 0x0003),  # func -> null -> argtuple -> retval
            Instr("POP_TOP"),  # func -> null -> argtuple
            Instr(
                "UNPACK_SEQUENCE", i_arg
            ),  # func -> null -> arg(n-1) -> arg(n-2) -> ... -> arg(0)
            Instr("BUILD_TUPLE", i_arg),  # func -> null -> reversedargtuple
            Instr(
                "UNPACK_SEQUENCE", i_arg
            ),  # func -> null -> arg(0) -> arg(1) -> ... -> arg(n-1)
        ]

    def call_tracer_binary_subscr(tracer_op: str, Instr=bc.Instr):
        return [  # obj -> idx
            Instr(tracer_op, "_[tracer]"),  # obj -> idx -> tracer
            Instr("LOAD_ATTR", (False, "binary_subscr")),  # obj -> idx -> tracefunc
            Instr("PUSH_NULL"),  # obj -> idx -> tracefunc -> null
            Instr("COPY", 4),  # obj -> idx -> tracefunc -> null -> obj
            Instr("COPY", 4),  # obj -> idx -> tracefunc -> null -> obj -> idx
            Instr("CALL", 0x0002),  # obj -> idx -> retval
            Instr("POP_TOP"),  # obj -> idx
        ]

    def call_tracer_get_iter(tracer_op: str, Instr=bc.Instr):
        return [  # obj
            Instr(tracer_op, "_[tracer]"),  # obj -> tracer
            Instr("LOAD_ATTR", (False, "get_iter")),  # obj -> tracefunc
            Instr("PUSH_NULL"),  # obj -> tracefunc -> null
            Instr("COPY", 3),  # obj -> tracefunc -> null -> obj
            Instr("CALL", 0x0001),  # obj -> retval
            Instr("POP_TOP"),  # obj
        ]

    def call_tracer_return_value(tracer_op: str, Instr=bc.Instr):
        return [
            Instr(tracer_op, "_[tracer]"),  # obj -> tracer
            Instr("LOAD_ATTR", (False, "return_value")),  # obj -> tracefunc
            Instr("PUSH_NULL"),  # obj -> tracefunc -> null
            Instr("COPY", 3),  # obj -> tracefunc -> null -> obj
            Instr("CALL", 0x0001),  # obj -> retval
            Instr("POP_TOP"),  # obj
        ]

    def call_tracer_return_const(tracer_op: str, const, Instr=bc.Instr):
        return [
            Instr(tracer_op, "_[tracer]"),  # obj -> tracer
            Instr("LOAD_ATTR", (False, "return_value")),  # obj -> tracefunc
            Instr("PUSH_NULL"),  # obj -> tracefunc -> null
            Instr("LOAD_CONST", const),  # obj -> tracefunc -> null -> obj
            Instr("CALL", 0x0001),  # obj -> retval
            Instr("POP_TOP"),  # obj
        ]

elif PY312:

    def call_tracer_load_attr(tracer_op: str, i_arg: int, Instr=bc.Instr):
        return [  # obj
            Instr("PUSH_NULL"),  # obj -> null
            Instr(tracer_op, "_[tracer]"),  # obj -> null -> tracer
            Instr("LOAD_ATTR", (False, "load_attr")),  # obj -> null -> tracefunc
            Instr("COPY", 3),  # obj -> null -> tracefunc -> obj
            Instr("LOAD_CONST", i_arg),  # obj -> null -> tracefunc -> obj -> attr
            Instr("CALL", 0x0002),  # obj -> retval
            Instr("POP_TOP"),  # obj
        ]

    def call_tracer_call_function(tracer_op: str, i_arg: int, Instr=bc.Instr):
        # n_stack_args = i_arg
        return [  # func -> arg(0) -> arg(1) -> ... -> arg(n-1)
            Instr("BUILD_TUPLE", i_arg),  # func -> argtuple
            Instr("PUSH_NULL"),  # func -> argtuple -> null
            Instr(tracer_op, "_[tracer]"),  # func -> argtuple -> null -> tracer
            Instr(
                "LOAD_ATTR", (False, "call_function")
            ),  # func -> argtuple -> null -> tracefunc
            Instr("COPY", 4),  # func -> argtuple -> null -> tracefunc -> func
            Instr(
                "COPY", 4
            ),  # func -> argtuple -> null -> tracefunc -> func -> argtuple
            Instr(
                "LOAD_CONST", i_arg
            ),  # func -> argtuple -> null -> tracefunc -> func -> argtuple -> argspec
            Instr("CALL", 0x0003),  # func -> argtuple -> retval
            Instr("POP_TOP"),  # func -> argtuple
            Instr(
                "UNPACK_SEQUENCE", i_arg
            ),  # func -> arg(n-1) -> arg(n-2) -> ... -> arg(0)
            Instr("BUILD_TUPLE", i_arg),  # func -> reversedargtuple
            Instr(
                "UNPACK_SEQUENCE", i_arg
            ),  # func -> arg(0) -> arg(1) -> ... -> arg(n-1)
        ]

    def call_tracer_binary_subscr(tracer_op: str, Instr=bc.Instr):
        return [  # obj -> idx
            Instr("PUSH_NULL"),  # obj -> idx -> null
            Instr(tracer_op, "_[tracer]"),  # obj -> idx -> null -> tracer
            Instr(
                "LOAD_ATTR", (False, "binary_subscr")
            ),  # obj -> idx -> null -> tracefunc
            Instr("COPY", 4),  # obj -> idx -> null -> tracefunc -> obj
            Instr("COPY", 4),  # obj -> idx -> null -> tracefunc -> obj -> idx
            Instr("CALL", 0x0002),  # obj -> idx -> retval
            Instr("POP_TOP"),  # obj -> idx
        ]

    def call_tracer_get_iter(tracer_op: str, Instr=bc.Instr):
        return [  # obj
            Instr("PUSH_NULL"),  # obj -> null
            Instr(tracer_op, "_[tracer]"),  # obj -> null -> tracer
            Instr("LOAD_ATTR", (False, "get_iter")),  # obj -> null -> tracefunc
            Instr("COPY", 3),  # obj -> null -> tracefunc -> obj
            Instr("CALL", 0x0001),  # obj -> retval
            Instr("POP_TOP"),  # obj
        ]

    def call_tracer_return_value(tracer_op: str, Instr=bc.Instr):
        return [
            Instr("PUSH_NULL"),  # obj -> null
            Instr(tracer_op, "_[tracer]"),  # obj -> null -> tracer
            Instr("LOAD_ATTR", (False, "return_value")),  # obj -> null -> tracefunc
            Instr("COPY", 3),  # obj -> null -> tracefunc -> obj
            Instr("CALL", 0x0001),  # obj -> retval
            Instr("POP_TOP"),  # obj
        ]

    def call_tracer_return_const(tracer_op: str, const, Instr=bc.Instr):
        return [
            Instr("PUSH_NULL"),  # obj -> null
            Instr(tracer_op, "_[tracer]"),  # obj -> null -> tracer
            Instr("LOAD_ATTR", (False, "return_value")),  # obj -> null -> tracefunc
            Instr("LOAD_CONST", const),  # obj -> null -> tracefunc -> obj
            Instr("CALL", 0x0001),  # obj -> retval
            Instr("POP_TOP"),  # obj
        ]

elif PY311:

    def call_tracer_load_attr(tracer_op: str, i_arg: int, Instr=bc.Instr):
        return [  # obj
            Instr("PUSH_NULL"),  # obj -> null
            Instr(tracer_op, "_[tracer]"),  # obj -> null -> tracer
            Instr("LOAD_ATTR", "load_attr"),  # obj -> null -> tracefunc
            Instr("COPY", 3),  # obj -> null -> tracefunc -> obj
            Instr("LOAD_CONST", i_arg),  # obj -> null -> tracefunc -> obj -> attr
            Instr("PRECALL", 0x0002),
            Instr("CALL", 0x0002),  # obj -> retval
            Instr("POP_TOP"),  # obj
        ]

    def call_tracer_call_function(tracer_op: str, i_arg: int, Instr=bc.Instr):
        # n_stack_args = i_arg
        return [  # func -> arg(0) -> arg(1) -> ... -> arg(n-1)
            Instr("BUILD_TUPLE", i_arg),  # func -> argtuple
            Instr("PUSH_NULL"),  # func -> argtuple -> null
            Instr(tracer_op, "_[tracer]"),  # func -> argtuple -> null -> tracer
            Instr(
                "LOAD_ATTR", "call_function"
            ),  # func -> argtuple -> null -> tracefunc
            Instr("COPY", 4),  # func -> argtuple -> null -> tracefunc -> func
            Instr(
                "COPY", 4
            ),  # func -> argtuple -> null -> tracefunc -> func -> argtuple
            Instr(
                "LOAD_CONST", i_arg
            ),  # func -> argtuple -> null -> tracefunc -> func -> argtuple -> argspec
            Instr("PRECALL", 0x0003),
            Instr("CALL", 0x0003),  # func -> argtuple -> retval
            Instr("POP_TOP"),  # func -> argtuple
            Instr(
                "UNPACK_SEQUENCE", i_arg
            ),  # func -> arg(n-1) -> arg(n-2) -> ... -> arg(0)
            Instr("BUILD_TUPLE", i_arg),  # func -> reversedargtuple
            Instr(
                "UNPACK_SEQUENCE", i_arg
            ),  # func -> arg(0) -> arg(1) -> ... -> arg(n-1)
        ]

    def call_tracer_binary_subscr(tracer_op: str, Instr=bc.Instr):
        return [  # obj -> idx
            Instr("PUSH_NULL"),  # obj -> idx -> null
            Instr(tracer_op, "_[tracer]"),  # obj -> idx -> null -> tracer
            Instr("LOAD_ATTR", "binary_subscr"),  # obj -> idx -> null -> tracefunc
            Instr("COPY", 4),  # obj -> idx -> null -> tracefunc -> obj
            Instr("COPY", 4),  # obj -> idx -> null -> tracefunc -> obj -> idx
            Instr("PRECALL", 0x0002),
            Instr("CALL", 0x0002),  # obj -> idx -> retval
            Instr("POP_TOP"),  # obj -> idx
        ]

    def call_tracer_get_iter(tracer_op: str, Instr=bc.Instr):
        return [  # obj
            Instr("PUSH_NULL"),  # obj -> null
            Instr(tracer_op, "_[tracer]"),  # obj -> null -> tracer
            Instr("LOAD_ATTR", "get_iter"),  # obj -> null -> tracefunc
            Instr("COPY", 3),  # obj -> null -> tracefunc -> obj
            Instr("PRECALL", 0x0001),
            Instr("CALL", 0x0001),  # obj -> retval
            Instr("POP_TOP"),  # obj
        ]

    def call_tracer_return_value(tracer_op: str, Instr=bc.Instr):
        return [
            Instr("PUSH_NULL"),  # obj -> null
            Instr(tracer_op, "_[tracer]"),  # obj -> null -> tracer
            Instr("LOAD_ATTR", "return_value"),  # obj -> null -> tracefunc
            Instr("COPY", 3),  # obj -> null -> tracefunc -> obj
            Instr("PRECALL", 0x0001),
            Instr("CALL", 0x0001),  # obj -> retval
            Instr("POP_TOP"),  # obj
        ]

    def call_tracer_return_const(tracer_op: str, const, Instr=bc.Instr):
        raise NotImplementedError()

else:

    def call_tracer_load_attr(tracer_op: str, i_arg: int, Instr=bc.Instr):
        return [  # obj
            Instr("DUP_TOP"),  # obj -> obj
            Instr(tracer_op, "_[tracer]"),  # obj -> obj -> tracer
            Instr("LOAD_ATTR", "load_attr"),  # obj -> obj -> tracefunc
            Instr("ROT_TWO"),  # obj -> tracefunc -> obj
            Instr("LOAD_CONST", i_arg),  # obj -> tracefunc -> obj -> attr
            Instr("CALL_FUNCTION", 0x0002),  # obj -> retval
            Instr("POP_TOP"),  # obj
        ]

    def call_tracer_call_function(tracer_op: str, i_arg: int, Instr=bc.Instr):
        # n_stack_args = i_arg
        return [  # func -> arg(0) -> arg(1) -> ... -> arg(n-1)
            Instr("BUILD_TUPLE", i_arg),  # func -> argtuple
            Instr("DUP_TOP_TWO"),  # func -> argtuple -> func -> argtuple
            Instr(
                tracer_op, "_[tracer]"
            ),  # func -> argtuple -> func -> argtuple -> tracer
            Instr(
                "LOAD_ATTR", "call_function"
            ),  # func -> argtuple -> func -> argtuple -> tracefunc
            Instr("ROT_THREE"),  # func -> argtuple -> tracefunc -> func -> argtuple
            Instr(
                "LOAD_CONST", i_arg
            ),  # func -> argtuple -> tracefunc -> func -> argtuple -> argspec
            Instr("CALL_FUNCTION", 0x0003),  # func -> argtuple -> retval
            Instr("POP_TOP"),  # func -> argtuple
            Instr(
                "UNPACK_SEQUENCE", i_arg
            ),  # func -> arg(n-1) -> arg(n-2) -> ... -> arg(0)
            Instr("BUILD_TUPLE", i_arg),  # func -> reversedargtuple
            Instr(
                "UNPACK_SEQUENCE", i_arg
            ),  # func -> arg(0) -> arg(1) -> ... -> arg(n-1)
        ]

    def call_tracer_binary_subscr(tracer_op: str, Instr=bc.Instr):
        return [  # obj -> idx
            Instr("DUP_TOP_TWO"),  # obj -> idx -> obj -> idx
            Instr(tracer_op, "_[tracer]"),  # obj -> idx -> obj -> idx -> tracer
            Instr(
                "LOAD_ATTR", "binary_subscr"
            ),  # obj -> idx -> obj -> idx -> tracefunc
            Instr("ROT_THREE"),  # obj -> idx -> tracefunc -> obj -> idx
            Instr("CALL_FUNCTION", 0x0002),  # obj -> idx -> retval
            Instr("POP_TOP"),  # obj -> idx
        ]

    def call_tracer_get_iter(tracer_op: str, Instr=bc.Instr):
        return [  # obj
            Instr("DUP_TOP"),  # obj -> obj
            Instr(tracer_op, "_[tracer]"),  # obj -> obj -> tracer
            Instr("LOAD_ATTR", "get_iter"),  # obj -> obj -> tracefunc
            Instr("ROT_TWO"),  # obj -> tracefunc -> obj
            Instr("CALL_FUNCTION", 0x0001),  # obj -> retval
            Instr("POP_TOP"),  # obj
        ]

    def call_tracer_return_value(tracer_op: str, Instr=bc.Instr):
        return [  # obj
            Instr("DUP_TOP"),  # obj -> obj
            Instr(tracer_op, "_[tracer]"),  # obj -> obj -> tracer
            Instr("LOAD_ATTR", "return_value"),  # obj -> obj -> tracefunc
            Instr("ROT_TWO"),  # obj -> tracefunc -> obj
            Instr("CALL_FUNCTION", 0x0001),  # obj -> retval
            Instr("POP_TOP"),  # obj
        ]

    def call_tracer_return_const(tracer_op: str, const, Instr=bc.Instr):
        raise NotImplementedError()


def inject_tracing(bytecode, nested=False):
    """Inject tracing code into the given code list.

    This will inject the bytecode operations required to trace the
    execution of the code using a `CodeTracer` object. The generated
    opcodes expect a fast local '_[tracer]' to be available when the
    code is executed.

    Parameters
    ----------
    bytecode : bytecode.Bytecode
        The bytecode to modify.
    nested : bool
        Is the code modified defined inside an already traced code in which
        case the tracer is in the local namespace and not the fast locals.

    Returns
    -------
    result : list
        A *new* list of code ops which implement the desired behavior.

    """
    # If the code is nested into another already traced code, we need to use
    # LOAD_NAME to access the tracer rather than LOAD_FAST
    tracer_op = "LOAD_NAME" if nested else "LOAD_FAST"

    # This builds a mapping of code idx to a list of ops, which are the
    # tracing bytecode instructions which will be inserted into the code
    # object being transformed. The ops assume that a tracer object is
    # available in the fast locals using a non-clashable name. All of
    # the ops have a net-zero effect on the execution stack. Provided
    # that the tracer has no visible side effects, the tracing is
    # transparent.
    inserts = {}
    i_name = None
    for idx, instr in enumerate(bytecode):
        # Filter out SetLineno and Label
        if not isinstance(instr, bc.Instr):
            continue
        last_i_name = i_name
        i_name = instr.name
        i_arg = instr.arg
        if i_name == "LOAD_ATTR":
            inserts[idx] = call_tracer_load_attr(
                tracer_op, i_arg[1] if PY312 else i_arg
            )
        # We do not trace CALL_FUNCTION_KW and CALL_FUNCTION_EX since
        # tracing and inverting only require to detect getattr and setattr
        # both in this project and in traits-enaml. Those two are always called
        # using the CALL_FUNCTION bytecode instruction.
        elif i_name == "CALL_FUNCTION":
            # From Python 3.6, CALL_FUNCTION is only used for positional
            # arguments and the argument is directly the number of arguments
            # on the stack.
            inserts[idx] = call_tracer_call_function(tracer_op, i_arg)
        elif PY312 and i_name == "CALL" and last_i_name != "KW_NAMES":
            # On Python 3.12, CALL is not preceded with a PRECALL
            # Skip, if the last instruction was a KW_NAMES
            inserts[idx] = call_tracer_call_function(tracer_op, i_arg)
        elif i_name == "PRECALL" and last_i_name != "KW_NAMES":
            # On Python 3.11, CALL is preceded with a PRECALL
            # Skip, if the last instruction was a KW_NAMES
            inserts[idx] = call_tracer_call_function(tracer_op, i_arg)
        elif i_name == "BINARY_SUBSCR" or (
            PY314 and i_name == "BINARY_OP" and i_arg == bc.BinaryOp.SUBSCR
        ):
            inserts[idx] = call_tracer_binary_subscr(tracer_op)
        elif i_name == "GET_ITER":
            inserts[idx] = call_tracer_get_iter(tracer_op)
        elif i_name == "RETURN_VALUE":
            inserts[idx] = call_tracer_return_value(tracer_op)
        elif i_name == "RETURN_CONST":
            inserts[idx] = call_tracer_return_const(tracer_op, i_arg)
        elif isinstance(i_arg, CodeType):
            # Inject tracing in nested code object if they use their parent
            # locals.
            inner = bc.Bytecode.from_code(i_arg)
            if not inner.flags & bc.CompilerFlags.NEWLOCALS:
                instr.arg = inject_tracing(inner, nested=True).to_code()

    # Create a new code list which interleaves the generated code with
    # the original code at the appropriate location.
    new_code = bytecode.copy()
    new_code.clear()
    for idx, code_op in enumerate(bytecode):
        if idx in inserts:
            new_code.extend(inserts[idx])
        new_code.append(code_op)

    return new_code


if PY313:

    def call_inverter_load_name(i_arg: int, Instr=bc.Instr):
        return [
            Instr("LOAD_FAST", "_[inverter]"),  # inverter
            Instr("LOAD_ATTR", (False, "load_name")),  # invertfunc
            Instr("PUSH_NULL"),  # inverterfunc -> null
            Instr("LOAD_CONST", i_arg),  # invertfunc -> null -> name
            Instr("LOAD_FAST", "_[value]"),  # invertfunc -> null -> name - > value
            Instr("CALL", 0x0002),  # retval
            Instr("RETURN_VALUE"),  #
        ]

    def call_inverter_load_attr(i_arg: int, Instr=bc.Instr):
        return [  # obj
            Instr("LOAD_FAST", "_[inverter]"),  # obj -> inverter
            Instr("LOAD_ATTR", (False, "load_attr")),  # obj -> invertfunc
            Instr("SWAP", 2),  # invertfunc -> obj
            Instr("PUSH_NULL"),  # invertfunc -> obj -> null
            Instr("SWAP", 2),  # invertfunc -> null -> obj
            Instr("LOAD_CONST", i_arg),  # invertfunc -> null -> obj -> attr
            Instr(
                "LOAD_FAST", "_[value]"
            ),  # invertfunc -> null -> obj -> attr -> value
            Instr("CALL", 0x0003),  # retval
            Instr("RETURN_VALUE"),  #
        ]

    def call_inverter_call_function(i_arg: int, Instr=bc.Instr):
        # n_stack_args = i_arg
        return [  # func -> null -> arg(0) -> arg(1) -> ... -> arg(n-1)
            Instr("BUILD_TUPLE", i_arg),  # func -> null -> argtuple
            Instr("LOAD_FAST", "_[inverter]"),  # func -> null -> argtuple -> inverter
            Instr(
                "LOAD_ATTR", (False, "call_function")
            ),  # func -> null -> argtuple -> invertfunc
            Instr("SWAP", 4),  # invertfunc -> null -> argtuple -> func
            Instr("SWAP", 2),  # invertfunc -> null -> func -> argtuple
            Instr(
                "LOAD_CONST", i_arg
            ),  # invertfunc -> null -> func -> argtuple -> argspec
            Instr(
                "LOAD_FAST", "_[value]"
            ),  # invertfunc -> null -> func -> argtuple -> argspec -> value
            Instr("CALL", 0x0004),  # retval
            Instr("RETURN_VALUE"),  #
        ]

    def call_inverter_binary_subsrc(Instr=bc.Instr):
        return [  # obj -> index
            Instr("PUSH_NULL"),  # obj -> index -> null
            Instr("SWAP", 2),  # obj -> null -> index
            Instr("LOAD_FAST", "_[inverter]"),  # obj -> null -> index -> inverter
            Instr(
                "LOAD_ATTR", (False, "binary_subscr")
            ),  # obj -> null -> index -> invertfunc
            Instr("SWAP", 4),  # invertfunc -> null -> index ->  obj
            Instr("SWAP", 2),  # invertfunc -> null ->  obj -> index
            Instr(
                "LOAD_FAST", "_[value]"
            ),  # invertfunc -> null ->  obj -> index -> value
            Instr("CALL", 0x0003),  # retval
            Instr("RETURN_VALUE"),  #
        ]
elif PY312:

    def call_inverter_load_name(i_arg: int, Instr=bc.Instr):
        return [
            Instr("PUSH_NULL"),  # null -> inverter
            Instr("LOAD_FAST", "_[inverter]"),  # null -> inverter
            Instr("LOAD_ATTR", (False, "load_name")),  # null -> invertfunc
            Instr("LOAD_CONST", i_arg),  # null -> invertfunc -> name
            Instr("LOAD_FAST", "_[value]"),  # null -> invertfunc -> name - > value
            Instr("CALL", 0x0002),  # retval
            Instr("RETURN_VALUE"),  #
        ]

    def call_inverter_load_attr(i_arg: int, Instr=bc.Instr):
        return [  # obj
            Instr("PUSH_NULL"),  # obj -> null
            Instr("SWAP", 2),  # null -> obj
            Instr("LOAD_FAST", "_[inverter]"),  # null -> obj -> inverter
            Instr("LOAD_ATTR", (False, "load_attr")),  # null -> obj -> invertfunc
            Instr("SWAP", 2),  # null -> invertfunc -> obj
            Instr("LOAD_CONST", i_arg),  # null -> invertfunc -> obj -> attr
            Instr(
                "LOAD_FAST", "_[value]"
            ),  # null -> invertfunc -> obj -> attr -> value
            Instr("CALL", 0x0003),  # retval
            Instr("RETURN_VALUE"),  #
        ]

    def call_inverter_call_function(i_arg: int, Instr=bc.Instr):
        # n_stack_args = i_arg
        return [  # func -> arg(0) -> arg(1) -> ... -> arg(n-1)
            Instr("BUILD_TUPLE", i_arg),  # func -> argtuple
            Instr("PUSH_NULL"),  # func -> argtuple -> null
            Instr("SWAP", 3),  # null -> argtuple -> func
            Instr("LOAD_FAST", "_[inverter]"),  # null -> argtuple -> func -> inverter
            Instr(
                "LOAD_ATTR", (False, "call_function")
            ),  # null -> argtuple -> func -> invertfunc
            Instr("SWAP", 3),  # null -> invertfunc -> func -> argtuple
            Instr(
                "LOAD_CONST", i_arg
            ),  # null -> invertfunc -> func -> argtuple -> argspec
            Instr(
                "LOAD_FAST", "_[value]"
            ),  # null -> invertfunc -> func -> argtuple -> argspec -> value
            Instr("CALL", 0x0004),  # retval
            Instr("RETURN_VALUE"),  #
        ]

    def call_inverter_binary_subsrc(Instr=bc.Instr):
        return [  # obj -> index
            Instr("PUSH_NULL"),  # obj -> index -> null
            Instr("SWAP", 3),  # null -> index -> obj
            Instr("LOAD_FAST", "_[inverter]"),  # null -> index -> obj -> inverter
            Instr(
                "LOAD_ATTR", (False, "binary_subscr")
            ),  # null -> index -> obj -> invertfunc
            Instr("SWAP", 3),  # null -> invertfunc -> obj -> index
            Instr(
                "LOAD_FAST", "_[value]"
            ),  # null -> invertfunc -> obj -> index -> value
            Instr("CALL", 0x0003),  # retval
            Instr("RETURN_VALUE"),  #
        ]
elif PY311:

    def call_inverter_load_name(i_arg: int, Instr=bc.Instr):
        return [
            Instr("PUSH_NULL"),  # null -> inverter
            Instr("LOAD_FAST", "_[inverter]"),  # null -> inverter
            Instr("LOAD_ATTR", "load_name"),  # null -> invertfunc
            Instr("LOAD_CONST", i_arg),  # null -> invertfunc -> name
            Instr("LOAD_FAST", "_[value]"),  # null -> invertfunc -> name - > value
            Instr("PRECALL", 0x0002),
            Instr("CALL", 0x0002),  # retval
            Instr("RETURN_VALUE"),  #
        ]

    def call_inverter_load_attr(i_arg: int, Instr=bc.Instr):
        return [  # obj
            Instr("PUSH_NULL"),  # obj -> null
            Instr("SWAP", 2),  # null -> obj
            Instr("LOAD_FAST", "_[inverter]"),  # null -> obj -> inverter
            Instr("LOAD_ATTR", "load_attr"),  # null -> obj -> invertfunc
            Instr("SWAP", 2),  # null -> invertfunc -> obj
            Instr("LOAD_CONST", i_arg),  # null -> invertfunc -> obj -> attr
            Instr(
                "LOAD_FAST", "_[value]"
            ),  # null -> invertfunc -> obj -> attr -> value
            Instr("PRECALL", 0x0003),  #
            Instr("CALL", 0x0003),  # retval
            Instr("RETURN_VALUE"),  #
        ]

    def call_inverter_call_function(i_arg: int, Instr=bc.Instr):
        # n_stack_args = i_arg
        return [  # func -> arg(0) -> arg(1) -> ... -> arg(n-1)
            Instr("BUILD_TUPLE", i_arg),  # func -> argtuple
            Instr("PUSH_NULL"),  # func -> argtuple -> null
            Instr("SWAP", 3),  # null -> argtuple -> func
            Instr("LOAD_FAST", "_[inverter]"),  # null -> argtuple -> func -> inverter
            Instr(
                "LOAD_ATTR", "call_function"
            ),  # null -> argtuple -> func -> invertfunc
            Instr("SWAP", 3),  # null -> invertfunc -> func -> argtuple
            Instr(
                "LOAD_CONST", i_arg
            ),  # null -> invertfunc -> func -> argtuple -> argspec
            Instr(
                "LOAD_FAST", "_[value]"
            ),  # null -> invertfunc -> func -> argtuple -> argspec -> value
            Instr("PRECALL", 0x0004),
            Instr("CALL", 0x0004),  # retval
            Instr("RETURN_VALUE"),  #
        ]

    def call_inverter_binary_subsrc(Instr=bc.Instr):
        return [  # obj -> index
            Instr("PUSH_NULL"),  # obj -> index -> null
            Instr("SWAP", 3),  # null -> index -> obj
            Instr("LOAD_FAST", "_[inverter]"),  # null -> index -> obj -> inverter
            Instr("LOAD_ATTR", "binary_subscr"),  # null -> index -> obj -> invertfunc
            Instr("SWAP", 3),  # null -> invertfunc -> obj -> index
            Instr(
                "LOAD_FAST", "_[value]"
            ),  # null -> invertfunc -> obj -> index -> value
            Instr("PRECALL", 0x0003),  #
            Instr("CALL", 0x0003),  # retval
            Instr("RETURN_VALUE"),  #
        ]
else:

    def call_inverter_load_name(i_arg: int, Instr=bc.Instr):
        return [
            Instr("LOAD_FAST", "_[inverter]"),  # inverter
            Instr("LOAD_ATTR", "load_name"),  # invertfunc
            Instr("LOAD_CONST", i_arg),  # invertfunc -> name
            Instr("LOAD_FAST", "_[value]"),  # invertfunc -> name - > value
            Instr("CALL_FUNCTION", 0x0002),  # retval
            Instr("RETURN_VALUE"),  #
        ]

    def call_inverter_load_attr(i_arg: int, Instr=bc.Instr):
        return [  # obj
            Instr("LOAD_FAST", "_[inverter]"),  # obj -> inverter
            Instr("LOAD_ATTR", "load_attr"),  # obj -> invertfunc
            Instr("ROT_TWO"),  # invertfunc -> obj
            Instr("LOAD_CONST", i_arg),  # invertfunc -> obj -> attr
            Instr("LOAD_FAST", "_[value]"),  # invertfunc -> obj -> attr -> value
            Instr("CALL_FUNCTION", 0x0003),  # retval
            Instr("RETURN_VALUE"),  #
        ]

    def call_inverter_call_function(i_arg: int, Instr=bc.Instr):
        # n_stack_args = i_arg
        return [  # func -> arg(0) -> arg(1) -> ... -> arg(n-1)
            Instr("BUILD_TUPLE", i_arg),  # func -> argtuple
            Instr("LOAD_FAST", "_[inverter]"),  # func -> argtuple -> inverter
            Instr("LOAD_ATTR", "call_function"),  # func -> argtuple -> invertfunc
            Instr("ROT_THREE"),  # invertfunc -> func -> argtuple
            Instr("LOAD_CONST", i_arg),  # invertfunc -> func -> argtuple -> argspec
            Instr(
                "LOAD_FAST", "_[value]"
            ),  # invertfunc -> func -> argtuple -> argspec -> value
            Instr("CALL_FUNCTION", 0x0004),  # retval
            Instr("RETURN_VALUE"),  #
        ]

    def call_inverter_binary_subsrc(Instr=bc.Instr):
        return [  # obj -> index
            Instr("LOAD_FAST", "_[inverter]"),  # obj -> index -> inverter
            Instr("LOAD_ATTR", "binary_subscr"),  # obj -> index -> invertfunc
            Instr("ROT_THREE"),  # invertfunc -> obj -> index
            Instr("LOAD_FAST", "_[value]"),  # invertfunc -> obj -> index -> value
            Instr("CALL_FUNCTION", 0x0003),  # retval
            Instr("RETURN_VALUE"),  #
        ]


def inject_inversion(bytecode):
    """Inject inversion code into the given bytecode.

    This will inject the bytecode operations required to invert the
    execution of the code using a `CodeInverter` object. The generated
    opcodes expect the fast local '_[inverter]' and '_[value]' to be
    available when the code is executed.

    Parameters
    ----------
    bytecode : bc.Bytecode
        The list of code ops to modify.

    Returns
    -------
    result : list
        A *new* list of code ops which implement the desired behavior.

    Raises
    ------
    ValueError
        The given code is not suitable for inversion.

    """
    instr = bytecode[-2]
    i_name, i_arg = instr.name, instr.arg
    new_code = bytecode[:-2]
    # In Python 3.11 there is a RESUME in the bytecode
    if i_name == "LOAD_NAME" and len(bytecode) >= 2:
        new_code.extend(call_inverter_load_name(i_arg))
    elif i_name == "LOAD_ATTR":
        new_code.extend(call_inverter_load_attr(i_arg[1] if PY312 else i_arg))
    elif i_name == "CALL":
        # In Python 3.11 PRECALL is before CALL (CALL is new in 3.11)
        if not PY312:
            new_code.pop()  # Remove PRECALL
        new_code.extend(call_inverter_call_function(i_arg))
    elif i_name == "CALL_FUNCTION":
        # In Python 3.6+ CALL_FUNCTION is only used for calls with positional arguments
        # and the argument of the opcode is the number of argument on the stack.
        new_code.extend(call_inverter_call_function(i_arg))
    # We do not trace CALL_FUNCTION_KW and CALL_FUNCTION_EX since tracing and
    # inverting only require to detect getattr and setattr both in this project
    # and in traits-enaml. Those two are always called using the CALL_FUNCTION
    # bytecode instruction.
    elif i_name == "BINARY_SUBSCR" or (
        PY314 and i_name == "BINARY_OP" and i_arg == bc.BinaryOp.SUBSCR
    ):
        new_code.extend(call_inverter_binary_subsrc())
    else:
        raise ValueError("can't invert code '%s'" % i_name)

    return new_code