পাইথন স্ট্যান্ডার্ড লাইব্রের리의 dis মডিউল পাইথন বাইটকোডের বিশ্লেষণে উপকারী çeşitli ফাংশন প্রদান করে যা এটি ডিজাসেম্বল করে মানুষ পঠনযোগ্য ফরম্যাটে রূপান্তর করে। এটি অপ্টিমাইজেশন করতে সাহায্য করে। বাইটকোড ইন্টারপ্রেটারের একটি ভার্সন-স্পেসিফিক ইমপ্লিমেন্টেশন ডিটেইল।
dis() ফাংশন
dis() ফাংশন যেকোনো পাইথন কোড সোর্স অর্থাৎ মডিউল, ক্লাস, মেথড, ফাংশন, বা কোড অবজেক্টের ডিজাসেম্বলড রিপ্রেজেন্টেশন জেনারেট করে।
>>> def hello(): print("hello world")
>>> import dis
>>> dis.dis(hello)
2 0 LOAD_GLOBAL 0 (print)
3 LOAD_CONST 1 ('hello world')
6 CALL_FUNCTION 1 (1 positional, 0 keyword pair)
9 POP_TOP
10 LOAD_CONST 0 (None)
13 RETURN_VALUE
Bytecode ক্লাস
বাইটকোড অ্যানালিসিস API Bytecode ক্লাসে সংজ্ঞায়িত। এর কনস্ট্রাক্টর Bytecode অবজেক্ট রিটার্ন করে যাতে বাইটকোড বিশ্লেষণের জন্য নিচের মেথডগুলো রয়েছে:
Bytecode()
এটি কনস্ট্রাক্টর। একটি ফাংশন, জেনারেটর, মেথড, সোর্স কোডের স্ট্রিং, বা একটি কোড অবজেক্টের সংশ্লিষ্ট বাইটকোড বিশ্লেষণ করে। এটি অনেক ফাংশন γύ wrappers।
>>> string = dis.Bytecode(hello)
>>> for x in string:
print(x)
Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='print', argrepr='print', offset=0, starts_line=2, is_jump_target=False)
Instruction(opname='LOAD_CONST', opcode=100, arg=1, argval='hello world', argrepr="'hello world'", offset=3, starts_line=None, is_jump_target=False)
Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='1 positional, 0 keyword pair', offset=6, starts_line=None, is_jump_target=False)
Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=9, starts_line=None, is_jump_target=False)
Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=10, starts_line=None, is_jump_target=False)
Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=13, starts_line=None, is_jump_target=False)
code_info()
এই ফাংশন পাইথন কোড অবজেক্টের ইনফরমেশন রিটার্ন করে।
>>> dis.code_info(hello) "Name: hello\nFilename:\nArgument count: 0\nKw-only arguments: 0\nNumber of locals: 0\nStack size: 2\nFlags: OPTIMIZED, NEWLOCALS, NOFREE\nConstants:\n 0: None\n 1: 'hello world'\nNames:\n 0: print"
show_code()
এই ফাংশন পাইথন মডিউল, ফাংশন বা ক্লাসের বিস্তারিত কোড ইনফরমেশন প্রিন্ট করে।
>>> dis.show_code(hello) Name: hello Filename:Argument count: 0 Kw-only arguments: 0 Number of locals: 0 Stack size: 2 Flags: OPTIMIZED, NEWLOCALS, NOFREE Constants: 0: None 1: 'hello world' Names: 0: print
disassemble()
এই ফাংশন একটি কোড অবজেক্ট ডিজাসেম্বল করে এবং আউটপুট নিচের কলামগুলোর ভাগ করে দেয়:
- প্রতিটি লাইনের প্রথম ইনস্ট্রাকশনের জন্য লাইন নম্বর,
- বর্তমান ইনস্ট্রাকশন, --> হিসেবে নির্দেশিত,
- লেবেলড ইনস্ট্রাকশন, >> হিসেবে নির্দেশিত,
- ইনস্ট্রাকশনের অ্যাড্রেস,
- অপারেশন কোডের নাম,
- অপারেশন প্যারামিটার, এবং
- প্যারামিটারların ইন্টারপ্রেটেশন প্যারেনথেসিসে।
>>> codeInString = 'a = 5\nb = 6\nsum = a + b \nprint("sum = ",sum)'
>>> codeObejct = compile(codeInString, 'sumstring', 'exec')
>>> dis.disassemble(codeObejct)
আউটপুট
1 0 LOAD_CONST 0 (5)
3 STORE_NAME 0 (a)
2 6 LOAD_CONST 1 (6)
9 STORE_NAME 1 (b)
3 12 LOAD_NAME 0 (a)
15 LOAD_NAME 1 (b)
18 BINARY_ADD
19 STORE_NAME 2 (sum)
4 22 LOAD_NAME 3 (print)
25 LOAD_CONST 2 ('sum =')
28 LOAD_NAME 2 (sum)
31 CALL_FUNCTION 2 (2 positional, 0 keyword pair)
34 POP_TOP
35 LOAD_CONST 3 (None)
38 RETURN_VALUE
get_instructions()
এই ফাংশন সরবরাহ করা ফাংশন, মেথড, সোর্স কোড স্ট্রিং বা কোড অবজেক্টের ইনস্ট্রাকশনগুলোর ওপর একটি ইটেরেটর রিটার্ন করে। ইটেরেটর সরবরাহ করা কোডের প্রতিটি অপারেশনের ডিটেইলস দেয় এমন Instruction নামক টাপলের একটি সিরিজ জেনারেট করে।
>>> it = dis.get_instructions(code)
>>> for i in it:
print(i)
Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=", line 2>, argrepr='", line 2>', offset=0, starts_line=2, is_jump_target=False)
Instruction(opname='LOAD_CONST', opcode=100, arg=1, argval='hello', argrepr="'hello'", offset=3, starts_line=None, is_jump_target=False)
Instruction(opname='MAKE_FUNCTION', opcode=132, arg=0, argval=0, argrepr='', offset=6, starts_line=None, is_jump_target=False)
Instruction(opname='STORE_NAME', opcode=90, arg=0, argval='hello', argrepr='hello', offset=9, starts_line=None, is_jump_target=False)
Instruction(opname='LOAD_CONST', opcode=100, arg=2, argval=None, argrepr='None', offset=12, starts_line=None, is_jump_target=False)
Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=15, starts_line=None, is_jump_target=False)
ফাংশন ইনফরমেশন টাপল-লাইক অবজেক্টের আকারে থাকে নিচের প্যারামিটারসহ:
| Instruction | বাইটকোড অপারেশনের ডিটেইলস |
|---|---|
| opcode | অপারেশনের জন্য न्यুমেরিক কোড, নিচে লিস্ট করা opcode ভ্যালু এবং Opcode কালেকশনে বাইটকোড ভ্যালুর সাথে মিলে যায়। |
| opname | অপারেশনের জন্য মানব পঠনযোগ্য নাম |
| arg | অপারেশনের জন্য न्यুমেরিক আর্গুমেন্ট (যদি থাকে), অন্যথায় None |
| argval | রেজলভড আর্গুমেন্ট ভ্যালু (যদি জানা থাকে), অন্যথায় arg-এর মতো |
| argrepr | অপারেশন আর্গুমেন্টের মানব পঠনযোগ্য বর্ণনা |
| offset | বাইটকোড সিকোয়েন্সের মধ্যে অপারেশনের শুরুতের ইনডেক্স |
| starts_line | এই opcode দ্বারা শুরু হওয়া লাইন (যদি থাকে), অন্যথায় None |
| is_jump_target | True যদি অন্যান্য কোড এখানে জাম্প করে, অন্যথায় False |