开发者

How can I be sure of the possible return values for platform.release()

I have a program that we create an exe with for distribution. My clients run XP and Vista and 7. We are trying to make the installation easier by adjusting where the log file writes to in Win7 and Vista as we did not have to worry about that in XP. We are trying to use platform.release() to then decide where to write the log file. If it is not XP then we will write the log file to the location specified by os.environ["LOCALAPPDATA"].

I am just trying to make sur开发者_开发知识库e that when running on an XP machine that we are only going to get 'XP' as the return value, for Vista and 7 we also will only get those expected results. For instance is there some flavor of XP that will return XP.v2 (I made that up)?

We have Googled around for the answer and can't find anything that is specific enough to be certain.

Thanks for any help. Cheers


The definitive answer can be found in the source code to the exact version of your Python interpreter. I am using CPython 2.7.1. Its source code available for free download, and here is the relevant code (found in Lib/platform.py):

winver = GetVersionEx()
maj,min,buildno,plat,csd = winver
version = '%i.%i.%i' % (maj,min,buildno & 0xFFFF)
if hasattr(winver, "service_pack"):
    if winver.service_pack != "":
        csd = 'SP%s' % winver.service_pack_major
else:
    if csd[:13] == 'Service Pack ':
        csd = 'SP' + csd[13:]

if plat == VER_PLATFORM_WIN32_WINDOWS:
    regkey = 'SOFTWARE\\Microsoft\\Windows\\CurrentVersion'
    # Try to guess the release name
    if maj == 4:
        if min == 0:
            release = '95'
        elif min == 10:
            release = '98'
        elif min == 90:
            release = 'Me'
        else:
            release = 'postMe'
    elif maj == 5:
        release = '2000'

elif plat == VER_PLATFORM_WIN32_NT:
    regkey = 'SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion'
    if maj <= 4:
        release = 'NT'
    elif maj == 5:
        if min == 0:
            release = '2000'
        elif min == 1:
            release = 'XP'
        elif min == 2:
            release = '2003Server'
        else:
            release = 'post2003'
    elif maj == 6:
        if hasattr(winver, "product_type"):
            product_type = winver.product_type
        else:
            product_type = VER_NT_WORKSTATION
            # Without an OSVERSIONINFOEX capable sys.getwindowsversion(),
            # or help from the registry, we cannot properly identify
            # non-workstation versions.
            try:
                key = RegOpenKeyEx(HKEY_LOCAL_MACHINE, regkey)
                name, type = RegQueryValueEx(key, "ProductName")
                # Discard any type that isn't REG_SZ
                if type == REG_SZ and name.find("Server") != -1:
                    product_type = VER_NT_SERVER
            except WindowsError:
                # Use default of VER_NT_WORKSTATION
                pass

        if min == 0:
            if product_type == VER_NT_WORKSTATION:
                release = 'Vista'
            else:
                release = '2008Server'
        elif min == 1:
            if product_type == VER_NT_WORKSTATION:
                release = '7'
            else:
                release = '2008ServerR2'
        else:
            release = 'post2008Server'

else:
    if not release:
        # E.g. Win3.1 with win32s
        release = '%i.%i' % (maj,min)
    return release,version,csd,ptype

After reading this code I wouldn't expect to see a release value other than 'XP' on a Windows XP box.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜