Skip to content

Commit

Permalink
Calculate buffer size once
Browse files Browse the repository at this point in the history
  • Loading branch information
cpburnz committed May 8, 2014
1 parent f664dec commit d75081d
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions appdirs.py
Expand Up @@ -483,12 +483,13 @@ def _get_win_folder_with_jna(csidl_name):
import array
from com.sun import jna
from com.sun.jna.platform import win32

buf = array.zeros('c', win32.WinDef.MAX_PATH * 2)

buf_size = win32.WinDef.MAX_PATH * 2
buf = array.zeros('c', buf_size)
shell = win32.Shell32.INSTANCE
shell.SHGetFolderPath(None, getattr(win32.ShlObj, csidl_name), None, win32.ShlObj.SHGFP_TYPE_CURRENT, buf)
dir = jna.Native.toString(buf.tostring()).rstrip(u"\0")

# Downgrade to short path name if have highbit chars. See
# <http://bugs.activestate.com/show_bug.cgi?id=85099>.
has_high_char = False
Expand All @@ -497,13 +498,13 @@ def _get_win_folder_with_jna(csidl_name):
has_high_char = True
break
if has_high_char:
buf = array.zeros('c', win32.WinDef.MAX_PATH * 2)
buf = array.zeros('c', buf_size)
kernel = win32.Kernel32.INSTANCE
if kernal.GetShortPathName(dir, buf, win32.WinDef.MAX_PATH * 2):
if kernal.GetShortPathName(dir, buf, buf_size):
dir = jna.Native.toString(buf.tostring()).rstrip(u"\0")

return dir

if system == "win32":
try:
import win32com.shell
Expand Down

0 comments on commit d75081d

Please sign in to comment.