comparison runtime/doc/if_pyth.txt @ 4855:52850ef928f8 v7.3.1174

updated for version 7.3.1174 Problem: Python 2 and 3 use different ways to load modules. Solution: Use the same method. (ZyX)
author Bram Moolenaar <bram@vim.org>
date Wed, 12 Jun 2013 14:41:04 +0200
parents 96e154e825a7
children c458ff35497e
comparison
equal deleted inserted replaced
4854:4be96f67ba74 4855:52850ef928f8
313 the list of paths found in 'runtimepath': with this directory in sys.path and 313 the list of paths found in 'runtimepath': with this directory in sys.path and
314 vim.path_hooks in sys.path_hooks python will try to load module from 314 vim.path_hooks in sys.path_hooks python will try to load module from
315 {rtp}/python2 (or python3) and {rtp}/pythonx (for both python versions) for 315 {rtp}/python2 (or python3) and {rtp}/pythonx (for both python versions) for
316 each {rtp} found in 'runtimepath'. 316 each {rtp} found in 'runtimepath'.
317 317
318 Implementation for python 2 is similar to the following, but written in C: > 318 Implementation is similar to the following, but written in C: >
319 319
320 from imp import find_module, load_module 320 from imp import find_module, load_module
321 import vim 321 import vim
322 import sys 322 import sys
323 323
342 342
343 # It uses vim module itself in place of VimPathFinder class: it does not 343 # It uses vim module itself in place of VimPathFinder class: it does not
344 # matter for python which object has find_module function attached to as 344 # matter for python which object has find_module function attached to as
345 # an attribute. 345 # an attribute.
346 class VimPathFinder(object): 346 class VimPathFinder(object):
347 @classmethod
347 def find_module(cls, fullname, path=None): 348 def find_module(cls, fullname, path=None):
348 try: 349 try:
349 return VimModuleLoader(_find_module(fullname, fullname, path or vim._get_paths())) 350 return VimModuleLoader(_find_module(fullname, fullname, path or vim._get_paths()))
350 except ImportError: 351 except ImportError:
351 return None 352 return None
352 find_module = classmethod(find_module) 353
353 354 @classmethod
354 def load_module(cls, fullname, path=None): 355 def load_module(cls, fullname, path=None):
355 return _find_module(fullname, fullname, path or vim._get_paths()) 356 return _find_module(fullname, fullname, path or vim._get_paths())
356 load_module = classmethod(load_module)
357 357
358 def hook(path): 358 def hook(path):
359 if path == vim.VIM_SPECIAL_PATH: 359 if path == vim.VIM_SPECIAL_PATH:
360 return VimPathFinder 360 return VimPathFinder
361 else: 361 else:
362 raise ImportError 362 raise ImportError
363 363
364 sys.path_hooks.append(hook) 364 sys.path_hooks.append(hook)
365
366 Implementation for python 3 is cleaner: code is similar to the following, but,
367 again, written in C: >
368
369 from importlib.machinery import PathFinder
370 import sys
371
372 class Finder(PathFinder):
373 @classmethod
374 def find_module(cls, fullname):
375 # see vim._get_paths below
376 new_path = _get_paths()
377
378 # super().find_module is also a class method
379 # super() is not used because this variant is easier to implement
380 # in C
381 return PathFinder.find_module(fullname, new_path)
382
383 def path_hook(path):
384 if path == VIM_SPECIAL_PATH:
385 return Finder
386 raise ImportError
387
388 sys.path_hooks.append(path_hook)
389 365
390 vim.VIM_SPECIAL_PATH *python-VIM_SPECIAL_PATH* 366 vim.VIM_SPECIAL_PATH *python-VIM_SPECIAL_PATH*
391 String constant used in conjunction with vim path hook. If path hook 367 String constant used in conjunction with vim path hook. If path hook
392 installed by vim is requested to handle anything but path equal to 368 installed by vim is requested to handle anything but path equal to
393 vim.VIM_SPECIAL_PATH constant it raises ImportError. In the only other 369 vim.VIM_SPECIAL_PATH constant it raises ImportError. In the only other
400 vim.path_hook(path) *python-path_hook* 376 vim.path_hook(path) *python-path_hook*
401 Methods or objects used to implement path loading as described above. 377 Methods or objects used to implement path loading as described above.
402 You should not be using any of these directly except for vim.path_hook 378 You should not be using any of these directly except for vim.path_hook
403 in case you need to do something with sys.meta_path. It is not 379 in case you need to do something with sys.meta_path. It is not
404 guaranteed that any of the objects will exist in the future vim 380 guaranteed that any of the objects will exist in the future vim
405 versions. In fact, find_module methods do not exists 381 versions.
406 in python3.
407 382
408 vim._get_paths *python-_get_paths* 383 vim._get_paths *python-_get_paths*
409 Methods returning a list of paths which will be searched for by path 384 Methods returning a list of paths which will be searched for by path
410 hook. You should not rely on this method being present in future 385 hook. You should not rely on this method being present in future
411 versions, but can use it for debugging. 386 versions, but can use it for debugging.