[Python-ideas] Re: Allowing -b (BytesWarning) to be activated in other ways

2020-07-16 Thread Christian Heimes
On 16/07/2020 16.38, Shai Berger wrote:
> Hi Pythonistas,
> 
> The -b flag, which turns on checks which emit BytesWarnings on
> operations mixing bytes and str objects, is very useful.
> 
> However, the only way to set this flag is via the Python invocation.
> This limits its usability in contexts where the user's control of the
> Python invocation is limited, e.g when using Python embedded in another
> executable (such as uwsgi). There appears to be no function which can
> set the flag, and no environment variable which controls it.
> 
> Up to Python 3.7, the extension module provided by the bytes-warning
> package[1] works around this (it exposes a function which allows
> setting the flag from within Python). But with 3.8 (and I suspect,
> because of PEP-587[2] related changes), this fails silently and the sys
> flag bytes_warning remains unaffected.
> 
> Can we have a non-invocation way to control this flag?

You can use ctypes to modify bytes warnings. I'm using this trick in
FreeIPA. It works up to Python 3.7. For Python 3.8 and newer you to
modify the bytes_warning member of the current interpreter.

https://github.com/freeipa/freeipa/blob/53d472b490ac7a14fc78516b448d4aa312b79b7f/ipalib/__init__.py#L886-L912

Christian
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/6APQ6R5F425UKKPCE6T776DFPYB36GZ6/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Allowing -b (BytesWarning) to be activated in other ways

2020-07-16 Thread Serhiy Storchaka

16.07.20 20:05, Brett Cannon пише:
I honestly wouldn't expect that flag to last forever because it exists 
purely to help with Python 2 -> 3 transitions. Since it fundamentally 
changes how things like comparison and `str()` work it isn't something 
to flip on needlessly and you would be better off using a type checker 
or linter to look for those sorts of errors.


As such, changing the C API specifically for this flag isn't something I 
personally would want to see.


Seconded.
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/QRXR4DTYNLLJSENV2RN6VFELD7KPLVIO/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Allowing -b (BytesWarning) to be activated in other ways

2020-07-16 Thread Brett Cannon
I honestly wouldn't expect that flag to last forever because it exists
purely to help with Python 2 -> 3 transitions. Since it fundamentally
changes how things like comparison and `str()` work it isn't something to
flip on needlessly and you would be better off using a type checker or
linter to look for those sorts of errors.

As such, changing the C API specifically for this flag isn't something I
personally would want to see.

On Thu, Jul 16, 2020 at 8:17 AM Shai Berger  wrote:

> Hi Pythonistas,
>
> The -b flag, which turns on checks which emit BytesWarnings on
> operations mixing bytes and str objects, is very useful.
>
> However, the only way to set this flag is via the Python invocation.
> This limits its usability in contexts where the user's control of the
> Python invocation is limited, e.g when using Python embedded in another
> executable (such as uwsgi). There appears to be no function which can
> set the flag, and no environment variable which controls it.
>
> Up to Python 3.7, the extension module provided by the bytes-warning
> package[1] works around this (it exposes a function which allows
> setting the flag from within Python). But with 3.8 (and I suspect,
> because of PEP-587[2] related changes), this fails silently and the sys
> flag bytes_warning remains unaffected.
>
> Can we have a non-invocation way to control this flag?
>
> Thanks,
> Shai.
>
> [1] https://pypi.org/project/bytes-warning/
> [2] https://www.python.org/dev/peps/pep-0587/
> ___
> Python-ideas mailing list -- python-ideas@python.org
> To unsubscribe send an email to python-ideas-le...@python.org
> https://mail.python.org/mailman3/lists/python-ideas.python.org/
> Message archived at
> https://mail.python.org/archives/list/python-ideas@python.org/message/FV37A5SIQGY4VJ6ESUNAMVXHUXUHUVGO/
> Code of Conduct: http://python.org/psf/codeofconduct/
>
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/N6L6FUEQJF3GX3BLT65SG73LPCZTOUZU/
Code of Conduct: http://python.org/psf/codeofconduct/