version_info.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. """
  2. Copyright (c) 2020-2025 by Rocky Bernstein
  3. This program is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU General Public License
  5. as published by the Free Software Foundation; either version 2
  6. of the License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  14. """
  15. import platform
  16. import sys
  17. PYTHON3 = sys.version_info >= (3, 0)
  18. PYTHON_VERSION_TRIPLE = tuple(sys.version_info[:3])
  19. PYTHON_VERSION_STR = "%s.%s" % (sys.version_info[0], sys.version_info[1])
  20. IS_PYPY = "__pypy__" in sys.builtin_module_names
  21. IS_GRAAL = "Graal" in platform.python_implementation()
  22. IS_RUST = "RustPython" in platform.python_implementation()
  23. def version_tuple_to_str(
  24. version_tuple: tuple[int, ...]=PYTHON_VERSION_TRIPLE, start: int=0, end: int=3, delimiter: str="."
  25. ) -> str:
  26. """
  27. Turn a version tuple, e.g. (3,2,6), into a dotted string, e.g. "3.2.6".
  28. ``version_tuple`` is a tuple similar to what is might be returned in
  29. tuple(sys.version_info[:3]), however, the parts in the tuple could anything that
  30. has a str() method. By default, and often the length is 3, but in practice
  31. it could be other lengths.
  32. ``end`` is the length of version_tuple that you want to use.
  33. delimiter is what string to put in the between components.
  34. """
  35. return delimiter.join([str(v) for v in version_tuple[start:end]])
  36. def version_str_to_tuple(python_version: str, length: int=2) -> tuple:
  37. return tuple([int(v) for v in python_version.split(".")[:length]])