Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Type Python, Press Enter, What Happens?

Type Python, Press Enter, What Happens?

This talk discusses how the Python interpreter starts, from the perspective of the operating system (OS). Together, we will see the ins & outs of processes: fork(), exec(), stdin, and stdout. It focuses on OS concepts and requires no background knowledge, using analogies to Python data structures. (The talk does not discuss Python’s own initialization, such as site.py or global variables.)

Philip James

April 11, 2015
Tweet

More Decks by Philip James

Other Decks in Programming

Transcript

  1. paulproteus@vellum:~ $ python Python 2.7.6 (default, Apr 11 2014, 22:59:56)

    [GCC 4.8.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>>
  2. paulproteus@vellum:~ $ hexdump -C /usr/bin/python 00000000 7f 45 4c 46

    02 01 01 00 00 00 00 00 00 00 00 00 |.ELF............| 00000010 02 00 3e 00 01 00 00 00 55 49 57 00 00 00 00 00 |..>.....UIW.....| 00000020 40 00 00 00 00 00 00 00 08 15 33 00 00 00 00 00 |@.........3.....| 00000030 00 00 00 00 40 00 38 00 09 00 40 00 1c 00 1b 00 |[email protected]...@.....| 00000040 06 00 00 00 05 00 00 00 40 00 00 00 00 00 00 00 |........@.......| 00000050 40 00 40 00 00 00 00 00 40 00 40 00 00 00 00 00 |@.@.....@.@.....| 00000060 f8 01 00 00 00 00 00 00 f8 01 00 00 00 00 00 00 |................| 00000070 08 00 00 00 00 00 00 00 03 00 00 00 04 00 00 00 |................| 00000080 38 02 00 00 00 00 00 00 38 02 40 00 00 00 00 00 |8.......8.@.....| 00000090 38 02 40 00 00 00 00 00 1c 00 00 00 00 00 00 00 |8.@.............|
  3. # installing zipimport hook import zipimport # builtin # installed

    zipimport hook # /usr/lib/python2.7/site.pyc matches /usr/lib/python2.7/site.py import site # precompiled from /usr/ lib/python2.7/site.pyc # /usr/lib/python2.7/os.pyc matches /usr/lib/python2.7/os.py paulproteus@vellum:~ $ python -v
  4. bash pid: 1221 fork() python pid: 1222 argv = [‘python’,

    ‘-v’] exec( ‘/usr/bin/python’, [‘python’, ‘-v’] )
  5. Python 2.7.6 (default, Apr 11 2014, 22:59:56) [GCC 4.8.2] on

    linux2 Type "help", "copyright", "credits" or "license" for more information. >>>
  6. paulproteus@vellum:~ $ strace python [...] read(0, “2”, 1) read(0, “+”,

    1) read(0, “2”, 1) read(0, “\n”, 1) 2 + 2 Enter
  7. while True: line = ‘’ chr = sys.stdin.read(1) line +=

    chr if chr == ‘\n’: print eval(line)
  8. while True: line = ‘’ chr = sys.stdin.read(1) line +=

    chr if chr == ‘\n’: print eval(line)
  9. bash pid: 1221 python pid: 1222 0: /dev/pts/3 1: /dev/pts/3

    2: /dev/pts/3 0: /dev/pts/3 1: /dev/pts/3 2: /dev/pts/3
  10. bash pid: 1221 python pid: 1222 0: /dev/pts/3 1: /dev/pts/3

    2: /dev/pts/3 0: /dev/pts/3 1: /dev/pts/3 2: /dev/pts/3 foreground
  11. >>>

  12. python pid: 1222 argv = [‘python’] sigint sig pipe 0:

    /dev/pts/3 1: /dev/pts/3 2: /dev/pts/3 bash pid: 1221
  13. Talks to explore more • Systems Programming as a Swiss

    Army Knife (Julia Evans, Room 517c, Saturday @1050am)* • Exploring is never boring: understanding CPython without reading the code (Allison Kaptur, Room 710a, Saturday @ 245pm) • Where in your RAM is "python san_diego.py”? (Ying Li, Room 517c, Sunday @ 110pm) • Getting comfortable with web security: A hands-on session (Asheesh Laroia, Jacky Chang, Nicole Zuckerman, Room 512cg, Thursday @ 9am)* • Finding Spammers & Scammers through Rate Tracking with Python & Redis (Mica Swyers, Jay Chan, Room 511, Sunday @ 150pm) Talks by friends *time machine may be required