this repo has no description
1#!/usr/bin/env python3
2# Copyright (c) Facebook, Inc. and its affiliates. (http://www.facebook.com)
3# $builtin-init-module$
4
5
6# These values are all injected by our boot process. flake8 has no knowledge
7# about their definitions and will complain without these lines.
8CO_NEWLOCALS = CO_NEWLOCALS # noqa: F821
9CO_NOFREE = CO_NOFREE # noqa: F821
10CO_OPTIMIZED = CO_OPTIMIZED # noqa: F821
11opmap = opmap # noqa: F821
12
13EXTENDED_ARG = opmap["EXTENDED_ARG"]
14LOAD_CONST = opmap["LOAD_CONST"]
15LOAD_FAST = opmap["LOAD_FAST"]
16BUILD_TUPLE = opmap["BUILD_TUPLE"]
17CALL_FUNCTION = opmap["CALL_FUNCTION"]
18RETURN_VALUE = opmap["RETURN_VALUE"]
19
20
21def _emit_op(bytecode, opcode, arg):
22 assert 0 <= arg <= 0xFFFFFFFF
23 if arg > 0xFFFFFF:
24 bytecode.append(EXTENDED_ARG)
25 bytecode.append((arg >> 24) & 0xFF)
26 if arg > 0xFFFF:
27 bytecode.append(EXTENDED_ARG)
28 bytecode.append((arg >> 16) & 0xFF)
29 if arg > 0xFF:
30 bytecode.append(EXTENDED_ARG)
31 bytecode.append((arg >> 8) & 0xFF)
32 bytecode.append(opcode)
33 bytecode.append(arg & 0xFF)
34
35
36def _make_dunder_new(modulename, name, arg_list, target_func):
37 FunctionType = type(_make_dunder_new)
38 CodeType = type(_make_dunder_new.__code__)
39
40 argcount = len(arg_list) + 1
41 posonlyargcount = 0
42 kwonlyargcount = 0
43 nlocals = argcount + 1
44 stacksize = nlocals + 1
45 flags = CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE
46 constants = (target_func,)
47 names = ()
48 varnames = ("_cls", *arg_list)
49 filename = ""
50 name = "__new__"
51 firstlineno = 1
52 lnotab = b""
53 bytecode = bytearray()
54 _emit_op(bytecode, LOAD_CONST, 0)
55 for i in range(argcount):
56 _emit_op(bytecode, LOAD_FAST, i)
57 _emit_op(bytecode, BUILD_TUPLE, argcount - 1)
58 _emit_op(bytecode, CALL_FUNCTION, 2)
59 _emit_op(bytecode, RETURN_VALUE, 0)
60 code = CodeType(
61 argcount,
62 posonlyargcount,
63 kwonlyargcount,
64 nlocals,
65 stacksize,
66 flags,
67 bytes(bytecode),
68 constants,
69 names,
70 varnames,
71 filename,
72 name,
73 firstlineno,
74 lnotab,
75 )
76 return FunctionType(code, {}, name)