Search code examples
python-3.xcross-platformdocumentationcompatibility

Does Python define formal behavior for platform functions "Availability"


I'm creating cross-platform Python application, so I need to pay attention to the official disclaimers related to platform availability. For example:

os.getlogin()

...

Availability: Unix, Windows, not Emscripten, not WASI.

Unfortunatly I couldn't figure it out what "Unavailable" is in program behavior for wide spectr of functins (not only getlogin()).

  1. Is there some standard exception to distinguish "Unavailable" case?
  2. Is it enough to always wrap invocation with try...except StandardError to provide failover?
  3. Do I need hide import with try as well or it is enough for function invocation? In other words:
try:
    import os
    os.getlogin()
    ...
except StandardError: # platform doesn't support user recognition
    ...

VS

import os
try:
    os.getlogin()
    ...
except StandardError: # platform doesn't support user recognition
    ...

Solution

  • You will simply get an AttributeError. For example, on MacOS (set_handle_inheritable is only available on Windows):

    >>> os.get_handle_inheritable
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    AttributeError: module 'os' has no attribute 'get_handle_inheritable'
    

    So you can either:

    • Just import os and check the OS before every usage
    • Try to import the individual methods and catch ImportError
    • Try to call the functions and catch AttributeError