zsh-workers
 help / color / Atom feed
* Help Request/Bug Report: comparguments causes _arguments to fail in certain cases
@ 2020-05-03  7:40 Dan Arad
  2020-05-03 16:58 ` Daniel Shahaf
  0 siblings, 1 reply; 2+ messages in thread
From: Dan Arad @ 2020-05-03  7:40 UTC (permalink / raw)
  To: zsh-workers


[-- Attachment #1: Type: text/plain, Size: 5721 bytes --]

Hi there,

From what I understand, bug reports should be mailed here. If I'm wrong I'd
be happy to be redirected to the correct medium.
What I want to report is probably not a bug, and stems from my
misunderstanding of compsys. Any help will be greatly appreciated.
The following is a copy paste of the a this question
<https://stackoverflow.com/questions/61560687/comparguments-causes-arguments-to-fail-in-certain-cases>
I asked on stack overflow:

*I'm writing a zsh completion script for arbitrary python scripts (similar
to argcomplete <https://github.com/kislyuk/argcomplete>).*

*I'm trying to get the script to work in several use cases:*

*1. The script is invoked directly (e.g. ~/script.py)*

*2. The script is invoked through python (e.g. python script.py)*

*3. The script is invoked as a python module (e.g. python -m script)*


*I have so far managed to successfully handle the first case, but the
second case fails to retrieve any completions. Using zsh completion debug
log I was able to see where things went wrong:*

*The _arguments function calls a builtin function named comparguments. In
the first case the function returns 0 and the control flow continues as
expected. In the second case the function fails and causes _arguments to
immediately return 1. This happens even though the arguments to
comparguments are identical in both cases.*



*Here <https://gist.github.com/dan1994/45f97442dc488338b59ed8ddf70424e1> is
a link to debug logs for both situations. For the first scenario
comparguments is called in line 199, and in the second scenario it is
called in line 197.*



*My script:*



*#compdef -p **



*_python_script() {*

*    # Expand all words*

*    local -a expanded_words*

*    __pyzshcomplete_exapnd_tilde_in_all_words*



*    # Check if we should run or else quit*

*    __pyzshcomplete_should_run || return 1*



*    # Skip any other completion function*

*    _compskip=all*



*    # Retrieve valid completions and pass them to _arguments*

*    local arguments=(*

*        ${(f)"$(PYZSHCOMPLETE=1 __python_argcomplete_run
${expanded_words[@]})"}*

*    )*

*    _arguments -s -w : ${arguments[@]}*



*    # Always return 0 - if _arguments fails, compsys for some reason
invokes*

*    # this script again consuming more time and gaining nothing.*

*    # If we are in this context there shouldn't be other completions
anyways so*

*    # no reason to return 1 anyways...*

*    return 0*

*}*





*__pyzshcomplete_exapnd_tilde_in_all_words() {*

*    for ((i = 1; i <= $#words; i++)); do*

*        expanded_words[$i]=${~words[$i]}*

*    done*

*}*





*### The following code is taken from the argcomplete project, including*

*### original copyright. Changes from the original will be marked by a
comment*

*### Starting with CHANGE.*

*### Original code:*

*###
https://github.com/kislyuk/argcomplete/blob/v1.11.1/argcomplete/bash_completion.d/python-argcomplete
<https://github.com/kislyuk/argcomplete/blob/v1.11.1/argcomplete/bash_completion.d/python-argcomplete>*



*# Copyright 2012-2019, Andrey Kislyuk and argcomplete contributors.*

*# Licensed under the Apache License. See
https://github.com/kislyuk/argcomplete
<https://github.com/kislyuk/argcomplete> for more info.*



*# CHANGE: This function is a heavily refactored copy of the first part of*

*# _python_argcomplete_global*

*__pyzshcomplete_should_run() {*

*    local executable=${expanded_words[1]}*



*    if [[ $executable == python* ]] || [[ $executable == pypy* ]]; then*

*        # If 2nd word is the -m flag, check that the module has the magic
string*

*        [[ ${expanded_words[2]} == -m ]] && __python_argcomplete_run \*

*            $executable -m argcomplete._check_module ${expanded_words[3]}
&& \*

*            return 0*

*        # If 2nd word is a file, check that it has the magic string*

*        [[ -f ${expanded_words[2]} ]] &&
__python_argcomplete_scan_head_noerr \*

*            ${expanded_words[2]} && return 0*

*        return 1*

*    fi*



*    # Assume the first word is a script and find its path*

*    local script_path*

*    # Search in path*

*    if type -p $executable > /dev/null 2>&1; then*

*        script_path=$(type -p $executable | sed -r "s:$executable is ::")*

*    # Check if it's a file*

*    elif [[ -f $executable ]]; then*

*        script_path=$executable*

*    fi*



*    # If found a path, scan for magic*

*    if [[ -n $script_path ]]; then*

*        __python_argcomplete_scan_head_noerr $script_path && return 0*

*        return 1*

*    fi*



*    return 1*

*}*



*# Run something, muting output or redirecting it to the debug stream*

*# depending on the value of _ARC_DEBUG.*

*__python_argcomplete_run() {*

*    if [[ -z "$_ARC_DEBUG" ]]; then*

*        "$@" 8>&1 9>&2 1>/dev/null 2>&1*

*    else*

*        "$@" 8>&1 9>&2 1>&9 2>&1*

*    fi*

*}*



*# Scan the beginning of an executable file ($1) for a regexp ($2). By
default,*

*# scan for the magic string indicating that the executable supports the*

*# argcomplete completion protocol. Scan the first kilobyte.*

*__python_argcomplete_scan_head() {*

*    # CHANGE: the zsh read builtin has different options and behaves
differently*

*    read -s -r -k 1024 -u 0 < "$1"*

*    [[ "$REPLY" =~ ${2:-PYTHON_ARGCOMPLETE_OK} ]]*

*}*



*__python_argcomplete_scan_head_noerr() {*

*    __python_argcomplete_scan_head "$@" 2>/dev/null*

*}*


*EDIT:*

*In order to temporarily bypass the problem I tried adding a shift words
before calling _arguments. This caused comparguments to succeed (!), but
still causes _arguments to fail with a no arguments message later on.*

*I added the log for this case to the gist linked above.*

-- 
Dan Arad
dan1994@gmail.com

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Help Request/Bug Report: comparguments causes _arguments to fail in certain cases
  2020-05-03  7:40 Help Request/Bug Report: comparguments causes _arguments to fail in certain cases Dan Arad
@ 2020-05-03 16:58 ` Daniel Shahaf
  0 siblings, 0 replies; 2+ messages in thread
From: Daniel Shahaf @ 2020-05-03 16:58 UTC (permalink / raw)
  To: Dan Arad; +Cc: zsh-workers

Dan Arad wrote on Sun, 03 May 2020 10:40 +0300:
> Hi there,
> 
> From what I understand, bug reports should be mailed here. If I'm wrong I'd
> be happy to be redirected to the correct medium.

For future reference, you should have addressed your question to
zsh-users@, as the answer to it doesn't involve a change to zsh itself.
Furthermore, it would be helpful to configure your email client to
generate a text/plain part that doesn't surround every line by asterisks
and add spurious blank lines in code blocks.

> What I want to report is probably not a bug, and stems from my
> misunderstanding of compsys. Any help will be greatly appreciated.
> The following is a copy paste of the a this question
> <https://stackoverflow.com/questions/61560687/comparguments-causes-arguments-to-fail-in-certain-cases>
> I asked on stack overflow:
> 

Thanks for the cross-reference.

> *1. The script is invoked directly (e.g. ~/script.py)*
> 
> *2. The script is invoked through python (e.g. python script.py)*
> 
> *3. The script is invoked as a python module (e.g. python -m script)*
> 
> 
> *I have so far managed to successfully handle the first case, but the
> second case fails to retrieve any completions. Using zsh completion debug
> log I was able to see where things went wrong:*
> *EDIT:*
> 
> *In order to temporarily bypass the problem I tried adding a shift words
> before calling _arguments. This caused comparguments to succeed (!), but
> still causes _arguments to fail with a no arguments message later on.*
> 
> *I added the log for this case to the gist linked above.*

Well, for starters, if you «shift words», you should decrement CURRENT
as well.

However, _python uses the «*::…» form of an _arguments optspec,
which should take care of $words/$NUMERIC for you.  I assume the reason
it didn't is that you used «compdef -p».  Try -P instead?  If I'm not
mistaken, that would also handle «python … script.py <TAB>» for you
(where the ellipsis stands for python's --options).

Cheers,

Daniel

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, back to index

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-05-03  7:40 Help Request/Bug Report: comparguments causes _arguments to fail in certain cases Dan Arad
2020-05-03 16:58 ` Daniel Shahaf

zsh-workers

Archives are clonable: git clone --mirror http://inbox.vuxu.org/zsh-workers

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://inbox.vuxu.org/vuxu.archive.zsh.workers


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git