Python 如何執行
Upcoming SlideShare
Loading in...5
×

Like this? Share it with your network

Share

Python 如何執行

  • 840 views
Uploaded on

How does the python run code?

How does the python run code?

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
840
On Slideshare
683
From Embeds
157
Number of Embeds
3

Actions

Shares
Downloads
4
Comments
0
Likes
1

Embeds 157

http://www.blackwhite.tw 126
http://127.0.0.1 29
http://localhost 2

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Python 如何執行
  • 2. 演講經歷 ● 2013/04 在 taipei.py 演講關於 pdb 的實作。 相關投影片:http://www.slideshare. net/ya790026/recoverpdb ● 2013/05 在 pyconf.tw 演將 CPython 原始碼 解析。相關投影片:http://www.slideshare. net/ya790026/c-python-23247730。
  • 3. Python 如何被執行 1. 將程式碼編譯成 code object 2. 執行 code object 的 byte code,變數等相關 資訊放在 frame object 裡。 3. 一個 frame 對應到一個 code object
  • 4. code object ● co_name gives the function name; ● co_varnames is a tuple containing the names of the local variables (starting with the argument names); ● co_consts is a tuple containing the literals used by the bytecode;
  • 5. frame object ● f_back is to the previous stack frame (towards the caller), or None if this is the bottom stack frame; ● f_code is the code object being executed in this frame; ● f_locals is the dictionary used to look up local variables; ● f_globals is used for global variables;
  • 6. Stack TOS3 TOS4 TOS1 TOS BUTTON TOP
  • 7. op code ● opcode 定義在 Include/opcode.h ● opcode 的長度為一個 byte ● opcode 依照參數種類可分為三種 ○ 無參數,如 NOP ○ 參數來源來自 stack,如BINARY_ADD ○ 參數來源來自 opcdoe 後兩個 bype,如 LOAD_CONST ● 如果一個函數使用超過 2^16 個常數? ○ 當 opcdoe 的參數來源來自opcdoe 後兩個 bype,而 該參數無法以 2 byte 來表示。則在該 opcdoe 前插入 EXTENDED_ARG。該 opcode 的參數為 (EXTENDED_ARG 的參數 <<16 | 該opcdoe 的參數)
  • 8. Python Bytecode Instructions ● ● ● NOP() Do nothing code. Used as a placeholder by the bytecode optimizer. POP_TOP() Removes the top-of-stack (TOS) item. ROT_TWO() Swaps the two top-most stack items.
  • 9. Python Bytecode Instructions Unary Operations take the top of the stack, apply the operation, and push the result back on the stack ● ● ● UNARY_POSITIVE() Implements TOS = +TOS. UNARY_NEGATIVE() Implements TOS = -TOS. UNARY_NOT() Implements TOS = not TOS.
  • 10. Python Bytecode Instructions Binary operations remove the top of the stack (TOS) and the second top-most stack item (TOS1) from the stack. They perform the operation, and put the result back on the stack. BINARY_POWER() Implements TOS = TOS1 ** TOS. BINARY_MULTIPLY() Implements TOS = TOS1 * TOS.
  • 11. Python Bytecode Instructions Slice assignment needs even an additional parameter. As any statement, they put nothing on the stack. STORE_SLICE+0() Implements TOS[:] = TOS1. STORE_SLICE+1() Implements TOS1[TOS:] = TOS2.
  • 12. Miscellaneous opcodes. LOAD_FAST(var_num) Pushes a reference to the local co_varnames[var_num] onto the stack. LOAD_CONST(consti) Pushes co_consts[consti] onto the stack. BUILD_LIST(count) Works as BUILD_TUPLE, but creates a list.
  • 13. def test(data, data1): a=5 print 'hello', print data return 1 import dis print dis.dis(test)
  • 14. co_consts: (None, 5, 'hello', 1) co_varnames: ('data', 'data1', 'a')
  • 15. 2 3 4 5 0 LOAD_CONST 3 STORE_FAST 6 LOAD_CONST 9 PRINT_ITEM 10 LOAD_FAST 13 PRINT_ITEM 14 PRINT_NEWLINE 15 LOAD_CONST 18 RETURN_VALUE 1 (5) 2 (a) 2 ('hello') 0 (data) 3 (1)
  • 16. def test(): print 'hello', print 'hello', import dis print dis.dis(test)
  • 17. 2 0 LOAD_CONST 3 PRINT_ITEM 1 ('hello') 3 4 LOAD_CONST 7 PRINT_ITEM 8 LOAD_CONST 11 RETURN_VALUE 1 ('hello') 0 (None)
  • 18. def printf1(name, **kwargs): print kwargs[name] code = printf1.func_code import dis print dis.dis(code)
  • 19. 8 0 LOAD_FAST 3 LOAD_FAST 6 BINARY_SUBSCR 7 PRINT_ITEM 8 PRINT_NEWLINE 9 LOAD_CONST 12 RETURN_VALUE 1 (kwargs) 0 (name) 0 (None)
  • 20. def test1(): a=1 b=2 c=a+b d=a*b def test2(): a, b = 1, 2 c, d = a + b, a * b
  • 21. 5 0 (a) 6 LOAD_CONST 2 (2) 9 STORE_FAST 1 (b) 12 LOAD_FAST 0 (a) 15 LOAD_FAST 7 1 (1) 3 STORE_FAST 6 0 LOAD_CONST 1 (b) 18 BINARY_ADD 19 STORE_FAST 8 2 (c) 22 LOAD_FAST 0 (a) 25 LOAD_FAST 1 (b) 28 BINARY_MULTIPLY 29 STORE_FAST 3 (d) 32 LOAD_CONST 0 (None) 35 RETURN_VALUE
  • 22. 11 0 LOAD_CONST 3 ((1, 2)) 3 UNPACK_SEQUENCE 2 6 STORE_FAST 9 STORE_FAST 12 0 (a) 1 (b) 12 LOAD_FAST 0 (a) 15 LOAD_FAST 1 (b) 18 BINARY_ADD 19 LOAD_FAST 0 (a) 22 LOAD_FAST 1 (b) 25 BINARY_MULTIPLY 26 ROT_TWO 27 STORE_FAST 2 (c) 30 STORE_FAST 3 (d) 33 LOAD_CONST 0 (None) 36 RETURN_VALUE
  • 23. PyEval_EvalFrameEx ● Python/ceval.c ● 負責解讀 opcdoe 並呼叫對應的動作
  • 24. BINARY_ADD 1. 都是整數的話,直接相加 2. 都是字串的話,呼叫 string_concatenate 3. 不滿足上面條件,則呼叫 PyNumber_Add ( in Object/typeobject.c)。他會去呼叫 __add__ 4. 從 stack 取得兩個參數,放回一個結果
  • 25. 結論 1. 比較長的程式碼,op code 未必比較長 2. python 是一個程式碼大量重用的程式
  • 26. Olis ● http://www.olis.com.tw Vira: 一切渴望再度成為可能 http://viraapp.co 再看看看! - 免費簡易型防偷窺APP
  • 27. 謝謝大家 ● ● ● ● ya790206@gmail.com www.blackwhite.tw web.blackwhite.tw blog.blackwhite.tw