diff --git a/LICENSE b/LICENSE index 2e64b704307b..549ff6575611 100644 --- a/LICENSE +++ b/LICENSE @@ -26,8 +26,8 @@ DEALINGS IN THE SOFTWARE. = = = = = -Portions of mypy and mypyc are licensed under different licenses. The -files under stdlib-samples as well as the files +Portions of mypy and mypyc are licensed under different licenses. +The files mypyc/lib-rt/pythonsupport.h, mypyc/lib-rt/getargs.c and mypyc/lib-rt/getargsfast.c are licensed under the PSF 2 License, reproduced below. diff --git a/mypy/test/testsamples.py b/mypy/test/testsamples.py deleted file mode 100644 index 27b26af16f36..000000000000 --- a/mypy/test/testsamples.py +++ /dev/null @@ -1,35 +0,0 @@ -import os.path -from typing import List, Set - -from mypy.test.helpers import Suite, run_mypy - - -class SamplesSuite(Suite): - """Test that we can type check some sample code.""" - - def test_samples(self) -> None: - for f in find_files(os.path.join('test-data', 'samples'), suffix='.py'): - mypy_args = ['--no-strict-optional'] - if f == os.path.join('test-data', 'samples', 'crawl2.py'): - # This test requires 3.5 for async functions - mypy_args.append('--python-version=3.5') - run_mypy(mypy_args + [f]) - - def test_stdlibsamples(self) -> None: - seen: Set[str] = set() - stdlibsamples_dir = os.path.join('test-data', 'stdlib-samples', '3.2', 'test') - modules: List[str] = [] - for f in find_files(stdlibsamples_dir, prefix='test_', suffix='.py'): - if f not in seen: - seen.add(f) - modules.append(f) - if modules: - # TODO: Remove need for --no-strict-optional - run_mypy(['--no-strict-optional', '--platform=linux'] + modules) - - -def find_files(base: str, prefix: str = '', suffix: str = '') -> List[str]: - return [os.path.join(root, f) - for root, dirs, files in os.walk(base) - for f in files - if f.startswith(prefix) and f.endswith(suffix)] diff --git a/test-data/samples/bottles.py b/test-data/samples/bottles.py deleted file mode 100644 index ddf77f59eaa0..000000000000 --- a/test-data/samples/bottles.py +++ /dev/null @@ -1,13 +0,0 @@ -import typing - -REFRAIN = ''' -%d bottles of beer on the wall, -%d bottles of beer, -take one down, pass it around, -%d bottles of beer on the wall! -''' -bottles_of_beer = 99 -while bottles_of_beer > 1: - print(REFRAIN % (bottles_of_beer, bottles_of_beer, - bottles_of_beer - 1)) - bottles_of_beer -= 1 diff --git a/test-data/samples/class.py b/test-data/samples/class.py deleted file mode 100644 index d2eb4ac0516f..000000000000 --- a/test-data/samples/class.py +++ /dev/null @@ -1,18 +0,0 @@ -import typing - - -class BankAccount(object): - def __init__(self, initial_balance: int = 0) -> None: - self.balance = initial_balance - - def deposit(self, amount: int) -> None: - self.balance += amount - - def withdraw(self, amount: int) -> None: - self.balance -= amount - - def overdrawn(self) -> bool: - return self.balance < 0 -my_account = BankAccount(15) -my_account.withdraw(5) -print(my_account.balance) diff --git a/test-data/samples/cmdline.py b/test-data/samples/cmdline.py deleted file mode 100644 index 105c27a305b9..000000000000 --- a/test-data/samples/cmdline.py +++ /dev/null @@ -1,8 +0,0 @@ -# This program adds up integers in the command line -import sys -import typing -try: - total = sum(int(arg) for arg in sys.argv[1:]) - print('sum =', total) -except ValueError: - print('Please supply integer arguments') diff --git a/test-data/samples/crawl2.py b/test-data/samples/crawl2.py deleted file mode 100644 index 28b19f38c7c5..000000000000 --- a/test-data/samples/crawl2.py +++ /dev/null @@ -1,852 +0,0 @@ -#!/usr/bin/env python3.4 - -"""A simple web crawler.""" - -# This is cloned from /examples/crawl.py, -# with type annotations added (PEP 484). -# -# This version (crawl2.) has also been converted to use `async def` + -# `await` (PEP 492). - -import argparse -import asyncio -import cgi -from http.client import BadStatusLine -import logging -import re -import sys -import time -import urllib.parse -from typing import Any, Awaitable, IO, Optional, Sequence, Set, Tuple, List, Dict - - -ARGS = argparse.ArgumentParser(description="Web crawler") -ARGS.add_argument( - '--iocp', action='store_true', dest='iocp', - default=False, help='Use IOCP event loop (Windows only)') -ARGS.add_argument( - '--select', action='store_true', dest='select', - default=False, help='Use Select event loop instead of default') -ARGS.add_argument( - 'roots', nargs='*', - default=[], help='Root URL (may be repeated)') -ARGS.add_argument( - '--max_redirect', action='store', type=int, metavar='N', - default=10, help='Limit redirection chains (for 301, 302 etc.)') -ARGS.add_argument( - '--max_tries', action='store', type=int, metavar='N', - default=4, help='Limit retries on network errors') -ARGS.add_argument( - '--max_tasks', action='store', type=int, metavar='N', - default=100, help='Limit concurrent connections') -ARGS.add_argument( - '--max_pool', action='store', type=int, metavar='N', - default=100, help='Limit connection pool size') -ARGS.add_argument( - '--exclude', action='store', metavar='REGEX', - help='Exclude matching URLs') -ARGS.add_argument( - '--strict', action='store_true', - default=True, help='Strict host matching (default)') -ARGS.add_argument( - '--lenient', action='store_false', dest='strict', - default=False, help='Lenient host matching') -ARGS.add_argument( - '-v', '--verbose', action='count', dest='level', - default=1, help='Verbose logging (repeat for more verbose)') -ARGS.add_argument( - '-q', '--quiet', action='store_const', const=0, dest='level', - default=1, help='Quiet logging (opposite of --verbose)') - - -ESCAPES = [('quot', '"'), - ('gt', '>'), - ('lt', '<'), - ('amp', '&') # Must be last. - ] - - -def unescape(url: str) -> str: - """Turn & into &, and so on. - - This is the inverse of cgi.escape(). - """ - for name, char in ESCAPES: - url = url.replace('&' + name + ';', char) - return url - - -def fix_url(url: str) -> str: - """Prefix a schema-less URL with http://.""" - if '://' not in url: - url = 'http://' + url - return url - - -class Logger: - - def __init__(self, level: int) -> None: - self.level = level - - def _log(self, n: int, args: Sequence[Any]) -> None: - if self.level >= n: - print(*args, file=sys.stderr, flush=True) - - def log(self, n: int, *args: Any) -> None: - self._log(n, args) - - def __call__(self, n: int, *args: Any) -> None: - self._log(n, args) - - -KeyTuple = Tuple[str, int, bool] - - -class ConnectionPool: - """A connection pool. - - To open a connection, use reserve(). To recycle it, use unreserve(). - - The pool is mostly just a mapping from (host, port, ssl) tuples to - lists of Connections. The currently active connections are *not* - in the data structure; get_connection() takes the connection out, - and recycle_connection() puts it back in. To recycle a - connection, call conn.close(recycle=True). - - There are limits to both the overall pool and the per-key pool. - """ - - def __init__(self, log: Logger, max_pool: int = 10, max_tasks: int = 5) -> None: - self.log = log - self.max_pool = max_pool # Overall limit. - self.max_tasks = max_tasks # Per-key limit. - self.loop = asyncio.get_event_loop() - self.connections = {} # type: Dict[KeyTuple, List[Connection]] - self.queue = [] # type: List[Connection] - - def close(self) -> None: - """Close all connections available for reuse.""" - for conns in self.connections.values(): - for conn in conns: - conn.close() - self.connections.clear() - self.queue.clear() - - async def get_connection(self, host: str, port: int, ssl: bool) -> 'Connection': - """Create or reuse a connection.""" - port = port or (443 if ssl else 80) - try: - ipaddrs = await self.loop.getaddrinfo(host, port) - except Exception as exc: - self.log(0, 'Exception %r for (%r, %r)' % (exc, host, port)) - raise - self.log(1, '* %s resolves to %s' % - (host, ', '.join(ip[4][0] for ip in ipaddrs))) - - # Look for a reusable connection. - for _1, _2, _3, _4, (h, p, *_5) in ipaddrs: - key = h, p, ssl - conn = None - conns = self.connections.get(key) - while conns: - conn = conns.pop(0) - self.queue.remove(conn) - if not conns: - del self.connections[key] - if conn.stale(): - self.log(1, 'closing stale connection for', key) - conn.close() # Just in case. - else: - self.log(1, '* Reusing pooled connection', key, - 'FD =', conn.fileno()) - return conn - - # Create a new connection. - conn = Connection(self.log, self, host, port, ssl) - await conn.connect() - self.log(1, '* New connection', conn.key, 'FD =', conn.fileno()) - return conn - - def recycle_connection(self, conn: 'Connection') -> None: - """Make a connection available for reuse. - - This also prunes the pool if it exceeds the size limits. - """ - if conn.stale(): - conn.close() - return - - key = conn.key - conns = self.connections.setdefault(key, []) - conns.append(conn) - self.queue.append(conn) - - if len(conns) <= self.max_tasks and len(self.queue) <= self.max_pool: - return - - # Prune the queue. - - # Close stale connections for this key first. - stale = [conn for conn in conns if conn.stale()] - if stale: - for conn in stale: - conns.remove(conn) - self.queue.remove(conn) - self.log(1, 'closing stale connection for', key) - conn.close() - if not conns: - del self.connections[key] - - # Close oldest connection(s) for this key if limit reached. - while len(conns) > self.max_tasks: - conn = conns.pop(0) - self.queue.remove(conn) - self.log(1, 'closing oldest connection for', key) - conn.close() - - if len(self.queue) <= self.max_pool: - return - - # Close overall stale connections. - stale = [conn for conn in self.queue if conn.stale()] - if stale: - for conn in stale: - conns = self.connections.get(conn.key) - conns.remove(conn) - self.queue.remove(conn) - self.log(1, 'closing stale connection for', key) - conn.close() - - # Close oldest overall connection(s) if limit reached. - while len(self.queue) > self.max_pool: - conn = self.queue.pop(0) - conns = self.connections.get(conn.key) - c = conns.pop(0) - assert conn == c, (conn.key, conn, c, conns) - self.log(1, 'closing overall oldest connection for', conn.key) - conn.close() - - -class Connection: - - def __init__(self, log: Logger, pool: ConnectionPool, host: str, port: int, ssl: bool) -> None: - self.log = log - self.pool = pool - self.host = host - self.port = port - self.ssl = ssl - self.reader = None # type: asyncio.StreamReader - self.writer = None # type: asyncio.StreamWriter - self.key = None # type: KeyTuple - - def stale(self) -> bool: - return self.reader is None or self.reader.at_eof() - - def fileno(self) -> Optional[int]: - writer = self.writer - if writer is not None: - transport = writer.transport - if transport is not None: - sock = transport.get_extra_info('socket') - if sock is not None: - return sock.fileno() - return None - - async def connect(self) -> None: - self.reader, self.writer = await asyncio.open_connection( - self.host, self.port, ssl=self.ssl) - peername = self.writer.get_extra_info('peername') - if peername: - self.host, self.port = peername[:2] - else: - self.log(1, 'NO PEERNAME???', self.host, self.port, self.ssl) - self.key = self.host, self.port, self.ssl - - def close(self, recycle: bool = False) -> None: - if recycle and not self.stale(): - self.pool.recycle_connection(self) - else: - self.writer.close() - self.pool = self.reader = self.writer = None - - -class Request: - """HTTP request. - - Use connect() to open a connection; send_request() to send the - request; get_response() to receive the response headers. - """ - - def __init__(self, log: Logger, url: str, pool: ConnectionPool) -> None: - self.log = log - self.url = url - self.pool = pool - self.parts = urllib.parse.urlparse(self.url) - self.scheme = self.parts.scheme - assert self.scheme in ('http', 'https'), repr(url) - self.ssl = self.parts.scheme == 'https' - self.netloc = self.parts.netloc - self.hostname = self.parts.hostname - self.port = self.parts.port or (443 if self.ssl else 80) - self.path = (self.parts.path or '/') - self.query = self.parts.query - if self.query: - self.full_path = '%s?%s' % (self.path, self.query) - else: - self.full_path = self.path - self.http_version = 'HTTP/1.1' - self.method = 'GET' - self.headers = [] # type: List[Tuple[str, str]] - self.conn = None # type: Connection - - async def connect(self) -> None: - """Open a connection to the server.""" - self.log(1, '* Connecting to %s:%s using %s for %s' % - (self.hostname, self.port, - 'ssl' if self.ssl else 'tcp', - self.url)) - self.conn = await self.pool.get_connection(self.hostname, - self.port, self.ssl) - - def close(self, recycle: bool = False) -> None: - """Close the connection, recycle if requested.""" - if self.conn is not None: - if not recycle: - self.log(1, 'closing connection for', self.conn.key) - self.conn.close(recycle) - self.conn = None - - async def putline(self, line: str) -> None: - """Write a line to the connection. - - Used for the request line and headers. - """ - self.log(2, '>', line) - self.conn.writer.write(line.encode('latin-1') + b'\r\n') - - async def send_request(self) -> None: - """Send the request.""" - request_line = '%s %s %s' % (self.method, self.full_path, - self.http_version) - await self.putline(request_line) - # TODO: What if a header is already set? - self.headers.append(('User-Agent', 'asyncio-example-crawl/0.0')) - self.headers.append(('Host', self.netloc)) - self.headers.append(('Accept', '*/*')) - # self.headers.append(('Accept-Encoding', 'gzip')) - for key, value in self.headers: - line = '%s: %s' % (key, value) - await self.putline(line) - await self.putline('') - - async def get_response(self) -> 'Response': - """Receive the response.""" - response = Response(self.log, self.conn.reader) - await response.read_headers() - return response - - -class Response: - """HTTP response. - - Call read_headers() to receive the request headers. Then check - the status attribute and call get_header() to inspect the headers. - Finally call read() to receive the body. - """ - - def __init__(self, log: Logger, reader: asyncio.StreamReader) -> None: - self.log = log - self.reader = reader - self.http_version = None # type: str # 'HTTP/1.1' - self.status = None # type: int # 200 - self.reason = None # type: str # 'Ok' - self.headers = [] # type: List[Tuple[str, str]] # [('Content-Type', 'text/html')] - - async def getline(self) -> str: - """Read one line from the connection.""" - line = (await self.reader.readline()).decode('latin-1').rstrip() - self.log(2, '<', line) - return line - - async def read_headers(self) -> None: - """Read the response status and the request headers.""" - status_line = await self.getline() - status_parts = status_line.split(None, 2) - if len(status_parts) != 3: - self.log(0, 'bad status_line', repr(status_line)) - raise BadStatusLine(status_line) - self.http_version, status, self.reason = status_parts - self.status = int(status) - while True: - header_line = await self.getline() - if not header_line: - break - # TODO: Continuation lines. - key, value = header_line.split(':', 1) - self.headers.append((key, value.strip())) - - def get_redirect_url(self, default: str = '') -> str: - """Inspect the status and return the redirect url if appropriate.""" - if self.status not in (300, 301, 302, 303, 307): - return default - return self.get_header('Location', default) - - def get_header(self, key: str, default: str = '') -> str: - """Get one header value, using a case insensitive header name.""" - key = key.lower() - for k, v in self.headers: - if k.lower() == key: - return v - return default - - async def read(self) -> bytes: - """Read the response body. - - This honors Content-Length and Transfer-Encoding: chunked. - """ - nbytes = None - for key, value in self.headers: - if key.lower() == 'content-length': - nbytes = int(value) - break - if nbytes is None: - if self.get_header('transfer-encoding').lower() == 'chunked': - self.log(2, 'parsing chunked response') - blocks = [] - while True: - size_header = await self.reader.readline() - if not size_header: - self.log(0, 'premature end of chunked response') - break - self.log(3, 'size_header =', repr(size_header)) - parts = size_header.split(b';') - size = int(parts[0], 16) - if size: - self.log(3, 'reading chunk of', size, 'bytes') - block = await self.reader.readexactly(size) - assert len(block) == size, (len(block), size) - blocks.append(block) - crlf = await self.reader.readline() - assert crlf == b'\r\n', repr(crlf) - if not size: - break - body = b''.join(blocks) - self.log(1, 'chunked response had', len(body), - 'bytes in', len(blocks), 'blocks') - else: - self.log(3, 'reading until EOF') - body = await self.reader.read() - # TODO: Should make sure not to recycle the connection - # in this case. - else: - body = await self.reader.readexactly(nbytes) - return body - - -class Fetcher: - """Logic and state for one URL. - - When found in crawler.busy, this represents a URL to be fetched or - in the process of being fetched; when found in crawler.done, this - holds the results from fetching it. - - This is usually associated with a task. This references the - crawler for the connection pool and to add more URLs to its todo - list. - - Call fetch() to do the fetching, then report() to print the results. - """ - - def __init__(self, log: Logger, url: str, crawler: 'Crawler', - max_redirect: int = 10, max_tries: int = 4) -> None: - self.log = log - self.url = url - self.crawler = crawler - # We don't loop resolving redirects here -- we just use this - # to decide whether to add the redirect URL to crawler.todo. - self.max_redirect = max_redirect - # But we do loop to retry on errors a few times. - self.max_tries = max_tries - # Everything we collect from the response goes here. - self.task = None # type: asyncio.Task - self.exceptions = [] # type: List[Exception] - self.tries = 0 - self.request = None # type: Request - self.response = None # type: Response - self.body = None # type: bytes - self.next_url = None # type: str - self.ctype = None # type: str - self.pdict = None # type: Dict[str, str] - self.encoding = None # type: str - self.urls = None # type: Set[str] - self.new_urls = None # type: Set[str] - - async def fetch(self) -> None: - """Attempt to fetch the contents of the URL. - - If successful, and the data is HTML, extract further links and - add them to the crawler. Redirects are also added back there. - """ - while self.tries < self.max_tries: - self.tries += 1 - self.request = None - try: - self.request = Request(self.log, self.url, self.crawler.pool) - await self.request.connect() - await self.request.send_request() - self.response = await self.request.get_response() - self.body = await self.response.read() - h_conn = self.response.get_header('connection').lower() - if h_conn != 'close': - self.request.close(recycle=True) - self.request = None - if self.tries > 1: - self.log(1, 'try', self.tries, 'for', self.url, 'success') - break - except (BadStatusLine, OSError) as exc: - self.exceptions.append(exc) - self.log(1, 'try', self.tries, 'for', self.url, - 'raised', repr(exc)) - # import pdb; pdb.set_trace() - # Don't reuse the connection in this case. - finally: - if self.request is not None: - self.request.close() - else: - # We never broke out of the while loop, i.e. all tries failed. - self.log(0, 'no success for', self.url, - 'in', self.max_tries, 'tries') - return - next_url = self.response.get_redirect_url() - if next_url: - self.next_url = urllib.parse.urljoin(self.url, next_url) - if self.max_redirect > 0: - self.log(1, 'redirect to', self.next_url, 'from', self.url) - self.crawler.add_url(self.next_url, self.max_redirect - 1) - else: - self.log(0, 'redirect limit reached for', self.next_url, - 'from', self.url) - else: - if self.response.status == 200: - self.ctype = self.response.get_header('content-type') - self.pdict = {} - if self.ctype: - self.ctype, self.pdict = cgi.parse_header(self.ctype) - self.encoding = self.pdict.get('charset', 'utf-8') - if self.ctype == 'text/html': - body = self.body.decode(self.encoding, 'replace') - # Replace href with (?:href|src) to follow image links. - self.urls = set(re.findall(r'(?i)href=["\']?([^\s"\'<>]+)', - body)) - if self.urls: - self.log(1, 'got', len(self.urls), - 'distinct urls from', self.url) - self.new_urls = set() - for url in self.urls: - url = unescape(url) - url = urllib.parse.urljoin(self.url, url) - url, frag = urllib.parse.urldefrag(url) - if self.crawler.add_url(url): - self.new_urls.add(url) - - def report(self, stats: 'Stats', file: IO[str] = None) -> None: - """Print a report on the state for this URL. - - Also update the Stats instance. - """ - if self.task is not None: - if not self.task.done(): - stats.add('pending') - print(self.url, 'pending', file=file) - return - elif self.task.cancelled(): - stats.add('cancelled') - print(self.url, 'cancelled', file=file) - return - elif self.task.exception(): - stats.add('exception') - exc = self.task.exception() - stats.add('exception_' + exc.__class__.__name__) - print(self.url, exc, file=file) - return - if len(self.exceptions) == self.tries: - stats.add('fail') - exc = self.exceptions[-1] - stats.add('fail_' + str(exc.__class__.__name__)) - print(self.url, 'error', exc, file=file) - elif self.next_url: - stats.add('redirect') - print(self.url, self.response.status, 'redirect', self.next_url, - file=file) - elif self.ctype == 'text/html': - stats.add('html') - size = len(self.body or b'') - stats.add('html_bytes', size) - if self.log.level: - print(self.url, self.response.status, - self.ctype, self.encoding, - size, - '%d/%d' % (len(self.new_urls or ()), len(self.urls or ())), - file=file) - elif self.response is None: - print(self.url, 'no response object') - else: - size = len(self.body or b'') - if self.response.status == 200: - stats.add('other') - stats.add('other_bytes', size) - else: - stats.add('error') - stats.add('error_bytes', size) - stats.add('status_%s' % self.response.status) - print(self.url, self.response.status, - self.ctype, self.encoding, - size, - file=file) - - -class Stats: - """Record stats of various sorts.""" - - def __init__(self) -> None: - self.stats = {} # type: Dict[str, int] - - def add(self, key: str, count: int = 1) -> None: - self.stats[key] = self.stats.get(key, 0) + count - - def report(self, file: IO[str] = None) -> None: - for key, count in sorted(self.stats.items()): - print('%10d' % count, key, file=file) - - -class Crawler: - """Crawl a set of URLs. - - This manages three disjoint sets of URLs (todo, busy, done). The - data structures actually store dicts -- the values in todo give - the redirect limit, while the values in busy and done are Fetcher - instances. - """ - def __init__(self, log: Logger, - roots: Set[str], exclude: str = None, strict: bool = True, # What to crawl. - max_redirect: int = 10, max_tries: int = 4, # Per-url limits. - max_tasks: int = 10, max_pool: int = 10, # Global limits. - ) -> None: - self.log = log - self.roots = roots - self.exclude = exclude - self.strict = strict - self.max_redirect = max_redirect - self.max_tries = max_tries - self.max_tasks = max_tasks - self.max_pool = max_pool - self.todo = {} # type: Dict[str, int] - self.busy = {} # type: Dict[str, Fetcher] - self.done = {} # type: Dict[str, Fetcher] - self.pool = ConnectionPool(self.log, max_pool, max_tasks) - self.root_domains = set() # type: Set[str] - for root in roots: - host = urllib.parse.urlparse(root).hostname - if not host: - continue - if re.match(r'\A[\d\.]*\Z', host): - self.root_domains.add(host) - else: - host = host.lower() - if self.strict: - self.root_domains.add(host) - if host.startswith('www.'): - self.root_domains.add(host[4:]) - else: - self.root_domains.add('www.' + host) - else: - parts = host.split('.') - if len(parts) > 2: - host = '.'.join(parts[-2:]) - self.root_domains.add(host) - for root in roots: - self.add_url(root) - self.governor = asyncio.Semaphore(max_tasks) - self.termination = asyncio.Condition() - self.t0 = time.time() - self.t1 = None # type: Optional[float] - - def close(self) -> None: - """Close resources (currently only the pool).""" - self.pool.close() - - def host_okay(self, host: str) -> bool: - """Check if a host should be crawled. - - A literal match (after lowercasing) is always good. For hosts - that don't look like IP addresses, some approximate matches - are okay depending on the strict flag. - """ - host = host.lower() - if host in self.root_domains: - return True - if re.match(r'\A[\d\.]*\Z', host): - return False - if self.strict: - return self._host_okay_strictish(host) - else: - return self._host_okay_lenient(host) - - def _host_okay_strictish(self, host: str) -> bool: - """Check if a host should be crawled, strict-ish version. - - This checks for equality modulo an initial 'www.' component. - """ - if host.startswith('www.'): - if host[4:] in self.root_domains: - return True - else: - if 'www.' + host in self.root_domains: - return True - return False - - def _host_okay_lenient(self, host: str) -> bool: - """Check if a host should be crawled, lenient version. - - This compares the last two components of the host. - """ - parts = host.split('.') - if len(parts) > 2: - host = '.'.join(parts[-2:]) - return host in self.root_domains - - def add_url(self, url: str, max_redirect: int = None) -> bool: - """Add a URL to the todo list if not seen before.""" - if self.exclude and re.search(self.exclude, url): - return False - parsed = urllib.parse.urlparse(url) - if parsed.scheme not in ('http', 'https'): - self.log(2, 'skipping non-http scheme in', url) - return False - host = parsed.hostname - if not self.host_okay(host): - self.log(2, 'skipping non-root host in', url) - return False - if max_redirect is None: - max_redirect = self.max_redirect - if url in self.todo or url in self.busy or url in self.done: - return False - self.log(1, 'adding', url, max_redirect) - self.todo[url] = max_redirect - return True - - async def crawl(self) -> None: - """Run the crawler until all finished.""" - with (await self.termination): - while self.todo or self.busy: - if self.todo: - url, max_redirect = self.todo.popitem() - fetcher = Fetcher(self.log, url, - crawler=self, - max_redirect=max_redirect, - max_tries=self.max_tries, - ) - self.busy[url] = fetcher - fetcher.task = asyncio.Task(self.fetch(fetcher)) - else: - await self.termination.wait() - self.t1 = time.time() - - async def fetch(self, fetcher: Fetcher) -> None: - """Call the Fetcher's fetch(), with a limit on concurrency. - - Once this returns, move the fetcher from busy to done. - """ - url = fetcher.url - with (await self.governor): - try: - await fetcher.fetch() # Fetcher gonna fetch. - finally: - # Force GC of the task, so the error is logged. - fetcher.task = None - with (await self.termination): - self.done[url] = fetcher - del self.busy[url] - self.termination.notify() - - def report(self, file: IO[str] = None) -> None: - """Print a report on all completed URLs.""" - if self.t1 is None: - self.t1 = time.time() - dt = self.t1 - self.t0 - if dt and self.max_tasks: - speed = len(self.done) / dt / self.max_tasks - else: - speed = 0 - stats = Stats() - print('*** Report ***', file=file) - try: - show = [] # type: List[Tuple[str, Fetcher]] - show.extend(self.done.items()) - show.extend(self.busy.items()) - show.sort() - for url, fetcher in show: - fetcher.report(stats, file=file) - except KeyboardInterrupt: - print('\nInterrupted', file=file) - print('Finished', len(self.done), - 'urls in %.3f secs' % dt, - '(max_tasks=%d)' % self.max_tasks, - '(%.3f urls/sec/task)' % speed, - file=file) - stats.report(file=file) - print('Todo:', len(self.todo), file=file) - print('Busy:', len(self.busy), file=file) - print('Done:', len(self.done), file=file) - print('Date:', time.ctime(), 'local time', file=file) - - -def main() -> None: - """Main program. - - Parse arguments, set up event loop, run crawler, print report. - """ - args = ARGS.parse_args() - if not args.roots: - print('Use --help for command line help') - return - - log = Logger(args.level) - - if args.iocp: - if sys.platform == 'win32': - from asyncio import ProactorEventLoop - loop = ProactorEventLoop() # type: ignore - asyncio.set_event_loop(loop) - else: - assert False - elif args.select: - loop = asyncio.SelectorEventLoop() # type: ignore - asyncio.set_event_loop(loop) - else: - loop = asyncio.get_event_loop() # type: ignore - - roots = {fix_url(root) for root in args.roots} - - crawler = Crawler(log, - roots, exclude=args.exclude, - strict=args.strict, - max_redirect=args.max_redirect, - max_tries=args.max_tries, - max_tasks=args.max_tasks, - max_pool=args.max_pool, - ) - try: - loop.run_until_complete(crawler.crawl()) # Crawler gonna crawl. - except KeyboardInterrupt: - sys.stderr.flush() - print('\nInterrupted\n') - finally: - crawler.report() - crawler.close() - loop.close() - - -if __name__ == '__main__': - logging.basicConfig(level=logging.INFO) # type: ignore - main() diff --git a/test-data/samples/dict.py b/test-data/samples/dict.py deleted file mode 100644 index d74a5b5ea01a..000000000000 --- a/test-data/samples/dict.py +++ /dev/null @@ -1,8 +0,0 @@ -import typing -prices = {'apple': 0.40, 'banana': 0.50} -my_purchase = { - 'apple': 1, - 'banana': 6} -grocery_bill = sum(prices[fruit] * my_purchase[fruit] - for fruit in my_purchase) -print('I owe the grocer $%.2f' % grocery_bill) diff --git a/test-data/samples/fib.py b/test-data/samples/fib.py deleted file mode 100644 index 26248c866b1f..000000000000 --- a/test-data/samples/fib.py +++ /dev/null @@ -1,5 +0,0 @@ -import typing -parents, babies = (1, 1) -while babies < 100: - print('This generation has {0} babies'.format(babies)) - parents, babies = (babies, parents + babies) diff --git a/test-data/samples/files.py b/test-data/samples/files.py deleted file mode 100644 index f540c7c2b665..000000000000 --- a/test-data/samples/files.py +++ /dev/null @@ -1,14 +0,0 @@ -# indent your Python code to put into an email -import glob -import typing -# glob supports Unix style pathname extensions -python_files = glob.glob('*.py') -for file_name in sorted(python_files): - print(' ------' + file_name) - - f = open(file_name) - for line in f: - print(' ' + line.rstrip()) - f.close() - - print() diff --git a/test-data/samples/for.py b/test-data/samples/for.py deleted file mode 100644 index f7eeed4efbe6..000000000000 --- a/test-data/samples/for.py +++ /dev/null @@ -1,4 +0,0 @@ -import typing -friends = ['john', 'pat', 'gary', 'michael'] -for i, name in enumerate(friends): - print("iteration {iteration} is {name}".format(iteration=i, name=name)) diff --git a/test-data/samples/generators.py b/test-data/samples/generators.py deleted file mode 100644 index 17e94c1bfb45..000000000000 --- a/test-data/samples/generators.py +++ /dev/null @@ -1,24 +0,0 @@ -# Prime number sieve with generators - -import itertools -from typing import Iterator - - -def iter_primes() -> Iterator[int]: - # an iterator of all numbers between 2 and +infinity - numbers = itertools.count(2) # type: Iterator[int] - - # generate primes forever - while True: - # get the first number from the iterator (always a prime) - prime = next(numbers) - yield prime - - # this code iteratively builds up a chain of - # filters...slightly tricky, but ponder it a bit - numbers = filter(prime.__rmod__, numbers) - -for p in iter_primes(): - if p > 1000: - break - print(p) diff --git a/test-data/samples/greet.py b/test-data/samples/greet.py deleted file mode 100644 index 47e7626410c3..000000000000 --- a/test-data/samples/greet.py +++ /dev/null @@ -1,8 +0,0 @@ -import typing - - -def greet(name: str) -> None: - print('Hello', name) -greet('Jack') -greet('Jill') -greet('Bob') diff --git a/test-data/samples/guess.py b/test-data/samples/guess.py deleted file mode 100644 index d3f1cee4edc7..000000000000 --- a/test-data/samples/guess.py +++ /dev/null @@ -1,32 +0,0 @@ -# "Guess the Number" Game (edited) from http://inventwithpython.com - -import random -import typing - -guesses_made = 0 - -name = input('Hello! What is your name?\n') - -number = random.randint(1, 20) -print('Well, {0}, I am thinking of a number between 1 and 20.'.format(name)) - -while guesses_made < 6: - - guess = int(input('Take a guess: ')) - - guesses_made += 1 - - if guess < number: - print('Your guess is too low.') - - if guess > number: - print('Your guess is too high.') - - if guess == number: - break - -if guess == number: - print('Good job, {0}! You guessed my number in {1} guesses!'.format( - name, guesses_made)) -else: - print('Nope. The number I was thinking of was {0}'.format(number)) diff --git a/test-data/samples/hello.py b/test-data/samples/hello.py deleted file mode 100644 index 6c0b2caa7a60..000000000000 --- a/test-data/samples/hello.py +++ /dev/null @@ -1,2 +0,0 @@ -import typing -print('Hello, world') diff --git a/test-data/samples/input.py b/test-data/samples/input.py deleted file mode 100644 index cca92336f06b..000000000000 --- a/test-data/samples/input.py +++ /dev/null @@ -1,3 +0,0 @@ -import typing -name = input('What is your name?\n') -print('Hi, %s.' % name) diff --git a/test-data/samples/itertool.py b/test-data/samples/itertool.py deleted file mode 100644 index 9ee2475e01fb..000000000000 --- a/test-data/samples/itertool.py +++ /dev/null @@ -1,16 +0,0 @@ -from itertools import groupby -import typing -lines = ''' -This is the -first paragraph. - -This is the second. -'''.splitlines() -# Use itertools.groupby and bool to return groups of -# consecutive lines that either have content or don't. -for has_chars, frags in groupby(lines, bool): - if has_chars: - print(' '.join(frags)) -# PRINTS: -# This is the first paragraph. -# This is the second. diff --git a/test-data/samples/readme.txt b/test-data/samples/readme.txt deleted file mode 100644 index 5889a8ee00ca..000000000000 --- a/test-data/samples/readme.txt +++ /dev/null @@ -1,25 +0,0 @@ -Mypy Sample Programs --------------------- - -The sample programs use static typing unless otherwise noted in comments. - -Original credits for sample programs: - - fib.py - Python Wiki [1] - for.py - Python Wiki [1] - greet.py - Python Wiki [1] - hello.py - Python Wiki [1] - input.py - Python Wiki [1] - regexp.py - Python Wiki [1] - dict.py - Python Wiki [1] - cmdline.py - Python Wiki [1] - files.py - Python Wiki [1] - bottles.py - Python Wiki [1] - class.py - Python Wiki [1] - guess.py - Python Wiki [1] - generators.py - Python Wiki [1] - itertool.py - Python Wiki [1] - -The sample programs were ported to mypy by Jukka Lehtosalo. - -[1] http://wiki.python.org/moin/SimplePrograms diff --git a/test-data/samples/regexp.py b/test-data/samples/regexp.py deleted file mode 100644 index 6d8d7992d0ae..000000000000 --- a/test-data/samples/regexp.py +++ /dev/null @@ -1,7 +0,0 @@ -import typing -import re -for test_string in ['555-1212', 'ILL-EGAL']: - if re.match(r'^\d{3}-\d{4}$', test_string): - print(test_string, 'is a valid US local phone number') - else: - print(test_string, 'rejected') diff --git a/test-data/stdlib-samples/3.2/base64.py b/test-data/stdlib-samples/3.2/base64.py deleted file mode 100644 index ef9196490571..000000000000 --- a/test-data/stdlib-samples/3.2/base64.py +++ /dev/null @@ -1,411 +0,0 @@ -#! /usr/bin/env python3 - -"""RFC 3548: Base16, Base32, Base64 Data Encodings""" - -# Modified 04-Oct-1995 by Jack Jansen to use binascii module -# Modified 30-Dec-2003 by Barry Warsaw to add full RFC 3548 support -# Modified 22-May-2007 by Guido van Rossum to use bytes everywhere - -import re -import struct -import binascii - -from typing import Dict, List, AnyStr, IO - - -__all__ = [ - # Legacy interface exports traditional RFC 1521 Base64 encodings - 'encode', 'decode', 'encodebytes', 'decodebytes', - # Generalized interface for other encodings - 'b64encode', 'b64decode', 'b32encode', 'b32decode', - 'b16encode', 'b16decode', - # Standard Base64 encoding - 'standard_b64encode', 'standard_b64decode', - # Some common Base64 alternatives. As referenced by RFC 3458, see thread - # starting at: - # - # http://zgp.org/pipermail/p2p-hackers/2001-September/000316.html - 'urlsafe_b64encode', 'urlsafe_b64decode', - ] - - -bytes_types = (bytes, bytearray) # Types acceptable as binary data - - -def _translate(s: bytes, altchars: Dict[AnyStr, bytes]) -> bytes: - if not isinstance(s, bytes_types): - raise TypeError("expected bytes, not %s" % s.__class__.__name__) - translation = bytearray(range(256)) - for k, v in altchars.items(): - translation[ord(k)] = v[0] - return s.translate(translation) - - - -# Base64 encoding/decoding uses binascii - -def b64encode(s: bytes, altchars: bytes = None) -> bytes: - """Encode a byte string using Base64. - - s is the byte string to encode. Optional altchars must be a byte - string of length 2 which specifies an alternative alphabet for the - '+' and '/' characters. This allows an application to - e.g. generate url or filesystem safe Base64 strings. - - The encoded byte string is returned. - """ - if not isinstance(s, bytes_types): - raise TypeError("expected bytes, not %s" % s.__class__.__name__) - # Strip off the trailing newline - encoded = binascii.b2a_base64(s)[:-1] - if altchars is not None: - if not isinstance(altchars, bytes_types): - raise TypeError("expected bytes, not %s" - % altchars.__class__.__name__) - assert len(altchars) == 2, repr(altchars) - return _translate(encoded, {'+': altchars[0:1], '/': altchars[1:2]}) - return encoded - - -def b64decode(s: bytes, altchars: bytes = None, - validate: bool = False) -> bytes: - """Decode a Base64 encoded byte string. - - s is the byte string to decode. Optional altchars must be a - string of length 2 which specifies the alternative alphabet used - instead of the '+' and '/' characters. - - The decoded string is returned. A binascii.Error is raised if s is - incorrectly padded. - - If validate is False (the default), non-base64-alphabet characters are - discarded prior to the padding check. If validate is True, - non-base64-alphabet characters in the input result in a binascii.Error. - """ - if not isinstance(s, bytes_types): - raise TypeError("expected bytes, not %s" % s.__class__.__name__) - if altchars is not None: - if not isinstance(altchars, bytes_types): - raise TypeError("expected bytes, not %s" - % altchars.__class__.__name__) - assert len(altchars) == 2, repr(altchars) - s = _translate(s, {chr(altchars[0]): b'+', chr(altchars[1]): b'/'}) - if validate and not re.match(b'^[A-Za-z0-9+/]*={0,2}$', s): - raise binascii.Error('Non-base64 digit found') - return binascii.a2b_base64(s) - - -def standard_b64encode(s: bytes) -> bytes: - """Encode a byte string using the standard Base64 alphabet. - - s is the byte string to encode. The encoded byte string is returned. - """ - return b64encode(s) - -def standard_b64decode(s: bytes) -> bytes: - """Decode a byte string encoded with the standard Base64 alphabet. - - s is the byte string to decode. The decoded byte string is - returned. binascii.Error is raised if the input is incorrectly - padded or if there are non-alphabet characters present in the - input. - """ - return b64decode(s) - -def urlsafe_b64encode(s: bytes) -> bytes: - """Encode a byte string using a url-safe Base64 alphabet. - - s is the byte string to encode. The encoded byte string is - returned. The alphabet uses '-' instead of '+' and '_' instead of - '/'. - """ - return b64encode(s, b'-_') - -def urlsafe_b64decode(s: bytes) -> bytes: - """Decode a byte string encoded with the standard Base64 alphabet. - - s is the byte string to decode. The decoded byte string is - returned. binascii.Error is raised if the input is incorrectly - padded or if there are non-alphabet characters present in the - input. - - The alphabet uses '-' instead of '+' and '_' instead of '/'. - """ - return b64decode(s, b'-_') - - - -# Base32 encoding/decoding must be done in Python -_b32alphabet = { - 0: b'A', 9: b'J', 18: b'S', 27: b'3', - 1: b'B', 10: b'K', 19: b'T', 28: b'4', - 2: b'C', 11: b'L', 20: b'U', 29: b'5', - 3: b'D', 12: b'M', 21: b'V', 30: b'6', - 4: b'E', 13: b'N', 22: b'W', 31: b'7', - 5: b'F', 14: b'O', 23: b'X', - 6: b'G', 15: b'P', 24: b'Y', - 7: b'H', 16: b'Q', 25: b'Z', - 8: b'I', 17: b'R', 26: b'2', - } - -_b32tab = [v[0] for k, v in sorted(_b32alphabet.items())] -_b32rev = dict([(v[0], k) for k, v in _b32alphabet.items()]) - - -def b32encode(s: bytes) -> bytes: - """Encode a byte string using Base32. - - s is the byte string to encode. The encoded byte string is returned. - """ - if not isinstance(s, bytes_types): - raise TypeError("expected bytes, not %s" % s.__class__.__name__) - quanta, leftover = divmod(len(s), 5) - # Pad the last quantum with zero bits if necessary - if leftover: - s = s + bytes(5 - leftover) # Don't use += ! - quanta += 1 - encoded = bytes() - for i in range(quanta): - # c1 and c2 are 16 bits wide, c3 is 8 bits wide. The intent of this - # code is to process the 40 bits in units of 5 bits. So we take the 1 - # leftover bit of c1 and tack it onto c2. Then we take the 2 leftover - # bits of c2 and tack them onto c3. The shifts and masks are intended - # to give us values of exactly 5 bits in width. - c1, c2, c3 = struct.unpack('!HHB', s[i*5:(i+1)*5]) # type: (int, int, int) - c2 += (c1 & 1) << 16 # 17 bits wide - c3 += (c2 & 3) << 8 # 10 bits wide - encoded += bytes([_b32tab[c1 >> 11], # bits 1 - 5 - _b32tab[(c1 >> 6) & 0x1f], # bits 6 - 10 - _b32tab[(c1 >> 1) & 0x1f], # bits 11 - 15 - _b32tab[c2 >> 12], # bits 16 - 20 (1 - 5) - _b32tab[(c2 >> 7) & 0x1f], # bits 21 - 25 (6 - 10) - _b32tab[(c2 >> 2) & 0x1f], # bits 26 - 30 (11 - 15) - _b32tab[c3 >> 5], # bits 31 - 35 (1 - 5) - _b32tab[c3 & 0x1f], # bits 36 - 40 (1 - 5) - ]) - # Adjust for any leftover partial quanta - if leftover == 1: - return encoded[:-6] + b'======' - elif leftover == 2: - return encoded[:-4] + b'====' - elif leftover == 3: - return encoded[:-3] + b'===' - elif leftover == 4: - return encoded[:-1] + b'=' - return encoded - - -def b32decode(s: bytes, casefold: bool = False, map01: bytes = None) -> bytes: - """Decode a Base32 encoded byte string. - - s is the byte string to decode. Optional casefold is a flag - specifying whether a lowercase alphabet is acceptable as input. - For security purposes, the default is False. - - RFC 3548 allows for optional mapping of the digit 0 (zero) to the - letter O (oh), and for optional mapping of the digit 1 (one) to - either the letter I (eye) or letter L (el). The optional argument - map01 when not None, specifies which letter the digit 1 should be - mapped to (when map01 is not None, the digit 0 is always mapped to - the letter O). For security purposes the default is None, so that - 0 and 1 are not allowed in the input. - - The decoded byte string is returned. binascii.Error is raised if - the input is incorrectly padded or if there are non-alphabet - characters present in the input. - """ - if not isinstance(s, bytes_types): - raise TypeError("expected bytes, not %s" % s.__class__.__name__) - quanta, leftover = divmod(len(s), 8) - if leftover: - raise binascii.Error('Incorrect padding') - # Handle section 2.4 zero and one mapping. The flag map01 will be either - # False, or the character to map the digit 1 (one) to. It should be - # either L (el) or I (eye). - if map01 is not None: - if not isinstance(map01, bytes_types): - raise TypeError("expected bytes, not %s" % map01.__class__.__name__) - assert len(map01) == 1, repr(map01) - s = _translate(s, {b'0': b'O', b'1': map01}) - if casefold: - s = s.upper() - # Strip off pad characters from the right. We need to count the pad - # characters because this will tell us how many null bytes to remove from - # the end of the decoded string. - padchars = 0 - mo = re.search(b'(?P[=]*)$', s) - if mo: - padchars = len(mo.group('pad')) - if padchars > 0: - s = s[:-padchars] - # Now decode the full quanta - parts = [] # type: List[bytes] - acc = 0 - shift = 35 - for c in s: - val = _b32rev.get(c) - if val is None: - raise TypeError('Non-base32 digit found') - acc += _b32rev[c] << shift - shift -= 5 - if shift < 0: - parts.append(binascii.unhexlify(bytes('%010x' % acc, "ascii"))) - acc = 0 - shift = 35 - # Process the last, partial quanta - last = binascii.unhexlify(bytes('%010x' % acc, "ascii")) - if padchars == 0: - last = b'' # No characters - elif padchars == 1: - last = last[:-1] - elif padchars == 3: - last = last[:-2] - elif padchars == 4: - last = last[:-3] - elif padchars == 6: - last = last[:-4] - else: - raise binascii.Error('Incorrect padding') - parts.append(last) - return b''.join(parts) - - - -# RFC 3548, Base 16 Alphabet specifies uppercase, but hexlify() returns -# lowercase. The RFC also recommends against accepting input case -# insensitively. -def b16encode(s: bytes) -> bytes: - """Encode a byte string using Base16. - - s is the byte string to encode. The encoded byte string is returned. - """ - if not isinstance(s, bytes_types): - raise TypeError("expected bytes, not %s" % s.__class__.__name__) - return binascii.hexlify(s).upper() - - -def b16decode(s: bytes, casefold: bool = False) -> bytes: - """Decode a Base16 encoded byte string. - - s is the byte string to decode. Optional casefold is a flag - specifying whether a lowercase alphabet is acceptable as input. - For security purposes, the default is False. - - The decoded byte string is returned. binascii.Error is raised if - s were incorrectly padded or if there are non-alphabet characters - present in the string. - """ - if not isinstance(s, bytes_types): - raise TypeError("expected bytes, not %s" % s.__class__.__name__) - if casefold: - s = s.upper() - if re.search(b'[^0-9A-F]', s): - raise binascii.Error('Non-base16 digit found') - return binascii.unhexlify(s) - - - -# Legacy interface. This code could be cleaned up since I don't believe -# binascii has any line length limitations. It just doesn't seem worth it -# though. The files should be opened in binary mode. - -MAXLINESIZE = 76 # Excluding the CRLF -MAXBINSIZE = (MAXLINESIZE//4)*3 - -def encode(input: IO[bytes], output: IO[bytes]) -> None: - """Encode a file; input and output are binary files.""" - while True: - s = input.read(MAXBINSIZE) - if not s: - break - while len(s) < MAXBINSIZE: - ns = input.read(MAXBINSIZE-len(s)) - if not ns: - break - s += ns - line = binascii.b2a_base64(s) - output.write(line) - - -def decode(input: IO[bytes], output: IO[bytes]) -> None: - """Decode a file; input and output are binary files.""" - while True: - line = input.readline() - if not line: - break - s = binascii.a2b_base64(line) - output.write(s) - - -def encodebytes(s: bytes) -> bytes: - """Encode a bytestring into a bytestring containing multiple lines - of base-64 data.""" - if not isinstance(s, bytes_types): - raise TypeError("expected bytes, not %s" % s.__class__.__name__) - pieces = [] # type: List[bytes] - for i in range(0, len(s), MAXBINSIZE): - chunk = s[i : i + MAXBINSIZE] - pieces.append(binascii.b2a_base64(chunk)) - return b"".join(pieces) - -def encodestring(s: bytes) -> bytes: - """Legacy alias of encodebytes().""" - import warnings - warnings.warn("encodestring() is a deprecated alias, use encodebytes()", - DeprecationWarning, 2) - return encodebytes(s) - - -def decodebytes(s: bytes) -> bytes: - """Decode a bytestring of base-64 data into a bytestring.""" - if not isinstance(s, bytes_types): - raise TypeError("expected bytes, not %s" % s.__class__.__name__) - return binascii.a2b_base64(s) - -def decodestring(s: bytes) -> bytes: - """Legacy alias of decodebytes().""" - import warnings - warnings.warn("decodestring() is a deprecated alias, use decodebytes()", - DeprecationWarning, 2) - return decodebytes(s) - - -# Usable as a script... -def main() -> None: - """Small main program""" - import sys, getopt - try: - opts, args = getopt.getopt(sys.argv[1:], 'deut') - except getopt.error as msg: - sys.stdout = sys.stderr - print(msg) - print("""usage: %s [-d|-e|-u|-t] [file|-] - -d, -u: decode - -e: encode (default) - -t: encode and decode string 'Aladdin:open sesame'"""%sys.argv[0]) - sys.exit(2) - func = encode - for o, a in opts: - if o == '-e': func = encode - if o == '-d': func = decode - if o == '-u': func = decode - if o == '-t': test(); return - if args and args[0] != '-': - with open(args[0], 'rb') as f: - func(f, sys.stdout.buffer) - else: - func(sys.stdin.buffer, sys.stdout.buffer) - - -def test() -> None: - s0 = b"Aladdin:open sesame" - print(repr(s0)) - s1 = encodebytes(s0) - print(repr(s1)) - s2 = decodebytes(s1) - print(repr(s2)) - assert s0 == s2 - - -if __name__ == '__main__': - main() diff --git a/test-data/stdlib-samples/3.2/fnmatch.py b/test-data/stdlib-samples/3.2/fnmatch.py deleted file mode 100644 index 3dccb0ce65fc..000000000000 --- a/test-data/stdlib-samples/3.2/fnmatch.py +++ /dev/null @@ -1,112 +0,0 @@ -"""Filename matching with shell patterns. - -fnmatch(FILENAME, PATTERN) matches according to the local convention. -fnmatchcase(FILENAME, PATTERN) always takes case in account. - -The functions operate by translating the pattern into a regular -expression. They cache the compiled regular expressions for speed. - -The function translate(PATTERN) returns a regular expression -corresponding to PATTERN. (It does not compile it.) -""" -import os -import posixpath -import re -import functools - -from typing import Iterable, List, AnyStr, Any, Callable, Match - -__all__ = ["filter", "fnmatch", "fnmatchcase", "translate"] - -def fnmatch(name: AnyStr, pat: AnyStr) -> bool: - """Test whether FILENAME matches PATTERN. - - Patterns are Unix shell style: - - * matches everything - ? matches any single character - [seq] matches any character in seq - [!seq] matches any char not in seq - - An initial period in FILENAME is not special. - Both FILENAME and PATTERN are first case-normalized - if the operating system requires it. - If you don't want this, use fnmatchcase(FILENAME, PATTERN). - """ - name = os.path.normcase(name) - pat = os.path.normcase(pat) - return fnmatchcase(name, pat) - -@functools.lru_cache(maxsize=250) -def _compile_pattern(pat: AnyStr, - is_bytes: bool = False) -> Callable[[AnyStr], - Match[AnyStr]]: - if isinstance(pat, bytes): - pat_str = str(pat, 'ISO-8859-1') - res_str = translate(pat_str) - res = bytes(res_str, 'ISO-8859-1') - else: - res = translate(pat) - return re.compile(res).match - -def filter(names: Iterable[AnyStr], pat: AnyStr) -> List[AnyStr]: - """Return the subset of the list NAMES that match PAT.""" - result = [] # type: List[AnyStr] - pat = os.path.normcase(pat) - match = _compile_pattern(pat, isinstance(pat, bytes)) - if os.path is posixpath: - # normcase on posix is NOP. Optimize it away from the loop. - for name in names: - if match(name): - result.append(name) - else: - for name in names: - if match(os.path.normcase(name)): - result.append(name) - return result - -def fnmatchcase(name: AnyStr, pat: AnyStr) -> bool: - """Test whether FILENAME matches PATTERN, including case. - - This is a version of fnmatch() which doesn't case-normalize - its arguments. - """ - match = _compile_pattern(pat, isinstance(pat, bytes)) - return match(name) is not None - -def translate(pat: str) -> str: - """Translate a shell PATTERN to a regular expression. - - There is no way to quote meta-characters. - """ - - i, n = 0, len(pat) - res = '' - while i < n: - c = pat[i] - i = i+1 - if c == '*': - res = res + '.*' - elif c == '?': - res = res + '.' - elif c == '[': - j = i - if j < n and pat[j] == '!': - j = j+1 - if j < n and pat[j] == ']': - j = j+1 - while j < n and pat[j] != ']': - j = j+1 - if j >= n: - res = res + '\\[' - else: - stuff = pat[i:j].replace('\\','\\\\') - i = j+1 - if stuff[0] == '!': - stuff = '^' + stuff[1:] - elif stuff[0] == '^': - stuff = '\\' + stuff - res = '%s[%s]' % (res, stuff) - else: - res = res + re.escape(c) - return res + r'\Z(?ms)' diff --git a/test-data/stdlib-samples/3.2/genericpath.py b/test-data/stdlib-samples/3.2/genericpath.py deleted file mode 100644 index bd1fddf750bf..000000000000 --- a/test-data/stdlib-samples/3.2/genericpath.py +++ /dev/null @@ -1,112 +0,0 @@ -""" -Path operations common to more than one OS -Do not use directly. The OS specific modules import the appropriate -functions from this module themselves. -""" -import os -import stat - -from typing import ( - Any as Any_, List as List_, AnyStr as AnyStr_, Tuple as Tuple_ -) - -__all__ = ['commonprefix', 'exists', 'getatime', 'getctime', 'getmtime', - 'getsize', 'isdir', 'isfile'] - - -# Does a path exist? -# This is false for dangling symbolic links on systems that support them. -def exists(path: AnyStr_) -> bool: - """Test whether a path exists. Returns False for broken symbolic links""" - try: - os.stat(path) - except os.error: - return False - return True - - -# This follows symbolic links, so both islink() and isdir() can be true -# for the same path ono systems that support symlinks -def isfile(path: AnyStr_) -> bool: - """Test whether a path is a regular file""" - try: - st = os.stat(path) - except os.error: - return False - return stat.S_ISREG(st.st_mode) - - -# Is a path a directory? -# This follows symbolic links, so both islink() and isdir() -# can be true for the same path on systems that support symlinks -def isdir(s: AnyStr_) -> bool: - """Return true if the pathname refers to an existing directory.""" - try: - st = os.stat(s) - except os.error: - return False - return stat.S_ISDIR(st.st_mode) - - -def getsize(filename: AnyStr_) -> int: - """Return the size of a file, reported by os.stat().""" - return os.stat(filename).st_size - - -def getmtime(filename: AnyStr_) -> float: - """Return the last modification time of a file, reported by os.stat().""" - return os.stat(filename).st_mtime - - -def getatime(filename: AnyStr_) -> float: - """Return the last access time of a file, reported by os.stat().""" - return os.stat(filename).st_atime - - -def getctime(filename: AnyStr_) -> float: - """Return the metadata change time of a file, reported by os.stat().""" - return os.stat(filename).st_ctime - - -# Return the longest prefix of all list elements. -def commonprefix(m: List_[Any_]) -> Any_: - "Given a list of pathnames, returns the longest common leading component" - if not m: return '' - s1 = min(m) - s2 = max(m) - for i, c in enumerate(s1): - if c != s2[i]: - return s1[:i] - return s1 - - -# Split a path in root and extension. -# The extension is everything starting at the last dot in the last -# pathname component; the root is everything before that. -# It is always true that root + ext == p. - -# Generic implementation of splitext, to be parametrized with -# the separators -def _splitext(p: AnyStr_, sep: AnyStr_, altsep: AnyStr_, - extsep: AnyStr_) -> Tuple_[AnyStr_, AnyStr_]: - """Split the extension from a pathname. - - Extension is everything from the last dot to the end, ignoring - leading dots. Returns "(root, ext)"; ext may be empty.""" - # NOTE: This code must work for text and bytes strings. - - sepIndex = p.rfind(sep) - if altsep: - altsepIndex = p.rfind(altsep) - sepIndex = max(sepIndex, altsepIndex) - - dotIndex = p.rfind(extsep) - if dotIndex > sepIndex: - # skip all leading dots - filenameIndex = sepIndex + 1 - while filenameIndex < dotIndex: - if p[filenameIndex:filenameIndex+1] != extsep: - return p[:dotIndex], p[dotIndex:] - filenameIndex += 1 - - return p, p[:0] diff --git a/test-data/stdlib-samples/3.2/getopt.py b/test-data/stdlib-samples/3.2/getopt.py deleted file mode 100644 index 32f5bcec7420..000000000000 --- a/test-data/stdlib-samples/3.2/getopt.py +++ /dev/null @@ -1,220 +0,0 @@ -"""Parser for command line options. - -This module helps scripts to parse the command line arguments in -sys.argv. It supports the same conventions as the Unix getopt() -function (including the special meanings of arguments of the form `-' -and `--'). Long options similar to those supported by GNU software -may be used as well via an optional third argument. This module -provides two functions and an exception: - -getopt() -- Parse command line options -gnu_getopt() -- Like getopt(), but allow option and non-option arguments -to be intermixed. -GetoptError -- exception (class) raised with 'opt' attribute, which is the -option involved with the exception. -""" - -# Long option support added by Lars Wirzenius . -# -# Gerrit Holl moved the string-based exceptions -# to class-based exceptions. -# -# Peter Åstrand added gnu_getopt(). -# -# TODO for gnu_getopt(): -# -# - GNU getopt_long_only mechanism -# - allow the caller to specify ordering -# - RETURN_IN_ORDER option -# - GNU extension with '-' as first character of option string -# - optional arguments, specified by double colons -# - a option string with a W followed by semicolon should -# treat "-W foo" as "--foo" - -__all__ = ["GetoptError","error","getopt","gnu_getopt"] - -import os - -from typing import List, Tuple, Iterable - -class GetoptError(Exception): - opt = '' - msg = '' - def __init__(self, msg: str, opt: str = '') -> None: - self.msg = msg - self.opt = opt - Exception.__init__(self, msg, opt) - - def __str__(self) -> str: - return self.msg - -error = GetoptError # backward compatibility - -def getopt(args: List[str], shortopts: str, - longopts: Iterable[str] = []) -> Tuple[List[Tuple[str, str]], - List[str]]: - """getopt(args, options[, long_options]) -> opts, args - - Parses command line options and parameter list. args is the - argument list to be parsed, without the leading reference to the - running program. Typically, this means "sys.argv[1:]". shortopts - is the string of option letters that the script wants to - recognize, with options that require an argument followed by a - colon (i.e., the same format that Unix getopt() uses). If - specified, longopts is a list of strings with the names of the - long options which should be supported. The leading '--' - characters should not be included in the option name. Options - which require an argument should be followed by an equal sign - ('='). - - The return value consists of two elements: the first is a list of - (option, value) pairs; the second is the list of program arguments - left after the option list was stripped (this is a trailing slice - of the first argument). Each option-and-value pair returned has - the option as its first element, prefixed with a hyphen (e.g., - '-x'), and the option argument as its second element, or an empty - string if the option has no argument. The options occur in the - list in the same order in which they were found, thus allowing - multiple occurrences. Long and short options may be mixed. - - """ - - opts = [] # type: List[Tuple[str, str]] - if isinstance(longopts, str): - longopts = [longopts] - else: - longopts = list(longopts) - while args and args[0].startswith('-') and args[0] != '-': - if args[0] == '--': - args = args[1:] - break - if args[0].startswith('--'): - opts, args = do_longs(opts, args[0][2:], longopts, args[1:]) - else: - opts, args = do_shorts(opts, args[0][1:], shortopts, args[1:]) - - return opts, args - -def gnu_getopt(args: List[str], shortopts: str, - longopts: Iterable[str] = []) -> Tuple[List[Tuple[str, str]], - List[str]]: - """getopt(args, options[, long_options]) -> opts, args - - This function works like getopt(), except that GNU style scanning - mode is used by default. This means that option and non-option - arguments may be intermixed. The getopt() function stops - processing options as soon as a non-option argument is - encountered. - - If the first character of the option string is `+', or if the - environment variable POSIXLY_CORRECT is set, then option - processing stops as soon as a non-option argument is encountered. - - """ - - opts = [] # type: List[Tuple[str, str]] - prog_args = [] # type: List[str] - if isinstance(longopts, str): - longopts = [longopts] - else: - longopts = list(longopts) - - # Allow options after non-option arguments? - if shortopts.startswith('+'): - shortopts = shortopts[1:] - all_options_first = True - elif os.environ.get("POSIXLY_CORRECT"): - all_options_first = True - else: - all_options_first = False - - while args: - if args[0] == '--': - prog_args += args[1:] - break - - if args[0][:2] == '--': - opts, args = do_longs(opts, args[0][2:], longopts, args[1:]) - elif args[0][:1] == '-' and args[0] != '-': - opts, args = do_shorts(opts, args[0][1:], shortopts, args[1:]) - else: - if all_options_first: - prog_args += args - break - else: - prog_args.append(args[0]) - args = args[1:] - - return opts, prog_args - -def do_longs(opts: List[Tuple[str, str]], opt: str, - longopts: List[str], - args: List[str]) -> Tuple[List[Tuple[str, str]], List[str]]: - try: - i = opt.index('=') - except ValueError: - optarg = None # type: str - else: - opt, optarg = opt[:i], opt[i+1:] - - has_arg, opt = long_has_args(opt, longopts) - if has_arg: - if optarg is None: - if not args: - raise GetoptError('option --%s requires argument' % opt, opt) - optarg, args = args[0], args[1:] - elif optarg is not None: - raise GetoptError('option --%s must not have an argument' % opt, opt) - opts.append(('--' + opt, optarg or '')) - return opts, args - -# Return: -# has_arg? -# full option name -def long_has_args(opt: str, longopts: List[str]) -> Tuple[bool, str]: - possibilities = [o for o in longopts if o.startswith(opt)] - if not possibilities: - raise GetoptError('option --%s not recognized' % opt, opt) - # Is there an exact match? - if opt in possibilities: - return False, opt - elif opt + '=' in possibilities: - return True, opt - # No exact match, so better be unique. - if len(possibilities) > 1: - # XXX since possibilities contains all valid continuations, might be - # nice to work them into the error msg - raise GetoptError('option --%s not a unique prefix' % opt, opt) - assert len(possibilities) == 1 - unique_match = possibilities[0] - has_arg = unique_match.endswith('=') - if has_arg: - unique_match = unique_match[:-1] - return has_arg, unique_match - -def do_shorts(opts: List[Tuple[str, str]], optstring: str, - shortopts: str, args: List[str]) -> Tuple[List[Tuple[str, str]], - List[str]]: - while optstring != '': - opt, optstring = optstring[0], optstring[1:] - if short_has_arg(opt, shortopts): - if optstring == '': - if not args: - raise GetoptError('option -%s requires argument' % opt, - opt) - optstring, args = args[0], args[1:] - optarg, optstring = optstring, '' - else: - optarg = '' - opts.append(('-' + opt, optarg)) - return opts, args - -def short_has_arg(opt: str, shortopts: str) -> bool: - for i in range(len(shortopts)): - if opt == shortopts[i] != ':': - return shortopts.startswith(':', i+1) - raise GetoptError('option -%s not recognized' % opt, opt) - -if __name__ == '__main__': - import sys - print(getopt(sys.argv[1:], "a:b", ["alpha=", "beta"])) diff --git a/test-data/stdlib-samples/3.2/glob.py b/test-data/stdlib-samples/3.2/glob.py deleted file mode 100644 index 0f3d5f5d9a09..000000000000 --- a/test-data/stdlib-samples/3.2/glob.py +++ /dev/null @@ -1,84 +0,0 @@ -"""Filename globbing utility.""" - -import os -import re -import fnmatch - -from typing import List, Iterator, Iterable, Any, AnyStr - -__all__ = ["glob", "iglob"] - -def glob(pathname: AnyStr) -> List[AnyStr]: - """Return a list of paths matching a pathname pattern. - - The pattern may contain simple shell-style wildcards a la fnmatch. - - """ - return list(iglob(pathname)) - -def iglob(pathname: AnyStr) -> Iterator[AnyStr]: - """Return an iterator which yields the paths matching a pathname pattern. - - The pattern may contain simple shell-style wildcards a la fnmatch. - - """ - if not has_magic(pathname): - if os.path.lexists(pathname): - yield pathname - return - dirname, basename = os.path.split(pathname) - if not dirname: - for name in glob1(None, basename): - yield name - return - if has_magic(dirname): - dirs = iglob(dirname) # type: Iterable[AnyStr] - else: - dirs = [dirname] - if has_magic(basename): - glob_in_dir = glob1 # type: Any - else: - glob_in_dir = glob0 - for dirname in dirs: - for name in glob_in_dir(dirname, basename): - yield os.path.join(dirname, name) - -# These 2 helper functions non-recursively glob inside a literal directory. -# They return a list of basenames. `glob1` accepts a pattern while `glob0` -# takes a literal basename (so it only has to check for its existence). - -def glob1(dirname: AnyStr, pattern: AnyStr) -> List[AnyStr]: - if not dirname: - if isinstance(pattern, bytes): - dirname = bytes(os.curdir, 'ASCII') - else: - dirname = os.curdir - try: - names = os.listdir(dirname) - except os.error: - return [] - if pattern[0] != '.': - names = [x for x in names if x[0] != '.'] - return fnmatch.filter(names, pattern) - -def glob0(dirname: AnyStr, basename: AnyStr) -> List[AnyStr]: - if basename == '': - # `os.path.split()` returns an empty basename for paths ending with a - # directory separator. 'q*x/' should match only directories. - if os.path.isdir(dirname): - return [basename] - else: - if os.path.lexists(os.path.join(dirname, basename)): - return [basename] - return [] - - -magic_check = re.compile('[*?[]') -magic_check_bytes = re.compile(b'[*?[]') - -def has_magic(s: AnyStr) -> bool: - if isinstance(s, bytes): - match = magic_check_bytes.search(s) - else: - match = magic_check.search(s) - return match is not None diff --git a/test-data/stdlib-samples/3.2/posixpath.py b/test-data/stdlib-samples/3.2/posixpath.py deleted file mode 100644 index cf5d59e6a69a..000000000000 --- a/test-data/stdlib-samples/3.2/posixpath.py +++ /dev/null @@ -1,466 +0,0 @@ -"""Common operations on Posix pathnames. - -Instead of importing this module directly, import os and refer to -this module as os.path. The "os.path" name is an alias for this -module on Posix systems; on other systems (e.g. Mac, Windows), -os.path provides the same operations in a manner specific to that -platform, and is an alias to another module (e.g. macpath, ntpath). - -Some of this can actually be useful on non-Posix systems too, e.g. -for manipulation of the pathname component of URLs. -""" - -import os -import sys -import stat -import genericpath -from genericpath import * - -from typing import ( - Tuple, BinaryIO, TextIO, Pattern, AnyStr, List, Set, Any, Union, cast -) - -__all__ = ["normcase","isabs","join","splitdrive","split","splitext", - "basename","dirname","commonprefix","getsize","getmtime", - "getatime","getctime","islink","exists","lexists","isdir","isfile", - "ismount", "expanduser","expandvars","normpath","abspath", - "samefile","sameopenfile","samestat", - "curdir","pardir","sep","pathsep","defpath","altsep","extsep", - "devnull","realpath","supports_unicode_filenames","relpath"] - -# Strings representing various path-related bits and pieces. -# These are primarily for export; internally, they are hardcoded. -curdir = '.' -pardir = '..' -extsep = '.' -sep = '/' -pathsep = ':' -defpath = ':/bin:/usr/bin' -altsep = None # type: str -devnull = '/dev/null' - -def _get_sep(path: AnyStr) -> AnyStr: - if isinstance(path, bytes): - return b'/' - else: - return '/' - -# Normalize the case of a pathname. Trivial in Posix, string.lower on Mac. -# On MS-DOS this may also turn slashes into backslashes; however, other -# normalizations (such as optimizing '../' away) are not allowed -# (another function should be defined to do that). - -def normcase(s: AnyStr) -> AnyStr: - """Normalize case of pathname. Has no effect under Posix""" - # TODO: on Mac OS X, this should really return s.lower(). - if not isinstance(s, (bytes, str)): - raise TypeError("normcase() argument must be str or bytes, " - "not '{}'".format(s.__class__.__name__)) - return cast(AnyStr, s) - - -# Return whether a path is absolute. -# Trivial in Posix, harder on the Mac or MS-DOS. - -def isabs(s: AnyStr) -> bool: - """Test whether a path is absolute""" - sep = _get_sep(s) - return s.startswith(sep) - - -# Join pathnames. -# Ignore the previous parts if a part is absolute. -# Insert a '/' unless the first part is empty or already ends in '/'. - -def join(a: AnyStr, *p: AnyStr) -> AnyStr: - """Join two or more pathname components, inserting '/' as needed. - If any component is an absolute path, all previous path components - will be discarded.""" - sep = _get_sep(a) - path = a - for b in p: - if b.startswith(sep): - path = b - elif not path or path.endswith(sep): - path += b - else: - path += sep + b - return path - - -# Split a path in head (everything up to the last '/') and tail (the -# rest). If the path ends in '/', tail will be empty. If there is no -# '/' in the path, head will be empty. -# Trailing '/'es are stripped from head unless it is the root. - -def split(p: AnyStr) -> Tuple[AnyStr, AnyStr]: - """Split a pathname. Returns tuple "(head, tail)" where "tail" is - everything after the final slash. Either part may be empty.""" - sep = _get_sep(p) - i = p.rfind(sep) + 1 - head, tail = p[:i], p[i:] - if head and head != sep*len(head): - head = head.rstrip(sep) - return head, tail - - -# Split a path in root and extension. -# The extension is everything starting at the last dot in the last -# pathname component; the root is everything before that. -# It is always true that root + ext == p. - -def splitext(p: AnyStr) -> Tuple[AnyStr, AnyStr]: - if isinstance(p, bytes): - sep = b'/' - extsep = b'.' - else: - sep = '/' - extsep = '.' - return genericpath._splitext(p, sep, None, extsep) -splitext.__doc__ = genericpath._splitext.__doc__ - -# Split a pathname into a drive specification and the rest of the -# path. Useful on DOS/Windows/NT; on Unix, the drive is always empty. - -def splitdrive(p: AnyStr) -> Tuple[AnyStr, AnyStr]: - """Split a pathname into drive and path. On Posix, drive is always - empty.""" - return p[:0], p - - -# Return the tail (basename) part of a path, same as split(path)[1]. - -def basename(p: AnyStr) -> AnyStr: - """Returns the final component of a pathname""" - sep = _get_sep(p) - i = p.rfind(sep) + 1 - return p[i:] - - -# Return the head (dirname) part of a path, same as split(path)[0]. - -def dirname(p: AnyStr) -> AnyStr: - """Returns the directory component of a pathname""" - sep = _get_sep(p) - i = p.rfind(sep) + 1 - head = p[:i] - if head and head != sep*len(head): - head = head.rstrip(sep) - return head - - -# Is a path a symbolic link? -# This will always return false on systems where os.lstat doesn't exist. - -def islink(path: AnyStr) -> bool: - """Test whether a path is a symbolic link""" - try: - st = os.lstat(path) - except (os.error, AttributeError): - return False - return stat.S_ISLNK(st.st_mode) - -# Being true for dangling symbolic links is also useful. - -def lexists(path: AnyStr) -> bool: - """Test whether a path exists. Returns True for broken symbolic links""" - try: - os.lstat(path) - except os.error: - return False - return True - - -# Are two filenames really pointing to the same file? - -def samefile(f1: AnyStr, f2: AnyStr) -> bool: - """Test whether two pathnames reference the same actual file""" - s1 = os.stat(f1) - s2 = os.stat(f2) - return samestat(s1, s2) - - -# Are two open files really referencing the same file? -# (Not necessarily the same file descriptor!) - -def sameopenfile(fp1: int, fp2: int) -> bool: - """Test whether two open file objects reference the same file""" - s1 = os.fstat(fp1) - s2 = os.fstat(fp2) - return samestat(s1, s2) - - -# Are two stat buffers (obtained from stat, fstat or lstat) -# describing the same file? - -def samestat(s1: os.stat_result, s2: os.stat_result) -> bool: - """Test whether two stat buffers reference the same file""" - return s1.st_ino == s2.st_ino and \ - s1.st_dev == s2.st_dev - - -# Is a path a mount point? -# (Does this work for all UNIXes? Is it even guaranteed to work by Posix?) - -def ismount(path: AnyStr) -> bool: - """Test whether a path is a mount point""" - if islink(path): - # A symlink can never be a mount point - return False - try: - s1 = os.lstat(path) - if isinstance(path, bytes): - parent = join(path, b'..') - else: - parent = join(path, '..') - s2 = os.lstat(parent) - except os.error: - return False # It doesn't exist -- so not a mount point :-) - dev1 = s1.st_dev - dev2 = s2.st_dev - if dev1 != dev2: - return True # path/.. on a different device as path - ino1 = s1.st_ino - ino2 = s2.st_ino - if ino1 == ino2: - return True # path/.. is the same i-node as path - return False - - -# Expand paths beginning with '~' or '~user'. -# '~' means $HOME; '~user' means that user's home directory. -# If the path doesn't begin with '~', or if the user or $HOME is unknown, -# the path is returned unchanged (leaving error reporting to whatever -# function is called with the expanded path as argument). -# See also module 'glob' for expansion of *, ? and [...] in pathnames. -# (A function should also be defined to do full *sh-style environment -# variable expansion.) - -def expanduser(path: AnyStr) -> AnyStr: - """Expand ~ and ~user constructions. If user or $HOME is unknown, - do nothing.""" - if isinstance(path, bytes): - tilde = b'~' - else: - tilde = '~' - if not path.startswith(tilde): - return path - sep = _get_sep(path) - i = path.find(sep, 1) - if i < 0: - i = len(path) - if i == 1: - userhome = None # type: Union[str, bytes] - if 'HOME' not in os.environ: - import pwd - userhome = pwd.getpwuid(os.getuid()).pw_dir - else: - userhome = os.environ['HOME'] - else: - import pwd - name = path[1:i] # type: Union[str, bytes] - if isinstance(name, bytes): - name = str(name, 'ASCII') - try: - pwent = pwd.getpwnam(name) - except KeyError: - return path - userhome = pwent.pw_dir - if isinstance(path, bytes): - userhome = os.fsencode(userhome) - root = b'/' - else: - root = '/' - userhome = userhome.rstrip(root) - return (userhome + path[i:]) or root - - -# Expand paths containing shell variable substitutions. -# This expands the forms $variable and ${variable} only. -# Non-existent variables are left unchanged. - -_varprog = None # type: Pattern[str] -_varprogb = None # type: Pattern[bytes] - -def expandvars(path: AnyStr) -> AnyStr: - """Expand shell variables of form $var and ${var}. Unknown variables - are left unchanged.""" - global _varprog, _varprogb - if isinstance(path, bytes): - if b'$' not in path: - return path - if not _varprogb: - import re - _varprogb = re.compile(br'\$(\w+|\{[^}]*\})', re.ASCII) - search = _varprogb.search - start = b'{' - end = b'}' - else: - if '$' not in path: - return path - if not _varprog: - import re - _varprog = re.compile(r'\$(\w+|\{[^}]*\})', re.ASCII) - search = _varprog.search - start = '{' - end = '}' - i = 0 - while True: - m = search(path, i) - if not m: - break - i, j = m.span(0) - name = None # type: Union[str, bytes] - name = m.group(1) - if name.startswith(start) and name.endswith(end): - name = name[1:-1] - if isinstance(name, bytes): - name = str(name, 'ASCII') - if name in os.environ: - tail = path[j:] - value = None # type: Union[str, bytes] - value = os.environ[name] - if isinstance(path, bytes): - value = value.encode('ASCII') - path = path[:i] + value - i = len(path) - path += tail - else: - i = j - return path - - -# Normalize a path, e.g. A//B, A/./B and A/foo/../B all become A/B. -# It should be understood that this may change the meaning of the path -# if it contains symbolic links! - -def normpath(path: AnyStr) -> AnyStr: - """Normalize path, eliminating double slashes, etc.""" - if isinstance(path, bytes): - sep = b'/' - empty = b'' - dot = b'.' - dotdot = b'..' - else: - sep = '/' - empty = '' - dot = '.' - dotdot = '..' - if path == empty: - return dot - initial_slashes = path.startswith(sep) # type: int - # POSIX allows one or two initial slashes, but treats three or more - # as single slash. - if (initial_slashes and - path.startswith(sep*2) and not path.startswith(sep*3)): - initial_slashes = 2 - comps = path.split(sep) - new_comps = [] # type: List[AnyStr] - for comp in comps: - if comp in (empty, dot): - continue - if (comp != dotdot or (not initial_slashes and not new_comps) or - (new_comps and new_comps[-1] == dotdot)): - new_comps.append(comp) - elif new_comps: - new_comps.pop() - comps = new_comps - path = sep.join(comps) - if initial_slashes: - path = sep*initial_slashes + path - return path or dot - - -def abspath(path: AnyStr) -> AnyStr: - """Return an absolute path.""" - if not isabs(path): - if isinstance(path, bytes): - cwd = os.getcwdb() - else: - cwd = os.getcwd() - path = join(cwd, path) - return normpath(path) - - -# Return a canonical path (i.e. the absolute location of a file on the -# filesystem). - -def realpath(filename: AnyStr) -> AnyStr: - """Return the canonical path of the specified filename, eliminating any -symbolic links encountered in the path.""" - if isinstance(filename, bytes): - sep = b'/' - empty = b'' - else: - sep = '/' - empty = '' - if isabs(filename): - bits = [sep] + filename.split(sep)[1:] - else: - bits = [empty] + filename.split(sep) - - for i in range(2, len(bits)+1): - component = join(*bits[0:i]) - # Resolve symbolic links. - if islink(component): - resolved = _resolve_link(component) - if resolved is None: - # Infinite loop -- return original component + rest of the path - return abspath(join(*([component] + bits[i:]))) - else: - newpath = join(*([resolved] + bits[i:])) - return realpath(newpath) - - return abspath(filename) - - -def _resolve_link(path: AnyStr) -> AnyStr: - """Internal helper function. Takes a path and follows symlinks - until we either arrive at something that isn't a symlink, or - encounter a path we've seen before (meaning that there's a loop). - """ - paths_seen = set() # type: Set[AnyStr] - while islink(path): - if path in paths_seen: - # Already seen this path, so we must have a symlink loop - return None - paths_seen.add(path) - # Resolve where the link points to - resolved = os.readlink(path) - if not isabs(resolved): - dir = dirname(path) - path = normpath(join(dir, resolved)) - else: - path = normpath(resolved) - return path - -supports_unicode_filenames = (sys.platform == 'darwin') - -def relpath(path: AnyStr, start: AnyStr = None) -> AnyStr: - """Return a relative version of a path""" - - if not path: - raise ValueError("no path specified") - - if isinstance(path, bytes): - curdir = b'.' - sep = b'/' - pardir = b'..' - else: - curdir = '.' - sep = '/' - pardir = '..' - - if start is None: - start = curdir - - start_list = [x for x in abspath(start).split(sep) if x] - path_list = [x for x in abspath(path).split(sep) if x] - - # Work out how much of the filepath is shared by start and path. - i = len(commonprefix([start_list, path_list])) - - rel_list = [pardir] * (len(start_list)-i) + path_list[i:] - if not rel_list: - return curdir - return join(*rel_list) diff --git a/test-data/stdlib-samples/3.2/pprint.py b/test-data/stdlib-samples/3.2/pprint.py deleted file mode 100644 index 650c1a3b5afe..000000000000 --- a/test-data/stdlib-samples/3.2/pprint.py +++ /dev/null @@ -1,380 +0,0 @@ -# Author: Fred L. Drake, Jr. -# fdrake@acm.org -# -# This is a simple little module I wrote to make life easier. I didn't -# see anything quite like it in the library, though I may have overlooked -# something. I wrote this when I was trying to read some heavily nested -# tuples with fairly non-descriptive content. This is modeled very much -# after Lisp/Scheme - style pretty-printing of lists. If you find it -# useful, thank small children who sleep at night. - -"""Support to pretty-print lists, tuples, & dictionaries recursively. - -Very simple, but useful, especially in debugging data structures. - -Classes -------- - -PrettyPrinter() - Handle pretty-printing operations onto a stream using a configured - set of formatting parameters. - -Functions ---------- - -pformat() - Format a Python object into a pretty-printed representation. - -pprint() - Pretty-print a Python object to a stream [default is sys.stdout]. - -saferepr() - Generate a 'standard' repr()-like value, but protect against recursive - data structures. - -""" - -import sys as _sys -from collections import OrderedDict as _OrderedDict -from io import StringIO as _StringIO - -from typing import Any, Tuple, Dict, TextIO, cast, List - -__all__ = ["pprint","pformat","isreadable","isrecursive","saferepr", - "PrettyPrinter"] - -# cache these for faster access: -_commajoin = ", ".join -_id = id -_len = len -_type = type - - -def pprint(object: object, stream: TextIO = None, indent: int = 1, - width: int = 80, depth: int = None) -> None: - """Pretty-print a Python object to a stream [default is sys.stdout].""" - printer = PrettyPrinter( - stream=stream, indent=indent, width=width, depth=depth) - printer.pprint(object) - -def pformat(object: object, indent: int = 1, width: int = 80, - depth: int = None) -> str: - """Format a Python object into a pretty-printed representation.""" - return PrettyPrinter(indent=indent, width=width, depth=depth).pformat(object) - -def saferepr(object: object) -> str: - """Version of repr() which can handle recursive data structures.""" - return _safe_repr(object, {}, None, 0)[0] - -def isreadable(object: object) -> bool: - """Determine if saferepr(object) is readable by eval().""" - return _safe_repr(object, {}, None, 0)[1] - -def isrecursive(object: object) -> bool: - """Determine if object requires a recursive representation.""" - return _safe_repr(object, {}, None, 0)[2] - -class _safe_key: - """Helper function for key functions when sorting unorderable objects. - - The wrapped-object will fallback to an Py2.x style comparison for - unorderable types (sorting first comparing the type name and then by - the obj ids). Does not work recursively, so dict.items() must have - _safe_key applied to both the key and the value. - - """ - - __slots__ = ['obj'] - - def __init__(self, obj: Any) -> None: - self.obj = obj - - def __lt__(self, other: Any) -> Any: - rv = self.obj.__lt__(other.obj) # type: Any - if rv is NotImplemented: - rv = (str(type(self.obj)), id(self.obj)) < \ - (str(type(other.obj)), id(other.obj)) - return rv - -def _safe_tuple(t: Tuple[Any, Any]) -> Tuple[_safe_key, _safe_key]: - "Helper function for comparing 2-tuples" - return _safe_key(t[0]), _safe_key(t[1]) - -class PrettyPrinter: - def __init__(self, indent: int = 1, width: int = 80, depth: int = None, - stream: TextIO = None) -> None: - """Handle pretty printing operations onto a stream using a set of - configured parameters. - - indent - Number of spaces to indent for each level of nesting. - - width - Attempted maximum number of columns in the output. - - depth - The maximum depth to print out nested structures. - - stream - The desired output stream. If omitted (or false), the standard - output stream available at construction will be used. - - """ - indent = int(indent) - width = int(width) - assert indent >= 0, "indent must be >= 0" - assert depth is None or depth > 0, "depth must be > 0" - assert width, "width must be != 0" - self._depth = depth - self._indent_per_level = indent - self._width = width - if stream is not None: - self._stream = stream - else: - self._stream = _sys.stdout - - def pprint(self, object: object) -> None: - self._format(object, self._stream, 0, 0, {}, 0) - self._stream.write("\n") - - def pformat(self, object: object) -> str: - sio = _StringIO() - self._format(object, sio, 0, 0, {}, 0) - return sio.getvalue() - - def isrecursive(self, object: object) -> int: - return self.format(object, {}, 0, 0)[2] - - def isreadable(self, object: object) -> int: - s, readable, recursive = self.format(object, {}, 0, 0) - return readable and not recursive - - def _format(self, object: object, stream: TextIO, indent: int, - allowance: int, context: Dict[int, int], level: int) -> None: - level = level + 1 - objid = _id(object) - if objid in context: - stream.write(_recursion(object)) - self._recursive = True - self._readable = False - return - rep = self._repr(object, context, level - 1) - typ = _type(object) - sepLines = _len(rep) > (self._width - 1 - indent - allowance) - write = stream.write - - if self._depth and level > self._depth: - write(rep) - return - - if sepLines: - r = getattr(typ, "__repr__", None) - if isinstance(object, dict): - write('{') - if self._indent_per_level > 1: - write((self._indent_per_level - 1) * ' ') - length = _len(object) - if length: - context[objid] = 1 - indent = indent + self._indent_per_level - if issubclass(typ, _OrderedDict): - items = list(object.items()) - else: - items = sorted(object.items(), key=_safe_tuple) - key, ent = items[0] - rep = self._repr(key, context, level) - write(rep) - write(': ') - self._format(ent, stream, indent + _len(rep) + 2, - allowance + 1, context, level) - if length > 1: - for key, ent in items[1:]: - rep = self._repr(key, context, level) - write(',\n%s%s: ' % (' '*indent, rep)) - self._format(ent, stream, indent + _len(rep) + 2, - allowance + 1, context, level) - indent = indent - self._indent_per_level - del context[objid] - write('}') - return - - if ((issubclass(typ, list) and r is list.__repr__) or - (issubclass(typ, tuple) and r is tuple.__repr__) or - (issubclass(typ, set) and r is set.__repr__) or - (issubclass(typ, frozenset) and r is frozenset.__repr__) - ): - anyobj = cast(Any, object) # TODO Collection? - length = _len(anyobj) - if issubclass(typ, list): - write('[') - endchar = ']' - lst = anyobj - elif issubclass(typ, set): - if not length: - write('set()') - return - write('{') - endchar = '}' - lst = sorted(anyobj, key=_safe_key) - elif issubclass(typ, frozenset): - if not length: - write('frozenset()') - return - write('frozenset({') - endchar = '})' - lst = sorted(anyobj, key=_safe_key) - indent += 10 - else: - write('(') - endchar = ')' - lst = list(anyobj) - if self._indent_per_level > 1: - write((self._indent_per_level - 1) * ' ') - if length: - context[objid] = 1 - indent = indent + self._indent_per_level - self._format(lst[0], stream, indent, allowance + 1, - context, level) - if length > 1: - for ent in lst[1:]: - write(',\n' + ' '*indent) - self._format(ent, stream, indent, - allowance + 1, context, level) - indent = indent - self._indent_per_level - del context[objid] - if issubclass(typ, tuple) and length == 1: - write(',') - write(endchar) - return - - write(rep) - - def _repr(self, object: object, context: Dict[int, int], - level: int) -> str: - repr, readable, recursive = self.format(object, context.copy(), - self._depth, level) - if not readable: - self._readable = False - if recursive: - self._recursive = True - return repr - - def format(self, object: object, context: Dict[int, int], - maxlevels: int, level: int) -> Tuple[str, int, int]: - """Format object for a specific context, returning a string - and flags indicating whether the representation is 'readable' - and whether the object represents a recursive construct. - """ - return _safe_repr(object, context, maxlevels, level) - - -# Return triple (repr_string, isreadable, isrecursive). - -def _safe_repr(object: object, context: Dict[int, int], - maxlevels: int, level: int) -> Tuple[str, bool, bool]: - typ = _type(object) - if typ is str: - s = cast(str, object) - if 'locale' not in _sys.modules: - return repr(object), True, False - if "'" in s and '"' not in s: - closure = '"' - quotes = {'"': '\\"'} - else: - closure = "'" - quotes = {"'": "\\'"} - qget = quotes.get - sio = _StringIO() - write = sio.write - for char in s: - if char.isalpha(): - write(char) - else: - write(qget(char, repr(char)[1:-1])) - return ("%s%s%s" % (closure, sio.getvalue(), closure)), True, False - - r = getattr(typ, "__repr__", None) - if issubclass(typ, dict) and r is dict.__repr__: - if not object: - return "{}", True, False - objid = _id(object) - if maxlevels and level >= maxlevels: - return "{...}", False, objid in context - if objid in context: - return _recursion(object), False, True - context[objid] = 1 - readable = True - recursive = False - components = [] # type: List[str] - append = components.append - level += 1 - saferepr = _safe_repr - items = sorted((cast(dict, object)).items(), key=_safe_tuple) - for k, v in items: - krepr, kreadable, krecur = saferepr(k, context, maxlevels, level) - vrepr, vreadable, vrecur = saferepr(v, context, maxlevels, level) - append("%s: %s" % (krepr, vrepr)) - readable = readable and kreadable and vreadable - if krecur or vrecur: - recursive = True - del context[objid] - return "{%s}" % _commajoin(components), readable, recursive - - if (issubclass(typ, list) and r is list.__repr__) or \ - (issubclass(typ, tuple) and r is tuple.__repr__): - anyobj = cast(Any, object) # TODO Sequence? - if issubclass(typ, list): - if not object: - return "[]", True, False - format = "[%s]" - elif _len(anyobj) == 1: - format = "(%s,)" - else: - if not object: - return "()", True, False - format = "(%s)" - objid = _id(object) - if maxlevels and level >= maxlevels: - return format % "...", False, objid in context - if objid in context: - return _recursion(object), False, True - context[objid] = 1 - readable = True - recursive = False - components = [] - append = components.append - level += 1 - for o in anyobj: - orepr, oreadable, orecur = _safe_repr(o, context, maxlevels, level) - append(orepr) - if not oreadable: - readable = False - if orecur: - recursive = True - del context[objid] - return format % _commajoin(components), readable, recursive - - rep = repr(object) - return rep, bool(rep and not rep.startswith('<')), False - - -def _recursion(object: object) -> str: - return ("" - % (_type(object).__name__, _id(object))) - - -def _perfcheck(object: object = None) -> None: - import time - if object is None: - object = [("string", (1, 2), [3, 4], {5: 6, 7: 8})] * 100000 - p = PrettyPrinter() - t1 = time.time() - _safe_repr(object, {}, None, 0) - t2 = time.time() - p.pformat(object) - t3 = time.time() - print("_safe_repr:", t2 - t1) - print("pformat:", t3 - t2) - -if __name__ == "__main__": - _perfcheck() diff --git a/test-data/stdlib-samples/3.2/random.py b/test-data/stdlib-samples/3.2/random.py deleted file mode 100644 index 82bda03f7e53..000000000000 --- a/test-data/stdlib-samples/3.2/random.py +++ /dev/null @@ -1,743 +0,0 @@ -"""Random variable generators. - - integers - -------- - uniform within range - - sequences - --------- - pick random element - pick random sample - generate random permutation - - distributions on the real line: - ------------------------------ - uniform - triangular - normal (Gaussian) - lognormal - negative exponential - gamma - beta - pareto - Weibull - - distributions on the circle (angles 0 to 2pi) - --------------------------------------------- - circular uniform - von Mises - -General notes on the underlying Mersenne Twister core generator: - -* The period is 2**19937-1. -* It is one of the most extensively tested generators in existence. -* The random() method is implemented in C, executes in a single Python step, - and is, therefore, threadsafe. - -""" - -from warnings import warn as _warn -from types import MethodType as _MethodType, BuiltinMethodType as _BuiltinMethodType -from math import log as _log, exp as _exp, pi as _pi, e as _e, ceil as _ceil -from math import sqrt as _sqrt, acos as _acos, cos as _cos, sin as _sin -from os import urandom as _urandom -from collections.abc import Set as _Set, Sequence as _Sequence -from hashlib import sha512 as _sha512 - -from typing import ( - Any, TypeVar, Iterable, Sequence, List, Callable, Set, cast, SupportsInt, Union -) - -__all__ = ["Random","seed","random","uniform","randint","choice","sample", - "randrange","shuffle","normalvariate","lognormvariate", - "expovariate","vonmisesvariate","gammavariate","triangular", - "gauss","betavariate","paretovariate","weibullvariate", - "getstate","setstate", "getrandbits", - "SystemRandom"] - -NV_MAGICCONST = 4 * _exp(-0.5)/_sqrt(2.0) -TWOPI = 2.0*_pi -LOG4 = _log(4.0) -SG_MAGICCONST = 1.0 + _log(4.5) -BPF = 53 # Number of bits in a float -RECIP_BPF = 2**-BPF # type: float - - -# Translated by Guido van Rossum from C source provided by -# Adrian Baddeley. Adapted by Raymond Hettinger for use with -# the Mersenne Twister and os.urandom() core generators. - -import _random - -T = TypeVar('T') - -class Random(_random.Random): - """Random number generator base class used by bound module functions. - - Used to instantiate instances of Random to get generators that don't - share state. - - Class Random can also be subclassed if you want to use a different basic - generator of your own devising: in that case, override the following - methods: random(), seed(), getstate(), and setstate(). - Optionally, implement a getrandbits() method so that randrange() - can cover arbitrarily large ranges. - - """ - - VERSION = 3 # used by getstate/setstate - gauss_next = 0.0 - - def __init__(self, x: object = None) -> None: - """Initialize an instance. - - Optional argument x controls seeding, as for Random.seed(). - """ - - self.seed(x) - self.gauss_next = None - - def seed(self, a: Any = None, version: int = 2) -> None: - """Initialize internal state from hashable object. - - None or no argument seeds from current time or from an operating - system specific randomness source if available. - - For version 2 (the default), all of the bits are used if *a *is a str, - bytes, or bytearray. For version 1, the hash() of *a* is used instead. - - If *a* is an int, all bits are used. - - """ - - if a is None: - try: - a = int.from_bytes(_urandom(32), 'big') - except NotImplementedError: - import time - a = int(time.time() * 256) # use fractional seconds - - if version == 2: - if isinstance(a, (str, bytes, bytearray)): - if isinstance(a, str): - a = a.encode() - a += _sha512(a).digest() - a = int.from_bytes(a, 'big') - - super().seed(a) - self.gauss_next = None - - def getstate(self) -> tuple: - """Return internal state; can be passed to setstate() later.""" - return self.VERSION, super().getstate(), self.gauss_next - - def setstate(self, state: tuple) -> None: - """Restore internal state from object returned by getstate().""" - version = state[0] - if version == 3: - version, internalstate, self.gauss_next = state - super().setstate(internalstate) - elif version == 2: - version, internalstate, self.gauss_next = state - # In version 2, the state was saved as signed ints, which causes - # inconsistencies between 32/64-bit systems. The state is - # really unsigned 32-bit ints, so we convert negative ints from - # version 2 to positive longs for version 3. - try: - internalstate = tuple(x % (2**32) for x in internalstate) - except ValueError as e: - raise TypeError() - super().setstate(internalstate) - else: - raise ValueError("state with version %s passed to " - "Random.setstate() of version %s" % - (version, self.VERSION)) - -## ---- Methods below this point do not need to be overridden when -## ---- subclassing for the purpose of using a different core generator. - -## -------------------- pickle support ------------------- - - def __getstate__(self) -> object: # for pickle - return self.getstate() - - def __setstate__(self, state: Any) -> None: # for pickle - self.setstate(state) - - def __reduce__(self) -> tuple: - return self.__class__, (), self.getstate() - -## -------------------- integer methods ------------------- - - def randrange(self, start: SupportsInt, stop: SupportsInt = None, - step: int = 1, int: Callable[[SupportsInt], - int] = int) -> int: - """Choose a random item from range(start, stop[, step]). - - This fixes the problem with randint() which includes the - endpoint; in Python this is usually not what you want. - - Do not supply the 'int' argument. - """ - - # This code is a bit messy to make it fast for the - # common case while still doing adequate error checking. - istart = int(start) - if istart != start: - raise ValueError("non-integer arg 1 for randrange()") - if stop is None: - if istart > 0: - return self._randbelow(istart) - raise ValueError("empty range for randrange()") - - # stop argument supplied. - istop = int(stop) - if istop != stop: - raise ValueError("non-integer stop for randrange()") - width = istop - istart - if step == 1 and width > 0: - return istart + self._randbelow(width) - if step == 1: - raise ValueError("empty range for randrange() (%d,%d, %d)" % (istart, istop, width)) - - # Non-unit step argument supplied. - istep = int(step) - if istep != step: - raise ValueError("non-integer step for randrange()") - if istep > 0: - n = (width + istep - 1) // istep - elif istep < 0: - n = (width + istep + 1) // istep - else: - raise ValueError("zero step for randrange()") - - if n <= 0: - raise ValueError("empty range for randrange()") - - return istart + istep*self._randbelow(n) - - def randint(self, a: int, b: int) -> int: - """Return random integer in range [a, b], including both end points. - """ - - return self.randrange(a, b+1) - - def _randbelow(self, n: int, int: Callable[[float], int] = int, - maxsize: int = 1< int: - "Return a random int in the range [0,n). Raises ValueError if n==0." - - getrandbits = self.getrandbits - # Only call self.getrandbits if the original random() builtin method - # has not been overridden or if a new getrandbits() was supplied. - if type(self.random) is BuiltinMethod or type(getrandbits) is Method: - k = n.bit_length() # don't use (n-1) here because n can be 1 - r = getrandbits(k) # 0 <= r < 2**k - while r >= n: - r = getrandbits(k) - return r - # There's an overridden random() method but no new getrandbits() method, - # so we can only use random() from here. - random = self.random - if n >= maxsize: - _warn("Underlying random() generator does not supply \n" - "enough bits to choose from a population range this large.\n" - "To remove the range limitation, add a getrandbits() method.") - return int(random() * n) - rem = maxsize % n - limit = (maxsize - rem) / maxsize # int(limit * maxsize) % n == 0 - s = random() - while s >= limit: - s = random() - return int(s*maxsize) % n - -## -------------------- sequence methods ------------------- - - def choice(self, seq: Sequence[T]) -> T: - """Choose a random element from a non-empty sequence.""" - try: - i = self._randbelow(len(seq)) - except ValueError: - raise IndexError('Cannot choose from an empty sequence') - return seq[i] - - def shuffle(self, x: List[T], - random: Callable[[], float] = None, - int: Callable[[float], int] = int) -> None: - """x, random=random.random -> shuffle list x in place; return None. - - Optional arg random is a 0-argument function returning a random - float in [0.0, 1.0); by default, the standard random.random. - """ - - randbelow = self._randbelow - for i in reversed(range(1, len(x))): - # pick an element in x[:i+1] with which to exchange x[i] - j = randbelow(i+1) if random is None else int(random() * (i+1)) - x[i], x[j] = x[j], x[i] - - def sample(self, population: Union[_Set[T], _Sequence[T]], k: int) -> List[T]: - """Chooses k unique random elements from a population sequence or set. - - Returns a new list containing elements from the population while - leaving the original population unchanged. The resulting list is - in selection order so that all sub-slices will also be valid random - samples. This allows raffle winners (the sample) to be partitioned - into grand prize and second place winners (the subslices). - - Members of the population need not be hashable or unique. If the - population contains repeats, then each occurrence is a possible - selection in the sample. - - To choose a sample in a range of integers, use range as an argument. - This is especially fast and space efficient for sampling from a - large population: sample(range(10000000), 60) - """ - - # Sampling without replacement entails tracking either potential - # selections (the pool) in a list or previous selections in a set. - - # When the number of selections is small compared to the - # population, then tracking selections is efficient, requiring - # only a small set and an occasional reselection. For - # a larger number of selections, the pool tracking method is - # preferred since the list takes less space than the - # set and it doesn't suffer from frequent reselections. - - if isinstance(population, _Set): - population = list(population) - if not isinstance(population, _Sequence): - raise TypeError("Population must be a sequence or set. For dicts, use list(d).") - randbelow = self._randbelow - n = len(population) - if not (0 <= k and k <= n): - raise ValueError("Sample larger than population") - result = [cast(T, None)] * k - setsize = 21 # size of a small set minus size of an empty list - if k > 5: - setsize += 4 ** _ceil(_log(k * 3, 4)) # table size for big sets - if n <= setsize: - # An n-length list is smaller than a k-length set - pool = list(population) - for i in range(k): # invariant: non-selected at [0,n-i) - j = randbelow(n-i) - result[i] = pool[j] - pool[j] = pool[n-i-1] # move non-selected item into vacancy - else: - selected = set() # type: Set[int] - selected_add = selected.add - for i in range(k): - j = randbelow(n) - while j in selected: - j = randbelow(n) - selected_add(j) - result[i] = population[j] - return result - -## -------------------- real-valued distributions ------------------- - -## -------------------- uniform distribution ------------------- - - def uniform(self, a: float, b: float) -> float: - "Get a random number in the range [a, b) or [a, b] depending on rounding." - return a + (b-a) * self.random() - -## -------------------- triangular -------------------- - - def triangular(self, low: float = 0.0, high: float = 1.0, - mode: float = None) -> float: - """Triangular distribution. - - Continuous distribution bounded by given lower and upper limits, - and having a given mode value in-between. - - http://en.wikipedia.org/wiki/Triangular_distribution - - """ - u = self.random() - c = 0.5 if mode is None else (mode - low) / (high - low) - if u > c: - u = 1.0 - u - c = 1.0 - c - low, high = high, low - return low + (high - low) * (u * c) ** 0.5 - -## -------------------- normal distribution -------------------- - - def normalvariate(self, mu: float, sigma: float) -> float: - """Normal distribution. - - mu is the mean, and sigma is the standard deviation. - - """ - # mu = mean, sigma = standard deviation - - # Uses Kinderman and Monahan method. Reference: Kinderman, - # A.J. and Monahan, J.F., "Computer generation of random - # variables using the ratio of uniform deviates", ACM Trans - # Math Software, 3, (1977), pp257-260. - - random = self.random - while 1: - u1 = random() - u2 = 1.0 - random() - z = NV_MAGICCONST*(u1-0.5)/u2 - zz = z*z/4.0 - if zz <= -_log(u2): - break - return mu + z*sigma - -## -------------------- lognormal distribution -------------------- - - def lognormvariate(self, mu: float, sigma: float) -> float: - """Log normal distribution. - - If you take the natural logarithm of this distribution, you'll get a - normal distribution with mean mu and standard deviation sigma. - mu can have any value, and sigma must be greater than zero. - - """ - return _exp(self.normalvariate(mu, sigma)) - -## -------------------- exponential distribution -------------------- - - def expovariate(self, lambd: float) -> float: - """Exponential distribution. - - lambd is 1.0 divided by the desired mean. It should be - nonzero. (The parameter would be called "lambda", but that is - a reserved word in Python.) Returned values range from 0 to - positive infinity if lambd is positive, and from negative - infinity to 0 if lambd is negative. - - """ - # lambd: rate lambd = 1/mean - # ('lambda' is a Python reserved word) - - # we use 1-random() instead of random() to preclude the - # possibility of taking the log of zero. - return -_log(1.0 - self.random())/lambd - -## -------------------- von Mises distribution -------------------- - - def vonmisesvariate(self, mu: float, kappa: float) -> float: - """Circular data distribution. - - mu is the mean angle, expressed in radians between 0 and 2*pi, and - kappa is the concentration parameter, which must be greater than or - equal to zero. If kappa is equal to zero, this distribution reduces - to a uniform random angle over the range 0 to 2*pi. - - """ - # mu: mean angle (in radians between 0 and 2*pi) - # kappa: concentration parameter kappa (>= 0) - # if kappa = 0 generate uniform random angle - - # Based upon an algorithm published in: Fisher, N.I., - # "Statistical Analysis of Circular Data", Cambridge - # University Press, 1993. - - # Thanks to Magnus Kessler for a correction to the - # implementation of step 4. - - random = self.random - if kappa <= 1e-6: - return TWOPI * random() - - a = 1.0 + _sqrt(1.0 + 4.0 * kappa * kappa) - b = (a - _sqrt(2.0 * a))/(2.0 * kappa) - r = (1.0 + b * b)/(2.0 * b) - - while 1: - u1 = random() - - z = _cos(_pi * u1) - f = (1.0 + r * z)/(r + z) - c = kappa * (r - f) - - u2 = random() - - if u2 < c * (2.0 - c) or u2 <= c * _exp(1.0 - c): - break - - u3 = random() - if u3 > 0.5: - theta = (mu % TWOPI) + _acos(f) - else: - theta = (mu % TWOPI) - _acos(f) - - return theta - -## -------------------- gamma distribution -------------------- - - def gammavariate(self, alpha: float, beta: float) -> float: - """Gamma distribution. Not the gamma function! - - Conditions on the parameters are alpha > 0 and beta > 0. - - The probability distribution function is: - - x ** (alpha - 1) * math.exp(-x / beta) - pdf(x) = -------------------------------------- - math.gamma(alpha) * beta ** alpha - - """ - - # alpha > 0, beta > 0, mean is alpha*beta, variance is alpha*beta**2 - - # Warning: a few older sources define the gamma distribution in terms - # of alpha > -1.0 - if alpha <= 0.0 or beta <= 0.0: - raise ValueError('gammavariate: alpha and beta must be > 0.0') - - random = self.random - if alpha > 1.0: - - # Uses R.C.H. Cheng, "The generation of Gamma - # variables with non-integral shape parameters", - # Applied Statistics, (1977), 26, No. 1, p71-74 - - ainv = _sqrt(2.0 * alpha - 1.0) - bbb = alpha - LOG4 - ccc = alpha + ainv - - while 1: - u1 = random() - if not (1e-7 < u1 and u1 < .9999999): - continue - u2 = 1.0 - random() - v = _log(u1/(1.0-u1))/ainv - x = alpha*_exp(v) - z = u1*u1*u2 - r = bbb+ccc*v-x - if r + SG_MAGICCONST - 4.5*z >= 0.0 or r >= _log(z): - return x * beta - - elif alpha == 1.0: - # expovariate(1) - u = random() - while u <= 1e-7: - u = random() - return -_log(u) * beta - - else: # alpha is between 0 and 1 (exclusive) - - # Uses ALGORITHM GS of Statistical Computing - Kennedy & Gentle - - while 1: - u = random() - b = (_e + alpha)/_e - p = b*u - if p <= 1.0: - x = p ** (1.0/alpha) - else: - x = -_log((b-p)/alpha) - u1 = random() - if p > 1.0: - if u1 <= x ** (alpha - 1.0): - break - elif u1 <= _exp(-x): - break - return x * beta - -## -------------------- Gauss (faster alternative) -------------------- - - def gauss(self, mu: float, sigma: float) -> float: - """Gaussian distribution. - - mu is the mean, and sigma is the standard deviation. This is - slightly faster than the normalvariate() function. - - Not thread-safe without a lock around calls. - - """ - - # When x and y are two variables from [0, 1), uniformly - # distributed, then - # - # cos(2*pi*x)*sqrt(-2*log(1-y)) - # sin(2*pi*x)*sqrt(-2*log(1-y)) - # - # are two *independent* variables with normal distribution - # (mu = 0, sigma = 1). - # (Lambert Meertens) - # (corrected version; bug discovered by Mike Miller, fixed by LM) - - # Multithreading note: When two threads call this function - # simultaneously, it is possible that they will receive the - # same return value. The window is very small though. To - # avoid this, you have to use a lock around all calls. (I - # didn't want to slow this down in the serial case by using a - # lock here.) - - random = self.random - z = self.gauss_next - self.gauss_next = None - if z is None: - x2pi = random() * TWOPI - g2rad = _sqrt(-2.0 * _log(1.0 - random())) - z = _cos(x2pi) * g2rad - self.gauss_next = _sin(x2pi) * g2rad - - return mu + z*sigma - -## -------------------- beta -------------------- -## See -## http://mail.python.org/pipermail/python-bugs-list/2001-January/003752.html -## for Ivan Frohne's insightful analysis of why the original implementation: -## -## def betavariate(self, alpha, beta): -## # Discrete Event Simulation in C, pp 87-88. -## -## y = self.expovariate(alpha) -## z = self.expovariate(1.0/beta) -## return z/(y+z) -## -## was dead wrong, and how it probably got that way. - - def betavariate(self, alpha: float, beta: float) -> 'float': - """Beta distribution. - - Conditions on the parameters are alpha > 0 and beta > 0. - Returned values range between 0 and 1. - - """ - - # This version due to Janne Sinkkonen, and matches all the std - # texts (e.g., Knuth Vol 2 Ed 3 pg 134 "the beta distribution"). - y = self.gammavariate(alpha, 1.) - if y == 0: - return 0.0 - else: - return y / (y + self.gammavariate(beta, 1.)) - -## -------------------- Pareto -------------------- - - def paretovariate(self, alpha: float) -> float: - """Pareto distribution. alpha is the shape parameter.""" - # Jain, pg. 495 - - u = 1.0 - self.random() - return 1.0 / u ** (1.0/alpha) - -## -------------------- Weibull -------------------- - - def weibullvariate(self, alpha: float, beta: float) -> float: - """Weibull distribution. - - alpha is the scale parameter and beta is the shape parameter. - - """ - # Jain, pg. 499; bug fix courtesy Bill Arms - - u = 1.0 - self.random() - return alpha * (-_log(u)) ** (1.0/beta) - -## --------------- Operating System Random Source ------------------ - -class SystemRandom(Random): - """Alternate random number generator using sources provided - by the operating system (such as /dev/urandom on Unix or - CryptGenRandom on Windows). - - Not available on all systems (see os.urandom() for details). - """ - - def random(self) -> float: - """Get the next random number in the range [0.0, 1.0).""" - return (int.from_bytes(_urandom(7), 'big') >> 3) * RECIP_BPF - - def getrandbits(self, k: int) -> int: - """getrandbits(k) -> x. Generates a long int with k random bits.""" - if k <= 0: - raise ValueError('number of bits must be greater than zero') - if k != int(k): - raise TypeError('number of bits should be an integer') - numbytes = (k + 7) // 8 # bits / 8 and rounded up - x = int.from_bytes(_urandom(numbytes), 'big') - return x >> (numbytes * 8 - k) # trim excess bits - - def seed(self, a: object = None, version: int = None) -> None: - "Stub method. Not used for a system random number generator." - return - - def _notimplemented(self, *args: Any, **kwds: Any) -> Any: - "Method should not be called for a system random number generator." - raise NotImplementedError('System entropy source does not have state.') - getstate = setstate = _notimplemented - -# Create one instance, seeded from current time, and export its methods -# as module-level functions. The functions share state across all uses -#(both in the user's code and in the Python libraries), but that's fine -# for most programs and is easier for the casual user than making them -# instantiate their own Random() instance. - -_inst = Random() -seed = _inst.seed -random = _inst.random -uniform = _inst.uniform -triangular = _inst.triangular -randint = _inst.randint -choice = _inst.choice -randrange = _inst.randrange -sample = _inst.sample -shuffle = _inst.shuffle -normalvariate = _inst.normalvariate -lognormvariate = _inst.lognormvariate -expovariate = _inst.expovariate -vonmisesvariate = _inst.vonmisesvariate -gammavariate = _inst.gammavariate -gauss = _inst.gauss -betavariate = _inst.betavariate -paretovariate = _inst.paretovariate -weibullvariate = _inst.weibullvariate -getstate = _inst.getstate -setstate = _inst.setstate -getrandbits = _inst.getrandbits - -## -------------------- test program -------------------- - -def _test_generator(n: int, func: Any, args: tuple) -> None: - import time - print(n, 'times', func.__name__) - total = 0.0 - sqsum = 0.0 - smallest = 1e10 - largest = -1e10 - t0 = time.time() - for i in range(n): - x = func(*args) # type: float - total += x - sqsum = sqsum + x*x - smallest = min(x, smallest) - largest = max(x, largest) - t1 = time.time() - print(round(t1-t0, 3), 'sec,', end=' ') - avg = total/n - stddev = _sqrt(sqsum/n - avg*avg) - print('avg %g, stddev %g, min %g, max %g' % \ - (avg, stddev, smallest, largest)) - - -def _test(N: int = 2000) -> None: - _test_generator(N, random, ()) - _test_generator(N, normalvariate, (0.0, 1.0)) - _test_generator(N, lognormvariate, (0.0, 1.0)) - _test_generator(N, vonmisesvariate, (0.0, 1.0)) - _test_generator(N, gammavariate, (0.01, 1.0)) - _test_generator(N, gammavariate, (0.1, 1.0)) - _test_generator(N, gammavariate, (0.1, 2.0)) - _test_generator(N, gammavariate, (0.5, 1.0)) - _test_generator(N, gammavariate, (0.9, 1.0)) - _test_generator(N, gammavariate, (1.0, 1.0)) - _test_generator(N, gammavariate, (2.0, 1.0)) - _test_generator(N, gammavariate, (20.0, 1.0)) - _test_generator(N, gammavariate, (200.0, 1.0)) - _test_generator(N, gauss, (0.0, 1.0)) - _test_generator(N, betavariate, (3.0, 3.0)) - _test_generator(N, triangular, (0.0, 1.0, 1.0/3.0)) - -if __name__ == '__main__': - _test() diff --git a/test-data/stdlib-samples/3.2/shutil.py b/test-data/stdlib-samples/3.2/shutil.py deleted file mode 100644 index bcefb7787952..000000000000 --- a/test-data/stdlib-samples/3.2/shutil.py +++ /dev/null @@ -1,790 +0,0 @@ -"""Utility functions for copying and archiving files and directory trees. - -XXX The functions here don't copy the resource fork or other metadata on Mac. - -""" - -import os -import sys -import stat -from os.path import abspath -import fnmatch -import collections -import errno -import tarfile -import builtins - -from typing import ( - Any, AnyStr, IO, List, Iterable, Callable, Tuple, Dict, Sequence, cast -) -from types import TracebackType - -try: - import bz2 - _BZ2_SUPPORTED = True -except ImportError: - _BZ2_SUPPORTED = False - -try: - from pwd import getpwnam as _getpwnam - getpwnam = _getpwnam -except ImportError: - getpwnam = None - -try: - from grp import getgrnam as _getgrnam - getgrnam = _getgrnam -except ImportError: - getgrnam = None - -__all__ = ["copyfileobj", "copyfile", "copymode", "copystat", "copy", "copy2", - "copytree", "move", "rmtree", "Error", "SpecialFileError", - "ExecError", "make_archive", "get_archive_formats", - "register_archive_format", "unregister_archive_format", - "get_unpack_formats", "register_unpack_format", - "unregister_unpack_format", "unpack_archive", "ignore_patterns"] - -class Error(EnvironmentError): - pass - -class SpecialFileError(EnvironmentError): - """Raised when trying to do a kind of operation (e.g. copying) which is - not supported on a special file (e.g. a named pipe)""" - -class ExecError(EnvironmentError): - """Raised when a command could not be executed""" - -class ReadError(EnvironmentError): - """Raised when an archive cannot be read""" - -class RegistryError(Exception): - """Raised when a registry operation with the archiving - and unpacking registeries fails""" - - -if sys.platform == "win32": - _WindowsError = WindowsError -else: - _WindowsError = None - - -# Function aliases to be patched in test cases -rename = os.rename -open = builtins.open - - -def copyfileobj(fsrc: IO[AnyStr], fdst: IO[AnyStr], - length: int = 16*1024) -> None: - """copy data from file-like object fsrc to file-like object fdst""" - while 1: - buf = fsrc.read(length) - if not buf: - break - fdst.write(buf) - -def _samefile(src: str, dst: str) -> bool: - # Macintosh, Unix. - if hasattr(os.path, 'samefile'): - try: - return os.path.samefile(src, dst) - except OSError: - return False - - # All other platforms: check for same pathname. - return (os.path.normcase(os.path.abspath(src)) == - os.path.normcase(os.path.abspath(dst))) - -def copyfile(src: str, dst: str) -> None: - """Copy data from src to dst""" - if _samefile(src, dst): - raise Error("`%s` and `%s` are the same file" % (src, dst)) - - for fn in [src, dst]: - try: - st = os.stat(fn) - except OSError: - # File most likely does not exist - pass - else: - # XXX What about other special files? (sockets, devices...) - if stat.S_ISFIFO(st.st_mode): - raise SpecialFileError("`%s` is a named pipe" % fn) - - with open(src, 'rb') as fsrc: - with open(dst, 'wb') as fdst: - copyfileobj(fsrc, fdst) - -def copymode(src: str, dst: str) -> None: - """Copy mode bits from src to dst""" - if hasattr(os, 'chmod'): - st = os.stat(src) - mode = stat.S_IMODE(st.st_mode) - os.chmod(dst, mode) - -def copystat(src: str, dst: str) -> None: - """Copy all stat info (mode bits, atime, mtime, flags) from src to dst""" - st = os.stat(src) - mode = stat.S_IMODE(st.st_mode) - if hasattr(os, 'utime'): - os.utime(dst, (st.st_atime, st.st_mtime)) - if hasattr(os, 'chmod'): - os.chmod(dst, mode) - if hasattr(os, 'chflags') and hasattr(st, 'st_flags'): - try: - os.chflags(dst, st.st_flags) - except OSError as why: - if (not hasattr(errno, 'EOPNOTSUPP') or - why.errno != errno.EOPNOTSUPP): - raise - -def copy(src: str, dst: str) -> None: - """Copy data and mode bits ("cp src dst"). - - The destination may be a directory. - - """ - if os.path.isdir(dst): - dst = os.path.join(dst, os.path.basename(src)) - copyfile(src, dst) - copymode(src, dst) - -def copy2(src: str, dst: str) -> None: - """Copy data and all stat info ("cp -p src dst"). - - The destination may be a directory. - - """ - if os.path.isdir(dst): - dst = os.path.join(dst, os.path.basename(src)) - copyfile(src, dst) - copystat(src, dst) - -def ignore_patterns(*patterns: str) -> Callable[[str, List[str]], - Iterable[str]]: - """Function that can be used as copytree() ignore parameter. - - Patterns is a sequence of glob-style patterns - that are used to exclude files""" - def _ignore_patterns(path: str, names: List[str]) -> Iterable[str]: - ignored_names = [] # type: List[str] - for pattern in patterns: - ignored_names.extend(fnmatch.filter(names, pattern)) - return set(ignored_names) - return _ignore_patterns - -def copytree(src: str, dst: str, symlinks: bool = False, - ignore: Callable[[str, List[str]], Iterable[str]] = None, - copy_function: Callable[[str, str], None] = copy2, - ignore_dangling_symlinks: bool = False) -> None: - """Recursively copy a directory tree. - - The destination directory must not already exist. - If exception(s) occur, an Error is raised with a list of reasons. - - If the optional symlinks flag is true, symbolic links in the - source tree result in symbolic links in the destination tree; if - it is false, the contents of the files pointed to by symbolic - links are copied. If the file pointed by the symlink doesn't - exist, an exception will be added in the list of errors raised in - an Error exception at the end of the copy process. - - You can set the optional ignore_dangling_symlinks flag to true if you - want to silence this exception. Notice that this has no effect on - platforms that don't support os.symlink. - - The optional ignore argument is a callable. If given, it - is called with the `src` parameter, which is the directory - being visited by copytree(), and `names` which is the list of - `src` contents, as returned by os.listdir(): - - callable(src, names) -> ignored_names - - Since copytree() is called recursively, the callable will be - called once for each directory that is copied. It returns a - list of names relative to the `src` directory that should - not be copied. - - The optional copy_function argument is a callable that will be used - to copy each file. It will be called with the source path and the - destination path as arguments. By default, copy2() is used, but any - function that supports the same signature (like copy()) can be used. - - """ - names = os.listdir(src) - if ignore is not None: - ignored_names = ignore(src, names) - else: - ignored_names = set() - - os.makedirs(dst) - errors = [] # type: List[Tuple[str, str, str]] - for name in names: - if name in ignored_names: - continue - srcname = os.path.join(src, name) - dstname = os.path.join(dst, name) - try: - if os.path.islink(srcname): - linkto = os.readlink(srcname) - if symlinks: - os.symlink(linkto, dstname) - else: - # ignore dangling symlink if the flag is on - if not os.path.exists(linkto) and ignore_dangling_symlinks: - continue - # otherwise let the copy occurs. copy2 will raise an error - copy_function(srcname, dstname) - elif os.path.isdir(srcname): - copytree(srcname, dstname, symlinks, ignore, copy_function) - else: - # Will raise a SpecialFileError for unsupported file types - copy_function(srcname, dstname) - # catch the Error from the recursive copytree so that we can - # continue with other files - except Error as err: - errors.extend(err.args[0]) - except EnvironmentError as why: - errors.append((srcname, dstname, str(why))) - try: - copystat(src, dst) - except OSError as why: - if _WindowsError is not None and isinstance(why, _WindowsError): - # Copying file access times may fail on Windows - pass - else: - errors.append((src, dst, str(why))) - if errors: - raise Error(errors) - -def rmtree(path: str, ignore_errors: bool = False, - onerror: Callable[[Any, str, Tuple[type, BaseException, TracebackType]], - None] = None) -> None: - """Recursively delete a directory tree. - - If ignore_errors is set, errors are ignored; otherwise, if onerror - is set, it is called to handle the error with arguments (func, - path, exc_info) where func is os.listdir, os.remove, or os.rmdir; - path is the argument to that function that caused it to fail; and - exc_info is a tuple returned by sys.exc_info(). If ignore_errors - is false and onerror is None, an exception is raised. - - """ - if ignore_errors: - def _onerror(x: Any, y: str, - z: Tuple[type, BaseException, TracebackType]) -> None: - pass - onerror = _onerror - elif onerror is None: - def __onerror(x: Any, y: str, - z: Tuple[type, BaseException, TracebackType]) -> None: - raise - onerror = __onerror - try: - if os.path.islink(path): - # symlinks to directories are forbidden, see bug #1669 - raise OSError("Cannot call rmtree on a symbolic link") - except OSError: - onerror(os.path.islink, path, sys.exc_info()) - # can't continue even if onerror hook returns - return - names = [] # type: List[str] - try: - names = os.listdir(path) - except os.error as err: - onerror(os.listdir, path, sys.exc_info()) - for name in names: - fullname = os.path.join(path, name) - try: - mode = os.lstat(fullname).st_mode - except os.error: - mode = 0 - if stat.S_ISDIR(mode): - rmtree(fullname, ignore_errors, onerror) - else: - try: - os.remove(fullname) - except os.error as err: - onerror(os.remove, fullname, sys.exc_info()) - try: - os.rmdir(path) - except os.error: - onerror(os.rmdir, path, sys.exc_info()) - - -def _basename(path: str) -> str: - # A basename() variant which first strips the trailing slash, if present. - # Thus we always get the last component of the path, even for directories. - return os.path.basename(path.rstrip(os.path.sep)) - -def move(src: str, dst: str) -> None: - """Recursively move a file or directory to another location. This is - similar to the Unix "mv" command. - - If the destination is a directory or a symlink to a directory, the source - is moved inside the directory. The destination path must not already - exist. - - If the destination already exists but is not a directory, it may be - overwritten depending on os.rename() semantics. - - If the destination is on our current filesystem, then rename() is used. - Otherwise, src is copied to the destination and then removed. - A lot more could be done here... A look at a mv.c shows a lot of - the issues this implementation glosses over. - - """ - real_dst = dst - if os.path.isdir(dst): - if _samefile(src, dst): - # We might be on a case insensitive filesystem, - # perform the rename anyway. - os.rename(src, dst) - return - - real_dst = os.path.join(dst, _basename(src)) - if os.path.exists(real_dst): - raise Error("Destination path '%s' already exists" % real_dst) - try: - os.rename(src, real_dst) - except OSError as exc: - if os.path.isdir(src): - if _destinsrc(src, dst): - raise Error("Cannot move a directory '%s' into itself '%s'." % (src, dst)) - copytree(src, real_dst, symlinks=True) - rmtree(src) - else: - copy2(src, real_dst) - os.unlink(src) - -def _destinsrc(src: str, dst: str) -> bool: - src = abspath(src) - dst = abspath(dst) - if not src.endswith(os.path.sep): - src += os.path.sep - if not dst.endswith(os.path.sep): - dst += os.path.sep - return dst.startswith(src) - -def _get_gid(name: str) -> int: - """Returns a gid, given a group name.""" - if getgrnam is None or name is None: - return None - try: - result = getgrnam(name) - except KeyError: - result = None - if result is not None: - return result.gr_gid - return None - -def _get_uid(name: str) -> int: - """Returns an uid, given a user name.""" - if getpwnam is None or name is None: - return None - try: - result = getpwnam(name) - except KeyError: - result = None - if result is not None: - return result.pw_uid - return None - -def _make_tarball(base_name: str, base_dir: str, compress: str = "gzip", - verbose: bool = False, dry_run: bool = False, - owner: str = None, group: str = None, - logger: Any = None) -> str: - """Create a (possibly compressed) tar file from all the files under - 'base_dir'. - - 'compress' must be "gzip" (the default), "bzip2", or None. - - 'owner' and 'group' can be used to define an owner and a group for the - archive that is being built. If not provided, the current owner and group - will be used. - - The output tar file will be named 'base_name' + ".tar", possibly plus - the appropriate compression extension (".gz", or ".bz2"). - - Returns the output filename. - """ - tar_compression = {'gzip': 'gz', None: ''} - compress_ext = {'gzip': '.gz'} - - if _BZ2_SUPPORTED: - tar_compression['bzip2'] = 'bz2' - compress_ext['bzip2'] = '.bz2' - - # flags for compression program, each element of list will be an argument - if compress is not None and compress not in compress_ext.keys(): - raise ValueError("bad value for 'compress', or compression format not " - "supported : {0}".format(compress)) - - archive_name = base_name + '.tar' + compress_ext.get(compress, '') - archive_dir = os.path.dirname(archive_name) - - if not os.path.exists(archive_dir): - if logger is not None: - logger.info("creating %s", archive_dir) - if not dry_run: - os.makedirs(archive_dir) - - # creating the tarball - if logger is not None: - logger.info('Creating tar archive') - - uid = _get_uid(owner) - gid = _get_gid(group) - - def _set_uid_gid(tarinfo): - if gid is not None: - tarinfo.gid = gid - tarinfo.gname = group - if uid is not None: - tarinfo.uid = uid - tarinfo.uname = owner - return tarinfo - - if not dry_run: - tar = tarfile.open(archive_name, 'w|%s' % tar_compression[compress]) - try: - tar.add(base_dir, filter=_set_uid_gid) - finally: - tar.close() - - return archive_name - -def _call_external_zip(base_dir: str, zip_filename: str, verbose: bool = False, - dry_run: bool = False) -> None: - # XXX see if we want to keep an external call here - if verbose: - zipoptions = "-r" - else: - zipoptions = "-rq" - from distutils.errors import DistutilsExecError - from distutils.spawn import spawn - try: - spawn(["zip", zipoptions, zip_filename, base_dir], dry_run=dry_run) - except DistutilsExecError: - # XXX really should distinguish between "couldn't find - # external 'zip' command" and "zip failed". - raise ExecError(("unable to create zip file '%s': " - "could neither import the 'zipfile' module nor " - "find a standalone zip utility") % zip_filename) - -def _make_zipfile(base_name: str, base_dir: str, verbose: bool = False, - dry_run: bool = False, logger: Any = None) -> str: - """Create a zip file from all the files under 'base_dir'. - - The output zip file will be named 'base_name' + ".zip". Uses either the - "zipfile" Python module (if available) or the InfoZIP "zip" utility - (if installed and found on the default search path). If neither tool is - available, raises ExecError. Returns the name of the output zip - file. - """ - zip_filename = base_name + ".zip" - archive_dir = os.path.dirname(base_name) - - if not os.path.exists(archive_dir): - if logger is not None: - logger.info("creating %s", archive_dir) - if not dry_run: - os.makedirs(archive_dir) - - # If zipfile module is not available, try spawning an external 'zip' - # command. - try: - import zipfile - except ImportError: - zipfile = None - - if zipfile is None: - _call_external_zip(base_dir, zip_filename, verbose, dry_run) - else: - if logger is not None: - logger.info("creating '%s' and adding '%s' to it", - zip_filename, base_dir) - - if not dry_run: - zip = zipfile.ZipFile(zip_filename, "w", - compression=zipfile.ZIP_DEFLATED) - - for dirpath, dirnames, filenames in os.walk(base_dir): - for name in filenames: - path = os.path.normpath(os.path.join(dirpath, name)) - if os.path.isfile(path): - zip.write(path, path) - if logger is not None: - logger.info("adding '%s'", path) - zip.close() - - return zip_filename - -_ARCHIVE_FORMATS = { - 'gztar': (_make_tarball, [('compress', 'gzip')], "gzip'ed tar-file"), - 'tar': (_make_tarball, [('compress', None)], "uncompressed tar file"), - 'zip': (_make_zipfile, [],"ZIP file") - } # type: Dict[str, Tuple[Any, Sequence[Tuple[str, str]], str]] - -if _BZ2_SUPPORTED: - _ARCHIVE_FORMATS['bztar'] = (_make_tarball, [('compress', 'bzip2')], - "bzip2'ed tar-file") - -def get_archive_formats() -> List[Tuple[str, str]]: - """Returns a list of supported formats for archiving and unarchiving. - - Each element of the returned sequence is a tuple (name, description) - """ - formats = [(name, registry[2]) for name, registry in - _ARCHIVE_FORMATS.items()] - formats.sort() - return formats - -def register_archive_format(name: str, function: Any, - extra_args: Sequence[Tuple[str, Any]] = None, - description: str = '') -> None: - """Registers an archive format. - - name is the name of the format. function is the callable that will be - used to create archives. If provided, extra_args is a sequence of - (name, value) tuples that will be passed as arguments to the callable. - description can be provided to describe the format, and will be returned - by the get_archive_formats() function. - """ - if extra_args is None: - extra_args = [] - if not callable(function): - raise TypeError('The %s object is not callable' % function) - if not isinstance(extra_args, (tuple, list)): - raise TypeError('extra_args needs to be a sequence') - for element in extra_args: - if not isinstance(element, (tuple, list)) or len(cast(tuple, element)) !=2 : - raise TypeError('extra_args elements are : (arg_name, value)') - - _ARCHIVE_FORMATS[name] = (function, extra_args, description) - -def unregister_archive_format(name: str) -> None: - del _ARCHIVE_FORMATS[name] - -def make_archive(base_name: str, format: str, root_dir: str = None, - base_dir: str = None, verbose: bool = False, - dry_run: bool = False, owner: str = None, - group: str = None, logger: Any = None) -> str: - """Create an archive file (eg. zip or tar). - - 'base_name' is the name of the file to create, minus any format-specific - extension; 'format' is the archive format: one of "zip", "tar", "bztar" - or "gztar". - - 'root_dir' is a directory that will be the root directory of the - archive; ie. we typically chdir into 'root_dir' before creating the - archive. 'base_dir' is the directory where we start archiving from; - ie. 'base_dir' will be the common prefix of all files and - directories in the archive. 'root_dir' and 'base_dir' both default - to the current directory. Returns the name of the archive file. - - 'owner' and 'group' are used when creating a tar archive. By default, - uses the current owner and group. - """ - save_cwd = os.getcwd() - if root_dir is not None: - if logger is not None: - logger.debug("changing into '%s'", root_dir) - base_name = os.path.abspath(base_name) - if not dry_run: - os.chdir(root_dir) - - if base_dir is None: - base_dir = os.curdir - - kwargs = {'dry_run': dry_run, 'logger': logger} - - try: - format_info = _ARCHIVE_FORMATS[format] - except KeyError: - raise ValueError("unknown archive format '%s'" % format) - - func = format_info[0] - for arg, val in format_info[1]: - kwargs[arg] = val - - if format != 'zip': - kwargs['owner'] = owner - kwargs['group'] = group - - try: - filename = func(base_name, base_dir, **kwargs) - finally: - if root_dir is not None: - if logger is not None: - logger.debug("changing back to '%s'", save_cwd) - os.chdir(save_cwd) - - return filename - - -def get_unpack_formats() -> List[Tuple[str, List[str], str]]: - """Returns a list of supported formats for unpacking. - - Each element of the returned sequence is a tuple - (name, extensions, description) - """ - formats = [(name, info[0], info[3]) for name, info in - _UNPACK_FORMATS.items()] - formats.sort() - return formats - -def _check_unpack_options(extensions: List[str], function: Any, - extra_args: Sequence[Tuple[str, Any]]) -> None: - """Checks what gets registered as an unpacker.""" - # first make sure no other unpacker is registered for this extension - existing_extensions = {} # type: Dict[str, str] - for name, info in _UNPACK_FORMATS.items(): - for ext in info[0]: - existing_extensions[ext] = name - - for extension in extensions: - if extension in existing_extensions: - msg = '%s is already registered for "%s"' - raise RegistryError(msg % (extension, - existing_extensions[extension])) - - if not callable(function): - raise TypeError('The registered function must be a callable') - - -def register_unpack_format(name: str, extensions: List[str], function: Any, - extra_args: Sequence[Tuple[str, Any]] = None, - description: str = '') -> None: - """Registers an unpack format. - - `name` is the name of the format. `extensions` is a list of extensions - corresponding to the format. - - `function` is the callable that will be - used to unpack archives. The callable will receive archives to unpack. - If it's unable to handle an archive, it needs to raise a ReadError - exception. - - If provided, `extra_args` is a sequence of - (name, value) tuples that will be passed as arguments to the callable. - description can be provided to describe the format, and will be returned - by the get_unpack_formats() function. - """ - if extra_args is None: - extra_args = [] - _check_unpack_options(extensions, function, extra_args) - _UNPACK_FORMATS[name] = extensions, function, extra_args, description - -def unregister_unpack_format(name: str) -> None: - """Removes the pack format from the registry.""" - del _UNPACK_FORMATS[name] - -def _ensure_directory(path: str) -> None: - """Ensure that the parent directory of `path` exists""" - dirname = os.path.dirname(path) - if not os.path.isdir(dirname): - os.makedirs(dirname) - -def _unpack_zipfile(filename: str, extract_dir: str) -> None: - """Unpack zip `filename` to `extract_dir` - """ - try: - import zipfile - except ImportError: - raise ReadError('zlib not supported, cannot unpack this archive.') - - if not zipfile.is_zipfile(filename): - raise ReadError("%s is not a zip file" % filename) - - zip = zipfile.ZipFile(filename) - try: - for info in zip.infolist(): - name = info.filename - - # don't extract absolute paths or ones with .. in them - if name.startswith('/') or '..' in name: - continue - - target = os.path.join(extract_dir, *name.split('/')) - if not target: - continue - - _ensure_directory(target) - if not name.endswith('/'): - # file - data = zip.read(info.filename) - f = open(target,'wb') - try: - f.write(data) - finally: - f.close() - del data - finally: - zip.close() - -def _unpack_tarfile(filename: str, extract_dir: str) -> None: - """Unpack tar/tar.gz/tar.bz2 `filename` to `extract_dir` - """ - try: - tarobj = tarfile.open(filename) - except tarfile.TarError: - raise ReadError( - "%s is not a compressed or uncompressed tar file" % filename) - try: - tarobj.extractall(extract_dir) - finally: - tarobj.close() - -_UNPACK_FORMATS = { - 'gztar': (['.tar.gz', '.tgz'], _unpack_tarfile, [], "gzip'ed tar-file"), - 'tar': (['.tar'], _unpack_tarfile, [], "uncompressed tar file"), - 'zip': (['.zip'], _unpack_zipfile, [], "ZIP file") - } # type: Dict[str, Tuple[List[str], Any, Sequence[Tuple[str, Any]], str]] - -if _BZ2_SUPPORTED: - _UNPACK_FORMATS['bztar'] = (['.bz2'], _unpack_tarfile, [], - "bzip2'ed tar-file") - -def _find_unpack_format(filename: str) -> str: - for name, info in _UNPACK_FORMATS.items(): - for extension in info[0]: - if filename.endswith(extension): - return name - return None - -def unpack_archive(filename: str, extract_dir: str = None, - format: str = None) -> None: - """Unpack an archive. - - `filename` is the name of the archive. - - `extract_dir` is the name of the target directory, where the archive - is unpacked. If not provided, the current working directory is used. - - `format` is the archive format: one of "zip", "tar", or "gztar". Or any - other registered format. If not provided, unpack_archive will use the - filename extension and see if an unpacker was registered for that - extension. - - In case none is found, a ValueError is raised. - """ - if extract_dir is None: - extract_dir = os.getcwd() - - if format is not None: - try: - format_info = _UNPACK_FORMATS[format] - except KeyError: - raise ValueError("Unknown unpack format '{0}'".format(format)) - - func = format_info[1] - func(filename, extract_dir, **dict(format_info[2])) - else: - # we need to look at the registered unpackers supported extensions - format = _find_unpack_format(filename) - if format is None: - raise ReadError("Unknown archive format '{0}'".format(filename)) - - func = _UNPACK_FORMATS[format][1] - kwargs = dict(_UNPACK_FORMATS[format][2]) - func(filename, extract_dir, **kwargs) diff --git a/test-data/stdlib-samples/3.2/tempfile.py b/test-data/stdlib-samples/3.2/tempfile.py deleted file mode 100644 index fa4059276fcb..000000000000 --- a/test-data/stdlib-samples/3.2/tempfile.py +++ /dev/null @@ -1,724 +0,0 @@ -"""Temporary files. - -This module provides generic, low- and high-level interfaces for -creating temporary files and directories. The interfaces listed -as "safe" just below can be used without fear of race conditions. -Those listed as "unsafe" cannot, and are provided for backward -compatibility only. - -This module also provides some data items to the user: - - TMP_MAX - maximum number of names that will be tried before - giving up. - template - the default prefix for all temporary names. - You may change this to control the default prefix. - tempdir - If this is set to a string before the first use of - any routine from this module, it will be considered as - another candidate location to store temporary files. -""" - -__all__ = [ - "NamedTemporaryFile", "TemporaryFile", # high level safe interfaces - "SpooledTemporaryFile", "TemporaryDirectory", - "mkstemp", "mkdtemp", # low level safe interfaces - "mktemp", # deprecated unsafe interface - "TMP_MAX", "gettempprefix", # constants - "tempdir", "gettempdir" - ] - - -# Imports. - -import warnings as _warnings -import sys as _sys -import io as _io -import os as _os -import errno as _errno -from random import Random as _Random - -from typing import ( - Any as _Any, Callable as _Callable, Iterator as _Iterator, - List as _List, Tuple as _Tuple, Dict as _Dict, Iterable as _Iterable, - IO as _IO, cast as _cast, Optional as _Optional, Type as _Type, -) -from typing_extensions import Literal -from types import TracebackType as _TracebackType - -try: - import fcntl as _fcntl -except ImportError: - def _set_cloexec(fd: int) -> None: - pass -else: - def _set_cloexec(fd: int) -> None: - try: - flags = _fcntl.fcntl(fd, _fcntl.F_GETFD, 0) - except IOError: - pass - else: - # flags read successfully, modify - flags |= _fcntl.FD_CLOEXEC - _fcntl.fcntl(fd, _fcntl.F_SETFD, flags) - - -try: - import _thread - _allocate_lock = _thread.allocate_lock # type: _Callable[[], _Any] -except ImportError: - import _dummy_thread - _allocate_lock = _dummy_thread.allocate_lock - -_text_openflags = _os.O_RDWR | _os.O_CREAT | _os.O_EXCL -if hasattr(_os, 'O_NOINHERIT'): - _text_openflags |= _os.O_NOINHERIT -if hasattr(_os, 'O_NOFOLLOW'): - _text_openflags |= _os.O_NOFOLLOW - -_bin_openflags = _text_openflags -if hasattr(_os, 'O_BINARY'): - _bin_openflags |= _os.O_BINARY - -if hasattr(_os, 'TMP_MAX'): - TMP_MAX = _os.TMP_MAX -else: - TMP_MAX = 10000 - -template = "tmp" - -# Internal routines. - -_once_lock = _allocate_lock() - -if hasattr(_os, "lstat"): - _stat = _os.lstat # type: _Callable[[str], object] -elif hasattr(_os, "stat"): - _stat = _os.stat -else: - # Fallback. All we need is something that raises os.error if the - # file doesn't exist. - def __stat(fn: str) -> object: - try: - f = open(fn) - except IOError: - raise _os.error() - f.close() - return None - _stat = __stat - -def _exists(fn: str) -> bool: - try: - _stat(fn) - except _os.error: - return False - else: - return True - -class _RandomNameSequence(_Iterator[str]): - """An instance of _RandomNameSequence generates an endless - sequence of unpredictable strings which can safely be incorporated - into file names. Each string is six characters long. Multiple - threads can safely use the same instance at the same time. - - _RandomNameSequence is an iterator.""" - - characters = "abcdefghijklmnopqrstuvwxyz0123456789_" - - @property - def rng(self) -> _Random: - cur_pid = _os.getpid() - if cur_pid != getattr(self, '_rng_pid', None): - self._rng = _Random() - self._rng_pid = cur_pid - return self._rng - - def __iter__(self) -> _Iterator[str]: - return self - - def __next__(self) -> str: - c = self.characters - choose = self.rng.choice - letters = [choose(c) for dummy in "123456"] - return ''.join(letters) - -def _candidate_tempdir_list() -> _List[str]: - """Generate a list of candidate temporary directories which - _get_default_tempdir will try.""" - - dirlist = [] # type: _List[str] - - # First, try the environment. - for envname in 'TMPDIR', 'TEMP', 'TMP': - dirname = _os.getenv(envname) - if dirname: dirlist.append(dirname) - - # Failing that, try OS-specific locations. - if _os.name == 'nt': - dirlist.extend([ r'c:\temp', r'c:\tmp', r'\temp', r'\tmp' ]) - else: - dirlist.extend([ '/tmp', '/var/tmp', '/usr/tmp' ]) - - # As a last resort, the current directory. - try: - dirlist.append(_os.getcwd()) - except (AttributeError, _os.error): - dirlist.append(_os.curdir) - - return dirlist - -def _get_default_tempdir() -> str: - """Calculate the default directory to use for temporary files. - This routine should be called exactly once. - - We determine whether or not a candidate temp dir is usable by - trying to create and write to a file in that directory. If this - is successful, the test file is deleted. To prevent denial of - service, the name of the test file must be randomized.""" - - namer = _RandomNameSequence() - dirlist = _candidate_tempdir_list() - - for dir in dirlist: - if dir != _os.curdir: - dir = _os.path.normcase(_os.path.abspath(dir)) - # Try only a few names per directory. - for seq in range(100): - name = next(namer) - filename = _os.path.join(dir, name) - try: - fd = _os.open(filename, _bin_openflags, 0o600) - fp = _io.open(fd, 'wb') - fp.write(b'blat') - fp.close() - _os.unlink(filename) - fp = fd = None - return dir - except (OSError, IOError) as e: - if e.args[0] != _errno.EEXIST: - break # no point trying more names in this directory - pass - raise IOError(_errno.ENOENT, - "No usable temporary directory found in %s" % dirlist) - -_name_sequence = None # type: _RandomNameSequence - -def _get_candidate_names() -> _RandomNameSequence: - """Common setup sequence for all user-callable interfaces.""" - - global _name_sequence - if _name_sequence is None: - _once_lock.acquire() - try: - if _name_sequence is None: - _name_sequence = _RandomNameSequence() - finally: - _once_lock.release() - return _name_sequence - - -def _mkstemp_inner(dir: str, pre: str, suf: str, - flags: int) -> _Tuple[int, str]: - """Code common to mkstemp, TemporaryFile, and NamedTemporaryFile.""" - - names = _get_candidate_names() - - for seq in range(TMP_MAX): - name = next(names) - file = _os.path.join(dir, pre + name + suf) - try: - fd = _os.open(file, flags, 0o600) - _set_cloexec(fd) - return (fd, _os.path.abspath(file)) - except OSError as e: - if e.errno == _errno.EEXIST: - continue # try again - raise - - raise IOError(_errno.EEXIST, "No usable temporary file name found") - - -# User visible interfaces. - -def gettempprefix() -> str: - """Accessor for tempdir.template.""" - return template - -tempdir = None # type: str - -def gettempdir() -> str: - """Accessor for tempfile.tempdir.""" - global tempdir - if tempdir is None: - _once_lock.acquire() - try: - if tempdir is None: - tempdir = _get_default_tempdir() - finally: - _once_lock.release() - return tempdir - -def mkstemp(suffix: str = "", prefix: str = template, dir: str = None, - text: bool = False) -> _Tuple[int, str]: - """User-callable function to create and return a unique temporary - file. The return value is a pair (fd, name) where fd is the - file descriptor returned by os.open, and name is the filename. - - If 'suffix' is specified, the file name will end with that suffix, - otherwise there will be no suffix. - - If 'prefix' is specified, the file name will begin with that prefix, - otherwise a default prefix is used. - - If 'dir' is specified, the file will be created in that directory, - otherwise a default directory is used. - - If 'text' is specified and true, the file is opened in text - mode. Else (the default) the file is opened in binary mode. On - some operating systems, this makes no difference. - - The file is readable and writable only by the creating user ID. - If the operating system uses permission bits to indicate whether a - file is executable, the file is executable by no one. The file - descriptor is not inherited by children of this process. - - Caller is responsible for deleting the file when done with it. - """ - - if dir is None: - dir = gettempdir() - - if text: - flags = _text_openflags - else: - flags = _bin_openflags - - return _mkstemp_inner(dir, prefix, suffix, flags) - - -def mkdtemp(suffix: str = "", prefix: str = template, dir: str = None) -> str: - """User-callable function to create and return a unique temporary - directory. The return value is the pathname of the directory. - - Arguments are as for mkstemp, except that the 'text' argument is - not accepted. - - The directory is readable, writable, and searchable only by the - creating user. - - Caller is responsible for deleting the directory when done with it. - """ - - if dir is None: - dir = gettempdir() - - names = _get_candidate_names() - - for seq in range(TMP_MAX): - name = next(names) - file = _os.path.join(dir, prefix + name + suffix) - try: - _os.mkdir(file, 0o700) - return file - except OSError as e: - if e.errno == _errno.EEXIST: - continue # try again - raise - - raise IOError(_errno.EEXIST, "No usable temporary directory name found") - -def mktemp(suffix: str = "", prefix: str = template, dir: str = None) -> str: - """User-callable function to return a unique temporary file name. The - file is not created. - - Arguments are as for mkstemp, except that the 'text' argument is - not accepted. - - This function is unsafe and should not be used. The file name - refers to a file that did not exist at some point, but by the time - you get around to creating it, someone else may have beaten you to - the punch. - """ - -## from warnings import warn as _warn -## _warn("mktemp is a potential security risk to your program", -## RuntimeWarning, stacklevel=2) - - if dir is None: - dir = gettempdir() - - names = _get_candidate_names() - for seq in range(TMP_MAX): - name = next(names) - file = _os.path.join(dir, prefix + name + suffix) - if not _exists(file): - return file - - raise IOError(_errno.EEXIST, "No usable temporary filename found") - - -class _TemporaryFileWrapper: - """Temporary file wrapper - - This class provides a wrapper around files opened for - temporary use. In particular, it seeks to automatically - remove the file when it is no longer needed. - """ - - def __init__(self, file: _IO[_Any], name: str, - delete: bool = True) -> None: - self.file = file - self.name = name - self.close_called = False - self.delete = delete - - if _os.name != 'nt': - # Cache the unlinker so we don't get spurious errors at - # shutdown when the module-level "os" is None'd out. Note - # that this must be referenced as self.unlink, because the - # name TemporaryFileWrapper may also get None'd out before - # __del__ is called. - self.unlink = _os.unlink - - def __getattr__(self, name: str) -> _Any: - # Attribute lookups are delegated to the underlying file - # and cached for non-numeric results - # (i.e. methods are cached, closed and friends are not) - file = _cast(_Any, self).__dict__['file'] # type: _IO[_Any] - a = getattr(file, name) - if not isinstance(a, int): - setattr(self, name, a) - return a - - # The underlying __enter__ method returns the wrong object - # (self.file) so override it to return the wrapper - def __enter__(self) -> '_TemporaryFileWrapper': - self.file.__enter__() - return self - - # iter() doesn't use __getattr__ to find the __iter__ method - def __iter__(self) -> _Iterator[_Any]: - return iter(self.file) - - # NT provides delete-on-close as a primitive, so we don't need - # the wrapper to do anything special. We still use it so that - # file.name is useful (i.e. not "(fdopen)") with NamedTemporaryFile. - if _os.name != 'nt': - def close(self) -> None: - if not self.close_called: - self.close_called = True - self.file.close() - if self.delete: - self.unlink(self.name) - - def __del__(self) -> None: - self.close() - - # Need to trap __exit__ as well to ensure the file gets - # deleted when used in a with statement - def __exit__(self, exc: _Type[BaseException], value: BaseException, - tb: _Optional[_TracebackType]) -> bool: - result = self.file.__exit__(exc, value, tb) - self.close() - return result - else: - def __exit__(self, # type: ignore[misc] - exc: _Type[BaseException], - value: BaseException, - tb: _Optional[_TracebackType]) -> Literal[False]: - self.file.__exit__(exc, value, tb) - return False - - -def NamedTemporaryFile(mode: str = 'w+b', buffering: int = -1, - encoding: str = None, newline: str = None, - suffix: str = "", prefix: str = template, - dir: str = None, delete: bool = True) -> _IO[_Any]: - """Create and return a temporary file. - Arguments: - 'prefix', 'suffix', 'dir' -- as for mkstemp. - 'mode' -- the mode argument to io.open (default "w+b"). - 'buffering' -- the buffer size argument to io.open (default -1). - 'encoding' -- the encoding argument to io.open (default None) - 'newline' -- the newline argument to io.open (default None) - 'delete' -- whether the file is deleted on close (default True). - The file is created as mkstemp() would do it. - - Returns an object with a file-like interface; the name of the file - is accessible as file.name. The file will be automatically deleted - when it is closed unless the 'delete' argument is set to False. - """ - - if dir is None: - dir = gettempdir() - - flags = _bin_openflags - - # Setting O_TEMPORARY in the flags causes the OS to delete - # the file when it is closed. This is only supported by Windows. - if _os.name == 'nt' and delete: - flags |= _os.O_TEMPORARY - - (fd, name) = _mkstemp_inner(dir, prefix, suffix, flags) - file = _io.open(fd, mode, buffering=buffering, - newline=newline, encoding=encoding) - - return _cast(_IO[_Any], _TemporaryFileWrapper(file, name, delete)) - -if _os.name != 'posix' or _sys.platform == 'cygwin': - # On non-POSIX and Cygwin systems, assume that we cannot unlink a file - # while it is open. - TemporaryFile = NamedTemporaryFile - -else: - def _TemporaryFile(mode: str = 'w+b', buffering: int = -1, - encoding: str = None, newline: str = None, - suffix: str = "", prefix: str = template, - dir: str = None, delete: bool = True) -> _IO[_Any]: - """Create and return a temporary file. - Arguments: - 'prefix', 'suffix', 'dir' -- as for mkstemp. - 'mode' -- the mode argument to io.open (default "w+b"). - 'buffering' -- the buffer size argument to io.open (default -1). - 'encoding' -- the encoding argument to io.open (default None) - 'newline' -- the newline argument to io.open (default None) - The file is created as mkstemp() would do it. - - Returns an object with a file-like interface. The file has no - name, and will cease to exist when it is closed. - """ - - if dir is None: - dir = gettempdir() - - flags = _bin_openflags - - (fd, name) = _mkstemp_inner(dir, prefix, suffix, flags) - try: - _os.unlink(name) - return _io.open(fd, mode, buffering=buffering, - newline=newline, encoding=encoding) - except: - _os.close(fd) - raise - TemporaryFile = _TemporaryFile - -class SpooledTemporaryFile: - """Temporary file wrapper, specialized to switch from - StringIO to a real file when it exceeds a certain size or - when a fileno is needed. - """ - _rolled = False - _file = None # type: _Any # BytesIO, StringIO or TemporaryFile - - def __init__(self, max_size: int = 0, mode: str = 'w+b', - buffering: int = -1, encoding: str = None, - newline: str = None, suffix: str = "", - prefix: str = template, dir: str = None) -> None: - if 'b' in mode: - self._file = _io.BytesIO() - else: - # Setting newline="\n" avoids newline translation; - # this is important because otherwise on Windows we'd - # hget double newline translation upon rollover(). - self._file = _io.StringIO(newline="\n") - self._max_size = max_size - self._rolled = False - self._TemporaryFileArgs = { - 'mode': mode, 'buffering': buffering, - 'suffix': suffix, 'prefix': prefix, - 'encoding': encoding, 'newline': newline, - 'dir': dir} # type: _Dict[str, _Any] - - def _check(self, file: _IO[_Any]) -> None: - if self._rolled: return - max_size = self._max_size - if max_size and file.tell() > max_size: - self.rollover() - - def rollover(self) -> None: - if self._rolled: return - file = self._file - newfile = self._file = TemporaryFile(**self._TemporaryFileArgs) - self._TemporaryFileArgs = None - - newfile.write(file.getvalue()) - newfile.seek(file.tell(), 0) - - self._rolled = True - - # The method caching trick from NamedTemporaryFile - # won't work here, because _file may change from a - # _StringIO instance to a real file. So we list - # all the methods directly. - - # Context management protocol - def __enter__(self) -> 'SpooledTemporaryFile': - if self._file.closed: - raise ValueError("Cannot enter context with closed file") - return self - - def __exit__(self, exc: type, value: BaseException, - tb: _TracebackType) -> Literal[False]: - self._file.close() - return False - - # file protocol - def __iter__(self) -> _Iterable[_Any]: - return self._file.__iter__() - - def close(self) -> None: - self._file.close() - - @property - def closed(self) -> bool: - return self._file.closed - - @property - def encoding(self) -> str: - return self._file.encoding - - def fileno(self) -> int: - self.rollover() - return self._file.fileno() - - def flush(self) -> None: - self._file.flush() - - def isatty(self) -> bool: - return self._file.isatty() - - @property - def mode(self) -> str: - return self._file.mode - - @property - def name(self) -> str: - return self._file.name - - @property - def newlines(self) -> _Any: - return self._file.newlines - - #def next(self): - # return self._file.next - - def read(self, n: int = -1) -> _Any: - return self._file.read(n) - - def readline(self, limit: int = -1) -> _Any: - return self._file.readline(limit) - - def readlines(self, *args) -> _List[_Any]: - return self._file.readlines(*args) - - def seek(self, offset: int, whence: int = 0) -> None: - self._file.seek(offset, whence) - - @property - def softspace(self) -> bool: - return self._file.softspace - - def tell(self) -> int: - return self._file.tell() - - def truncate(self) -> None: - self._file.truncate() - - def write(self, s: _Any) -> int: - file = self._file # type: _IO[_Any] - rv = file.write(s) - self._check(file) - return rv - - def writelines(self, iterable: _Iterable[_Any]) -> None: - file = self._file # type: _IO[_Any] - file.writelines(iterable) - self._check(file) - - #def xreadlines(self, *args) -> _Any: - # return self._file.xreadlines(*args) - - -class TemporaryDirectory(object): - """Create and return a temporary directory. This has the same - behavior as mkdtemp but can be used as a context manager. For - example: - - with TemporaryDirectory() as tmpdir: - ... - - Upon exiting the context, the directory and everything contained - in it are removed. - """ - - def __init__(self, suffix: str = "", prefix: str = template, - dir: str = None) -> None: - self._closed = False - self.name = None # type: str # Handle mkdtemp throwing an exception - self.name = mkdtemp(suffix, prefix, dir) - - # XXX (ncoghlan): The following code attempts to make - # this class tolerant of the module nulling out process - # that happens during CPython interpreter shutdown - # Alas, it doesn't actually manage it. See issue #10188 - self._listdir = _os.listdir - self._path_join = _os.path.join - self._isdir = _os.path.isdir - self._islink = _os.path.islink - self._remove = _os.remove - self._rmdir = _os.rmdir - self._os_error = _os.error - self._warn = _warnings.warn - - def __repr__(self) -> str: - return "<{} {!r}>".format(self.__class__.__name__, self.name) - - def __enter__(self) -> str: - return self.name - - def cleanup(self, _warn: bool = False) -> None: - if self.name and not self._closed: - try: - self._rmtree(self.name) - except (TypeError, AttributeError) as ex: - # Issue #10188: Emit a warning on stderr - # if the directory could not be cleaned - # up due to missing globals - if "None" not in str(ex): - raise - print("ERROR: {!r} while cleaning up {!r}".format(ex, self,), - file=_sys.stderr) - return - self._closed = True - if _warn: - self._warn("Implicitly cleaning up {!r}".format(self), - ResourceWarning) - - def __exit__(self, exc: type, value: BaseException, - tb: _TracebackType) -> Literal[False]: - self.cleanup() - return False - - def __del__(self) -> None: - # Issue a ResourceWarning if implicit cleanup needed - self.cleanup(_warn=True) - - def _rmtree(self, path: str) -> None: - # Essentially a stripped down version of shutil.rmtree. We can't - # use globals because they may be None'ed out at shutdown. - for name in self._listdir(path): - fullname = self._path_join(path, name) - try: - isdir = self._isdir(fullname) and not self._islink(fullname) - except self._os_error: - isdir = False - if isdir: - self._rmtree(fullname) - else: - try: - self._remove(fullname) - except self._os_error: - pass - try: - self._rmdir(path) - except self._os_error: - pass diff --git a/test-data/stdlib-samples/3.2/test/__init__.py b/test-data/stdlib-samples/3.2/test/__init__.py deleted file mode 100644 index e69de29bb2d1..000000000000 diff --git a/test-data/stdlib-samples/3.2/test/mypy.ini b/test-data/stdlib-samples/3.2/test/mypy.ini deleted file mode 100644 index 90a0e394b258..000000000000 --- a/test-data/stdlib-samples/3.2/test/mypy.ini +++ /dev/null @@ -1,2 +0,0 @@ -[mypy] -mypy_path = .. diff --git a/test-data/stdlib-samples/3.2/test/randv2_32.pck b/test-data/stdlib-samples/3.2/test/randv2_32.pck deleted file mode 100644 index 587ab241091e..000000000000 --- a/test-data/stdlib-samples/3.2/test/randv2_32.pck +++ /dev/null @@ -1,633 +0,0 @@ -crandom -Random -p0 -(tRp1 -(I2 -(I-2147483648 -I-845974985 -I-1294090086 -I1193659239 -I-1849481736 -I-946579732 -I-34406770 -I1749049471 -I1997774682 -I1432026457 -I1288127073 -I-943175655 -I-1718073964 -I339993548 -I-1045260575 -I582505037 -I-1555108250 -I-1114765620 -I1578648750 -I-350384412 -I-20845848 -I-288255314 -I738790953 -I1901249641 -I1999324672 -I-277361068 -I-1515885839 -I2061761596 -I-809068089 -I1287981136 -I258129492 -I-6303745 -I-765148337 -I1090344911 -I1653434703 -I-1242923628 -I1639171313 -I-1870042660 -I-1655014050 -I345609048 -I2093410138 -I1963263374 -I-2122098342 -I1336859961 -I-810942729 -I945857753 -I2103049942 -I623922684 -I1418349549 -I690877342 -I754973107 -I-1605111847 -I1607137813 -I-1704917131 -I1317536428 -I1714882872 -I-1665385120 -I1823694397 -I-1790836866 -I-1696724812 -I-603979847 -I-498599394 -I-341265291 -I927388804 -I1778562135 -I1716895781 -I1023198122 -I1726145967 -I941955525 -I1240148950 -I-1929634545 -I-1288147083 -I-519318335 -I754559777 -I-707571958 -I374604022 -I420424061 -I-1095443486 -I1621934944 -I-1220502522 -I-140049608 -I-918917122 -I304341024 -I-1637446057 -I-353934485 -I1973436235 -I433380241 -I-686759465 -I-2111563154 -I-573422032 -I804304541 -I1513063483 -I1417381689 -I-804778729 -I211756408 -I544537322 -I890881641 -I150378374 -I1765739392 -I1011604116 -I584889095 -I1400520554 -I413747808 -I-1741992587 -I-1882421574 -I-1373001903 -I-1885348538 -I903819480 -I1083220038 -I-1318105424 -I1740421404 -I1693089625 -I775965557 -I1319608037 -I-2127475785 -I-367562895 -I-1416273451 -I1693000327 -I-1217438421 -I834405522 -I-128287275 -I864057548 -I-973917356 -I7304111 -I1712253182 -I1353897741 -I672982288 -I1778575559 -I-403058377 -I-38540378 -I-1393713496 -I13193171 -I1127196200 -I205176472 -I-2104790506 -I299985416 -I1403541685 -I-1018270667 -I-1980677490 -I-1182625797 -I1637015181 -I-1795357414 -I1514413405 -I-924516237 -I-1841873650 -I-1014591269 -I1576616065 -I-1319103135 -I-120847840 -I2062259778 -I-9285070 -I1160890300 -I-575137313 -I-1509108275 -I46701926 -I-287560914 -I-256824960 -I577558250 -I900598310 -I944607867 -I2121154920 -I-1170505192 -I-1347170575 -I77247778 -I-1899015765 -I1234103327 -I1027053658 -I1934632322 -I-792031234 -I1147322536 -I1290655117 -I1002059715 -I1325898538 -I896029793 -I-790940694 -I-980470721 -I-1922648255 -I-951672814 -I291543943 -I1158740218 -I-1959023736 -I-1977185236 -I1527900076 -I514104195 -I-814154113 -I-593157883 -I-1023704660 -I1285688377 -I-2117525386 -I768954360 -I-38676846 -I-799848659 -I-1305517259 -I-1938213641 -I-462146758 -I-1663302892 -I1899591069 -I-22935388 -I-275856976 -I-443736893 -I-739441156 -I93862068 -I-838105669 -I1735629845 -I-817484206 -I280814555 -I1753547179 -I1811123479 -I1974543632 -I-48447465 -I-642694345 -I-531149613 -I518698953 -I-221642627 -I-686519187 -I776644303 -I257774400 -I-1499134857 -I-1055273455 -I-237023943 -I1981752330 -I-917671662 -I-372905983 -I1588058420 -I1171936660 -I-1730977121 -I1360028989 -I1769469287 -I1910709542 -I-852692959 -I1396944667 -I-1723999155 -I-310975435 -I-1965453954 -I-1636858570 -I2005650794 -I680293715 -I1355629386 -I844514684 -I-1909152807 -I-808646074 -I1936510018 -I1134413810 -I-143411047 -I-1478436304 -I1394969244 -I-1170110660 -I1963112086 -I-1518351049 -I-1506287443 -I-455023090 -I-855366028 -I-1746785568 -I933990882 -I-703625141 -I-285036872 -I188277905 -I1471578620 -I-981382835 -I-586974220 -I945619758 -I1608778444 -I-1708548066 -I-1897629320 -I-42617810 -I-836840790 -I539154487 -I-235706962 -I332074418 -I-575700589 -I1534608003 -I632116560 -I-1819760653 -I642052958 -I-722391771 -I-1104719475 -I-1196847084 -I582413973 -I1563394876 -I642007944 -I108989456 -I361625014 -I677308625 -I-1806529496 -I-959050708 -I-1858251070 -I-216069832 -I701624579 -I501238033 -I12287030 -I1895107107 -I2089098638 -I-874806230 -I1236279203 -I563718890 -I-544352489 -I-1879707498 -I1767583393 -I-1776604656 -I-693294301 -I-88882831 -I169303357 -I1299196152 -I-1122791089 -I-379157172 -I1934671851 -I1575736961 -I-19573174 -I-1401511009 -I9305167 -I-1115174467 -I1670735537 -I1226436501 -I-2004524535 -I1767463878 -I-1722855079 -I-559413926 -I1529810851 -I1201272087 -I-1297130971 -I-1188149982 -I1396557188 -I-370358342 -I-1006619702 -I1600942463 -I906087130 -I-76991909 -I2069580179 -I-1674195181 -I-2098404729 -I-940972459 -I-573399187 -I-1930386277 -I-721311199 -I-647834744 -I1452181671 -I688681916 -I1812793731 -I1704380620 -I-1389615179 -I866287837 -I-1435265007 -I388400782 -I-147986600 -I-1613598851 -I-1040347408 -I782063323 -I-239282031 -I-575966722 -I-1865208174 -I-481365146 -I579572803 -I-1239481494 -I335361280 -I-429722947 -I1881772789 -I1908103808 -I1653690013 -I-1668588344 -I1933787953 -I-2033480609 -I22162797 -I-1516527040 -I-461232482 -I-16201372 -I-2043092030 -I114990337 -I-1524090084 -I1456374020 -I458606440 -I-1928083218 -I227773125 -I-1129028159 -I1678689 -I1575896907 -I-1792935220 -I-151387575 -I64084088 -I-95737215 -I1337335688 -I-1963466345 -I1243315130 -I-1798518411 -I-546013212 -I-607065396 -I1219824160 -I1715218469 -I-1368163783 -I1701552913 -I-381114888 -I1068821717 -I266062971 -I-2066513172 -I1767407229 -I-780936414 -I-705413443 -I-1256268847 -I1646874149 -I1107690353 -I839133072 -I67001749 -I860763503 -I884880613 -I91977084 -I755371933 -I420745153 -I-578480690 -I-1520193551 -I1011369331 -I-99754575 -I-733141064 -I-500598588 -I1081124271 -I-1341266575 -I921002612 -I-848852487 -I-1904467341 -I-1294256973 -I-94074714 -I-1778758498 -I-1401188547 -I2101830578 -I2058864877 -I-272875991 -I-1375854779 -I-1332937870 -I619425525 -I-1034529639 -I-36454393 -I-2030499985 -I-1637127500 -I-1408110287 -I-2108625749 -I-961007436 -I1475654951 -I-791946251 -I1667792115 -I1818978830 -I1897980514 -I1959546477 -I-74478911 -I-508643347 -I461594399 -I538802715 -I-2094970071 -I-2076660253 -I1091358944 -I1944029246 -I-343957436 -I-1915845022 -I1237620188 -I1144125174 -I1522190520 -I-670252952 -I-19469226 -I675626510 -I758750096 -I909724354 -I-1846259652 -I544669343 -I445182495 -I-821519930 -I-1124279685 -I-1668995122 -I1653284793 -I-678555151 -I-687513207 -I1558259445 -I-1978866839 -I1558835601 -I1732138472 -I-1904793363 -I620020296 -I1562597874 -I1942617227 -I-549632552 -I721603795 -I417978456 -I-1355281522 -I-538065208 -I-1079523196 -I187375699 -I449064972 -I1018083947 -I1632388882 -I-493269866 -I92769041 -I1477146750 -I1782708404 -I444873376 -I1085851104 -I-6823272 -I-1302251853 -I1602050688 -I-1042187824 -I287161745 -I-1972094479 -I103271491 -I2131619773 -I-2064115870 -I766815498 -I990861458 -I-1664407378 -I1083746756 -I-1018331904 -I-677315687 -I-951670647 -I-952356874 -I451460609 -I-818615564 -I851439508 -I656362634 -I-1351240485 -I823378078 -I1985597385 -I597757740 -I-1512303057 -I1590872798 -I1108424213 -I818850898 -I-1368594306 -I-201107761 -I1793370378 -I1247597611 -I-1594326264 -I-601653890 -I427642759 -I248322113 -I-292545338 -I1708985870 -I1917042771 -I429354503 -I-478470329 -I793960014 -I369939133 -I1728189157 -I-518963626 -I-278523974 -I-1877289696 -I-2088617658 -I-1367940049 -I-62295925 -I197975119 -I-252900777 -I803430539 -I485759441 -I-528283480 -I-1287443963 -I-478617444 -I-861906946 -I-649095555 -I-893184337 -I2050571322 -I803433133 -I1629574571 -I1649720417 -I-2050225209 -I1208598977 -I720314344 -I-615166251 -I-835077127 -I-1405372429 -I995698064 -I148123240 -I-943016676 -I-594609622 -I-1381596711 -I1017195301 -I-1268893013 -I-1815985179 -I-1393570351 -I-870027364 -I-476064472 -I185582645 -I569863326 -I1098584267 -I-1599147006 -I-485054391 -I-852098365 -I1477320135 -I222316762 -I-1515583064 -I-935051367 -I393383063 -I819617226 -I722921837 -I-1241806499 -I-1358566385 -I1666813591 -I1333875114 -I-1663688317 -I-47254623 -I-885800726 -I307388991 -I-1219459496 -I1374870300 -I2132047877 -I-1385624198 -I-245139206 -I1015139214 -I-926198559 -I1969798868 -I-1950480619 -I-559193432 -I-1256446518 -I-1983476981 -I790179655 -I1004289659 -I1541827617 -I1555805575 -I501127333 -I-1123446797 -I-453230915 -I2035104883 -I1296122398 -I-1843698604 -I-715464588 -I337143971 -I-1972119192 -I606777909 -I726977302 -I-1149501872 -I-1963733522 -I-1797504644 -I624 -tp2 -Ntp3 -b. \ No newline at end of file diff --git a/test-data/stdlib-samples/3.2/test/randv2_64.pck b/test-data/stdlib-samples/3.2/test/randv2_64.pck deleted file mode 100644 index 090dd6fd1968..000000000000 --- a/test-data/stdlib-samples/3.2/test/randv2_64.pck +++ /dev/null @@ -1,633 +0,0 @@ -crandom -Random -p0 -(tRp1 -(I2 -(I2147483648 -I1812115682 -I2741755497 -I1028055730 -I809166036 -I2773628650 -I62321950 -I535290043 -I349877800 -I976167039 -I2490696940 -I3631326955 -I2107991114 -I2941205793 -I3199611605 -I1871971556 -I1456108540 -I2984591044 -I140836801 -I4203227310 -I3652722980 -I4031971234 -I555769760 -I697301296 -I2347638880 -I3302335858 -I320255162 -I2553586608 -I1570224361 -I2838780912 -I2315834918 -I2351348158 -I3545433015 -I2292018579 -I1177569331 -I758497559 -I2913311175 -I1014948880 -I1793619243 -I3982451053 -I3850988342 -I2393984324 -I1583100093 -I3144742543 -I3655047493 -I3507532385 -I3094515442 -I350042434 -I2455294844 -I1038739312 -I313809152 -I189433072 -I1653165452 -I4186650593 -I19281455 -I2589680619 -I4145931590 -I4283266118 -I636283172 -I943618337 -I3170184633 -I2308766231 -I634615159 -I538152647 -I2079576891 -I1029442616 -I3410689412 -I1370292761 -I1071718978 -I2139496322 -I1876699543 -I3485866187 -I3157490130 -I1633105386 -I1453253160 -I3841322080 -I3789608924 -I4110770792 -I95083673 -I931354627 -I2065389591 -I3448339827 -I3348204577 -I3263528560 -I2411324590 -I4003055026 -I1869670093 -I2737231843 -I4150701155 -I2689667621 -I2993263224 -I3239890140 -I1191430483 -I1214399779 -I3623428533 -I1817058866 -I3052274451 -I326030082 -I1505129312 -I2306812262 -I1349150363 -I1099127895 -I2543465574 -I2396380193 -I503926466 -I1607109730 -I3451716817 -I58037114 -I4290081119 -I947517597 -I3083440186 -I520522630 -I2948962496 -I4184319574 -I2957636335 -I668374201 -I2325446473 -I472785314 -I3791932366 -I573017189 -I2185725379 -I1262251492 -I3525089379 -I2951262653 -I1305347305 -I940958122 -I3343754566 -I359371744 -I3874044973 -I396897232 -I147188248 -I716683703 -I4013880315 -I1133359586 -I1794612249 -I3480815192 -I3988787804 -I1729355809 -I573408542 -I1419310934 -I1770030447 -I3552845567 -I1693976502 -I1271189893 -I2298236738 -I2049219027 -I3464198070 -I1233574082 -I1007451781 -I1838253750 -I687096593 -I1131375603 -I1223013895 -I1490478435 -I339265439 -I4232792659 -I491538536 -I2816256769 -I1044097522 -I2566227049 -I748762793 -I1511830494 -I3593259822 -I4121279213 -I3735541309 -I3609794797 -I1939942331 -I377570434 -I1437957554 -I1831285696 -I55062811 -I2046783110 -I1303902283 -I1838349877 -I420993556 -I1256392560 -I2795216506 -I2783687924 -I3322303169 -I512794749 -I308405826 -I517164429 -I3320436022 -I1328403632 -I2269184746 -I3729522810 -I3304314450 -I2238756124 -I1690581361 -I3813277532 -I4119706879 -I2659447875 -I388818978 -I2064580814 -I1586227676 -I2627522685 -I2017792269 -I547928109 -I859107450 -I1062238929 -I858886237 -I3795783146 -I4173914756 -I3835915965 -I3329504821 -I3494579904 -I838863205 -I3399734724 -I4247387481 -I3618414834 -I2984433798 -I2165205561 -I4260685684 -I3045904244 -I3450093836 -I3597307595 -I3215851166 -I3162801328 -I2558283799 -I950068105 -I1829664117 -I3108542987 -I2378860527 -I790023460 -I280087750 -I1171478018 -I2333653728 -I3976932140 -I896746152 -I1802494195 -I1232873794 -I2749440836 -I2032037296 -I2012091682 -I1296131034 -I3892133385 -I908161334 -I2296791795 -I548169794 -I696265 -I893156828 -I426904709 -I3565374535 -I2655906825 -I2792178515 -I2406814632 -I4038847579 -I3123934642 -I2197503004 -I3535032597 -I2266216689 -I2117613462 -I1787448518 -I1875089416 -I2037165384 -I1140676321 -I3606296464 -I3229138231 -I2458267132 -I1874651171 -I3331900867 -I1000557654 -I1432861701 -I473636323 -I2691783927 -I1871437447 -I1328016401 -I4118690062 -I449467602 -I681789035 -I864889442 -I1200888928 -I75769445 -I4008690037 -I2464577667 -I4167795823 -I3070097648 -I2579174882 -I1216886568 -I3810116343 -I2249507485 -I3266903480 -I3671233480 -I100191658 -I3087121334 -I365063087 -I3821275176 -I2165052848 -I1282465245 -I3601570637 -I3132413236 -I2780570459 -I3222142917 -I3129794692 -I2611590811 -I947031677 -I2991908938 -I750997949 -I3632575131 -I1632014461 -I2846484755 -I2347261779 -I2903959448 -I1397316686 -I1904578392 -I774649578 -I3164598558 -I2429587609 -I738244516 -I1563304975 -I1399317414 -I1021316297 -I3187933234 -I2126780757 -I4011907847 -I4095169219 -I3358010054 -I2729978247 -I3736811646 -I3009656410 -I2893043637 -I4027447385 -I1239610110 -I1488806900 -I2674866844 -I442876374 -I2853687260 -I2785921005 -I3151378528 -I1180567 -I2803146964 -I982221759 -I2192919417 -I3087026181 -I2480838002 -I738452921 -I687986185 -I3049371676 -I3636492954 -I3468311299 -I2379621102 -I788988633 -I1643210601 -I2983998168 -I2492730801 -I2586048705 -I604073029 -I4121082815 -I1496476928 -I2972357110 -I2663116968 -I2642628592 -I2116052039 -I487186279 -I2577680328 -I3974766614 -I730776636 -I3842528855 -I1929093695 -I44626622 -I3989908833 -I1695426222 -I3675479382 -I3051784964 -I1514876613 -I1254036595 -I2420450649 -I3034377361 -I2332990590 -I1535175126 -I185834384 -I1107372900 -I1707278185 -I1286285295 -I3332574225 -I2785672437 -I883170645 -I2005666473 -I3403131327 -I4122021352 -I1464032858 -I3702576112 -I260554598 -I1837731650 -I2594435345 -I75771049 -I2012484289 -I3058649775 -I29979703 -I3861335335 -I2506495152 -I3786448704 -I442947790 -I2582724774 -I4291336243 -I2568189843 -I1923072690 -I1121589611 -I837696302 -I3284631720 -I3865021324 -I3576453165 -I2559531629 -I1459231762 -I3506550036 -I3754420159 -I2622000757 -I124228596 -I1084328605 -I1692830753 -I547273558 -I674282621 -I655259103 -I3188629610 -I490502174 -I2081001293 -I3191330704 -I4109943593 -I1859948504 -I3163806460 -I508833168 -I1256371033 -I2709253790 -I2068956572 -I3092842814 -I3913926529 -I2039638759 -I981982529 -I536094190 -I368855295 -I51993975 -I1597480732 -I4058175522 -I2155896702 -I3196251991 -I1081913893 -I3952353788 -I3545548108 -I2370669647 -I2206572308 -I2576392991 -I1732303374 -I1153136290 -I537641955 -I1738691747 -I3232854186 -I2539632206 -I2829760278 -I3058187853 -I1202425792 -I3762361970 -I2863949342 -I2640635867 -I376638744 -I1857679757 -I330798087 -I1457400505 -I1135610046 -I606400715 -I1859536026 -I509811335 -I529772308 -I2579273244 -I1890382004 -I3959908876 -I2612335971 -I2834052227 -I1434475986 -I3684202717 -I4015011345 -I582567852 -I3689969571 -I3934753460 -I3034960691 -I208573292 -I4004113742 -I3992904842 -I2587153719 -I3529179079 -I1565424987 -I779130678 -I1048582935 -I3213591622 -I3607793434 -I3951254937 -I2047811901 -I7508850 -I248544605 -I4210090324 -I2331490884 -I70057213 -I776474945 -I1345528889 -I3290403612 -I1664955269 -I1533143116 -I545003424 -I4141564478 -I1257326139 -I868843601 -I2337603029 -I1918131449 -I1843439523 -I1125519035 -I673340118 -I421408852 -I1520454906 -I1804722630 -I3621254196 -I2329968000 -I39464672 -I430583134 -I294026512 -I53978525 -I2892276105 -I1418863764 -I3419054451 -I1391595797 -I3544981798 -I4191780858 -I825672357 -I2972000844 -I1571305069 -I4231982845 -I3611916419 -I3045163168 -I2982349733 -I278572141 -I4215338078 -I839860504 -I1819151779 -I1412347479 -I1386770353 -I3914589491 -I3783104977 -I4124296733 -I830546258 -I89825624 -I4110601328 -I2545483429 -I300600527 -I516641158 -I3693021034 -I2852912854 -I3240039868 -I4167407959 -I1479557946 -I3621188804 -I1391590944 -I3578441128 -I1227055556 -I406898396 -I3064054983 -I25835338 -I402664165 -I4097682779 -I2106728012 -I203613622 -I3045467686 -I1381726438 -I3798670110 -I1342314961 -I3552497361 -I535913619 -I2625787583 -I1606574307 -I1101269630 -I1950513752 -I1121355862 -I3586816903 -I438529984 -I2473182121 -I1229997203 -I405445940 -I1695535315 -I427014336 -I3916768430 -I392298359 -I1884642868 -I1244730821 -I741058080 -I567479957 -I3527621168 -I3191971011 -I3267069104 -I4108668146 -I1520795587 -I166581006 -I473794477 -I1562126550 -I929843010 -I889533294 -I1266556608 -I874518650 -I3520162092 -I3013765049 -I4220231414 -I547246449 -I3998093769 -I3737193746 -I3872944207 -I793651876 -I2606384318 -I875991012 -I1394836334 -I4102011644 -I854380426 -I2618666767 -I2568302000 -I1995512132 -I229491093 -I2673500286 -I3364550739 -I3836923416 -I243656987 -I3944388983 -I4064949677 -I1416956378 -I1703244487 -I3990798829 -I2023425781 -I3926702214 -I1229015501 -I3174247824 -I624 -tp2 -Ntp3 -b. \ No newline at end of file diff --git a/test-data/stdlib-samples/3.2/test/randv3.pck b/test-data/stdlib-samples/3.2/test/randv3.pck deleted file mode 100644 index 09fc38b1a876..000000000000 --- a/test-data/stdlib-samples/3.2/test/randv3.pck +++ /dev/null @@ -1,633 +0,0 @@ -crandom -Random -p0 -(tRp1 -(I3 -(L2147483648L -L994081831L -L2806287265L -L2228999830L -L3396498069L -L2956805457L -L3273927761L -L920726507L -L1862624492L -L2921292485L -L1779526843L -L2469105503L -L251696293L -L1254390717L -L779197080L -L3165356830L -L2007365218L -L1870028812L -L2896519363L -L1855578438L -L979518416L -L3481710246L -L3191861507L -L3993006593L -L2967971479L -L3353342753L -L3576782572L -L339685558L -L2367675732L -L116208555L -L1220054437L -L486597056L -L1912115141L -L1037044792L -L4096904723L -L3409146175L -L3701651227L -L315824610L -L4138604583L -L1385764892L -L191878900L -L2320582219L -L3420677494L -L2776503169L -L1148247403L -L829555069L -L902064012L -L2934642741L -L2477108577L -L2583928217L -L1658612579L -L2865447913L -L129147346L -L3691171887L -L1569328110L -L1372860143L -L1054139183L -L1617707080L -L69020592L -L3810271603L -L1853953416L -L3499803073L -L1027545027L -L3229043605L -L250848720L -L3324932626L -L3537002962L -L2494323345L -L3238103962L -L4147541579L -L3636348186L -L3025455083L -L2678771977L -L584700256L -L3461826909L -L854511420L -L943463552L -L3609239025L -L3977577989L -L253070090L -L777394544L -L2144086567L -L1092947992L -L854327284L -L2222750082L -L360183510L -L1312466483L -L3227531091L -L2235022500L -L3013060530L -L2541091298L -L3480126342L -L1839762775L -L2632608190L -L1108889403L -L3045050923L -L731513126L -L3505436788L -L3062762017L -L1667392680L -L1354126500L -L1143573930L -L2816645702L -L2100356873L -L2817679106L -L1210746010L -L2409915248L -L2910119964L -L2309001420L -L220351824L -L3667352871L -L3993148590L -L2886160232L -L4239393701L -L1189270581L -L3067985541L -L147374573L -L2355164869L -L3696013550L -L4227037846L -L1905112743L -L3312843689L -L2930678266L -L1828795355L -L76933594L -L3987100796L -L1288361435L -L3464529151L -L965498079L -L1444623093L -L1372893415L -L1536235597L -L1341994850L -L963594758L -L2115295754L -L982098685L -L1053433904L -L2078469844L -L3059765792L -L1753606181L -L2130171254L -L567588194L -L529629426L -L3621523534L -L3027576564L -L1176438083L -L4096287858L -L1168574683L -L1425058962L -L1429631655L -L2902106759L -L761900641L -L1329183956L -L1947050932L -L447490289L -L3282516276L -L200037389L -L921868197L -L3331403999L -L4088760249L -L2188326318L -L288401961L -L1360802675L -L314302808L -L3314639210L -L3749821203L -L2286081570L -L2768939062L -L3200541016L -L2133495482L -L385029880L -L4217232202L -L3171617231L -L1660846653L -L2459987621L -L2691776124L -L4225030408L -L3595396773L -L1103680661L -L539064057L -L1492841101L -L166195394L -L757973658L -L533893054L -L2784879594L -L1021821883L -L2350548162L -L176852116L -L3503166025L -L148079914L -L1633466236L -L2773090165L -L1162846701L -L3575737795L -L1624178239L -L2454894710L -L3014691938L -L526355679L -L1870824081L -L3362425857L -L3907566665L -L3462563184L -L2229112004L -L4203735748L -L1557442481L -L924133999L -L1906634214L -L880459727L -L4065895870L -L141426254L -L1258450159L -L3243115027L -L1574958840L -L313939294L -L3055664260L -L3459714255L -L531778790L -L509505506L -L1620227491L -L2675554942L -L2516509560L -L3797299887L -L237135890L -L3203142213L -L1087745310L -L1897151854L -L3936590041L -L132765167L -L2385908063L -L1360600289L -L3574567769L -L2752788114L -L2644228966L -L2377705183L -L601277909L -L4046480498L -L324401408L -L3279931760L -L2227059377L -L1538827493L -L4220532064L -L478044564L -L2917117761L -L635492832L -L2319763261L -L795944206L -L1820473234L -L1673151409L -L1404095402L -L1661067505L -L3217106938L -L2406310683L -L1931309248L -L2458622868L -L3323670524L -L3266852755L -L240083943L -L3168387397L -L607722198L -L1256837690L -L3608124913L -L4244969357L -L1289959293L -L519750328L -L3229482463L -L1105196988L -L1832684479L -L3761037224L -L2363631822L -L3297957711L -L572766355L -L1195822137L -L2239207981L -L2034241203L -L163540514L -L288160255L -L716403680L -L4019439143L -L1536281935L -L2345100458L -L2786059178L -L2822232109L -L987025395L -L3061166559L -L490422513L -L2551030115L -L2638707620L -L1344728502L -L714108911L -L2831719700L -L2188615369L -L373509061L -L1351077504L -L3136217056L -L783521095L -L2554949468L -L2662499550L -L1203826951L -L1379632388L -L1918858985L -L607465976L -L1980450237L -L3540079211L -L3397813410L -L2913309266L -L2289572621L -L4133935327L -L4166227663L -L3371801704L -L3065474909L -L3580562343L -L3832172378L -L2556130719L -L310473705L -L3734014346L -L2490413810L -L347233056L -L526668037L -L1158393656L -L544329703L -L2150085419L -L3914038146L -L1060237586L -L4159394837L -L113205121L -L309966775L -L4098784465L -L3635222960L -L2417516569L -L2089579233L -L1725807541L -L2728122526L -L2365836523L -L2504078522L -L1443946869L -L2384171411L -L997046534L -L3249131657L -L1699875986L -L3618097146L -L1716038224L -L2629818607L -L2929217876L -L1367250314L -L1726434951L -L1388496325L -L2107602181L -L2822366842L -L3052979190L -L3796798633L -L1543813381L -L959000121L -L1363845999L -L2952528150L -L874184932L -L1888387194L -L2328695295L -L3442959855L -L841805947L -L1087739275L -L3230005434L -L3045399265L -L1161817318L -L2898673139L -L860011094L -L940539782L -L1297818080L -L4243941623L -L1577613033L -L4204131887L -L3819057225L -L1969439558L -L3297963932L -L241874069L -L3517033453L -L2295345664L -L1098911422L -L886955008L -L1477397621L -L4279347332L -L3616558791L -L2384411957L -L742537731L -L764221540L -L2871698900L -L3530636393L -L691256644L -L758730966L -L1717773090L -L2751856377L -L3188484000L -L3767469670L -L1623863053L -L3533236793L -L4099284176L -L723921107L -L310594036L -L223978745L -L2266565776L -L201843303L -L2969968546L -L3351170888L -L3465113624L -L2712246712L -L1521383057L -L2384461798L -L216357551L -L2167301975L -L3144653194L -L2781220155L -L3620747666L -L95971265L -L4255400243L -L59999757L -L4174273472L -L3974511524L -L1007123950L -L3112477628L -L806461512L -L3148074008L -L528352882L -L2545979588L -L2562281969L -L3010249477L -L1886331611L -L3210656433L -L1034099976L -L2906893579L -L1197048779L -L1870004401L -L3898300490L -L2686856402L -L3975723478L -L613043532L -L2565674353L -L3760045310L -L3468984376L -L4126258L -L303855424L -L3988963552L -L276256796L -L544071807L -L1023872062L -L1747461519L -L1975571260L -L4033766958L -L2946555557L -L1492957796L -L958271685L -L46480515L -L907760635L -L1306626357L -L819652378L -L1172300279L -L1116851319L -L495601075L -L1157715330L -L534220108L -L377320028L -L1672286106L -L2066219284L -L1842386355L -L2546059464L -L1839457336L -L3476194446L -L3050550028L -L594705582L -L1905813535L -L1813033412L -L2700858157L -L169067972L -L4252889045L -L1921944555L -L497671474L -L210143935L -L2688398489L -L325158375L -L3450846447L -L891760597L -L712802536L -L1132557436L -L1417044075L -L1639889660L -L1746379970L -L1478741647L -L2817563486L -L2573612532L -L4266444457L -L2911601615L -L804745411L -L2207254652L -L1189140646L -L3829725111L -L3637367348L -L1944731747L -L2193440343L -L1430195413L -L1173515229L -L1582618217L -L2070767037L -L247908936L -L1460675439L -L556001596L -L327629335L -L1036133876L -L4228129605L -L999174048L -L3635804039L -L1416550481L -L1270540269L -L4280743815L -L39607659L -L1552540623L -L2762294062L -L504137289L -L4117044239L -L1417130225L -L1342970056L -L1755716449L -L1169447322L -L2731401356L -L2319976745L -L2869221479L -L23972655L -L2251495389L -L1429860878L -L3728135992L -L4241432973L -L3698275076L -L216416432L -L4040046960L -L246077176L -L894675685L -L3932282259L -L3097205100L -L2128818650L -L1319010656L -L1601974009L -L2552960957L -L3554016055L -L4209395641L -L2013340102L -L3370447801L -L2307272002L -L1795091354L -L202109401L -L988345070L -L2514870758L -L1132726850L -L582746224L -L3112305421L -L1843020683L -L3600189223L -L1101349165L -L4211905855L -L2866677581L -L2881621130L -L4165324109L -L4238773191L -L3635649550L -L2670481044L -L2996248219L -L1676992480L -L3473067050L -L4205793699L -L4019490897L -L1579990481L -L1899617990L -L1136347713L -L1802842268L -L3591752960L -L1197308739L -L433629786L -L4032142790L -L3148041979L -L3312138845L -L3896860449L -L3298182567L -L907605170L -L1658664067L -L2682980313L -L2523523173L -L1208722103L -L3808530363L -L1079003946L -L4282402864L -L2041010073L -L2667555071L -L688018180L -L1405121012L -L4167994076L -L3504695336L -L1923944749L -L1143598790L -L3936268898L -L3606243846L -L1017420080L -L4026211169L -L596529763L -L1844259624L -L2840216282L -L2673807759L -L3407202575L -L2737971083L -L4075423068L -L3684057432L -L3146627241L -L599650513L -L69773114L -L1257035919L -L807485291L -L2376230687L -L3036593147L -L2642411658L -L106080044L -L2199622729L -L291834511L -L2697611361L -L11689733L -L625123952L -L3226023062L -L3229663265L -L753059444L -L2843610189L -L624L -tp2 -Ntp3 -b. \ No newline at end of file diff --git a/test-data/stdlib-samples/3.2/test/subprocessdata/fd_status.py b/test-data/stdlib-samples/3.2/test/subprocessdata/fd_status.py deleted file mode 100644 index 1f61e13a3456..000000000000 --- a/test-data/stdlib-samples/3.2/test/subprocessdata/fd_status.py +++ /dev/null @@ -1,24 +0,0 @@ -"""When called as a script, print a comma-separated list of the open -file descriptors on stdout.""" - -import errno -import os - -try: - _MAXFD = os.sysconf("SC_OPEN_MAX") -except: - _MAXFD = 256 - -if __name__ == "__main__": - fds = [] - for fd in range(0, _MAXFD): - try: - st = os.fstat(fd) - except OSError as e: - if e.errno == errno.EBADF: - continue - raise - # Ignore Solaris door files - if st.st_mode & 0xF000 != 0xd000: - fds.append(fd) - print(','.join(map(str, fds))) diff --git a/test-data/stdlib-samples/3.2/test/subprocessdata/input_reader.py b/test-data/stdlib-samples/3.2/test/subprocessdata/input_reader.py deleted file mode 100644 index 1dc3191ad183..000000000000 --- a/test-data/stdlib-samples/3.2/test/subprocessdata/input_reader.py +++ /dev/null @@ -1,7 +0,0 @@ -"""When called as a script, consumes the input""" - -import sys - -if __name__ == "__main__": - for line in sys.stdin: - pass diff --git a/test-data/stdlib-samples/3.2/test/subprocessdata/qcat.py b/test-data/stdlib-samples/3.2/test/subprocessdata/qcat.py deleted file mode 100644 index fe6f9db25c97..000000000000 --- a/test-data/stdlib-samples/3.2/test/subprocessdata/qcat.py +++ /dev/null @@ -1,7 +0,0 @@ -"""When ran as a script, simulates cat with no arguments.""" - -import sys - -if __name__ == "__main__": - for line in sys.stdin: - sys.stdout.write(line) diff --git a/test-data/stdlib-samples/3.2/test/subprocessdata/qgrep.py b/test-data/stdlib-samples/3.2/test/subprocessdata/qgrep.py deleted file mode 100644 index 69906379a9b3..000000000000 --- a/test-data/stdlib-samples/3.2/test/subprocessdata/qgrep.py +++ /dev/null @@ -1,10 +0,0 @@ -"""When called with a single argument, simulated fgrep with a single -argument and no options.""" - -import sys - -if __name__ == "__main__": - pattern = sys.argv[1] - for line in sys.stdin: - if pattern in line: - sys.stdout.write(line) diff --git a/test-data/stdlib-samples/3.2/test/subprocessdata/sigchild_ignore.py b/test-data/stdlib-samples/3.2/test/subprocessdata/sigchild_ignore.py deleted file mode 100644 index 6072aece28a8..000000000000 --- a/test-data/stdlib-samples/3.2/test/subprocessdata/sigchild_ignore.py +++ /dev/null @@ -1,6 +0,0 @@ -import signal, subprocess, sys -# On Linux this causes os.waitpid to fail with OSError as the OS has already -# reaped our child process. The wait() passing the OSError on to the caller -# and causing us to exit with an error is what we are testing against. -signal.signal(signal.SIGCHLD, signal.SIG_IGN) -subprocess.Popen([sys.executable, '-c', 'print("albatross")']).wait() diff --git a/test-data/stdlib-samples/3.2/test/support.py b/test-data/stdlib-samples/3.2/test/support.py deleted file mode 100644 index 88ce10cd74a9..000000000000 --- a/test-data/stdlib-samples/3.2/test/support.py +++ /dev/null @@ -1,1602 +0,0 @@ -"""Supporting definitions for the Python regression tests.""" - -if __name__ != 'test.support': - raise ImportError('support must be imported from the test package') - -import contextlib -import errno -import functools -import gc -import socket -import sys -import os -import platform -import shutil -import warnings -import unittest -import importlib -import collections -import re -import subprocess -import imp -import time -import sysconfig -import fnmatch -import logging.handlers - -import _thread, threading -from typing import Any, Dict, cast -#try: -# import multiprocessing.process -#except ImportError: -# multiprocessing = None - - -__all__ = [ - "Error", "TestFailed", "ResourceDenied", "import_module", - "verbose", "use_resources", "max_memuse", "record_original_stdout", - "get_original_stdout", "unload", "unlink", "rmtree", "forget", - "is_resource_enabled", "requires", "requires_mac_ver", - "find_unused_port", "bind_port", - "fcmp", "is_jython", "TESTFN", "HOST", "FUZZ", "SAVEDCWD", "temp_cwd", - "findfile", "sortdict", "check_syntax_error", "open_urlresource", - "check_warnings", "CleanImport", "EnvironmentVarGuard", - "TransientResource", "captured_output", "captured_stdout", - "captured_stdin", "captured_stderr", - "time_out", "socket_peer_reset", "ioerror_peer_reset", - "run_with_locale", 'temp_umask', "transient_internet", - "set_memlimit", "bigmemtest", "bigaddrspacetest", "BasicTestRunner", - "run_unittest", "run_doctest", "threading_setup", "threading_cleanup", - "reap_children", "cpython_only", "check_impl_detail", "get_attribute", - "swap_item", "swap_attr", "requires_IEEE_754", - "TestHandler", "Matcher", "can_symlink", "skip_unless_symlink", - "import_fresh_module", "failfast", - ] - -class Error(Exception): - """Base class for regression test exceptions.""" - -class TestFailed(Error): - """Test failed.""" - -class ResourceDenied(unittest.SkipTest): - """Test skipped because it requested a disallowed resource. - - This is raised when a test calls requires() for a resource that - has not be enabled. It is used to distinguish between expected - and unexpected skips. - """ - -@contextlib.contextmanager -def _ignore_deprecated_imports(ignore=True): - """Context manager to suppress package and module deprecation - warnings when importing them. - - If ignore is False, this context manager has no effect.""" - if ignore: - with warnings.catch_warnings(): - warnings.filterwarnings("ignore", ".+ (module|package)", - DeprecationWarning) - yield None - else: - yield None - - -def import_module(name, deprecated=False): - """Import and return the module to be tested, raising SkipTest if - it is not available. - - If deprecated is True, any module or package deprecation messages - will be suppressed.""" - with _ignore_deprecated_imports(deprecated): - try: - return importlib.import_module(name) - except ImportError as msg: - raise unittest.SkipTest(str(msg)) - - -def _save_and_remove_module(name, orig_modules): - """Helper function to save and remove a module from sys.modules - - Raise ImportError if the module can't be imported.""" - # try to import the module and raise an error if it can't be imported - if name not in sys.modules: - __import__(name) - del sys.modules[name] - for modname in list(sys.modules): - if modname == name or modname.startswith(name + '.'): - orig_modules[modname] = sys.modules[modname] - del sys.modules[modname] - -def _save_and_block_module(name, orig_modules): - """Helper function to save and block a module in sys.modules - - Return True if the module was in sys.modules, False otherwise.""" - saved = True - try: - orig_modules[name] = sys.modules[name] - except KeyError: - saved = False - sys.modules[name] = None - return saved - - -def import_fresh_module(name, fresh=(), blocked=(), deprecated=False): - """Imports and returns a module, deliberately bypassing the sys.modules cache - and importing a fresh copy of the module. Once the import is complete, - the sys.modules cache is restored to its original state. - - Modules named in fresh are also imported anew if needed by the import. - If one of these modules can't be imported, None is returned. - - Importing of modules named in blocked is prevented while the fresh import - takes place. - - If deprecated is True, any module or package deprecation messages - will be suppressed.""" - # NOTE: test_heapq, test_json and test_warnings include extra sanity checks - # to make sure that this utility function is working as expected - with _ignore_deprecated_imports(deprecated): - # Keep track of modules saved for later restoration as well - # as those which just need a blocking entry removed - orig_modules = {} - names_to_remove = [] - _save_and_remove_module(name, orig_modules) - try: - for fresh_name in fresh: - _save_and_remove_module(fresh_name, orig_modules) - for blocked_name in blocked: - if not _save_and_block_module(blocked_name, orig_modules): - names_to_remove.append(blocked_name) - fresh_module = importlib.import_module(name) - except ImportError: - fresh_module = None - finally: - for orig_name, module in orig_modules.items(): - sys.modules[orig_name] = module - for name_to_remove in names_to_remove: - del sys.modules[name_to_remove] - return fresh_module - - -def get_attribute(obj, name): - """Get an attribute, raising SkipTest if AttributeError is raised.""" - try: - attribute = getattr(obj, name) - except AttributeError: - raise unittest.SkipTest("module %s has no attribute %s" % ( - obj.__name__, name)) - else: - return attribute - -verbose = 1 # Flag set to 0 by regrtest.py -use_resources = None # type: Any # Flag set to [] by regrtest.py -max_memuse = 0 # Disable bigmem tests (they will still be run with - # small sizes, to make sure they work.) -real_max_memuse = 0 -failfast = False -match_tests = None # type: Any - -# _original_stdout is meant to hold stdout at the time regrtest began. -# This may be "the real" stdout, or IDLE's emulation of stdout, or whatever. -# The point is to have some flavor of stdout the user can actually see. -_original_stdout = None # type: 'Any' -def record_original_stdout(stdout): - global _original_stdout - _original_stdout = stdout - -def get_original_stdout(): - return _original_stdout or sys.stdout - -def unload(name): - try: - del sys.modules[name] - except KeyError: - pass - -def unlink(filename): - try: - os.unlink(filename) - except OSError as error: - # The filename need not exist. - if error.errno not in (errno.ENOENT, errno.ENOTDIR): - raise - -def rmtree(path): - try: - shutil.rmtree(path) - except OSError as error: - # Unix returns ENOENT, Windows returns ESRCH. - if error.errno not in (errno.ENOENT, errno.ESRCH): - raise - -def make_legacy_pyc(source): - """Move a PEP 3147 pyc/pyo file to its legacy pyc/pyo location. - - The choice of .pyc or .pyo extension is done based on the __debug__ flag - value. - - :param source: The file system path to the source file. The source file - does not need to exist, however the PEP 3147 pyc file must exist. - :return: The file system path to the legacy pyc file. - """ - pyc_file = imp.cache_from_source(source) - up_one = os.path.dirname(os.path.abspath(source)) - if __debug__: - ch = 'c' - else: - ch = 'o' - legacy_pyc = os.path.join(up_one, source + ch) - os.rename(pyc_file, legacy_pyc) - return legacy_pyc - -def forget(modname): - """'Forget' a module was ever imported. - - This removes the module from sys.modules and deletes any PEP 3147 or - legacy .pyc and .pyo files. - """ - unload(modname) - for dirname in sys.path: - source = os.path.join(dirname, modname + '.py') - # It doesn't matter if they exist or not, unlink all possible - # combinations of PEP 3147 and legacy pyc and pyo files. - unlink(source + 'c') - unlink(source + 'o') - unlink(imp.cache_from_source(source, debug_override=True)) - unlink(imp.cache_from_source(source, debug_override=False)) - -# On some platforms, should not run gui test even if it is allowed -# in `use_resources'. -#if sys.platform.startswith('win'): - #import ctypes - #import ctypes.wintypes - #def _is_gui_available(): - # UOI_FLAGS = 1 - # WSF_VISIBLE = 0x0001 - # class USEROBJECTFLAGS(ctypes.Structure): - # _fields_ = [("fInherit", ctypes.wintypes.BOOL), - # ("fReserved", ctypes.wintypes.BOOL), - # ("dwFlags", ctypes.wintypes.DWORD)] - # dll = ctypes.windll.user32 - # h = dll.GetProcessWindowStation() - # if not h: - # raise ctypes.WinError() - # uof = USEROBJECTFLAGS() - # needed = ctypes.wintypes.DWORD() - # res = dll.GetUserObjectInformationW(h, - # UOI_FLAGS, - # ctypes.byref(uof), - # ctypes.sizeof(uof), - # ctypes.byref(needed)) - # if not res: - # raise ctypes.WinError() - # return bool(uof.dwFlags & WSF_VISIBLE) -#else: -def _is_gui_available(): - return True - -def is_resource_enabled(resource): - """Test whether a resource is enabled. Known resources are set by - regrtest.py.""" - return use_resources is not None and resource in use_resources - -def requires(resource, msg=None): - """Raise ResourceDenied if the specified resource is not available. - - If the caller's module is __main__ then automatically return True. The - possibility of False being returned occurs when regrtest.py is - executing. - """ - if resource == 'gui' and not _is_gui_available(): - raise unittest.SkipTest("Cannot use the 'gui' resource") - # see if the caller's module is __main__ - if so, treat as if - # the resource was set - if sys._getframe(1).f_globals.get("__name__") == "__main__": - return - if not is_resource_enabled(resource): - if msg is None: - msg = "Use of the `%s' resource not enabled" % resource - raise ResourceDenied(msg) - -def requires_mac_ver(*min_version): - """Decorator raising SkipTest if the OS is Mac OS X and the OS X - version if less than min_version. - - For example, @requires_mac_ver(10, 5) raises SkipTest if the OS X version - is lesser than 10.5. - """ - def decorator(func): - @functools.wraps(func) - def wrapper(*args, **kw): - if sys.platform == 'darwin': - version_txt = platform.mac_ver()[0] - try: - version = tuple(map(int, version_txt.split('.'))) - except ValueError: - pass - else: - if version < min_version: - min_version_txt = '.'.join(map(str, min_version)) - raise unittest.SkipTest( - "Mac OS X %s or higher required, not %s" - % (min_version_txt, version_txt)) - return func(*args, **kw) - wrapper.min_version = min_version - return wrapper - return decorator - -HOST = 'localhost' - -def find_unused_port(family=socket.AF_INET, socktype=socket.SOCK_STREAM): - """Returns an unused port that should be suitable for binding. This is - achieved by creating a temporary socket with the same family and type as - the 'sock' parameter (default is AF_INET, SOCK_STREAM), and binding it to - the specified host address (defaults to 0.0.0.0) with the port set to 0, - eliciting an unused ephemeral port from the OS. The temporary socket is - then closed and deleted, and the ephemeral port is returned. - - Either this method or bind_port() should be used for any tests where a - server socket needs to be bound to a particular port for the duration of - the test. Which one to use depends on whether the calling code is creating - a python socket, or if an unused port needs to be provided in a constructor - or passed to an external program (i.e. the -accept argument to openssl's - s_server mode). Always prefer bind_port() over find_unused_port() where - possible. Hard coded ports should *NEVER* be used. As soon as a server - socket is bound to a hard coded port, the ability to run multiple instances - of the test simultaneously on the same host is compromised, which makes the - test a ticking time bomb in a buildbot environment. On Unix buildbots, this - may simply manifest as a failed test, which can be recovered from without - intervention in most cases, but on Windows, the entire python process can - completely and utterly wedge, requiring someone to log in to the buildbot - and manually kill the affected process. - - (This is easy to reproduce on Windows, unfortunately, and can be traced to - the SO_REUSEADDR socket option having different semantics on Windows versus - Unix/Linux. On Unix, you can't have two AF_INET SOCK_STREAM sockets bind, - listen and then accept connections on identical host/ports. An EADDRINUSE - socket.error will be raised at some point (depending on the platform and - the order bind and listen were called on each socket). - - However, on Windows, if SO_REUSEADDR is set on the sockets, no EADDRINUSE - will ever be raised when attempting to bind two identical host/ports. When - accept() is called on each socket, the second caller's process will steal - the port from the first caller, leaving them both in an awkwardly wedged - state where they'll no longer respond to any signals or graceful kills, and - must be forcibly killed via OpenProcess()/TerminateProcess(). - - The solution on Windows is to use the SO_EXCLUSIVEADDRUSE socket option - instead of SO_REUSEADDR, which effectively affords the same semantics as - SO_REUSEADDR on Unix. Given the propensity of Unix developers in the Open - Source world compared to Windows ones, this is a common mistake. A quick - look over OpenSSL's 0.9.8g source shows that they use SO_REUSEADDR when - openssl.exe is called with the 's_server' option, for example. See - http://bugs.python.org/issue2550 for more info. The following site also - has a very thorough description about the implications of both REUSEADDR - and EXCLUSIVEADDRUSE on Windows: - http://msdn2.microsoft.com/en-us/library/ms740621(VS.85).aspx) - - XXX: although this approach is a vast improvement on previous attempts to - elicit unused ports, it rests heavily on the assumption that the ephemeral - port returned to us by the OS won't immediately be dished back out to some - other process when we close and delete our temporary socket but before our - calling code has a chance to bind the returned port. We can deal with this - issue if/when we come across it. - """ - - tempsock = socket.socket(family, socktype) - port = bind_port(tempsock) - tempsock.close() - #del tempsock - return port - -def bind_port(sock, host=HOST): - """Bind the socket to a free port and return the port number. Relies on - ephemeral ports in order to ensure we are using an unbound port. This is - important as many tests may be running simultaneously, especially in a - buildbot environment. This method raises an exception if the sock.family - is AF_INET and sock.type is SOCK_STREAM, *and* the socket has SO_REUSEADDR - or SO_REUSEPORT set on it. Tests should *never* set these socket options - for TCP/IP sockets. The only case for setting these options is testing - multicasting via multiple UDP sockets. - - Additionally, if the SO_EXCLUSIVEADDRUSE socket option is available (i.e. - on Windows), it will be set on the socket. This will prevent anyone else - from bind()'ing to our host/port for the duration of the test. - """ - - if sock.family == socket.AF_INET and sock.type == socket.SOCK_STREAM: - if hasattr(socket, 'SO_REUSEADDR'): - if sock.getsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR) == 1: - raise TestFailed("tests should never set the SO_REUSEADDR " \ - "socket option on TCP/IP sockets!") - if hasattr(socket, 'SO_REUSEPORT'): - if sock.getsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT) == 1: - raise TestFailed("tests should never set the SO_REUSEPORT " \ - "socket option on TCP/IP sockets!") - if hasattr(socket, 'SO_EXCLUSIVEADDRUSE'): - sock.setsockopt(socket.SOL_SOCKET, socket.SO_EXCLUSIVEADDRUSE, 1) - - sock.bind((host, 0)) - port = sock.getsockname()[1] - return port - -FUZZ = 1e-6 - -def fcmp(x, y): # fuzzy comparison function - if isinstance(x, float) or isinstance(y, float): - try: - fuzz = (abs(x) + abs(y)) * FUZZ - if abs(x-y) <= fuzz: - return 0 - except: - pass - elif type(x) == type(y) and isinstance(x, (tuple, list)): - for i in range(min(len(x), len(y))): - outcome = fcmp(x[i], y[i]) - if outcome != 0: - return outcome - return (len(x) > len(y)) - (len(x) < len(y)) - return (x > y) - (x < y) - -# decorator for skipping tests on non-IEEE 754 platforms -requires_IEEE_754 = unittest.skipUnless( - cast(Any, float).__getformat__("double").startswith("IEEE"), - "test requires IEEE 754 doubles") - -is_jython = sys.platform.startswith('java') - -TESTFN = '' -# Filename used for testing -if os.name == 'java': - # Jython disallows @ in module names - TESTFN = '$test' -else: - TESTFN = '@test' - -# Disambiguate TESTFN for parallel testing, while letting it remain a valid -# module name. -TESTFN = "{}_{}_tmp".format(TESTFN, os.getpid()) - - -# TESTFN_UNICODE is a non-ascii filename -TESTFN_UNICODE = TESTFN + "-\xe0\xf2\u0258\u0141\u011f" -if sys.platform == 'darwin': - # In Mac OS X's VFS API file names are, by definition, canonically - # decomposed Unicode, encoded using UTF-8. See QA1173: - # http://developer.apple.com/mac/library/qa/qa2001/qa1173.html - import unicodedata - TESTFN_UNICODE = unicodedata.normalize('NFD', TESTFN_UNICODE) -TESTFN_ENCODING = sys.getfilesystemencoding() - -# TESTFN_UNENCODABLE is a filename (str type) that should *not* be able to be -# encoded by the filesystem encoding (in strict mode). It can be None if we -# cannot generate such filename. -TESTFN_UNENCODABLE = None # type: Any -if sys.platform == "win32": - # skip win32s (0) or Windows 9x/ME (1) - if sys.getwindowsversion().platform >= 2: - # Different kinds of characters from various languages to minimize the - # probability that the whole name is encodable to MBCS (issue #9819) - TESTFN_UNENCODABLE = TESTFN + "-\u5171\u0141\u2661\u0363\uDC80" - try: - TESTFN_UNENCODABLE.encode(TESTFN_ENCODING) - except UnicodeEncodeError: - pass - else: - print('WARNING: The filename %r CAN be encoded by the filesystem encoding (%s). ' - 'Unicode filename tests may not be effective' - % (TESTFN_UNENCODABLE, TESTFN_ENCODING)) - TESTFN_UNENCODABLE = None -# Mac OS X denies unencodable filenames (invalid utf-8) -elif sys.platform != 'darwin': - try: - # ascii and utf-8 cannot encode the byte 0xff - b'\xff'.decode(TESTFN_ENCODING) - except UnicodeDecodeError: - # 0xff will be encoded using the surrogate character u+DCFF - TESTFN_UNENCODABLE = TESTFN \ - + b'-\xff'.decode(TESTFN_ENCODING, 'surrogateescape') - else: - # File system encoding (eg. ISO-8859-* encodings) can encode - # the byte 0xff. Skip some unicode filename tests. - pass - -# Save the initial cwd -SAVEDCWD = os.getcwd() - -@contextlib.contextmanager -def temp_cwd(name='tempcwd', quiet=False, path=None): - """ - Context manager that temporarily changes the CWD. - - An existing path may be provided as *path*, in which case this - function makes no changes to the file system. - - Otherwise, the new CWD is created in the current directory and it's - named *name*. If *quiet* is False (default) and it's not possible to - create or change the CWD, an error is raised. If it's True, only a - warning is raised and the original CWD is used. - """ - saved_dir = os.getcwd() - is_temporary = False - if path is None: - path = name - try: - os.mkdir(name) - is_temporary = True - except OSError: - if not quiet: - raise - warnings.warn('tests may fail, unable to create temp CWD ' + name, - RuntimeWarning, stacklevel=3) - try: - os.chdir(path) - except OSError: - if not quiet: - raise - warnings.warn('tests may fail, unable to change the CWD to ' + name, - RuntimeWarning, stacklevel=3) - try: - yield os.getcwd() - finally: - os.chdir(saved_dir) - if is_temporary: - rmtree(name) - - -@contextlib.contextmanager -def temp_umask(umask): - """Context manager that temporarily sets the process umask.""" - oldmask = os.umask(umask) - try: - yield None - finally: - os.umask(oldmask) - - -def findfile(file, here=__file__, subdir=None): - """Try to find a file on sys.path and the working directory. If it is not - found the argument passed to the function is returned (this does not - necessarily signal failure; could still be the legitimate path).""" - if os.path.isabs(file): - return file - if subdir is not None: - file = os.path.join(subdir, file) - path = sys.path - path = [os.path.dirname(here)] + path - for dn in path: - fn = os.path.join(dn, file) - if os.path.exists(fn): return fn - return file - -def sortdict(dict): - "Like repr(dict), but in sorted order." - items = sorted(dict.items()) - reprpairs = ["%r: %r" % pair for pair in items] - withcommas = ", ".join(reprpairs) - return "{%s}" % withcommas - -def make_bad_fd(): - """ - Create an invalid file descriptor by opening and closing a file and return - its fd. - """ - file = open(TESTFN, "wb") - try: - return file.fileno() - finally: - file.close() - unlink(TESTFN) - -def check_syntax_error(testcase, statement): - raise NotImplementedError('no compile built-in') - #testcase.assertRaises(SyntaxError, compile, statement, - # '', 'exec') - -def open_urlresource(url, *args, **kw): - from urllib import request, parse - - check = kw.pop('check', None) - - filename = parse.urlparse(url)[2].split('/')[-1] # '/': it's URL! - - fn = os.path.join(os.path.dirname(__file__), "data", filename) - - def check_valid_file(fn): - f = open(fn, *args, **kw) - if check is None: - return f - elif check(f): - f.seek(0) - return f - f.close() - - if os.path.exists(fn): - f = check_valid_file(fn) - if f is not None: - return f - unlink(fn) - - # Verify the requirement before downloading the file - requires('urlfetch') - - print('\tfetching %s ...' % url, file=get_original_stdout()) - f = request.urlopen(url, timeout=15) - try: - with open(fn, "wb") as out: - s = f.read() - while s: - out.write(s) - s = f.read() - finally: - f.close() - - f = check_valid_file(fn) - if f is not None: - return f - raise TestFailed('invalid resource "%s"' % fn) - - -class WarningsRecorder(object): - """Convenience wrapper for the warnings list returned on - entry to the warnings.catch_warnings() context manager. - """ - def __init__(self, warnings_list): - self._warnings = warnings_list - self._last = 0 - - def __getattr__(self, attr): - if len(self._warnings) > self._last: - return getattr(self._warnings[-1], attr) - elif attr in warnings.WarningMessage._WARNING_DETAILS: - return None - raise AttributeError("%r has no attribute %r" % (self, attr)) - - #@property - #def warnings(self): - # return self._warnings[self._last:] - - def reset(self): - self._last = len(self._warnings) - - -def _filterwarnings(filters, quiet=False): - """Catch the warnings, then check if all the expected - warnings have been raised and re-raise unexpected warnings. - If 'quiet' is True, only re-raise the unexpected warnings. - """ - # Clear the warning registry of the calling module - # in order to re-raise the warnings. - frame = sys._getframe(2) - registry = frame.f_globals.get('__warningregistry__') - if registry: - registry.clear() - with warnings.catch_warnings(record=True) as w: - # Set filter "always" to record all warnings. Because - # test_warnings swap the module, we need to look up in - # the sys.modules dictionary. - sys.modules['warnings'].simplefilter("always") - yield WarningsRecorder(w) - # Filter the recorded warnings - reraise = list(w) - missing = [] - for msg, cat in filters: - seen = False - for w in reraise[:]: - warning = w.message - # Filter out the matching messages - if (re.match(msg, str(warning), re.I) and - issubclass(warning.__class__, cat)): - seen = True - reraise.remove(w) - if not seen and not quiet: - # This filter caught nothing - missing.append((msg, cat.__name__)) - if reraise: - raise AssertionError("unhandled warning %s" % reraise[0]) - if missing: - raise AssertionError("filter (%r, %s) did not catch any warning" % - missing[0]) - - -@contextlib.contextmanager -def check_warnings(*filters, **kwargs): - """Context manager to silence warnings. - - Accept 2-tuples as positional arguments: - ("message regexp", WarningCategory) - - Optional argument: - - if 'quiet' is True, it does not fail if a filter catches nothing - (default True without argument, - default False if some filters are defined) - - Without argument, it defaults to: - check_warnings(("", Warning), quiet=True) - """ - quiet = kwargs.get('quiet') - if not filters: - filters = (("", Warning),) - # Preserve backward compatibility - if quiet is None: - quiet = True - return _filterwarnings(filters, quiet) - - -class CleanImport(object): - """Context manager to force import to return a new module reference. - - This is useful for testing module-level behaviours, such as - the emission of a DeprecationWarning on import. - - Use like this: - - with CleanImport("foo"): - importlib.import_module("foo") # new reference - """ - - def __init__(self, *module_names): - self.original_modules = sys.modules.copy() - for module_name in module_names: - if module_name in sys.modules: - module = sys.modules[module_name] - # It is possible that module_name is just an alias for - # another module (e.g. stub for modules renamed in 3.x). - # In that case, we also need delete the real module to clear - # the import cache. - if module.__name__ != module_name: - del sys.modules[module.__name__] - del sys.modules[module_name] - - def __enter__(self): - return self - - def __exit__(self, *ignore_exc): - sys.modules.update(self.original_modules) - - -class EnvironmentVarGuard(dict): - - """Class to help protect the environment variable properly. Can be used as - a context manager.""" - - def __init__(self): - self._environ = os.environ - self._changed = {} - - def __getitem__(self, envvar): - return self._environ[envvar] - - def __setitem__(self, envvar, value): - # Remember the initial value on the first access - if envvar not in self._changed: - self._changed[envvar] = self._environ.get(envvar) - self._environ[envvar] = value - - def __delitem__(self, envvar): - # Remember the initial value on the first access - if envvar not in self._changed: - self._changed[envvar] = self._environ.get(envvar) - if envvar in self._environ: - del self._environ[envvar] - - def keys(self): - return self._environ.keys() - - def __iter__(self): - return iter(self._environ) - - def __len__(self): - return len(self._environ) - - def set(self, envvar, value): - self[envvar] = value - - def unset(self, envvar): - del self[envvar] - - def __enter__(self): - return self - - def __exit__(self, *ignore_exc): - for k, v in self._changed.items(): - if v is None: - if k in self._environ: - del self._environ[k] - else: - self._environ[k] = v - os.environ = self._environ - - -class DirsOnSysPath(object): - """Context manager to temporarily add directories to sys.path. - - This makes a copy of sys.path, appends any directories given - as positional arguments, then reverts sys.path to the copied - settings when the context ends. - - Note that *all* sys.path modifications in the body of the - context manager, including replacement of the object, - will be reverted at the end of the block. - """ - - def __init__(self, *paths): - self.original_value = sys.path[:] - self.original_object = sys.path - sys.path.extend(paths) - - def __enter__(self): - return self - - def __exit__(self, *ignore_exc): - sys.path = self.original_object - sys.path[:] = self.original_value - - -class TransientResource(object): - - """Raise ResourceDenied if an exception is raised while the context manager - is in effect that matches the specified exception and attributes.""" - - def __init__(self, exc, **kwargs): - self.exc = exc - self.attrs = kwargs - - def __enter__(self): - return self - - def __exit__(self, type_=None, value=None, traceback=None): - """If type_ is a subclass of self.exc and value has attributes matching - self.attrs, raise ResourceDenied. Otherwise let the exception - propagate (if any).""" - if type_ is not None and issubclass(self.exc, type_): - for attr, attr_value in self.attrs.items(): - if not hasattr(value, attr): - break - if getattr(value, attr) != attr_value: - break - else: - raise ResourceDenied("an optional resource is not available") - -# Context managers that raise ResourceDenied when various issues -# with the Internet connection manifest themselves as exceptions. -# XXX deprecate these and use transient_internet() instead -time_out = TransientResource(IOError, errno=errno.ETIMEDOUT) -socket_peer_reset = TransientResource(socket.error, errno=errno.ECONNRESET) -ioerror_peer_reset = TransientResource(IOError, errno=errno.ECONNRESET) - - -@contextlib.contextmanager -def transient_internet(resource_name, *, timeout=30.0, errnos=()): - """Return a context manager that raises ResourceDenied when various issues - with the Internet connection manifest themselves as exceptions.""" - default_errnos = [ - ('ECONNREFUSED', 111), - ('ECONNRESET', 104), - ('EHOSTUNREACH', 113), - ('ENETUNREACH', 101), - ('ETIMEDOUT', 110), - ] - default_gai_errnos = [ - ('EAI_AGAIN', -3), - ('EAI_FAIL', -4), - ('EAI_NONAME', -2), - ('EAI_NODATA', -5), - # Encountered when trying to resolve IPv6-only hostnames - ('WSANO_DATA', 11004), - ] - - denied = ResourceDenied("Resource '%s' is not available" % resource_name) - captured_errnos = errnos - gai_errnos = [] - if not captured_errnos: - captured_errnos = [getattr(errno, name, num) - for name, num in default_errnos] - gai_errnos = [getattr(socket, name, num) - for name, num in default_gai_errnos] - - def filter_error(err): - n = getattr(err, 'errno', None) - if (isinstance(err, socket.timeout) or - (isinstance(err, socket.gaierror) and n in gai_errnos) or - n in captured_errnos): - if not verbose: - sys.stderr.write(denied.args[0] + "\n") - raise denied from err - - old_timeout = socket.getdefaulttimeout() - try: - if timeout is not None: - socket.setdefaulttimeout(timeout) - yield None - except IOError as err: - # urllib can wrap original socket errors multiple times (!), we must - # unwrap to get at the original error. - while True: - a = err.args - if len(a) >= 1 and isinstance(a[0], IOError): - err = a[0] - # The error can also be wrapped as args[1]: - # except socket.error as msg: - # raise IOError('socket error', msg).with_traceback(sys.exc_info()[2]) - elif len(a) >= 2 and isinstance(a[1], IOError): - err = a[1] - else: - break - filter_error(err) - raise - # XXX should we catch generic exceptions and look for their - # __cause__ or __context__? - finally: - socket.setdefaulttimeout(old_timeout) - - -@contextlib.contextmanager -def captured_output(stream_name): - """Return a context manager used by captured_stdout/stdin/stderr - that temporarily replaces the sys stream *stream_name* with a StringIO.""" - import io - orig_stdout = getattr(sys, stream_name) - setattr(sys, stream_name, io.StringIO()) - try: - yield getattr(sys, stream_name) - finally: - setattr(sys, stream_name, orig_stdout) - -def captured_stdout(): - """Capture the output of sys.stdout: - - with captured_stdout() as s: - print("hello") - self.assertEqual(s.getvalue(), "hello") - """ - return captured_output("stdout") - -def captured_stderr(): - return captured_output("stderr") - -def captured_stdin(): - return captured_output("stdin") - - -def gc_collect(): - """Force as many objects as possible to be collected. - - In non-CPython implementations of Python, this is needed because timely - deallocation is not guaranteed by the garbage collector. (Even in CPython - this can be the case in case of reference cycles.) This means that __del__ - methods may be called later than expected and weakrefs may remain alive for - longer than expected. This function tries its best to force all garbage - objects to disappear. - """ - gc.collect() - if is_jython: - time.sleep(0.1) - gc.collect() - gc.collect() - - -def python_is_optimized(): - """Find if Python was built with optimizations.""" - cflags = sysconfig.get_config_var('PY_CFLAGS') or '' - final_opt = "" - for opt in cflags.split(): - if opt.startswith('-O'): - final_opt = opt - return final_opt and final_opt != '-O0' - - -#======================================================================= -# Decorator for running a function in a different locale, correctly resetting -# it afterwards. - -def run_with_locale(catstr, *locales): - def decorator(func): - def inner(*args, **kwds): - try: - import locale - category = getattr(locale, catstr) - orig_locale = locale.setlocale(category) - except AttributeError: - # if the test author gives us an invalid category string - raise - except: - # cannot retrieve original locale, so do nothing - locale = orig_locale = None - else: - for loc in locales: - try: - locale.setlocale(category, loc) - break - except: - pass - - # now run the function, resetting the locale on exceptions - try: - return func(*args, **kwds) - finally: - if locale and orig_locale: - locale.setlocale(category, orig_locale) - inner.__name__ = func.__name__ - inner.__doc__ = func.__doc__ - return inner - return decorator - -#======================================================================= -# Big-memory-test support. Separate from 'resources' because memory use -# should be configurable. - -# Some handy shorthands. Note that these are used for byte-limits as well -# as size-limits, in the various bigmem tests -_1M = 1024*1024 -_1G = 1024 * _1M -_2G = 2 * _1G -_4G = 4 * _1G - -MAX_Py_ssize_t = sys.maxsize - -def set_memlimit(limit): - global max_memuse - global real_max_memuse - sizes = { - 'k': 1024, - 'm': _1M, - 'g': _1G, - 't': 1024*_1G, - } - m = re.match(r'(\d+(\.\d+)?) (K|M|G|T)b?$', limit, - re.IGNORECASE | re.VERBOSE) - if m is None: - raise ValueError('Invalid memory limit %r' % (limit,)) - memlimit = int(float(m.group(1)) * sizes[m.group(3).lower()]) - real_max_memuse = memlimit - if memlimit > MAX_Py_ssize_t: - memlimit = MAX_Py_ssize_t - if memlimit < _2G - 1: - raise ValueError('Memory limit %r too low to be useful' % (limit,)) - max_memuse = memlimit - -def _memory_watchdog(start_evt, finish_evt, period=10.0): - """A function which periodically watches the process' memory consumption - and prints it out. - """ - # XXX: because of the GIL, and because the very long operations tested - # in most bigmem tests are uninterruptible, the loop below gets woken up - # much less often than expected. - # The polling code should be rewritten in raw C, without holding the GIL, - # and push results onto an anonymous pipe. - try: - page_size = os.sysconf('SC_PAGESIZE') - except (ValueError, AttributeError): - try: - page_size = os.sysconf('SC_PAGE_SIZE') - except (ValueError, AttributeError): - page_size = 4096 - procfile = '/proc/{pid}/statm'.format(pid=os.getpid()) - try: - f = open(procfile, 'rb') - except IOError as e: - warnings.warn('/proc not available for stats: {}'.format(e), - RuntimeWarning) - sys.stderr.flush() - return - with f: - start_evt.set() - old_data = -1 - while not finish_evt.wait(period): - f.seek(0) - statm = f.read().decode('ascii') - data = int(statm.split()[5]) - if data != old_data: - old_data = data - print(" ... process data size: {data:.1f}G" - .format(data=data * page_size / (1024 ** 3))) - -def bigmemtest(size, memuse, dry_run=True): - """Decorator for bigmem tests. - - 'minsize' is the minimum useful size for the test (in arbitrary, - test-interpreted units.) 'memuse' is the number of 'bytes per size' for - the test, or a good estimate of it. - - if 'dry_run' is False, it means the test doesn't support dummy runs - when -M is not specified. - """ - def decorator(f): - def wrapper(self): - size = wrapper.size - memuse = wrapper.memuse - if not real_max_memuse: - maxsize = 5147 - else: - maxsize = size - - if ((real_max_memuse or not dry_run) - and real_max_memuse < maxsize * memuse): - raise unittest.SkipTest( - "not enough memory: %.1fG minimum needed" - % (size * memuse / (1024 ** 3))) - - if real_max_memuse and verbose and threading: - print() - print(" ... expected peak memory use: {peak:.1f}G" - .format(peak=size * memuse / (1024 ** 3))) - sys.stdout.flush() - start_evt = threading.Event() - finish_evt = threading.Event() - t = threading.Thread(target=_memory_watchdog, - args=(start_evt, finish_evt, 0.5)) - t.daemon = True - t.start() - start_evt.set() - else: - t = None - - try: - return f(self, maxsize) - finally: - if t: - finish_evt.set() - t.join() - - wrapper.size = size - wrapper.memuse = memuse - return wrapper - return decorator - -def bigaddrspacetest(f): - """Decorator for tests that fill the address space.""" - def wrapper(self): - if max_memuse < MAX_Py_ssize_t: - if MAX_Py_ssize_t >= 2**63 - 1 and max_memuse >= 2**31: - raise unittest.SkipTest( - "not enough memory: try a 32-bit build instead") - else: - raise unittest.SkipTest( - "not enough memory: %.1fG minimum needed" - % (MAX_Py_ssize_t / (1024 ** 3))) - else: - return f(self) - return wrapper - -#======================================================================= -# unittest integration. - -class BasicTestRunner: - def run(self, test): - result = unittest.TestResult() - test(result) - return result - -def _id(obj): - return obj - -def requires_resource(resource): - if resource == 'gui' and not _is_gui_available(): - return unittest.skip("resource 'gui' is not available") - if is_resource_enabled(resource): - return _id - else: - return unittest.skip("resource {0!r} is not enabled".format(resource)) - -def cpython_only(test): - """ - Decorator for tests only applicable on CPython. - """ - return impl_detail(cpython=True)(test) - -def impl_detail(msg=None, **guards): - if check_impl_detail(**guards): - return _id - if msg is None: - guardnames, default = _parse_guards(guards) - if default: - msg = "implementation detail not available on {0}" - else: - msg = "implementation detail specific to {0}" - guardnames = sorted(guardnames.keys()) - msg = msg.format(' or '.join(guardnames)) - return unittest.skip(msg) - -def _parse_guards(guards): - # Returns a tuple ({platform_name: run_me}, default_value) - if not guards: - return ({'cpython': True}, False) - is_true = list(guards.values())[0] - assert list(guards.values()) == [is_true] * len(guards) # all True or all False - return (guards, not is_true) - -# Use the following check to guard CPython's implementation-specific tests -- -# or to run them only on the implementation(s) guarded by the arguments. -def check_impl_detail(**guards): - """This function returns True or False depending on the host platform. - Examples: - if check_impl_detail(): # only on CPython (default) - if check_impl_detail(jython=True): # only on Jython - if check_impl_detail(cpython=False): # everywhere except on CPython - """ - guards, default = _parse_guards(guards) - return guards.get(platform.python_implementation().lower(), default) - - -def _filter_suite(suite, pred): - """Recursively filter test cases in a suite based on a predicate.""" - newtests = [] - for test in suite._tests: - if isinstance(test, unittest.TestSuite): - _filter_suite(test, pred) - newtests.append(test) - else: - if pred(test): - newtests.append(test) - suite._tests = newtests - - -def _run_suite(suite): - """Run tests from a unittest.TestSuite-derived class.""" - if verbose: - runner = unittest.TextTestRunner(sys.stdout, verbosity=2, - failfast=failfast) - else: - runner = BasicTestRunner() - - result = runner.run(suite) - if not result.wasSuccessful(): - if len(result.errors) == 1 and not result.failures: - err = result.errors[0][1] - elif len(result.failures) == 1 and not result.errors: - err = result.failures[0][1] - else: - err = "multiple errors occurred" - if not verbose: err += "; run in verbose mode for details" - raise TestFailed(err) - - -def run_unittest(*classes): - """Run tests from unittest.TestCase-derived classes.""" - valid_types = (unittest.TestSuite, unittest.TestCase) - suite = unittest.TestSuite() - for cls in classes: - if isinstance(cls, str): - if cls in sys.modules: - suite.addTest(unittest.findTestCases(sys.modules[cls])) - else: - raise ValueError("str arguments must be keys in sys.modules") - elif isinstance(cls, valid_types): - suite.addTest(cls) - else: - suite.addTest(unittest.makeSuite(cls)) - def case_pred(test): - if match_tests is None: - return True - for name in test.id().split("."): - if fnmatch.fnmatchcase(name, match_tests): - return True - return False - _filter_suite(suite, case_pred) - _run_suite(suite) - - -#======================================================================= -# doctest driver. - -def run_doctest(module, verbosity=None): - """Run doctest on the given module. Return (#failures, #tests). - - If optional argument verbosity is not specified (or is None), pass - support's belief about verbosity on to doctest. Else doctest's - usual behavior is used (it searches sys.argv for -v). - """ - - import doctest - - if verbosity is None: - verbosity = verbose - else: - verbosity = None - - f, t = doctest.testmod(module, verbose=verbosity) - if f: - raise TestFailed("%d of %d doctests failed" % (f, t)) - if verbose: - print('doctest (%s) ... %d tests with zero failures' % - (module.__name__, t)) - return f, t - - -#======================================================================= -# Support for saving and restoring the imported modules. - -def modules_setup(): - return sys.modules.copy(), - -def modules_cleanup(oldmodules): - # Encoders/decoders are registered permanently within the internal - # codec cache. If we destroy the corresponding modules their - # globals will be set to None which will trip up the cached functions. - encodings = [(k, v) for k, v in sys.modules.items() - if k.startswith('encodings.')] - sys.modules.clear() - sys.modules.update(encodings) - # XXX: This kind of problem can affect more than just encodings. In particular - # extension modules (such as _ssl) don't cope with reloading properly. - # Really, test modules should be cleaning out the test specific modules they - # know they added (ala test_runpy) rather than relying on this function (as - # test_importhooks and test_pkg do currently). - # Implicitly imported *real* modules should be left alone (see issue 10556). - sys.modules.update(oldmodules) - -#======================================================================= -# Threading support to prevent reporting refleaks when running regrtest.py -R - -# NOTE: we use thread._count() rather than threading.enumerate() (or the -# moral equivalent thereof) because a threading.Thread object is still alive -# until its __bootstrap() method has returned, even after it has been -# unregistered from the threading module. -# thread._count(), on the other hand, only gets decremented *after* the -# __bootstrap() method has returned, which gives us reliable reference counts -# at the end of a test run. - -def threading_setup(): - if _thread: - return _thread._count(), threading._dangling.copy() - else: - return 1, () - -def threading_cleanup(*original_values): - if not _thread: - return - _MAX_COUNT = 10 - for count in range(_MAX_COUNT): - values = _thread._count(), threading._dangling - if values == original_values: - break - time.sleep(0.1) - gc_collect() - # XXX print a warning in case of failure? - -def reap_threads(func): - """Use this function when threads are being used. This will - ensure that the threads are cleaned up even when the test fails. - If threading is unavailable this function does nothing. - """ - if not _thread: - return func - - @functools.wraps(func) - def decorator(*args): - key = threading_setup() - try: - return func(*args) - finally: - threading_cleanup(*key) - return decorator - -def reap_children(): - """Use this function at the end of test_main() whenever sub-processes - are started. This will help ensure that no extra children (zombies) - stick around to hog resources and create problems when looking - for refleaks. - """ - - # Reap all our dead child processes so we don't leave zombies around. - # These hog resources and might be causing some of the buildbots to die. - if hasattr(os, 'waitpid'): - any_process = -1 - while True: - try: - # This will raise an exception on Windows. That's ok. - pid, status = os.waitpid(any_process, os.WNOHANG) - if pid == 0: - break - except: - break - -@contextlib.contextmanager -def swap_attr(obj, attr, new_val): - """Temporary swap out an attribute with a new object. - - Usage: - with swap_attr(obj, "attr", 5): - ... - - This will set obj.attr to 5 for the duration of the with: block, - restoring the old value at the end of the block. If `attr` doesn't - exist on `obj`, it will be created and then deleted at the end of the - block. - """ - if hasattr(obj, attr): - real_val = getattr(obj, attr) - setattr(obj, attr, new_val) - try: - yield None - finally: - setattr(obj, attr, real_val) - else: - setattr(obj, attr, new_val) - try: - yield None - finally: - delattr(obj, attr) - -@contextlib.contextmanager -def swap_item(obj, item, new_val): - """Temporary swap out an item with a new object. - - Usage: - with swap_item(obj, "item", 5): - ... - - This will set obj["item"] to 5 for the duration of the with: block, - restoring the old value at the end of the block. If `item` doesn't - exist on `obj`, it will be created and then deleted at the end of the - block. - """ - if item in obj: - real_val = obj[item] - obj[item] = new_val - try: - yield None - finally: - obj[item] = real_val - else: - obj[item] = new_val - try: - yield None - finally: - del obj[item] - -def strip_python_stderr(stderr): - """Strip the stderr of a Python process from potential debug output - emitted by the interpreter. - - This will typically be run on the result of the communicate() method - of a subprocess.Popen object. - """ - stderr = re.sub(br"\[\d+ refs\]\r?\n?$", b"", stderr).strip() - return stderr - -def args_from_interpreter_flags(): - """Return a list of command-line arguments reproducing the current - settings in sys.flags.""" - flag_opt_map = { - 'bytes_warning': 'b', - 'dont_write_bytecode': 'B', - 'hash_randomization': 'R', - 'ignore_environment': 'E', - 'no_user_site': 's', - 'no_site': 'S', - 'optimize': 'O', - 'verbose': 'v', - } - args = [] - for flag, opt in flag_opt_map.items(): - v = getattr(sys.flags, flag) - if v > 0: - args.append('-' + opt * v) - return args - -#============================================================ -# Support for assertions about logging. -#============================================================ - -class TestHandler(logging.handlers.BufferingHandler): - def __init__(self, matcher): - # BufferingHandler takes a "capacity" argument - # so as to know when to flush. As we're overriding - # shouldFlush anyway, we can set a capacity of zero. - # You can call flush() manually to clear out the - # buffer. - logging.handlers.BufferingHandler.__init__(self, 0) - self.matcher = matcher - - def shouldFlush(self, record): - return False - - def emit(self, record): - self.format(record) - self.buffer.append(record.__dict__) - - def matches(self, **kwargs): - """ - Look for a saved dict whose keys/values match the supplied arguments. - """ - result = False - for d in self.buffer: - if self.matcher.matches(d, **kwargs): - result = True - break - return result - -class Matcher(object): - - _partial_matches = ('msg', 'message') - - def matches(self, d, **kwargs): - """ - Try to match a single dict with the supplied arguments. - - Keys whose values are strings and which are in self._partial_matches - will be checked for partial (i.e. substring) matches. You can extend - this scheme to (for example) do regular expression matching, etc. - """ - result = True - for k in kwargs: - v = kwargs[k] - dv = d.get(k) - if not self.match_value(k, dv, v): - result = False - break - return result - - def match_value(self, k, dv, v): - """ - Try to match a single stored value (dv) with a supplied value (v). - """ - if type(v) != type(dv): - result = False - elif type(dv) is not str or k not in self._partial_matches: - result = (v == dv) - else: - result = dv.find(v) >= 0 - return result - - -_can_symlink = None # type: Any -def can_symlink(): - global _can_symlink - if _can_symlink is not None: - return _can_symlink - symlink_path = TESTFN + "can_symlink" - try: - os.symlink(TESTFN, symlink_path) - can = True - except (OSError, NotImplementedError, AttributeError): - can = False - else: - os.remove(symlink_path) - _can_symlink = can - return can - -def skip_unless_symlink(test): - """Skip decorator for tests that require functional symlink""" - ok = can_symlink() - msg = "Requires functional symlink implementation" - if ok: - return test - else: - return unittest.skip(msg)(test) - -def patch(test_instance, object_to_patch, attr_name, new_value): - """Override 'object_to_patch'.'attr_name' with 'new_value'. - - Also, add a cleanup procedure to 'test_instance' to restore - 'object_to_patch' value for 'attr_name'. - The 'attr_name' should be a valid attribute for 'object_to_patch'. - - """ - # check that 'attr_name' is a real attribute for 'object_to_patch' - # will raise AttributeError if it does not exist - getattr(object_to_patch, attr_name) - - # keep a copy of the old value - attr_is_local = False - try: - old_value = object_to_patch.__dict__[attr_name] - except (AttributeError, KeyError): - old_value = getattr(object_to_patch, attr_name, None) - else: - attr_is_local = True - - # restore the value when the test is done - def cleanup(): - if attr_is_local: - setattr(object_to_patch, attr_name, old_value) - else: - delattr(object_to_patch, attr_name) - - test_instance.addCleanup(cleanup) - - # actually override the attribute - setattr(object_to_patch, attr_name, new_value) diff --git a/test-data/stdlib-samples/3.2/test/test_base64.py b/test-data/stdlib-samples/3.2/test/test_base64.py deleted file mode 100644 index 9e4dcf5544ed..000000000000 --- a/test-data/stdlib-samples/3.2/test/test_base64.py +++ /dev/null @@ -1,267 +0,0 @@ -import unittest -from test import support -import base64 -import binascii -import sys -import subprocess - -from typing import Any - - - -class LegacyBase64TestCase(unittest.TestCase): - def test_encodebytes(self) -> None: - eq = self.assertEqual - eq(base64.encodebytes(b"www.python.org"), b"d3d3LnB5dGhvbi5vcmc=\n") - eq(base64.encodebytes(b"a"), b"YQ==\n") - eq(base64.encodebytes(b"ab"), b"YWI=\n") - eq(base64.encodebytes(b"abc"), b"YWJj\n") - eq(base64.encodebytes(b""), b"") - eq(base64.encodebytes(b"abcdefghijklmnopqrstuvwxyz" - b"ABCDEFGHIJKLMNOPQRSTUVWXYZ" - b"0123456789!@#0^&*();:<>,. []{}"), - b"YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE" - b"RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0\nNT" - b"Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ==\n") - self.assertRaises(TypeError, base64.encodebytes, "") - - def test_decodebytes(self) -> None: - eq = self.assertEqual - eq(base64.decodebytes(b"d3d3LnB5dGhvbi5vcmc=\n"), b"www.python.org") - eq(base64.decodebytes(b"YQ==\n"), b"a") - eq(base64.decodebytes(b"YWI=\n"), b"ab") - eq(base64.decodebytes(b"YWJj\n"), b"abc") - eq(base64.decodebytes(b"YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE" - b"RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0\nNT" - b"Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ==\n"), - b"abcdefghijklmnopqrstuvwxyz" - b"ABCDEFGHIJKLMNOPQRSTUVWXYZ" - b"0123456789!@#0^&*();:<>,. []{}") - eq(base64.decodebytes(b''), b'') - self.assertRaises(TypeError, base64.decodebytes, "") - - def test_encode(self) -> None: - eq = self.assertEqual - from io import BytesIO - infp = BytesIO(b'abcdefghijklmnopqrstuvwxyz' - b'ABCDEFGHIJKLMNOPQRSTUVWXYZ' - b'0123456789!@#0^&*();:<>,. []{}') - outfp = BytesIO() - base64.encode(infp, outfp) - eq(outfp.getvalue(), - b'YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE' - b'RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0\nNT' - b'Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ==\n') - - def test_decode(self) -> None: - from io import BytesIO - infp = BytesIO(b'd3d3LnB5dGhvbi5vcmc=') - outfp = BytesIO() - base64.decode(infp, outfp) - self.assertEqual(outfp.getvalue(), b'www.python.org') - - -class BaseXYTestCase(unittest.TestCase): - def test_b64encode(self) -> None: - eq = self.assertEqual - # Test default alphabet - eq(base64.b64encode(b"www.python.org"), b"d3d3LnB5dGhvbi5vcmc=") - eq(base64.b64encode(b'\x00'), b'AA==') - eq(base64.b64encode(b"a"), b"YQ==") - eq(base64.b64encode(b"ab"), b"YWI=") - eq(base64.b64encode(b"abc"), b"YWJj") - eq(base64.b64encode(b""), b"") - eq(base64.b64encode(b"abcdefghijklmnopqrstuvwxyz" - b"ABCDEFGHIJKLMNOPQRSTUVWXYZ" - b"0123456789!@#0^&*();:<>,. []{}"), - b"YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE" - b"RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0NT" - b"Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ==") - # Test with arbitrary alternative characters - eq(base64.b64encode(b'\xd3V\xbeo\xf7\x1d', altchars=b'*$'), b'01a*b$cd') - # Check if passing a str object raises an error - self.assertRaises(TypeError, base64.b64encode, "") - self.assertRaises(TypeError, base64.b64encode, b"", altchars="") - # Test standard alphabet - eq(base64.standard_b64encode(b"www.python.org"), b"d3d3LnB5dGhvbi5vcmc=") - eq(base64.standard_b64encode(b"a"), b"YQ==") - eq(base64.standard_b64encode(b"ab"), b"YWI=") - eq(base64.standard_b64encode(b"abc"), b"YWJj") - eq(base64.standard_b64encode(b""), b"") - eq(base64.standard_b64encode(b"abcdefghijklmnopqrstuvwxyz" - b"ABCDEFGHIJKLMNOPQRSTUVWXYZ" - b"0123456789!@#0^&*();:<>,. []{}"), - b"YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE" - b"RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0NT" - b"Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ==") - # Check if passing a str object raises an error - self.assertRaises(TypeError, base64.standard_b64encode, "") - self.assertRaises(TypeError, base64.standard_b64encode, b"", altchars="") - # Test with 'URL safe' alternative characters - eq(base64.urlsafe_b64encode(b'\xd3V\xbeo\xf7\x1d'), b'01a-b_cd') - # Check if passing a str object raises an error - self.assertRaises(TypeError, base64.urlsafe_b64encode, "") - - def test_b64decode(self) -> None: - eq = self.assertEqual - eq(base64.b64decode(b"d3d3LnB5dGhvbi5vcmc="), b"www.python.org") - eq(base64.b64decode(b'AA=='), b'\x00') - eq(base64.b64decode(b"YQ=="), b"a") - eq(base64.b64decode(b"YWI="), b"ab") - eq(base64.b64decode(b"YWJj"), b"abc") - eq(base64.b64decode(b"YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE" - b"RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0\nNT" - b"Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ=="), - b"abcdefghijklmnopqrstuvwxyz" - b"ABCDEFGHIJKLMNOPQRSTUVWXYZ" - b"0123456789!@#0^&*();:<>,. []{}") - eq(base64.b64decode(b''), b'') - # Test with arbitrary alternative characters - eq(base64.b64decode(b'01a*b$cd', altchars=b'*$'), b'\xd3V\xbeo\xf7\x1d') - # Check if passing a str object raises an error - self.assertRaises(TypeError, base64.b64decode, "") - self.assertRaises(TypeError, base64.b64decode, b"", altchars="") - # Test standard alphabet - eq(base64.standard_b64decode(b"d3d3LnB5dGhvbi5vcmc="), b"www.python.org") - eq(base64.standard_b64decode(b"YQ=="), b"a") - eq(base64.standard_b64decode(b"YWI="), b"ab") - eq(base64.standard_b64decode(b"YWJj"), b"abc") - eq(base64.standard_b64decode(b""), b"") - eq(base64.standard_b64decode(b"YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE" - b"RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0NT" - b"Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ=="), - b"abcdefghijklmnopqrstuvwxyz" - b"ABCDEFGHIJKLMNOPQRSTUVWXYZ" - b"0123456789!@#0^&*();:<>,. []{}") - # Check if passing a str object raises an error - self.assertRaises(TypeError, base64.standard_b64decode, "") - self.assertRaises(TypeError, base64.standard_b64decode, b"", altchars="") - # Test with 'URL safe' alternative characters - eq(base64.urlsafe_b64decode(b'01a-b_cd'), b'\xd3V\xbeo\xf7\x1d') - self.assertRaises(TypeError, base64.urlsafe_b64decode, "") - - def test_b64decode_padding_error(self) -> None: - self.assertRaises(binascii.Error, base64.b64decode, b'abc') - - def test_b64decode_invalid_chars(self) -> None: - # issue 1466065: Test some invalid characters. - tests = ((b'%3d==', b'\xdd'), - (b'$3d==', b'\xdd'), - (b'[==', b''), - (b'YW]3=', b'am'), - (b'3{d==', b'\xdd'), - (b'3d}==', b'\xdd'), - (b'@@', b''), - (b'!', b''), - (b'YWJj\nYWI=', b'abcab')) - for bstr, res in tests: - self.assertEqual(base64.b64decode(bstr), res) - with self.assertRaises(binascii.Error): - base64.b64decode(bstr, validate=True) - - def test_b32encode(self) -> None: - eq = self.assertEqual - eq(base64.b32encode(b''), b'') - eq(base64.b32encode(b'\x00'), b'AA======') - eq(base64.b32encode(b'a'), b'ME======') - eq(base64.b32encode(b'ab'), b'MFRA====') - eq(base64.b32encode(b'abc'), b'MFRGG===') - eq(base64.b32encode(b'abcd'), b'MFRGGZA=') - eq(base64.b32encode(b'abcde'), b'MFRGGZDF') - self.assertRaises(TypeError, base64.b32encode, "") - - def test_b32decode(self) -> None: - eq = self.assertEqual - eq(base64.b32decode(b''), b'') - eq(base64.b32decode(b'AA======'), b'\x00') - eq(base64.b32decode(b'ME======'), b'a') - eq(base64.b32decode(b'MFRA===='), b'ab') - eq(base64.b32decode(b'MFRGG==='), b'abc') - eq(base64.b32decode(b'MFRGGZA='), b'abcd') - eq(base64.b32decode(b'MFRGGZDF'), b'abcde') - self.assertRaises(TypeError, base64.b32decode, "") - - def test_b32decode_casefold(self) -> None: - eq = self.assertEqual - eq(base64.b32decode(b'', True), b'') - eq(base64.b32decode(b'ME======', True), b'a') - eq(base64.b32decode(b'MFRA====', True), b'ab') - eq(base64.b32decode(b'MFRGG===', True), b'abc') - eq(base64.b32decode(b'MFRGGZA=', True), b'abcd') - eq(base64.b32decode(b'MFRGGZDF', True), b'abcde') - # Lower cases - eq(base64.b32decode(b'me======', True), b'a') - eq(base64.b32decode(b'mfra====', True), b'ab') - eq(base64.b32decode(b'mfrgg===', True), b'abc') - eq(base64.b32decode(b'mfrggza=', True), b'abcd') - eq(base64.b32decode(b'mfrggzdf', True), b'abcde') - # Expected exceptions - self.assertRaises(TypeError, base64.b32decode, b'me======') - # Mapping zero and one - eq(base64.b32decode(b'MLO23456'), b'b\xdd\xad\xf3\xbe') - eq(base64.b32decode(b'M1023456', map01=b'L'), b'b\xdd\xad\xf3\xbe') - eq(base64.b32decode(b'M1023456', map01=b'I'), b'b\x1d\xad\xf3\xbe') - self.assertRaises(TypeError, base64.b32decode, b"", map01="") - - def test_b32decode_error(self) -> None: - self.assertRaises(binascii.Error, base64.b32decode, b'abc') - self.assertRaises(binascii.Error, base64.b32decode, b'ABCDEF==') - - def test_b16encode(self) -> None: - eq = self.assertEqual - eq(base64.b16encode(b'\x01\x02\xab\xcd\xef'), b'0102ABCDEF') - eq(base64.b16encode(b'\x00'), b'00') - self.assertRaises(TypeError, base64.b16encode, "") - - def test_b16decode(self) -> None: - eq = self.assertEqual - eq(base64.b16decode(b'0102ABCDEF'), b'\x01\x02\xab\xcd\xef') - eq(base64.b16decode(b'00'), b'\x00') - # Lower case is not allowed without a flag - self.assertRaises(binascii.Error, base64.b16decode, b'0102abcdef') - # Case fold - eq(base64.b16decode(b'0102abcdef', True), b'\x01\x02\xab\xcd\xef') - self.assertRaises(TypeError, base64.b16decode, "") - - def test_ErrorHeritage(self) -> None: - self.assertTrue(issubclass(binascii.Error, ValueError)) - - - -class TestMain(unittest.TestCase): - def get_output(self, *args_tuple: str, **options: Any) -> Any: - args = [sys.executable, '-m', 'base64'] + list(args_tuple) - return subprocess.check_output(args, **options) - - def test_encode_decode(self) -> None: - output = self.get_output('-t') - self.assertSequenceEqual(output.splitlines(), [ - b"b'Aladdin:open sesame'", - br"b'QWxhZGRpbjpvcGVuIHNlc2FtZQ==\n'", - b"b'Aladdin:open sesame'", - ]) - - def test_encode_file(self) -> None: - with open(support.TESTFN, 'wb') as fp: - fp.write(b'a\xffb\n') - - output = self.get_output('-e', support.TESTFN) - self.assertEqual(output.rstrip(), b'Yf9iCg==') - - with open(support.TESTFN, 'rb') as fp: - output = self.get_output('-e', stdin=fp) - self.assertEqual(output.rstrip(), b'Yf9iCg==') - - def test_decode(self) -> None: - with open(support.TESTFN, 'wb') as fp: - fp.write(b'Yf9iCg==') - output = self.get_output('-d', support.TESTFN) - self.assertEqual(output.rstrip(), b'a\xffb') - - - -def test_main() -> None: - support.run_unittest(__name__) - -if __name__ == '__main__': - test_main() diff --git a/test-data/stdlib-samples/3.2/test/test_fnmatch.py b/test-data/stdlib-samples/3.2/test/test_fnmatch.py deleted file mode 100644 index b5309c118be0..000000000000 --- a/test-data/stdlib-samples/3.2/test/test_fnmatch.py +++ /dev/null @@ -1,93 +0,0 @@ -"""Test cases for the fnmatch module.""" - -from test import support -import unittest - -from fnmatch import fnmatch, fnmatchcase, translate, filter - -from typing import Any, AnyStr, Callable - -class FnmatchTestCase(unittest.TestCase): - - def check_match(self, filename: AnyStr, pattern: AnyStr, - should_match: int = 1, - fn: Any = fnmatch) -> None: # see #270 - if should_match: - self.assertTrue(fn(filename, pattern), - "expected %r to match pattern %r" - % (filename, pattern)) - else: - self.assertTrue(not fn(filename, pattern), - "expected %r not to match pattern %r" - % (filename, pattern)) - - def test_fnmatch(self) -> None: - check = self.check_match - check('abc', 'abc') - check('abc', '?*?') - check('abc', '???*') - check('abc', '*???') - check('abc', '???') - check('abc', '*') - check('abc', 'ab[cd]') - check('abc', 'ab[!de]') - check('abc', 'ab[de]', 0) - check('a', '??', 0) - check('a', 'b', 0) - - # these test that '\' is handled correctly in character sets; - # see SF bug #409651 - check('\\', r'[\]') - check('a', r'[!\]') - check('\\', r'[!\]', 0) - - # test that filenames with newlines in them are handled correctly. - # http://bugs.python.org/issue6665 - check('foo\nbar', 'foo*') - check('foo\nbar\n', 'foo*') - check('\nfoo', 'foo*', False) - check('\n', '*') - - def test_mix_bytes_str(self) -> None: - self.assertRaises(TypeError, fnmatch, 'test', b'*') - self.assertRaises(TypeError, fnmatch, b'test', '*') - self.assertRaises(TypeError, fnmatchcase, 'test', b'*') - self.assertRaises(TypeError, fnmatchcase, b'test', '*') - - def test_fnmatchcase(self) -> None: - check = self.check_match - check('AbC', 'abc', 0, fnmatchcase) - check('abc', 'AbC', 0, fnmatchcase) - - def test_bytes(self) -> None: - self.check_match(b'test', b'te*') - self.check_match(b'test\xff', b'te*\xff') - self.check_match(b'foo\nbar', b'foo*') - -class TranslateTestCase(unittest.TestCase): - - def test_translate(self) -> None: - self.assertEqual(translate('*'), r'.*\Z(?ms)') - self.assertEqual(translate('?'), r'.\Z(?ms)') - self.assertEqual(translate('a?b*'), r'a.b.*\Z(?ms)') - self.assertEqual(translate('[abc]'), r'[abc]\Z(?ms)') - self.assertEqual(translate('[]]'), r'[]]\Z(?ms)') - self.assertEqual(translate('[!x]'), r'[^x]\Z(?ms)') - self.assertEqual(translate('[^x]'), r'[\\^x]\Z(?ms)') - self.assertEqual(translate('[x'), r'\\[x\Z(?ms)') - - -class FilterTestCase(unittest.TestCase): - - def test_filter(self) -> None: - self.assertEqual(filter(['a', 'b'], 'a'), ['a']) - - -def test_main() -> None: - support.run_unittest(FnmatchTestCase, - TranslateTestCase, - FilterTestCase) - - -if __name__ == "__main__": - test_main() diff --git a/test-data/stdlib-samples/3.2/test/test_genericpath.py b/test-data/stdlib-samples/3.2/test/test_genericpath.py deleted file mode 100644 index df0e10701d39..000000000000 --- a/test-data/stdlib-samples/3.2/test/test_genericpath.py +++ /dev/null @@ -1,313 +0,0 @@ -""" -Tests common to genericpath, macpath, ntpath and posixpath -""" - -import unittest -from test import support -import os - -import genericpath -import imp -imp.reload(genericpath) # Make sure we are using the local copy - -import sys -from typing import Any, List - - -def safe_rmdir(dirname: str) -> None: - try: - os.rmdir(dirname) - except OSError: - pass - - -class GenericTest(unittest.TestCase): - # The path module to be tested - pathmodule = genericpath # type: Any - common_attributes = ['commonprefix', 'getsize', 'getatime', 'getctime', - 'getmtime', 'exists', 'isdir', 'isfile'] - attributes = [] # type: List[str] - - def test_no_argument(self) -> None: - for attr in self.common_attributes + self.attributes: - with self.assertRaises(TypeError): - getattr(self.pathmodule, attr)() - self.fail("{}.{}() did not raise a TypeError" - .format(self.pathmodule.__name__, attr)) - - def test_commonprefix(self) -> None: - commonprefix = self.pathmodule.commonprefix - self.assertEqual( - commonprefix([]), - "" - ) - self.assertEqual( - commonprefix(["/home/swenson/spam", "/home/swen/spam"]), - "/home/swen" - ) - self.assertEqual( - commonprefix(["/home/swen/spam", "/home/swen/eggs"]), - "/home/swen/" - ) - self.assertEqual( - commonprefix(["/home/swen/spam", "/home/swen/spam"]), - "/home/swen/spam" - ) - self.assertEqual( - commonprefix(["home:swenson:spam", "home:swen:spam"]), - "home:swen" - ) - self.assertEqual( - commonprefix([":home:swen:spam", ":home:swen:eggs"]), - ":home:swen:" - ) - self.assertEqual( - commonprefix([":home:swen:spam", ":home:swen:spam"]), - ":home:swen:spam" - ) - - self.assertEqual( - commonprefix([b"/home/swenson/spam", b"/home/swen/spam"]), - b"/home/swen" - ) - self.assertEqual( - commonprefix([b"/home/swen/spam", b"/home/swen/eggs"]), - b"/home/swen/" - ) - self.assertEqual( - commonprefix([b"/home/swen/spam", b"/home/swen/spam"]), - b"/home/swen/spam" - ) - self.assertEqual( - commonprefix([b"home:swenson:spam", b"home:swen:spam"]), - b"home:swen" - ) - self.assertEqual( - commonprefix([b":home:swen:spam", b":home:swen:eggs"]), - b":home:swen:" - ) - self.assertEqual( - commonprefix([b":home:swen:spam", b":home:swen:spam"]), - b":home:swen:spam" - ) - - testlist = ['', 'abc', 'Xbcd', 'Xb', 'XY', 'abcd', - 'aXc', 'abd', 'ab', 'aX', 'abcX'] - for s1 in testlist: - for s2 in testlist: - p = commonprefix([s1, s2]) - self.assertTrue(s1.startswith(p)) - self.assertTrue(s2.startswith(p)) - if s1 != s2: - n = len(p) - self.assertNotEqual(s1[n:n+1], s2[n:n+1]) - - def test_getsize(self) -> None: - f = open(support.TESTFN, "wb") - try: - f.write(b"foo") - f.close() - self.assertEqual(self.pathmodule.getsize(support.TESTFN), 3) - finally: - if not f.closed: - f.close() - support.unlink(support.TESTFN) - - def test_time(self) -> None: - f = open(support.TESTFN, "wb") - try: - f.write(b"foo") - f.close() - f = open(support.TESTFN, "ab") - f.write(b"bar") - f.close() - f = open(support.TESTFN, "rb") - d = f.read() - f.close() - self.assertEqual(d, b"foobar") - - self.assertLessEqual( - self.pathmodule.getctime(support.TESTFN), - self.pathmodule.getmtime(support.TESTFN) - ) - finally: - if not f.closed: - f.close() - support.unlink(support.TESTFN) - - def test_exists(self) -> None: - self.assertIs(self.pathmodule.exists(support.TESTFN), False) - f = open(support.TESTFN, "wb") - try: - f.write(b"foo") - f.close() - self.assertIs(self.pathmodule.exists(support.TESTFN), True) - if not self.pathmodule == genericpath: - self.assertIs(self.pathmodule.lexists(support.TESTFN), - True) - finally: - if not f.closed: - f.close() - support.unlink(support.TESTFN) - - def test_isdir(self) -> None: - self.assertIs(self.pathmodule.isdir(support.TESTFN), False) - f = open(support.TESTFN, "wb") - try: - f.write(b"foo") - f.close() - self.assertIs(self.pathmodule.isdir(support.TESTFN), False) - os.remove(support.TESTFN) - os.mkdir(support.TESTFN) - self.assertIs(self.pathmodule.isdir(support.TESTFN), True) - os.rmdir(support.TESTFN) - finally: - if not f.closed: - f.close() - support.unlink(support.TESTFN) - safe_rmdir(support.TESTFN) - - def test_isfile(self) -> None: - self.assertIs(self.pathmodule.isfile(support.TESTFN), False) - f = open(support.TESTFN, "wb") - try: - f.write(b"foo") - f.close() - self.assertIs(self.pathmodule.isfile(support.TESTFN), True) - os.remove(support.TESTFN) - os.mkdir(support.TESTFN) - self.assertIs(self.pathmodule.isfile(support.TESTFN), False) - os.rmdir(support.TESTFN) - finally: - if not f.closed: - f.close() - support.unlink(support.TESTFN) - safe_rmdir(support.TESTFN) - - -# Following TestCase is not supposed to be run from test_genericpath. -# It is inherited by other test modules (macpath, ntpath, posixpath). - -class CommonTest(GenericTest): - # The path module to be tested - pathmodule = None # type: Any - common_attributes = GenericTest.common_attributes + [ - # Properties - 'curdir', 'pardir', 'extsep', 'sep', - 'pathsep', 'defpath', 'altsep', 'devnull', - # Methods - 'normcase', 'splitdrive', 'expandvars', 'normpath', 'abspath', - 'join', 'split', 'splitext', 'isabs', 'basename', 'dirname', - 'lexists', 'islink', 'ismount', 'expanduser', 'normpath', 'realpath', - ] - - def test_normcase(self) -> None: - normcase = self.pathmodule.normcase - # check that normcase() is idempotent - for p in ["FoO/./BaR", b"FoO/./BaR"]: - p = normcase(p) - self.assertEqual(p, normcase(p)) - - self.assertEqual(normcase(''), '') - self.assertEqual(normcase(b''), b'') - - # check that normcase raises a TypeError for invalid types - for path in (None, True, 0, 2.5, [], bytearray(b''), {'o','o'}): - self.assertRaises(TypeError, normcase, path) - - def test_splitdrive(self) -> None: - # splitdrive for non-NT paths - splitdrive = self.pathmodule.splitdrive - self.assertEqual(splitdrive("/foo/bar"), ("", "/foo/bar")) - self.assertEqual(splitdrive("foo:bar"), ("", "foo:bar")) - self.assertEqual(splitdrive(":foo:bar"), ("", ":foo:bar")) - - self.assertEqual(splitdrive(b"/foo/bar"), (b"", b"/foo/bar")) - self.assertEqual(splitdrive(b"foo:bar"), (b"", b"foo:bar")) - self.assertEqual(splitdrive(b":foo:bar"), (b"", b":foo:bar")) - - def test_expandvars(self) -> None: - if self.pathmodule.__name__ == 'macpath': - self.skipTest('macpath.expandvars is a stub') - expandvars = self.pathmodule.expandvars - with support.EnvironmentVarGuard() as env: - env.clear() - env["foo"] = "bar" - env["{foo"] = "baz1" - env["{foo}"] = "baz2" - self.assertEqual(expandvars("foo"), "foo") - self.assertEqual(expandvars("$foo bar"), "bar bar") - self.assertEqual(expandvars("${foo}bar"), "barbar") - self.assertEqual(expandvars("$[foo]bar"), "$[foo]bar") - self.assertEqual(expandvars("$bar bar"), "$bar bar") - self.assertEqual(expandvars("$?bar"), "$?bar") - self.assertEqual(expandvars("${foo}bar"), "barbar") - self.assertEqual(expandvars("$foo}bar"), "bar}bar") - self.assertEqual(expandvars("${foo"), "${foo") - self.assertEqual(expandvars("${{foo}}"), "baz1}") - self.assertEqual(expandvars("$foo$foo"), "barbar") - self.assertEqual(expandvars("$bar$bar"), "$bar$bar") - - self.assertEqual(expandvars(b"foo"), b"foo") - self.assertEqual(expandvars(b"$foo bar"), b"bar bar") - self.assertEqual(expandvars(b"${foo}bar"), b"barbar") - self.assertEqual(expandvars(b"$[foo]bar"), b"$[foo]bar") - self.assertEqual(expandvars(b"$bar bar"), b"$bar bar") - self.assertEqual(expandvars(b"$?bar"), b"$?bar") - self.assertEqual(expandvars(b"${foo}bar"), b"barbar") - self.assertEqual(expandvars(b"$foo}bar"), b"bar}bar") - self.assertEqual(expandvars(b"${foo"), b"${foo") - self.assertEqual(expandvars(b"${{foo}}"), b"baz1}") - self.assertEqual(expandvars(b"$foo$foo"), b"barbar") - self.assertEqual(expandvars(b"$bar$bar"), b"$bar$bar") - - def test_abspath(self) -> None: - self.assertIn("foo", self.pathmodule.abspath("foo")) - self.assertIn(b"foo", self.pathmodule.abspath(b"foo")) - - # Abspath returns bytes when the arg is bytes - for path in (b'', b'foo', b'f\xf2\xf2', b'/foo', b'C:\\'): - self.assertIsInstance(self.pathmodule.abspath(path), bytes) - - def test_realpath(self) -> None: - self.assertIn("foo", self.pathmodule.realpath("foo")) - self.assertIn(b"foo", self.pathmodule.realpath(b"foo")) - - def test_normpath_issue5827(self) -> None: - # Make sure normpath preserves unicode - for path in ('', '.', '/', '\\', '///foo/.//bar//'): - self.assertIsInstance(self.pathmodule.normpath(path), str) - - def test_abspath_issue3426(self) -> None: - # Check that abspath returns unicode when the arg is unicode - # with both ASCII and non-ASCII cwds. - abspath = self.pathmodule.abspath - for path in ('', 'fuu', 'f\xf9\xf9', '/fuu', 'U:\\'): - self.assertIsInstance(abspath(path), str) - - unicwd = '\xe7w\xf0' - try: - fsencoding = support.TESTFN_ENCODING or "ascii" - unicwd.encode(fsencoding) - except (AttributeError, UnicodeEncodeError): - # FS encoding is probably ASCII - pass - else: - with support.temp_cwd(unicwd): - for path in ('', 'fuu', 'f\xf9\xf9', '/fuu', 'U:\\'): - self.assertIsInstance(abspath(path), str) - - @unittest.skipIf(sys.platform == 'darwin', - "Mac OS X denies the creation of a directory with an invalid utf8 name") - def test_nonascii_abspath(self) -> None: - # Test non-ASCII, non-UTF8 bytes in the path. - with support.temp_cwd(b'\xe7w\xf0'): - self.test_abspath() - - -def test_main() -> None: - support.run_unittest(GenericTest) - - -if __name__=="__main__": - test_main() diff --git a/test-data/stdlib-samples/3.2/test/test_getopt.py b/test-data/stdlib-samples/3.2/test/test_getopt.py deleted file mode 100644 index 33205521ebd2..000000000000 --- a/test-data/stdlib-samples/3.2/test/test_getopt.py +++ /dev/null @@ -1,190 +0,0 @@ -# test_getopt.py -# David Goodger 2000-08-19 - -from test.support import verbose, run_doctest, run_unittest, EnvironmentVarGuard -import unittest - -import getopt - -from typing import cast, Any - -sentinel = object() - -class GetoptTests(unittest.TestCase): - def setUp(self) -> None: - self.env = EnvironmentVarGuard() - if "POSIXLY_CORRECT" in self.env: - del self.env["POSIXLY_CORRECT"] - - def tearDown(self) -> None: - self.env.__exit__() - del self.env - - def assertError(self, *args: Any, **kwargs: Any) -> None: - # JLe: work around mypy bug #229 - cast(Any, self.assertRaises)(getopt.GetoptError, *args, **kwargs) - - def test_short_has_arg(self) -> None: - self.assertTrue(getopt.short_has_arg('a', 'a:')) - self.assertFalse(getopt.short_has_arg('a', 'a')) - self.assertError(getopt.short_has_arg, 'a', 'b') - - def test_long_has_args(self) -> None: - has_arg, option = getopt.long_has_args('abc', ['abc=']) - self.assertTrue(has_arg) - self.assertEqual(option, 'abc') - - has_arg, option = getopt.long_has_args('abc', ['abc']) - self.assertFalse(has_arg) - self.assertEqual(option, 'abc') - - has_arg, option = getopt.long_has_args('abc', ['abcd']) - self.assertFalse(has_arg) - self.assertEqual(option, 'abcd') - - self.assertError(getopt.long_has_args, 'abc', ['def']) - self.assertError(getopt.long_has_args, 'abc', []) - self.assertError(getopt.long_has_args, 'abc', ['abcd','abcde']) - - def test_do_shorts(self) -> None: - opts, args = getopt.do_shorts([], 'a', 'a', []) - self.assertEqual(opts, [('-a', '')]) - self.assertEqual(args, []) - - opts, args = getopt.do_shorts([], 'a1', 'a:', []) - self.assertEqual(opts, [('-a', '1')]) - self.assertEqual(args, []) - - #opts, args = getopt.do_shorts([], 'a=1', 'a:', []) - #self.assertEqual(opts, [('-a', '1')]) - #self.assertEqual(args, []) - - opts, args = getopt.do_shorts([], 'a', 'a:', ['1']) - self.assertEqual(opts, [('-a', '1')]) - self.assertEqual(args, []) - - opts, args = getopt.do_shorts([], 'a', 'a:', ['1', '2']) - self.assertEqual(opts, [('-a', '1')]) - self.assertEqual(args, ['2']) - - self.assertError(getopt.do_shorts, [], 'a1', 'a', []) - self.assertError(getopt.do_shorts, [], 'a', 'a:', []) - - def test_do_longs(self) -> None: - opts, args = getopt.do_longs([], 'abc', ['abc'], []) - self.assertEqual(opts, [('--abc', '')]) - self.assertEqual(args, []) - - opts, args = getopt.do_longs([], 'abc=1', ['abc='], []) - self.assertEqual(opts, [('--abc', '1')]) - self.assertEqual(args, []) - - opts, args = getopt.do_longs([], 'abc=1', ['abcd='], []) - self.assertEqual(opts, [('--abcd', '1')]) - self.assertEqual(args, []) - - opts, args = getopt.do_longs([], 'abc', ['ab', 'abc', 'abcd'], []) - self.assertEqual(opts, [('--abc', '')]) - self.assertEqual(args, []) - - # Much like the preceding, except with a non-alpha character ("-") in - # option name that precedes "="; failed in - # http://python.org/sf/126863 - opts, args = getopt.do_longs([], 'foo=42', ['foo-bar', 'foo=',], []) - self.assertEqual(opts, [('--foo', '42')]) - self.assertEqual(args, []) - - self.assertError(getopt.do_longs, [], 'abc=1', ['abc'], []) - self.assertError(getopt.do_longs, [], 'abc', ['abc='], []) - - def test_getopt(self) -> None: - # note: the empty string between '-a' and '--beta' is significant: - # it simulates an empty string option argument ('-a ""') on the - # command line. - cmdline = ['-a', '1', '-b', '--alpha=2', '--beta', '-a', '3', '-a', - '', '--beta', 'arg1', 'arg2'] - - opts, args = getopt.getopt(cmdline, 'a:b', ['alpha=', 'beta']) - self.assertEqual(opts, [('-a', '1'), ('-b', ''), - ('--alpha', '2'), ('--beta', ''), - ('-a', '3'), ('-a', ''), ('--beta', '')]) - # Note ambiguity of ('-b', '') and ('-a', '') above. This must be - # accounted for in the code that calls getopt(). - self.assertEqual(args, ['arg1', 'arg2']) - - self.assertError(getopt.getopt, cmdline, 'a:b', ['alpha', 'beta']) - - def test_gnu_getopt(self) -> None: - # Test handling of GNU style scanning mode. - cmdline = ['-a', 'arg1', '-b', '1', '--alpha', '--beta=2'] - - # GNU style - opts, args = getopt.gnu_getopt(cmdline, 'ab:', ['alpha', 'beta=']) - self.assertEqual(args, ['arg1']) - self.assertEqual(opts, [('-a', ''), ('-b', '1'), - ('--alpha', ''), ('--beta', '2')]) - - # recognize "-" as an argument - opts, args = getopt.gnu_getopt(['-a', '-', '-b', '-'], 'ab:', []) - self.assertEqual(args, ['-']) - self.assertEqual(opts, [('-a', ''), ('-b', '-')]) - - # Posix style via + - opts, args = getopt.gnu_getopt(cmdline, '+ab:', ['alpha', 'beta=']) - self.assertEqual(opts, [('-a', '')]) - self.assertEqual(args, ['arg1', '-b', '1', '--alpha', '--beta=2']) - - # Posix style via POSIXLY_CORRECT - self.env["POSIXLY_CORRECT"] = "1" - opts, args = getopt.gnu_getopt(cmdline, 'ab:', ['alpha', 'beta=']) - self.assertEqual(opts, [('-a', '')]) - self.assertEqual(args, ['arg1', '-b', '1', '--alpha', '--beta=2']) - - def test_libref_examples(self) -> None: - s = """ - Examples from the Library Reference: Doc/lib/libgetopt.tex - - An example using only Unix style options: - - - >>> import getopt - >>> args = '-a -b -cfoo -d bar a1 a2'.split() - >>> args - ['-a', '-b', '-cfoo', '-d', 'bar', 'a1', 'a2'] - >>> optlist, args = getopt.getopt(args, 'abc:d:') - >>> optlist - [('-a', ''), ('-b', ''), ('-c', 'foo'), ('-d', 'bar')] - >>> args - ['a1', 'a2'] - - Using long option names is equally easy: - - - >>> s = '--condition=foo --testing --output-file abc.def -x a1 a2' - >>> args = s.split() - >>> args - ['--condition=foo', '--testing', '--output-file', 'abc.def', '-x', 'a1', 'a2'] - >>> optlist, args = getopt.getopt(args, 'x', [ - ... 'condition=', 'output-file=', 'testing']) - >>> optlist - [('--condition', 'foo'), ('--testing', ''), ('--output-file', 'abc.def'), ('-x', '')] - >>> args - ['a1', 'a2'] - """ - - import types - m = types.ModuleType("libreftest", s) - run_doctest(m, verbose) - - def test_issue4629(self) -> None: - longopts, shortopts = getopt.getopt(['--help='], '', ['help=']) - self.assertEqual(longopts, [('--help', '')]) - longopts, shortopts = getopt.getopt(['--help=x'], '', ['help=']) - self.assertEqual(longopts, [('--help', 'x')]) - self.assertRaises(getopt.GetoptError, getopt.getopt, ['--help='], '', ['help']) - -def test_main() -> None: - run_unittest(GetoptTests) - -if __name__ == "__main__": - test_main() diff --git a/test-data/stdlib-samples/3.2/test/test_glob.py b/test-data/stdlib-samples/3.2/test/test_glob.py deleted file mode 100644 index 08c8932c5759..000000000000 --- a/test-data/stdlib-samples/3.2/test/test_glob.py +++ /dev/null @@ -1,122 +0,0 @@ -import unittest -from test.support import run_unittest, TESTFN, skip_unless_symlink, can_symlink -import glob -import os -import shutil - -from typing import TypeVar, Iterable, List, cast - -T = TypeVar('T') - -class GlobTests(unittest.TestCase): - - tempdir = '' - - # JLe: work around mypy issue #231 - def norm(self, first: str, *parts: str) -> str: - return os.path.normpath(os.path.join(self.tempdir, first, *parts)) - - def mktemp(self, *parts: str) -> None: - filename = self.norm(*parts) - base, file = os.path.split(filename) - if not os.path.exists(base): - os.makedirs(base) - f = open(filename, 'w') - f.close() - - def setUp(self) -> None: - self.tempdir = TESTFN+"_dir" - self.mktemp('a', 'D') - self.mktemp('aab', 'F') - self.mktemp('aaa', 'zzzF') - self.mktemp('ZZZ') - self.mktemp('a', 'bcd', 'EF') - self.mktemp('a', 'bcd', 'efg', 'ha') - if can_symlink(): - os.symlink(self.norm('broken'), self.norm('sym1')) - os.symlink(self.norm('broken'), self.norm('sym2')) - - def tearDown(self) -> None: - shutil.rmtree(self.tempdir) - - def glob(self, *parts: str) -> List[str]: - if len(parts) == 1: - pattern = parts[0] - else: - pattern = os.path.join(*parts) - p = os.path.join(self.tempdir, pattern) - res = glob.glob(p) - self.assertEqual(list(glob.iglob(p)), res) - return res - - def assertSequencesEqual_noorder(self, l1: Iterable[T], - l2: Iterable[T]) -> None: - self.assertEqual(set(l1), set(l2)) - - def test_glob_literal(self) -> None: - eq = self.assertSequencesEqual_noorder - eq(self.glob('a'), [self.norm('a')]) - eq(self.glob('a', 'D'), [self.norm('a', 'D')]) - eq(self.glob('aab'), [self.norm('aab')]) - eq(self.glob('zymurgy'), cast(List[str], [])) # JLe: work around #230 - - # test return types are unicode, but only if os.listdir - # returns unicode filenames - uniset = set([str]) - tmp = os.listdir('.') - if set(type(x) for x in tmp) == uniset: - u1 = glob.glob('*') - u2 = glob.glob('./*') - self.assertEqual(set(type(r) for r in u1), uniset) - self.assertEqual(set(type(r) for r in u2), uniset) - - def test_glob_one_directory(self) -> None: - eq = self.assertSequencesEqual_noorder - eq(self.glob('a*'), map(self.norm, ['a', 'aab', 'aaa'])) - eq(self.glob('*a'), map(self.norm, ['a', 'aaa'])) - eq(self.glob('aa?'), map(self.norm, ['aaa', 'aab'])) - eq(self.glob('aa[ab]'), map(self.norm, ['aaa', 'aab'])) - eq(self.glob('*q'), cast(List[str], [])) # JLe: work around #230 - - def test_glob_nested_directory(self) -> None: - eq = self.assertSequencesEqual_noorder - if os.path.normcase("abCD") == "abCD": - # case-sensitive filesystem - eq(self.glob('a', 'bcd', 'E*'), [self.norm('a', 'bcd', 'EF')]) - else: - # case insensitive filesystem - eq(self.glob('a', 'bcd', 'E*'), [self.norm('a', 'bcd', 'EF'), - self.norm('a', 'bcd', 'efg')]) - eq(self.glob('a', 'bcd', '*g'), [self.norm('a', 'bcd', 'efg')]) - - def test_glob_directory_names(self) -> None: - eq = self.assertSequencesEqual_noorder - eq(self.glob('*', 'D'), [self.norm('a', 'D')]) - eq(self.glob('*', '*a'), cast(List[str], [])) # JLe: work around #230 - eq(self.glob('a', '*', '*', '*a'), - [self.norm('a', 'bcd', 'efg', 'ha')]) - eq(self.glob('?a?', '*F'), map(self.norm, [os.path.join('aaa', 'zzzF'), - os.path.join('aab', 'F')])) - - def test_glob_directory_with_trailing_slash(self) -> None: - # We are verifying that when there is wildcard pattern which - # ends with os.sep doesn't blow up. - res = glob.glob(self.tempdir + '*' + os.sep) - self.assertEqual(len(res), 1) - # either of these results are reasonable - self.assertIn(res[0], [self.tempdir, self.tempdir + os.sep]) - - @skip_unless_symlink - def test_glob_broken_symlinks(self) -> None: - eq = self.assertSequencesEqual_noorder - eq(self.glob('sym*'), [self.norm('sym1'), self.norm('sym2')]) - eq(self.glob('sym1'), [self.norm('sym1')]) - eq(self.glob('sym2'), [self.norm('sym2')]) - - -def test_main() -> None: - run_unittest(GlobTests) - - -if __name__ == "__main__": - test_main() diff --git a/test-data/stdlib-samples/3.2/test/test_posixpath.py b/test-data/stdlib-samples/3.2/test/test_posixpath.py deleted file mode 100644 index de98975ad92e..000000000000 --- a/test-data/stdlib-samples/3.2/test/test_posixpath.py +++ /dev/null @@ -1,531 +0,0 @@ -import unittest -from test import support, test_genericpath - -import posixpath -import genericpath - -import imp -imp.reload(posixpath) # Make sure we are using the local copy -imp.reload(genericpath) - -import os -import sys -from posixpath import realpath, abspath, dirname, basename - -import posix -from typing import cast, Any, TypeVar, Callable - -T = TypeVar('T') - -# An absolute path to a temporary filename for testing. We can't rely on TESTFN -# being an absolute path, so we need this. - -ABSTFN = abspath(support.TESTFN) - -def skip_if_ABSTFN_contains_backslash( - test: Callable[[T], None]) -> Callable[[T], None]: - """ - On Windows, posixpath.abspath still returns paths with backslashes - instead of posix forward slashes. If this is the case, several tests - fail, so skip them. - """ - found_backslash = '\\' in ABSTFN - msg = "ABSTFN is not a posix path - tests fail" - return [test, unittest.skip(msg)(test)][found_backslash] - -def safe_rmdir(dirname: str) -> None: - try: - os.rmdir(dirname) - except OSError: - pass - -class PosixPathTest(unittest.TestCase): - - def setUp(self) -> None: - self.tearDown() - - def tearDown(self) -> None: - for suffix in ["", "1", "2"]: - support.unlink(support.TESTFN + suffix) - safe_rmdir(support.TESTFN + suffix) - - def test_join(self) -> None: - self.assertEqual(posixpath.join("/foo", "bar", "/bar", "baz"), - "/bar/baz") - self.assertEqual(posixpath.join("/foo", "bar", "baz"), "/foo/bar/baz") - self.assertEqual(posixpath.join("/foo/", "bar/", "baz/"), - "/foo/bar/baz/") - - self.assertEqual(posixpath.join(b"/foo", b"bar", b"/bar", b"baz"), - b"/bar/baz") - self.assertEqual(posixpath.join(b"/foo", b"bar", b"baz"), - b"/foo/bar/baz") - self.assertEqual(posixpath.join(b"/foo/", b"bar/", b"baz/"), - b"/foo/bar/baz/") - - self.assertRaises(TypeError, posixpath.join, b"bytes", "str") - self.assertRaises(TypeError, posixpath.join, "str", b"bytes") - - def test_split(self) -> None: - self.assertEqual(posixpath.split("/foo/bar"), ("/foo", "bar")) - self.assertEqual(posixpath.split("/"), ("/", "")) - self.assertEqual(posixpath.split("foo"), ("", "foo")) - self.assertEqual(posixpath.split("////foo"), ("////", "foo")) - self.assertEqual(posixpath.split("//foo//bar"), ("//foo", "bar")) - - self.assertEqual(posixpath.split(b"/foo/bar"), (b"/foo", b"bar")) - self.assertEqual(posixpath.split(b"/"), (b"/", b"")) - self.assertEqual(posixpath.split(b"foo"), (b"", b"foo")) - self.assertEqual(posixpath.split(b"////foo"), (b"////", b"foo")) - self.assertEqual(posixpath.split(b"//foo//bar"), (b"//foo", b"bar")) - - def splitextTest(self, path: str, filename: str, ext: str) -> None: - self.assertEqual(posixpath.splitext(path), (filename, ext)) - self.assertEqual(posixpath.splitext("/" + path), ("/" + filename, ext)) - self.assertEqual(posixpath.splitext("abc/" + path), - ("abc/" + filename, ext)) - self.assertEqual(posixpath.splitext("abc.def/" + path), - ("abc.def/" + filename, ext)) - self.assertEqual(posixpath.splitext("/abc.def/" + path), - ("/abc.def/" + filename, ext)) - self.assertEqual(posixpath.splitext(path + "/"), - (filename + ext + "/", "")) - - pathb = bytes(path, "ASCII") - filenameb = bytes(filename, "ASCII") - extb = bytes(ext, "ASCII") - - self.assertEqual(posixpath.splitext(pathb), (filenameb, extb)) - self.assertEqual(posixpath.splitext(b"/" + pathb), - (b"/" + filenameb, extb)) - self.assertEqual(posixpath.splitext(b"abc/" + pathb), - (b"abc/" + filenameb, extb)) - self.assertEqual(posixpath.splitext(b"abc.def/" + pathb), - (b"abc.def/" + filenameb, extb)) - self.assertEqual(posixpath.splitext(b"/abc.def/" + pathb), - (b"/abc.def/" + filenameb, extb)) - self.assertEqual(posixpath.splitext(pathb + b"/"), - (filenameb + extb + b"/", b"")) - - def test_splitext(self) -> None: - self.splitextTest("foo.bar", "foo", ".bar") - self.splitextTest("foo.boo.bar", "foo.boo", ".bar") - self.splitextTest("foo.boo.biff.bar", "foo.boo.biff", ".bar") - self.splitextTest(".csh.rc", ".csh", ".rc") - self.splitextTest("nodots", "nodots", "") - self.splitextTest(".cshrc", ".cshrc", "") - self.splitextTest("...manydots", "...manydots", "") - self.splitextTest("...manydots.ext", "...manydots", ".ext") - self.splitextTest(".", ".", "") - self.splitextTest("..", "..", "") - self.splitextTest("........", "........", "") - self.splitextTest("", "", "") - - def test_isabs(self) -> None: - self.assertIs(posixpath.isabs(""), False) - self.assertIs(posixpath.isabs("/"), True) - self.assertIs(posixpath.isabs("/foo"), True) - self.assertIs(posixpath.isabs("/foo/bar"), True) - self.assertIs(posixpath.isabs("foo/bar"), False) - - self.assertIs(posixpath.isabs(b""), False) - self.assertIs(posixpath.isabs(b"/"), True) - self.assertIs(posixpath.isabs(b"/foo"), True) - self.assertIs(posixpath.isabs(b"/foo/bar"), True) - self.assertIs(posixpath.isabs(b"foo/bar"), False) - - def test_basename(self) -> None: - self.assertEqual(posixpath.basename("/foo/bar"), "bar") - self.assertEqual(posixpath.basename("/"), "") - self.assertEqual(posixpath.basename("foo"), "foo") - self.assertEqual(posixpath.basename("////foo"), "foo") - self.assertEqual(posixpath.basename("//foo//bar"), "bar") - - self.assertEqual(posixpath.basename(b"/foo/bar"), b"bar") - self.assertEqual(posixpath.basename(b"/"), b"") - self.assertEqual(posixpath.basename(b"foo"), b"foo") - self.assertEqual(posixpath.basename(b"////foo"), b"foo") - self.assertEqual(posixpath.basename(b"//foo//bar"), b"bar") - - def test_dirname(self) -> None: - self.assertEqual(posixpath.dirname("/foo/bar"), "/foo") - self.assertEqual(posixpath.dirname("/"), "/") - self.assertEqual(posixpath.dirname("foo"), "") - self.assertEqual(posixpath.dirname("////foo"), "////") - self.assertEqual(posixpath.dirname("//foo//bar"), "//foo") - - self.assertEqual(posixpath.dirname(b"/foo/bar"), b"/foo") - self.assertEqual(posixpath.dirname(b"/"), b"/") - self.assertEqual(posixpath.dirname(b"foo"), b"") - self.assertEqual(posixpath.dirname(b"////foo"), b"////") - self.assertEqual(posixpath.dirname(b"//foo//bar"), b"//foo") - - def test_islink(self) -> None: - self.assertIs(posixpath.islink(support.TESTFN + "1"), False) - self.assertIs(posixpath.lexists(support.TESTFN + "2"), False) - f = open(support.TESTFN + "1", "wb") - try: - f.write(b"foo") - f.close() - self.assertIs(posixpath.islink(support.TESTFN + "1"), False) - if support.can_symlink(): - os.symlink(support.TESTFN + "1", support.TESTFN + "2") - self.assertIs(posixpath.islink(support.TESTFN + "2"), True) - os.remove(support.TESTFN + "1") - self.assertIs(posixpath.islink(support.TESTFN + "2"), True) - self.assertIs(posixpath.exists(support.TESTFN + "2"), False) - self.assertIs(posixpath.lexists(support.TESTFN + "2"), True) - finally: - if not f.closed: - f.close() - - @staticmethod - def _create_file(filename: str) -> None: - with open(filename, 'wb') as f: - f.write(b'foo') - - def test_samefile(self) -> None: - test_fn = support.TESTFN + "1" - self._create_file(test_fn) - self.assertTrue(posixpath.samefile(test_fn, test_fn)) - self.assertRaises(TypeError, posixpath.samefile) - - @unittest.skipIf( - sys.platform.startswith('win'), - "posixpath.samefile does not work on links in Windows") - @unittest.skipUnless(hasattr(os, "symlink"), - "Missing symlink implementation") - def test_samefile_on_links(self) -> None: - test_fn1 = support.TESTFN + "1" - test_fn2 = support.TESTFN + "2" - self._create_file(test_fn1) - - os.symlink(test_fn1, test_fn2) - self.assertTrue(posixpath.samefile(test_fn1, test_fn2)) - os.remove(test_fn2) - - self._create_file(test_fn2) - self.assertFalse(posixpath.samefile(test_fn1, test_fn2)) - - - def test_samestat(self) -> None: - test_fn = support.TESTFN + "1" - self._create_file(test_fn) - test_fns = [test_fn]*2 - stats = map(os.stat, test_fns) - self.assertTrue(posixpath.samestat(*stats)) - - @unittest.skipIf( - sys.platform.startswith('win'), - "posixpath.samestat does not work on links in Windows") - @unittest.skipUnless(hasattr(os, "symlink"), - "Missing symlink implementation") - def test_samestat_on_links(self) -> None: - test_fn1 = support.TESTFN + "1" - test_fn2 = support.TESTFN + "2" - self._create_file(test_fn1) - test_fns = [test_fn1, test_fn2] - cast(Any, os.symlink)(*test_fns) - stats = map(os.stat, test_fns) - self.assertTrue(posixpath.samestat(*stats)) - os.remove(test_fn2) - - self._create_file(test_fn2) - stats = map(os.stat, test_fns) - self.assertFalse(posixpath.samestat(*stats)) - - self.assertRaises(TypeError, posixpath.samestat) - - def test_ismount(self) -> None: - self.assertIs(posixpath.ismount("/"), True) - self.assertIs(posixpath.ismount(b"/"), True) - - def test_ismount_non_existent(self) -> None: - # Non-existent mountpoint. - self.assertIs(posixpath.ismount(ABSTFN), False) - try: - os.mkdir(ABSTFN) - self.assertIs(posixpath.ismount(ABSTFN), False) - finally: - safe_rmdir(ABSTFN) - - @unittest.skipUnless(support.can_symlink(), - "Test requires symlink support") - def test_ismount_symlinks(self) -> None: - # Symlinks are never mountpoints. - try: - os.symlink("/", ABSTFN) - self.assertIs(posixpath.ismount(ABSTFN), False) - finally: - os.unlink(ABSTFN) - - @unittest.skipIf(posix is None, "Test requires posix module") - def test_ismount_different_device(self) -> None: - # Simulate the path being on a different device from its parent by - # mocking out st_dev. - save_lstat = os.lstat - def fake_lstat(path): - st_ino = 0 - st_dev = 0 - if path == ABSTFN: - st_dev = 1 - st_ino = 1 - return posix.stat_result((0, st_ino, st_dev, 0, 0, 0, 0, 0, 0, 0)) - try: - setattr(os, 'lstat', fake_lstat) # mypy: can't modify os directly - self.assertIs(posixpath.ismount(ABSTFN), True) - finally: - setattr(os, 'lstat', save_lstat) - - def test_expanduser(self) -> None: - self.assertEqual(posixpath.expanduser("foo"), "foo") - self.assertEqual(posixpath.expanduser(b"foo"), b"foo") - try: - import pwd - except ImportError: - pass - else: - self.assertIsInstance(posixpath.expanduser("~/"), str) - self.assertIsInstance(posixpath.expanduser(b"~/"), bytes) - # if home directory == root directory, this test makes no sense - if posixpath.expanduser("~") != '/': - self.assertEqual( - posixpath.expanduser("~") + "/", - posixpath.expanduser("~/") - ) - self.assertEqual( - posixpath.expanduser(b"~") + b"/", - posixpath.expanduser(b"~/") - ) - self.assertIsInstance(posixpath.expanduser("~root/"), str) - self.assertIsInstance(posixpath.expanduser("~foo/"), str) - self.assertIsInstance(posixpath.expanduser(b"~root/"), bytes) - self.assertIsInstance(posixpath.expanduser(b"~foo/"), bytes) - - with support.EnvironmentVarGuard() as env: - env['HOME'] = '/' - self.assertEqual(posixpath.expanduser("~"), "/") - # expanduser should fall back to using the password database - del env['HOME'] - home = pwd.getpwuid(os.getuid()).pw_dir - self.assertEqual(posixpath.expanduser("~"), home) - - def test_normpath(self) -> None: - self.assertEqual(posixpath.normpath(""), ".") - self.assertEqual(posixpath.normpath("/"), "/") - self.assertEqual(posixpath.normpath("//"), "//") - self.assertEqual(posixpath.normpath("///"), "/") - self.assertEqual(posixpath.normpath("///foo/.//bar//"), "/foo/bar") - self.assertEqual(posixpath.normpath("///foo/.//bar//.//..//.//baz"), - "/foo/baz") - self.assertEqual(posixpath.normpath("///..//./foo/.//bar"), "/foo/bar") - - self.assertEqual(posixpath.normpath(b""), b".") - self.assertEqual(posixpath.normpath(b"/"), b"/") - self.assertEqual(posixpath.normpath(b"//"), b"//") - self.assertEqual(posixpath.normpath(b"///"), b"/") - self.assertEqual(posixpath.normpath(b"///foo/.//bar//"), b"/foo/bar") - self.assertEqual(posixpath.normpath(b"///foo/.//bar//.//..//.//baz"), - b"/foo/baz") - self.assertEqual(posixpath.normpath(b"///..//./foo/.//bar"), - b"/foo/bar") - - @unittest.skipUnless(hasattr(os, "symlink"), - "Missing symlink implementation") - @skip_if_ABSTFN_contains_backslash - def test_realpath_basic(self) -> None: - # Basic operation. - try: - os.symlink(ABSTFN+"1", ABSTFN) - self.assertEqual(realpath(ABSTFN), ABSTFN+"1") - finally: - support.unlink(ABSTFN) - - @unittest.skipUnless(hasattr(os, "symlink"), - "Missing symlink implementation") - @skip_if_ABSTFN_contains_backslash - def test_realpath_relative(self) -> None: - try: - os.symlink(posixpath.relpath(ABSTFN+"1"), ABSTFN) - self.assertEqual(realpath(ABSTFN), ABSTFN+"1") - finally: - support.unlink(ABSTFN) - - @unittest.skipUnless(hasattr(os, "symlink"), - "Missing symlink implementation") - @skip_if_ABSTFN_contains_backslash - def test_realpath_symlink_loops(self) -> None: - # Bug #930024, return the path unchanged if we get into an infinite - # symlink loop. - try: - old_path = abspath('.') - os.symlink(ABSTFN, ABSTFN) - self.assertEqual(realpath(ABSTFN), ABSTFN) - - os.symlink(ABSTFN+"1", ABSTFN+"2") - os.symlink(ABSTFN+"2", ABSTFN+"1") - self.assertEqual(realpath(ABSTFN+"1"), ABSTFN+"1") - self.assertEqual(realpath(ABSTFN+"2"), ABSTFN+"2") - - # Test using relative path as well. - os.chdir(dirname(ABSTFN)) - self.assertEqual(realpath(basename(ABSTFN)), ABSTFN) - finally: - os.chdir(old_path) - support.unlink(ABSTFN) - support.unlink(ABSTFN+"1") - support.unlink(ABSTFN+"2") - - @unittest.skipUnless(hasattr(os, "symlink"), - "Missing symlink implementation") - @skip_if_ABSTFN_contains_backslash - def test_realpath_resolve_parents(self) -> None: - # We also need to resolve any symlinks in the parents of a relative - # path passed to realpath. E.g.: current working directory is - # /usr/doc with 'doc' being a symlink to /usr/share/doc. We call - # realpath("a"). This should return /usr/share/doc/a/. - try: - old_path = abspath('.') - os.mkdir(ABSTFN) - os.mkdir(ABSTFN + "/y") - os.symlink(ABSTFN + "/y", ABSTFN + "/k") - - os.chdir(ABSTFN + "/k") - self.assertEqual(realpath("a"), ABSTFN + "/y/a") - finally: - os.chdir(old_path) - support.unlink(ABSTFN + "/k") - safe_rmdir(ABSTFN + "/y") - safe_rmdir(ABSTFN) - - @unittest.skipUnless(hasattr(os, "symlink"), - "Missing symlink implementation") - @skip_if_ABSTFN_contains_backslash - def test_realpath_resolve_before_normalizing(self) -> None: - # Bug #990669: Symbolic links should be resolved before we - # normalize the path. E.g.: if we have directories 'a', 'k' and 'y' - # in the following hierarchy: - # a/k/y - # - # and a symbolic link 'link-y' pointing to 'y' in directory 'a', - # then realpath("link-y/..") should return 'k', not 'a'. - try: - old_path = abspath('.') - os.mkdir(ABSTFN) - os.mkdir(ABSTFN + "/k") - os.mkdir(ABSTFN + "/k/y") - os.symlink(ABSTFN + "/k/y", ABSTFN + "/link-y") - - # Absolute path. - self.assertEqual(realpath(ABSTFN + "/link-y/.."), ABSTFN + "/k") - # Relative path. - os.chdir(dirname(ABSTFN)) - self.assertEqual(realpath(basename(ABSTFN) + "/link-y/.."), - ABSTFN + "/k") - finally: - os.chdir(old_path) - support.unlink(ABSTFN + "/link-y") - safe_rmdir(ABSTFN + "/k/y") - safe_rmdir(ABSTFN + "/k") - safe_rmdir(ABSTFN) - - @unittest.skipUnless(hasattr(os, "symlink"), - "Missing symlink implementation") - @skip_if_ABSTFN_contains_backslash - def test_realpath_resolve_first(self) -> None: - # Bug #1213894: The first component of the path, if not absolute, - # must be resolved too. - - try: - old_path = abspath('.') - os.mkdir(ABSTFN) - os.mkdir(ABSTFN + "/k") - os.symlink(ABSTFN, ABSTFN + "link") - os.chdir(dirname(ABSTFN)) - - base = basename(ABSTFN) - self.assertEqual(realpath(base + "link"), ABSTFN) - self.assertEqual(realpath(base + "link/k"), ABSTFN + "/k") - finally: - os.chdir(old_path) - support.unlink(ABSTFN + "link") - safe_rmdir(ABSTFN + "/k") - safe_rmdir(ABSTFN) - - def test_relpath(self) -> None: - real_getcwd = os.getcwd - # mypy: can't modify os directly - setattr(os, 'getcwd', lambda: r"/home/user/bar") - try: - curdir = os.path.split(os.getcwd())[-1] - self.assertRaises(ValueError, posixpath.relpath, "") - self.assertEqual(posixpath.relpath("a"), "a") - self.assertEqual(posixpath.relpath(posixpath.abspath("a")), "a") - self.assertEqual(posixpath.relpath("a/b"), "a/b") - self.assertEqual(posixpath.relpath("../a/b"), "../a/b") - self.assertEqual(posixpath.relpath("a", "../b"), "../"+curdir+"/a") - self.assertEqual(posixpath.relpath("a/b", "../c"), - "../"+curdir+"/a/b") - self.assertEqual(posixpath.relpath("a", "b/c"), "../../a") - self.assertEqual(posixpath.relpath("a", "a"), ".") - self.assertEqual(posixpath.relpath("/foo/bar/bat", "/x/y/z"), '../../../foo/bar/bat') - self.assertEqual(posixpath.relpath("/foo/bar/bat", "/foo/bar"), 'bat') - self.assertEqual(posixpath.relpath("/foo/bar/bat", "/"), 'foo/bar/bat') - self.assertEqual(posixpath.relpath("/", "/foo/bar/bat"), '../../..') - self.assertEqual(posixpath.relpath("/foo/bar/bat", "/x"), '../foo/bar/bat') - self.assertEqual(posixpath.relpath("/x", "/foo/bar/bat"), '../../../x') - self.assertEqual(posixpath.relpath("/", "/"), '.') - self.assertEqual(posixpath.relpath("/a", "/a"), '.') - self.assertEqual(posixpath.relpath("/a/b", "/a/b"), '.') - finally: - setattr(os, 'getcwd', real_getcwd) - - def test_relpath_bytes(self) -> None: - real_getcwdb = os.getcwdb - # mypy: can't modify os directly - setattr(os, 'getcwdb', lambda: br"/home/user/bar") - try: - curdir = os.path.split(os.getcwdb())[-1] - self.assertRaises(ValueError, posixpath.relpath, b"") - self.assertEqual(posixpath.relpath(b"a"), b"a") - self.assertEqual(posixpath.relpath(posixpath.abspath(b"a")), b"a") - self.assertEqual(posixpath.relpath(b"a/b"), b"a/b") - self.assertEqual(posixpath.relpath(b"../a/b"), b"../a/b") - self.assertEqual(posixpath.relpath(b"a", b"../b"), - b"../"+curdir+b"/a") - self.assertEqual(posixpath.relpath(b"a/b", b"../c"), - b"../"+curdir+b"/a/b") - self.assertEqual(posixpath.relpath(b"a", b"b/c"), b"../../a") - self.assertEqual(posixpath.relpath(b"a", b"a"), b".") - self.assertEqual(posixpath.relpath(b"/foo/bar/bat", b"/x/y/z"), b'../../../foo/bar/bat') - self.assertEqual(posixpath.relpath(b"/foo/bar/bat", b"/foo/bar"), b'bat') - self.assertEqual(posixpath.relpath(b"/foo/bar/bat", b"/"), b'foo/bar/bat') - self.assertEqual(posixpath.relpath(b"/", b"/foo/bar/bat"), b'../../..') - self.assertEqual(posixpath.relpath(b"/foo/bar/bat", b"/x"), b'../foo/bar/bat') - self.assertEqual(posixpath.relpath(b"/x", b"/foo/bar/bat"), b'../../../x') - self.assertEqual(posixpath.relpath(b"/", b"/"), b'.') - self.assertEqual(posixpath.relpath(b"/a", b"/a"), b'.') - self.assertEqual(posixpath.relpath(b"/a/b", b"/a/b"), b'.') - - self.assertRaises(TypeError, posixpath.relpath, b"bytes", "str") - self.assertRaises(TypeError, posixpath.relpath, "str", b"bytes") - finally: - setattr(os, 'getcwdb', real_getcwdb) - - def test_sameopenfile(self) -> None: - fname = support.TESTFN + "1" - with open(fname, "wb") as a, open(fname, "wb") as b: - self.assertTrue(posixpath.sameopenfile(a.fileno(), b.fileno())) - - -class PosixCommonTest(test_genericpath.CommonTest): - pathmodule = posixpath - attributes = ['relpath', 'samefile', 'sameopenfile', 'samestat'] - - -def test_main() -> None: - support.run_unittest(PosixPathTest, PosixCommonTest) - - -if __name__=="__main__": - test_main() diff --git a/test-data/stdlib-samples/3.2/test/test_pprint.py b/test-data/stdlib-samples/3.2/test/test_pprint.py deleted file mode 100644 index cf54ebde6adc..000000000000 --- a/test-data/stdlib-samples/3.2/test/test_pprint.py +++ /dev/null @@ -1,488 +0,0 @@ -import pprint -import test.support -import unittest -import test.test_set -import random -import collections -import itertools - -from typing import List, Any, Dict, Tuple, cast, Callable - -# list, tuple and dict subclasses that do or don't overwrite __repr__ -class list2(list): - pass - -class list3(list): - def __repr__(self) -> str: - return list.__repr__(self) - -class tuple2(tuple): - pass - -class tuple3(tuple): - def __repr__(self) -> str: - return tuple.__repr__(self) - -class dict2(dict): - pass - -class dict3(dict): - def __repr__(self) -> str: - return dict.__repr__(self) - -class Unorderable: - def __repr__(self) -> str: - return str(id(self)) - -class QueryTestCase(unittest.TestCase): - - def setUp(self) -> None: - self.a = list(range(100)) # type: List[Any] - self.b = list(range(200)) # type: List[Any] - self.a[-12] = self.b - - def test_basic(self) -> None: - # Verify .isrecursive() and .isreadable() w/o recursion - pp = pprint.PrettyPrinter() - for safe in (2, 2.0, complex(0.0, 2.0), "abc", [3], (2,2), {3: 3}, "yaddayadda", - self.a, self.b): - # module-level convenience functions - self.assertFalse(pprint.isrecursive(safe), - "expected not isrecursive for %r" % (safe,)) - self.assertTrue(pprint.isreadable(safe), - "expected isreadable for %r" % (safe,)) - # PrettyPrinter methods - self.assertFalse(pp.isrecursive(safe), - "expected not isrecursive for %r" % (safe,)) - self.assertTrue(pp.isreadable(safe), - "expected isreadable for %r" % (safe,)) - - def test_knotted(self) -> None: - # Verify .isrecursive() and .isreadable() w/ recursion - # Tie a knot. - self.b[67] = self.a - # Messy dict. - self.d = {} # type: Dict[int, dict] - self.d[0] = self.d[1] = self.d[2] = self.d - - pp = pprint.PrettyPrinter() - - for icky in self.a, self.b, self.d, (self.d, self.d): - self.assertTrue(pprint.isrecursive(icky), "expected isrecursive") - self.assertFalse(pprint.isreadable(icky), "expected not isreadable") - self.assertTrue(pp.isrecursive(icky), "expected isrecursive") - self.assertFalse(pp.isreadable(icky), "expected not isreadable") - - # Break the cycles. - self.d.clear() - del self.a[:] - del self.b[:] - - for safe in self.a, self.b, self.d, (self.d, self.d): - # module-level convenience functions - self.assertFalse(pprint.isrecursive(safe), - "expected not isrecursive for %r" % (safe,)) - self.assertTrue(pprint.isreadable(safe), - "expected isreadable for %r" % (safe,)) - # PrettyPrinter methods - self.assertFalse(pp.isrecursive(safe), - "expected not isrecursive for %r" % (safe,)) - self.assertTrue(pp.isreadable(safe), - "expected isreadable for %r" % (safe,)) - - def test_unreadable(self) -> None: - # Not recursive but not readable anyway - pp = pprint.PrettyPrinter() - for unreadable in type(3), pprint, pprint.isrecursive: - # module-level convenience functions - self.assertFalse(pprint.isrecursive(unreadable), - "expected not isrecursive for %r" % (unreadable,)) - self.assertFalse(pprint.isreadable(unreadable), - "expected not isreadable for %r" % (unreadable,)) - # PrettyPrinter methods - self.assertFalse(pp.isrecursive(unreadable), - "expected not isrecursive for %r" % (unreadable,)) - self.assertFalse(pp.isreadable(unreadable), - "expected not isreadable for %r" % (unreadable,)) - - def test_same_as_repr(self) -> None: - # Simple objects, small containers and classes that overwrite __repr__ - # For those the result should be the same as repr(). - # Ahem. The docs don't say anything about that -- this appears to - # be testing an implementation quirk. Starting in Python 2.5, it's - # not true for dicts: pprint always sorts dicts by key now; before, - # it sorted a dict display if and only if the display required - # multiple lines. For that reason, dicts with more than one element - # aren't tested here. - for simple in (0, 0, complex(0.0), 0.0, "", b"", - (), tuple2(), tuple3(), - [], list2(), list3(), - {}, dict2(), dict3(), - self.assertTrue, pprint, - -6, -6, complex(-6.,-6.), -1.5, "x", b"x", (3,), [3], {3: 6}, - (1,2), [3,4], {5: 6}, - tuple2((1,2)), tuple3((1,2)), tuple3(range(100)), # type: ignore - [3,4], list2(cast(Any, [3,4])), list3(cast(Any, [3,4])), - list3(cast(Any, range(100))), dict2(cast(Any, {5: 6})), - dict3(cast(Any, {5: 6})), # JLe: work around mypy issue #233 - range(10, -11, -1) - ): - native = repr(simple) - for function in "pformat", "saferepr": - f = getattr(pprint, function) - got = f(simple) - self.assertEqual(native, got, - "expected %s got %s from pprint.%s" % - (native, got, function)) - - def test_basic_line_wrap(self) -> None: - # verify basic line-wrapping operation - o = {'RPM_cal': 0, - 'RPM_cal2': 48059, - 'Speed_cal': 0, - 'controldesk_runtime_us': 0, - 'main_code_runtime_us': 0, - 'read_io_runtime_us': 0, - 'write_io_runtime_us': 43690} - exp = """\ -{'RPM_cal': 0, - 'RPM_cal2': 48059, - 'Speed_cal': 0, - 'controldesk_runtime_us': 0, - 'main_code_runtime_us': 0, - 'read_io_runtime_us': 0, - 'write_io_runtime_us': 43690}""" - # JLe: work around mypy issue #232 - for type in cast(List[Any], [dict, dict2]): - self.assertEqual(pprint.pformat(type(o)), exp) - - o2 = range(100) - exp = '[%s]' % ',\n '.join(map(str, o2)) - for type in cast(List[Any], [list, list2]): - self.assertEqual(pprint.pformat(type(o2)), exp) - - o3 = tuple(range(100)) - exp = '(%s)' % ',\n '.join(map(str, o3)) - for type in cast(List[Any], [tuple, tuple2]): - self.assertEqual(pprint.pformat(type(o3)), exp) - - # indent parameter - o4 = range(100) - exp = '[ %s]' % ',\n '.join(map(str, o4)) - for type in cast(List[Any], [list, list2]): - self.assertEqual(pprint.pformat(type(o4), indent=4), exp) - - def test_nested_indentations(self) -> None: - o1 = list(range(10)) - o2 = {'first':1, 'second':2, 'third':3} - o = [o1, o2] - expected = """\ -[ [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], - { 'first': 1, - 'second': 2, - 'third': 3}]""" - self.assertEqual(pprint.pformat(o, indent=4, width=42), expected) - - def test_sorted_dict(self) -> None: - # Starting in Python 2.5, pprint sorts dict displays by key regardless - # of how small the dictionary may be. - # Before the change, on 32-bit Windows pformat() gave order - # 'a', 'c', 'b' here, so this test failed. - d = {'a': 1, 'b': 1, 'c': 1} - self.assertEqual(pprint.pformat(d), "{'a': 1, 'b': 1, 'c': 1}") - self.assertEqual(pprint.pformat([d, d]), - "[{'a': 1, 'b': 1, 'c': 1}, {'a': 1, 'b': 1, 'c': 1}]") - - # The next one is kind of goofy. The sorted order depends on the - # alphabetic order of type names: "int" < "str" < "tuple". Before - # Python 2.5, this was in the test_same_as_repr() test. It's worth - # keeping around for now because it's one of few tests of pprint - # against a crazy mix of types. - self.assertEqual(pprint.pformat({"xy\tab\n": (3,), 5: [[]], (): {}}), - r"{5: [[]], 'xy\tab\n': (3,), (): {}}") - - def test_ordered_dict(self) -> None: - words = 'the quick brown fox jumped over a lazy dog'.split() - d = collections.OrderedDict(zip(words, itertools.count())) - self.assertEqual(pprint.pformat(d), -"""\ -{'the': 0, - 'quick': 1, - 'brown': 2, - 'fox': 3, - 'jumped': 4, - 'over': 5, - 'a': 6, - 'lazy': 7, - 'dog': 8}""") - def test_subclassing(self) -> None: - o = {'names with spaces': 'should be presented using repr()', - 'others.should.not.be': 'like.this'} - exp = """\ -{'names with spaces': 'should be presented using repr()', - others.should.not.be: like.this}""" - self.assertEqual(DottedPrettyPrinter().pformat(o), exp) - - @test.support.cpython_only - def test_set_reprs(self) -> None: - # This test creates a complex arrangement of frozensets and - # compares the pretty-printed repr against a string hard-coded in - # the test. The hard-coded repr depends on the sort order of - # frozensets. - # - # However, as the docs point out: "Since sets only define - # partial ordering (subset relationships), the output of the - # list.sort() method is undefined for lists of sets." - # - # In a nutshell, the test assumes frozenset({0}) will always - # sort before frozenset({1}), but: - # - # >>> frozenset({0}) < frozenset({1}) - # False - # >>> frozenset({1}) < frozenset({0}) - # False - # - # Consequently, this test is fragile and - # implementation-dependent. Small changes to Python's sort - # algorithm cause the test to fail when it should pass. - - self.assertEqual(pprint.pformat(set()), 'set()') - self.assertEqual(pprint.pformat(set(range(3))), '{0, 1, 2}') - self.assertEqual(pprint.pformat(frozenset()), 'frozenset()') - self.assertEqual(pprint.pformat(frozenset(range(3))), 'frozenset({0, 1, 2})') - cube_repr_tgt = """\ -{frozenset(): frozenset({frozenset({2}), frozenset({0}), frozenset({1})}), - frozenset({0}): frozenset({frozenset(), - frozenset({0, 2}), - frozenset({0, 1})}), - frozenset({1}): frozenset({frozenset(), - frozenset({1, 2}), - frozenset({0, 1})}), - frozenset({2}): frozenset({frozenset(), - frozenset({1, 2}), - frozenset({0, 2})}), - frozenset({1, 2}): frozenset({frozenset({2}), - frozenset({1}), - frozenset({0, 1, 2})}), - frozenset({0, 2}): frozenset({frozenset({2}), - frozenset({0}), - frozenset({0, 1, 2})}), - frozenset({0, 1}): frozenset({frozenset({0}), - frozenset({1}), - frozenset({0, 1, 2})}), - frozenset({0, 1, 2}): frozenset({frozenset({1, 2}), - frozenset({0, 2}), - frozenset({0, 1})})}""" - cube = test.test_set.cube(3) - self.assertEqual(pprint.pformat(cube), cube_repr_tgt) - cubo_repr_tgt = """\ -{frozenset({frozenset({0, 2}), frozenset({0})}): frozenset({frozenset({frozenset({0, - 2}), - frozenset({0, - 1, - 2})}), - frozenset({frozenset({0}), - frozenset({0, - 1})}), - frozenset({frozenset(), - frozenset({0})}), - frozenset({frozenset({2}), - frozenset({0, - 2})})}), - frozenset({frozenset({0, 1}), frozenset({1})}): frozenset({frozenset({frozenset({0, - 1}), - frozenset({0, - 1, - 2})}), - frozenset({frozenset({0}), - frozenset({0, - 1})}), - frozenset({frozenset({1}), - frozenset({1, - 2})}), - frozenset({frozenset(), - frozenset({1})})}), - frozenset({frozenset({1, 2}), frozenset({1})}): frozenset({frozenset({frozenset({1, - 2}), - frozenset({0, - 1, - 2})}), - frozenset({frozenset({2}), - frozenset({1, - 2})}), - frozenset({frozenset(), - frozenset({1})}), - frozenset({frozenset({1}), - frozenset({0, - 1})})}), - frozenset({frozenset({1, 2}), frozenset({2})}): frozenset({frozenset({frozenset({1, - 2}), - frozenset({0, - 1, - 2})}), - frozenset({frozenset({1}), - frozenset({1, - 2})}), - frozenset({frozenset({2}), - frozenset({0, - 2})}), - frozenset({frozenset(), - frozenset({2})})}), - frozenset({frozenset(), frozenset({0})}): frozenset({frozenset({frozenset({0}), - frozenset({0, - 1})}), - frozenset({frozenset({0}), - frozenset({0, - 2})}), - frozenset({frozenset(), - frozenset({1})}), - frozenset({frozenset(), - frozenset({2})})}), - frozenset({frozenset(), frozenset({1})}): frozenset({frozenset({frozenset(), - frozenset({0})}), - frozenset({frozenset({1}), - frozenset({1, - 2})}), - frozenset({frozenset(), - frozenset({2})}), - frozenset({frozenset({1}), - frozenset({0, - 1})})}), - frozenset({frozenset({2}), frozenset()}): frozenset({frozenset({frozenset({2}), - frozenset({1, - 2})}), - frozenset({frozenset(), - frozenset({0})}), - frozenset({frozenset(), - frozenset({1})}), - frozenset({frozenset({2}), - frozenset({0, - 2})})}), - frozenset({frozenset({0, 1, 2}), frozenset({0, 1})}): frozenset({frozenset({frozenset({1, - 2}), - frozenset({0, - 1, - 2})}), - frozenset({frozenset({0, - 2}), - frozenset({0, - 1, - 2})}), - frozenset({frozenset({0}), - frozenset({0, - 1})}), - frozenset({frozenset({1}), - frozenset({0, - 1})})}), - frozenset({frozenset({0}), frozenset({0, 1})}): frozenset({frozenset({frozenset(), - frozenset({0})}), - frozenset({frozenset({0, - 1}), - frozenset({0, - 1, - 2})}), - frozenset({frozenset({0}), - frozenset({0, - 2})}), - frozenset({frozenset({1}), - frozenset({0, - 1})})}), - frozenset({frozenset({2}), frozenset({0, 2})}): frozenset({frozenset({frozenset({0, - 2}), - frozenset({0, - 1, - 2})}), - frozenset({frozenset({2}), - frozenset({1, - 2})}), - frozenset({frozenset({0}), - frozenset({0, - 2})}), - frozenset({frozenset(), - frozenset({2})})}), - frozenset({frozenset({0, 1, 2}), frozenset({0, 2})}): frozenset({frozenset({frozenset({1, - 2}), - frozenset({0, - 1, - 2})}), - frozenset({frozenset({0, - 1}), - frozenset({0, - 1, - 2})}), - frozenset({frozenset({0}), - frozenset({0, - 2})}), - frozenset({frozenset({2}), - frozenset({0, - 2})})}), - frozenset({frozenset({1, 2}), frozenset({0, 1, 2})}): frozenset({frozenset({frozenset({0, - 2}), - frozenset({0, - 1, - 2})}), - frozenset({frozenset({0, - 1}), - frozenset({0, - 1, - 2})}), - frozenset({frozenset({2}), - frozenset({1, - 2})}), - frozenset({frozenset({1}), - frozenset({1, - 2})})})}""" - - cubo = test.test_set.linegraph(cube) - self.assertEqual(pprint.pformat(cubo), cubo_repr_tgt) - - def test_depth(self) -> None: - nested_tuple = (1, (2, (3, (4, (5, 6))))) - nested_dict = {1: {2: {3: {4: {5: {6: 6}}}}}} - nested_list = [1, [2, [3, [4, [5, [6, []]]]]]] - self.assertEqual(pprint.pformat(nested_tuple), repr(nested_tuple)) - self.assertEqual(pprint.pformat(nested_dict), repr(nested_dict)) - self.assertEqual(pprint.pformat(nested_list), repr(nested_list)) - - lv1_tuple = '(1, (...))' - lv1_dict = '{1: {...}}' - lv1_list = '[1, [...]]' - self.assertEqual(pprint.pformat(nested_tuple, depth=1), lv1_tuple) - self.assertEqual(pprint.pformat(nested_dict, depth=1), lv1_dict) - self.assertEqual(pprint.pformat(nested_list, depth=1), lv1_list) - - def test_sort_unorderable_values(self) -> None: - # Issue 3976: sorted pprints fail for unorderable values. - n = 20 - keys = [Unorderable() for i in range(n)] - random.shuffle(keys) - skeys = sorted(keys, key=id) - clean = lambda s: s.replace(' ', '').replace('\n','') # type: Callable[[str], str] - - self.assertEqual(clean(pprint.pformat(set(keys))), - '{' + ','.join(map(repr, skeys)) + '}') - self.assertEqual(clean(pprint.pformat(frozenset(keys))), - 'frozenset({' + ','.join(map(repr, skeys)) + '})') - self.assertEqual(clean(pprint.pformat(dict.fromkeys(keys))), - '{' + ','.join('%r:None' % k for k in skeys) + '}') - -class DottedPrettyPrinter(pprint.PrettyPrinter): - - def format(self, object: object, context: Dict[int, Any], maxlevels: int, - level: int) -> Tuple[str, int, int]: - if isinstance(object, str): - if ' ' in object: - return repr(object), 1, 0 - else: - return object, 0, 0 - else: - return pprint.PrettyPrinter.format( - self, object, context, maxlevels, level) - - -def test_main() -> None: - test.support.run_unittest(QueryTestCase) - - -if __name__ == "__main__": - test_main() diff --git a/test-data/stdlib-samples/3.2/test/test_random.py b/test-data/stdlib-samples/3.2/test/test_random.py deleted file mode 100644 index 5989ceeee2bb..000000000000 --- a/test-data/stdlib-samples/3.2/test/test_random.py +++ /dev/null @@ -1,533 +0,0 @@ -#!/usr/bin/env python3 - -import unittest -import random -import time -import pickle -import warnings -from math import log, exp, pi, fsum, sin -from test import support - -from typing import Any, Dict, List, Callable, Generic, TypeVar, cast - -RT = TypeVar('RT', random.Random, random.SystemRandom) - -class TestBasicOps(unittest.TestCase, Generic[RT]): - # Superclass with tests common to all generators. - # Subclasses must arrange for self.gen to retrieve the Random instance - # to be tested. - - gen = None # type: RT # Either Random or SystemRandom - - def randomlist(self, n: int) -> List[float]: - """Helper function to make a list of random numbers""" - return [self.gen.random() for i in range(n)] - - def test_autoseed(self) -> None: - self.gen.seed() - state1 = self.gen.getstate() - time.sleep(0.1) - self.gen.seed() # diffent seeds at different times - state2 = self.gen.getstate() - self.assertNotEqual(state1, state2) - - def test_saverestore(self) -> None: - N = 1000 - self.gen.seed() - state = self.gen.getstate() - randseq = self.randomlist(N) - self.gen.setstate(state) # should regenerate the same sequence - self.assertEqual(randseq, self.randomlist(N)) - - def test_seedargs(self) -> None: - for arg in [None, 0, 0, 1, 1, -1, -1, 10**20, -(10**20), - 3.14, complex(1., 2.), 'a', tuple('abc')]: - self.gen.seed(arg) - for arg in [list(range(3)), {'one': 1}]: - self.assertRaises(TypeError, self.gen.seed, arg) - self.assertRaises(TypeError, self.gen.seed, 1, 2, 3, 4) - self.assertRaises(TypeError, type(self.gen), []) # type: ignore # mypy issue 1846 - - def test_choice(self) -> None: - choice = self.gen.choice - with self.assertRaises(IndexError): - choice([]) - self.assertEqual(choice([50]), 50) - self.assertIn(choice([25, 75]), [25, 75]) - - def test_sample(self) -> None: - # For the entire allowable range of 0 <= k <= N, validate that - # the sample is of the correct length and contains only unique items - N = 100 - population = range(N) - for k in range(N+1): - s = self.gen.sample(population, k) - self.assertEqual(len(s), k) - uniq = set(s) - self.assertEqual(len(uniq), k) - self.assertTrue(uniq <= set(population)) - self.assertEqual(self.gen.sample([], 0), []) # test edge case N==k==0 - - def test_sample_distribution(self) -> None: - # For the entire allowable range of 0 <= k <= N, validate that - # sample generates all possible permutations - n = 5 - pop = range(n) - trials = 10000 # large num prevents false negatives without slowing normal case - def factorial(n: int) -> int: - if n == 0: - return 1 - return n * factorial(n - 1) - for k in range(n): - expected = factorial(n) // factorial(n-k) - perms = {} # type: Dict[tuple, object] - for i in range(trials): - perms[tuple(self.gen.sample(pop, k))] = None - if len(perms) == expected: - break - else: - self.fail() - - def test_sample_inputs(self) -> None: - # SF bug #801342 -- population can be any iterable defining __len__() - self.gen.sample(set(range(20)), 2) - self.gen.sample(range(20), 2) - self.gen.sample(range(20), 2) - self.gen.sample(str('abcdefghijklmnopqrst'), 2) - self.gen.sample(tuple('abcdefghijklmnopqrst'), 2) - - def test_sample_on_dicts(self) -> None: - self.assertRaises(TypeError, self.gen.sample, dict.fromkeys('abcdef'), 2) - - def test_gauss(self) -> None: - # Ensure that the seed() method initializes all the hidden state. In - # particular, through 2.2.1 it failed to reset a piece of state used - # by (and only by) the .gauss() method. - - for seed in 1, 12, 123, 1234, 12345, 123456, 654321: - self.gen.seed(seed) - x1 = self.gen.random() - y1 = self.gen.gauss(0, 1) - - self.gen.seed(seed) - x2 = self.gen.random() - y2 = self.gen.gauss(0, 1) - - self.assertEqual(x1, x2) - self.assertEqual(y1, y2) - - def test_pickling(self) -> None: - state = pickle.dumps(self.gen) - origseq = [self.gen.random() for i in range(10)] - newgen = pickle.loads(state) - restoredseq = [newgen.random() for i in range(10)] - self.assertEqual(origseq, restoredseq) - - def test_bug_1727780(self) -> None: - # verify that version-2-pickles can be loaded - # fine, whether they are created on 32-bit or 64-bit - # platforms, and that version-3-pickles load fine. - files = [("randv2_32.pck", 780), - ("randv2_64.pck", 866), - ("randv3.pck", 343)] - for file, value in files: - f = open(support.findfile(file),"rb") - r = pickle.load(f) - f.close() - self.assertEqual(int(r.random()*1000), value) - - def test_bug_9025(self) -> None: - # Had problem with an uneven distribution in int(n*random()) - # Verify the fix by checking that distributions fall within expectations. - n = 100000 - randrange = self.gen.randrange - k = sum(randrange(6755399441055744) % 3 == 2 for i in range(n)) - self.assertTrue(0.30 < k/n and k/n < .37, (k/n)) - -class SystemRandom_TestBasicOps(TestBasicOps[random.SystemRandom]): - gen = random.SystemRandom() - - def test_autoseed(self) -> None: - # Doesn't need to do anything except not fail - self.gen.seed() - - def test_saverestore(self) -> None: - self.assertRaises(NotImplementedError, self.gen.getstate) - self.assertRaises(NotImplementedError, self.gen.setstate, None) - - def test_seedargs(self) -> None: - # Doesn't need to do anything except not fail - self.gen.seed(100) - - def test_gauss(self) -> None: - self.gen.gauss_next = None - self.gen.seed(100) - self.assertEqual(self.gen.gauss_next, None) - - def test_pickling(self) -> None: - self.assertRaises(NotImplementedError, pickle.dumps, self.gen) - - def test_53_bits_per_float(self) -> None: - # This should pass whenever a C double has 53 bit precision. - span = 2 ** 53 # type: int - cum = 0 - for i in range(100): - cum |= int(self.gen.random() * span) - self.assertEqual(cum, span-1) - - def test_bigrand(self) -> None: - # The randrange routine should build-up the required number of bits - # in stages so that all bit positions are active. - span = 2 ** 500 # type: int - cum = 0 - for i in range(100): - r = self.gen.randrange(span) - self.assertTrue(0 <= r < span) - cum |= r - self.assertEqual(cum, span-1) - - def test_bigrand_ranges(self) -> None: - for i in [40,80, 160, 200, 211, 250, 375, 512, 550]: - start = self.gen.randrange(2 ** i) - stop = self.gen.randrange(2 ** (i-2)) - if stop <= start: - return - self.assertTrue(start <= self.gen.randrange(start, stop) < stop) - - def test_rangelimits(self) -> None: - for start, stop in [(-2,0), (-(2**60)-2,-(2**60)), (2**60,2**60+2)]: - self.assertEqual(set(range(start,stop)), - set([self.gen.randrange(start,stop) for i in range(100)])) - - def test_genrandbits(self) -> None: - # Verify ranges - for k in range(1, 1000): - self.assertTrue(0 <= self.gen.getrandbits(k) < 2**k) - - # Verify all bits active - getbits = self.gen.getrandbits - for span in [1, 2, 3, 4, 31, 32, 32, 52, 53, 54, 119, 127, 128, 129]: - cum = 0 - for i in range(100): - cum |= getbits(span) - self.assertEqual(cum, 2**span-1) - - # Verify argument checking - self.assertRaises(TypeError, self.gen.getrandbits) - self.assertRaises(TypeError, self.gen.getrandbits, 1, 2) - self.assertRaises(ValueError, self.gen.getrandbits, 0) - self.assertRaises(ValueError, self.gen.getrandbits, -1) - self.assertRaises(TypeError, self.gen.getrandbits, 10.1) - - def test_randbelow_logic(self, _log: Callable[[float, float], float] = log, - int: Callable[[float], int] = int) -> None: - # check bitcount transition points: 2**i and 2**(i+1)-1 - # show that: k = int(1.001 + _log(n, 2)) - # is equal to or one greater than the number of bits in n - for i in range(1, 1000): - n = 1 << i # check an exact power of two - numbits = i+1 - k = int(1.00001 + _log(n, 2)) - self.assertEqual(k, numbits) - self.assertEqual(n, 2**(k-1)) - - n += n - 1 # check 1 below the next power of two - k = int(1.00001 + _log(n, 2)) - self.assertIn(k, [numbits, numbits+1]) - self.assertTrue(2**k > n > 2**(k-2)) - - n -= n >> 15 # check a little farther below the next power of two - k = int(1.00001 + _log(n, 2)) - self.assertEqual(k, numbits) # note the stronger assertion - self.assertTrue(2**k > n > 2**(k-1)) # note the stronger assertion - - -class MersenneTwister_TestBasicOps(TestBasicOps[random.Random]): - gen = random.Random() - - def test_guaranteed_stable(self) -> None: - # These sequences are guaranteed to stay the same across versions of python - self.gen.seed(3456147, version=1) - self.assertEqual([self.gen.random().hex() for i in range(4)], - ['0x1.ac362300d90d2p-1', '0x1.9d16f74365005p-1', - '0x1.1ebb4352e4c4dp-1', '0x1.1a7422abf9c11p-1']) - self.gen.seed("the quick brown fox", version=2) - self.assertEqual([self.gen.random().hex() for i in range(4)], - ['0x1.1239ddfb11b7cp-3', '0x1.b3cbb5c51b120p-4', - '0x1.8c4f55116b60fp-1', '0x1.63eb525174a27p-1']) - - def test_setstate_first_arg(self) -> None: - self.assertRaises(ValueError, self.gen.setstate, (1, None, None)) - - def test_setstate_middle_arg(self) -> None: - # Wrong type, s/b tuple - self.assertRaises(TypeError, self.gen.setstate, (2, None, None)) - # Wrong length, s/b 625 - self.assertRaises(ValueError, self.gen.setstate, (2, (1,2,3), None)) - # Wrong type, s/b tuple of 625 ints - self.assertRaises(TypeError, self.gen.setstate, (2, tuple(['a',]*625), None)) - # Last element s/b an int also - self.assertRaises(TypeError, self.gen.setstate, (2, cast(Any, (0,))*624+('a',), None)) - - def test_referenceImplementation(self) -> None: - # Compare the python implementation with results from the original - # code. Create 2000 53-bit precision random floats. Compare only - # the last ten entries to show that the independent implementations - # are tracking. Here is the main() function needed to create the - # list of expected random numbers: - # void main(void){ - # int i; - # unsigned long init[4]={61731, 24903, 614, 42143}, length=4; - # init_by_array(init, length); - # for (i=0; i<2000; i++) { - # printf("%.15f ", genrand_res53()); - # if (i%5==4) printf("\n"); - # } - # } - expected = [0.45839803073713259, - 0.86057815201978782, - 0.92848331726782152, - 0.35932681119782461, - 0.081823493762449573, - 0.14332226470169329, - 0.084297823823520024, - 0.53814864671831453, - 0.089215024911993401, - 0.78486196105372907] - - self.gen.seed(61731 + (24903<<32) + (614<<64) + (42143<<96)) - actual = self.randomlist(2000)[-10:] - for a, e in zip(actual, expected): - self.assertAlmostEqual(a,e,places=14) - - def test_strong_reference_implementation(self) -> None: - # Like test_referenceImplementation, but checks for exact bit-level - # equality. This should pass on any box where C double contains - # at least 53 bits of precision (the underlying algorithm suffers - # no rounding errors -- all results are exact). - from math import ldexp - - expected = [0x0eab3258d2231f, - 0x1b89db315277a5, - 0x1db622a5518016, - 0x0b7f9af0d575bf, - 0x029e4c4db82240, - 0x04961892f5d673, - 0x02b291598e4589, - 0x11388382c15694, - 0x02dad977c9e1fe, - 0x191d96d4d334c6] - self.gen.seed(61731 + (24903<<32) + (614<<64) + (42143<<96)) - actual = self.randomlist(2000)[-10:] - for a, e in zip(actual, expected): - self.assertEqual(int(ldexp(a, 53)), e) - - def test_long_seed(self) -> None: - # This is most interesting to run in debug mode, just to make sure - # nothing blows up. Under the covers, a dynamically resized array - # is allocated, consuming space proportional to the number of bits - # in the seed. Unfortunately, that's a quadratic-time algorithm, - # so don't make this horribly big. - seed = (1 << (10000 * 8)) - 1 # about 10K bytes - self.gen.seed(seed) - - def test_53_bits_per_float(self) -> None: - # This should pass whenever a C double has 53 bit precision. - span = 2 ** 53 # type: int - cum = 0 - for i in range(100): - cum |= int(self.gen.random() * span) - self.assertEqual(cum, span-1) - - def test_bigrand(self) -> None: - # The randrange routine should build-up the required number of bits - # in stages so that all bit positions are active. - span = 2 ** 500 # type: int - cum = 0 - for i in range(100): - r = self.gen.randrange(span) - self.assertTrue(0 <= r < span) - cum |= r - self.assertEqual(cum, span-1) - - def test_bigrand_ranges(self) -> None: - for i in [40,80, 160, 200, 211, 250, 375, 512, 550]: - start = self.gen.randrange(2 ** i) - stop = self.gen.randrange(2 ** (i-2)) - if stop <= start: - return - self.assertTrue(start <= self.gen.randrange(start, stop) < stop) - - def test_rangelimits(self) -> None: - for start, stop in [(-2,0), (-(2**60)-2,-(2**60)), (2**60,2**60+2)]: - self.assertEqual(set(range(start,stop)), - set([self.gen.randrange(start,stop) for i in range(100)])) - - def test_genrandbits(self) -> None: - # Verify cross-platform repeatability - self.gen.seed(1234567) - self.assertEqual(self.gen.getrandbits(100), - 97904845777343510404718956115) - # Verify ranges - for k in range(1, 1000): - self.assertTrue(0 <= self.gen.getrandbits(k) < 2**k) - - # Verify all bits active - getbits = self.gen.getrandbits - for span in [1, 2, 3, 4, 31, 32, 32, 52, 53, 54, 119, 127, 128, 129]: - cum = 0 - for i in range(100): - cum |= getbits(span) - self.assertEqual(cum, 2**span-1) - - # Verify argument checking - self.assertRaises(TypeError, self.gen.getrandbits) - self.assertRaises(TypeError, self.gen.getrandbits, 'a') - self.assertRaises(TypeError, self.gen.getrandbits, 1, 2) - self.assertRaises(ValueError, self.gen.getrandbits, 0) - self.assertRaises(ValueError, self.gen.getrandbits, -1) - - def test_randbelow_logic(self, - _log: Callable[[int, float], float] = log, - int: Callable[[float], int] = int) -> None: - # check bitcount transition points: 2**i and 2**(i+1)-1 - # show that: k = int(1.001 + _log(n, 2)) - # is equal to or one greater than the number of bits in n - for i in range(1, 1000): - n = 1 << i # check an exact power of two - numbits = i+1 - k = int(1.00001 + _log(n, 2)) - self.assertEqual(k, numbits) - self.assertEqual(n, 2**(k-1)) - - n += n - 1 # check 1 below the next power of two - k = int(1.00001 + _log(n, 2)) - self.assertIn(k, [numbits, numbits+1]) - self.assertTrue(2**k > n > 2**(k-2)) - - n -= n >> 15 # check a little farther below the next power of two - k = int(1.00001 + _log(n, 2)) - self.assertEqual(k, numbits) # note the stronger assertion - self.assertTrue(2**k > n > 2**(k-1)) # note the stronger assertion - - def test_randrange_bug_1590891(self) -> None: - start = 1000000000000 - stop = -100000000000000000000 - step = -200 - x = self.gen.randrange(start, stop, step) - self.assertTrue(stop < x <= start) - self.assertEqual((x+stop)%step, 0) - -def gamma(z: float, sqrt2pi: float = (2.0*pi)**0.5) -> float: - # Reflection to right half of complex plane - if z < 0.5: - return pi / sin(pi*z) / gamma(1.0-z) - # Lanczos approximation with g=7 - az = z + (7.0 - 0.5) - return az ** (z-0.5) / exp(az) * sqrt2pi * fsum([ - 0.9999999999995183, - 676.5203681218835 / z, - -1259.139216722289 / (z+1.0), - 771.3234287757674 / (z+2.0), - -176.6150291498386 / (z+3.0), - 12.50734324009056 / (z+4.0), - -0.1385710331296526 / (z+5.0), - 0.9934937113930748e-05 / (z+6.0), - 0.1659470187408462e-06 / (z+7.0), - ]) - -class TestDistributions(unittest.TestCase): - def test_zeroinputs(self) -> None: - # Verify that distributions can handle a series of zero inputs' - g = random.Random() - x = [g.random() for i in range(50)] + [0.0]*5 - def patch() -> None: - setattr(g, 'random', x[:].pop) - patch(); g.uniform(1.0,10.0) - patch(); g.paretovariate(1.0) - patch(); g.expovariate(1.0) - patch(); g.weibullvariate(1.0, 1.0) - patch(); g.normalvariate(0.0, 1.0) - patch(); g.gauss(0.0, 1.0) - patch(); g.lognormvariate(0.0, 1.0) - patch(); g.vonmisesvariate(0.0, 1.0) - patch(); g.gammavariate(0.01, 1.0) - patch(); g.gammavariate(1.0, 1.0) - patch(); g.gammavariate(200.0, 1.0) - patch(); g.betavariate(3.0, 3.0) - patch(); g.triangular(0.0, 1.0, 1.0/3.0) - - def test_avg_std(self) -> None: - # Use integration to test distribution average and standard deviation. - # Only works for distributions which do not consume variates in pairs - g = random.Random() - N = 5000 - x = [i/float(N) for i in range(1,N)] - variate = None # type: Any - for variate, args, mu, sigmasqrd in [ - (g.uniform, (1.0,10.0), (10.0+1.0)/2, (10.0-1.0)**2/12), - (g.triangular, (0.0, 1.0, 1.0/3.0), 4.0/9.0, 7.0/9.0/18.0), - (g.expovariate, (1.5,), 1/1.5, 1/1.5**2), - (g.paretovariate, (5.0,), 5.0/(5.0-1), - 5.0/((5.0-1)**2*(5.0-2))), - (g.weibullvariate, (1.0, 3.0), gamma(1+1/3.0), - gamma(1+2/3.0)-gamma(1+1/3.0)**2) ]: - setattr(g, 'random', x[:].pop) - y = [] # type: List[float] - for i in range(len(x)): - try: - y.append(variate(*args)) - except IndexError: - pass - s1 = s2 = 0.0 - for e in y: - s1 += e - s2 += (e - mu) ** 2 - N = len(y) - self.assertAlmostEqual(s1/N, mu, places=2) - self.assertAlmostEqual(s2/(N-1), sigmasqrd, places=2) - -class TestModule(unittest.TestCase): - def testMagicConstants(self) -> None: - self.assertAlmostEqual(random.NV_MAGICCONST, 1.71552776992141) - self.assertAlmostEqual(random.TWOPI, 6.28318530718) - self.assertAlmostEqual(random.LOG4, 1.38629436111989) - self.assertAlmostEqual(random.SG_MAGICCONST, 2.50407739677627) - - def test__all__(self) -> None: - # tests validity but not completeness of the __all__ list - self.assertTrue(set(random.__all__) <= set(dir(random))) - - def test_random_subclass_with_kwargs(self) -> None: - # SF bug #1486663 -- this used to erroneously raise a TypeError - class Subclass(random.Random): - def __init__(self, newarg: object = None) -> None: - random.Random.__init__(self) - Subclass(newarg=1) - - -def test_main(verbose: bool = None) -> None: - testclasses = [MersenneTwister_TestBasicOps, - TestDistributions, - TestModule] - - try: - random.SystemRandom().random() - except NotImplementedError: - pass - else: - testclasses.append(SystemRandom_TestBasicOps) - - support.run_unittest(*testclasses) - - # verify reference counting - import sys - if verbose and hasattr(sys, "gettotalrefcount"): - counts = [None] * 5 # type: List[int] - for i in range(len(counts)): - support.run_unittest(*testclasses) - counts[i] = sys.gettotalrefcount() - print(counts) - -if __name__ == "__main__": - test_main(verbose=True) diff --git a/test-data/stdlib-samples/3.2/test/test_set.py b/test-data/stdlib-samples/3.2/test/test_set.py deleted file mode 100644 index 16f86198cc0f..000000000000 --- a/test-data/stdlib-samples/3.2/test/test_set.py +++ /dev/null @@ -1,1884 +0,0 @@ -import unittest -from test import support -import gc -import weakref -import operator -import copy -import pickle -from random import randrange, shuffle -import sys -import warnings -import collections -from typing import Set, Any - -class PassThru(Exception): - pass - -def check_pass_thru(): - raise PassThru - yield 1 - -class BadCmp: - def __hash__(self): - return 1 - def __eq__(self, other): - raise RuntimeError - -class ReprWrapper: - 'Used to test self-referential repr() calls' - def __repr__(self): - return repr(self.value) - -#class HashCountingInt(int): -# 'int-like object that counts the number of times __hash__ is called' -# def __init__(self, *args): -# self.hash_count = 0 -# def __hash__(self): -# self.hash_count += 1 -# return int.__hash__(self) - -class TestJointOps(unittest.TestCase): - # Tests common to both set and frozenset - - def setUp(self): - self.word = word = 'simsalabim' - self.otherword = 'madagascar' - self.letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' - self.s = self.thetype(word) - self.d = dict.fromkeys(word) - - def test_new_or_init(self): - self.assertRaises(TypeError, self.thetype, [], 2) - self.assertRaises(TypeError, set().__init__, a=1) - - def test_uniquification(self): - actual = sorted(self.s) - expected = sorted(self.d) - self.assertEqual(actual, expected) - self.assertRaises(PassThru, self.thetype, check_pass_thru()) - self.assertRaises(TypeError, self.thetype, [[]]) - - def test_len(self): - self.assertEqual(len(self.s), len(self.d)) - - def test_contains(self): - for c in self.letters: - self.assertEqual(c in self.s, c in self.d) - self.assertRaises(TypeError, self.s.__contains__, [[]]) - s = self.thetype([frozenset(self.letters)]) - self.assertIn(self.thetype(self.letters), s) - - def test_union(self): - u = self.s.union(self.otherword) - for c in self.letters: - self.assertEqual(c in u, c in self.d or c in self.otherword) - self.assertEqual(self.s, self.thetype(self.word)) - self.assertEqual(type(u), self.basetype) - self.assertRaises(PassThru, self.s.union, check_pass_thru()) - self.assertRaises(TypeError, self.s.union, [[]]) - for C in set, frozenset, dict.fromkeys, str, list, tuple: - self.assertEqual(self.thetype('abcba').union(C('cdc')), set('abcd')) - self.assertEqual(self.thetype('abcba').union(C('efgfe')), set('abcefg')) - self.assertEqual(self.thetype('abcba').union(C('ccb')), set('abc')) - self.assertEqual(self.thetype('abcba').union(C('ef')), set('abcef')) - self.assertEqual(self.thetype('abcba').union(C('ef'), C('fg')), set('abcefg')) - - # Issue #6573 - x = self.thetype() - self.assertEqual(x.union(set([1]), x, set([2])), self.thetype([1, 2])) - - def test_or(self): - i = self.s.union(self.otherword) - self.assertEqual(self.s | set(self.otherword), i) - self.assertEqual(self.s | frozenset(self.otherword), i) - try: - self.s | self.otherword - except TypeError: - pass - else: - self.fail("s|t did not screen-out general iterables") - - def test_intersection(self): - i = self.s.intersection(self.otherword) - for c in self.letters: - self.assertEqual(c in i, c in self.d and c in self.otherword) - self.assertEqual(self.s, self.thetype(self.word)) - self.assertEqual(type(i), self.basetype) - self.assertRaises(PassThru, self.s.intersection, check_pass_thru()) - for C in set, frozenset, dict.fromkeys, str, list, tuple: - self.assertEqual(self.thetype('abcba').intersection(C('cdc')), set('cc')) - self.assertEqual(self.thetype('abcba').intersection(C('efgfe')), set('')) - self.assertEqual(self.thetype('abcba').intersection(C('ccb')), set('bc')) - self.assertEqual(self.thetype('abcba').intersection(C('ef')), set('')) - self.assertEqual(self.thetype('abcba').intersection(C('cbcf'), C('bag')), set('b')) - s = self.thetype('abcba') - z = s.intersection() - if self.thetype == frozenset(): - self.assertEqual(id(s), id(z)) - else: - self.assertNotEqual(id(s), id(z)) - - def test_isdisjoint(self): - def f(s1, s2): - 'Pure python equivalent of isdisjoint()' - return not set(s1).intersection(s2) - for larg in '', 'a', 'ab', 'abc', 'ababac', 'cdc', 'cc', 'efgfe', 'ccb', 'ef': - s1 = self.thetype(larg) - for rarg in '', 'a', 'ab', 'abc', 'ababac', 'cdc', 'cc', 'efgfe', 'ccb', 'ef': - for C in set, frozenset, dict.fromkeys, str, list, tuple: - s2 = C(rarg) - actual = s1.isdisjoint(s2) - expected = f(s1, s2) - self.assertEqual(actual, expected) - self.assertTrue(actual is True or actual is False) - - def test_and(self): - i = self.s.intersection(self.otherword) - self.assertEqual(self.s & set(self.otherword), i) - self.assertEqual(self.s & frozenset(self.otherword), i) - try: - self.s & self.otherword - except TypeError: - pass - else: - self.fail("s&t did not screen-out general iterables") - - def test_difference(self): - i = self.s.difference(self.otherword) - for c in self.letters: - self.assertEqual(c in i, c in self.d and c not in self.otherword) - self.assertEqual(self.s, self.thetype(self.word)) - self.assertEqual(type(i), self.basetype) - self.assertRaises(PassThru, self.s.difference, check_pass_thru()) - self.assertRaises(TypeError, self.s.difference, [[]]) - for C in set, frozenset, dict.fromkeys, str, list, tuple: - self.assertEqual(self.thetype('abcba').difference(C('cdc')), set('ab')) - self.assertEqual(self.thetype('abcba').difference(C('efgfe')), set('abc')) - self.assertEqual(self.thetype('abcba').difference(C('ccb')), set('a')) - self.assertEqual(self.thetype('abcba').difference(C('ef')), set('abc')) - self.assertEqual(self.thetype('abcba').difference(), set('abc')) - self.assertEqual(self.thetype('abcba').difference(C('a'), C('b')), set('c')) - - def test_sub(self): - i = self.s.difference(self.otherword) - self.assertEqual(self.s - set(self.otherword), i) - self.assertEqual(self.s - frozenset(self.otherword), i) - try: - self.s - self.otherword - except TypeError: - pass - else: - self.fail("s-t did not screen-out general iterables") - - def test_symmetric_difference(self): - i = self.s.symmetric_difference(self.otherword) - for c in self.letters: - self.assertEqual(c in i, (c in self.d) ^ (c in self.otherword)) - self.assertEqual(self.s, self.thetype(self.word)) - self.assertEqual(type(i), self.basetype) - self.assertRaises(PassThru, self.s.symmetric_difference, check_pass_thru()) - self.assertRaises(TypeError, self.s.symmetric_difference, [[]]) - for C in set, frozenset, dict.fromkeys, str, list, tuple: - self.assertEqual(self.thetype('abcba').symmetric_difference(C('cdc')), set('abd')) - self.assertEqual(self.thetype('abcba').symmetric_difference(C('efgfe')), set('abcefg')) - self.assertEqual(self.thetype('abcba').symmetric_difference(C('ccb')), set('a')) - self.assertEqual(self.thetype('abcba').symmetric_difference(C('ef')), set('abcef')) - - def test_xor(self): - i = self.s.symmetric_difference(self.otherword) - self.assertEqual(self.s ^ set(self.otherword), i) - self.assertEqual(self.s ^ frozenset(self.otherword), i) - try: - self.s ^ self.otherword - except TypeError: - pass - else: - self.fail("s^t did not screen-out general iterables") - - def test_equality(self): - self.assertEqual(self.s, set(self.word)) - self.assertEqual(self.s, frozenset(self.word)) - self.assertEqual(self.s == self.word, False) - self.assertNotEqual(self.s, set(self.otherword)) - self.assertNotEqual(self.s, frozenset(self.otherword)) - self.assertEqual(self.s != self.word, True) - - def test_setOfFrozensets(self): - t = map(frozenset, ['abcdef', 'bcd', 'bdcb', 'fed', 'fedccba']) - s = self.thetype(t) - self.assertEqual(len(s), 3) - - def test_sub_and_super(self): - p, q, r = map(self.thetype, ['ab', 'abcde', 'def']) - self.assertTrue(p < q) - self.assertTrue(p <= q) - self.assertTrue(q <= q) - self.assertTrue(q > p) - self.assertTrue(q >= p) - self.assertFalse(q < r) - self.assertFalse(q <= r) - self.assertFalse(q > r) - self.assertFalse(q >= r) - self.assertTrue(set('a').issubset('abc')) - self.assertTrue(set('abc').issuperset('a')) - self.assertFalse(set('a').issubset('cbs')) - self.assertFalse(set('cbs').issuperset('a')) - - def test_pickling(self): - for i in range(pickle.HIGHEST_PROTOCOL + 1): - p = pickle.dumps(self.s, i) - dup = pickle.loads(p) - self.assertEqual(self.s, dup, "%s != %s" % (self.s, dup)) - if type(self.s) not in (set, frozenset): - self.s.x = 10 - p = pickle.dumps(self.s) - dup = pickle.loads(p) - self.assertEqual(self.s.x, dup.x) - - def test_deepcopy(self): - class Tracer: - def __init__(self, value): - self.value = value - def __hash__(self): - return self.value - def __deepcopy__(self, memo=None): - return Tracer(self.value + 1) - t = Tracer(10) - s = self.thetype([t]) - dup = copy.deepcopy(s) - self.assertNotEqual(id(s), id(dup)) - for elem in dup: - newt = elem - self.assertNotEqual(id(t), id(newt)) - self.assertEqual(t.value + 1, newt.value) - - def test_gc(self): - # Create a nest of cycles to exercise overall ref count check - class A: - pass - s = set(A() for i in range(1000)) - for elem in s: - elem.cycle = s - elem.sub = elem - elem.set = set([elem]) - - def test_subclass_with_custom_hash(self): - raise NotImplementedError() # runtime computed base class below - # Bug #1257731 - class H: # (self.thetype): - def __hash__(self): - return int(id(self) & 0x7fffffff) - s=H() - f=set() - f.add(s) - self.assertIn(s, f) - f.remove(s) - f.add(s) - f.discard(s) - - def test_badcmp(self): - s = self.thetype([BadCmp()]) - # Detect comparison errors during insertion and lookup - self.assertRaises(RuntimeError, self.thetype, [BadCmp(), BadCmp()]) - self.assertRaises(RuntimeError, s.__contains__, BadCmp()) - # Detect errors during mutating operations - if hasattr(s, 'add'): - self.assertRaises(RuntimeError, s.add, BadCmp()) - self.assertRaises(RuntimeError, s.discard, BadCmp()) - self.assertRaises(RuntimeError, s.remove, BadCmp()) - - def test_cyclical_repr(self): - w = ReprWrapper() - s = self.thetype([w]) - w.value = s - if self.thetype == set: - self.assertEqual(repr(s), '{set(...)}') - else: - name = repr(s).partition('(')[0] # strip class name - self.assertEqual(repr(s), '%s({%s(...)})' % (name, name)) - - def test_cyclical_print(self): - w = ReprWrapper() - s = self.thetype([w]) - w.value = s - fo = open(support.TESTFN, "w") - try: - fo.write(str(s)) - fo.close() - fo = open(support.TESTFN, "r") - self.assertEqual(fo.read(), repr(s)) - finally: - fo.close() - support.unlink(support.TESTFN) - - def test_do_not_rehash_dict_keys(self): - raise NotImplementedError() # cannot subclass int - n = 10 - d = None # dict.fromkeys(map(HashCountingInt, range(n))) - self.assertEqual(sum(elem.hash_count for elem in d), n) - s = self.thetype(d) - self.assertEqual(sum(elem.hash_count for elem in d), n) - s.difference(d) - self.assertEqual(sum(elem.hash_count for elem in d), n) - if hasattr(s, 'symmetric_difference_update'): - s.symmetric_difference_update(d) - self.assertEqual(sum(elem.hash_count for elem in d), n) - d2 = dict.fromkeys(set(d)) - self.assertEqual(sum(elem.hash_count for elem in d), n) - d3 = dict.fromkeys(frozenset(d)) - self.assertEqual(sum(elem.hash_count for elem in d), n) - d3 = dict.fromkeys(frozenset(d), 123) - self.assertEqual(sum(elem.hash_count for elem in d), n) - self.assertEqual(d3, dict.fromkeys(d, 123)) - - def test_container_iterator(self): - # Bug #3680: tp_traverse was not implemented for set iterator object - class C(object): - pass - obj = C() - ref = weakref.ref(obj) - container = set([obj, 1]) - obj.x = iter(container) - obj = None - container = None - gc.collect() - self.assertTrue(ref() is None, "Cycle was not collected") - -class TestSet(TestJointOps): - thetype = set - basetype = set - - def test_init(self): - s = self.thetype() - s.__init__(self.word) - self.assertEqual(s, set(self.word)) - s.__init__(self.otherword) - self.assertEqual(s, set(self.otherword)) - self.assertRaises(TypeError, s.__init__, s, 2); - self.assertRaises(TypeError, s.__init__, 1) - - def test_constructor_identity(self): - s = self.thetype(range(3)) - t = self.thetype(s) - self.assertNotEqual(id(s), id(t)) - - def test_set_literal(self): - raise NotImplementedError() - #s = set([1,2,3]) - #t = {1,2,3} - #self.assertEqual(s, t) - - def test_hash(self): - self.assertRaises(TypeError, hash, self.s) - - def test_clear(self): - self.s.clear() - self.assertEqual(self.s, set()) - self.assertEqual(len(self.s), 0) - - def test_copy(self): - dup = self.s.copy() - self.assertEqual(self.s, dup) - self.assertNotEqual(id(self.s), id(dup)) - self.assertEqual(type(dup), self.basetype) - - def test_add(self): - self.s.add('Q') - self.assertIn('Q', self.s) - dup = self.s.copy() - self.s.add('Q') - self.assertEqual(self.s, dup) - self.assertRaises(TypeError, self.s.add, []) - - def test_remove(self): - self.s.remove('a') - self.assertNotIn('a', self.s) - self.assertRaises(KeyError, self.s.remove, 'Q') - self.assertRaises(TypeError, self.s.remove, []) - s = self.thetype([frozenset(self.word)]) - self.assertIn(self.thetype(self.word), s) - s.remove(self.thetype(self.word)) - self.assertNotIn(self.thetype(self.word), s) - self.assertRaises(KeyError, self.s.remove, self.thetype(self.word)) - - def test_remove_keyerror_unpacking(self): - # bug: www.python.org/sf/1576657 - for v1 in ['Q', (1,)]: - try: - self.s.remove(v1) - except KeyError as e: - v2 = e.args[0] - self.assertEqual(v1, v2) - else: - self.fail() - - def test_remove_keyerror_set(self): - key = self.thetype([3, 4]) - try: - self.s.remove(key) - except KeyError as e: - self.assertTrue(e.args[0] is key, - "KeyError should be {0}, not {1}".format(key, - e.args[0])) - else: - self.fail() - - def test_discard(self): - self.s.discard('a') - self.assertNotIn('a', self.s) - self.s.discard('Q') - self.assertRaises(TypeError, self.s.discard, []) - s = self.thetype([frozenset(self.word)]) - self.assertIn(self.thetype(self.word), s) - s.discard(self.thetype(self.word)) - self.assertNotIn(self.thetype(self.word), s) - s.discard(self.thetype(self.word)) - - def test_pop(self): - for i in range(len(self.s)): - elem = self.s.pop() - self.assertNotIn(elem, self.s) - self.assertRaises(KeyError, self.s.pop) - - def test_update(self): - retval = self.s.update(self.otherword) - self.assertEqual(retval, None) - for c in (self.word + self.otherword): - self.assertIn(c, self.s) - self.assertRaises(PassThru, self.s.update, check_pass_thru()) - self.assertRaises(TypeError, self.s.update, [[]]) - for p, q in (('cdc', 'abcd'), ('efgfe', 'abcefg'), ('ccb', 'abc'), ('ef', 'abcef')): - for C in set, frozenset, dict.fromkeys, str, list, tuple: - s = self.thetype('abcba') - self.assertEqual(s.update(C(p)), None) - self.assertEqual(s, set(q)) - for p in ('cdc', 'efgfe', 'ccb', 'ef', 'abcda'): - q = 'ahi' - for C in set, frozenset, dict.fromkeys, str, list, tuple: - s = self.thetype('abcba') - self.assertEqual(s.update(C(p), C(q)), None) - self.assertEqual(s, set(s) | set(p) | set(q)) - - def test_ior(self): - self.s |= set(self.otherword) - for c in (self.word + self.otherword): - self.assertIn(c, self.s) - - def test_intersection_update(self): - retval = self.s.intersection_update(self.otherword) - self.assertEqual(retval, None) - for c in (self.word + self.otherword): - if c in self.otherword and c in self.word: - self.assertIn(c, self.s) - else: - self.assertNotIn(c, self.s) - self.assertRaises(PassThru, self.s.intersection_update, check_pass_thru()) - self.assertRaises(TypeError, self.s.intersection_update, [[]]) - for p, q in (('cdc', 'c'), ('efgfe', ''), ('ccb', 'bc'), ('ef', '')): - for C in set, frozenset, dict.fromkeys, str, list, tuple: - s = self.thetype('abcba') - self.assertEqual(s.intersection_update(C(p)), None) - self.assertEqual(s, set(q)) - ss = 'abcba' - s = self.thetype(ss) - t = 'cbc' - self.assertEqual(s.intersection_update(C(p), C(t)), None) - self.assertEqual(s, set('abcba')&set(p)&set(t)) - - def test_iand(self): - self.s &= set(self.otherword) - for c in (self.word + self.otherword): - if c in self.otherword and c in self.word: - self.assertIn(c, self.s) - else: - self.assertNotIn(c, self.s) - - def test_difference_update(self): - retval = self.s.difference_update(self.otherword) - self.assertEqual(retval, None) - for c in (self.word + self.otherword): - if c in self.word and c not in self.otherword: - self.assertIn(c, self.s) - else: - self.assertNotIn(c, self.s) - self.assertRaises(PassThru, self.s.difference_update, check_pass_thru()) - self.assertRaises(TypeError, self.s.difference_update, [[]]) - self.assertRaises(TypeError, self.s.symmetric_difference_update, [[]]) - for p, q in (('cdc', 'ab'), ('efgfe', 'abc'), ('ccb', 'a'), ('ef', 'abc')): - for C in set, frozenset, dict.fromkeys, str, list, tuple: - s = self.thetype('abcba') - self.assertEqual(s.difference_update(C(p)), None) - self.assertEqual(s, set(q)) - - s = self.thetype('abcdefghih') - s.difference_update() - self.assertEqual(s, self.thetype('abcdefghih')) - - s = self.thetype('abcdefghih') - s.difference_update(C('aba')) - self.assertEqual(s, self.thetype('cdefghih')) - - s = self.thetype('abcdefghih') - s.difference_update(C('cdc'), C('aba')) - self.assertEqual(s, self.thetype('efghih')) - - def test_isub(self): - self.s -= set(self.otherword) - for c in (self.word + self.otherword): - if c in self.word and c not in self.otherword: - self.assertIn(c, self.s) - else: - self.assertNotIn(c, self.s) - - def test_symmetric_difference_update(self): - retval = self.s.symmetric_difference_update(self.otherword) - self.assertEqual(retval, None) - for c in (self.word + self.otherword): - if (c in self.word) ^ (c in self.otherword): - self.assertIn(c, self.s) - else: - self.assertNotIn(c, self.s) - self.assertRaises(PassThru, self.s.symmetric_difference_update, check_pass_thru()) - self.assertRaises(TypeError, self.s.symmetric_difference_update, [[]]) - for p, q in (('cdc', 'abd'), ('efgfe', 'abcefg'), ('ccb', 'a'), ('ef', 'abcef')): - for C in set, frozenset, dict.fromkeys, str, list, tuple: - s = self.thetype('abcba') - self.assertEqual(s.symmetric_difference_update(C(p)), None) - self.assertEqual(s, set(q)) - - def test_ixor(self): - self.s ^= set(self.otherword) - for c in (self.word + self.otherword): - if (c in self.word) ^ (c in self.otherword): - self.assertIn(c, self.s) - else: - self.assertNotIn(c, self.s) - - def test_inplace_on_self(self): - t = self.s.copy() - t |= t - self.assertEqual(t, self.s) - t &= t - self.assertEqual(t, self.s) - t -= t - self.assertEqual(t, self.thetype()) - t = self.s.copy() - t ^= t - self.assertEqual(t, self.thetype()) - - def test_weakref(self): - s = self.thetype('gallahad') - p = weakref.proxy(s) - self.assertEqual(str(p), str(s)) - s = None - self.assertRaises(ReferenceError, str, p) - - def test_rich_compare(self): - class TestRichSetCompare: - def __gt__(self, some_set): - self.gt_called = True - return False - def __lt__(self, some_set): - self.lt_called = True - return False - def __ge__(self, some_set): - self.ge_called = True - return False - def __le__(self, some_set): - self.le_called = True - return False - - # This first tries the builtin rich set comparison, which doesn't know - # how to handle the custom object. Upon returning NotImplemented, the - # corresponding comparison on the right object is invoked. - myset = {1, 2, 3} - - myobj = TestRichSetCompare() - myset < myobj - self.assertTrue(myobj.gt_called) - - myobj = TestRichSetCompare() - myset > myobj - self.assertTrue(myobj.lt_called) - - myobj = TestRichSetCompare() - myset <= myobj - self.assertTrue(myobj.ge_called) - - myobj = TestRichSetCompare() - myset >= myobj - self.assertTrue(myobj.le_called) - - # C API test only available in a debug build - if hasattr(set, "test_c_api"): - def test_c_api(self): - self.assertEqual(set().test_c_api(), True) - -class SetSubclass(set): - pass - -class TestSetSubclass(TestSet): - thetype = SetSubclass - basetype = set - -class SetSubclassWithKeywordArgs(set): - def __init__(self, iterable=[], newarg=None): - set.__init__(self, iterable) - -class TestSetSubclassWithKeywordArgs(TestSet): - - def test_keywords_in_subclass(self): - 'SF bug #1486663 -- this used to erroneously raise a TypeError' - SetSubclassWithKeywordArgs(newarg=1) - -class TestFrozenSet(TestJointOps): - thetype = frozenset - basetype = frozenset - - def test_init(self): - s = self.thetype(self.word) - s.__init__(self.otherword) - self.assertEqual(s, set(self.word)) - - def test_singleton_empty_frozenset(self): - f = frozenset() - efs = [frozenset(), frozenset([]), frozenset(()), frozenset(''), - frozenset(), frozenset([]), frozenset(()), frozenset(''), - frozenset(range(0)), frozenset(frozenset()), - frozenset(f), f] - # All of the empty frozensets should have just one id() - self.assertEqual(len(set(map(id, efs))), 1) - - def test_constructor_identity(self): - s = self.thetype(range(3)) - t = self.thetype(s) - self.assertEqual(id(s), id(t)) - - def test_hash(self): - self.assertEqual(hash(self.thetype('abcdeb')), - hash(self.thetype('ebecda'))) - - # make sure that all permutations give the same hash value - n = 100 - seq = [randrange(n) for i in range(n)] - results = set() - for i in range(200): - shuffle(seq) - results.add(hash(self.thetype(seq))) - self.assertEqual(len(results), 1) - - def test_copy(self): - dup = self.s.copy() - self.assertEqual(id(self.s), id(dup)) - - def test_frozen_as_dictkey(self): - seq = list(range(10)) + list('abcdefg') + ['apple'] - key1 = self.thetype(seq) - key2 = self.thetype(reversed(seq)) - self.assertEqual(key1, key2) - self.assertNotEqual(id(key1), id(key2)) - d = {} - d[key1] = 42 - self.assertEqual(d[key2], 42) - - def test_hash_caching(self): - f = self.thetype('abcdcda') - self.assertEqual(hash(f), hash(f)) - - def test_hash_effectiveness(self): - n = 13 - hashvalues = set() - addhashvalue = hashvalues.add - elemmasks = [(i+1, 1<=": "issuperset", - } - - reverse = {"==": "==", - "!=": "!=", - "<": ">", - ">": "<", - "<=": ">=", - ">=": "<=", - } - - def test_issubset(self): - raise NotImplementedError() # eval not supported below - x = self.left - y = self.right - for case in "!=", "==", "<", "<=", ">", ">=": - expected = case in self.cases - # Test the binary infix spelling. - result = None ## eval("x" + case + "y", locals()) - self.assertEqual(result, expected) - # Test the "friendly" method-name spelling, if one exists. - if case in TestSubsets.case2method: - method = getattr(x, TestSubsets.case2method[case]) - result = method(y) - self.assertEqual(result, expected) - - # Now do the same for the operands reversed. - rcase = TestSubsets.reverse[case] - result = None ## eval("y" + rcase + "x", locals()) - self.assertEqual(result, expected) - if rcase in TestSubsets.case2method: - method = getattr(y, TestSubsets.case2method[rcase]) - result = method(x) - self.assertEqual(result, expected) -#------------------------------------------------------------------------------ - -class TestSubsetEqualEmpty(TestSubsets): - left = set() # type: Any - right = set() # type: Any - name = "both empty" - cases = "==", "<=", ">=" - -#------------------------------------------------------------------------------ - -class TestSubsetEqualNonEmpty(TestSubsets): - left = set([1, 2]) - right = set([1, 2]) - name = "equal pair" - cases = "==", "<=", ">=" - -#------------------------------------------------------------------------------ - -class TestSubsetEmptyNonEmpty(TestSubsets): - left = set() # type: Any - right = set([1, 2]) - name = "one empty, one non-empty" - cases = "!=", "<", "<=" - -#------------------------------------------------------------------------------ - -class TestSubsetPartial(TestSubsets): - left = set([1]) - right = set([1, 2]) - name = "one a non-empty proper subset of other" - cases = "!=", "<", "<=" - -#------------------------------------------------------------------------------ - -class TestSubsetNonOverlap(TestSubsets): - left = set([1]) - right = set([2]) - name = "neither empty, neither contains" - cases = "!=" - -#============================================================================== - -class TestOnlySetsInBinaryOps(unittest.TestCase): - - def test_eq_ne(self): - # Unlike the others, this is testing that == and != *are* allowed. - self.assertEqual(self.other == self.set, False) - self.assertEqual(self.set == self.other, False) - self.assertEqual(self.other != self.set, True) - self.assertEqual(self.set != self.other, True) - - def test_ge_gt_le_lt(self): - self.assertRaises(TypeError, lambda: self.set < self.other) - self.assertRaises(TypeError, lambda: self.set <= self.other) - self.assertRaises(TypeError, lambda: self.set > self.other) - self.assertRaises(TypeError, lambda: self.set >= self.other) - - self.assertRaises(TypeError, lambda: self.other < self.set) - self.assertRaises(TypeError, lambda: self.other <= self.set) - self.assertRaises(TypeError, lambda: self.other > self.set) - self.assertRaises(TypeError, lambda: self.other >= self.set) - - def test_update_operator(self): - try: - self.set |= self.other - except TypeError: - pass - else: - self.fail("expected TypeError") - - def test_update(self): - if self.otherIsIterable: - self.set.update(self.other) - else: - self.assertRaises(TypeError, self.set.update, self.other) - - def test_union(self): - self.assertRaises(TypeError, lambda: self.set | self.other) - self.assertRaises(TypeError, lambda: self.other | self.set) - if self.otherIsIterable: - self.set.union(self.other) - else: - self.assertRaises(TypeError, self.set.union, self.other) - - def test_intersection_update_operator(self): - try: - self.set &= self.other - except TypeError: - pass - else: - self.fail("expected TypeError") - - def test_intersection_update(self): - if self.otherIsIterable: - self.set.intersection_update(self.other) - else: - self.assertRaises(TypeError, - self.set.intersection_update, - self.other) - - def test_intersection(self): - self.assertRaises(TypeError, lambda: self.set & self.other) - self.assertRaises(TypeError, lambda: self.other & self.set) - if self.otherIsIterable: - self.set.intersection(self.other) - else: - self.assertRaises(TypeError, self.set.intersection, self.other) - - def test_sym_difference_update_operator(self): - try: - self.set ^= self.other - except TypeError: - pass - else: - self.fail("expected TypeError") - - def test_sym_difference_update(self): - if self.otherIsIterable: - self.set.symmetric_difference_update(self.other) - else: - self.assertRaises(TypeError, - self.set.symmetric_difference_update, - self.other) - - def test_sym_difference(self): - self.assertRaises(TypeError, lambda: self.set ^ self.other) - self.assertRaises(TypeError, lambda: self.other ^ self.set) - if self.otherIsIterable: - self.set.symmetric_difference(self.other) - else: - self.assertRaises(TypeError, self.set.symmetric_difference, self.other) - - def test_difference_update_operator(self): - try: - self.set -= self.other - except TypeError: - pass - else: - self.fail("expected TypeError") - - def test_difference_update(self): - if self.otherIsIterable: - self.set.difference_update(self.other) - else: - self.assertRaises(TypeError, - self.set.difference_update, - self.other) - - def test_difference(self): - self.assertRaises(TypeError, lambda: self.set - self.other) - self.assertRaises(TypeError, lambda: self.other - self.set) - if self.otherIsIterable: - self.set.difference(self.other) - else: - self.assertRaises(TypeError, self.set.difference, self.other) - -#------------------------------------------------------------------------------ - -class TestOnlySetsNumeric(TestOnlySetsInBinaryOps): - def setUp(self): - self.set = set((1, 2, 3)) - self.other = 19 - self.otherIsIterable = False - -#------------------------------------------------------------------------------ - -class TestOnlySetsDict(TestOnlySetsInBinaryOps): - def setUp(self): - self.set = set((1, 2, 3)) - self.other = {1:2, 3:4} - self.otherIsIterable = True - -#------------------------------------------------------------------------------ - -class TestOnlySetsOperator(TestOnlySetsInBinaryOps): - def setUp(self): - self.set = set((1, 2, 3)) - self.other = operator.add - self.otherIsIterable = False - -#------------------------------------------------------------------------------ - -class TestOnlySetsTuple(TestOnlySetsInBinaryOps): - def setUp(self): - self.set = set((1, 2, 3)) - self.other = (2, 4, 6) - self.otherIsIterable = True - -#------------------------------------------------------------------------------ - -class TestOnlySetsString(TestOnlySetsInBinaryOps): - def setUp(self): - self.set = set((1, 2, 3)) - self.other = 'abc' - self.otherIsIterable = True - -#------------------------------------------------------------------------------ - -class TestOnlySetsGenerator(TestOnlySetsInBinaryOps): - def setUp(self): - def gen(): - for i in range(0, 10, 2): - yield i - self.set = set((1, 2, 3)) - self.other = gen() - self.otherIsIterable = True - -#============================================================================== - -class TestCopying(unittest.TestCase): - - def test_copy(self): - dup = self.set.copy() - dup_list = sorted(dup, key=repr) - set_list = sorted(self.set, key=repr) - self.assertEqual(len(dup_list), len(set_list)) - for i in range(len(dup_list)): - self.assertTrue(dup_list[i] is set_list[i]) - - def test_deep_copy(self): - dup = copy.deepcopy(self.set) - ##print type(dup), repr(dup) - dup_list = sorted(dup, key=repr) - set_list = sorted(self.set, key=repr) - self.assertEqual(len(dup_list), len(set_list)) - for i in range(len(dup_list)): - self.assertEqual(dup_list[i], set_list[i]) - -#------------------------------------------------------------------------------ - -class TestCopyingEmpty(TestCopying): - def setUp(self): - self.set = set() - -#------------------------------------------------------------------------------ - -class TestCopyingSingleton(TestCopying): - def setUp(self): - self.set = set(["hello"]) - -#------------------------------------------------------------------------------ - -class TestCopyingTriple(TestCopying): - def setUp(self): - self.set = set(["zero", 0, None]) - -#------------------------------------------------------------------------------ - -class TestCopyingTuple(TestCopying): - def setUp(self): - self.set = set([(1, 2)]) - -#------------------------------------------------------------------------------ - -class TestCopyingNested(TestCopying): - def setUp(self): - self.set = set([((1, 2), (3, 4))]) - -#============================================================================== - -class TestIdentities(unittest.TestCase): - def setUp(self): - self.a = set('abracadabra') - self.b = set('alacazam') - - def test_binopsVsSubsets(self): - a, b = self.a, self.b - self.assertTrue(a - b < a) - self.assertTrue(b - a < b) - self.assertTrue(a & b < a) - self.assertTrue(a & b < b) - self.assertTrue(a | b > a) - self.assertTrue(a | b > b) - self.assertTrue(a ^ b < a | b) - - def test_commutativity(self): - a, b = self.a, self.b - self.assertEqual(a&b, b&a) - self.assertEqual(a|b, b|a) - self.assertEqual(a^b, b^a) - if a != b: - self.assertNotEqual(a-b, b-a) - - def test_summations(self): - # check that sums of parts equal the whole - a, b = self.a, self.b - self.assertEqual((a-b)|(a&b)|(b-a), a|b) - self.assertEqual((a&b)|(a^b), a|b) - self.assertEqual(a|(b-a), a|b) - self.assertEqual((a-b)|b, a|b) - self.assertEqual((a-b)|(a&b), a) - self.assertEqual((b-a)|(a&b), b) - self.assertEqual((a-b)|(b-a), a^b) - - def test_exclusion(self): - # check that inverse operations show non-overlap - a, b, zero = self.a, self.b, set() - self.assertEqual((a-b)&b, zero) - self.assertEqual((b-a)&a, zero) - self.assertEqual((a&b)&(a^b), zero) - -# Tests derived from test_itertools.py ======================================= - -def R(seqn): - 'Regular generator' - for i in seqn: - yield i - -class G: - 'Sequence using __getitem__' - def __init__(self, seqn): - self.seqn = seqn - def __getitem__(self, i): - return self.seqn[i] - -class I: - 'Sequence using iterator protocol' - def __init__(self, seqn): - self.seqn = seqn - self.i = 0 - def __iter__(self): - return self - def __next__(self): - if self.i >= len(self.seqn): raise StopIteration - v = self.seqn[self.i] - self.i += 1 - return v - -class Ig: - 'Sequence using iterator protocol defined with a generator' - def __init__(self, seqn): - self.seqn = seqn - self.i = 0 - def __iter__(self): - for val in self.seqn: - yield val - -class X: - 'Missing __getitem__ and __iter__' - def __init__(self, seqn): - self.seqn = seqn - self.i = 0 - def __next__(self): - if self.i >= len(self.seqn): raise StopIteration - v = self.seqn[self.i] - self.i += 1 - return v - -class N: - 'Iterator missing __next__()' - def __init__(self, seqn): - self.seqn = seqn - self.i = 0 - def __iter__(self): - return self - -class E: - 'Test propagation of exceptions' - def __init__(self, seqn): - self.seqn = seqn - self.i = 0 - def __iter__(self): - return self - def __next__(self): - 3 // 0 - -class S: - 'Test immediate stop' - def __init__(self, seqn): - pass - def __iter__(self): - return self - def __next__(self): - raise StopIteration - -from itertools import chain -def L(seqn): - 'Test multiple tiers of iterators' - return chain(map(lambda x:x, R(Ig(G(seqn))))) - -class TestVariousIteratorArgs(unittest.TestCase): - - def test_constructor(self): - for cons in (set, frozenset): - for s in ("123", "", range(1000), ('do', 1.2), range(2000,2200,5)): - for g in (G, I, Ig, S, L, R): - self.assertEqual(sorted(cons(g(s)), key=repr), sorted(g(s), key=repr)) - self.assertRaises(TypeError, cons , X(s)) - self.assertRaises(TypeError, cons , N(s)) - self.assertRaises(ZeroDivisionError, cons , E(s)) - - def test_inline_methods(self): - s = set('november') - for data in ("123", "", range(1000), ('do', 1.2), range(2000,2200,5), 'december'): - for meth in (s.union, s.intersection, s.difference, s.symmetric_difference, s.isdisjoint): - for g in (G, I, Ig, L, R): - expected = meth(data) - actual = meth(G(data)) - if isinstance(expected, bool): - self.assertEqual(actual, expected) - else: - self.assertEqual(sorted(actual, key=repr), sorted(expected, key=repr)) - self.assertRaises(TypeError, meth, X(s)) - self.assertRaises(TypeError, meth, N(s)) - self.assertRaises(ZeroDivisionError, meth, E(s)) - - def test_inplace_methods(self): - for data in ("123", "", range(1000), ('do', 1.2), range(2000,2200,5), 'december'): - for methname in ('update', 'intersection_update', - 'difference_update', 'symmetric_difference_update'): - for g in (G, I, Ig, S, L, R): - s = set('january') - t = s.copy() - getattr(s, methname)(list(g(data))) - getattr(t, methname)(g(data)) - self.assertEqual(sorted(s, key=repr), sorted(t, key=repr)) - - self.assertRaises(TypeError, getattr(set('january'), methname), X(data)) - self.assertRaises(TypeError, getattr(set('january'), methname), N(data)) - self.assertRaises(ZeroDivisionError, getattr(set('january'), methname), E(data)) - -be_bad = set2 = dict2 = None # type: Any - -class bad_eq: - def __eq__(self, other): - if be_bad: - set2.clear() - raise ZeroDivisionError - return self is other - def __hash__(self): - return 0 - -class bad_dict_clear: - def __eq__(self, other): - if be_bad: - dict2.clear() - return self is other - def __hash__(self): - return 0 - -class TestWeirdBugs(unittest.TestCase): - def test_8420_set_merge(self): - # This used to segfault - global be_bad, set2, dict2 - be_bad = False - set1 = {bad_eq()} - set2 = {bad_eq() for i in range(75)} - be_bad = True - self.assertRaises(ZeroDivisionError, set1.update, set2) - - be_bad = False - set1 = {bad_dict_clear()} - dict2 = {bad_dict_clear(): None} - be_bad = True - set1.symmetric_difference_update(dict2) - -# Application tests (based on David Eppstein's graph recipes ==================================== - -def powerset(U): - """Generates all subsets of a set or sequence U.""" - U = iter(U) - try: - x = frozenset([next(U)]) - for S in powerset(U): - yield S - yield S | x - except StopIteration: - yield frozenset() - -def cube(n): - """Graph of n-dimensional hypercube.""" - singletons = [frozenset([x]) for x in range(n)] - return dict([(x, frozenset([x^s for s in singletons])) - for x in powerset(range(n))]) - -def linegraph(G): - """Graph, the vertices of which are edges of G, - with two vertices being adjacent iff the corresponding - edges share a vertex.""" - L = {} - for x in G: - for y in G[x]: - nx = [frozenset([x,z]) for z in G[x] if z != y] - ny = [frozenset([y,z]) for z in G[y] if z != x] - L[frozenset([x,y])] = frozenset(nx+ny) - return L - -def faces(G): - 'Return a set of faces in G. Where a face is a set of vertices on that face' - # currently limited to triangles,squares, and pentagons - f = set() - for v1, edges in G.items(): - for v2 in edges: - for v3 in G[v2]: - if v1 == v3: - continue - if v1 in G[v3]: - f.add(frozenset([v1, v2, v3])) - else: - for v4 in G[v3]: - if v4 == v2: - continue - if v1 in G[v4]: - f.add(frozenset([v1, v2, v3, v4])) - else: - for v5 in G[v4]: - if v5 == v3 or v5 == v2: - continue - if v1 in G[v5]: - f.add(frozenset([v1, v2, v3, v4, v5])) - return f - - -class TestGraphs(unittest.TestCase): - - def test_cube(self): - - g = cube(3) # vert --> {v1, v2, v3} - vertices1 = set(g) - self.assertEqual(len(vertices1), 8) # eight vertices - for edge in g.values(): - self.assertEqual(len(edge), 3) # each vertex connects to three edges - vertices2 = set() - for edges in g.values(): - for v in edges: - vertices2.add(v) - self.assertEqual(vertices1, vertices2) # edge vertices in original set - - cubefaces = faces(g) - self.assertEqual(len(cubefaces), 6) # six faces - for face in cubefaces: - self.assertEqual(len(face), 4) # each face is a square - - def test_cuboctahedron(self): - - # http://en.wikipedia.org/wiki/Cuboctahedron - # 8 triangular faces and 6 square faces - # 12 identical vertices each connecting a triangle and square - - g = cube(3) - cuboctahedron = linegraph(g) # V( --> {V1, V2, V3, V4} - self.assertEqual(len(cuboctahedron), 12)# twelve vertices - - vertices = set(cuboctahedron) - for edges in cuboctahedron.values(): - self.assertEqual(len(edges), 4) # each vertex connects to four other vertices - othervertices = set(edge for edges in cuboctahedron.values() for edge in edges) - self.assertEqual(vertices, othervertices) # edge vertices in original set - - cubofaces = faces(cuboctahedron) - facesizes = collections.defaultdict(int) - for face in cubofaces: - facesizes[len(face)] += 1 - self.assertEqual(facesizes[3], 8) # eight triangular faces - self.assertEqual(facesizes[4], 6) # six square faces - - for vertex in cuboctahedron: - edge = vertex # Cuboctahedron vertices are edges in Cube - self.assertEqual(len(edge), 2) # Two cube vertices define an edge - for cubevert in edge: - self.assertIn(cubevert, g) - - -#============================================================================== - -def test_main(verbose=None): - test_classes = ( - TestSet, - TestSetSubclass, - TestSetSubclassWithKeywordArgs, - TestFrozenSet, - TestFrozenSetSubclass, - TestSetOfSets, - TestExceptionPropagation, - TestBasicOpsEmpty, - TestBasicOpsSingleton, - TestBasicOpsTuple, - TestBasicOpsTriple, - TestBasicOpsString, - TestBasicOpsBytes, - TestBasicOpsMixedStringBytes, - TestBinaryOps, - TestUpdateOps, - TestMutate, - TestSubsetEqualEmpty, - TestSubsetEqualNonEmpty, - TestSubsetEmptyNonEmpty, - TestSubsetPartial, - TestSubsetNonOverlap, - TestOnlySetsNumeric, - TestOnlySetsDict, - TestOnlySetsOperator, - TestOnlySetsTuple, - TestOnlySetsString, - TestOnlySetsGenerator, - TestCopyingEmpty, - TestCopyingSingleton, - TestCopyingTriple, - TestCopyingTuple, - TestCopyingNested, - TestIdentities, - TestVariousIteratorArgs, - TestGraphs, - TestWeirdBugs, - ) - - support.run_unittest(*test_classes) - - # verify reference counting - if verbose and hasattr(sys, "gettotalrefcount"): - import gc - counts = [None] * 5 - for i in range(len(counts)): - support.run_unittest(*test_classes) - gc.collect() - counts[i] = sys.gettotalrefcount() - print(counts) - -if __name__ == "__main__": - test_main(verbose=True) diff --git a/test-data/stdlib-samples/3.2/test/test_shutil.py b/test-data/stdlib-samples/3.2/test/test_shutil.py deleted file mode 100644 index 32e0fd153bcf..000000000000 --- a/test-data/stdlib-samples/3.2/test/test_shutil.py +++ /dev/null @@ -1,978 +0,0 @@ -# Copyright (C) 2003 Python Software Foundation - -import unittest -import shutil -import tempfile -import sys -import stat -import os -import os.path -import functools -from test import support -from test.support import TESTFN -from os.path import splitdrive -from distutils.spawn import find_executable, spawn -from shutil import (_make_tarball, _make_zipfile, make_archive, - register_archive_format, unregister_archive_format, - get_archive_formats, Error, unpack_archive, - register_unpack_format, RegistryError, - unregister_unpack_format, get_unpack_formats) -import tarfile -import warnings - -from test import support -from test.support import check_warnings, captured_stdout - -from typing import ( - Any, Callable, Tuple, List, Sequence, BinaryIO, IO, Union, cast -) -from types import TracebackType - -import bz2 -BZ2_SUPPORTED = True - -TESTFN2 = TESTFN + "2" - -import grp -import pwd -UID_GID_SUPPORT = True - -import zlib - -import zipfile -ZIP_SUPPORT = True - -def _fake_rename(*args: Any, **kwargs: Any) -> None: - # Pretend the destination path is on a different filesystem. - raise OSError() - -def mock_rename(func: Any) -> Any: - @functools.wraps(func) - def wrap(*args: Any, **kwargs: Any) -> Any: - try: - builtin_rename = shutil.rename - shutil.rename = cast(Any, _fake_rename) - return func(*args, **kwargs) - finally: - shutil.rename = cast(Any, builtin_rename) - return wrap - -class TestShutil(unittest.TestCase): - - def setUp(self) -> None: - super().setUp() - self.tempdirs = [] # type: List[str] - - def tearDown(self) -> None: - super().tearDown() - while self.tempdirs: - d = self.tempdirs.pop() - shutil.rmtree(d, os.name in ('nt', 'cygwin')) - - def write_file(self, path: Union[str, List[str], tuple], content: str = 'xxx') -> None: - """Writes a file in the given path. - - - path can be a string or a sequence. - """ - if isinstance(path, list): - path = os.path.join(*path) - elif isinstance(path, tuple): - path = cast(str, os.path.join(*path)) - f = open(path, 'w') - try: - f.write(content) - finally: - f.close() - - def mkdtemp(self) -> str: - """Create a temporary directory that will be cleaned up. - - Returns the path of the directory. - """ - d = tempfile.mkdtemp() - self.tempdirs.append(d) - return d - - def test_rmtree_errors(self) -> None: - # filename is guaranteed not to exist - filename = tempfile.mktemp() - self.assertRaises(OSError, shutil.rmtree, filename) - - # See bug #1071513 for why we don't run this on cygwin - # and bug #1076467 for why we don't run this as root. - if (hasattr(os, 'chmod') and sys.platform[:6] != 'cygwin' - and not (hasattr(os, 'geteuid') and os.geteuid() == 0)): - def test_on_error(self) -> None: - self.errorState = 0 - os.mkdir(TESTFN) - self.childpath = os.path.join(TESTFN, 'a') - f = open(self.childpath, 'w') - f.close() - old_dir_mode = os.stat(TESTFN).st_mode - old_child_mode = os.stat(self.childpath).st_mode - # Make unwritable. - os.chmod(self.childpath, stat.S_IREAD) - os.chmod(TESTFN, stat.S_IREAD) - - shutil.rmtree(TESTFN, onerror=self.check_args_to_onerror) - # Test whether onerror has actually been called. - self.assertEqual(self.errorState, 2, - "Expected call to onerror function did not happen.") - - # Make writable again. - os.chmod(TESTFN, old_dir_mode) - os.chmod(self.childpath, old_child_mode) - - # Clean up. - shutil.rmtree(TESTFN) - - def check_args_to_onerror(self, func: Callable[[str], Any], arg: str, - exc: Tuple[type, BaseException, - TracebackType]) -> None: - # test_rmtree_errors deliberately runs rmtree - # on a directory that is chmod 400, which will fail. - # This function is run when shutil.rmtree fails. - # 99.9% of the time it initially fails to remove - # a file in the directory, so the first time through - # func is os.remove. - # However, some Linux machines running ZFS on - # FUSE experienced a failure earlier in the process - # at os.listdir. The first failure may legally - # be either. - if self.errorState == 0: - if func is os.remove: - self.assertEqual(arg, self.childpath) - else: - self.assertIs(func, os.listdir, - "func must be either os.remove or os.listdir") - self.assertEqual(arg, TESTFN) - self.assertTrue(issubclass(exc[0], OSError)) - self.errorState = 1 - else: - self.assertEqual(func, os.rmdir) - self.assertEqual(arg, TESTFN) - self.assertTrue(issubclass(exc[0], OSError)) - self.errorState = 2 - - def test_rmtree_dont_delete_file(self) -> None: - # When called on a file instead of a directory, don't delete it. - handle, path = tempfile.mkstemp() - os.fdopen(handle).close() - self.assertRaises(OSError, shutil.rmtree, path) - os.remove(path) - - def _write_data(self, path: str, data: str) -> None: - f = open(path, "w") - f.write(data) - f.close() - - def test_copytree_simple(self) -> None: - - def read_data(path: str) -> str: - f = open(path) - data = f.read() - f.close() - return data - - src_dir = tempfile.mkdtemp() - dst_dir = os.path.join(tempfile.mkdtemp(), 'destination') - self._write_data(os.path.join(src_dir, 'test.txt'), '123') - os.mkdir(os.path.join(src_dir, 'test_dir')) - self._write_data(os.path.join(src_dir, 'test_dir', 'test.txt'), '456') - - try: - shutil.copytree(src_dir, dst_dir) - self.assertTrue(os.path.isfile(os.path.join(dst_dir, 'test.txt'))) - self.assertTrue(os.path.isdir(os.path.join(dst_dir, 'test_dir'))) - self.assertTrue(os.path.isfile(os.path.join(dst_dir, 'test_dir', - 'test.txt'))) - actual = read_data(os.path.join(dst_dir, 'test.txt')) - self.assertEqual(actual, '123') - actual = read_data(os.path.join(dst_dir, 'test_dir', 'test.txt')) - self.assertEqual(actual, '456') - finally: - for path in ( - os.path.join(src_dir, 'test.txt'), - os.path.join(dst_dir, 'test.txt'), - os.path.join(src_dir, 'test_dir', 'test.txt'), - os.path.join(dst_dir, 'test_dir', 'test.txt'), - ): - if os.path.exists(path): - os.remove(path) - for path in (src_dir, - os.path.dirname(dst_dir) - ): - if os.path.exists(path): - shutil.rmtree(path) - - def test_copytree_with_exclude(self) -> None: - - def read_data(path: str) -> str: - f = open(path) - data = f.read() - f.close() - return data - - # creating data - join = os.path.join - exists = os.path.exists - src_dir = tempfile.mkdtemp() - try: - dst_dir = join(tempfile.mkdtemp(), 'destination') - self._write_data(join(src_dir, 'test.txt'), '123') - self._write_data(join(src_dir, 'test.tmp'), '123') - os.mkdir(join(src_dir, 'test_dir')) - self._write_data(join(src_dir, 'test_dir', 'test.txt'), '456') - os.mkdir(join(src_dir, 'test_dir2')) - self._write_data(join(src_dir, 'test_dir2', 'test.txt'), '456') - os.mkdir(join(src_dir, 'test_dir2', 'subdir')) - os.mkdir(join(src_dir, 'test_dir2', 'subdir2')) - self._write_data(join(src_dir, 'test_dir2', 'subdir', 'test.txt'), - '456') - self._write_data(join(src_dir, 'test_dir2', 'subdir2', 'test.py'), - '456') - - - # testing glob-like patterns - try: - patterns = shutil.ignore_patterns('*.tmp', 'test_dir2') - shutil.copytree(src_dir, dst_dir, ignore=patterns) - # checking the result: some elements should not be copied - self.assertTrue(exists(join(dst_dir, 'test.txt'))) - self.assertTrue(not exists(join(dst_dir, 'test.tmp'))) - self.assertTrue(not exists(join(dst_dir, 'test_dir2'))) - finally: - if os.path.exists(dst_dir): - shutil.rmtree(dst_dir) - try: - patterns = shutil.ignore_patterns('*.tmp', 'subdir*') - shutil.copytree(src_dir, dst_dir, ignore=patterns) - # checking the result: some elements should not be copied - self.assertTrue(not exists(join(dst_dir, 'test.tmp'))) - self.assertTrue(not exists(join(dst_dir, 'test_dir2', 'subdir2'))) - self.assertTrue(not exists(join(dst_dir, 'test_dir2', 'subdir'))) - finally: - if os.path.exists(dst_dir): - shutil.rmtree(dst_dir) - - # testing callable-style - try: - def _filter(src: str, names: Sequence[str]) -> List[str]: - res = [] # type: List[str] - for name in names: - path = os.path.join(src, name) - - if (os.path.isdir(path) and - path.split()[-1] == 'subdir'): - res.append(name) - elif os.path.splitext(path)[-1] in ('.py'): - res.append(name) - return res - - shutil.copytree(src_dir, dst_dir, ignore=_filter) - - # checking the result: some elements should not be copied - self.assertTrue(not exists(join(dst_dir, 'test_dir2', 'subdir2', - 'test.py'))) - self.assertTrue(not exists(join(dst_dir, 'test_dir2', 'subdir'))) - - finally: - if os.path.exists(dst_dir): - shutil.rmtree(dst_dir) - finally: - shutil.rmtree(src_dir) - shutil.rmtree(os.path.dirname(dst_dir)) - - @unittest.skipUnless(hasattr(os, 'link'), 'requires os.link') - def test_dont_copy_file_onto_link_to_itself(self) -> None: - # Temporarily disable test on Windows. - if os.name == 'nt': - return - # bug 851123. - os.mkdir(TESTFN) - src = os.path.join(TESTFN, 'cheese') - dst = os.path.join(TESTFN, 'shop') - try: - with open(src, 'w') as f: - f.write('cheddar') - os.link(src, dst) - self.assertRaises(shutil.Error, shutil.copyfile, src, dst) - with open(src, 'r') as f: - self.assertEqual(f.read(), 'cheddar') - os.remove(dst) - finally: - shutil.rmtree(TESTFN, ignore_errors=True) - - @support.skip_unless_symlink - def test_dont_copy_file_onto_symlink_to_itself(self) -> None: - # bug 851123. - os.mkdir(TESTFN) - src = os.path.join(TESTFN, 'cheese') - dst = os.path.join(TESTFN, 'shop') - try: - with open(src, 'w') as f: - f.write('cheddar') - # Using `src` here would mean we end up with a symlink pointing - # to TESTFN/TESTFN/cheese, while it should point at - # TESTFN/cheese. - os.symlink('cheese', dst) - self.assertRaises(shutil.Error, shutil.copyfile, src, dst) - with open(src, 'r') as f: - self.assertEqual(f.read(), 'cheddar') - os.remove(dst) - finally: - shutil.rmtree(TESTFN, ignore_errors=True) - - @support.skip_unless_symlink - def test_rmtree_on_symlink(self) -> None: - # bug 1669. - os.mkdir(TESTFN) - try: - src = os.path.join(TESTFN, 'cheese') - dst = os.path.join(TESTFN, 'shop') - os.mkdir(src) - os.symlink(src, dst) - self.assertRaises(OSError, shutil.rmtree, dst) - finally: - shutil.rmtree(TESTFN, ignore_errors=True) - - if hasattr(os, "mkfifo"): - # Issue #3002: copyfile and copytree block indefinitely on named pipes - def test_copyfile_named_pipe(self) -> None: - os.mkfifo(TESTFN) - try: - self.assertRaises(shutil.SpecialFileError, - shutil.copyfile, TESTFN, TESTFN2) - self.assertRaises(shutil.SpecialFileError, - shutil.copyfile, __file__, TESTFN) - finally: - os.remove(TESTFN) - - @support.skip_unless_symlink - def test_copytree_named_pipe(self) -> None: - os.mkdir(TESTFN) - try: - subdir = os.path.join(TESTFN, "subdir") - os.mkdir(subdir) - pipe = os.path.join(subdir, "mypipe") - os.mkfifo(pipe) - try: - shutil.copytree(TESTFN, TESTFN2) - except shutil.Error as e: - errors = e.args[0] - self.assertEqual(len(errors), 1) - src, dst, error_msg = errors[0] - self.assertEqual("`%s` is a named pipe" % pipe, error_msg) - else: - self.fail("shutil.Error should have been raised") - finally: - shutil.rmtree(TESTFN, ignore_errors=True) - shutil.rmtree(TESTFN2, ignore_errors=True) - - def test_copytree_special_func(self) -> None: - - src_dir = self.mkdtemp() - dst_dir = os.path.join(self.mkdtemp(), 'destination') - self._write_data(os.path.join(src_dir, 'test.txt'), '123') - os.mkdir(os.path.join(src_dir, 'test_dir')) - self._write_data(os.path.join(src_dir, 'test_dir', 'test.txt'), '456') - - copied = [] # type: List[Tuple[str, str]] - def _copy(src: str, dst: str) -> None: - copied.append((src, dst)) - - shutil.copytree(src_dir, dst_dir, copy_function=_copy) - self.assertEqual(len(copied), 2) - - @support.skip_unless_symlink - def test_copytree_dangling_symlinks(self) -> None: - - # a dangling symlink raises an error at the end - src_dir = self.mkdtemp() - dst_dir = os.path.join(self.mkdtemp(), 'destination') - os.symlink('IDONTEXIST', os.path.join(src_dir, 'test.txt')) - os.mkdir(os.path.join(src_dir, 'test_dir')) - self._write_data(os.path.join(src_dir, 'test_dir', 'test.txt'), '456') - self.assertRaises(Error, shutil.copytree, src_dir, dst_dir) - - # a dangling symlink is ignored with the proper flag - dst_dir = os.path.join(self.mkdtemp(), 'destination2') - shutil.copytree(src_dir, dst_dir, ignore_dangling_symlinks=True) - self.assertNotIn('test.txt', os.listdir(dst_dir)) - - # a dangling symlink is copied if symlinks=True - dst_dir = os.path.join(self.mkdtemp(), 'destination3') - shutil.copytree(src_dir, dst_dir, symlinks=True) - self.assertIn('test.txt', os.listdir(dst_dir)) - - def _copy_file(self, - method: Callable[[str, str], None]) -> Tuple[str, str]: - fname = 'test.txt' - tmpdir = self.mkdtemp() - self.write_file([tmpdir, fname]) - file1 = os.path.join(tmpdir, fname) - tmpdir2 = self.mkdtemp() - method(file1, tmpdir2) - file2 = os.path.join(tmpdir2, fname) - return (file1, file2) - - @unittest.skipUnless(hasattr(os, 'chmod'), 'requires os.chmod') - def test_copy(self) -> None: - # Ensure that the copied file exists and has the same mode bits. - file1, file2 = self._copy_file(shutil.copy) - self.assertTrue(os.path.exists(file2)) - self.assertEqual(os.stat(file1).st_mode, os.stat(file2).st_mode) - - @unittest.skipUnless(hasattr(os, 'chmod'), 'requires os.chmod') - @unittest.skipUnless(hasattr(os, 'utime'), 'requires os.utime') - def test_copy2(self) -> None: - # Ensure that the copied file exists and has the same mode and - # modification time bits. - file1, file2 = self._copy_file(shutil.copy2) - self.assertTrue(os.path.exists(file2)) - file1_stat = os.stat(file1) - file2_stat = os.stat(file2) - self.assertEqual(file1_stat.st_mode, file2_stat.st_mode) - for attr in 'st_atime', 'st_mtime': - # The modification times may be truncated in the new file. - self.assertLessEqual(getattr(file1_stat, attr), - getattr(file2_stat, attr) + 1) - if hasattr(os, 'chflags') and hasattr(file1_stat, 'st_flags'): - self.assertEqual(getattr(file1_stat, 'st_flags'), - getattr(file2_stat, 'st_flags')) - - @unittest.skipUnless(zlib, "requires zlib") - def test_make_tarball(self) -> None: - # creating something to tar - tmpdir = self.mkdtemp() - self.write_file([tmpdir, 'file1'], 'xxx') - self.write_file([tmpdir, 'file2'], 'xxx') - os.mkdir(os.path.join(tmpdir, 'sub')) - self.write_file([tmpdir, 'sub', 'file3'], 'xxx') - - tmpdir2 = self.mkdtemp() - # force shutil to create the directory - os.rmdir(tmpdir2) - unittest.skipUnless(splitdrive(tmpdir)[0] == splitdrive(tmpdir2)[0], - "source and target should be on same drive") - - base_name = os.path.join(tmpdir2, 'archive') - - # working with relative paths to avoid tar warnings - old_dir = os.getcwd() - os.chdir(tmpdir) - try: - _make_tarball(splitdrive(base_name)[1], '.') - finally: - os.chdir(old_dir) - - # check if the compressed tarball was created - tarball = base_name + '.tar.gz' - self.assertTrue(os.path.exists(tarball)) - - # trying an uncompressed one - base_name = os.path.join(tmpdir2, 'archive') - old_dir = os.getcwd() - os.chdir(tmpdir) - try: - _make_tarball(splitdrive(base_name)[1], '.', compress=None) - finally: - os.chdir(old_dir) - tarball = base_name + '.tar' - self.assertTrue(os.path.exists(tarball)) - - def _tarinfo(self, path: str) -> tuple: - tar = tarfile.open(path) - try: - names = tar.getnames() - names.sort() - return tuple(names) - finally: - tar.close() - - def _create_files(self) -> Tuple[str, str, str]: - # creating something to tar - tmpdir = self.mkdtemp() - dist = os.path.join(tmpdir, 'dist') - os.mkdir(dist) - self.write_file([dist, 'file1'], 'xxx') - self.write_file([dist, 'file2'], 'xxx') - os.mkdir(os.path.join(dist, 'sub')) - self.write_file([dist, 'sub', 'file3'], 'xxx') - os.mkdir(os.path.join(dist, 'sub2')) - tmpdir2 = self.mkdtemp() - base_name = os.path.join(tmpdir2, 'archive') - return tmpdir, tmpdir2, base_name - - @unittest.skipUnless(zlib, "Requires zlib") - @unittest.skipUnless(find_executable('tar') and find_executable('gzip'), - 'Need the tar command to run') - def test_tarfile_vs_tar(self) -> None: - tmpdir, tmpdir2, base_name = self._create_files() - old_dir = os.getcwd() - os.chdir(tmpdir) - try: - _make_tarball(base_name, 'dist') - finally: - os.chdir(old_dir) - - # check if the compressed tarball was created - tarball = base_name + '.tar.gz' - self.assertTrue(os.path.exists(tarball)) - - # now create another tarball using `tar` - tarball2 = os.path.join(tmpdir, 'archive2.tar.gz') - tar_cmd = ['tar', '-cf', 'archive2.tar', 'dist'] - gzip_cmd = ['gzip', '-f9', 'archive2.tar'] - old_dir = os.getcwd() - os.chdir(tmpdir) - try: - with captured_stdout() as s: - spawn(tar_cmd) - spawn(gzip_cmd) - finally: - os.chdir(old_dir) - - self.assertTrue(os.path.exists(tarball2)) - # let's compare both tarballs - self.assertEqual(self._tarinfo(tarball), self._tarinfo(tarball2)) - - # trying an uncompressed one - base_name = os.path.join(tmpdir2, 'archive') - old_dir = os.getcwd() - os.chdir(tmpdir) - try: - _make_tarball(base_name, 'dist', compress=None) - finally: - os.chdir(old_dir) - tarball = base_name + '.tar' - self.assertTrue(os.path.exists(tarball)) - - # now for a dry_run - base_name = os.path.join(tmpdir2, 'archive') - old_dir = os.getcwd() - os.chdir(tmpdir) - try: - _make_tarball(base_name, 'dist', compress=None, dry_run=True) - finally: - os.chdir(old_dir) - tarball = base_name + '.tar' - self.assertTrue(os.path.exists(tarball)) - - @unittest.skipUnless(zlib, "Requires zlib") - @unittest.skipUnless(ZIP_SUPPORT, 'Need zip support to run') - def test_make_zipfile(self) -> None: - # creating something to tar - tmpdir = self.mkdtemp() - self.write_file([tmpdir, 'file1'], 'xxx') - self.write_file([tmpdir, 'file2'], 'xxx') - - tmpdir2 = self.mkdtemp() - # force shutil to create the directory - os.rmdir(tmpdir2) - base_name = os.path.join(tmpdir2, 'archive') - _make_zipfile(base_name, tmpdir) - - # check if the compressed tarball was created - tarball = base_name + '.zip' - self.assertTrue(os.path.exists(tarball)) - - - def test_make_archive(self) -> None: - tmpdir = self.mkdtemp() - base_name = os.path.join(tmpdir, 'archive') - self.assertRaises(ValueError, make_archive, base_name, 'xxx') - - @unittest.skipUnless(zlib, "Requires zlib") - def test_make_archive_owner_group(self) -> None: - # testing make_archive with owner and group, with various combinations - # this works even if there's not gid/uid support - if UID_GID_SUPPORT: - group = grp.getgrgid(0).gr_name - owner = pwd.getpwuid(0).pw_name - else: - group = owner = 'root' - - base_dir, root_dir, base_name = self._create_files() - base_name = os.path.join(self.mkdtemp() , 'archive') - res = make_archive(base_name, 'zip', root_dir, base_dir, owner=owner, - group=group) - self.assertTrue(os.path.exists(res)) - - res = make_archive(base_name, 'zip', root_dir, base_dir) - self.assertTrue(os.path.exists(res)) - - res = make_archive(base_name, 'tar', root_dir, base_dir, - owner=owner, group=group) - self.assertTrue(os.path.exists(res)) - - res = make_archive(base_name, 'tar', root_dir, base_dir, - owner='kjhkjhkjg', group='oihohoh') - self.assertTrue(os.path.exists(res)) - - - @unittest.skipUnless(zlib, "Requires zlib") - @unittest.skipUnless(UID_GID_SUPPORT, "Requires grp and pwd support") - def test_tarfile_root_owner(self) -> None: - tmpdir, tmpdir2, base_name = self._create_files() - old_dir = os.getcwd() - os.chdir(tmpdir) - group = grp.getgrgid(0).gr_name - owner = pwd.getpwuid(0).pw_name - try: - archive_name = _make_tarball(base_name, 'dist', compress=None, - owner=owner, group=group) - finally: - os.chdir(old_dir) - - # check if the compressed tarball was created - self.assertTrue(os.path.exists(archive_name)) - - # now checks the rights - archive = tarfile.open(archive_name) - try: - for member in archive.getmembers(): - self.assertEqual(member.uid, 0) - self.assertEqual(member.gid, 0) - finally: - archive.close() - - def test_make_archive_cwd(self) -> None: - current_dir = os.getcwd() - def _breaks(*args: Any, **kw: Any) -> None: - raise RuntimeError() - - register_archive_format('xxx', _breaks, [], 'xxx file') - try: - try: - make_archive('xxx', 'xxx', root_dir=self.mkdtemp()) - except Exception: - pass - self.assertEqual(os.getcwd(), current_dir) - finally: - unregister_archive_format('xxx') - - def test_register_archive_format(self) -> None: - - self.assertRaises(TypeError, register_archive_format, 'xxx', 1) - self.assertRaises(TypeError, register_archive_format, 'xxx', - lambda: 1/0, - 1) - self.assertRaises(TypeError, register_archive_format, 'xxx', - lambda: 1/0, - [(1, 2), (1, 2, 3)]) - - register_archive_format('xxx', lambda: 1/0, [('x', 2)], 'xxx file') - formats = [name for name, params in get_archive_formats()] - self.assertIn('xxx', formats) - - unregister_archive_format('xxx') - formats = [name for name, params in get_archive_formats()] - self.assertNotIn('xxx', formats) - - def _compare_dirs(self, dir1: str, dir2: str) -> List[str]: - # check that dir1 and dir2 are equivalent, - # return the diff - diff = [] # type: List[str] - for root, dirs, files in os.walk(dir1): - for file_ in files: - path = os.path.join(root, file_) - target_path = os.path.join(dir2, os.path.split(path)[-1]) - if not os.path.exists(target_path): - diff.append(file_) - return diff - - @unittest.skipUnless(zlib, "Requires zlib") - def test_unpack_archive(self) -> None: - formats = ['tar', 'gztar', 'zip'] - if BZ2_SUPPORTED: - formats.append('bztar') - - for format in formats: - tmpdir = self.mkdtemp() - base_dir, root_dir, base_name = self._create_files() - tmpdir2 = self.mkdtemp() - filename = make_archive(base_name, format, root_dir, base_dir) - - # let's try to unpack it now - unpack_archive(filename, tmpdir2) - diff = self._compare_dirs(tmpdir, tmpdir2) - self.assertEqual(diff, []) - - # and again, this time with the format specified - tmpdir3 = self.mkdtemp() - unpack_archive(filename, tmpdir3, format=format) - diff = self._compare_dirs(tmpdir, tmpdir3) - self.assertEqual(diff, []) - self.assertRaises(shutil.ReadError, unpack_archive, TESTFN) - self.assertRaises(ValueError, unpack_archive, TESTFN, format='xxx') - - def test_unpack_registery(self) -> None: - - formats = get_unpack_formats() - - def _boo(filename: str, extract_dir: str, extra: int) -> None: - self.assertEqual(extra, 1) - self.assertEqual(filename, 'stuff.boo') - self.assertEqual(extract_dir, 'xx') - - register_unpack_format('Boo', ['.boo', '.b2'], _boo, [('extra', 1)]) - unpack_archive('stuff.boo', 'xx') - - # trying to register a .boo unpacker again - self.assertRaises(RegistryError, register_unpack_format, 'Boo2', - ['.boo'], _boo) - - # should work now - unregister_unpack_format('Boo') - register_unpack_format('Boo2', ['.boo'], _boo) - self.assertIn(('Boo2', ['.boo'], ''), get_unpack_formats()) - self.assertNotIn(('Boo', ['.boo'], ''), get_unpack_formats()) - - # let's leave a clean state - unregister_unpack_format('Boo2') - self.assertEqual(get_unpack_formats(), formats) - - -class TestMove(unittest.TestCase): - - def setUp(self) -> None: - filename = "foo" - self.src_dir = tempfile.mkdtemp() - self.dst_dir = tempfile.mkdtemp() - self.src_file = os.path.join(self.src_dir, filename) - self.dst_file = os.path.join(self.dst_dir, filename) - with open(self.src_file, "wb") as f: - f.write(b"spam") - - def tearDown(self) -> None: - for d in (self.src_dir, self.dst_dir): - try: - if d: - shutil.rmtree(d) - except: - pass - - def _check_move_file(self, src: str, dst: str, real_dst: str) -> None: - with open(src, "rb") as f: - contents = f.read() - shutil.move(src, dst) - with open(real_dst, "rb") as f: - self.assertEqual(contents, f.read()) - self.assertFalse(os.path.exists(src)) - - def _check_move_dir(self, src: str, dst: str, real_dst: str) -> None: - contents = sorted(os.listdir(src)) - shutil.move(src, dst) - self.assertEqual(contents, sorted(os.listdir(real_dst))) - self.assertFalse(os.path.exists(src)) - - def test_move_file(self) -> None: - # Move a file to another location on the same filesystem. - self._check_move_file(self.src_file, self.dst_file, self.dst_file) - - def test_move_file_to_dir(self) -> None: - # Move a file inside an existing dir on the same filesystem. - self._check_move_file(self.src_file, self.dst_dir, self.dst_file) - - @mock_rename - def test_move_file_other_fs(self) -> None: - # Move a file to an existing dir on another filesystem. - self.test_move_file() - - @mock_rename - def test_move_file_to_dir_other_fs(self) -> None: - # Move a file to another location on another filesystem. - self.test_move_file_to_dir() - - def test_move_dir(self) -> None: - # Move a dir to another location on the same filesystem. - dst_dir = tempfile.mktemp() - try: - self._check_move_dir(self.src_dir, dst_dir, dst_dir) - finally: - try: - shutil.rmtree(dst_dir) - except: - pass - - @mock_rename - def test_move_dir_other_fs(self) -> None: - # Move a dir to another location on another filesystem. - self.test_move_dir() - - def test_move_dir_to_dir(self) -> None: - # Move a dir inside an existing dir on the same filesystem. - self._check_move_dir(self.src_dir, self.dst_dir, - os.path.join(self.dst_dir, os.path.basename(self.src_dir))) - - @mock_rename - def test_move_dir_to_dir_other_fs(self) -> None: - # Move a dir inside an existing dir on another filesystem. - self.test_move_dir_to_dir() - - def test_existing_file_inside_dest_dir(self) -> None: - # A file with the same name inside the destination dir already exists. - with open(self.dst_file, "wb"): - pass - self.assertRaises(shutil.Error, shutil.move, self.src_file, self.dst_dir) - - def test_dont_move_dir_in_itself(self) -> None: - # Moving a dir inside itself raises an Error. - dst = os.path.join(self.src_dir, "bar") - self.assertRaises(shutil.Error, shutil.move, self.src_dir, dst) - - def test_destinsrc_false_negative(self) -> None: - os.mkdir(TESTFN) - try: - for src, dst in [('srcdir', 'srcdir/dest')]: - src = os.path.join(TESTFN, src) - dst = os.path.join(TESTFN, dst) - self.assertTrue(shutil._destinsrc(src, dst), - msg='_destinsrc() wrongly concluded that ' - 'dst (%s) is not in src (%s)' % (dst, src)) - finally: - shutil.rmtree(TESTFN, ignore_errors=True) - - def test_destinsrc_false_positive(self) -> None: - os.mkdir(TESTFN) - try: - for src, dst in [('srcdir', 'src/dest'), ('srcdir', 'srcdir.new')]: - src = os.path.join(TESTFN, src) - dst = os.path.join(TESTFN, dst) - self.assertFalse(shutil._destinsrc(src, dst), - msg='_destinsrc() wrongly concluded that ' - 'dst (%s) is in src (%s)' % (dst, src)) - finally: - shutil.rmtree(TESTFN, ignore_errors=True) - - -class TestCopyFile(unittest.TestCase): - - _delete = False - - class Faux(object): - _entered = False - _exited_with = None # type: tuple - _raised = False - def __init__(self, raise_in_exit: bool = False, - suppress_at_exit: bool = True) -> None: - self._raise_in_exit = raise_in_exit - self._suppress_at_exit = suppress_at_exit - def read(self, *args: Any) -> str: - return '' - def __enter__(self) -> None: - self._entered = True - def __exit__(self, exc_type: type, exc_val: BaseException, - exc_tb: TracebackType) -> bool: - self._exited_with = exc_type, exc_val, exc_tb - if self._raise_in_exit: - self._raised = True - raise IOError("Cannot close") - return self._suppress_at_exit - - def tearDown(self) -> None: - shutil.open = open - - def _set_shutil_open(self, func: Any) -> None: - shutil.open = func - self._delete = True - - def test_w_source_open_fails(self) -> None: - def _open(filename: str, mode: str= 'r') -> BinaryIO: - if filename == 'srcfile': - raise IOError('Cannot open "srcfile"') - assert 0 # shouldn't reach here. - - self._set_shutil_open(_open) - - self.assertRaises(IOError, shutil.copyfile, 'srcfile', 'destfile') - - def test_w_dest_open_fails(self) -> None: - - srcfile = TestCopyFile.Faux() - - def _open(filename: str, mode: str = 'r') -> TestCopyFile.Faux: - if filename == 'srcfile': - return srcfile - if filename == 'destfile': - raise IOError('Cannot open "destfile"') - assert 0 # shouldn't reach here. - - self._set_shutil_open(_open) - - shutil.copyfile('srcfile', 'destfile') - self.assertTrue(srcfile._entered) - self.assertTrue(srcfile._exited_with[0] is IOError) - self.assertEqual(srcfile._exited_with[1].args, - ('Cannot open "destfile"',)) - - def test_w_dest_close_fails(self) -> None: - - srcfile = TestCopyFile.Faux() - destfile = TestCopyFile.Faux(True) - - def _open(filename: str, mode: str = 'r') -> TestCopyFile.Faux: - if filename == 'srcfile': - return srcfile - if filename == 'destfile': - return destfile - assert 0 # shouldn't reach here. - - self._set_shutil_open(_open) - - shutil.copyfile('srcfile', 'destfile') - self.assertTrue(srcfile._entered) - self.assertTrue(destfile._entered) - self.assertTrue(destfile._raised) - self.assertTrue(srcfile._exited_with[0] is IOError) - self.assertEqual(srcfile._exited_with[1].args, - ('Cannot close',)) - - def test_w_source_close_fails(self) -> None: - - srcfile = TestCopyFile.Faux(True) - destfile = TestCopyFile.Faux() - - def _open(filename: str, mode: str= 'r') -> TestCopyFile.Faux: - if filename == 'srcfile': - return srcfile - if filename == 'destfile': - return destfile - assert 0 # shouldn't reach here. - - self._set_shutil_open(_open) - - self.assertRaises(IOError, - shutil.copyfile, 'srcfile', 'destfile') - self.assertTrue(srcfile._entered) - self.assertTrue(destfile._entered) - self.assertFalse(destfile._raised) - self.assertTrue(srcfile._exited_with[0] is None) - self.assertTrue(srcfile._raised) - - def test_move_dir_caseinsensitive(self) -> None: - # Renames a folder to the same name - # but a different case. - - self.src_dir = tempfile.mkdtemp() - dst_dir = os.path.join( - os.path.dirname(self.src_dir), - os.path.basename(self.src_dir).upper()) - self.assertNotEqual(self.src_dir, dst_dir) - - try: - shutil.move(self.src_dir, dst_dir) - self.assertTrue(os.path.isdir(dst_dir)) - finally: - if os.path.exists(dst_dir): - os.rmdir(dst_dir) - - - -def test_main() -> None: - support.run_unittest(TestShutil, TestMove, TestCopyFile) - -if __name__ == '__main__': - test_main() diff --git a/test-data/stdlib-samples/3.2/test/test_tempfile.py b/test-data/stdlib-samples/3.2/test/test_tempfile.py deleted file mode 100644 index 31b0fecbf677..000000000000 --- a/test-data/stdlib-samples/3.2/test/test_tempfile.py +++ /dev/null @@ -1,1122 +0,0 @@ -# tempfile.py unit tests. -import tempfile -import os -import signal -import sys -import re -import warnings - -import unittest -from test import support - -from typing import Any, AnyStr, List, Dict, IO - - -if hasattr(os, 'stat'): - import stat - has_stat = 1 -else: - has_stat = 0 - -has_textmode = (tempfile._text_openflags != tempfile._bin_openflags) -has_spawnl = hasattr(os, 'spawnl') - -# TEST_FILES may need to be tweaked for systems depending on the maximum -# number of files that can be opened at one time (see ulimit -n) -if sys.platform in ('openbsd3', 'openbsd4'): - TEST_FILES = 48 -else: - TEST_FILES = 100 - -# This is organized as one test for each chunk of code in tempfile.py, -# in order of their appearance in the file. Testing which requires -# threads is not done here. - -# Common functionality. -class TC(unittest.TestCase): - - str_check = re.compile(r"[a-zA-Z0-9_-]{6}$") - - def setUp(self) -> None: - self._warnings_manager = support.check_warnings() - self._warnings_manager.__enter__() - warnings.filterwarnings("ignore", category=RuntimeWarning, - message="mktemp", module=__name__) - - def tearDown(self) -> None: - self._warnings_manager.__exit__(None, None, None) - - - def failOnException(self, what: str, ei: tuple = None) -> None: - if ei is None: - ei = sys.exc_info() - self.fail("%s raised %s: %s" % (what, ei[0], ei[1])) - - def nameCheck(self, name: str, dir: str, pre: str, suf: str) -> None: - (ndir, nbase) = os.path.split(name) - npre = nbase[:len(pre)] - nsuf = nbase[len(nbase)-len(suf):] - - # check for equality of the absolute paths! - self.assertEqual(os.path.abspath(ndir), os.path.abspath(dir), - "file '%s' not in directory '%s'" % (name, dir)) - self.assertEqual(npre, pre, - "file '%s' does not begin with '%s'" % (nbase, pre)) - self.assertEqual(nsuf, suf, - "file '%s' does not end with '%s'" % (nbase, suf)) - - nbase = nbase[len(pre):len(nbase)-len(suf)] - self.assertTrue(self.str_check.match(nbase), - "random string '%s' does not match /^[a-zA-Z0-9_-]{6}$/" - % nbase) - -test_classes = [] # type: List[type] - -class test_exports(TC): - def test_exports(self) -> None: - # There are no surprising symbols in the tempfile module - dict = tempfile.__dict__ - - expected = { - "NamedTemporaryFile" : 1, - "TemporaryFile" : 1, - "mkstemp" : 1, - "mkdtemp" : 1, - "mktemp" : 1, - "TMP_MAX" : 1, - "gettempprefix" : 1, - "gettempdir" : 1, - "tempdir" : 1, - "template" : 1, - "SpooledTemporaryFile" : 1, - "TemporaryDirectory" : 1, - } - - unexp = [] # type: List[str] - for key in dict: - if key[0] != '_' and key not in expected: - unexp.append(key) - self.assertTrue(len(unexp) == 0, - "unexpected keys: %s" % unexp) - -test_classes.append(test_exports) - - -class test__RandomNameSequence(TC): - """Test the internal iterator object _RandomNameSequence.""" - - def setUp(self) -> None: - self.r = tempfile._RandomNameSequence() - super().setUp() - - def test_get_six_char_str(self) -> None: - # _RandomNameSequence returns a six-character string - s = next(self.r) - self.nameCheck(s, '', '', '') - - def test_many(self) -> None: - # _RandomNameSequence returns no duplicate strings (stochastic) - - dict = {} # type: Dict[str, int] - r = self.r - for i in range(TEST_FILES): - s = next(r) - self.nameCheck(s, '', '', '') - self.assertNotIn(s, dict) - dict[s] = 1 - - def supports_iter(self) -> None: - # _RandomNameSequence supports the iterator protocol - - i = 0 - r = self.r - try: - for s in r: - i += 1 - if i == 20: - break - except: - self.failOnException("iteration") - - @unittest.skipUnless(hasattr(os, 'fork'), - "os.fork is required for this test") - def test_process_awareness(self) -> None: - # ensure that the random source differs between - # child and parent. - read_fd, write_fd = os.pipe() - pid = None # type: int - try: - pid = os.fork() - if not pid: - os.close(read_fd) - os.write(write_fd, next(self.r).encode("ascii")) - os.close(write_fd) - # bypass the normal exit handlers- leave those to - # the parent. - os._exit(0) - parent_value = next(self.r) - child_value = os.read(read_fd, len(parent_value)).decode("ascii") - finally: - if pid: - # best effort to ensure the process can't bleed out - # via any bugs above - try: - os.kill(pid, signal.SIGKILL) - except EnvironmentError: - pass - os.close(read_fd) - os.close(write_fd) - self.assertNotEqual(child_value, parent_value) - - -test_classes.append(test__RandomNameSequence) - - -class test__candidate_tempdir_list(TC): - """Test the internal function _candidate_tempdir_list.""" - - def test_nonempty_list(self) -> None: - # _candidate_tempdir_list returns a nonempty list of strings - - cand = tempfile._candidate_tempdir_list() - - self.assertFalse(len(cand) == 0) - for c in cand: - self.assertIsInstance(c, str) - - def test_wanted_dirs(self) -> None: - # _candidate_tempdir_list contains the expected directories - - # Make sure the interesting environment variables are all set. - with support.EnvironmentVarGuard() as env: - for envname in 'TMPDIR', 'TEMP', 'TMP': - dirname = os.getenv(envname) - if not dirname: - env[envname] = os.path.abspath(envname) - - cand = tempfile._candidate_tempdir_list() - - for envname in 'TMPDIR', 'TEMP', 'TMP': - dirname = os.getenv(envname) - if not dirname: raise ValueError - self.assertIn(dirname, cand) - - try: - dirname = os.getcwd() - except (AttributeError, os.error): - dirname = os.curdir - - self.assertIn(dirname, cand) - - # Not practical to try to verify the presence of OS-specific - # paths in this list. - -test_classes.append(test__candidate_tempdir_list) - - -# We test _get_default_tempdir by testing gettempdir. - - -class test__get_candidate_names(TC): - """Test the internal function _get_candidate_names.""" - - def test_retval(self) -> None: - # _get_candidate_names returns a _RandomNameSequence object - obj = tempfile._get_candidate_names() - self.assertIsInstance(obj, tempfile._RandomNameSequence) - - def test_same_thing(self) -> None: - # _get_candidate_names always returns the same object - a = tempfile._get_candidate_names() - b = tempfile._get_candidate_names() - - self.assertTrue(a is b) - -test_classes.append(test__get_candidate_names) - - -class test__mkstemp_inner(TC): - """Test the internal function _mkstemp_inner.""" - - class mkstemped: - _bflags = tempfile._bin_openflags - _tflags = tempfile._text_openflags - - def __init__(self, dir: str, pre: str, suf: str, bin: int) -> None: - if bin: flags = self._bflags - else: flags = self._tflags - - (self.fd, self.name) = tempfile._mkstemp_inner(dir, pre, suf, flags) - - self._close = os.close - self._unlink = os.unlink - - def write(self, str: bytes) -> None: - os.write(self.fd, str) - - def __del__(self) -> None: - self._close(self.fd) - self._unlink(self.name) - - def do_create(self, dir: str = None, pre: str = "", suf: str= "", - bin: int = 1) -> mkstemped: - if dir is None: - dir = tempfile.gettempdir() - try: - file = test__mkstemp_inner.mkstemped(dir, pre, suf, bin) # see #259 - except: - self.failOnException("_mkstemp_inner") - - self.nameCheck(file.name, dir, pre, suf) - return file - - def test_basic(self) -> None: - # _mkstemp_inner can create files - self.do_create().write(b"blat") - self.do_create(pre="a").write(b"blat") - self.do_create(suf="b").write(b"blat") - self.do_create(pre="a", suf="b").write(b"blat") - self.do_create(pre="aa", suf=".txt").write(b"blat") - - def test_basic_many(self) -> None: - # _mkstemp_inner can create many files (stochastic) - extant = list(range(TEST_FILES)) # type: List[Any] - for i in extant: - extant[i] = self.do_create(pre="aa") - - def test_choose_directory(self) -> None: - # _mkstemp_inner can create files in a user-selected directory - dir = tempfile.mkdtemp() - try: - self.do_create(dir=dir).write(b"blat") - finally: - os.rmdir(dir) - - def test_file_mode(self) -> None: - # _mkstemp_inner creates files with the proper mode - if not has_stat: - return # ugh, can't use SkipTest. - - file = self.do_create() - mode = stat.S_IMODE(os.stat(file.name).st_mode) - expected = 0o600 - if sys.platform in ('win32', 'os2emx'): - # There's no distinction among 'user', 'group' and 'world'; - # replicate the 'user' bits. - user = expected >> 6 - expected = user * (1 + 8 + 64) - self.assertEqual(mode, expected) - - def test_noinherit(self) -> None: - # _mkstemp_inner file handles are not inherited by child processes - if not has_spawnl: - return # ugh, can't use SkipTest. - - if support.verbose: - v="v" - else: - v="q" - - file = self.do_create() - fd = "%d" % file.fd - - try: - me = __file__ # type: str - except NameError: - me = sys.argv[0] - - # We have to exec something, so that FD_CLOEXEC will take - # effect. The core of this test is therefore in - # tf_inherit_check.py, which see. - tester = os.path.join(os.path.dirname(os.path.abspath(me)), - "tf_inherit_check.py") - - # On Windows a spawn* /path/ with embedded spaces shouldn't be quoted, - # but an arg with embedded spaces should be decorated with double - # quotes on each end - if sys.platform in ('win32',): - decorated = '"%s"' % sys.executable - tester = '"%s"' % tester - else: - decorated = sys.executable - - retval = os.spawnl(os.P_WAIT, sys.executable, decorated, tester, v, fd) - self.assertFalse(retval < 0, - "child process caught fatal signal %d" % -retval) - self.assertFalse(retval > 0, "child process reports failure %d"%retval) - - def test_textmode(self) -> None: - # _mkstemp_inner can create files in text mode - if not has_textmode: - return # ugh, can't use SkipTest. - - # A text file is truncated at the first Ctrl+Z byte - f = self.do_create(bin=0) - f.write(b"blat\x1a") - f.write(b"extra\n") - os.lseek(f.fd, 0, os.SEEK_SET) - self.assertEqual(os.read(f.fd, 20), b"blat") - -test_classes.append(test__mkstemp_inner) - - -class test_gettempprefix(TC): - """Test gettempprefix().""" - - def test_sane_template(self) -> None: - # gettempprefix returns a nonempty prefix string - p = tempfile.gettempprefix() - - self.assertIsInstance(p, str) - self.assertTrue(len(p) > 0) - - def test_usable_template(self) -> None: - # gettempprefix returns a usable prefix string - - # Create a temp directory, avoiding use of the prefix. - # Then attempt to create a file whose name is - # prefix + 'xxxxxx.xxx' in that directory. - p = tempfile.gettempprefix() + "xxxxxx.xxx" - d = tempfile.mkdtemp(prefix="") - try: - p = os.path.join(d, p) - try: - fd = os.open(p, os.O_RDWR | os.O_CREAT) - except: - self.failOnException("os.open") - os.close(fd) - os.unlink(p) - finally: - os.rmdir(d) - -test_classes.append(test_gettempprefix) - - -class test_gettempdir(TC): - """Test gettempdir().""" - - def test_directory_exists(self) -> None: - # gettempdir returns a directory which exists - - dir = tempfile.gettempdir() - self.assertTrue(os.path.isabs(dir) or dir == os.curdir, - "%s is not an absolute path" % dir) - self.assertTrue(os.path.isdir(dir), - "%s is not a directory" % dir) - - def test_directory_writable(self) -> None: - # gettempdir returns a directory writable by the user - - # sneaky: just instantiate a NamedTemporaryFile, which - # defaults to writing into the directory returned by - # gettempdir. - try: - file = tempfile.NamedTemporaryFile() - file.write(b"blat") - file.close() - except: - self.failOnException("create file in %s" % tempfile.gettempdir()) - - def test_same_thing(self) -> None: - # gettempdir always returns the same object - a = tempfile.gettempdir() - b = tempfile.gettempdir() - - self.assertTrue(a is b) - -test_classes.append(test_gettempdir) - - -class test_mkstemp(TC): - """Test mkstemp().""" - - def do_create(self, dir: str = None, pre: str = "", suf: str = "") -> None: - if dir is None: - dir = tempfile.gettempdir() - try: - (fd, name) = tempfile.mkstemp(dir=dir, prefix=pre, suffix=suf) - (ndir, nbase) = os.path.split(name) - adir = os.path.abspath(dir) - self.assertEqual(adir, ndir, - "Directory '%s' incorrectly returned as '%s'" % (adir, ndir)) - except: - self.failOnException("mkstemp") - - try: - self.nameCheck(name, dir, pre, suf) - finally: - os.close(fd) - os.unlink(name) - - def test_basic(self) -> None: - # mkstemp can create files - self.do_create() - self.do_create(pre="a") - self.do_create(suf="b") - self.do_create(pre="a", suf="b") - self.do_create(pre="aa", suf=".txt") - self.do_create(dir=".") - - def test_choose_directory(self) -> None: - # mkstemp can create directories in a user-selected directory - dir = tempfile.mkdtemp() - try: - self.do_create(dir=dir) - finally: - os.rmdir(dir) - -test_classes.append(test_mkstemp) - - -class test_mkdtemp(TC): - """Test mkdtemp().""" - - def do_create(self, dir: str = None, pre: str = "", suf: str = "") -> str: - if dir is None: - dir = tempfile.gettempdir() - try: - name = tempfile.mkdtemp(dir=dir, prefix=pre, suffix=suf) - except: - self.failOnException("mkdtemp") - - try: - self.nameCheck(name, dir, pre, suf) - return name - except: - os.rmdir(name) - raise - - def test_basic(self) -> None: - # mkdtemp can create directories - os.rmdir(self.do_create()) - os.rmdir(self.do_create(pre="a")) - os.rmdir(self.do_create(suf="b")) - os.rmdir(self.do_create(pre="a", suf="b")) - os.rmdir(self.do_create(pre="aa", suf=".txt")) - - def test_basic_many(self) -> None: - # mkdtemp can create many directories (stochastic) - extant = list(range(TEST_FILES)) # type: List[Any] - try: - for i in extant: - extant[i] = self.do_create(pre="aa") - finally: - for i in extant: - if(isinstance(i, str)): - os.rmdir(i) - - def test_choose_directory(self) -> None: - # mkdtemp can create directories in a user-selected directory - dir = tempfile.mkdtemp() - try: - os.rmdir(self.do_create(dir=dir)) - finally: - os.rmdir(dir) - - def test_mode(self) -> None: - # mkdtemp creates directories with the proper mode - if not has_stat: - return # ugh, can't use SkipTest. - - dir = self.do_create() - try: - mode = stat.S_IMODE(os.stat(dir).st_mode) - mode &= 0o777 # Mask off sticky bits inherited from /tmp - expected = 0o700 - if sys.platform in ('win32', 'os2emx'): - # There's no distinction among 'user', 'group' and 'world'; - # replicate the 'user' bits. - user = expected >> 6 - expected = user * (1 + 8 + 64) - self.assertEqual(mode, expected) - finally: - os.rmdir(dir) - -test_classes.append(test_mkdtemp) - - -class test_mktemp(TC): - """Test mktemp().""" - - # For safety, all use of mktemp must occur in a private directory. - # We must also suppress the RuntimeWarning it generates. - def setUp(self) -> None: - self.dir = tempfile.mkdtemp() - super().setUp() - - def tearDown(self) -> None: - if self.dir: - os.rmdir(self.dir) - self.dir = None - super().tearDown() - - class mktemped: - def _unlink(self, path: str) -> None: - os.unlink(path) - - _bflags = tempfile._bin_openflags - - def __init__(self, dir: str, pre: str, suf: str) -> None: - self.name = tempfile.mktemp(dir=dir, prefix=pre, suffix=suf) - # Create the file. This will raise an exception if it's - # mysteriously appeared in the meanwhile. - os.close(os.open(self.name, self._bflags, 0o600)) - - def __del__(self) -> None: - self._unlink(self.name) - - def do_create(self, pre: str = "", suf: str = "") -> mktemped: - try: - file = test_mktemp.mktemped(self.dir, pre, suf) # see #259 - except: - self.failOnException("mktemp") - - self.nameCheck(file.name, self.dir, pre, suf) - return file - - def test_basic(self) -> None: - # mktemp can choose usable file names - self.do_create() - self.do_create(pre="a") - self.do_create(suf="b") - self.do_create(pre="a", suf="b") - self.do_create(pre="aa", suf=".txt") - - def test_many(self) -> None: - # mktemp can choose many usable file names (stochastic) - extant = list(range(TEST_FILES)) # type: List[Any] - for i in extant: - extant[i] = self.do_create(pre="aa") - -## def test_warning(self): -## # mktemp issues a warning when used -## warnings.filterwarnings("error", -## category=RuntimeWarning, -## message="mktemp") -## self.assertRaises(RuntimeWarning, -## tempfile.mktemp, dir=self.dir) - -test_classes.append(test_mktemp) - - -# We test _TemporaryFileWrapper by testing NamedTemporaryFile. - - -class test_NamedTemporaryFile(TC): - """Test NamedTemporaryFile().""" - - def do_create(self, dir: str = None, pre: str = "", suf: str = "", - delete: bool = True) -> IO[Any]: - if dir is None: - dir = tempfile.gettempdir() - try: - file = tempfile.NamedTemporaryFile(dir=dir, prefix=pre, suffix=suf, - delete=delete) - except: - self.failOnException("NamedTemporaryFile") - - self.nameCheck(file.name, dir, pre, suf) - return file - - - def test_basic(self) -> None: - # NamedTemporaryFile can create files - self.do_create() - self.do_create(pre="a") - self.do_create(suf="b") - self.do_create(pre="a", suf="b") - self.do_create(pre="aa", suf=".txt") - - def test_creates_named(self) -> None: - # NamedTemporaryFile creates files with names - f = tempfile.NamedTemporaryFile() - self.assertTrue(os.path.exists(f.name), - "NamedTemporaryFile %s does not exist" % f.name) - - def test_del_on_close(self) -> None: - # A NamedTemporaryFile is deleted when closed - dir = tempfile.mkdtemp() - try: - f = tempfile.NamedTemporaryFile(dir=dir) - f.write(b'blat') - f.close() - self.assertFalse(os.path.exists(f.name), - "NamedTemporaryFile %s exists after close" % f.name) - finally: - os.rmdir(dir) - - def test_dis_del_on_close(self) -> None: - # Tests that delete-on-close can be disabled - dir = tempfile.mkdtemp() - tmp = None # type: str - try: - f = tempfile.NamedTemporaryFile(dir=dir, delete=False) - tmp = f.name - f.write(b'blat') - f.close() - self.assertTrue(os.path.exists(f.name), - "NamedTemporaryFile %s missing after close" % f.name) - finally: - if tmp is not None: - os.unlink(tmp) - os.rmdir(dir) - - def test_multiple_close(self) -> None: - # A NamedTemporaryFile can be closed many times without error - f = tempfile.NamedTemporaryFile() - f.write(b'abc\n') - f.close() - try: - f.close() - f.close() - except: - self.failOnException("close") - - def test_context_manager(self) -> None: - # A NamedTemporaryFile can be used as a context manager - with tempfile.NamedTemporaryFile() as f: - self.assertTrue(os.path.exists(f.name)) - self.assertFalse(os.path.exists(f.name)) - def use_closed(): - with f: - pass - self.assertRaises(ValueError, use_closed) - - # How to test the mode and bufsize parameters? - -test_classes.append(test_NamedTemporaryFile) - -class test_SpooledTemporaryFile(TC): - """Test SpooledTemporaryFile().""" - - def do_create(self, max_size: int = 0, dir: str = None, pre: str = "", - suf: str = "") -> tempfile.SpooledTemporaryFile: - if dir is None: - dir = tempfile.gettempdir() - try: - file = tempfile.SpooledTemporaryFile(max_size=max_size, dir=dir, prefix=pre, suffix=suf) - except: - self.failOnException("SpooledTemporaryFile") - - return file - - - def test_basic(self) -> None: - # SpooledTemporaryFile can create files - f = self.do_create() - self.assertFalse(f._rolled) - f = self.do_create(max_size=100, pre="a", suf=".txt") - self.assertFalse(f._rolled) - - def test_del_on_close(self) -> None: - # A SpooledTemporaryFile is deleted when closed - dir = tempfile.mkdtemp() - try: - f = tempfile.SpooledTemporaryFile(max_size=10, dir=dir) - self.assertFalse(f._rolled) - f.write(b'blat ' * 5) - self.assertTrue(f._rolled) - filename = f.name - f.close() - self.assertFalse(isinstance(filename, str) and os.path.exists(filename), - "SpooledTemporaryFile %s exists after close" % filename) - finally: - os.rmdir(dir) - - def test_rewrite_small(self) -> None: - # A SpooledTemporaryFile can be written to multiple within the max_size - f = self.do_create(max_size=30) - self.assertFalse(f._rolled) - for i in range(5): - f.seek(0, 0) - f.write(b'x' * 20) - self.assertFalse(f._rolled) - - def test_write_sequential(self) -> None: - # A SpooledTemporaryFile should hold exactly max_size bytes, and roll - # over afterward - f = self.do_create(max_size=30) - self.assertFalse(f._rolled) - f.write(b'x' * 20) - self.assertFalse(f._rolled) - f.write(b'x' * 10) - self.assertFalse(f._rolled) - f.write(b'x') - self.assertTrue(f._rolled) - - def test_writelines(self) -> None: - # Verify writelines with a SpooledTemporaryFile - f = self.do_create() - f.writelines([b'x', b'y', b'z']) - f.seek(0) - buf = f.read() - self.assertEqual(buf, b'xyz') - - def test_writelines_sequential(self) -> None: - # A SpooledTemporaryFile should hold exactly max_size bytes, and roll - # over afterward - f = self.do_create(max_size=35) - f.writelines([b'x' * 20, b'x' * 10, b'x' * 5]) - self.assertFalse(f._rolled) - f.write(b'x') - self.assertTrue(f._rolled) - - def test_sparse(self) -> None: - # A SpooledTemporaryFile that is written late in the file will extend - # when that occurs - f = self.do_create(max_size=30) - self.assertFalse(f._rolled) - f.seek(100, 0) - self.assertFalse(f._rolled) - f.write(b'x') - self.assertTrue(f._rolled) - - def test_fileno(self) -> None: - # A SpooledTemporaryFile should roll over to a real file on fileno() - f = self.do_create(max_size=30) - self.assertFalse(f._rolled) - self.assertTrue(f.fileno() > 0) - self.assertTrue(f._rolled) - - def test_multiple_close_before_rollover(self) -> None: - # A SpooledTemporaryFile can be closed many times without error - f = tempfile.SpooledTemporaryFile() - f.write(b'abc\n') - self.assertFalse(f._rolled) - f.close() - try: - f.close() - f.close() - except: - self.failOnException("close") - - def test_multiple_close_after_rollover(self) -> None: - # A SpooledTemporaryFile can be closed many times without error - f = tempfile.SpooledTemporaryFile(max_size=1) - f.write(b'abc\n') - self.assertTrue(f._rolled) - f.close() - try: - f.close() - f.close() - except: - self.failOnException("close") - - def test_bound_methods(self) -> None: - # It should be OK to steal a bound method from a SpooledTemporaryFile - # and use it independently; when the file rolls over, those bound - # methods should continue to function - f = self.do_create(max_size=30) - read = f.read - write = f.write - seek = f.seek - - write(b"a" * 35) - write(b"b" * 35) - seek(0, 0) - self.assertEqual(read(70), b'a'*35 + b'b'*35) - - def test_text_mode(self) -> None: - # Creating a SpooledTemporaryFile with a text mode should produce - # a file object reading and writing (Unicode) text strings. - f = tempfile.SpooledTemporaryFile(mode='w+', max_size=10) - f.write("abc\n") - f.seek(0) - self.assertEqual(f.read(), "abc\n") - f.write("def\n") - f.seek(0) - self.assertEqual(f.read(), "abc\ndef\n") - f.write("xyzzy\n") - f.seek(0) - self.assertEqual(f.read(), "abc\ndef\nxyzzy\n") - # Check that Ctrl+Z doesn't truncate the file - f.write("foo\x1abar\n") - f.seek(0) - self.assertEqual(f.read(), "abc\ndef\nxyzzy\nfoo\x1abar\n") - - def test_text_newline_and_encoding(self) -> None: - f = tempfile.SpooledTemporaryFile(mode='w+', max_size=10, - newline='', encoding='utf-8') - f.write("\u039B\r\n") - f.seek(0) - self.assertEqual(f.read(), "\u039B\r\n") - self.assertFalse(f._rolled) - - f.write("\u039B" * 20 + "\r\n") - f.seek(0) - self.assertEqual(f.read(), "\u039B\r\n" + ("\u039B" * 20) + "\r\n") - self.assertTrue(f._rolled) - - def test_context_manager_before_rollover(self) -> None: - # A SpooledTemporaryFile can be used as a context manager - with tempfile.SpooledTemporaryFile(max_size=1) as f: - self.assertFalse(f._rolled) - self.assertFalse(f.closed) - self.assertTrue(f.closed) - def use_closed(): - with f: - pass - self.assertRaises(ValueError, use_closed) - - def test_context_manager_during_rollover(self) -> None: - # A SpooledTemporaryFile can be used as a context manager - with tempfile.SpooledTemporaryFile(max_size=1) as f: - self.assertFalse(f._rolled) - f.write(b'abc\n') - f.flush() - self.assertTrue(f._rolled) - self.assertFalse(f.closed) - self.assertTrue(f.closed) - def use_closed(): - with f: - pass - self.assertRaises(ValueError, use_closed) - - def test_context_manager_after_rollover(self) -> None: - # A SpooledTemporaryFile can be used as a context manager - f = tempfile.SpooledTemporaryFile(max_size=1) - f.write(b'abc\n') - f.flush() - self.assertTrue(f._rolled) - with f: - self.assertFalse(f.closed) - self.assertTrue(f.closed) - def use_closed(): - with f: - pass - self.assertRaises(ValueError, use_closed) - - -test_classes.append(test_SpooledTemporaryFile) - - -class test_TemporaryFile(TC): - """Test TemporaryFile().""" - - def test_basic(self) -> None: - # TemporaryFile can create files - # No point in testing the name params - the file has no name. - try: - tempfile.TemporaryFile() - except: - self.failOnException("TemporaryFile") - - def test_has_no_name(self) -> None: - # TemporaryFile creates files with no names (on this system) - dir = tempfile.mkdtemp() - f = tempfile.TemporaryFile(dir=dir) - f.write(b'blat') - - # Sneaky: because this file has no name, it should not prevent - # us from removing the directory it was created in. - try: - os.rmdir(dir) - except: - ei = sys.exc_info() - # cleanup - f.close() - os.rmdir(dir) - self.failOnException("rmdir", ei) - - def test_multiple_close(self) -> None: - # A TemporaryFile can be closed many times without error - f = tempfile.TemporaryFile() - f.write(b'abc\n') - f.close() - try: - f.close() - f.close() - except: - self.failOnException("close") - - # How to test the mode and bufsize parameters? - def test_mode_and_encoding(self) -> None: - - def roundtrip(input: AnyStr, *args: Any, **kwargs: Any) -> None: - with tempfile.TemporaryFile(*args, **kwargs) as fileobj: - fileobj.write(input) - fileobj.seek(0) - self.assertEqual(input, fileobj.read()) - - roundtrip(b"1234", "w+b") - roundtrip("abdc\n", "w+") - roundtrip("\u039B", "w+", encoding="utf-16") - roundtrip("foo\r\n", "w+", newline="") - - -if tempfile.NamedTemporaryFile is not tempfile.TemporaryFile: - test_classes.append(test_TemporaryFile) - - -# Helper for test_del_on_shutdown -class NulledModules: - def __init__(self, *modules: Any) -> None: - self.refs = [mod.__dict__ for mod in modules] - self.contents = [ref.copy() for ref in self.refs] - - def __enter__(self) -> None: - for d in self.refs: - for key in d: - d[key] = None - - def __exit__(self, *exc_info: Any) -> None: - for d, c in zip(self.refs, self.contents): - d.clear() - d.update(c) - -class test_TemporaryDirectory(TC): - """Test TemporaryDirectory().""" - - def do_create(self, dir: str = None, pre: str = "", suf: str = "", - recurse: int = 1) -> tempfile.TemporaryDirectory: - if dir is None: - dir = tempfile.gettempdir() - try: - tmp = tempfile.TemporaryDirectory(dir=dir, prefix=pre, suffix=suf) - except: - self.failOnException("TemporaryDirectory") - self.nameCheck(tmp.name, dir, pre, suf) - # Create a subdirectory and some files - if recurse: - self.do_create(tmp.name, pre, suf, recurse-1) - with open(os.path.join(tmp.name, "test.txt"), "wb") as f: - f.write(b"Hello world!") - return tmp - - def test_mkdtemp_failure(self) -> None: - # Check no additional exception if mkdtemp fails - # Previously would raise AttributeError instead - # (noted as part of Issue #10188) - with tempfile.TemporaryDirectory() as nonexistent: - pass - with self.assertRaises(os.error): - tempfile.TemporaryDirectory(dir=nonexistent) - - def test_explicit_cleanup(self) -> None: - # A TemporaryDirectory is deleted when cleaned up - dir = tempfile.mkdtemp() - try: - d = self.do_create(dir=dir) - self.assertTrue(os.path.exists(d.name), - "TemporaryDirectory %s does not exist" % d.name) - d.cleanup() - self.assertFalse(os.path.exists(d.name), - "TemporaryDirectory %s exists after cleanup" % d.name) - finally: - os.rmdir(dir) - - @support.skip_unless_symlink - def test_cleanup_with_symlink_to_a_directory(self) -> None: - # cleanup() should not follow symlinks to directories (issue #12464) - d1 = self.do_create() - d2 = self.do_create() - - # Symlink d1/foo -> d2 - os.symlink(d2.name, os.path.join(d1.name, "foo")) - - # This call to cleanup() should not follow the "foo" symlink - d1.cleanup() - - self.assertFalse(os.path.exists(d1.name), - "TemporaryDirectory %s exists after cleanup" % d1.name) - self.assertTrue(os.path.exists(d2.name), - "Directory pointed to by a symlink was deleted") - self.assertEqual(os.listdir(d2.name), ['test.txt'], - "Contents of the directory pointed to by a symlink " - "were deleted") - d2.cleanup() - - @support.cpython_only - def test_del_on_collection(self) -> None: - # A TemporaryDirectory is deleted when garbage collected - dir = tempfile.mkdtemp() - try: - d = self.do_create(dir=dir) - name = d.name - del d # Rely on refcounting to invoke __del__ - self.assertFalse(os.path.exists(name), - "TemporaryDirectory %s exists after __del__" % name) - finally: - os.rmdir(dir) - - @unittest.expectedFailure # See issue #10188 - def test_del_on_shutdown(self) -> None: - # A TemporaryDirectory may be cleaned up during shutdown - # Make sure it works with the relevant modules nulled out - with self.do_create() as dir: - d = self.do_create(dir=dir) - # Mimic the nulling out of modules that - # occurs during system shutdown - modules = [os, os.path] - if has_stat: - modules.append(stat) - # Currently broken, so suppress the warning - # that is otherwise emitted on stdout - with support.captured_stderr() as err: - with NulledModules(*modules): - d.cleanup() - # Currently broken, so stop spurious exception by - # indicating the object has already been closed - d._closed = True - # And this assert will fail, as expected by the - # unittest decorator... - self.assertFalse(os.path.exists(d.name), - "TemporaryDirectory %s exists after cleanup" % d.name) - - def test_warnings_on_cleanup(self) -> None: - # Two kinds of warning on shutdown - # Issue 10888: may write to stderr if modules are nulled out - # ResourceWarning will be triggered by __del__ - with self.do_create() as dir: - if os.sep != '\\': - # Embed a backslash in order to make sure string escaping - # in the displayed error message is dealt with correctly - suffix = '\\check_backslash_handling' - else: - suffix = '' - d = self.do_create(dir=dir, suf=suffix) - - #Check for the Issue 10888 message - modules = [os, os.path] - if has_stat: - modules.append(stat) - with support.captured_stderr() as err: - with NulledModules(*modules): - d.cleanup() - message = err.getvalue().replace('\\\\', '\\') - self.assertIn("while cleaning up", message) - self.assertIn(d.name, message) - - # Check for the resource warning - with support.check_warnings(('Implicitly', ResourceWarning), quiet=False): - warnings.filterwarnings("always", category=ResourceWarning) - d.__del__() - self.assertFalse(os.path.exists(d.name), - "TemporaryDirectory %s exists after __del__" % d.name) - - def test_multiple_close(self) -> None: - # Can be cleaned-up many times without error - d = self.do_create() - d.cleanup() - try: - d.cleanup() - d.cleanup() - except: - self.failOnException("cleanup") - - def test_context_manager(self) -> None: - # Can be used as a context manager - d = self.do_create() - with d as name: - self.assertTrue(os.path.exists(name)) - self.assertEqual(name, d.name) - self.assertFalse(os.path.exists(name)) - - -test_classes.append(test_TemporaryDirectory) - -def test_main() -> None: - support.run_unittest(*test_classes) - -if __name__ == "__main__": - test_main() diff --git a/test-data/stdlib-samples/3.2/test/test_textwrap.py b/test-data/stdlib-samples/3.2/test/test_textwrap.py deleted file mode 100644 index 79d921a583e6..000000000000 --- a/test-data/stdlib-samples/3.2/test/test_textwrap.py +++ /dev/null @@ -1,601 +0,0 @@ -# -# Test suite for the textwrap module. -# -# Original tests written by Greg Ward . -# Converted to PyUnit by Peter Hansen . -# Currently maintained by Greg Ward. -# -# $Id$ -# - -import unittest -from test import support - -from typing import Any, List, Sequence - -from textwrap import TextWrapper, wrap, fill, dedent - - -class BaseTestCase(unittest.TestCase): - '''Parent class with utility methods for textwrap tests.''' - - wrapper = None # type: TextWrapper - - def show(self, textin: Sequence[str]) -> str: - if isinstance(textin, list): - results = [] # type: List[str] - for i in range(len(textin)): - results.append(" %d: %r" % (i, textin[i])) - result = '\n'.join(results) - elif isinstance(textin, str): - result = " %s\n" % repr(textin) - return result - - - def check(self, result: Sequence[str], expect: Sequence[str]) -> None: - self.assertEqual(result, expect, - 'expected:\n%s\nbut got:\n%s' % ( - self.show(expect), self.show(result))) - - def check_wrap(self, text: str, width: int, expect: Sequence[str], - **kwargs: Any) -> None: - result = wrap(text, width, **kwargs) - self.check(result, expect) - - def check_split(self, text: str, expect: Sequence[str]) -> None: - result = self.wrapper._split(text) - self.assertEqual(result, expect, - "\nexpected %r\n" - "but got %r" % (expect, result)) - - -class WrapTestCase(BaseTestCase): - - def setUp(self) -> None: - self.wrapper = TextWrapper(width=45) - - def test_simple(self) -> None: - # Simple case: just words, spaces, and a bit of punctuation - - text = "Hello there, how are you this fine day? I'm glad to hear it!" - - self.check_wrap(text, 12, - ["Hello there,", - "how are you", - "this fine", - "day? I'm", - "glad to hear", - "it!"]) - self.check_wrap(text, 42, - ["Hello there, how are you this fine day?", - "I'm glad to hear it!"]) - self.check_wrap(text, 80, [text]) - - - def test_whitespace(self) -> None: - # Whitespace munging and end-of-sentence detection - - text = """\ -This is a paragraph that already has -line breaks. But some of its lines are much longer than the others, -so it needs to be wrapped. -Some lines are \ttabbed too. -What a mess! -""" - - expect = ["This is a paragraph that already has line", - "breaks. But some of its lines are much", - "longer than the others, so it needs to be", - "wrapped. Some lines are tabbed too. What a", - "mess!"] - - wrapper = TextWrapper(45, fix_sentence_endings=True) - result = wrapper.wrap(text) - self.check(result, expect) - - results = wrapper.fill(text) - self.check(results, '\n'.join(expect)) - - def test_fix_sentence_endings(self) -> None: - wrapper = TextWrapper(60, fix_sentence_endings=True) - - # SF #847346: ensure that fix_sentence_endings=True does the - # right thing even on input short enough that it doesn't need to - # be wrapped. - text = "A short line. Note the single space." - expect = ["A short line. Note the single space."] - self.check(wrapper.wrap(text), expect) - - # Test some of the hairy end cases that _fix_sentence_endings() - # is supposed to handle (the easy stuff is tested in - # test_whitespace() above). - text = "Well, Doctor? What do you think?" - expect = ["Well, Doctor? What do you think?"] - self.check(wrapper.wrap(text), expect) - - text = "Well, Doctor?\nWhat do you think?" - self.check(wrapper.wrap(text), expect) - - text = 'I say, chaps! Anyone for "tennis?"\nHmmph!' - expect = ['I say, chaps! Anyone for "tennis?" Hmmph!'] - self.check(wrapper.wrap(text), expect) - - wrapper.width = 20 - expect = ['I say, chaps!', 'Anyone for "tennis?"', 'Hmmph!'] - self.check(wrapper.wrap(text), expect) - - text = 'And she said, "Go to hell!"\nCan you believe that?' - expect = ['And she said, "Go to', - 'hell!" Can you', - 'believe that?'] - self.check(wrapper.wrap(text), expect) - - wrapper.width = 60 - expect = ['And she said, "Go to hell!" Can you believe that?'] - self.check(wrapper.wrap(text), expect) - - text = 'File stdio.h is nice.' - expect = ['File stdio.h is nice.'] - self.check(wrapper.wrap(text), expect) - - def test_wrap_short(self) -> None: - # Wrapping to make short lines longer - - text = "This is a\nshort paragraph." - - self.check_wrap(text, 20, ["This is a short", - "paragraph."]) - self.check_wrap(text, 40, ["This is a short paragraph."]) - - - def test_wrap_short_1line(self) -> None: - # Test endcases - - text = "This is a short line." - - self.check_wrap(text, 30, ["This is a short line."]) - self.check_wrap(text, 30, ["(1) This is a short line."], - initial_indent="(1) ") - - - def test_hyphenated(self) -> None: - # Test breaking hyphenated words - - text = ("this-is-a-useful-feature-for-" - "reformatting-posts-from-tim-peters'ly") - - self.check_wrap(text, 40, - ["this-is-a-useful-feature-for-", - "reformatting-posts-from-tim-peters'ly"]) - self.check_wrap(text, 41, - ["this-is-a-useful-feature-for-", - "reformatting-posts-from-tim-peters'ly"]) - self.check_wrap(text, 42, - ["this-is-a-useful-feature-for-reformatting-", - "posts-from-tim-peters'ly"]) - - def test_hyphenated_numbers(self) -> None: - # Test that hyphenated numbers (eg. dates) are not broken like words. - text = ("Python 1.0.0 was released on 1994-01-26. Python 1.0.1 was\n" - "released on 1994-02-15.") - - self.check_wrap(text, 30, ['Python 1.0.0 was released on', - '1994-01-26. Python 1.0.1 was', - 'released on 1994-02-15.']) - self.check_wrap(text, 40, ['Python 1.0.0 was released on 1994-01-26.', - 'Python 1.0.1 was released on 1994-02-15.']) - - text = "I do all my shopping at 7-11." - self.check_wrap(text, 25, ["I do all my shopping at", - "7-11."]) - self.check_wrap(text, 27, ["I do all my shopping at", - "7-11."]) - self.check_wrap(text, 29, ["I do all my shopping at 7-11."]) - - def test_em_dash(self) -> None: - # Test text with em-dashes - text = "Em-dashes should be written -- thus." - self.check_wrap(text, 25, - ["Em-dashes should be", - "written -- thus."]) - - # Probe the boundaries of the properly written em-dash, - # ie. " -- ". - self.check_wrap(text, 29, - ["Em-dashes should be written", - "-- thus."]) - expect = ["Em-dashes should be written --", - "thus."] - self.check_wrap(text, 30, expect) - self.check_wrap(text, 35, expect) - self.check_wrap(text, 36, - ["Em-dashes should be written -- thus."]) - - # The improperly written em-dash is handled too, because - # it's adjacent to non-whitespace on both sides. - text = "You can also do--this or even---this." - expect = ["You can also do", - "--this or even", - "---this."] - self.check_wrap(text, 15, expect) - self.check_wrap(text, 16, expect) - expect = ["You can also do--", - "this or even---", - "this."] - self.check_wrap(text, 17, expect) - self.check_wrap(text, 19, expect) - expect = ["You can also do--this or even", - "---this."] - self.check_wrap(text, 29, expect) - self.check_wrap(text, 31, expect) - expect = ["You can also do--this or even---", - "this."] - self.check_wrap(text, 32, expect) - self.check_wrap(text, 35, expect) - - # All of the above behaviour could be deduced by probing the - # _split() method. - text = "Here's an -- em-dash and--here's another---and another!" - expect = ["Here's", " ", "an", " ", "--", " ", "em-", "dash", " ", - "and", "--", "here's", " ", "another", "---", - "and", " ", "another!"] - self.check_split(text, expect) - - text = "and then--bam!--he was gone" - expect = ["and", " ", "then", "--", "bam!", "--", - "he", " ", "was", " ", "gone"] - self.check_split(text, expect) - - - def test_unix_options (self) -> None: - # Test that Unix-style command-line options are wrapped correctly. - # Both Optik (OptionParser) and Docutils rely on this behaviour! - - text = "You should use the -n option, or --dry-run in its long form." - self.check_wrap(text, 20, - ["You should use the", - "-n option, or --dry-", - "run in its long", - "form."]) - self.check_wrap(text, 21, - ["You should use the -n", - "option, or --dry-run", - "in its long form."]) - expect = ["You should use the -n option, or", - "--dry-run in its long form."] - self.check_wrap(text, 32, expect) - self.check_wrap(text, 34, expect) - self.check_wrap(text, 35, expect) - self.check_wrap(text, 38, expect) - expect = ["You should use the -n option, or --dry-", - "run in its long form."] - self.check_wrap(text, 39, expect) - self.check_wrap(text, 41, expect) - expect = ["You should use the -n option, or --dry-run", - "in its long form."] - self.check_wrap(text, 42, expect) - - # Again, all of the above can be deduced from _split(). - text = "the -n option, or --dry-run or --dryrun" - expect = ["the", " ", "-n", " ", "option,", " ", "or", " ", - "--dry-", "run", " ", "or", " ", "--dryrun"] - self.check_split(text, expect) - - def test_funky_hyphens (self) -> None: - # Screwy edge cases cooked up by David Goodger. All reported - # in SF bug #596434. - self.check_split("what the--hey!", ["what", " ", "the", "--", "hey!"]) - self.check_split("what the--", ["what", " ", "the--"]) - self.check_split("what the--.", ["what", " ", "the--."]) - self.check_split("--text--.", ["--text--."]) - - # When I first read bug #596434, this is what I thought David - # was talking about. I was wrong; these have always worked - # fine. The real problem is tested in test_funky_parens() - # below... - self.check_split("--option", ["--option"]) - self.check_split("--option-opt", ["--option-", "opt"]) - self.check_split("foo --option-opt bar", - ["foo", " ", "--option-", "opt", " ", "bar"]) - - def test_punct_hyphens(self) -> None: - # Oh bother, SF #965425 found another problem with hyphens -- - # hyphenated words in single quotes weren't handled correctly. - # In fact, the bug is that *any* punctuation around a hyphenated - # word was handled incorrectly, except for a leading "--", which - # was special-cased for Optik and Docutils. So test a variety - # of styles of punctuation around a hyphenated word. - # (Actually this is based on an Optik bug report, #813077). - self.check_split("the 'wibble-wobble' widget", - ['the', ' ', "'wibble-", "wobble'", ' ', 'widget']) - self.check_split('the "wibble-wobble" widget', - ['the', ' ', '"wibble-', 'wobble"', ' ', 'widget']) - self.check_split("the (wibble-wobble) widget", - ['the', ' ', "(wibble-", "wobble)", ' ', 'widget']) - self.check_split("the ['wibble-wobble'] widget", - ['the', ' ', "['wibble-", "wobble']", ' ', 'widget']) - - def test_funky_parens (self) -> None: - # Second part of SF bug #596434: long option strings inside - # parentheses. - self.check_split("foo (--option) bar", - ["foo", " ", "(--option)", " ", "bar"]) - - # Related stuff -- make sure parens work in simpler contexts. - self.check_split("foo (bar) baz", - ["foo", " ", "(bar)", " ", "baz"]) - self.check_split("blah (ding dong), wubba", - ["blah", " ", "(ding", " ", "dong),", - " ", "wubba"]) - - def test_initial_whitespace(self) -> None: - # SF bug #622849 reported inconsistent handling of leading - # whitespace; let's test that a bit, shall we? - text = " This is a sentence with leading whitespace." - self.check_wrap(text, 50, - [" This is a sentence with leading whitespace."]) - self.check_wrap(text, 30, - [" This is a sentence with", "leading whitespace."]) - - def test_no_drop_whitespace(self) -> None: - # SF patch #1581073 - text = " This is a sentence with much whitespace." - self.check_wrap(text, 10, - [" This is a", " ", "sentence ", - "with ", "much white", "space."], - drop_whitespace=False) - - def test_split(self) -> None: - # Ensure that the standard _split() method works as advertised - # in the comments - - text = "Hello there -- you goof-ball, use the -b option!" - - result = self.wrapper._split(text) - self.check(result, - ["Hello", " ", "there", " ", "--", " ", "you", " ", "goof-", - "ball,", " ", "use", " ", "the", " ", "-b", " ", "option!"]) - - def test_break_on_hyphens(self) -> None: - # Ensure that the break_on_hyphens attributes work - text = "yaba daba-doo" - self.check_wrap(text, 10, ["yaba daba-", "doo"], - break_on_hyphens=True) - self.check_wrap(text, 10, ["yaba", "daba-doo"], - break_on_hyphens=False) - - def test_bad_width(self) -> None: - # Ensure that width <= 0 is caught. - text = "Whatever, it doesn't matter." - self.assertRaises(ValueError, wrap, text, 0) - self.assertRaises(ValueError, wrap, text, -1) - - def test_no_split_at_umlaut(self) -> None: - text = "Die Empf\xe4nger-Auswahl" - self.check_wrap(text, 13, ["Die", "Empf\xe4nger-", "Auswahl"]) - - def test_umlaut_followed_by_dash(self) -> None: - text = "aa \xe4\xe4-\xe4\xe4" - self.check_wrap(text, 7, ["aa \xe4\xe4-", "\xe4\xe4"]) - - -class LongWordTestCase (BaseTestCase): - def setUp(self) -> None: - self.wrapper = TextWrapper() - self.text = '''\ -Did you say "supercalifragilisticexpialidocious?" -How *do* you spell that odd word, anyways? -''' - - def test_break_long(self) -> None: - # Wrap text with long words and lots of punctuation - - self.check_wrap(self.text, 30, - ['Did you say "supercalifragilis', - 'ticexpialidocious?" How *do*', - 'you spell that odd word,', - 'anyways?']) - self.check_wrap(self.text, 50, - ['Did you say "supercalifragilisticexpialidocious?"', - 'How *do* you spell that odd word, anyways?']) - - # SF bug 797650. Prevent an infinite loop by making sure that at - # least one character gets split off on every pass. - self.check_wrap('-'*10+'hello', 10, - ['----------', - ' h', - ' e', - ' l', - ' l', - ' o'], - subsequent_indent = ' '*15) - - # bug 1146. Prevent a long word to be wrongly wrapped when the - # preceding word is exactly one character shorter than the width - self.check_wrap(self.text, 12, - ['Did you say ', - '"supercalifr', - 'agilisticexp', - 'ialidocious?', - '" How *do*', - 'you spell', - 'that odd', - 'word,', - 'anyways?']) - - def test_nobreak_long(self) -> None: - # Test with break_long_words disabled - self.wrapper.break_long_words = False - self.wrapper.width = 30 - expect = ['Did you say', - '"supercalifragilisticexpialidocious?"', - 'How *do* you spell that odd', - 'word, anyways?' - ] - result = self.wrapper.wrap(self.text) - self.check(result, expect) - - # Same thing with kwargs passed to standalone wrap() function. - result = wrap(self.text, width=30, break_long_words=0) - self.check(result, expect) - - -class IndentTestCases(BaseTestCase): - - # called before each test method - def setUp(self) -> None: - self.text = '''\ -This paragraph will be filled, first without any indentation, -and then with some (including a hanging indent).''' - - - def test_fill(self) -> None: - # Test the fill() method - - expect = '''\ -This paragraph will be filled, first -without any indentation, and then with -some (including a hanging indent).''' - - result = fill(self.text, 40) - self.check(result, expect) - - - def test_initial_indent(self) -> None: - # Test initial_indent parameter - - expect = [" This paragraph will be filled,", - "first without any indentation, and then", - "with some (including a hanging indent)."] - result = wrap(self.text, 40, initial_indent=" ") - self.check(result, expect) - - expects = "\n".join(expect) - results = fill(self.text, 40, initial_indent=" ") - self.check(results, expects) - - - def test_subsequent_indent(self) -> None: - # Test subsequent_indent parameter - - expect = '''\ - * This paragraph will be filled, first - without any indentation, and then - with some (including a hanging - indent).''' - - result = fill(self.text, 40, - initial_indent=" * ", subsequent_indent=" ") - self.check(result, expect) - - -# Despite the similar names, DedentTestCase is *not* the inverse -# of IndentTestCase! -class DedentTestCase(unittest.TestCase): - - def assertUnchanged(self, text: str) -> None: - """assert that dedent() has no effect on 'text'""" - self.assertEqual(text, dedent(text)) - - def test_dedent_nomargin(self) -> None: - # No lines indented. - text = "Hello there.\nHow are you?\nOh good, I'm glad." - self.assertUnchanged(text) - - # Similar, with a blank line. - text = "Hello there.\n\nBoo!" - self.assertUnchanged(text) - - # Some lines indented, but overall margin is still zero. - text = "Hello there.\n This is indented." - self.assertUnchanged(text) - - # Again, add a blank line. - text = "Hello there.\n\n Boo!\n" - self.assertUnchanged(text) - - def test_dedent_even(self) -> None: - # All lines indented by two spaces. - text = " Hello there.\n How are ya?\n Oh good." - expect = "Hello there.\nHow are ya?\nOh good." - self.assertEqual(expect, dedent(text)) - - # Same, with blank lines. - text = " Hello there.\n\n How are ya?\n Oh good.\n" - expect = "Hello there.\n\nHow are ya?\nOh good.\n" - self.assertEqual(expect, dedent(text)) - - # Now indent one of the blank lines. - text = " Hello there.\n \n How are ya?\n Oh good.\n" - expect = "Hello there.\n\nHow are ya?\nOh good.\n" - self.assertEqual(expect, dedent(text)) - - def test_dedent_uneven(self) -> None: - # Lines indented unevenly. - text = '''\ - def foo(): - while 1: - return foo - ''' - expect = '''\ -def foo(): - while 1: - return foo -''' - self.assertEqual(expect, dedent(text)) - - # Uneven indentation with a blank line. - text = " Foo\n Bar\n\n Baz\n" - expect = "Foo\n Bar\n\n Baz\n" - self.assertEqual(expect, dedent(text)) - - # Uneven indentation with a whitespace-only line. - text = " Foo\n Bar\n \n Baz\n" - expect = "Foo\n Bar\n\n Baz\n" - self.assertEqual(expect, dedent(text)) - - # dedent() should not mangle internal tabs - def test_dedent_preserve_internal_tabs(self) -> None: - text = " hello\tthere\n how are\tyou?" - expect = "hello\tthere\nhow are\tyou?" - self.assertEqual(expect, dedent(text)) - - # make sure that it preserves tabs when it's not making any - # changes at all - self.assertEqual(expect, dedent(expect)) - - # dedent() should not mangle tabs in the margin (i.e. - # tabs and spaces both count as margin, but are *not* - # considered equivalent) - def test_dedent_preserve_margin_tabs(self) -> None: - text = " hello there\n\thow are you?" - self.assertUnchanged(text) - - # same effect even if we have 8 spaces - text = " hello there\n\thow are you?" - self.assertUnchanged(text) - - # dedent() only removes whitespace that can be uniformly removed! - text = "\thello there\n\thow are you?" - expect = "hello there\nhow are you?" - self.assertEqual(expect, dedent(text)) - - text = " \thello there\n \thow are you?" - self.assertEqual(expect, dedent(text)) - - text = " \t hello there\n \t how are you?" - self.assertEqual(expect, dedent(text)) - - text = " \thello there\n \t how are you?" - expect = "hello there\n how are you?" - self.assertEqual(expect, dedent(text)) - - -def test_main() -> None: - support.run_unittest(WrapTestCase, - LongWordTestCase, - IndentTestCases, - DedentTestCase) - -if __name__ == '__main__': - test_main() diff --git a/test-data/stdlib-samples/3.2/test/tf_inherit_check.py b/test-data/stdlib-samples/3.2/test/tf_inherit_check.py deleted file mode 100644 index 92ebd95e5236..000000000000 --- a/test-data/stdlib-samples/3.2/test/tf_inherit_check.py +++ /dev/null @@ -1,25 +0,0 @@ -# Helper script for test_tempfile.py. argv[2] is the number of a file -# descriptor which should _not_ be open. Check this by attempting to -# write to it -- if we succeed, something is wrong. - -import sys -import os - -verbose = (sys.argv[1] == 'v') -try: - fd = int(sys.argv[2]) - - try: - os.write(fd, b"blat") - except os.error: - # Success -- could not write to fd. - sys.exit(0) - else: - if verbose: - sys.stderr.write("fd %d is open in child" % fd) - sys.exit(1) - -except Exception: - if verbose: - raise - sys.exit(1) diff --git a/test-data/stdlib-samples/3.2/textwrap.py b/test-data/stdlib-samples/3.2/textwrap.py deleted file mode 100644 index a6d026699704..000000000000 --- a/test-data/stdlib-samples/3.2/textwrap.py +++ /dev/null @@ -1,391 +0,0 @@ -"""Text wrapping and filling. -""" - -# Copyright (C) 1999-2001 Gregory P. Ward. -# Copyright (C) 2002, 2003 Python Software Foundation. -# Written by Greg Ward - -import string, re - -from typing import Dict, List, Any - -__all__ = ['TextWrapper', 'wrap', 'fill', 'dedent'] - -# Hardcode the recognized whitespace characters to the US-ASCII -# whitespace characters. The main reason for doing this is that in -# ISO-8859-1, 0xa0 is non-breaking whitespace, so in certain locales -# that character winds up in string.whitespace. Respecting -# string.whitespace in those cases would 1) make textwrap treat 0xa0 the -# same as any other whitespace char, which is clearly wrong (it's a -# *non-breaking* space), 2) possibly cause problems with Unicode, -# since 0xa0 is not in range(128). -_whitespace = '\t\n\x0b\x0c\r ' - -class TextWrapper: - """ - Object for wrapping/filling text. The public interface consists of - the wrap() and fill() methods; the other methods are just there for - subclasses to override in order to tweak the default behaviour. - If you want to completely replace the main wrapping algorithm, - you'll probably have to override _wrap_chunks(). - - Several instance attributes control various aspects of wrapping: - width (default: 70) - the maximum width of wrapped lines (unless break_long_words - is false) - initial_indent (default: "") - string that will be prepended to the first line of wrapped - output. Counts towards the line's width. - subsequent_indent (default: "") - string that will be prepended to all lines save the first - of wrapped output; also counts towards each line's width. - expand_tabs (default: true) - Expand tabs in input text to spaces before further processing. - Each tab will become 1 .. 8 spaces, depending on its position in - its line. If false, each tab is treated as a single character. - replace_whitespace (default: true) - Replace all whitespace characters in the input text by spaces - after tab expansion. Note that if expand_tabs is false and - replace_whitespace is true, every tab will be converted to a - single space! - fix_sentence_endings (default: false) - Ensure that sentence-ending punctuation is always followed - by two spaces. Off by default because the algorithm is - (unavoidably) imperfect. - break_long_words (default: true) - Break words longer than 'width'. If false, those words will not - be broken, and some lines might be longer than 'width'. - break_on_hyphens (default: true) - Allow breaking hyphenated words. If true, wrapping will occur - preferably on whitespaces and right after hyphens part of - compound words. - drop_whitespace (default: true) - Drop leading and trailing whitespace from lines. - """ - - unicode_whitespace_trans = {} # type: Dict[int, int] - uspace = ord(' ') - for x in _whitespace: - unicode_whitespace_trans[ord(x)] = uspace - - # This funky little regex is just the trick for splitting - # text up into word-wrappable chunks. E.g. - # "Hello there -- you goof-ball, use the -b option!" - # splits into - # Hello/ /there/ /--/ /you/ /goof-/ball,/ /use/ /the/ /-b/ /option! - # (after stripping out empty strings). - wordsep_re = re.compile( - r'(\s+|' # any whitespace - r'[^\s\w]*\w+[^0-9\W]-(?=\w+[^0-9\W])|' # hyphenated words - r'(?<=[\w\!\"\'\&\.\,\?])-{2,}(?=\w))') # em-dash - - # This less funky little regex just split on recognized spaces. E.g. - # "Hello there -- you goof-ball, use the -b option!" - # splits into - # Hello/ /there/ /--/ /you/ /goof-ball,/ /use/ /the/ /-b/ /option!/ - wordsep_simple_re = re.compile(r'(\s+)') - - # XXX this is not locale- or charset-aware -- string.lowercase - # is US-ASCII only (and therefore English-only) - sentence_end_re = re.compile(r'[a-z]' # lowercase letter - r'[\.\!\?]' # sentence-ending punct. - r'[\"\']?' # optional end-of-quote - r'\Z') # end of chunk - - - def __init__(self, - width: int = 70, - initial_indent: str = "", - subsequent_indent: str = "", - expand_tabs: bool = True, - replace_whitespace: bool = True, - fix_sentence_endings: bool = False, - break_long_words: bool = True, - drop_whitespace: bool = True, - break_on_hyphens: bool = True) -> None: - self.width = width - self.initial_indent = initial_indent - self.subsequent_indent = subsequent_indent - self.expand_tabs = expand_tabs - self.replace_whitespace = replace_whitespace - self.fix_sentence_endings = fix_sentence_endings - self.break_long_words = break_long_words - self.drop_whitespace = drop_whitespace - self.break_on_hyphens = break_on_hyphens - - - # -- Private methods ----------------------------------------------- - # (possibly useful for subclasses to override) - - def _munge_whitespace(self, text: str) -> str: - """_munge_whitespace(text : string) -> string - - Munge whitespace in text: expand tabs and convert all other - whitespace characters to spaces. Eg. " foo\tbar\n\nbaz" - becomes " foo bar baz". - """ - if self.expand_tabs: - text = text.expandtabs() - if self.replace_whitespace: - text = text.translate(self.unicode_whitespace_trans) - return text - - - def _split(self, text: str) -> List[str]: - """_split(text : string) -> [string] - - Split the text to wrap into indivisible chunks. Chunks are - not quite the same as words; see _wrap_chunks() for full - details. As an example, the text - Look, goof-ball -- use the -b option! - breaks into the following chunks: - 'Look,', ' ', 'goof-', 'ball', ' ', '--', ' ', - 'use', ' ', 'the', ' ', '-b', ' ', 'option!' - if break_on_hyphens is True, or in: - 'Look,', ' ', 'goof-ball', ' ', '--', ' ', - 'use', ' ', 'the', ' ', '-b', ' ', option!' - otherwise. - """ - if self.break_on_hyphens is True: - chunks = self.wordsep_re.split(text) - else: - chunks = self.wordsep_simple_re.split(text) - chunks = [c for c in chunks if c] - return chunks - - def _fix_sentence_endings(self, chunks: List[str]) -> None: - """_fix_sentence_endings(chunks : [string]) - - Correct for sentence endings buried in 'chunks'. Eg. when the - original text contains "... foo.\nBar ...", munge_whitespace() - and split() will convert that to [..., "foo.", " ", "Bar", ...] - which has one too few spaces; this method simply changes the one - space to two. - """ - i = 0 - patsearch = self.sentence_end_re.search - while i < len(chunks)-1: - if chunks[i+1] == " " and patsearch(chunks[i]): - chunks[i+1] = " " - i += 2 - else: - i += 1 - - def _handle_long_word(self, reversed_chunks: List[str], - cur_line: List[str], cur_len: int, - width: int) -> None: - """_handle_long_word(chunks : [string], - cur_line : [string], - cur_len : int, width : int) - - Handle a chunk of text (most likely a word, not whitespace) that - is too long to fit in any line. - """ - # Figure out when indent is larger than the specified width, and make - # sure at least one character is stripped off on every pass - if width < 1: - space_left = 1 - else: - space_left = width - cur_len - - # If we're allowed to break long words, then do so: put as much - # of the next chunk onto the current line as will fit. - if self.break_long_words: - cur_line.append(reversed_chunks[-1][:space_left]) - reversed_chunks[-1] = reversed_chunks[-1][space_left:] - - # Otherwise, we have to preserve the long word intact. Only add - # it to the current line if there's nothing already there -- - # that minimizes how much we violate the width constraint. - elif not cur_line: - cur_line.append(reversed_chunks.pop()) - - # If we're not allowed to break long words, and there's already - # text on the current line, do nothing. Next time through the - # main loop of _wrap_chunks(), we'll wind up here again, but - # cur_len will be zero, so the next line will be entirely - # devoted to the long word that we can't handle right now. - - def _wrap_chunks(self, chunks: List[str]) -> List[str]: - """_wrap_chunks(chunks : [string]) -> [string] - - Wrap a sequence of text chunks and return a list of lines of - length 'self.width' or less. (If 'break_long_words' is false, - some lines may be longer than this.) Chunks correspond roughly - to words and the whitespace between them: each chunk is - indivisible (modulo 'break_long_words'), but a line break can - come between any two chunks. Chunks should not have internal - whitespace; ie. a chunk is either all whitespace or a "word". - Whitespace chunks will be removed from the beginning and end of - lines, but apart from that whitespace is preserved. - """ - lines = [] # type: List[str] - if self.width <= 0: - raise ValueError("invalid width %r (must be > 0)" % self.width) - - # Arrange in reverse order so items can be efficiently popped - # from a stack of chucks. - chunks.reverse() - - while chunks: - - # Start the list of chunks that will make up the current line. - # cur_len is just the length of all the chunks in cur_line. - cur_line = [] # type: List[str] - cur_len = 0 - - # Figure out which static string will prefix this line. - if lines: - indent = self.subsequent_indent - else: - indent = self.initial_indent - - # Maximum width for this line. - width = self.width - len(indent) - - # First chunk on line is whitespace -- drop it, unless this - # is the very beginning of the text (ie. no lines started yet). - if self.drop_whitespace and chunks[-1].strip() == '' and lines: - del chunks[-1] - - while chunks: - l = len(chunks[-1]) - - # Can at least squeeze this chunk onto the current line. - if cur_len + l <= width: - cur_line.append(chunks.pop()) - cur_len += l - - # Nope, this line is full. - else: - break - - # The current line is full, and the next chunk is too big to - # fit on *any* line (not just this one). - if chunks and len(chunks[-1]) > width: - self._handle_long_word(chunks, cur_line, cur_len, width) - - # If the last chunk on this line is all whitespace, drop it. - if self.drop_whitespace and cur_line and cur_line[-1].strip() == '': - del cur_line[-1] - - # Convert current line back to a string and store it in list - # of all lines (return value). - if cur_line: - lines.append(indent + ''.join(cur_line)) - - return lines - - - # -- Public interface ---------------------------------------------- - - def wrap(self, text: str) -> List[str]: - """wrap(text : string) -> [string] - - Reformat the single paragraph in 'text' so it fits in lines of - no more than 'self.width' columns, and return a list of wrapped - lines. Tabs in 'text' are expanded with string.expandtabs(), - and all other whitespace characters (including newline) are - converted to space. - """ - text = self._munge_whitespace(text) - chunks = self._split(text) - if self.fix_sentence_endings: - self._fix_sentence_endings(chunks) - return self._wrap_chunks(chunks) - - def fill(self, text: str) -> str: - """fill(text : string) -> string - - Reformat the single paragraph in 'text' to fit in lines of no - more than 'self.width' columns, and return a new string - containing the entire wrapped paragraph. - """ - return "\n".join(self.wrap(text)) - - -# -- Convenience interface --------------------------------------------- - -def wrap(text: str, width: int = 70, **kwargs: Any) -> List[str]: - """Wrap a single paragraph of text, returning a list of wrapped lines. - - Reformat the single paragraph in 'text' so it fits in lines of no - more than 'width' columns, and return a list of wrapped lines. By - default, tabs in 'text' are expanded with string.expandtabs(), and - all other whitespace characters (including newline) are converted to - space. See TextWrapper class for available keyword args to customize - wrapping behaviour. - """ - w = TextWrapper(width=width, **kwargs) - return w.wrap(text) - -def fill(text: str, width: int = 70, **kwargs: Any) -> str: - """Fill a single paragraph of text, returning a new string. - - Reformat the single paragraph in 'text' to fit in lines of no more - than 'width' columns, and return a new string containing the entire - wrapped paragraph. As with wrap(), tabs are expanded and other - whitespace characters converted to space. See TextWrapper class for - available keyword args to customize wrapping behaviour. - """ - w = TextWrapper(width=width, **kwargs) - return w.fill(text) - - -# -- Loosely related functionality ------------------------------------- - -_whitespace_only_re = re.compile('^[ \t]+$', re.MULTILINE) -_leading_whitespace_re = re.compile('(^[ \t]*)(?:[^ \t\n])', re.MULTILINE) - -def dedent(text: str) -> str: - """Remove any common leading whitespace from every line in `text`. - - This can be used to make triple-quoted strings line up with the left - edge of the display, while still presenting them in the source code - in indented form. - - Note that tabs and spaces are both treated as whitespace, but they - are not equal: the lines " hello" and "\thello" are - considered to have no common leading whitespace. (This behaviour is - new in Python 2.5; older versions of this module incorrectly - expanded tabs before searching for common leading whitespace.) - """ - # Look for the longest leading string of spaces and tabs common to - # all lines. - margin = None # type: str - text = _whitespace_only_re.sub('', text) - indents = _leading_whitespace_re.findall(text) - for indent in indents: - if margin is None: - margin = indent - - # Current line more deeply indented than previous winner: - # no change (previous winner is still on top). - elif indent.startswith(margin): - pass - - # Current line consistent with and no deeper than previous winner: - # it's the new winner. - elif margin.startswith(indent): - margin = indent - - # Current line and previous winner have no common whitespace: - # there is no margin. - else: - margin = "" - break - - # sanity check (testing/debugging only) - if 0 and margin: - for line in text.split("\n"): - assert not line or line.startswith(margin), \ - "line = %r, margin = %r" % (line, margin) - - if margin: - text = re.sub(r'(?m)^' + margin, '', text) - return text - -if __name__ == "__main__": - #print dedent("\tfoo\n\tbar") - #print dedent(" \thello there\n \t how are you?") - print(dedent("Hello there.\n This is indented."))