Variable: python-shell-completion-setup-code

python-shell-completion-setup-code is a customizable variable defined in python.el.gz.

Value

"\ndef __PYTHON_EL_get_completions(text):\n    completions = []\n    completer = None\n\n    import json\n    try:\n        import readline, re\n\n        try:\n            import __builtin__\n        except ImportError:\n            # Python 3\n            import builtins as __builtin__\n        builtins = dir(__builtin__)\n\n        is_ipython = ('__IPYTHON__' in builtins or\n                      '__IPYTHON__active' in builtins)\n\n        if is_ipython and 'get_ipython' in builtins:\n            def filter_c(prefix, c):\n                if re.match('_+(i?[0-9]+)?$', c):\n                    return False\n                elif c[0] == '%' and not re.match('[%a-zA-Z]+$', prefix):\n                    return False\n                return True\n\n            import IPython\n            try:\n                if IPython.version_info[0] >= 6:\n                    from IPython.core.completer import provisionalcompleter\n                    with provisionalcompleter():\n                        completions = [\n                            [c.text, c.start, c.end, c.type or '?', c.signature or '']\n                             for c in get_ipython().Completer.completions(text, len(text))\n                             if filter_c(text, c.text)]\n                else:\n                    part, matches = get_ipython().Completer.complete(line_buffer=text)\n                    completions = [text + m[len(part):] for m in matches if filter_c(text, m)]\n            except:\n                pass\n        else:\n            # Try to reuse current completer.\n            completer = readline.get_completer()\n            if not completer:\n                # importing rlcompleter sets the completer, use it as a\n                # last resort to avoid breaking customizations.\n                import rlcompleter\n                completer = readline.get_completer()\n            if getattr(completer, 'PYTHON_EL_WRAPPED', False):\n                completer.print_mode = False\n            i = 0\n            while True:\n                completion = completer(text, i)\n                if not completion:\n                    break\n                i += 1\n                completions.append(completion)\n    except:\n        pass\n    finally:\n        if getattr(completer, 'PYTHON_EL_WRAPPED', False):\n            completer.print_mode = True\n    return json.dumps(completions)\n\ndef __PYTHON_EL_wrap_completer():\n    import readline\n    completer = readline.get_completer()\n\n    if not completer:\n        # Used as last resort to avoid breaking customizations.\n        import rlcompleter\n        completer = readline.get_completer()\n\n    if completer and not getattr(completer, 'PYTHON_EL_WRAPPED', False):\n        class __PYTHON_EL_Completer:\n            '''Completer wrapper that prints candidates to stdout.\n\n            It wraps an existing completer function and changes its behavior so\n            that the user input is unchanged and real candidates are printed to\n            stdout.\n\n            Returned candidates are '0__dummy_completion__' and\n            '1__dummy_completion__' in that order ('0__dummy_completion__' is\n            returned repeatedly until all possible candidates are consumed).\n\n            The real candidates are printed to stdout so that they can be\n            easily retrieved through comint output redirect trickery.\n            '''\n\n            PYTHON_EL_WRAPPED = True\n\n            def __init__(self, completer):\n                self.completer = completer\n                self.last_completion = None\n                self.print_mode = True\n\n            def __call__(self, text, state):\n                if state == 0:\n                    # Set the first dummy completion.\n                    self.last_completion = None\n                    completion = '0__dummy_completion__'\n                else:\n                    completion = self.completer(text, state - 1)\n\n                if not completion:\n                    if self.last_completion != '1__dummy_completion__':\n                        # When no more completions are available, returning a\n                        # dummy with non-sharing prefix allow ensuring output\n                        # while preventing changes to current input.\n                        # Coincidentally it's also the end of output.\n                        completion = '1__dummy_completion__'\n                elif completion.endswith('('):\n                    # Remove parens on callables as it breaks completion on\n                    # arguments (e.g. str(Ari<tab>)).\n                    completion = completion[:-1]\n                self.last_completion = completion\n\n                if completion in (\n                        '0__dummy_completion__', '1__dummy_completion__'):\n                    return completion\n                elif completion:\n                    # For every non-dummy completion, return a repeated dummy\n                    # one and print the real candidate so it can be retrieved\n                    # by comint output filters.\n                    if self.print_mode:\n                        print (completion)\n                        return '0__dummy_completion__'\n                    else:\n                        return completion\n                else:\n                    return completion\n\n        # Wrap the existing completer function only once.\n        new_completer = __PYTHON_EL_Completer(completer)\n        readline.set_completer(new_completer)"

Documentation

Code used to setup completion in inferior Python processes.

This variable was added, or its default value changed, in Emacs 31.1.

Probably introduced at or before Emacs version 31.1.

Source Code

;; Defined in /usr/src/emacs/lisp/progmodes/python.el.gz
;;; Shell completion

(defcustom python-shell-completion-setup-code
  "
def __PYTHON_EL_get_completions(text):
    completions = []
    completer = None

    import json
    try:
        import readline, re

        try:
            import __builtin__
        except ImportError:
            # Python 3
            import builtins as __builtin__
        builtins = dir(__builtin__)

        is_ipython = ('__IPYTHON__' in builtins or
                      '__IPYTHON__active' in builtins)

        if is_ipython and 'get_ipython' in builtins:
            def filter_c(prefix, c):
                if re.match('_+(i?[0-9]+)?$', c):
                    return False
                elif c[0] == '%' and not re.match('[%a-zA-Z]+$', prefix):
                    return False
                return True

            import IPython
            try:
                if IPython.version_info[0] >= 6:
                    from IPython.core.completer import provisionalcompleter
                    with provisionalcompleter():
                        completions = [
                            [c.text, c.start, c.end, c.type or '?', c.signature or '']
                             for c in get_ipython().Completer.completions(text, len(text))
                             if filter_c(text, c.text)]
                else:
                    part, matches = get_ipython().Completer.complete(line_buffer=text)
                    completions = [text + m[len(part):] for m in matches if filter_c(text, m)]
            except:
                pass
        else:
            # Try to reuse current completer.
            completer = readline.get_completer()
            if not completer:
                # importing rlcompleter sets the completer, use it as a
                # last resort to avoid breaking customizations.
                import rlcompleter
                completer = readline.get_completer()
            if getattr(completer, 'PYTHON_EL_WRAPPED', False):
                completer.print_mode = False
            i = 0
            while True:
                completion = completer(text, i)
                if not completion:
                    break
                i += 1
                completions.append(completion)
    except:
        pass
    finally:
        if getattr(completer, 'PYTHON_EL_WRAPPED', False):
            completer.print_mode = True
    return json.dumps(completions)

def __PYTHON_EL_wrap_completer():
    import readline
    completer = readline.get_completer()

    if not completer:
        # Used as last resort to avoid breaking customizations.
        import rlcompleter
        completer = readline.get_completer()

    if completer and not getattr(completer, 'PYTHON_EL_WRAPPED', False):
        class __PYTHON_EL_Completer:
            '''Completer wrapper that prints candidates to stdout.

            It wraps an existing completer function and changes its behavior so
            that the user input is unchanged and real candidates are printed to
            stdout.

            Returned candidates are '0__dummy_completion__' and
            '1__dummy_completion__' in that order ('0__dummy_completion__' is
            returned repeatedly until all possible candidates are consumed).

            The real candidates are printed to stdout so that they can be
            easily retrieved through comint output redirect trickery.
            '''

            PYTHON_EL_WRAPPED = True

            def __init__(self, completer):
                self.completer = completer
                self.last_completion = None
                self.print_mode = True

            def __call__(self, text, state):
                if state == 0:
                    # Set the first dummy completion.
                    self.last_completion = None
                    completion = '0__dummy_completion__'
                else:
                    completion = self.completer(text, state - 1)

                if not completion:
                    if self.last_completion != '1__dummy_completion__':
                        # When no more completions are available, returning a
                        # dummy with non-sharing prefix allow ensuring output
                        # while preventing changes to current input.
                        # Coincidentally it's also the end of output.
                        completion = '1__dummy_completion__'
                elif completion.endswith('('):
                    # Remove parens on callables as it breaks completion on
                    # arguments (e.g. str(Ari<tab>)).
                    completion = completion[:-1]
                self.last_completion = completion

                if completion in (
                        '0__dummy_completion__', '1__dummy_completion__'):
                    return completion
                elif completion:
                    # For every non-dummy completion, return a repeated dummy
                    # one and print the real candidate so it can be retrieved
                    # by comint output filters.
                    if self.print_mode:
                        print (completion)
                        return '0__dummy_completion__'
                    else:
                        return completion
                else:
                    return completion

        # Wrap the existing completer function only once.
        new_completer = __PYTHON_EL_Completer(completer)
        readline.set_completer(new_completer)"
  "Code used to setup completion in inferior Python processes."
  :type 'string
  :version "31.1")