Skip to content

Commit 59f5fdd

Browse files
authored
Merge pull request #67 from justinfx/66_module_path
Fixes #66 - Also copy binding module to __name__ in sys.modules
2 parents 1c0cc64 + 12a0d21 commit 59f5fdd

File tree

2 files changed

+67
-3
lines changed

2 files changed

+67
-3
lines changed

Qt.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ def _init():
180180
this has executed.
181181
182182
"""
183-
183+
184184
preferred = os.getenv("QT_PREFERRED_BINDING")
185185
verbose = os.getenv("QT_VERBOSE") is not None
186186

@@ -202,7 +202,8 @@ def _init():
202202
raise ImportError("Preferred Qt binding \"%s\" "
203203
"not available" % preferred)
204204

205-
sys.modules["Qt"] = available[preferred]()
205+
binding = available[preferred]
206+
sys.modules[__name__] = binding()
206207
return
207208

208209
else:
@@ -215,7 +216,7 @@ def _init():
215216
sys.stdout.write("Trying %s" % binding.__name__[1:])
216217

217218
try:
218-
sys.modules["Qt"] = binding()
219+
sys.modules[__name__] = binding()
219220
return
220221

221222
except ImportError as e:

tests.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
import os
99
import sys
1010
import imp
11+
import shutil
12+
import tempfile
13+
import subprocess
1114
import contextlib
1215

1316
from nose.tools import (
@@ -92,6 +95,66 @@ def test_sip_api_qtpy():
9295
"instead is %s"
9396
% sip.getapi("QString"))
9497

98+
99+
def test_vendoring():
100+
"""Qt.py may be bundled along with another library/project
101+
102+
Create toy project
103+
104+
from project.vendor import Qt # Absolute
105+
from .vendor import Qt # Relative
106+
107+
project/
108+
vendor/
109+
__init__.py
110+
__init__.py
111+
112+
"""
113+
114+
dirname = os.path.dirname(__file__)
115+
tempdir = tempfile.mkdtemp()
116+
117+
try:
118+
project = os.path.join(tempdir, "myproject")
119+
vendor = os.path.join(tempdir, "myproject", "vendor")
120+
121+
os.makedirs(vendor)
122+
123+
# Make packages out of folders
124+
with open(os.path.join(project, "__init__.py"), "w") as f:
125+
f.write("from .vendor.Qt import QtWidgets")
126+
127+
with open(os.path.join(vendor, "__init__.py"), "w") as f:
128+
pass
129+
130+
shutil.copy(os.path.join(dirname, "Qt.py"),
131+
os.path.join(vendor, "Qt.py"))
132+
133+
print("Testing relative import..")
134+
assert subprocess.call(
135+
["python", "-c", "import myproject"],
136+
cwd=tempdir,
137+
env={}
138+
) == 0
139+
140+
print("Testing absolute import..")
141+
assert subprocess.call(
142+
["python", "-c", "from myproject.vendor.Qt import QtWidgets"],
143+
cwd=tempdir,
144+
env={}
145+
) == 0
146+
147+
print("Testing direct import..")
148+
assert subprocess.call(
149+
["python", "-c", "import myproject.vendor.Qt"],
150+
cwd=tempdir,
151+
env={}
152+
) == 0
153+
154+
finally:
155+
shutil.rmtree(tempdir)
156+
157+
95158
if PYTHON == 2:
96159
def test_sip_api_already_set():
97160
"""Raise ImportError if sip API v1 was already set (Python 2.x only)"""

0 commit comments

Comments
 (0)