Mercurial > public > mercurial-scm > hg
comparison mercurial/util.py @ 33022:ce96efec8112
py3: add utility to forward __str__() to __bytes__()
It calls unifromlocal() instead of sysstr() because __bytes__() may contain
locale-dependent values such as paths.
author | Yuya Nishihara <yuya@tcha.org> |
---|---|
date | Sat, 24 Jun 2017 13:48:04 +0900 |
parents | 19b0fd4b5570 |
children | 4c5af472a599 |
comparison
equal
deleted
inserted
replaced
33021:24c0a9a7fa86 | 33022:ce96efec8112 |
---|---|
2738 v = getattr(self, a) | 2738 v = getattr(self, a) |
2739 if v is not None: | 2739 if v is not None: |
2740 attrs.append('%s: %r' % (a, v)) | 2740 attrs.append('%s: %r' % (a, v)) |
2741 return '<url %s>' % ', '.join(attrs) | 2741 return '<url %s>' % ', '.join(attrs) |
2742 | 2742 |
2743 def __str__(self): | 2743 def __bytes__(self): |
2744 r"""Join the URL's components back into a URL string. | 2744 r"""Join the URL's components back into a URL string. |
2745 | 2745 |
2746 Examples: | 2746 Examples: |
2747 | 2747 |
2748 >>> str(url('http://user:pw@host:80/c:/bob?fo:oo#ba:ar')) | 2748 >>> str(url('http://user:pw@host:80/c:/bob?fo:oo#ba:ar')) |
2772 >>> print url(r'bundle:foo\bar') | 2772 >>> print url(r'bundle:foo\bar') |
2773 bundle:foo\bar | 2773 bundle:foo\bar |
2774 >>> print url(r'file:///D:\data\hg') | 2774 >>> print url(r'file:///D:\data\hg') |
2775 file:///D:\data\hg | 2775 file:///D:\data\hg |
2776 """ | 2776 """ |
2777 return encoding.strfromlocal(self.__bytes__()) | |
2778 | |
2779 def __bytes__(self): | |
2780 if self._localpath: | 2777 if self._localpath: |
2781 s = self.path | 2778 s = self.path |
2782 if self.scheme == 'bundle': | 2779 if self.scheme == 'bundle': |
2783 s = 'bundle:' + s | 2780 s = 'bundle:' + s |
2784 if self.fragment: | 2781 if self.fragment: |
2818 s += '?' + self.query | 2815 s += '?' + self.query |
2819 if self.fragment is not None: | 2816 if self.fragment is not None: |
2820 s += '#' + urlreq.quote(self.fragment, safe=self._safepchars) | 2817 s += '#' + urlreq.quote(self.fragment, safe=self._safepchars) |
2821 return s | 2818 return s |
2822 | 2819 |
2820 __str__ = encoding.strmethod(__bytes__) | |
2821 | |
2823 def authinfo(self): | 2822 def authinfo(self): |
2824 user, passwd = self.user, self.passwd | 2823 user, passwd = self.user, self.passwd |
2825 try: | 2824 try: |
2826 self.user, self.passwd = None, None | 2825 self.user, self.passwd = None, None |
2827 s = bytes(self) | 2826 s = bytes(self) |