Home Explore Blog CI



neovim

5th chunk of `runtime/doc/if_pyth.txt`
12fe2b2c534b2c0fd66bcfd424d29e790d22220e2e94cd7a0000000100000fa6
      self.module = module

        def load_module(self, fullname, path=None):
            return self.module

    def _find_module(fullname, oldtail, path):
        idx = oldtail.find('.')
        if idx > 0:
            name = oldtail[:idx]
            tail = oldtail[idx+1:]
            fmr = find_module(name, path)
            module = load_module(fullname[:-len(oldtail)] + name, *fmr)
            return _find_module(fullname, tail, module.__path__)
        else:
            fmr = find_module(fullname, path)
            return load_module(fullname, *fmr)

    # It uses vim module itself in place of VimPathFinder class: it does not
    # matter for python which object has find_module function attached to as
    # an attribute.
    class VimPathFinder(object):
        @classmethod
        def find_module(cls, fullname, path=None):
            try:
                return VimModuleLoader(_find_module(fullname, fullname, path or vim._get_paths()))
            except ImportError:
                return None

        @classmethod
        def load_module(cls, fullname, path=None):
            return _find_module(fullname, fullname, path or vim._get_paths())

    def hook(path):
        if path == vim.VIM_SPECIAL_PATH:
            return VimPathFinder
        else:
            raise ImportError

    sys.path_hooks.append(hook)

vim.VIM_SPECIAL_PATH					*python-VIM_SPECIAL_PATH*
	String constant used in conjunction with vim path hook. If path hook
	installed by vim is requested to handle anything but path equal to
	vim.VIM_SPECIAL_PATH constant it raises ImportError. In the only other
	case it uses special loader.

	Note: you must not use value of this constant directly, always use
	      vim.VIM_SPECIAL_PATH object.

vim.find_module(...)					*python-find_module*
vim.path_hook(path)					*python-path_hook*
	Methods or objects used to implement path loading as described above.
	You should not be using any of these directly except for vim.path_hook
	in case you need to do something with sys.meta_path. It is not
	guaranteed that any of the objects will exist in the future vim
	versions.

vim._get_paths						*python-_get_paths*
	Methods returning a list of paths which will be searched for by path
	hook. You should not rely on this method being present in future
	versions, but can use it for debugging.

	It returns a list of {rtp}/python3 and {rtp}/pythonx
	directories for each {rtp} in 'runtimepath'.

==============================================================================
Buffer objects						*python-buffer*

Buffer objects represent vim buffers.  You can obtain them in a number of ways:
	- via vim.current.buffer (|python-current|)
	- from indexing vim.buffers (|python-buffers|)
	- from the "buffer" attribute of a window (|python-window|)

Buffer objects have two read-only attributes - name - the full file name for
the buffer, and number - the buffer number.  They also have three methods
(append, mark, and range; see below).

You can also treat buffer objects as sequence objects.  In this context, they
act as if they were lists (yes, they are mutable) of strings, with each
element being a line of the buffer.  All of the usual sequence operations,
including indexing, index assignment, slicing and slice assignment, work as
you would expect.  Note that the result of indexing (slicing) a buffer is a
string (list of strings).  This has one unusual consequence - b[:] is different
from b.  In particular, "b[:] = None" deletes the whole of the buffer, whereas
"b = None" merely updates the variable b, with no effect on the buffer.

Buffer indexes start at zero, as is normal in Python.  This differs from vim
line numbers, which start from 1.  This is particularly relevant when dealing
with marks (see below) which use vim line numbers.

The buffer object attributes are:
	b.vars		Dictionary-like object used to access
			|buffer-variable|s.
	b.options	Mapping object (supports item getting, setting and
			deleting) that provides access to buffer-local

Title: NVim Python Interface: Path Hook Implementation and Buffer Objects
Summary
This section provides the code implementation for handling Python module loading from Vim's 'runtimepath'. It outlines the `VimModuleLoader`, `VimPathFinder`, and `hook` functions that enable the system to find and load modules from the `python3` and `pythonx` directories within each runtimepath entry. The section then transitions to describing Buffer objects in the NVim Python interface, detailing how to obtain them, their attributes (name, number, vars, options), and their behavior as mutable sequences of strings representing buffer lines.