-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Add namespace redirect hook for qiskit-aer #5089
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
2356149
3174149
990864b
6e896c5
0f985de
d2a93b9
32b5499
c17e0c1
fa1781e
9483708
208131e
5e493fc
e3866ea
f759582
4836642
643cbb6
90240d1
945bf7a
08cb82f
fe8f90a
c841200
1aa04ef
876c630
4cc552b
84166c0
8eb7ce7
5e43989
6ba893a
20c63c8
132c894
9e11213
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| # This code is part of Qiskit. | ||
| # | ||
| # (C) Copyright IBM 2020. | ||
| # | ||
| # This code is licensed under the Apache License, Version 2.0. You may | ||
| # obtain a copy of this license in the LICENSE.txt file in the root directory | ||
| # of this source tree or at http://www.apache.org/licenses/LICENSE-2.0. | ||
| # | ||
| # Any modifications or derivative works of this code must retain this | ||
| # copyright notice, and modified files need to carry a notice indicating | ||
| # that they have been altered from the originals. | ||
|
|
||
| # pylint: disable=unused-argument | ||
|
|
||
| """Module for utilities to manually construct qiskit namespace""" | ||
|
|
||
| import sys | ||
| from importlib.abc import MetaPathFinder, Loader | ||
| import importlib | ||
|
|
||
|
|
||
| def _new_namespace(fullname, old_namespace, new_package): | ||
| names = fullname.split(".") | ||
| new_namespace_names = new_package.split(".") | ||
| old_namespace_names = old_namespace.split(".") | ||
| fullname = ".".join(new_namespace_names + names[len(old_namespace_names) :]) | ||
| return fullname | ||
|
|
||
|
|
||
| class QiskitLoader(Loader): | ||
| """Load qiskit element as a namespace package.""" | ||
|
|
||
| def __init__(self, new_package, old_namespace): | ||
| super().__init__() | ||
| self.new_package = new_package | ||
| self.old_namespace = old_namespace | ||
|
|
||
| def module_repr(self, module): | ||
| return repr(module) | ||
|
|
||
| def load_module(self, fullname): | ||
| old_name = fullname | ||
| fullname = _new_namespace(fullname, self.old_namespace, self.new_package) | ||
| module = importlib.import_module(fullname) | ||
| sys.modules[old_name] = module | ||
| return module | ||
|
|
||
|
|
||
| class QiskitElementImport(MetaPathFinder): | ||
| """Meta importer to enable unified qiskit namespace.""" | ||
|
|
||
| def __init__(self, old_namespace, new_package): | ||
| super().__init__() | ||
| self.old_namespace = old_namespace | ||
| self.new_package = new_package | ||
|
|
||
| def find_spec(self, fullname, path=None, target=None): | ||
| """Return the ModuleSpec for Qiskit element.""" | ||
| if fullname.startswith(self.old_namespace): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suppose technically this might break some compatibility if some random package is extending the namespace with
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure namespace packaging would work for someone trying to extend
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I mean if somebody extends
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ah ok now I follow what you're saying. Yeah I should have done a proper regex match something like |
||
| try: | ||
| importlib.import_module( | ||
| _new_namespace(fullname, self.old_namespace, self.new_package) | ||
| ) | ||
| return importlib.util.spec_from_loader( | ||
| fullname, QiskitLoader(self.new_package, self.old_namespace), origin="qiskit" | ||
| ) | ||
| except ModuleNotFoundError: | ||
| return None | ||
| return None | ||
Uh oh!
There was an error while loading. Please reload this page.