[3.6] bpo-32370: Use the correct encoding for ipconfig output in the … · python/cpython@c3f9d7e (original) (raw)

Original file line number Diff line number Diff line change
@@ -419,7 +419,7 @@ def _netstat_getnode():
419 419
420 420 def _ipconfig_getnode():
421 421 """Get the hardware address on Windows by running ipconfig.exe."""
422 -import os, re
422 +import os, re, subprocess
423 423 dirs = ['', r'c:\windows\system32', r'c:\winnt\system32']
424 424 try:
425 425 import ctypes
@@ -430,11 +430,13 @@ def _ipconfig_getnode():
430 430 pass
431 431 for dir in dirs:
432 432 try:
433 -pipe = os.popen(os.path.join(dir, 'ipconfig') + ' /all')
433 +proc = subprocess.Popen([os.path.join(dir, 'ipconfig'), '/all'],
434 +stdout=subprocess.PIPE,
435 +encoding="oem")
434 436 except OSError:
435 437 continue
436 -with pipe:
437 -for line in pipe:
438 +with proc:
439 +for line in proc.stdout:
438 440 value = line.split(':')[-1].strip().lower()
439 441 if re.match('([0-9a-f][0-9a-f]-){5}[0-9a-f][0-9a-f]', value):
440 442 return int(value.replace('-', ''), 16)