jonhadfield / python-hosts

a hosts file manager library written in python
MIT License
125 stars 33 forks source link

fix str repr #47

Closed trim21 closed 9 months ago

trim21 commented 1 year ago

mentioned in https://github.com/jonhadfield/python-hosts/pull/45

Sorry, I didn't see the image as it doesn't show on the GitHub app on iOS.

When Hosts had a path that didn't use string literals, and so included double backslashes, the repr (in the formatting) would show with single backslashes. As the repr needs to be something you can execute to reproduce the object, having single backslashes means it couldn't be evaluated. I thought about having the repr return Host(path=r'{0!r}'... but it looked messy in contrast. I also looked at Path but that isn't in 2.7 and I'm trying to retain that compatibilty.

literal string and non-literal string have same repr, assert repr(r'ss\ss') == repr('ss\\ss')

Current repr of path in Hosts is still not valid...

from python_hosts import Hosts

print(repr(Hosts()))
Hosts(path='c:\windows\system32\drivers\etc\hosts', entries=[...])

It's expected to be Hosts(path='c:\\windows\\system32\\drivers\\etc\\hosts', entries=[...], repr of single backslash should be double backslashes.

https://github.com/jonhadfield/python-hosts/blob/04bf6cddf9fce40cf317c0d3773b9467ad37fb06/python_hosts/hosts.py#L163-L166

So current manually quoted is not valid, it should be return 'Hosts(path={0!r}, entries={1!r})'.format(self.path, self.entries)

trim21 commented 9 months ago

@jonhadfield

jonhadfield commented 9 months ago

Apologies for the delay. Thanks.