Import

1,089 views

Published on

Overview of shiny new Python 3.3 import system.

1 Comment
0 Likes
Statistics
Notes
  • Be the first to like this

No Downloads
Views
Total views
1,089
On SlideShare
0
From Embeds
0
Number of Embeds
6
Actions
Shares
0
Downloads
20
Comments
1
Likes
0
Embeds 0
No embeds

No notes for slide

Import

  1. 1. Import Andrew Svetlovandrew.svetlov@gmail.com asvetlov.blogspot.com
  2. 2. Basics● Import statement● __import__ builtin● sys.path● WTF???
  3. 3. Import statement● import a.b.c● from a.b import c● from a.b import name● from ..b import c● from __future__ import absolute_import+- a +- b <== current +- c
  4. 4. __import__● __import__(name:str, globals:dict=None, locals:dict=None, fromlist:list=(), level:int=0) -> Module● Returns module or toplevel package depending from fromlist.
  5. 5. Import lockdef f(): from twisted.internet import reactor reactor.callLater(...)● Slow● Multithreading → Deadlock
  6. 6. Modules & Packages● __name__: str● __doc__: str● __file__: str● __path__: [str]● __package__: str● __cached__: str● __loader__: Loader
  7. 7. ABCMetaPathFinderMetaPathFinder LoaderPathEntryFinderPathEntryFinder ResourceLoader InspectLoader FileLoader ExecutionLoader SourceLoader
  8. 8. sys.modules● keyword: <module keyword from .../Lib/keyword.py>● unittest.signals: <module unittest.signals from …/Lib/unittest/signals.py>● _frozen_importlib: <module _frozen_importlib from <frozen>>● sys: <module sys (built-in)>● _heapq: <module _heapq from .../lib.linux-x86_64-3.3- pydebug/_heapq.cpython-33dm.so>
  9. 9. Finder● MetaPathFinder – find_module(fullname: str, path: str) -> Loader – invalidate_caches()● PathEntryFinder – find_loader(fullname: str) -> (Loader, str) – invalidate_caches()
  10. 10. sys.meta_path● [Finder()] – BuiltinImporter – FrozenImporter – PathFinder
  11. 11. sys.path_hooks● [callable(path:str) -> Finder] – <class zipimport.zipimporter> – <function PathEntryFinder path_hook>● sys.path: [str]● sys.path_importer_cache: {str:Finder}
  12. 12. ABCMetaPathFinderMetaPathFinder Loader LoaderPathEntryFinderPathEntryFinder ResourceLoader ResourceLoader InspectLoader InspectLoader FileLoader FileLoader ExecutionLoader ExecutionLoader SourceLoader SourceLoader
  13. 13. Loader● Base loader● load_module(fullname:str) -> Module● module_repr(module:Module) -> str
  14. 14. ResourceLoader(Loader)● Can return data from backend storage● load_module● get_data(path: str) -> bytes>>> folder = os.path.dirname(__file__)>>> resname = os.path.join(folder,image.png)>>> __loader__.get_data(resname)… <img bytes>
  15. 15. InspectLoader(Loader)● Introspection support● load_module● is_package(fullname:str) -> bool● get_code(fullname:str) -> CodeObject● get_source(fullname:str) -> str
  16. 16. ExecutionLoader(InspectLoader)● Support for execution modules as scripts● load_module, is_package, get_code, get_source● get_filename(fullname:str) -> str – __file__ – Python -m package.module
  17. 17. FileLoader(ResourceLoader, ExecutionLoader)● Just to be. Sourceless loader● load_module, is_package, get_code, get_data, get_source, get_filename
  18. 18. SourceLoader(ResourceLoader, ExecutionLoader)● load_module, is_package, get_code, get_data, get_source, get_filename● path_mtime(path:str) -> int● path_stats(path:str) -> dict – mtime:int – size:int [optional]● set_data(path:str, data:bytes)
  19. 19. importlib● find_loader(name:str, path:str=None) -> Loader● import_module(name:str, package:str=None) -> Module
  20. 20. Bootstrap● site.py● venv● user sitepackages – ~/.local/lib/python3.3/site-packages● site-packages● .pth files● sitecustomize.py● usersite.py
  21. 21. Shutdown● For every module in sys.modules set all global attrs to None● Delete modules
  22. 22. Вопросы? Andrew Svetlovandrew.svetlov@gmail.com asvetlov.blogspot.com

×