Page 1 of 1

Bug ensure_dir?

Published: July 22, 2018 - 1:22 PM
by Eric
Good morning,

Wapt version 1.5

I tried using the `setuphelper ensure_dir` in a package, and I encountered a malfunction:

In fact, the command `ensure_dir('C:\Programmes\Ape')` did nothing. I tested it in a Python console with all possible combinations (putting "r" before 'C:\Programmes...', using `Programmes` instead of `Programmes`...), nothing worked.

So, I looked at the function's code and ran some tests directly in Python:
(of course the directory C:\Program Files does not exist)

Code: Select all

>>> ## Test 1 :
>>> d=path.dirname('C:\program rrr')
>>> d
'C:\\'
>>> path.isdir(d)
True

>>> ## Test 2 :
>>> d="C:\Program rrr"
>>> d
'C:\\Program rrr'
>>> path.isdir(d)
False

>>> ##Test 3 :
>>> d="C:\Program Files"
>>> d
'C:\\Program Files'
>>> path.isdir(d)
True

In test 1, we see that if `d` is declared as `path.dirname`, the `isdir()` function always returns `True`, even if the path doesn't exist. (The response I get when I ask it to display `d` is a bit surprising, but later on, we see that this doesn't seem to be the cause of the problem, even though it's already problematic.)
In test 2, we see that if d is declared as a string and the path does not exist, we do indeed get False.
In test 3, we see that, on the other hand, if the path exists when d is declared as a string, we do get a True.

It seems to me, therefore, that in ensure_dir(), rather than using

Code: Select all

d = os.path.dirname(filename)
You should use

Code: Select all

d = filename
Or, you can directly use the filename variable, since `d`, in this case, doesn't seem to serve much purpose, except perhaps to convert filename to a string:

Code: Select all

d = str(filename)
Sincerely
E. Trezel

Re: Bug ensure_dir?

Published: July 22, 2018 - 5:10 PM
by Eric
Hello again,

I pushed my investigations a little further, very surprised to find a bug in a function that must be used quite a lot, and also by the display of the variable d in my test 1, which is in fact the cause of the problem:

Code: Select all

>>> ## Test 1 :
>>> d=path.dirname('C:\program rrr')
>>> d
'C:\\'
>>> path.isdir(d)
True
The response from isdir() is logical, since d is equal to C:, and C: does exist.
So I looked for which syntax(es) gave the value of d that was the one I was looking for.
Using the "intuitive" values ​​(normal Windows path, with or without a "\" at the end), it doesn't actually work:

Code: Select all


>>> d = path.dirname('C:\Program Files')
>>> d
'C:\\'

>>> d = path.dirname('C:\ProgramFiles\')
	File "<stdin>", line 1
		d = path.dirname('C:\ProgramFiles\')
									^
SyntaxError:  EOL while scanning string literal

Note: Adding 'r' before the parameter (e.g., d = path.dirname(r'C:\ProgramFiles\')) did not change my results.
The values ​​for which I managed to get this function to work correctly (da is the desired value, and path.isdir() returns the correct value) are:

Code: Select all

## Chemin Windows "normal", et on échappe le dernier "\" :
d = path.dirname('C:\Program Files\\')
## Chemin Windows "normal", et on ajoute un espace après le dernier "\" :
d = path.dirname('C:\Program Files\ ')
## Chemin Windows "normal", et on remplace le dernier "\" par un "/" :
d = path.dirname('C:\Program Files/')
## Chemin style "Linux", avec des "/" et non des "\" (et "/" à la fin obligatoire) :
d = path.dirname('C:/Program Files/')
## 
This is not mentioned in the documentation for ensure_dir() (https://wapt.tranquil.it/wapt/nightly/a ... ensure_dir), and is not taken into account in the code, unless I've missed something.
I also found nothing in the documentation for path.dirname() or isdir() on the Python side.

Hopefully this helps...
AND.