Skip to content

Pyenv versions listing should use version sort #2097

@Rotonen

Description

@Rotonen

With the introduction of 3.10, the sorting of versions is no longer intuitively correct.

$ pyenv versions
* system
  3.10.0
  3.9.7

This seems to happen as a side effect of glob expansion sorting on a byte for byte basis on most filesystems.

for env_path in "${path}/envs/"*; do

GNU sort would have a -V --version-sort [1] flag, but that's not portable as at least sort on macOS does not have that [2].

Columnar sort with a separator [2]could be a way to approach the problem. This could be added as a pipe to the end of that for loop.

This is the path the --definitions command of python-build has taken.

list_definitions() {
{ for DEFINITION_DIR in "${PYTHON_BUILD_DEFINITIONS[@]}"; do
[ -d "$DEFINITION_DIR" ] && ls "$DEFINITION_DIR" | grep -xv patches
done
} | sort_versions | uniq
}

sort_versions() {
sed 'h; s/[+-]/./g; s/.p\([[:digit:]]\)/.z.\1/; s/$/.z/; G; s/\n/ /' | \
LC_ALL=C sort -t. -k 1,1 -k 2,2n -k 3,3n -k 4,4n -k 5,5n | awk '{print $2}'
}

[1] https://man7.org/linux/man-pages/man1/sort.1.html
[2] https://ss64.com/osx/sort.html
[3] https://stackoverflow.com/a/39454220/1214697

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions