Mark Hammond avatar Mark Hammond committed 19f1c88

Ensure win32com.gen_py exists even when the genpath is the default,
and if the win32com\gen_py directory doesn't exist use
%TEMP%\gen_py\%Py_VERSION% (rather than trying to create a directory
which may be read-only after installation for some users)

Comments (0)

Files changed (1)

com/win32com/__init__.py

 # Initialization for the win32com package
 #
 
-import win32api, sys
+import win32api, sys, os
 import pythoncom
 
 # Add support for an external "COM Extensions" path.
 			
 		found = 0
 		global __gen_path__
-		try:
-			if key is not None:
+		if key is not None:
+			try:
 				__gen_path__ = win32api.RegQueryValue(key, "GenPath")
 				found = 1
-				# Import a special module, Otherwise it is already all setup for us.
-				import new
-				global gen_py # Exists in the win32com namespace.
-				gen_py = new.module("win32com.gen_py")
-				gen_py.__path__ = [ __gen_path__ ]
-				sys.modules[gen_py.__name__]=gen_py
-				
-		except win32api.error:
-			found = 0
-		
+			except win32api.error:
+				pass
 		if not found:
-			__gen_path__ = win32api.GetFullPathName( __path__[0] + "\\gen_py")
+			# no key.
+			# We used to use a directory under win32com - but this sucks.
+			# If that directory exists, we still use it, but now we prefer
+			# a version specific directory under the user temp directory.
+			if os.path.isdir(win32api.GetFullPathName( __path__[0] + "\\gen_py")):
+				__gen_path__ = win32api.GetFullPathName( __path__[0] + "\\gen_py")
+			else:
+				__gen_path__ = os.path.join(
+									win32api.GetTempPath(), "gen_py",
+									"%d.%d" % (sys.version_info[0], sys.version_info[1]))
+		# Create a "win32com.gen_py", but with a custom __path__
+		import new
+		global gen_py # Exists in the win32com namespace.
+		gen_py = new.module("win32com.gen_py")
+		gen_py.__path__ = [ __gen_path__ ]
+		sys.modules[gen_py.__name__]=gen_py
 	finally:
 		if key is not None:
 			key.Close()
 	SetupEnvironment()
 
 # get rid of these for module users
-del sys, win32api, pythoncom
+del os, sys, win32api, pythoncom
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.