forked from M-Labs/artiq
tools/file_import: restore sys.modules. Closes #976
This commit is contained in:
parent
843f871fd1
commit
5f3e417bb5
|
@ -7,6 +7,7 @@ import os
|
||||||
import atexit
|
import atexit
|
||||||
import string
|
import string
|
||||||
import random
|
import random
|
||||||
|
from copy import copy
|
||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
|
||||||
|
@ -84,9 +85,13 @@ def file_import(filename, prefix="file_import_"):
|
||||||
|
|
||||||
path = os.path.dirname(os.path.realpath(filename))
|
path = os.path.dirname(os.path.realpath(filename))
|
||||||
sys.path.insert(0, path)
|
sys.path.insert(0, path)
|
||||||
|
try:
|
||||||
|
previous_modules = copy(sys.modules)
|
||||||
try:
|
try:
|
||||||
loader = importlib.machinery.SourceFileLoader(modname, filename)
|
loader = importlib.machinery.SourceFileLoader(modname, filename)
|
||||||
module = loader.load_module()
|
module = loader.load_module()
|
||||||
|
finally:
|
||||||
|
sys.modules = previous_modules
|
||||||
finally:
|
finally:
|
||||||
sys.path.remove(path)
|
sys.path.remove(path)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue