Skip to content

Commit 02c2bb4

Browse files
committed
Fix get available api to run on py27, add docs
1 parent 8b9236c commit 02c2bb4

File tree

1 file changed

+29
-7
lines changed

1 file changed

+29
-7
lines changed

qtpy/__init__.py

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@
9898
9999
"""
100100

101-
import importlib.util
101+
import pkgutil
102102
import os
103103
import sys
104104
import warnings
@@ -118,7 +118,12 @@ class PythonQtWarning(Warning):
118118

119119

120120
def get_imported_api(apis_to_search):
121-
"""Return an ordered list of Qt bindings that have been already imported."""
121+
"""Return an ordered list of Qt bindings that have been already imported.
122+
123+
``apis_to_search`` is a list of importing names, case sensitive.
124+
125+
Return the same list excluding api names not imported.
126+
"""
122127
imported_api = []
123128

124129
for api_name in apis_to_search:
@@ -129,19 +134,36 @@ def get_imported_api(apis_to_search):
129134

130135

131136
def get_available_api(apis_to_search):
132-
"""Return an ordered list of Qt bindings that are available (installed)."""
137+
"""Return an ordered list of Qt bindings that are available (installed).
138+
139+
``apis_to_search`` is a list of importing names, case sensitive.
140+
141+
Return the same list excluding api names not available.
142+
"""
133143
available_api = []
134144

135145
for api_name in apis_to_search:
136-
module_spec = importlib.util.find_spec(api_name)
137-
if module_spec:
146+
# Using try...import or __import__ causes the api_name to be
147+
# imported accumulating on sys.modules.
148+
# Using pkgutil.get_loader(), that works on both py2/3
149+
# it works as expected.
150+
can_import = pkgutil.get_loader(api_name)
151+
if can_import:
138152
available_api.append(api_name)
139153

140154
return available_api
141155

142156

143157
def get_api_information(api_name):
144-
"""Get API information of version and Qt version, also"""
158+
"""Get API information of version and Qt version.
159+
160+
``api_name`` is an importing name, case sensitive.
161+
162+
Note: this function is not prepared to be called more than once.
163+
Multiple calls will accumulate imports on sys.modules if they are
164+
installed. It must be rewrite to use pkgutil/importlib to check
165+
check version numbers.
166+
"""
145167

146168
if api_name == 'PyQt4':
147169
try:
@@ -236,7 +258,7 @@ def get_api_information(api_name):
236258

237259
# If more than one Qt binding is imported, just warns for now
238260
if len(imported_api_list) >= 2:
239-
warnings.warn('There is more than one imported Qt binding {}.'
261+
warnings.warn('There is more than one imported Qt binding {}. '
240262
'This may cause some issues, check your code '
241263
'consistence'.format(imported_api_list), RuntimeWarning)
242264

0 commit comments

Comments
 (0)