#6631 Use Python warnings for development
Closed: Fixed None Opened 7 years ago 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:

  • a33b25d Enable additional warnings (BytesWarning, DeprecationWarning)
  • 3d9bec2 cryptography has deprecated serial in favor of serial_number
  • e6129a7 Stable _is_null check
  • 4965735 test_StrEnum: use int as bad type
  • 8d3bea8 Ditch version_info and use version number from ipapython.version

master:

  • 5b56952 Bump required python-cryptography version

master:

  • 6686731 Bump python-cryptography version in ipasetup.py.in

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

7 years ago

Login to comment on this ticket.

Metadata