#6631 Use Python warnings for development
Closed: Fixed Opened by cheimes.

mbasti ran into an issue with certificates. He later discovered that the issue was caused by unicode(b'some bytes') where unicode is an alias of str. In Python 3, str(bytestring) returns the representation of the byte string, e.g. {{{str(b'hello') == "b'hello'"}}}. This leads to hard to debug issues.

To find issues with Python 2 to 3 migration, Python has special flags. In Python 2, the '-3' command line argument enables deprecation warnings. In Python 3 the '-bb' command line argument causes exceptions for operations like str(bytestring). I propose to add the flags to all shebangs for development RPMs and to run all Python 3 CI tests with -bb.

https://docs.python.org/2.7/using/cmdline.html?highlight=#cmdoption-3
https://docs.python.org/3.6/using/cmdline.html?highlight=#cmdoption-b


Python 2 has a bytes warning mode as well. I just forgot to document it when I added the feature to 2.6 in 2008. Stupid past-me. We can have -bb in both Python 2 and 3. It's officially supported.

https://hg.python.org/cpython/rev/5341b30b1812

It is possible to enable the bytes warning at runtime with a little ctypes hack:

import ctypes, warnings
warnings.simplefilter('error', BytesWarning)
ctypes.c_int.in_dll(ctypes.pythonapi, 'Py_BytesWarningFlag').value = 2

demo

$ python3
>>> str(b'')
"b''"
>>> import ctypes, warnings
>>> warnings.simplefilter('error', BytesWarning)
>>> ctypes.c_int.in_dll(ctypes.pythonapi, 'Py_BytesWarningFlag').value = 2
>>> str(b'')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
BytesWarning: str() on a bytes instance

filter for FreeIPA

The warnings framework is pretty flexible. We can use the default mode for all modules (warn once per line) and only trigger exceptions for ipa* and __main__ (untested).

warnings.simplefilter('default', BytesWarning)
warnings.filterwarnings('errors', message=None, category=BytesWarning, module='(ipa.*|__main__)', lineno=0

master:

  • a33b25dea988aa34844869a8adc57d5cd396d3aa Enable additional warnings (BytesWarning, DeprecationWarning)
  • 3d9bec2e879d60e6bb7b2602084d3314765a6283 cryptography has deprecated serial in favor of serial_number
  • e6129a76e7093b8f9f7717e5f63ed06f9e9ef30a Stable _is_null check
  • 4965735382425356ece27e7827e2a91bc2ab2055 test_StrEnum: use int as bad type
  • 8d3bea8accb9814b3a973f4a606110fee78baf72 Ditch version_info and use version number from ipapython.version

master:

  • 5b56952a547277fab4c68da02f213d40f931a4ca Bump required python-cryptography version

master:

  • 66867319d903f7693a535471d3b81716a258ce9d Bump python-cryptography version in ipasetup.py.in

Metadata Update from @cheimes:
- Issue assigned to someone
- Issue set to the milestone: FreeIPA 4.5

Metadata