Mercurial > vim
comparison runtime/doc/if_pyth.txt @ 4833:70b1178dec79 v7.3.1163
updated for version 7.3.1163
Problem: Not easy to load Python modules.
Solution: Search "python2", "python3" and "pythonx" directories in
'runtimepath' for Python modules. (ZyX)
author | Bram Moolenaar <bram@vim.org> |
---|---|
date | Mon, 10 Jun 2013 21:27:29 +0200 |
parents | 2b11ac90d9e9 |
children | 96e154e825a7 |
comparison
equal
deleted
inserted
replaced
4832:54430b55ceff | 4833:70b1178dec79 |
---|---|
177 or |Dictionary|) or call (|Funcref|) vim objects. | 177 or |Dictionary|) or call (|Funcref|) vim objects. |
178 | 178 |
179 vim.strwidth(str) *python-strwidth* | 179 vim.strwidth(str) *python-strwidth* |
180 Like |strwidth()|: returns number of display cells str occupies, tab | 180 Like |strwidth()|: returns number of display cells str occupies, tab |
181 is counted as one cell. | 181 is counted as one cell. |
182 | |
183 vim.foreach_rtp(callable) *python-foreach_rtp* | |
184 Call the given callable for each path in 'runtimepath' until either | |
185 callable returns something but None, the exception is raised or there | |
186 are no longer paths. If stopped in case callable returned non-None, | |
187 vim.foreach_rtp function returns the value returned by callable. | |
182 | 188 |
183 vim.chdir(*args, **kwargs) *python-chdir* | 189 vim.chdir(*args, **kwargs) *python-chdir* |
184 vim.fchdir(*args, **kwargs) *python-fchdir* | 190 vim.fchdir(*args, **kwargs) *python-fchdir* |
185 Run os.chdir or os.fchdir, then all appropriate vim stuff. | 191 Run os.chdir or os.fchdir, then all appropriate vim stuff. |
186 Note: you should not use these functions directly, use os.chdir and | 192 Note: you should not use these functions directly, use os.chdir and |
297 | 303 |
298 *python-input* | 304 *python-input* |
299 Input (via sys.stdin, including input() and raw_input()) is not | 305 Input (via sys.stdin, including input() and raw_input()) is not |
300 supported, and may cause the program to crash. This should probably be | 306 supported, and may cause the program to crash. This should probably be |
301 fixed. | 307 fixed. |
308 | |
309 *python2-directory* *python3-directory* *pythonx-directory* | |
310 Python 'runtimepath' handling *python-special-path* | |
311 | |
312 In python vim.VIM_SPECIAL_PATH special directory is used as a replacement for | |
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 | |
315 {rtp}/python2 (or python3) and {rtp}/pythonx (for both python versions) for | |
316 each {rtp} found in 'runtimepath'. | |
317 | |
318 Implementation for python 2 is the following: usual importing code with empty | |
319 lists in place of sys.path_hooks and sys.meta_path. Code is similar to the | |
320 below, but written in C: > | |
321 | |
322 # Assuming vim variable is already accessible and is set to the current | |
323 # module | |
324 import sys | |
325 | |
326 def find_module(fullname): | |
327 return vim | |
328 | |
329 def load_module(fullname): | |
330 # see vim._get_paths below | |
331 new_path = _get_paths() | |
332 | |
333 try: old_path = sys.path | |
334 except: pass | |
335 try: old_meta_path = sys.meta_path | |
336 except: pass | |
337 try: old_path_hooks = sys.path_hooks | |
338 except: pass | |
339 | |
340 sys.meta_path = [] | |
341 sys.path_hooks = sys.meta_path | |
342 sys.path = new_path | |
343 | |
344 try: | |
345 exec ('import ' + fullname + ' as m') # No actual exec in C code | |
346 return m | |
347 finally: | |
348 e = None | |
349 try: sys.path = old_path | |
350 except Exception as e: pass | |
351 try: sys.meta_path = old_meta_path | |
352 except Exception as e: pass | |
353 try: sys.path_hooks = old_path_hooks | |
354 except Exception as e: pass | |
355 if e: | |
356 raise e | |
357 | |
358 def path_hook(d): | |
359 if d == VIM_SPECIAL_PATH: | |
360 return vim | |
361 raise ImportError | |
362 | |
363 sys.path_hooks.append(path_hook) | |
364 | |
365 Implementation for python 3 is cleaner: code is similar to the following, but, | |
366 again, written in C: > | |
367 | |
368 from importlib.machinery import PathFinder | |
369 import sys | |
370 | |
371 class Finder(PathFinder): | |
372 @classmethod | |
373 def find_module(cls, fullname): | |
374 # see vim._get_paths below | |
375 new_path = _get_paths() | |
376 | |
377 # super().find_module is also a class method | |
378 # super() is not used because this variant is easier to implement | |
379 # in C | |
380 return PathFinder.find_module(fullname, new_path) | |
381 | |
382 def path_hook(path): | |
383 if path == VIM_SPECIAL_PATH: | |
384 return Finder | |
385 raise ImportError | |
386 | |
387 sys.path_hooks.append(path_hook) | |
388 | |
389 vim.VIM_SPECIAL_PATH *python-VIM_SPECIAL_PATH* | |
390 String constant used in conjunction with vim path hook. If path hook | |
391 installed by vim is requested to handle anything but path equal to | |
392 vim.VIM_SPECIAL_PATH constant it raises ImportError. In the only other | |
393 case it uses special loader. | |
394 | |
395 Note: you must not use value of this constant directly, always use | |
396 vim.VIM_SPECIAL_PATH object. | |
397 | |
398 vim.load_module(name) *python-load_module* | |
399 vim.find_module(...) *python-find_module* | |
400 vim.path_hook(path) *python-path_hook* | |
401 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 | |
403 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 | |
405 versions. In fact, load_module and find_module methods do not exists | |
406 in python3. | |
407 | |
408 vim._get_paths *python-_get_paths* | |
409 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 | |
411 versions, but can use it for debugging. | |
412 | |
413 It returns a list of {rtp}/python2 (or {rtp}/python3) and | |
414 {rtp}/pythonx directories for each {rtp} in 'runtimepath'. | |
302 | 415 |
303 ============================================================================== | 416 ============================================================================== |
304 3. Buffer objects *python-buffer* | 417 3. Buffer objects *python-buffer* |
305 | 418 |
306 Buffer objects represent vim buffers. You can obtain them in a number of ways: | 419 Buffer objects represent vim buffers. You can obtain them in a number of ways: |