##// END OF EJS Templates
py3: add utility to forward __str__() to __bytes__()...
Yuya Nishihara -
r33022:ce96efec default
parent child Browse files
Show More
@@ -65,15 +65,11 b' class basectx(object):'
65
65
66 return o
66 return o
67
67
68 def __str__(self):
69 r = short(self.node())
70 if pycompat.ispy3:
71 return r.decode('ascii')
72 return r
73
74 def __bytes__(self):
68 def __bytes__(self):
75 return short(self.node())
69 return short(self.node())
76
70
71 __str__ = encoding.strmethod(__bytes__)
72
77 def __int__(self):
73 def __int__(self):
78 return self.rev()
74 return self.rev()
79
75
@@ -710,17 +706,13 b' class basefilectx(object):'
710
706
711 __bool__ = __nonzero__
707 __bool__ = __nonzero__
712
708
713 def __str__(self):
709 def __bytes__(self):
714 try:
710 try:
715 return "%s@%s" % (self.path(), self._changectx)
711 return "%s@%s" % (self.path(), self._changectx)
716 except error.LookupError:
712 except error.LookupError:
717 return "%s@???" % self.path()
713 return "%s@???" % self.path()
718
714
719 def __bytes__(self):
715 __str__ = encoding.strmethod(__bytes__)
720 try:
721 return "%s@%s" % (self.path(), self._changectx)
722 except error.LookupError:
723 return "%s@???" % self.path()
724
716
725 def __repr__(self):
717 def __repr__(self):
726 return "<%s %s>" % (type(self).__name__, str(self))
718 return "<%s %s>" % (type(self).__name__, str(self))
@@ -1306,12 +1298,11 b' class committablectx(basectx):'
1306 if self._extra['branch'] == '':
1298 if self._extra['branch'] == '':
1307 self._extra['branch'] = 'default'
1299 self._extra['branch'] = 'default'
1308
1300
1309 def __str__(self):
1310 return str(self._parents[0]) + r"+"
1311
1312 def __bytes__(self):
1301 def __bytes__(self):
1313 return bytes(self._parents[0]) + "+"
1302 return bytes(self._parents[0]) + "+"
1314
1303
1304 __str__ = encoding.strmethod(__bytes__)
1305
1315 def __nonzero__(self):
1306 def __nonzero__(self):
1316 return True
1307 return True
1317
1308
@@ -177,15 +177,24 b' def unifromlocal(s):'
177 """Convert a byte string of local encoding to a unicode string"""
177 """Convert a byte string of local encoding to a unicode string"""
178 return fromlocal(s).decode('utf-8')
178 return fromlocal(s).decode('utf-8')
179
179
180 def unimethod(bytesfunc):
181 """Create a proxy method that forwards __unicode__() and __str__() of
182 Python 3 to __bytes__()"""
183 def unifunc(obj):
184 return unifromlocal(bytesfunc(obj))
185 return unifunc
186
180 # converter functions between native str and byte string. use these if the
187 # converter functions between native str and byte string. use these if the
181 # character encoding is not aware (e.g. exception message) or is known to
188 # character encoding is not aware (e.g. exception message) or is known to
182 # be locale dependent (e.g. date formatting.)
189 # be locale dependent (e.g. date formatting.)
183 if pycompat.ispy3:
190 if pycompat.ispy3:
184 strtolocal = unitolocal
191 strtolocal = unitolocal
185 strfromlocal = unifromlocal
192 strfromlocal = unifromlocal
193 strmethod = unimethod
186 else:
194 else:
187 strtolocal = pycompat.identity
195 strtolocal = pycompat.identity
188 strfromlocal = pycompat.identity
196 strfromlocal = pycompat.identity
197 strmethod = pycompat.identity
189
198
190 if not _nativeenviron:
199 if not _nativeenviron:
191 # now encoding and helper functions are available, recreate the environ
200 # now encoding and helper functions are available, recreate the environ
@@ -2740,7 +2740,7 b' class url(object):'
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:
@@ -2774,9 +2774,6 b' class url(object):'
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':
@@ -2820,6 +2817,8 b' class url(object):'
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:
General Comments 0
You need to be logged in to leave comments. Login now