[Python-Dev] Re: python3 -bb and hash collisions

2019-09-12 Thread Sebastian Rittau

Am 11.09.19 um 15:34 schrieb Daniel Holth:


It's different. One hint is that there's already an option to disable 
the feature. The old style of error will occasionally reveal itself 
with decode errors but the new style error happens silently, you 
discover it somehow, then enable the -bb option, track down the source 
of the error, and deal with the fallout. The proposed change would 
allow `print(bytes)` for (de)bugging by letting you toggle `python3 
-bb` behavior at runtime instead of only at the command line. Or you 
could debug more explicitly by `print(bytes.decode('ebcdic'))` or 
`print(repr(bytes))`


I didn't realize you could override __builtins__.str. That's interesting.


Being able to call str() on everything is such a fundamental assumption 
that changing the behavior of str(bytes) would break Python. Porting 
from Python 2 to Python 3 is a big task and especially the 
str/unicode/bytes handling needs extra care, and this is one of those 
corner cases that might prove problematic when porting. That doesn't 
justify breaking Python, especially not for those users that have 
decided to port to Python 3 in a timely manner.


 - Sebastian

___
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/PUDMIH55ZCVWNWLTCIC4PLYJM2XZSN22/


[Python-Dev] Re: python3 -bb and hash collisions

2019-09-12 Thread Steven D'Aprano
On Wed, Sep 11, 2019 at 09:34:07AM -0400, Daniel Holth wrote:

> I didn't realize you could override __builtins__.str. That's interesting.

Don't touch __builtins__ that's a CPython implementation detail. The 
public API is to ``import builtins`` and use that.

This override technique is called monkey-patching, it's permitted but 
considered a fairly dubious thing to do in production code, since it 
risks breaking other libraries or even parts of your own code which 
relies on str(b'') working.

It may be better to isolate the monkey-patch to the module (hopefully 
there is only one!) that needs it, by a simple global that shadows the 
built-in:

import builtins
def str(obj):
assert not isinstance(obj, bytes)
return builtins.str(obj)

instead of putting it into builtins itself.


-- 
Steven
___
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/YXVMSJPQ7RTDQRQQ77NXHMYYV57K2OCI/


[Python-Dev] Re: python3 -bb and hash collisions

2019-09-12 Thread Cameron Simpson

On 10Sep2019 10:42, Daniel Holth  wrote:
[...]
I stopped using Python 3 after learning about str(bytes) by finding it 
in

my corrupted database. [...]


Could you outline how this happened to you?

Cheers,
Cameron Simpson 
___
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/47XIWHCI43C5CA3AG4PSOPAZM4KNC7ZS/


[Python-Dev] Re: python3 -bb and hash collisions

2019-09-12 Thread Nick Coghlan
On Fri., 13 Sep. 2019, 7:21 am Steven D'Aprano,  wrote:

> On Wed, Sep 11, 2019 at 09:34:07AM -0400, Daniel Holth wrote:
>
> > I didn't realize you could override __builtins__.str. That's interesting.
>
> Don't touch __builtins__ that's a CPython implementation detail. The
> public API is to ``import builtins`` and use that.
>
> This override technique is called monkey-patching, it's permitted but
> considered a fairly dubious thing to do in production code, since it
> risks breaking other libraries or even parts of your own code which
> relies on str(b'') working.
>
> It may be better to isolate the monkey-patch to the module (hopefully
> there is only one!) that needs it, by a simple global that shadows the
> built-in:
>
> import builtins
> def str(obj):
> assert not isinstance(obj, bytes)
> return builtins.str(obj)
>
> instead of putting it into builtins itself.
>

In a lot of cases like this, the problems aren't with directly calling
str(), but calling third party APIs that indirectly call str().

One nice thing a debugging str() monkey patch can do that the -bb command
line switch can't is be selective in when it fails, by inspecting the frame
stack for the modules of interest before throwing an exception.

Cheers,
Nick.



>
___
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/PC6UI6RSTGLWBRVOLII7KFDGJLD56SNU/