[New-bugs-announce] [issue39544] Pathlib PureWindowsPath sorting incorrect (is not natural sort)

tegavu report at bugs.python.org
Mon Feb 3 12:53:35 EST 2020


New submission from tegavu <tegavu83ag at sogetthis.com>:

Wrong behavior in pathlib.PureWindowsPath - sorting does not use natural sort.

Everything below was written based on W7x64 & Python 3.8.1 (tags/v3.8.1:1b293b6, Dec 18 2019, 23:11:46) [MSC v.1916 64 bit (AMD64)] on win32.

The documentation (https://docs.python.org/3/library/pathlib.html#general-properties) states: "Paths of a same flavour are comparable and orderable."

This can be done like this:

from pathlib import *
print( PureWindowsPath('C:\\1') < PureWindowsPath('C:\\a') )

This returns True. This is expected because 1 is sorted before a on Windows.

This sorting also works well for harder cases where other sorting functions fail: !1 should be before 1 and !a should be before a.

But it fails with natural sorting:

from pathlib import *
print( PureWindowsPath('C:\\15') < PureWindowsPath('C:\\100') )

This returns False.

This is a bug in my opinion, since PureWindowsPath should sort like Windows(Explorer) would sort. 

Right now PureWindowsPath does probably something like NTFS sorting, but NTFS is not Windows and from a function called 'WindowsPath' I expect a path that would be given in Windows Explorer.

In case a simple `dir` on Windows sorts by NTFS names (I am not sure!), PureWindowsPath also fails, since (for example) "[" < "a" should be False.

See this image for comparison:
https://i.imgur.com/GjBhWsS.png

Here is a string that can be used directly as a list to check sorting:

test_list = ['15', '100', '11111', '!', '#', '$', '%', '&', "'", '(', ')', '+', '+11111', '+aaaaa', ',', '-', ';', '=', '@', '[', ']', '^', '_', '`', 'aaaaa', 'foo0', 'foo_0', '{', '}', '~', '§', '°', '´', 'µ', '€']

----------
messages: 361315
nosy: tegavu
priority: normal
severity: normal
status: open
title: Pathlib PureWindowsPath sorting incorrect (is not natural sort)
type: behavior
versions: Python 3.8

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue39544>
_______________________________________


More information about the New-bugs-announce mailing list