[PATCH V2] py3: change explicit conversion from str to pycompat.bytestr

Martin von Zweigbergk martinvonz at google.com
Sat Mar 18 01:16:11 EDT 2017


On Fri, Mar 17, 2017 at 7:01 PM, Yuya Nishihara <yuya at tcha.org> wrote:
> On Fri, 17 Mar 2017 11:42:03 -0700, Martin von Zweigbergk via Mercurial-devel wrote:
>> On Fri, Mar 17, 2017 at 11:22 AM, Rishabh Madan <rishabhmadan96 at gmail.com>
>> wrote:
>>
>> > The replace() function for py3 wasn't working when `value` was being
>> > typecasted as str. When I used pycompat.bytestr the error was resolved.
>> > So I guess it is preserved by such methods.
>> >
>>
>> Do you know how that works? Since it's not pycompat.bytestr, I don't
>> understand what makes it work. So if you add something like this at the end
>> of the "if fm.isplain():" block:
>>
>>             if pycompat.ispy3:
>>                print 'value is bytestr', isinstance(value, pycompat.bytestr)
>>
>> it prints "value is bytestr: True"? Or is that actually false and it
>> somehow doesn't rely on the __iter__ or __getitem__ methods to work
>> correctly when fm.isplain()?
>
> The bytestr-ness isn't important here. We just need to stringify arbitrary
> objects (e.g. booleans, integers) and we can't use bytes() as it would create
> an n-length zeroed bytes.

Aha! I had thought the whole point of pycompat.bytestr was the
overridden __getitem__ and __iter__, but it's also useful for
stringifying. So we could also use it instead of the "b'%d' % x"
pattern we've used in some places. That would more clearly express the
intent, but it is longer, so I guess that's why we haven't done it.
Thanks for explaining.


More information about the Mercurial-devel mailing list