summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rwxr-xr-xLib/platform.py61
-rw-r--r--Lib/test/test_platform.py19
-rw-r--r--Misc/NEWS2
3 files changed, 71 insertions, 11 deletions
diff --git a/Lib/platform.py b/Lib/platform.py
index be49f89..64025a3 100755
--- a/Lib/platform.py
+++ b/Lib/platform.py
@@ -696,27 +696,19 @@ def _bcd2str(bcd):
return hex(bcd)[2:]
-def mac_ver(release='',versioninfo=('','',''),machine=''):
-
- """ Get MacOS version information and return it as tuple (release,
- versioninfo, machine) with versioninfo being a tuple (version,
- dev_stage, non_release_version).
-
- Entries which cannot be determined are set to the paramter values
- which default to ''. All tuple entries are strings.
-
+def _mac_ver_gestalt():
+ """
Thanks to Mark R. Levinson for mailing documentation links and
code examples for this function. Documentation for the
gestalt() API is available online at:
http://www.rgaros.nl/gestalt/
-
"""
# Check whether the version info module is available
try:
import _gestalt
except ImportError:
- return release,versioninfo,machine
+ return None
# Get the infos
sysv, sysa = _mac_ver_lookup(('sysv','sysa'))
# Decode the infos
@@ -740,6 +732,53 @@ def mac_ver(release='',versioninfo=('','',''),machine=''):
machine = {0x1: '68k',
0x2: 'PowerPC',
0xa: 'i386'}.get(sysa,'')
+
+ return release,versioninfo,machine
+
+def _mac_ver_xml():
+ fn = '/System/Library/CoreServices/SystemVersion.plist'
+ if not os.path.exists(fn):
+ return None
+
+ try:
+ import plistlib
+ except ImportError:
+ return None
+
+ pl = plistlib.readPlist(fn)
+ release = pl['ProductVersion']
+ versioninfo=('', '', '')
+ machine = os.uname()[4]
+ if machine == 'ppc':
+ # for compatibility with the gestalt based code
+ machine = 'PowerPC'
+
+ return release,versioninfo,machine
+
+
+def mac_ver(release='',versioninfo=('','',''),machine=''):
+
+ """ Get MacOS version information and return it as tuple (release,
+ versioninfo, machine) with versioninfo being a tuple (version,
+ dev_stage, non_release_version).
+
+ Entries which cannot be determined are set to the paramter values
+ which default to ''. All tuple entries are strings.
+ """
+
+ # First try reading the information from an XML file which should
+ # always be present
+ info = _mac_ver_xml()
+ if info is not None:
+ return info
+
+ # If that doesn't work for some reason fall back to reading the
+ # information using gestalt calls.
+ info = _mac_ver_gestalt()
+ if info is not None:
+ return info
+
+ # If that also doesn't work return the default values
return release,versioninfo,machine
def _java_getprop(name,default):
diff --git a/Lib/test/test_platform.py b/Lib/test/test_platform.py
index af37185..466cbe3 100644
--- a/Lib/test/test_platform.py
+++ b/Lib/test/test_platform.py
@@ -167,6 +167,25 @@ class PlatformTest(unittest.TestCase):
else:
self.assertEquals(res[2], 'PowerPC')
+
+ @unittest.skipUnless(sys.platform == 'darwin', "OSX only test")
+ def test_mac_ver_with_fork(self):
+ # Issue7895: platform.mac_ver() crashes when using fork without exec
+ #
+ # This test checks that the fix for that issue works.
+ #
+ pid = os.fork()
+ if pid == 0:
+ # child
+ info = platform.mac_ver()
+ os._exit(0)
+
+ else:
+ # parent
+ cpid, sts = os.waitpid(pid, 0)
+ self.assertEquals(cpid, pid)
+ self.assertEquals(sts, 0)
+
def test_dist(self):
res = platform.dist()
diff --git a/Misc/NEWS b/Misc/NEWS
index 5c25c6c..4756daf 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -78,6 +78,8 @@ C-API
Library
-------
+- Issue #7895: platform.mac_ver() no longer crashes after calling os.fork()
+
- Issue #9323: Fixed a bug in trace.py that resulted in loosing the
name of the script being traced. Patch by Eli Bendersky.