explain _ord() method

This method is a historical oddity that we needed to deal with pieces of
RPM headers (strings) on Python 2. Since ord() no longer exists on
Python 3, it's difficult for new developers to understand what this code
is.  Add comments so it's easier to read.
This commit is contained in:
Ken Dreyer 2023-05-30 14:44:56 -04:00
parent d3d83dd649
commit 6b842439b0

View file

@ -958,10 +958,19 @@ def rip_rpm_hdr(src):
def _ord(s):
# in python2 it is char/str, while in py3 it is already int/bytes
"""Convert a string (single character) to an int (byte).
Use this method to get bytes from RPM headers in Python 2 and 3.
:returns: int
"""
if isinstance(s, int):
# In Python 3, RPM headers are bytes (sequences of integers), so we
# already have an int here:
return s
else:
# In Python 2, RPM headers are strings, so we have a one-character
# string here, which we must convert to an int:
return ord(s)