|
1 | | -# Copyright 2022 MathWorks, Inc. |
| 1 | +#Copyright 2014-2021 MathWorks, Inc. |
2 | 2 |
|
3 | 3 | """ |
4 | 4 | The MATLAB Engine enables you to call any MATLAB statement either synchronously |
|
19 | 19 | """ |
20 | 20 |
|
21 | 21 |
|
| 22 | +import os |
22 | 23 | import sys |
23 | 24 | import importlib |
24 | 25 | import atexit |
| 26 | +import weakref |
25 | 27 | import threading |
26 | | -import platform |
27 | | -import os |
28 | | - |
29 | | -package_folder = os.path.dirname(os.path.realpath(__file__)) |
30 | | - |
31 | | -def add_dirs_to_path(bin_dir, engine_dir, extern_dir): |
32 | | - """ |
33 | | - Adds MATLAB engine and extern/bin directories to sys.path. |
34 | | - """ |
35 | | - path = 'PATH' |
36 | | - |
37 | | - if not os.path.isdir(engine_dir): |
38 | | - raise RuntimeError("Could not find directory: {0}".format(engine_dir)) |
39 | | - |
40 | | - if not os.path.isdir(extern_dir): |
41 | | - raise RuntimeError("Could not find directory: {0}".format(extern_dir)) |
42 | | - |
43 | | - if platform.system() == 'Windows': |
44 | | - if not os.path.isdir(bin_dir): |
45 | | - raise RuntimeError("Could not find directory: {0}".format(bin_dir)) |
46 | | - if path in os.environ: |
47 | | - paths = os.environ[path] |
48 | | - os.environ[path] = bin_dir + os.pathsep + paths |
49 | | - else: |
50 | | - os.environ[path] = bin_dir |
51 | | - if sys.version_info.major >= 3 and sys.version_info.minor >= 8: |
52 | | - os.add_dll_directory(bin_dir) |
53 | | - |
54 | | - sys.path.insert(0, engine_dir) |
55 | | - sys.path.insert(0, extern_dir) |
56 | 28 |
|
57 | | -# MUST_BE_UPDATED_EACH_RELEASE (Search repo for this string) |
58 | | -_supported_versions = set(['3_7', '3_8', '3_9']) |
| 29 | +# UPDATE_IF_PYTHON_VERSION_ADDED_OR_REMOVED : search for this string in codebase |
| 30 | +# when support for a Python version must be added or removed |
| 31 | +_supported_versions = ['2_7', '3_7', '3_8', '3_9'] |
59 | 32 | _ver = sys.version_info |
60 | 33 | _version = '{0}_{1}'.format(_ver[0], _ver[1]) |
61 | | -if _version not in _supported_versions: |
62 | | - raise RuntimeError("Python {0}.{1} is not supported. Supported versions " + |
63 | | - 'are {2}.'.format(_ver[0], _ver[1, _supported_versions])) |
64 | | - |
65 | | -first_exception_message = '' |
66 | | -second_exception_message = '' |
| 34 | +_PYTHONVERSION = None |
| 35 | + |
| 36 | +if _version in _supported_versions: |
| 37 | + _PYTHONVERSION = _version |
| 38 | +else: |
| 39 | + raise EnvironmentError("Python %s is not supported." % _version) |
| 40 | + |
| 41 | +_module_folder = os.path.dirname(os.path.realpath(__file__)) |
| 42 | +_arch_filename = os.path.join(_module_folder, "_arch.txt") |
| 43 | +success = False |
| 44 | +firstExceptionMessage = '' |
| 45 | +secondExceptionMessage = '' |
67 | 46 | try: |
68 | | - pythonengine = importlib.import_module("matlabengineforpython"+_version) |
69 | | -except Exception as first_error: |
70 | | - first_exception_message = str(first_error) |
| 47 | + pythonengine = importlib.import_module("matlabengineforpython"+_PYTHONVERSION) |
| 48 | +except Exception as firstE: |
| 49 | + firstExceptionMessage = str(firstE) |
71 | 50 |
|
72 | | -if first_exception_message: |
| 51 | +if firstExceptionMessage: |
73 | 52 | try: |
74 | | - arch_file = os.path.join(package_folder, '_arch.txt') |
75 | | - with open(arch_file, 'r') as root: |
76 | | - [arch, bin_folder, engine_folder, extern_bin] = [line.strip() for line in root.readlines()] |
77 | | - |
78 | | - add_dirs_to_path(bin_folder, engine_folder, extern_bin) |
79 | | - pythonengine = importlib.import_module("matlabengineforpython"+_version) |
80 | | - |
81 | | - except Exception as second_error: |
| 53 | + _arch_file = open(_arch_filename,'r') |
| 54 | + _lines = _arch_file.readlines() |
| 55 | + [_arch, _bin_dir,_engine_dir, _extern_bin_dir] = [x.rstrip() for x in _lines if x.rstrip() != ""] |
| 56 | + _arch_file.close() |
| 57 | + sys.path.insert(0,_engine_dir) |
| 58 | + sys.path.insert(0,_extern_bin_dir) |
| 59 | + |
| 60 | + _envs = {'win32': 'PATH', 'win64': 'PATH'} |
| 61 | + if _arch in _envs: |
| 62 | + if _envs[_arch] in os.environ: |
| 63 | + _env = os.environ[_envs[_arch]] |
| 64 | + os.environ[_envs[_arch]] = _bin_dir + os.pathsep + os.environ[_envs[_arch]] |
| 65 | + else: |
| 66 | + os.environ[_envs[_arch]] = _bin_dir |
| 67 | + if sys.version_info.major >= 3 and sys.version_info.minor >= 8: |
| 68 | + os.add_dll_directory(_bin_dir) |
| 69 | + pythonengine = importlib.import_module("matlabengineforpython"+_PYTHONVERSION) |
| 70 | + except Exception as secondE: |
82 | 71 | str1 = 'Please reinstall MATLAB Engine for Python or contact ' |
83 | 72 | str2 = 'MathWorks Technical Support for assistance:\nFirst issue: {}\nSecond issue: {}'.format( |
84 | | - first_exception_message, second_error) |
85 | | - second_exception_message = str1 + str2 |
| 73 | + firstExceptionMessage, secondE) |
| 74 | + secondExceptionMessage = str1 + str2 |
| 75 | + |
| 76 | +if secondExceptionMessage: |
| 77 | + raise EnvironmentError(secondExceptionMessage) |
86 | 78 |
|
87 | | -if second_exception_message: |
88 | | - raise EnvironmentError(second_exception_message) |
89 | 79 |
|
90 | 80 | """ |
91 | 81 | This lock can make sure the global variable _engines is updated correctly in |
|
0 commit comments