| 1 |
import errno |
| 2 |
import imp |
| 3 |
import marshal |
| 4 |
import os |
| 5 |
import py_compile |
| 6 |
import random |
| 7 |
import stat |
| 8 |
import struct |
| 9 |
import sys |
| 10 |
import unittest |
| 11 |
import textwrap |
| 12 |
import shutil |
| 13 |
|
| 14 |
from test.test_support import (unlink, TESTFN, unload, run_unittest, rmtree, |
| 15 |
is_jython, check_warnings, EnvironmentVarGuard) |
| 16 |
from test import symlink_support |
| 17 |
from test import script_helper |
| 18 |
|
| 19 |
def _files(name): |
| 20 |
return (name + os.extsep + "py", |
| 21 |
name + os.extsep + "pyc", |
| 22 |
name + os.extsep + "pyo", |
| 23 |
name + os.extsep + "pyw", |
| 24 |
name + "$py.class") |
| 25 |
|
| 26 |
def chmod_files(name): |
| 27 |
for f in _files(name): |
| 28 |
try: |
| 29 |
os.chmod(f, 0600) |
| 30 |
except OSError as exc: |
| 31 |
if exc.errno != errno.ENOENT: |
| 32 |
raise |
| 33 |
|
| 34 |
def remove_files(name): |
| 35 |
for f in _files(name): |
| 36 |
unlink(f) |
| 37 |
|
| 38 |
|
| 39 |
class ImportTests(unittest.TestCase): |
| 40 |
|
| 41 |
def tearDown(self): |
| 42 |
unload(TESTFN) |
| 43 |
setUp = tearDown |
| 44 |
|
| 45 |
def test_case_sensitivity(self): |
| 46 |
|
| 47 |
|
| 48 |
try: |
| 49 |
import RAnDoM |
| 50 |
except ImportError: |
| 51 |
pass |
| 52 |
else: |
| 53 |
self.fail("import of RAnDoM should have failed (case mismatch)") |
| 54 |
|
| 55 |
def test_double_const(self): |
| 56 |
|
| 57 |
|
| 58 |
from test import double_const |
| 59 |
|
| 60 |
def test_import(self): |
| 61 |
def test_with_extension(ext): |
| 62 |
|
| 63 |
source = TESTFN + ext |
| 64 |
pyo = TESTFN + os.extsep + "pyo" |
| 65 |
if is_jython: |
| 66 |
pyc = TESTFN + "$py.class" |
| 67 |
else: |
| 68 |
pyc = TESTFN + os.extsep + "pyc" |
| 69 |
|
| 70 |
with open(source, "w") as f: |
| 71 |
print >> f, ("# This tests Python's ability to import a", ext, |
| 72 |
"file.") |
| 73 |
a = random.randrange(1000) |
| 74 |
b = random.randrange(1000) |
| 75 |
print >> f, "a =", a |
| 76 |
print >> f, "b =", b |
| 77 |
|
| 78 |
try: |
| 79 |
mod = __import__(TESTFN) |
| 80 |
except ImportError, err: |
| 81 |
self.fail("import from %s failed: %s" % (ext, err)) |
| 82 |
else: |
| 83 |
self.assertEqual(mod.a, a, |
| 84 |
"module loaded (%s) but contents invalid" % mod) |
| 85 |
self.assertEqual(mod.b, b, |
| 86 |
"module loaded (%s) but contents invalid" % mod) |
| 87 |
finally: |
| 88 |
unlink(source) |
| 89 |
|
| 90 |
try: |
| 91 |
if not sys.dont_write_bytecode: |
| 92 |
imp.reload(mod) |
| 93 |
except ImportError, err: |
| 94 |
self.fail("import from .pyc/.pyo failed: %s" % err) |
| 95 |
finally: |
| 96 |
unlink(pyc) |
| 97 |
unlink(pyo) |
| 98 |
unload(TESTFN) |
| 99 |
|
| 100 |
sys.path.insert(0, os.curdir) |
| 101 |
try: |
| 102 |
test_with_extension(os.extsep + "py") |
| 103 |
if sys.platform.startswith("win"): |
| 104 |
for ext in [".PY", ".Py", ".pY", ".pyw", ".PYW", ".pYw"]: |
| 105 |
test_with_extension(ext) |
| 106 |
finally: |
| 107 |
del sys.path[0] |
| 108 |
|
| 109 |
@unittest.skipUnless(os.name == 'posix', |
| 110 |
"test meaningful only on posix systems") |
| 111 |
@unittest.skipIf(sys.dont_write_bytecode, |
| 112 |
"test meaningful only when writing bytecode") |
| 113 |
def test_execute_bit_not_copied(self): |
| 114 |
|
| 115 |
|
| 116 |
oldmask = os.umask(022) |
| 117 |
sys.path.insert(0, os.curdir) |
| 118 |
try: |
| 119 |
fname = TESTFN + os.extsep + "py" |
| 120 |
f = open(fname, 'w').close() |
| 121 |
os.chmod(fname, (stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH | |
| 122 |
stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH)) |
| 123 |
__import__(TESTFN) |
| 124 |
fn = fname + 'c' |
| 125 |
if not os.path.exists(fn): |
| 126 |
fn = fname + 'o' |
| 127 |
if not os.path.exists(fn): |
| 128 |
self.fail("__import__ did not result in creation of " |
| 129 |
"either a .pyc or .pyo file") |
| 130 |
s = os.stat(fn) |
| 131 |
self.assertEqual(stat.S_IMODE(s.st_mode), |
| 132 |
stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH) |
| 133 |
finally: |
| 134 |
os.umask(oldmask) |
| 135 |
remove_files(TESTFN) |
| 136 |
unload(TESTFN) |
| 137 |
del sys.path[0] |
| 138 |
|
| 139 |
@unittest.skipIf(sys.dont_write_bytecode, |
| 140 |
"test meaningful only when writing bytecode") |
| 141 |
def test_rewrite_pyc_with_read_only_source(self): |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
sys.path.insert(0, os.curdir) |
| 146 |
fname = TESTFN + os.extsep + "py" |
| 147 |
try: |
| 148 |
|
| 149 |
with open(fname, 'w') as f: |
| 150 |
f.write("x = 'original'\n") |
| 151 |
|
| 152 |
s = os.stat(fname) |
| 153 |
os.utime(fname, (s.st_atime, s.st_mtime-100000000)) |
| 154 |
os.chmod(fname, 0400) |
| 155 |
m1 = __import__(TESTFN) |
| 156 |
self.assertEqual(m1.x, 'original') |
| 157 |
|
| 158 |
os.chmod(fname, 0600) |
| 159 |
with open(fname, 'w') as f: |
| 160 |
f.write("x = 'rewritten'\n") |
| 161 |
unload(TESTFN) |
| 162 |
m2 = __import__(TESTFN) |
| 163 |
self.assertEqual(m2.x, 'rewritten') |
| 164 |
|
| 165 |
unlink(fname) |
| 166 |
unload(TESTFN) |
| 167 |
m3 = __import__(TESTFN) |
| 168 |
self.assertEqual(m3.x, 'rewritten') |
| 169 |
finally: |
| 170 |
chmod_files(TESTFN) |
| 171 |
remove_files(TESTFN) |
| 172 |
unload(TESTFN) |
| 173 |
del sys.path[0] |
| 174 |
|
| 175 |
def test_imp_module(self): |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
orig_path = os.path |
| 186 |
orig_getenv = os.getenv |
| 187 |
with EnvironmentVarGuard(): |
| 188 |
x = imp.find_module("os") |
| 189 |
new_os = imp.load_module("os", *x) |
| 190 |
self.assertIs(os, new_os) |
| 191 |
self.assertIs(orig_path, new_os.path) |
| 192 |
self.assertIsNot(orig_getenv, new_os.getenv) |
| 193 |
|
| 194 |
def test_module_with_large_stack(self, module='longlist'): |
| 195 |
|
| 196 |
filename = module + os.extsep + 'py' |
| 197 |
|
| 198 |
|
| 199 |
with open(filename, 'w+') as f: |
| 200 |
f.write('d = [\n') |
| 201 |
for i in range(65000): |
| 202 |
f.write('"",\n') |
| 203 |
f.write(']') |
| 204 |
|
| 205 |
|
| 206 |
with open(filename, 'r') as f: |
| 207 |
py_compile.compile(filename) |
| 208 |
unlink(filename) |
| 209 |
|
| 210 |
|
| 211 |
sys.path.append('') |
| 212 |
|
| 213 |
|
| 214 |
exec 'import ' + module |
| 215 |
|
| 216 |
|
| 217 |
del sys.path[-1] |
| 218 |
unlink(filename + 'c') |
| 219 |
unlink(filename + 'o') |
| 220 |
|
| 221 |
def test_failing_import_sticks(self): |
| 222 |
source = TESTFN + os.extsep + "py" |
| 223 |
with open(source, "w") as f: |
| 224 |
print >> f, "a = 1 // 0" |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
sys.path.insert(0, os.curdir) |
| 229 |
try: |
| 230 |
for i in [1, 2, 3]: |
| 231 |
self.assertRaises(ZeroDivisionError, __import__, TESTFN) |
| 232 |
self.assertNotIn(TESTFN, sys.modules, |
| 233 |
"damaged module in sys.modules on %i try" % i) |
| 234 |
finally: |
| 235 |
del sys.path[0] |
| 236 |
remove_files(TESTFN) |
| 237 |
|
| 238 |
def test_failing_reload(self): |
| 239 |
|
| 240 |
source = TESTFN + os.extsep + "py" |
| 241 |
with open(source, "w") as f: |
| 242 |
print >> f, "a = 1" |
| 243 |
print >> f, "b = 2" |
| 244 |
|
| 245 |
sys.path.insert(0, os.curdir) |
| 246 |
try: |
| 247 |
mod = __import__(TESTFN) |
| 248 |
self.assertIn(TESTFN, sys.modules) |
| 249 |
self.assertEqual(mod.a, 1, "module has wrong attribute values") |
| 250 |
self.assertEqual(mod.b, 2, "module has wrong attribute values") |
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
remove_files(TESTFN) |
| 257 |
|
| 258 |
|
| 259 |
with open(source, "w") as f: |
| 260 |
print >> f, "a = 10" |
| 261 |
print >> f, "b = 20//0" |
| 262 |
|
| 263 |
self.assertRaises(ZeroDivisionError, imp.reload, mod) |
| 264 |
|
| 265 |
|
| 266 |
mod = sys.modules.get(TESTFN) |
| 267 |
self.assertIsNot(mod, None, "expected module to be in sys.modules") |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
self.assertEqual(mod.a, 10, "module has wrong attribute values") |
| 272 |
self.assertEqual(mod.b, 2, "module has wrong attribute values") |
| 273 |
|
| 274 |
finally: |
| 275 |
del sys.path[0] |
| 276 |
remove_files(TESTFN) |
| 277 |
unload(TESTFN) |
| 278 |
|
| 279 |
def test_infinite_reload(self): |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
sys.path.insert(0, os.path.dirname(__file__)) |
| 284 |
try: |
| 285 |
import infinite_reload |
| 286 |
finally: |
| 287 |
del sys.path[0] |
| 288 |
|
| 289 |
def test_import_name_binding(self): |
| 290 |
|
| 291 |
import test as x |
| 292 |
import test.test_support |
| 293 |
self.assertIs(x, test, x.__name__) |
| 294 |
self.assertTrue(hasattr(test.test_support, "__file__")) |
| 295 |
|
| 296 |
|
| 297 |
import test.test_support as y |
| 298 |
self.assertIs(y, test.test_support, y.__name__) |
| 299 |
|
| 300 |
def test_import_initless_directory_warning(self): |
| 301 |
with check_warnings(('', ImportWarning)): |
| 302 |
|
| 303 |
|
| 304 |
self.assertRaises(ImportError, __import__, "site-packages") |
| 305 |
|
| 306 |
def test_import_by_filename(self): |
| 307 |
path = os.path.abspath(TESTFN) |
| 308 |
with self.assertRaises(ImportError) as c: |
| 309 |
__import__(path) |
| 310 |
self.assertEqual("Import by filename is not supported.", |
| 311 |
c.exception.args[0]) |
| 312 |
|
| 313 |
def test_import_in_del_does_not_crash(self): |
| 314 |
|
| 315 |
testfn = script_helper.make_script('', TESTFN, textwrap.dedent("""\ |
| 316 |
import sys |
| 317 |
class C: |
| 318 |
def __del__(self): |
| 319 |
import imp |
| 320 |
sys.argv.insert(0, C()) |
| 321 |
""")) |
| 322 |
try: |
| 323 |
script_helper.assert_python_ok(testfn) |
| 324 |
finally: |
| 325 |
unlink(testfn) |
| 326 |
|
| 327 |
def test_bug7732(self): |
| 328 |
source = TESTFN + '.py' |
| 329 |
os.mkdir(source) |
| 330 |
try: |
| 331 |
self.assertRaises((ImportError, IOError), |
| 332 |
imp.find_module, TESTFN, ["."]) |
| 333 |
finally: |
| 334 |
os.rmdir(source) |
| 335 |
|
| 336 |
def test_timestamp_overflow(self): |
| 337 |
|
| 338 |
|
| 339 |
sys.path.insert(0, os.curdir) |
| 340 |
try: |
| 341 |
source = TESTFN + ".py" |
| 342 |
compiled = source + ('c' if __debug__ else 'o') |
| 343 |
with open(source, 'w') as f: |
| 344 |
pass |
| 345 |
try: |
| 346 |
os.utime(source, (2 ** 33 - 5, 2 ** 33 - 5)) |
| 347 |
except OverflowError: |
| 348 |
self.skipTest("cannot set modification time to large integer") |
| 349 |
except OSError as e: |
| 350 |
if e.errno != getattr(errno, 'EOVERFLOW', None): |
| 351 |
raise |
| 352 |
self.skipTest("cannot set modification time to large integer ({})".format(e)) |
| 353 |
__import__(TESTFN) |
| 354 |
|
| 355 |
os.stat(compiled) |
| 356 |
finally: |
| 357 |
del sys.path[0] |
| 358 |
remove_files(TESTFN) |
| 359 |
|
| 360 |
def test_pyc_mtime(self): |
| 361 |
|
| 362 |
sys.path.insert(0, os.curdir) |
| 363 |
try: |
| 364 |
|
| 365 |
mtimes = 1325376000, 1341100800 |
| 366 |
|
| 367 |
|
| 368 |
tails = "spam", "eggs" |
| 369 |
for mtime, tail in zip(mtimes, tails): |
| 370 |
module = TESTFN + tail |
| 371 |
source = module + ".py" |
| 372 |
compiled = source + ('c' if __debug__ else 'o') |
| 373 |
|
| 374 |
|
| 375 |
with open(source, 'w') as f: |
| 376 |
f.write("# Just testing\nx=1, 2, 3\n") |
| 377 |
os.utime(source, (mtime, mtime)) |
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
m = __import__(module) |
| 382 |
if not os.path.exists(compiled): |
| 383 |
unlink(source) |
| 384 |
self.skipTest("Couldn't create .pyc/.pyo file.") |
| 385 |
|
| 386 |
|
| 387 |
mtime1 = int(os.stat(source).st_mtime) & 0xffffffff |
| 388 |
|
| 389 |
|
| 390 |
with open(compiled, 'rb') as f: |
| 391 |
mtime2 = struct.unpack('<L', f.read(8)[4:])[0] |
| 392 |
|
| 393 |
unlink(compiled) |
| 394 |
unlink(source) |
| 395 |
|
| 396 |
self.assertEqual(mtime1, mtime2) |
| 397 |
finally: |
| 398 |
sys.path.pop(0) |
| 399 |
|
| 400 |
def test_replace_parent_in_sys_modules(self): |
| 401 |
dir_name = os.path.abspath(TESTFN) |
| 402 |
os.mkdir(dir_name) |
| 403 |
self.addCleanup(rmtree, dir_name) |
| 404 |
pkg_dir = os.path.join(dir_name, 'sa') |
| 405 |
os.mkdir(pkg_dir) |
| 406 |
with open(os.path.join(pkg_dir, '__init__.py'), 'w') as init_file: |
| 407 |
init_file.write("import v1") |
| 408 |
with open(os.path.join(pkg_dir, 'v1.py'), 'w') as v1_file: |
| 409 |
v1_file.write("import sys;" |
| 410 |
"sys.modules['sa'] = sys.modules[__name__];" |
| 411 |
"import sa") |
| 412 |
sys.path.insert(0, dir_name) |
| 413 |
self.addCleanup(sys.path.pop, 0) |
| 414 |
|
| 415 |
import sa |
| 416 |
|
| 417 |
def test_fromlist_type(self): |
| 418 |
with self.assertRaises(TypeError) as cm: |
| 419 |
__import__('encodings', fromlist=[u'aliases']) |
| 420 |
self.assertIn('must be str, not unicode', str(cm.exception)) |
| 421 |
with self.assertRaises(TypeError) as cm: |
| 422 |
__import__('encodings', fromlist=[1]) |
| 423 |
self.assertIn('must be str, not int', str(cm.exception)) |
| 424 |
|
| 425 |
|
| 426 |
class PycRewritingTests(unittest.TestCase): |
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
module_name = "unlikely_module_name" |
| 432 |
module_source = """ |
| 433 |
import sys |
| 434 |
code_filename = sys._getframe().f_code.co_filename |
| 435 |
module_filename = __file__ |
| 436 |
constant = 1 |
| 437 |
def func(): |
| 438 |
pass |
| 439 |
func_filename = func.func_code.co_filename |
| 440 |
""" |
| 441 |
dir_name = os.path.abspath(TESTFN) |
| 442 |
file_name = os.path.join(dir_name, module_name) + os.extsep + "py" |
| 443 |
compiled_name = file_name + ("c" if __debug__ else "o") |
| 444 |
|
| 445 |
def setUp(self): |
| 446 |
self.sys_path = sys.path[:] |
| 447 |
self.orig_module = sys.modules.pop(self.module_name, None) |
| 448 |
os.mkdir(self.dir_name) |
| 449 |
with open(self.file_name, "w") as f: |
| 450 |
f.write(self.module_source) |
| 451 |
sys.path.insert(0, self.dir_name) |
| 452 |
|
| 453 |
def tearDown(self): |
| 454 |
sys.path[:] = self.sys_path |
| 455 |
if self.orig_module is not None: |
| 456 |
sys.modules[self.module_name] = self.orig_module |
| 457 |
else: |
| 458 |
unload(self.module_name) |
| 459 |
unlink(self.file_name) |
| 460 |
unlink(self.compiled_name) |
| 461 |
rmtree(self.dir_name) |
| 462 |
|
| 463 |
def import_module(self): |
| 464 |
ns = globals() |
| 465 |
__import__(self.module_name, ns, ns) |
| 466 |
return sys.modules[self.module_name] |
| 467 |
|
| 468 |
def test_basics(self): |
| 469 |
mod = self.import_module() |
| 470 |
self.assertEqual(mod.module_filename, self.file_name) |
| 471 |
self.assertEqual(mod.code_filename, self.file_name) |
| 472 |
self.assertEqual(mod.func_filename, self.file_name) |
| 473 |
del sys.modules[self.module_name] |
| 474 |
mod = self.import_module() |
| 475 |
if not sys.dont_write_bytecode: |
| 476 |
self.assertEqual(mod.module_filename, self.compiled_name) |
| 477 |
self.assertEqual(mod.code_filename, self.file_name) |
| 478 |
self.assertEqual(mod.func_filename, self.file_name) |
| 479 |
|
| 480 |
def test_incorrect_code_name(self): |
| 481 |
py_compile.compile(self.file_name, dfile="another_module.py") |
| 482 |
mod = self.import_module() |
| 483 |
self.assertEqual(mod.module_filename, self.compiled_name) |
| 484 |
self.assertEqual(mod.code_filename, self.file_name) |
| 485 |
self.assertEqual(mod.func_filename, self.file_name) |
| 486 |
|
| 487 |
def test_module_without_source(self): |
| 488 |
target = "another_module.py" |
| 489 |
py_compile.compile(self.file_name, dfile=target) |
| 490 |
os.remove(self.file_name) |
| 491 |
mod = self.import_module() |
| 492 |
self.assertEqual(mod.module_filename, self.compiled_name) |
| 493 |
self.assertEqual(mod.code_filename, target) |
| 494 |
self.assertEqual(mod.func_filename, target) |
| 495 |
|
| 496 |
def test_foreign_code(self): |
| 497 |
py_compile.compile(self.file_name) |
| 498 |
with open(self.compiled_name, "rb") as f: |
| 499 |
header = f.read(8) |
| 500 |
code = marshal.load(f) |
| 501 |
constants = list(code.co_consts) |
| 502 |
foreign_code = test_main.func_code |
| 503 |
pos = constants.index(1) |
| 504 |
constants[pos] = foreign_code |
| 505 |
code = type(code)(code.co_argcount, code.co_nlocals, code.co_stacksize, |
| 506 |
code.co_flags, code.co_code, tuple(constants), |
| 507 |
code.co_names, code.co_varnames, code.co_filename, |
| 508 |
code.co_name, code.co_firstlineno, code.co_lnotab, |
| 509 |
code.co_freevars, code.co_cellvars) |
| 510 |
with open(self.compiled_name, "wb") as f: |
| 511 |
f.write(header) |
| 512 |
marshal.dump(code, f) |
| 513 |
mod = self.import_module() |
| 514 |
self.assertEqual(mod.constant.co_filename, foreign_code.co_filename) |
| 515 |
|
| 516 |
|
| 517 |
class PathsTests(unittest.TestCase): |
| 518 |
path = TESTFN |
| 519 |
|
| 520 |
def setUp(self): |
| 521 |
os.mkdir(self.path) |
| 522 |
self.syspath = sys.path[:] |
| 523 |
|
| 524 |
def tearDown(self): |
| 525 |
rmtree(self.path) |
| 526 |
sys.path[:] = self.syspath |
| 527 |
|
| 528 |
|
| 529 |
def test_trailing_slash(self): |
| 530 |
with open(os.path.join(self.path, 'test_trailing_slash.py'), 'w') as f: |
| 531 |
f.write("testdata = 'test_trailing_slash'") |
| 532 |
sys.path.append(self.path+'/') |
| 533 |
mod = __import__("test_trailing_slash") |
| 534 |
self.assertEqual(mod.testdata, 'test_trailing_slash') |
| 535 |
unload("test_trailing_slash") |
| 536 |
|
| 537 |
|
| 538 |
def _test_UNC_path(self): |
| 539 |
with open(os.path.join(self.path, 'test_trailing_slash.py'), 'w') as f: |
| 540 |
f.write("testdata = 'test_trailing_slash'") |
| 541 |
|
| 542 |
path = os.path.abspath(self.path) |
| 543 |
import socket |
| 544 |
hn = socket.gethostname() |
| 545 |
drive = path[0] |
| 546 |
unc = "\\\\%s\\%s$"%(hn, drive) |
| 547 |
unc += path[2:] |
| 548 |
try: |
| 549 |
os.listdir(unc) |
| 550 |
except OSError as e: |
| 551 |
if e.errno in (errno.EPERM, errno.EACCES, errno.ENOENT): |
| 552 |
|
| 553 |
self.skipTest("cannot access administrative share %r" % (unc,)) |
| 554 |
raise |
| 555 |
sys.path.append(path) |
| 556 |
mod = __import__("test_trailing_slash") |
| 557 |
self.assertEqual(mod.testdata, 'test_trailing_slash') |
| 558 |
unload("test_trailing_slash") |
| 559 |
|
| 560 |
if sys.platform == "win32": |
| 561 |
test_UNC_path = _test_UNC_path |
| 562 |
|
| 563 |
|
| 564 |
class RelativeImportTests(unittest.TestCase): |
| 565 |
|
| 566 |
def tearDown(self): |
| 567 |
unload("test.relimport") |
| 568 |
setUp = tearDown |
| 569 |
|
| 570 |
def test_relimport_star(self): |
| 571 |
|
| 572 |
from . import relimport |
| 573 |
self.assertTrue(hasattr(relimport, "RelativeImportTests")) |
| 574 |
|
| 575 |
def test_issue3221(self): |
| 576 |
|
| 577 |
def check_absolute(): |
| 578 |
exec "from os import path" in ns |
| 579 |
def check_relative(): |
| 580 |
exec "from . import relimport" in ns |
| 581 |
|
| 582 |
|
| 583 |
ns = dict(__package__='test', __name__='test.notarealmodule') |
| 584 |
check_absolute() |
| 585 |
check_relative() |
| 586 |
|
| 587 |
|
| 588 |
ns = dict(__package__='test', __name__='notarealpkg.notarealmodule') |
| 589 |
check_absolute() |
| 590 |
check_relative() |
| 591 |
|
| 592 |
|
| 593 |
ns = dict(__package__='foo', __name__='test.notarealmodule') |
| 594 |
with check_warnings(('.+foo', RuntimeWarning)): |
| 595 |
check_absolute() |
| 596 |
self.assertRaises(SystemError, check_relative) |
| 597 |
|
| 598 |
|
| 599 |
ns = dict(__package__='foo', __name__='notarealpkg.notarealmodule') |
| 600 |
with check_warnings(('.+foo', RuntimeWarning)): |
| 601 |
check_absolute() |
| 602 |
self.assertRaises(SystemError, check_relative) |
| 603 |
|
| 604 |
|
| 605 |
ns = dict(__package__=object()) |
| 606 |
self.assertRaises(ValueError, check_absolute) |
| 607 |
self.assertRaises(ValueError, check_relative) |
| 608 |
|
| 609 |
def test_absolute_import_without_future(self): |
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
with self.assertRaises(ImportError): |
| 614 |
from .os import sep |
| 615 |
self.fail("explicit relative import triggered an " |
| 616 |
"implicit absolute import") |
| 617 |
|
| 618 |
|
| 619 |
class TestSymbolicallyLinkedPackage(unittest.TestCase): |
| 620 |
package_name = 'sample' |
| 621 |
|
| 622 |
def setUp(self): |
| 623 |
if os.path.exists(self.tagged): |
| 624 |
shutil.rmtree(self.tagged) |
| 625 |
if os.path.exists(self.package_name): |
| 626 |
symlink_support.remove_symlink(self.package_name) |
| 627 |
self.orig_sys_path = sys.path[:] |
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
os.mkdir(self.tagged) |
| 632 |
init_file = os.path.join(self.tagged, '__init__.py') |
| 633 |
open(init_file, 'w').close() |
| 634 |
assert os.path.exists(init_file) |
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
symlink_support.symlink(self.tagged, self.package_name) |
| 639 |
|
| 640 |
assert os.path.isdir(self.package_name) |
| 641 |
assert os.path.isfile(os.path.join(self.package_name, '__init__.py')) |
| 642 |
|
| 643 |
@property |
| 644 |
def tagged(self): |
| 645 |
return self.package_name + '-tagged' |
| 646 |
|
| 647 |
|
| 648 |
@unittest.skipUnless( |
| 649 |
not hasattr(sys, 'getwindowsversion') |
| 650 |
or sys.getwindowsversion() >= (6, 0), |
| 651 |
"Windows Vista or later required") |
| 652 |
@symlink_support.skip_unless_symlink |
| 653 |
def test_symlinked_dir_importable(self): |
| 654 |
|
| 655 |
sys.path[:] = ['.'] |
| 656 |
|
| 657 |
|
| 658 |
__import__(self.package_name) |
| 659 |
|
| 660 |
def tearDown(self): |
| 661 |
|
| 662 |
if os.path.exists(self.package_name): |
| 663 |
symlink_support.remove_symlink(self.package_name) |
| 664 |
if os.path.exists(self.tagged): |
| 665 |
shutil.rmtree(self.tagged) |
| 666 |
sys.path[:] = self.orig_sys_path |
| 667 |
|
| 668 |
def test_main(verbose=None): |
| 669 |
run_unittest(ImportTests, PycRewritingTests, PathsTests, |
| 670 |
RelativeImportTests, TestSymbolicallyLinkedPackage) |
| 671 |
|
| 672 |
if __name__ == '__main__': |
| 673 |
|
| 674 |
from test.test_import import test_main |
| 675 |
test_main() |