Skip to content

Commit

Permalink
Revert "Replace imp module with import lib (issue 1256). (#1537)" for…
Browse files Browse the repository at this point in the history
… python2 breakage.

I'll reapply it after killing python 2.

This reverts commit 5c39439.
  • Loading branch information
mhammond committed Jun 22, 2020
1 parent 7eb8434 commit c325739
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 45 deletions.
2 changes: 1 addition & 1 deletion Pythonwin/pywin/framework/scriptutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ def ImportFile():
## The interpreter sees this import as a local assignment, so Python 2.x throws
## UnboundLocalError: local variable 'reload' referenced before assignment
## when you try to use reload after this fails
from importlib import reload as my_reload # py3k
from imp import reload as my_reload # py3k
except ImportError:
my_reload = reload # reload a builtin in py2k
mod = my_reload(sys.modules[modName])
Expand Down
6 changes: 3 additions & 3 deletions Pythonwin/pywin/scintilla/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import traceback
import pywin
import glob
import importlib
import imp

import win32api

Expand Down Expand Up @@ -94,7 +94,7 @@ def __init__(self, f):
magic = marshal.load(cf)
size = marshal.load(cf)
mtime = marshal.load(cf)
if magic == importlib.util.MAGIC_NUMBER and \
if magic == imp.get_magic() and \
win32api.GetKeyboardLayoutName() == kblayoutname and \
src_stat[stat.ST_MTIME] == mtime and \
src_stat[stat.ST_SIZE] == size:
Expand Down Expand Up @@ -139,7 +139,7 @@ def __init__(self, f):
cf = open(compiled_name, "wb")
marshal.dump(compiled_config_version, cf)
marshal.dump(win32api.GetKeyboardLayoutName(), cf)
marshal.dump(importlib.util.MAGIC_NUMBER, cf)
marshal.dump(imp.get_magic(), cf)
marshal.dump(src_stat[stat.ST_SIZE], cf)
marshal.dump(src_stat[stat.ST_MTIME], cf)
marshal.dump(self.cache, cf)
Expand Down
6 changes: 3 additions & 3 deletions com/win32com/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,11 @@ def __PackageSupportBuildPath__(package_path):
# set that up.
if "win32com.gen_py" not in sys.modules:
# Create a "win32com.gen_py", but with a custom __path__
import types
gen_py = types.ModuleType("win32com.gen_py")
import imp
gen_py = imp.new_module("win32com.gen_py")
gen_py.__path__ = [ __gen_path__ ]
sys.modules[gen_py.__name__]=gen_py
del types
del imp
gen_py = sys.modules["win32com.gen_py"]

# get rid of these for module users
Expand Down
2 changes: 1 addition & 1 deletion com/win32com/client/gencache.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import CLSIDToClass
import operator
try:
from importlib import reload # exported by the importlib module in py3k.
from imp import reload # exported by the imp module in py3k.
except:
pass # a builtin on py2k.

Expand Down
2 changes: 1 addition & 1 deletion com/win32com/servers/PythonTools.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Tools:
def reload(self, module):
if module in sys.modules:
try:
from importlib import reload
from imp import reload
except ImportError:
pass # builtin in py2k
reload(sys.modules[module])
Expand Down
6 changes: 3 additions & 3 deletions com/win32comext/axscript/client/pyscript.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,9 @@ def __init__(self):

def InitNew(self):
framework.COMScript.InitNew(self)
import types
import imp
self.scriptDispatch = None
self.globalNameSpaceModule = types.ModuleType("__ax_main__")
self.globalNameSpaceModule = imp.new_module("__ax_main__")
self.globalNameSpaceModule.__dict__['ax'] = AXScriptAttribute(self)

self.codeBlocks = []
Expand Down Expand Up @@ -369,4 +369,4 @@ def Register(klass=PyScript):
return ret

if __name__=='__main__':
Register()
Register()
5 changes: 3 additions & 2 deletions isapi/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# this code adapted from "Tomcat JK2 ISAPI redirector", part of Apache
# Created July 2004, Mark Hammond.
import sys, os, importlib, shutil, stat
import sys, os, imp, shutil, stat
import operator
from win32com.client import GetObject, Dispatch
from win32com.client.gencache import EnsureModule, EnsureDispatch
Expand Down Expand Up @@ -35,7 +35,8 @@
_DEFAULT_ENABLE_DIR_BROWSING = False
_DEFAULT_ENABLE_DEFAULT_DOC = False

is_debug_build = '_d.pyd' in importlib.machinery.EXTENSION_SUFFIXES
_extensions = [ext for ext, _, _ in imp.get_suffixes()]
is_debug_build = '_d.pyd' in _extensions

this_dir = os.path.abspath(os.path.dirname(__file__))

Expand Down
12 changes: 6 additions & 6 deletions pywin32_postinstall.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,17 +161,17 @@ def CopyTo(desc, src, dest):
# our pywintypes_system32 directory.
def LoadSystemModule(lib_dir, modname):
# See if this is a debug build.
import importlib
if '_d.pyd' in importlib.machinery.EXTENSION_SUFFIXES:
suffix = '_d'
import imp
for suffix_item in imp.get_suffixes():
if suffix_item[0]=='_d.pyd':
suffix = '_d'
break
else:
suffix = ""
filename = "%s%d%d%s.dll" % \
(modname, sys.version_info[0], sys.version_info[1], suffix)
filename = os.path.join(lib_dir, "pywin32_system32", filename)
loader = importlib.machinery.ExtensionFileLoader(modname, filename)
spec = importlib.machinery.ModuleSpec(name=modname, loader=loader, origin=filename)
mod = importlib._bootstrap._load(spec)
mod = imp.load_dynamic(modname, filename)


def SetPyKeyVal(key_name, value_name, value):
Expand Down
40 changes: 18 additions & 22 deletions win32/Lib/pywintypes.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
# Magic utility that "redirects" to pywintypesxx.dll
import importlib, sys, os

def _load_dynamic(name, path, file=None):
loader = importlib.machinery.ExtensionFileLoader(name, path)
spec = importlib.machinery.ModuleSpec(name=name, loader=loader, origin=path)
return importlib._bootstrap._load(spec)


import imp, sys, os
def __import_pywin32_system_module__(modname, globs):
# This has been through a number of iterations. The problem: how to
# locate pywintypesXX.dll when it may be in a number of places, and how
Expand All @@ -24,26 +17,29 @@ def __import_pywin32_system_module__(modname, globs):
# rely on a _win32sysloader module, implemented in C but not relying
# on pywintypesXX.dll. It then can check if the DLL we are looking for
# lib is already loaded.
suffixes = importlib.machinery.EXTENSION_SUFFIXES
if not sys.platform.startswith("win32"):
# These extensions can be built on Linux via the 'mainwin' toolkit.
# Look for a native 'lib{modname}.so'
# NOTE: The _win32sysloader module will probably build in this
# environment, so it may be better to use that here too.
for ext in suffixes:
for path in sys.path:
look = os.path.join(path, "lib" + modname + ext)
if os.path.isfile(look):
mod = _load_dynamic(modname, look)
# and fill our namespace with it.
# XXX - if this ever moves to py3k, this will probably
# need similar adjustments as below...
globs.update(mod.__dict__)
return
for ext, mode, ext_type in imp.get_suffixes():
if ext_type==imp.C_EXTENSION:
for path in sys.path:
look = os.path.join(path, "lib" + modname + ext)
if os.path.isfile(look):
mod = imp.load_module(modname, None, look,
(ext, mode, ext_type))
# and fill our namespace with it.
# XXX - if this ever moves to py3k, this will probably
# need similar adjustments as below...
globs.update(mod.__dict__)
return
raise ImportError("No dynamic module " + modname)
# See if this is a debug build.
if "_d.pyd" in suffixes:
suffix = "_d"
for suffix_item in imp.get_suffixes():
if suffix_item[0]=='_d.pyd':
suffix = '_d'
break
else:
suffix = ""
filename = "%s%d%d%s.dll" % \
Expand Down Expand Up @@ -124,7 +120,7 @@ def __import_pywin32_system_module__(modname, globs):
# copy its globals to ours.
old_mod = sys.modules[modname]
# Python can load the module
mod = _load_dynamic(modname, found)
mod = imp.load_dynamic(modname, found)
# Check the sys.modules[] behaviour we describe above is true...
if sys.version_info < (3,0):
assert sys.modules[modname] is old_mod
Expand Down
7 changes: 4 additions & 3 deletions win32/scripts/regsetup.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ def IsDebug():
This is to be used within DLL names when locating them.
"""
import importlib
if '_d.pyd' in importlib.machinery.EXTENSION_SUFFIXES:
return '_d'
import imp
for suffix_item in imp.get_suffixes():
if suffix_item[0]=='_d.pyd':
return '_d'
return ''

def FindPackagePath(packageName, knownFileName, searchPaths):
Expand Down

0 comments on commit c325739

Please sign in to comment.