| Georg Brandl | 0eaab97 | 2009-06-08 08:00:22 +0000 | [diff] [blame] | 1 | :mod:`platform` ---  Access to underlying platform's identifying data | 
 | 2 | ===================================================================== | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3 |  | 
 | 4 | .. module:: platform | 
 | 5 |    :synopsis: Retrieves as much platform identifying data as possible. | 
 | 6 | .. moduleauthor:: Marc-Andre Lemburg <mal@egenix.com> | 
 | 7 | .. sectionauthor:: Bjorn Pettersen <bpettersen@corp.fairisaac.com> | 
 | 8 |  | 
 | 9 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 10 | .. note:: | 
 | 11 |  | 
 | 12 |    Specific platforms listed alphabetically, with Linux included in the Unix | 
 | 13 |    section. | 
 | 14 |  | 
 | 15 |  | 
 | 16 | Cross Platform | 
 | 17 | -------------- | 
 | 18 |  | 
 | 19 |  | 
 | 20 | .. function:: architecture(executable=sys.executable, bits='', linkage='') | 
 | 21 |  | 
 | 22 |    Queries the given executable (defaults to the Python interpreter binary) for | 
 | 23 |    various architecture information. | 
 | 24 |  | 
 | 25 |    Returns a tuple ``(bits, linkage)`` which contain information about the bit | 
 | 26 |    architecture and the linkage format used for the executable. Both values are | 
 | 27 |    returned as strings. | 
 | 28 |  | 
 | 29 |    Values that cannot be determined are returned as given by the parameter presets. | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 30 |    If bits is given as ``''``, the :c:func:`sizeof(pointer)` (or | 
 | 31 |    :c:func:`sizeof(long)` on Python version < 1.5.2) is used as indicator for the | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 32 |    supported pointer size. | 
 | 33 |  | 
 | 34 |    The function relies on the system's :file:`file` command to do the actual work. | 
 | 35 |    This is available on most if not all Unix  platforms and some non-Unix platforms | 
 | 36 |    and then only if the executable points to the Python interpreter.  Reasonable | 
 | 37 |    defaults are used when the above needs are not met. | 
 | 38 |  | 
| Antoine Pitrou | f259076 | 2010-12-21 18:49:01 +0000 | [diff] [blame] | 39 |    .. note:: | 
 | 40 |  | 
 | 41 |       On Mac OS X (and perhaps other platforms), executable files may be | 
 | 42 |       universal files containing multiple architectures. | 
 | 43 |  | 
 | 44 |       To get at the "64-bitness" of the current interpreter, it is more | 
 | 45 |       reliable to query the :attr:`sys.maxsize` attribute:: | 
 | 46 |  | 
 | 47 |          is_64bits = sys.maxsize > 2**32 | 
 | 48 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 49 |  | 
 | 50 | .. function:: machine() | 
 | 51 |  | 
 | 52 |    Returns the machine type, e.g. ``'i386'``. An empty string is returned if the | 
 | 53 |    value cannot be determined. | 
 | 54 |  | 
 | 55 |  | 
 | 56 | .. function:: node() | 
 | 57 |  | 
 | 58 |    Returns the computer's network name (may not be fully qualified!). An empty | 
 | 59 |    string is returned if the value cannot be determined. | 
 | 60 |  | 
 | 61 |  | 
 | 62 | .. function:: platform(aliased=0, terse=0) | 
 | 63 |  | 
 | 64 |    Returns a single string identifying the underlying platform with as much useful | 
 | 65 |    information as possible. | 
 | 66 |  | 
 | 67 |    The output is intended to be *human readable* rather than machine parseable. It | 
 | 68 |    may look different on different platforms and this is intended. | 
 | 69 |  | 
 | 70 |    If *aliased* is true, the function will use aliases for various platforms that | 
 | 71 |    report system names which differ from their common names, for example SunOS will | 
 | 72 |    be reported as Solaris.  The :func:`system_alias` function is used to implement | 
 | 73 |    this. | 
 | 74 |  | 
 | 75 |    Setting *terse* to true causes the function to return only the absolute minimum | 
 | 76 |    information needed to identify the platform. | 
 | 77 |  | 
 | 78 |  | 
 | 79 | .. function:: processor() | 
 | 80 |  | 
 | 81 |    Returns the (real) processor name, e.g. ``'amdk6'``. | 
 | 82 |  | 
 | 83 |    An empty string is returned if the value cannot be determined. Note that many | 
 | 84 |    platforms do not provide this information or simply return the same value as for | 
 | 85 |    :func:`machine`.  NetBSD does this. | 
 | 86 |  | 
 | 87 |  | 
 | 88 | .. function:: python_build() | 
 | 89 |  | 
 | 90 |    Returns a tuple ``(buildno, builddate)`` stating the Python build number and | 
 | 91 |    date as strings. | 
 | 92 |  | 
 | 93 |  | 
 | 94 | .. function:: python_compiler() | 
 | 95 |  | 
 | 96 |    Returns a string identifying the compiler used for compiling Python. | 
 | 97 |  | 
 | 98 |  | 
 | 99 | .. function:: python_branch() | 
 | 100 |  | 
 | 101 |    Returns a string identifying the Python implementation SCM branch. | 
 | 102 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 103 |  | 
 | 104 | .. function:: python_implementation() | 
 | 105 |  | 
 | 106 |    Returns a string identifying the Python implementation. Possible return values | 
| Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 107 |    are: 'CPython', 'IronPython', 'Jython'. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 108 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 109 |  | 
 | 110 | .. function:: python_revision() | 
 | 111 |  | 
 | 112 |    Returns a string identifying the Python implementation SCM revision. | 
 | 113 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 114 |  | 
 | 115 | .. function:: python_version() | 
 | 116 |  | 
 | 117 |    Returns the Python version as string ``'major.minor.patchlevel'`` | 
 | 118 |  | 
 | 119 |    Note that unlike the Python ``sys.version``, the returned value will always | 
 | 120 |    include the patchlevel (it defaults to 0). | 
 | 121 |  | 
 | 122 |  | 
 | 123 | .. function:: python_version_tuple() | 
 | 124 |  | 
 | 125 |    Returns the Python version as tuple ``(major, minor, patchlevel)`` of strings. | 
 | 126 |  | 
 | 127 |    Note that unlike the Python ``sys.version``, the returned value will always | 
 | 128 |    include the patchlevel (it defaults to ``'0'``). | 
 | 129 |  | 
 | 130 |  | 
 | 131 | .. function:: release() | 
 | 132 |  | 
 | 133 |    Returns the system's release, e.g. ``'2.2.0'`` or ``'NT'`` An empty string is | 
 | 134 |    returned if the value cannot be determined. | 
 | 135 |  | 
 | 136 |  | 
 | 137 | .. function:: system() | 
 | 138 |  | 
 | 139 |    Returns the system/OS name, e.g. ``'Linux'``, ``'Windows'``, or ``'Java'``. An | 
 | 140 |    empty string is returned if the value cannot be determined. | 
 | 141 |  | 
 | 142 |  | 
 | 143 | .. function:: system_alias(system, release, version) | 
 | 144 |  | 
 | 145 |    Returns ``(system, release, version)`` aliased to common marketing names used | 
 | 146 |    for some systems.  It also does some reordering of the information in some cases | 
 | 147 |    where it would otherwise cause confusion. | 
 | 148 |  | 
 | 149 |  | 
 | 150 | .. function:: version() | 
 | 151 |  | 
 | 152 |    Returns the system's release version, e.g. ``'#3 on degas'``. An empty string is | 
 | 153 |    returned if the value cannot be determined. | 
 | 154 |  | 
 | 155 |  | 
 | 156 | .. function:: uname() | 
 | 157 |  | 
 | 158 |    Fairly portable uname interface. Returns a tuple of strings ``(system, node, | 
 | 159 |    release, version, machine, processor)`` identifying the underlying platform. | 
 | 160 |  | 
 | 161 |    Note that unlike the :func:`os.uname` function this also returns possible | 
 | 162 |    processor information as additional tuple entry. | 
 | 163 |  | 
 | 164 |    Entries which cannot be determined are set to ``''``. | 
 | 165 |  | 
 | 166 |  | 
 | 167 | Java Platform | 
 | 168 | ------------- | 
 | 169 |  | 
 | 170 |  | 
 | 171 | .. function:: java_ver(release='', vendor='', vminfo=('','',''), osinfo=('','','')) | 
 | 172 |  | 
| Georg Brandl | c6c3178 | 2009-06-08 13:41:29 +0000 | [diff] [blame] | 173 |    Version interface for Jython. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 174 |  | 
 | 175 |    Returns a tuple ``(release, vendor, vminfo, osinfo)`` with *vminfo* being a | 
 | 176 |    tuple ``(vm_name, vm_release, vm_vendor)`` and *osinfo* being a tuple | 
 | 177 |    ``(os_name, os_version, os_arch)``. Values which cannot be determined are set to | 
 | 178 |    the defaults given as parameters (which all default to ``''``). | 
 | 179 |  | 
 | 180 |  | 
 | 181 | Windows Platform | 
 | 182 | ---------------- | 
 | 183 |  | 
 | 184 |  | 
 | 185 | .. function:: win32_ver(release='', version='', csd='', ptype='') | 
 | 186 |  | 
 | 187 |    Get additional version information from the Windows Registry and return a tuple | 
 | 188 |    ``(version, csd, ptype)`` referring to version number, CSD level and OS type | 
 | 189 |    (multi/single processor). | 
 | 190 |  | 
 | 191 |    As a hint: *ptype* is ``'Uniprocessor Free'`` on single processor NT machines | 
 | 192 |    and ``'Multiprocessor Free'`` on multi processor machines. The *'Free'* refers | 
 | 193 |    to the OS version being free of debugging code. It could also state *'Checked'* | 
 | 194 |    which means the OS version uses debugging code, i.e. code that checks arguments, | 
 | 195 |    ranges, etc. | 
 | 196 |  | 
 | 197 |    .. note:: | 
 | 198 |  | 
| Antoine Pitrou | f259076 | 2010-12-21 18:49:01 +0000 | [diff] [blame] | 199 |       This function works best with Mark Hammond's | 
| Christian Heimes | 02781dc | 2008-03-21 01:11:52 +0000 | [diff] [blame] | 200 |       :mod:`win32all` package installed, but also on Python 2.3 and | 
 | 201 |       later (support for this was added in Python 2.6). It obviously | 
 | 202 |       only runs on Win32 compatible platforms. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 203 |  | 
 | 204 |  | 
 | 205 | Win95/98 specific | 
 | 206 | ^^^^^^^^^^^^^^^^^ | 
 | 207 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 208 | .. function:: popen(cmd, mode='r', bufsize=None) | 
 | 209 |  | 
 | 210 |    Portable :func:`popen` interface.  Find a working popen implementation | 
 | 211 |    preferring :func:`win32pipe.popen`.  On Windows NT, :func:`win32pipe.popen` | 
 | 212 |    should work; on Windows 9x it hangs due to bugs in the MS C library. | 
 | 213 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 214 |  | 
 | 215 | Mac OS Platform | 
 | 216 | --------------- | 
 | 217 |  | 
 | 218 |  | 
 | 219 | .. function:: mac_ver(release='', versioninfo=('','',''), machine='') | 
 | 220 |  | 
 | 221 |    Get Mac OS version information and return it as tuple ``(release, versioninfo, | 
 | 222 |    machine)`` with *versioninfo* being a tuple ``(version, dev_stage, | 
 | 223 |    non_release_version)``. | 
 | 224 |  | 
 | 225 |    Entries which cannot be determined are set to ``''``.  All tuple entries are | 
 | 226 |    strings. | 
 | 227 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 228 |    Documentation for the underlying :c:func:`gestalt` API is available online at | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 229 |    http://www.rgaros.nl/gestalt/. | 
 | 230 |  | 
 | 231 |  | 
 | 232 | Unix Platforms | 
 | 233 | -------------- | 
 | 234 |  | 
 | 235 |  | 
| Christian Heimes | 02781dc | 2008-03-21 01:11:52 +0000 | [diff] [blame] | 236 | .. function:: dist(distname='', version='', id='', supported_dists=('SuSE','debian','redhat','mandrake',...)) | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 237 |  | 
| Benjamin Peterson | e9bbc8b | 2008-09-28 02:06:32 +0000 | [diff] [blame] | 238 |    This is another name for :func:`linux_distribution`. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 239 |  | 
| Christian Heimes | 02781dc | 2008-03-21 01:11:52 +0000 | [diff] [blame] | 240 | .. function:: linux_distribution(distname='', version='', id='', supported_dists=('SuSE','debian','redhat','mandrake',...), full_distribution_name=1) | 
 | 241 |  | 
 | 242 |    Tries to determine the name of the Linux OS distribution name. | 
 | 243 |  | 
| Benjamin Peterson | e9bbc8b | 2008-09-28 02:06:32 +0000 | [diff] [blame] | 244 |    ``supported_dists`` may be given to define the set of Linux distributions to | 
 | 245 |    look for. It defaults to a list of currently supported Linux distributions | 
 | 246 |    identified by their release file name. | 
| Christian Heimes | 02781dc | 2008-03-21 01:11:52 +0000 | [diff] [blame] | 247 |  | 
| Benjamin Peterson | e9bbc8b | 2008-09-28 02:06:32 +0000 | [diff] [blame] | 248 |    If ``full_distribution_name`` is true (default), the full distribution read | 
 | 249 |    from the OS is returned. Otherwise the short name taken from | 
 | 250 |    ``supported_dists`` is used. | 
| Christian Heimes | 02781dc | 2008-03-21 01:11:52 +0000 | [diff] [blame] | 251 |  | 
| Benjamin Peterson | e9bbc8b | 2008-09-28 02:06:32 +0000 | [diff] [blame] | 252 |    Returns a tuple ``(distname,version,id)`` which defaults to the args given as | 
 | 253 |    parameters.  ``id`` is the item in parentheses after the version number.  It | 
 | 254 |    is usually the version codename. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 255 |  | 
 | 256 | .. function:: libc_ver(executable=sys.executable, lib='', version='', chunksize=2048) | 
 | 257 |  | 
 | 258 |    Tries to determine the libc version against which the file executable (defaults | 
 | 259 |    to the Python interpreter) is linked.  Returns a tuple of strings ``(lib, | 
 | 260 |    version)`` which default to the given parameters in case the lookup fails. | 
 | 261 |  | 
 | 262 |    Note that this function has intimate knowledge of how different libc versions | 
| Christian Heimes | c3f30c4 | 2008-02-22 16:37:40 +0000 | [diff] [blame] | 263 |    add symbols to the executable is probably only usable for executables compiled | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 264 |    using :program:`gcc`. | 
 | 265 |  | 
 | 266 |    The file is read and scanned in chunks of *chunksize* bytes. | 
 | 267 |  |