##// 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 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 68 def __bytes__(self):
75 69 return short(self.node())
76 70
71 __str__ = encoding.strmethod(__bytes__)
72
77 73 def __int__(self):
78 74 return self.rev()
79 75
@@ -710,17 +706,13 b' class basefilectx(object):'
710 706
711 707 __bool__ = __nonzero__
712 708
713 def __str__(self):
709 def __bytes__(self):
714 710 try:
715 711 return "%s@%s" % (self.path(), self._changectx)
716 712 except error.LookupError:
717 713 return "%s@???" % self.path()
718 714
719 def __bytes__(self):
720 try:
721 return "%s@%s" % (self.path(), self._changectx)
722 except error.LookupError:
723 return "%s@???" % self.path()
715 __str__ = encoding.strmethod(__bytes__)
724 716
725 717 def __repr__(self):
726 718 return "<%s %s>" % (type(self).__name__, str(self))
@@ -1306,12 +1298,11 b' class committablectx(basectx):'
1306 1298 if self._extra['branch'] == '':
1307 1299 self._extra['branch'] = 'default'
1308 1300
1309 def __str__(self):
1310 return str(self._parents[0]) + r"+"
1311
1312 1301 def __bytes__(self):
1313 1302 return bytes(self._parents[0]) + "+"
1314 1303
1304 __str__ = encoding.strmethod(__bytes__)
1305
1315 1306 def __nonzero__(self):
1316 1307 return True
1317 1308
@@ -177,15 +177,24 b' def unifromlocal(s):'
177 177 """Convert a byte string of local encoding to a unicode string"""
178 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 187 # converter functions between native str and byte string. use these if the
181 188 # character encoding is not aware (e.g. exception message) or is known to
182 189 # be locale dependent (e.g. date formatting.)
183 190 if pycompat.ispy3:
184 191 strtolocal = unitolocal
185 192 strfromlocal = unifromlocal
193 strmethod = unimethod
186 194 else:
187 195 strtolocal = pycompat.identity
188 196 strfromlocal = pycompat.identity
197 strmethod = pycompat.identity
189 198
190 199 if not _nativeenviron:
191 200 # now encoding and helper functions are available, recreate the environ
@@ -2740,7 +2740,7 b' class url(object):'
2740 2740 attrs.append('%s: %r' % (a, v))
2741 2741 return '<url %s>' % ', '.join(attrs)
2742 2742
2743 def __str__(self):
2743 def __bytes__(self):
2744 2744 r"""Join the URL's components back into a URL string.
2745 2745
2746 2746 Examples:
@@ -2774,9 +2774,6 b' class url(object):'
2774 2774 >>> print url(r'file:///D:\data\hg')
2775 2775 file:///D:\data\hg
2776 2776 """
2777 return encoding.strfromlocal(self.__bytes__())
2778
2779 def __bytes__(self):
2780 2777 if self._localpath:
2781 2778 s = self.path
2782 2779 if self.scheme == 'bundle':
@@ -2820,6 +2817,8 b' class url(object):'
2820 2817 s += '#' + urlreq.quote(self.fragment, safe=self._safepchars)
2821 2818 return s
2822 2819
2820 __str__ = encoding.strmethod(__bytes__)
2821
2823 2822 def authinfo(self):
2824 2823 user, passwd = self.user, self.passwd
2825 2824 try:
General Comments 0
You need to be logged in to leave comments. Login now