Rev 3629: Use GetComputerNameEx from ctypes when available. in http://bzr.arbash-meinel.com/branches/bzr/1.7-dev/unicode_hostname

John Arbash Meinel john at arbash-meinel.com
Tue Aug 19 20:44:01 BST 2008


At http://bzr.arbash-meinel.com/branches/bzr/1.7-dev/unicode_hostname

------------------------------------------------------------
revno: 3629
revision-id: john at arbash-meinel.com-20080819194357-18n0w1tfvmgazbg5
parent: skip at vistadev--20080817032414-rzsqf40vu6c641ha
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: unicode_hostname
timestamp: Tue 2008-08-19 14:43:57 -0500
message:
  Use GetComputerNameEx from ctypes when available.
modified:
  bzrlib/win32utils.py           win32console.py-20051021033308-123c6c929d04973d
-------------- next part --------------
=== modified file 'bzrlib/win32utils.py'
--- a/bzrlib/win32utils.py	2008-08-17 03:24:14 +0000
+++ b/bzrlib/win32utils.py	2008-08-19 19:43:57 +0000
@@ -207,36 +207,54 @@
     return os.environ.get('USERNAME', None)
 
 
+# 1 == ComputerNameDnsHostname, which returns "The DNS host name of the local
+# computer or the cluster associated with the local computer."
+_WIN32_ComputerNameDnsHostname = 1
+
 def get_host_name():
     """Return host machine name.
     If name cannot be obtained return None.
 
-    Returned value can be unicode or plain sring.
-    To convert plain string to unicode use
-    s.decode(bzrlib.user_encoding)
+    :return: A unicode string representing the host name. On win98, this may be
+        a plain string as win32 api doesn't support unicode.
     """
     if has_win32api:
         try:
-            # 3 == ComputerNameDnsFullyQualified, which returns "The 
-            # fully-qualified DNS name that uniquely identifies the local 
-            # computer or the cluster associated with the local computer."
-            return win32api.GetComputerNameEx(3)
+            return win32api.GetComputerNameEx(_WIN32_ComputerNameDnsHostname)
         except (NotImplementedError, win32api.error):
             # NotImplemented will happen on win9x...
             pass
     if has_ctypes:
         try:
             kernel32 = ctypes.windll.kernel32
-            GetComputerName = getattr(kernel32, 'GetComputerName'+suffix)
         except AttributeError:
-            pass
+            pass # Missing the module we need
         else:
             buf = create_buffer(MAX_COMPUTERNAME_LENGTH+1)
             n = ctypes.c_int(MAX_COMPUTERNAME_LENGTH+1)
-            if GetComputerName(buf, ctypes.byref(n)):
+
+            # Try GetComputerName which gives a proper Unicode hostname
+            GetComputerNameEx = getattr(kernel32, 'GetComputerNameEx'+suffix,
+                                        None)
+            if (GetComputerNameEx is not None
+                and GetComputerNameEx(_WIN32_ComputerNameDnsHostname,
+                                      buf, ctypes.byref(n))):
+                return buf.value
+
+            # Try GetComputerName in case GetComputerNameEx wasn't found
+            # It returns the NETBIOS name, which isn't as good, but still ok.
+            # The first GetComputerNameEx might have changed 'n', so reset it
+            n = ctypes.c_int(MAX_COMPUTERNAME_LENGTH+1)
+            GetComputerName = getattr(kernel32, 'GetComputerName'+suffix,
+                                      None)
+            if (GetComputerName is not None
+                and GetComputerName(buf, ctypes.byref(n))):
                 return buf.value
     # otherwise try env variables, which will be 'mbcs' encoded
     # on Windows (Python doesn't expose the native win32 unicode environment)
+    # According to this:
+    # http://msdn.microsoft.com/en-us/library/aa246807.aspx
+    # environment variables should always be encoded in 'mbcs'.
     try:
         return os.environ['COMPUTERNAME'].decode("mbcs")
     except KeyError:
@@ -248,7 +266,7 @@
         import bzrlib
         s = s.decode(bzrlib.user_encoding)
     return s
-    
+
 
 def get_appdata_location_unicode():
     return _ensure_unicode(get_appdata_location())



More information about the bazaar-commits mailing list