Customising py py

1,314 views

Published on

Making a Python capable binary by customising PyPy

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,314
On SlideShare
0
From Embeds
0
Number of Embeds
26
Actions
Shares
0
Downloads
28
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Customising py py

  1. 1. Customising PyPy <ul><li>Make your own rules </li></ul>
  2. 2. Custom Executable <ul><li>Subcommand paradigm </li></ul><ul><li>Run from anywhere </li></ul><ul><li>Executable name and location </li></ul><ul><li>used to resolve library path </li></ul>
  3. 3. Executable Module in PyPy <ul><li>pypy/module/ mymod </li></ul><ul><li>__init__.py </li></ul><ul><li>app_part1.py </li></ul><ul><li>interp_part2.py </li></ul><ul><li>test/test_part1.py </li></ul><ul><li>test/test_part2.py </li></ul>
  4. 4. Interface in __init__.py Exposes symbols to the &quot;user&quot; from pypy.interpreter.mixedmodule import MixedModule class Module(MixedModule): interpleveldefs = {} appleveldefs = { 'commands' : 'app_commands.COMMANDS', 'find_commands' : 'app_commands.find_commands', 'CommandError' : 'app_commands.CommandError', }
  5. 5. App Level Python Start with #NOT RPYTHON Consider your imports carefully And then write python like you normally will
  6. 6. Testing - App Level Hacking a module is pain . Roundtrip to build executable is >30 mins TDD wins Write unit tests to cover as much as you can. Time to write pypy/module/test/test_one.py
  7. 7. Testing - App Level Python Beware of imports and test setup mocks from pypy.conftest import gettestobjspace class AppTestCall(object): def setup_class(cls): # import mymod, sys space = cls.space = gettestobjspace(usemodules=('mymod','sys',)) # sys.app_name = &quot;pypy-c&quot; space.setattr(space.sys, space.wrap(&quot;app_name&quot;), space.wrap(&quot;pypy-c&quot;)) def test_mymod(): import mymod
  8. 8. Interface in __init__.py Exposes symbols to the &quot;user&quot; from pypy.interpreter.mixedmodule import MixedModule class Module(MixedModule): interpleveldefs = { 'find_module': 'interp_imp.find_module',} appleveldefs = {}
  9. 9. Interp Level Python RPython for Speed You can implement the internals of a module in RPython which will be compiled to binary. It is your responsibility to handle wrapping and unwrapping def find_module(space, w_name, w_path=None): name = space.str_w(w_name) if space.is_w(w_path, space.w_None): w_path = None return space.newtuple([w_fileobj, w_filename, w_import_info])
  10. 10. Testing - Interp Level Python Beware of imports and test setup mocks from pypy.conftest import gettestobjspace class AppTestImpModule: def setup_class(cls): cls.w_imp = cls.space.getbuiltinmodule('imp') def test_find_module(self): import os file, pathname, description = self.imp .find_module('StringIO')
  11. 11. Customising the Command Line pypy/translator/goal/ targetpypystandalone.py # manually imports app_main.py filename = os.path.join(this_dir, 'app_main.py') app = gateway.applevel(open(filename).read(), 'app_main.py', 'app_main') Keep changes to a minimum as you have to build a binary to test them!
  12. 12. Customising the Command Line pypy/translator/goal/ app_main.py def run_command_line(.. settings .., cmd=None, **ignored): mainmodule = type(sys)('__main__') import site if run_command: ... elif run_module: ... elif run_stdin: # piped or interactive shell ... else: import installation if not sys.argv[0].endswith(&quot;.py&quot;) and sys.argv[0].find(&quot;/&quot;) == -1: argv = sys.argv[:] argv.insert(0,&quot;&quot;) installation.commands.execute(argv) success = True else: ...
  13. 13. Main Sequence (I’m not too sure about this) nanos module app_main.py ... full os module ... run_command_line(..) site.py sitecustomize.py main module/script/interpreter loading
  14. 14. Command Line Parsers app_main.py: def parse_command_line(argv) optparse.OptionParser argsparse.ArgsParser
  15. 15. My Objective Objective Self updating Filesystem monitoring Network Peer Daemon/Service capable Configurable command line
  16. 16. Building PyPy Add to pypy/config/pypyoptions.py (or) Use --with- mymod cd pypy/translator/goal translate.py --with- mymod targetpypystandalone.py
  17. 17. Henrik Vendelbo <ul><li>[email_address] </li></ul><ul><li>http://github.com/thepian/pypy </li></ul>

×