Skip to content

Commit

Permalink
Update _virtualenv.py
Browse files Browse the repository at this point in the history
Signed-off-by: Bernat Gabor <bgabor8@bloomberg.net>
  • Loading branch information
gaborbernat committed Mar 6, 2020
1 parent cff98e2 commit 4dd5c46
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions src/virtualenv/create/via_global_ref/_virtualenv.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import os
import sys
from copy import deepcopy

VIRTUALENV_PATCH_FILE = os.path.join(__file__)

Expand Down Expand Up @@ -56,18 +57,26 @@ def find_spec(self, fullname, path, target=None):
spec = find_spec(fullname, path)
if spec is not None:
# https://www.python.org/dev/peps/pep-0451/#how-loading-will-work
loader = spec.loader
# https://www.python.org/dev/peps/pep-0451/#how-loading-will-work
loader = deepcopy(spec.loader) # loaders may be shared, create new that also patches
spec.loader = loader
if hasattr(loader, "exec_module"): # new API

def exec_module(module):
old(module)
patch_dist(module)
def exec_module(module):
old(module)
patch_dist(module)

if hasattr(loader, "exec_module"):
old = loader.exec_module
loader.exec_module = exec_module
else:
else: # legacy API

def load_module(name):
module = old(name)
patch_dist(module)
return module

old = loader.load_module
loader.load_module = exec_module
loader.load_module = load_module
return spec
finally:
self.fullname = None
Expand Down

0 comments on commit 4dd5c46

Please sign in to comment.