01-arcadia.patch 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. --- contrib/python/ipython/py2/IPython/core/completerlib.py (index)
  2. +++ contrib/python/ipython/py2/IPython/core/completerlib.py (working tree)
  3. @@ -19,6 +19,7 @@ from __future__ import print_function
  4. # Stdlib imports
  5. import glob
  6. import inspect
  7. +import itertools
  8. import os
  9. import re
  10. import sys
  11. @@ -44,6 +45,8 @@ from IPython.utils.py3compat import string_types
  12. # FIXME: this should be pulled in with the right call via the component system
  13. from IPython import get_ipython
  14. +from __res import importer
  15. +
  16. #-----------------------------------------------------------------------------
  17. # Globals and constants
  18. #-----------------------------------------------------------------------------
  19. @@ -68,6 +71,51 @@ magic_run_re = re.compile(r'.*(\.ipy|\.ipynb|\.py[w]?)$')
  20. # Local utilities
  21. #-----------------------------------------------------------------------------
  22. +arcadia_rootmodules_cache = None
  23. +arcadia_modules_cache = None
  24. +
  25. +
  26. +def arcadia_init_cache():
  27. + global arcadia_rootmodules_cache, arcadia_modules_cache
  28. + arcadia_rootmodules_cache = set()
  29. + arcadia_modules_cache = {}
  30. +
  31. + all_modules = itertools.chain(
  32. + sys.builtin_module_names,
  33. + importer.memory
  34. + )
  35. +
  36. + for name in all_modules:
  37. + path = name.split('.')
  38. + arcadia_rootmodules_cache.add(path[0])
  39. +
  40. + prefix = path[0]
  41. + for element in path[1:]:
  42. + if element == '__init__':
  43. + continue
  44. +
  45. + arcadia_modules_cache.setdefault(prefix, set()).add(element)
  46. + prefix += '.' + element
  47. +
  48. + arcadia_rootmodules_cache = sorted(arcadia_rootmodules_cache)
  49. + arcadia_modules_cache = {k: sorted(v) for k, v in arcadia_modules_cache.items()}
  50. +
  51. +
  52. +def arcadia_module_list(mod):
  53. + if arcadia_modules_cache is None:
  54. + arcadia_init_cache()
  55. +
  56. + return arcadia_modules_cache.get(mod, ())
  57. +
  58. +
  59. +def arcadia_get_root_modules():
  60. + if arcadia_rootmodules_cache is None:
  61. + arcadia_init_cache()
  62. +
  63. + return arcadia_rootmodules_cache
  64. +
  65. +
  66. +
  67. def module_list(path):
  68. """
  69. Return the list containing the names of the modules available in the given
  70. @@ -168,7 +216,8 @@ def try_import(mod, only_modules=False):
  71. for module in mods[1:]:
  72. m = getattr(m, module)
  73. - m_is_init = hasattr(m, '__file__') and '__init__' in m.__file__
  74. + filename = getattr(m, '__file__', '')
  75. + m_is_init = '__init__' in (filename or '') or filename == mod
  76. completions = []
  77. if (not hasattr(m, '__file__')) or (not only_modules) or m_is_init:
  78. @@ -177,10 +226,10 @@ def try_import(mod, only_modules=False):
  79. completions.extend(getattr(m, '__all__', []))
  80. if m_is_init:
  81. - completions.extend(module_list(os.path.dirname(m.__file__)))
  82. + completions.extend(arcadia_module_list(mod))
  83. completions = {c for c in completions if isinstance(c, string_types)}
  84. completions.discard('__init__')
  85. - return list(completions)
  86. + return sorted(completions)
  87. #-----------------------------------------------------------------------------
  88. @@ -229,10 +278,10 @@ def module_completion(line):
  89. # 'from xy<tab>' or 'import xy<tab>'
  90. if nwords < 3 and (words[0] in {'%aimport', 'import', 'from'}) :
  91. if nwords == 1:
  92. - return get_root_modules()
  93. + return arcadia_get_root_modules()
  94. mod = words[1].split('.')
  95. if len(mod) < 2:
  96. - return get_root_modules()
  97. + return arcadia_get_root_modules()
  98. completion_list = try_import('.'.join(mod[:-1]), True)
  99. return ['.'.join(mod[:-1] + [el]) for el in completion_list]
  100. --- contrib/python/ipython/py2/IPython/core/extensions.py (index)
  101. +++ contrib/python/ipython/py2/IPython/core/extensions.py (working tree)
  102. @@ -75,11 +75,11 @@ class ExtensionManager(Configurable):
  103. if module_str in self.loaded:
  104. return "already loaded"
  105. - from IPython.utils.syspathcontext import prepended_to_syspath
  106. -
  107. with self.shell.builtin_trap:
  108. if module_str not in sys.modules:
  109. - with prepended_to_syspath(self.ipython_extension_dir):
  110. + try:
  111. + sys.modules[module_str] = __import__('IPython.extensions.' + module_str)
  112. + except ImportError:
  113. __import__(module_str)
  114. mod = sys.modules[module_str]
  115. if self._call_load_ipython_extension(mod):
  116. --- contrib/python/ipython/py2/IPython/core/profiledir.py (index)
  117. +++ contrib/python/ipython/py2/IPython/core/profiledir.py (working tree)
  118. @@ -112,13 +112,11 @@ class ProfileDir(LoggingConfigurable):
  119. self._mkdir(self.startup_dir)
  120. readme = os.path.join(self.startup_dir, 'README')
  121. - src = os.path.join(get_ipython_package_dir(), u'core', u'profile', u'README_STARTUP')
  122. - if not os.path.exists(src):
  123. - self.log.warning("Could not copy README_STARTUP to startup dir. Source file %s does not exist.", src)
  124. -
  125. - if os.path.exists(src) and not os.path.exists(readme):
  126. - shutil.copy(src, readme)
  127. + if not os.path.exists(readme):
  128. + import pkgutil
  129. + with open(readme, 'wb') as f:
  130. + f.write(pkgutil.get_data(__name__, 'profile/README_STARTUP'))
  131. @observe('security_dir')
  132. def check_security_dir(self, change=None):