Hi!
I use escapism as it is part of the dockerspawner:
https://github.com/jupyterhub/dockerspawner/blob/master/dockerspawner/dockerspawner.py#L20
I have a string containing an underscore, and after escaping it by an underscore, I have an additional two characters: 5F
(which is a part of an URL-encoded underscore). It appears to me that everytime the replacement character (third argument) is actually contained in the original string, it adds characters.
This is very unexpected. And it is very unpractical, as I cannot of course control the characters in my original string (if I could, I would not escape them).
Whether that replacement character is part of the "safe" characters does not affect this behaviour.
I am using Python 3.6 on a linux machine.
Please see here:
yyy@yyy:/srv/jupyterhub# python3
Python 3.6.9 (default, Nov 7 2019, 10:44:02)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import escapism
>>> import string
>>> s = 'foo_foo'
>>> safe = set(string.ascii_letters + string.digits + "-")
>>> e = escapism.escape(s, safe, '_')
>>> e
'foo_5Ffoo'
It does not change if I add the underscore to the safe chars:
>>> import escapism
>>> import string
>>>
>>> s = 'foo_foo'
>>> safe = set(string.ascii_letters + string.digits + "-"+ "_")
>>>
>>> 'f' in safe, 'o' in safe, '_' in safe
(True, True, True)
>>>
>>> escapism.escape(s, safe, '_')
'foo_5Ffoo'
And actually, it removes the replacement character from the safe characters:
>>> import escapism
>>>
>>> s = 'foo_foo'
>>> safe = {'f','o','_','x'}
>>> safe
{'o', '_', 'f', 'x'}
>>> escapism.escape(s, safe, 'x')
'foo_foo'
>>> safe
{'o', '_', 'f'}
>>> # x was removed
>>> escapism.escape(s, safe, 'x')
'foo_foo'
>>> safe
{'o', '_', 'f'}
And it adds characters to every occurrence of the replacement character in the string, no matter whether it is safe or not:
>>> import escapism
>>> s = 'foo_foo'
>>> safe = {'f','o','_'}
>>> safe
{'o', '_', 'f'}
>>> escapism.escape(s, safe, 'o')
'fo6Fo6F_fo6Fo6F' ### <-- chars were added!
>>> safe
{'_', 'f'} ### <-- o was removed from safe!
Can anybody reproduce this?