[issue18373] let code force str(bytes) to raise an exception

2016-06-17 Thread Daniel Holth

Daniel Holth added the comment:

What if we changed it so that Python code could only disable str_bytes() 
process-wide, editing the original flag? Would that be fatal to debuggers and 
the repl?

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18373] let code force str(bytes) to raise an exception

2016-05-27 Thread Daniel Holth

Daniel Holth added the comment:

Then you prefer the older three year old patch that adds to pystate.h ?
http://bugs.python.org/review/18373/#ps8545

I remember not being happy with how complicated it turned out, and
perhaps having problems with not wanting the "warn once" behavior? It
seems more Pythonic to store the flag in a dict.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18373] let code force str(bytes) to raise an exception

2016-05-27 Thread Daniel Holth

Daniel Holth added the comment:

My reasoning was that str(bytes) would normally be called so rarely that
who cares.

On Fri, May 27, 2016, at 09:02 AM, STINNER Victor wrote:
> 
> STINNER Victor added the comment:
> 
> strbytes.patch of #27134: adding calls to
> PyThreadState_GetDict()+PyDict_GetItemString() seems inefficient for a
> rare use case
> 
> I would prefer to modify the existing Py_BytesWarningFlag flag.
> 
> The flag is process-wide. If it's an issue, we can use a thread-local
> storage (TLS) for the flag in C.
> 
> FYI get_exec_path() uses the following code to temporarely ignore the
> warning:
> 
> # {b'PATH': ...}.get('PATH') and {'PATH': ...}.get(b'PATH') emit a
> # BytesWarning when using python -b or python -bb: ignore the warning
> with warnings.catch_warnings():
> warnings.simplefilter("ignore", BytesWarning)
> ...
> 
> --
> 
> ___
> Python tracker 
> 
> ___

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18373] let code force str(bytes) to raise an exception

2016-05-27 Thread STINNER Victor

STINNER Victor added the comment:

strbytes.patch of #27134: adding calls to 
PyThreadState_GetDict()+PyDict_GetItemString() seems inefficient for a rare use 
case

I would prefer to modify the existing Py_BytesWarningFlag flag.

The flag is process-wide. If it's an issue, we can use a thread-local storage 
(TLS) for the flag in C.

FYI get_exec_path() uses the following code to temporarely ignore the warning:

# {b'PATH': ...}.get('PATH') and {'PATH': ...}.get(b'PATH') emit a
# BytesWarning when using python -b or python -bb: ignore the warning
with warnings.catch_warnings():
warnings.simplefilter("ignore", BytesWarning)
...

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18373] let code force str(bytes) to raise an exception

2016-05-27 Thread Daniel Holth

Daniel Holth added the comment:

Better to continue discussion here.

Attached is my second, simpler version of the feature. A context manager is 
included:

with string.StrBytesRaises():
str(b'bytes')

# raises an exception

In a normal program, you might just set the flag to True, but in a library that 
has no hope of setting the -bb flag you might use it during serialization 
rather than checking isinstance() on all input.

--
title: implement sys.get/setbyteswarningflag() -> let code force str(bytes) to 
raise an exception
versions: +Python 3.6 -Python 3.5
Added file: http://bugs.python.org/file43029/strbytes.patch

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com