sys.rst 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. :mod:`sys` -- system specific functions
  2. =======================================
  3. .. module:: sys
  4. :synopsis: system specific functions
  5. |see_cpython_module| :mod:`python:sys`.
  6. Functions
  7. ---------
  8. .. function:: exit(retval=0)
  9. Terminate current program with a given exit code. Underlyingly, this
  10. function raise as `SystemExit` exception. If an argument is given, its
  11. value given as an argument to `SystemExit`.
  12. .. function:: print_exception(exc, file=sys.stdout)
  13. Print exception with a traceback to a file-like object *file* (or
  14. `sys.stdout` by default).
  15. .. admonition:: Difference to CPython
  16. :class: attention
  17. This is simplified version of a function which appears in the
  18. ``traceback`` module in CPython. Unlike ``traceback.print_exception()``,
  19. this function takes just exception value instead of exception type,
  20. exception value, and traceback object; *file* argument should be
  21. positional; further arguments are not supported. CPython-compatible
  22. ``traceback`` module can be found in `micropython-lib`.
  23. Constants
  24. ---------
  25. .. data:: argv
  26. A mutable list of arguments the current program was started with.
  27. .. data:: byteorder
  28. The byte order of the system (``"little"`` or ``"big"``).
  29. .. data:: implementation
  30. Object with information about the current Python implementation. For
  31. MicroPython, it has following attributes:
  32. * *name* - string "micropython"
  33. * *version* - tuple (major, minor, micro), e.g. (1, 7, 0)
  34. This object is the recommended way to distinguish MicroPython from other
  35. Python implementations (note that it still may not exist in the very
  36. minimal ports).
  37. .. admonition:: Difference to CPython
  38. :class: attention
  39. CPython mandates more attributes for this object, but the actual useful
  40. bare minimum is implemented in MicroPython.
  41. .. data:: maxsize
  42. Maximum value which a native integer type can hold on the current platform,
  43. or maximum value representable by MicroPython integer type, if it's smaller
  44. than platform max value (that is the case for MicroPython ports without
  45. long int support).
  46. This attribute is useful for detecting "bitness" of a platform (32-bit vs
  47. 64-bit, etc.). It's recommended to not compare this attribute to some
  48. value directly, but instead count number of bits in it::
  49. bits = 0
  50. v = sys.maxsize
  51. while v:
  52. bits += 1
  53. v >>= 1
  54. if bits > 32:
  55. # 64-bit (or more) platform
  56. ...
  57. else:
  58. # 32-bit (or less) platform
  59. # Note that on 32-bit platform, value of bits may be less than 32
  60. # (e.g. 31) due to peculiarities described above, so use "> 16",
  61. # "> 32", "> 64" style of comparisons.
  62. .. data:: modules
  63. Dictionary of loaded modules. On some ports, it may not include builtin
  64. modules.
  65. .. data:: path
  66. A mutable list of directories to search for imported modules.
  67. .. data:: platform
  68. The platform that MicroPython is running on. For OS/RTOS ports, this is
  69. usually an identifier of the OS, e.g. ``"linux"``. For baremetal ports it
  70. is an identifier of a board, e.g. ``"pyboard"`` for the original MicroPython
  71. reference board. It thus can be used to distinguish one board from another.
  72. If you need to check whether your program runs on MicroPython (vs other
  73. Python implementation), use `sys.implementation` instead.
  74. .. data:: stderr
  75. Standard error `stream`.
  76. .. data:: stdin
  77. Standard input `stream`.
  78. .. data:: stdout
  79. Standard output `stream`.
  80. .. data:: version
  81. Python language version that this implementation conforms to, as a string.
  82. .. data:: version_info
  83. Python language version that this implementation conforms to, as a tuple of ints.