pypy/module/imp/test/test_app.py::AppTestImpModule::()::test_rewrite_pyc_check_code_name
self = <pypy.interpreter.typedef.W_ObjectObjectUserDictWeakrefable object at 0x000002a37a01bbb0>
def test_rewrite_pyc_check_code_name(self):
# This one is adapted from cpython's Lib/test/test_import.py
from os import chmod
from os.path import join
from sys import modules, path
from shutil import rmtree
from tempfile import mkdtemp
code = b"""if 1:
import sys
code_filename = sys._getframe().f_code.co_filename
module_filename = __file__
constant = 1
def func():
pass
func_filename = func.__code__.co_filename
"""
module_name = "unlikely_module_name"
dir_name = mkdtemp(prefix='pypy_test')
file_name = join(dir_name, module_name + '.py')
with open(file_name, "wb") as f:
f.write(code)
compiled_name = file_name + ("c" if __debug__ else "o")
chmod(file_name, 0o777)
# Setup
sys_path = path[:]
orig_module = modules.pop(module_name, None)
assert modules.get(module_name) == None
path.insert(0, dir_name)
# Test
import py_compile
py_compile.compile(file_name, dfile="another_module.py")
__import__(module_name, globals(), locals())
mod = modules.get(module_name)
try:
# Ensure proper results
assert mod != orig_module
assert mod.module_filename == file_name
assert mod.code_filename == file_name
assert mod.func_filename == file_name
finally:
# TearDown
path[:] = sys_path
if orig_module is not None:
modules[module_name] = orig_module
else:
try:
del modules[module_name]
except KeyError:
pass
> rmtree(dir_name, True)
[d:\pypy_stuff\buildbot64\slave\own-win-x86-64\build\pypy\module\imp\test\test_app.py:156]:54:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
path = W_UnicodeObject('d:\\systemtemp\\pytest\\pypy_testt6cjm7sr')
ignore_errors = W_BoolObject(True)
onerror = <pypy.objspace.std.noneobject.W_NoneObject object at 0x000002a377885750>
def rmtree(path, ignore_errors=False, onerror=None, *, onexc=None, dir_fd=None):
"""Recursively delete a directory tree.
If dir_fd is not None, it should be a file descriptor open to a directory;
path will then be relative to that directory.
dir_fd may not be implemented on your platform.
If it is unavailable, using it will raise a NotImplementedError.
If ignore_errors is set, errors are ignored; otherwise, if onexc or
onerror is set, it is called to handle the error with arguments (func,
path, exc_info) where func is platform and implementation dependent;
path is the argument to that function that caused it to fail; and
the value of exc_info describes the exception. For onexc it is the
exception instance, and for onerror it is a tuple as returned by
sys.exc_info(). If ignore_errors is false and both onexc and
onerror are None, the exception is reraised.
onerror is deprecated and only remains for backwards compatibility.
If both onerror and onexc are set, onerror is ignored and onexc is used.
"""
sys.audit("shutil.rmtree", path, dir_fd)
if ignore_errors:
def onexc(*args):
pass
elif onerror is None and onexc is None:
def onexc(*args):
raise
elif onexc is None:
if onerror is None:
def onexc(*args):
raise
else:
# delegate to onerror
def onexc(*args):
func, path, exc = args
if exc is None:
exc_info = None, None, None
else:
exc_info = type(exc), exc, exc.__traceback__
return onerror(func, path, exc_info)
if _use_fd_functions:
# While the unsafe rmtree works fine on bytes, the fd based does not.
if isinstance(path, bytes):
path = os.fsdecode(path)
stack = [(os.lstat, dir_fd, path, None)]
try:
while stack:
_rmtree_safe_fd(stack, onexc)
finally:
# Close any file descriptors still on the stack.
while stack:
func, fd, path, entry = stack.pop()
if func is not os.close:
continue
try:
os.close(fd)
except OSError as err:
onexc(os.close, path, err)
else:
if dir_fd is not None:
raise NotImplementedError("dir_fd unavailable on this platform")
try:
if _rmtree_islink(path):
# symlinks to directories are forbidden, see bug #1669
raise OSError("Cannot call rmtree on a symbolic link")
except OSError as err:
onexc(os.path.islink, path, err)
# can't continue even if onexc hook returns
return
> return _rmtree_unsafe(path, onexc)
..\lib-python\3\shutil.py:781:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
path = W_UnicodeObject('d:\\systemtemp\\pytest\\pypy_testt6cjm7sr')
onexc = <Function onexc>
def _rmtree_unsafe(path, onexc):
def onerror(err):
onexc(os.scandir, err.filename, err)
results = os.walk(path, topdown=False, onerror=onerror, followlinks=os._walk_symlinks_as_files)
> for dirpath, dirnames, filenames in results:
..\lib-python\3\shutil.py:623:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
top = W_UnicodeObject('d:\\systemtemp\\pytest\\pypy_testt6cjm7sr')
topdown = W_BoolObject(False), onerror = <Function onerror>
followlinks = <pypy.objspace.std.objectobject.W_ObjectObject object at 0x000002a378dcd520>
def walk(top, topdown=True, onerror=None, followlinks=False):
"""Directory tree generator.
For each directory in the directory tree rooted at top (including top
itself, but excluding '.' and '..'), yields a 3-tuple
dirpath, dirnames, filenames
dirpath is a string, the path to the directory. dirnames is a list of
the names of the subdirectories in dirpath (including symlinks to directories,
and excluding '.' and '..').
filenames is a list of the names of the non-directory files in dirpath.
Note that the names in the lists are just names, with no path components.
To get a full path (which begins with top) to a file or directory in
dirpath, do os.path.join(dirpath, name).
If optional arg 'topdown' is true or not specified, the triple for a
directory is generated before the triples for any of its subdirectories
(directories are generated top down). If topdown is false, the triple
for a directory is generated after the triples for all of its
subdirectories (directories are generated bottom up).
When topdown is true, the caller can modify the dirnames list in-place
(e.g., via del or slice assignment), and walk will only recurse into the
subdirectories whose names remain in dirnames; this can be used to prune the
search, or to impose a specific order of visiting. Modifying dirnames when
topdown is false has no effect on the behavior of os.walk(), since the
directories in dirnames have already been generated by the time dirnames
itself is generated. No matter the value of topdown, the list of
subdirectories is retrieved before the tuples for the directory and its
subdirectories are generated.
By default errors from the os.scandir() call are ignored. If
optional arg 'onerror' is specified, it should be a function; it
will be called with one argument, an OSError instance. It can
report the error to continue with the walk, or raise the exception
to abort the walk. Note that the filename is available as the
filename attribute of the exception object.
By default, os.walk does not follow symbolic links to subdirectories on
systems that support them. In order to get this functionality, set the
optional argument 'followlinks' to true.
Caution: if you pass a relative pathname for top, don't change the
current working directory between resumptions of walk. walk never
changes the current directory, and assumes that the client doesn't
either.
Example:
import os
from os.path import join, getsize
for root, dirs, files in os.walk('python/Lib/xml'):
print(root, "consumes ")
print(sum(getsize(join(root, name)) for name in files), end=" ")
print("bytes in", len(files), "non-directory files")
if '__pycache__' in dirs:
dirs.remove('__pycache__') # don't visit __pycache__ directories
"""
sys.audit("os.walk", top, topdown, onerror, followlinks)
stack = [fspath(top)]
islink, join = path.islink, path.join
while stack:
top = stack.pop()
if isinstance(top, tuple):
yield top
continue
dirs = []
nondirs = []
walk_dirs = []
# We may not have read permission for top, in which case we can't
# get a list of the files the directory contains.
# We suppress the exception here, rather than blow up for a
# minor reason when (say) a thousand readable directories are still
# left to visit.
try:
scandir_it = scandir(top)
except OSError as error:
if onerror is not None:
onerror(error)
continue
cont = False
with scandir_it:
while True:
try:
try:
entry = next(scandir_it)
except StopIteration:
break
except OSError as error:
if onerror is not None:
onerror(error)
cont = True
break
try:
if followlinks is _walk_symlinks_as_files:
> is_dir = entry.is_dir(follow_symlinks=False) and not entry.is_junction()
E (application-level) AttributeError: 'posix.DirEntry' object has no attribute 'is_junction'
..\lib-python\3\os.py:388: AttributeError
builder: own-win-x86-64 build #2303+
test: pypy/module/imp/test/test_app.py::AppTestImpModule::()::test_rewrite_pyc_check_code_name