From c1618ca6bb8d5b6a80d24126fa4125662d1638d7 Mon Sep 17 00:00:00 2001 From: Dmitry Trofimov Date: Wed, 24 Oct 2018 16:37:50 +0200 Subject: [PATCH] Update Python mock SDK 3.4 -> 3.7 --- python/testData/MockSdk3.4/Lib/abc.py | 18 - .../MockSdk3.4/Lib/collections/__main__.py | 38 - .../Lib/_collections_abc.py | 190 ++- python/testData/MockSdk3.7/Lib/abc.py | 170 +++ .../Lib/collections/__init__.py | 438 ++++--- .../Lib/collections/abc.py | 0 .../Lib/datetime.py | 970 ++++++++++----- python/testData/MockSdk3.7/Lib/genericpath.py | 151 +++ .../{MockSdk3.4 => MockSdk3.7}/Lib/io.py | 9 +- python/testData/MockSdk3.7/Lib/ntpath.py | 669 ++++++++++ .../{MockSdk3.4 => MockSdk3.7}/Lib/numbers.py | 7 +- python/testData/MockSdk3.7/Lib/os.py | 1078 ++++++++++++++++ .../{MockSdk3.4 => MockSdk3.7}/Lib/re.py | 178 ++- .../python3.4 => MockSdk3.7/bin/python3.7} | 0 .../python_stubs/_io.py | 420 ++++--- .../python_stubs/builtins.py | 1082 +++++++++-------- .../python_stubs/sys.py | 204 +++- .../jetbrains/python/fixtures/PyTestCase.java | 2 +- 18 files changed, 4237 insertions(+), 1387 deletions(-) delete mode 100644 python/testData/MockSdk3.4/Lib/abc.py delete mode 100644 python/testData/MockSdk3.4/Lib/collections/__main__.py rename python/testData/{MockSdk3.4 => MockSdk3.7}/Lib/_collections_abc.py (84%) create mode 100644 python/testData/MockSdk3.7/Lib/abc.py rename python/testData/{MockSdk3.4 => MockSdk3.7}/Lib/collections/__init__.py (77%) rename python/testData/{MockSdk3.4 => MockSdk3.7}/Lib/collections/abc.py (100%) rename python/testData/{MockSdk3.4 => MockSdk3.7}/Lib/datetime.py (72%) create mode 100644 python/testData/MockSdk3.7/Lib/genericpath.py rename python/testData/{MockSdk3.4 => MockSdk3.7}/Lib/io.py (95%) create mode 100644 python/testData/MockSdk3.7/Lib/ntpath.py rename python/testData/{MockSdk3.4 => MockSdk3.7}/Lib/numbers.py (97%) create mode 100644 python/testData/MockSdk3.7/Lib/os.py rename python/testData/{MockSdk3.4 => MockSdk3.7}/Lib/re.py (76%) rename python/testData/{MockSdk3.4/bin/python3.4 => MockSdk3.7/bin/python3.7} (100%) rename python/testData/{MockSdk3.4 => MockSdk3.7}/python_stubs/_io.py (80%) rename python/testData/{MockSdk3.4 => MockSdk3.7}/python_stubs/builtins.py (87%) rename python/testData/{MockSdk3.4 => MockSdk3.7}/python_stubs/sys.py (76%) diff --git a/python/testData/MockSdk3.4/Lib/abc.py b/python/testData/MockSdk3.4/Lib/abc.py deleted file mode 100644 index 79850b48538d..000000000000 --- a/python/testData/MockSdk3.4/Lib/abc.py +++ /dev/null @@ -1,18 +0,0 @@ -# Stubs - -class object: - pass - -class ABCMeta: - pass - - -class ABC(metaclass=ABCMeta): - """Helper class that provides a standard way to create an ABC using - inheritance. - """ - pass - - -def abstractmethod(foo): - pass \ No newline at end of file diff --git a/python/testData/MockSdk3.4/Lib/collections/__main__.py b/python/testData/MockSdk3.4/Lib/collections/__main__.py deleted file mode 100644 index 763e38e0c40c..000000000000 --- a/python/testData/MockSdk3.4/Lib/collections/__main__.py +++ /dev/null @@ -1,38 +0,0 @@ -################################################################################ -### Simple tests -################################################################################ - -# verify that instances can be pickled -from collections import namedtuple -from pickle import loads, dumps -Point = namedtuple('Point', 'x, y', True) -p = Point(x=10, y=20) -assert p == loads(dumps(p)) - -# test and demonstrate ability to override methods -class Point(namedtuple('Point', 'x y')): - __slots__ = () - @property - def hypot(self): - return (self.x ** 2 + self.y ** 2) ** 0.5 - def __str__(self): - return 'Point: x=%6.3f y=%6.3f hypot=%6.3f' % (self.x, self.y, self.hypot) - -for p in Point(3, 4), Point(14, 5/7.): - print (p) - -class Point(namedtuple('Point', 'x y')): - 'Point class with optimized _make() and _replace() without error-checking' - __slots__ = () - _make = classmethod(tuple.__new__) - def _replace(self, _map=map, **kwds): - return self._make(_map(kwds.get, ('x', 'y'), self)) - -print(Point(11, 22)._replace(x=100)) - -Point3D = namedtuple('Point3D', Point._fields + ('z',)) -print(Point3D.__doc__) - -import doctest, collections -TestResults = namedtuple('TestResults', 'failed attempted') -print(TestResults(*doctest.testmod(collections))) diff --git a/python/testData/MockSdk3.4/Lib/_collections_abc.py b/python/testData/MockSdk3.7/Lib/_collections_abc.py similarity index 84% rename from python/testData/MockSdk3.4/Lib/_collections_abc.py rename to python/testData/MockSdk3.7/Lib/_collections_abc.py index 0d0eadd30fed..dbe30dff1fe1 100644 --- a/python/testData/MockSdk3.4/Lib/_collections_abc.py +++ b/python/testData/MockSdk3.7/Lib/_collections_abc.py @@ -9,9 +9,10 @@ Unit tests are in test_collections. from abc import ABCMeta, abstractmethod import sys -__all__ = ["Awaitable", "Coroutine", "AsyncIterable", "AsyncIterator", - "Hashable", "Iterable", "Iterator", "Generator", - "Sized", "Container", "Callable", +__all__ = ["Awaitable", "Coroutine", + "AsyncIterable", "AsyncIterator", "AsyncGenerator", + "Hashable", "Iterable", "Iterator", "Generator", "Reversible", + "Sized", "Container", "Callable", "Collection", "Set", "MutableSet", "Mapping", "MutableMapping", "MappingView", "KeysView", "ItemsView", "ValuesView", @@ -29,8 +30,8 @@ __name__ = "collections.abc" # so that they will pass tests like: # it = iter(somebytearray) # assert isinstance(it, Iterable) -# Note: in other implementations, these types many not be distinct -# and they make have their own implementation specific types that +# Note: in other implementations, these types might not be distinct +# and they may have their own implementation specific types that # are not included on this list. bytes_iterator = type(iter(b'')) bytearray_iterator = type(iter(bytearray())) @@ -41,6 +42,7 @@ dict_itemiterator = type(iter({}.items())) list_iterator = type(iter([])) list_reverseiterator = type(iter(reversed([]))) range_iterator = type(iter(range(0))) +longrange_iterator = type(iter(range(1 << 1000))) set_iterator = type(iter(set())) str_iterator = type(iter("")) tuple_iterator = type(iter(())) @@ -58,10 +60,27 @@ _coro = _coro() coroutine = type(_coro) _coro.close() # Prevent ResourceWarning del _coro +## asynchronous generator ## +async def _ag(): yield +_ag = _ag() +async_generator = type(_ag) +del _ag ### ONE-TRICK PONIES ### +def _check_methods(C, *methods): + mro = C.__mro__ + for method in methods: + for B in mro: + if method in B.__dict__: + if B.__dict__[method] is None: + return NotImplemented + break + else: + return NotImplemented + return True + class Hashable(metaclass=ABCMeta): __slots__ = () @@ -73,11 +92,7 @@ class Hashable(metaclass=ABCMeta): @classmethod def __subclasshook__(cls, C): if cls is Hashable: - for B in C.__mro__: - if "__hash__" in B.__dict__: - if B.__dict__["__hash__"]: - return True - break + return _check_methods(C, "__hash__") return NotImplemented @@ -92,11 +107,7 @@ class Awaitable(metaclass=ABCMeta): @classmethod def __subclasshook__(cls, C): if cls is Awaitable: - for B in C.__mro__: - if "__await__" in B.__dict__: - if B.__dict__["__await__"]: - return True - break + return _check_methods(C, "__await__") return NotImplemented @@ -137,14 +148,7 @@ class Coroutine(Awaitable): @classmethod def __subclasshook__(cls, C): if cls is Coroutine: - mro = C.__mro__ - for method in ('__await__', 'send', 'throw', 'close'): - for base in mro: - if method in base.__dict__: - break - else: - return NotImplemented - return True + return _check_methods(C, '__await__', 'send', 'throw', 'close') return NotImplemented @@ -162,8 +166,7 @@ class AsyncIterable(metaclass=ABCMeta): @classmethod def __subclasshook__(cls, C): if cls is AsyncIterable: - if any("__aiter__" in B.__dict__ for B in C.__mro__): - return True + return _check_methods(C, "__aiter__") return NotImplemented @@ -182,12 +185,61 @@ class AsyncIterator(AsyncIterable): @classmethod def __subclasshook__(cls, C): if cls is AsyncIterator: - if (any("__anext__" in B.__dict__ for B in C.__mro__) and - any("__aiter__" in B.__dict__ for B in C.__mro__)): - return True + return _check_methods(C, "__anext__", "__aiter__") return NotImplemented +class AsyncGenerator(AsyncIterator): + + __slots__ = () + + async def __anext__(self): + """Return the next item from the asynchronous generator. + When exhausted, raise StopAsyncIteration. + """ + return await self.asend(None) + + @abstractmethod + async def asend(self, value): + """Send a value into the asynchronous generator. + Return next yielded value or raise StopAsyncIteration. + """ + raise StopAsyncIteration + + @abstractmethod + async def athrow(self, typ, val=None, tb=None): + """Raise an exception in the asynchronous generator. + Return next yielded value or raise StopAsyncIteration. + """ + if val is None: + if tb is None: + raise typ + val = typ() + if tb is not None: + val = val.with_traceback(tb) + raise val + + async def aclose(self): + """Raise GeneratorExit inside coroutine. + """ + try: + await self.athrow(GeneratorExit) + except (GeneratorExit, StopAsyncIteration): + pass + else: + raise RuntimeError("asynchronous generator ignored GeneratorExit") + + @classmethod + def __subclasshook__(cls, C): + if cls is AsyncGenerator: + return _check_methods(C, '__aiter__', '__anext__', + 'asend', 'athrow', 'aclose') + return NotImplemented + + +AsyncGenerator.register(async_generator) + + class Iterable(metaclass=ABCMeta): __slots__ = () @@ -200,8 +252,7 @@ class Iterable(metaclass=ABCMeta): @classmethod def __subclasshook__(cls, C): if cls is Iterable: - if any("__iter__" in B.__dict__ for B in C.__mro__): - return True + return _check_methods(C, "__iter__") return NotImplemented @@ -220,9 +271,7 @@ class Iterator(Iterable): @classmethod def __subclasshook__(cls, C): if cls is Iterator: - if (any("__next__" in B.__dict__ for B in C.__mro__) and - any("__iter__" in B.__dict__ for B in C.__mro__)): - return True + return _check_methods(C, '__iter__', '__next__') return NotImplemented Iterator.register(bytes_iterator) @@ -234,12 +283,29 @@ Iterator.register(dict_itemiterator) Iterator.register(list_iterator) Iterator.register(list_reverseiterator) Iterator.register(range_iterator) +Iterator.register(longrange_iterator) Iterator.register(set_iterator) Iterator.register(str_iterator) Iterator.register(tuple_iterator) Iterator.register(zip_iterator) +class Reversible(Iterable): + + __slots__ = () + + @abstractmethod + def __reversed__(self): + while False: + yield None + + @classmethod + def __subclasshook__(cls, C): + if cls is Reversible: + return _check_methods(C, "__reversed__", "__iter__") + return NotImplemented + + class Generator(Iterator): __slots__ = () @@ -283,17 +349,10 @@ class Generator(Iterator): @classmethod def __subclasshook__(cls, C): if cls is Generator: - mro = C.__mro__ - for method in ('__iter__', '__next__', 'send', 'throw', 'close'): - for base in mro: - if method in base.__dict__: - break - else: - return NotImplemented - return True + return _check_methods(C, '__iter__', '__next__', + 'send', 'throw', 'close') return NotImplemented - Generator.register(generator) @@ -308,8 +367,7 @@ class Sized(metaclass=ABCMeta): @classmethod def __subclasshook__(cls, C): if cls is Sized: - if any("__len__" in B.__dict__ for B in C.__mro__): - return True + return _check_methods(C, "__len__") return NotImplemented @@ -324,10 +382,18 @@ class Container(metaclass=ABCMeta): @classmethod def __subclasshook__(cls, C): if cls is Container: - if any("__contains__" in B.__dict__ for B in C.__mro__): - return True + return _check_methods(C, "__contains__") return NotImplemented +class Collection(Sized, Iterable, Container): + + __slots__ = () + + @classmethod + def __subclasshook__(cls, C): + if cls is Collection: + return _check_methods(C, "__len__", "__iter__", "__contains__") + return NotImplemented class Callable(metaclass=ABCMeta): @@ -340,15 +406,14 @@ class Callable(metaclass=ABCMeta): @classmethod def __subclasshook__(cls, C): if cls is Callable: - if any("__call__" in B.__dict__ for B in C.__mro__): - return True + return _check_methods(C, "__call__") return NotImplemented ### SETS ### -class Set(Sized, Iterable, Container): +class Set(Collection): """A set is a finite, iterable container. @@ -524,7 +589,7 @@ class MutableSet(Set): try: value = next(it) except StopIteration: - raise KeyError + raise KeyError from None self.discard(value) return value @@ -573,7 +638,7 @@ MutableSet.register(set) ### MAPPINGS ### -class Mapping(Sized, Iterable, Container): +class Mapping(Collection): __slots__ = () @@ -621,6 +686,8 @@ class Mapping(Sized, Iterable, Container): return NotImplemented return dict(self.items()) == dict(other.items()) + __reversed__ = None + Mapping.register(mappingproxy) @@ -670,7 +737,7 @@ class ItemsView(MappingView, Set): except KeyError: return False else: - return v == value + return v is value or v == value def __iter__(self): for key in self._mapping: @@ -679,13 +746,14 @@ class ItemsView(MappingView, Set): ItemsView.register(dict_items) -class ValuesView(MappingView): +class ValuesView(MappingView, Collection): __slots__ = () def __contains__(self, value): for key in self._mapping: - if value == self._mapping[key]: + v = self._mapping[key] + if v is value or v == value: return True return False @@ -740,7 +808,7 @@ class MutableMapping(Mapping): try: key = next(iter(self)) except StopIteration: - raise KeyError + raise KeyError from None value = self[key] del self[key] return key, value @@ -794,7 +862,7 @@ MutableMapping.register(dict) ### SEQUENCES ### -class Sequence(Sized, Iterable, Container): +class Sequence(Reversible, Collection): """All the operations on a read-only sequence. @@ -820,7 +888,7 @@ class Sequence(Sized, Iterable, Container): def __contains__(self, value): for v in self: - if v == value: + if v is value or v == value: return True return False @@ -831,6 +899,9 @@ class Sequence(Sized, Iterable, Container): def index(self, value, start=0, stop=None): '''S.index(value, [start, [stop]]) -> integer -- return first index of value. Raises ValueError if the value is not present. + + Supporting start and stop arguments is optional, but + recommended. ''' if start is not None and start < 0: start = max(len(self) + start, 0) @@ -840,7 +911,8 @@ class Sequence(Sized, Iterable, Container): i = start while stop is None or i < stop: try: - if self[i] == value: + v = self[i] + if v is value or v == value: return i except IndexError: break @@ -849,7 +921,7 @@ class Sequence(Sized, Iterable, Container): def count(self, value): 'S.count(value) -> integer -- return number of occurrences of value' - return sum(1 for v in self if v == value) + return sum(1 for v in self if v is value or v == value) Sequence.register(tuple) Sequence.register(str) diff --git a/python/testData/MockSdk3.7/Lib/abc.py b/python/testData/MockSdk3.7/Lib/abc.py new file mode 100644 index 000000000000..7094141277ae --- /dev/null +++ b/python/testData/MockSdk3.7/Lib/abc.py @@ -0,0 +1,170 @@ +# Copyright 2007 Google, Inc. All Rights Reserved. +# Licensed to PSF under a Contributor Agreement. + +"""Abstract Base Classes (ABCs) according to PEP 3119.""" + + +def abstractmethod(funcobj): + """A decorator indicating abstract methods. + + Requires that the metaclass is ABCMeta or derived from it. A + class that has a metaclass derived from ABCMeta cannot be + instantiated unless all of its abstract methods are overridden. + The abstract methods can be called using any of the normal + 'super' call mechanisms. + + Usage: + + class C(metaclass=ABCMeta): + @abstractmethod + def my_abstract_method(self, ...): + ... + """ + funcobj.__isabstractmethod__ = True + return funcobj + + +class abstractclassmethod(classmethod): + """A decorator indicating abstract classmethods. + + Similar to abstractmethod. + + Usage: + + class C(metaclass=ABCMeta): + @abstractclassmethod + def my_abstract_classmethod(cls, ...): + ... + + 'abstractclassmethod' is deprecated. Use 'classmethod' with + 'abstractmethod' instead. + """ + + __isabstractmethod__ = True + + def __init__(self, callable): + callable.__isabstractmethod__ = True + super().__init__(callable) + + +class abstractstaticmethod(staticmethod): + """A decorator indicating abstract staticmethods. + + Similar to abstractmethod. + + Usage: + + class C(metaclass=ABCMeta): + @abstractstaticmethod + def my_abstract_staticmethod(...): + ... + + 'abstractstaticmethod' is deprecated. Use 'staticmethod' with + 'abstractmethod' instead. + """ + + __isabstractmethod__ = True + + def __init__(self, callable): + callable.__isabstractmethod__ = True + super().__init__(callable) + + +class abstractproperty(property): + """A decorator indicating abstract properties. + + Requires that the metaclass is ABCMeta or derived from it. A + class that has a metaclass derived from ABCMeta cannot be + instantiated unless all of its abstract properties are overridden. + The abstract properties can be called using any of the normal + 'super' call mechanisms. + + Usage: + + class C(metaclass=ABCMeta): + @abstractproperty + def my_abstract_property(self): + ... + + This defines a read-only property; you can also define a read-write + abstract property using the 'long' form of property declaration: + + class C(metaclass=ABCMeta): + def getx(self): ... + def setx(self, value): ... + x = abstractproperty(getx, setx) + + 'abstractproperty' is deprecated. Use 'property' with 'abstractmethod' + instead. + """ + + __isabstractmethod__ = True + + +try: + from _abc import (get_cache_token, _abc_init, _abc_register, + _abc_instancecheck, _abc_subclasscheck, _get_dump, + _reset_registry, _reset_caches) +except ImportError: + from _py_abc import ABCMeta, get_cache_token + ABCMeta.__module__ = 'abc' +else: + class ABCMeta(type): + """Metaclass for defining Abstract Base Classes (ABCs). + + Use this metaclass to create an ABC. An ABC can be subclassed + directly, and then acts as a mix-in class. You can also register + unrelated concrete classes (even built-in classes) and unrelated + ABCs as 'virtual subclasses' -- these and their descendants will + be considered subclasses of the registering ABC by the built-in + issubclass() function, but the registering ABC won't show up in + their MRO (Method Resolution Order) nor will method + implementations defined by the registering ABC be callable (not + even via super()). + """ + def __new__(mcls, name, bases, namespace, **kwargs): + cls = super().__new__(mcls, name, bases, namespace, **kwargs) + _abc_init(cls) + return cls + + def register(cls, subclass): + """Register a virtual subclass of an ABC. + + Returns the subclass, to allow usage as a class decorator. + """ + return _abc_register(cls, subclass) + + def __instancecheck__(cls, instance): + """Override for isinstance(instance, cls).""" + return _abc_instancecheck(cls, instance) + + def __subclasscheck__(cls, subclass): + """Override for issubclass(subclass, cls).""" + return _abc_subclasscheck(cls, subclass) + + def _dump_registry(cls, file=None): + """Debug helper to print the ABC registry.""" + print(f"Class: {cls.__module__}.{cls.__qualname__}", file=file) + print(f"Inv. counter: {get_cache_token()}", file=file) + (_abc_registry, _abc_cache, _abc_negative_cache, + _abc_negative_cache_version) = _get_dump(cls) + print(f"_abc_registry: {_abc_registry!r}", file=file) + print(f"_abc_cache: {_abc_cache!r}", file=file) + print(f"_abc_negative_cache: {_abc_negative_cache!r}", file=file) + print(f"_abc_negative_cache_version: {_abc_negative_cache_version!r}", + file=file) + + def _abc_registry_clear(cls): + """Clear the registry (for debugging or testing).""" + _reset_registry(cls) + + def _abc_caches_clear(cls): + """Clear the caches (for debugging or testing).""" + _reset_caches(cls) + + +class ABC(metaclass=ABCMeta): + """Helper class that provides a standard way to create an ABC using + inheritance. + """ + __slots__ = () diff --git a/python/testData/MockSdk3.4/Lib/collections/__init__.py b/python/testData/MockSdk3.7/Lib/collections/__init__.py similarity index 77% rename from python/testData/MockSdk3.4/Lib/collections/__init__.py rename to python/testData/MockSdk3.7/Lib/collections/__init__.py index d6deb6a7b262..9a753db71cae 100644 --- a/python/testData/MockSdk3.4/Lib/collections/__init__.py +++ b/python/testData/MockSdk3.7/Lib/collections/__init__.py @@ -1,13 +1,23 @@ +'''This module implements specialized container datatypes providing +alternatives to Python's general purpose built-in containers, dict, +list, set, and tuple. + +* namedtuple factory function for creating tuple subclasses with named fields +* deque list-like container with fast appends and pops on either end +* ChainMap dict-like class for creating a single view of multiple mappings +* Counter dict subclass for counting hashable objects +* OrderedDict dict subclass that remembers the order entries were added +* defaultdict dict subclass that calls a factory function to supply missing values +* UserDict wrapper around dictionary objects for easier dict subclassing +* UserList wrapper around list objects for easier list subclassing +* UserString wrapper around string objects for easier string subclassing + +''' + __all__ = ['deque', 'defaultdict', 'namedtuple', 'UserDict', 'UserList', 'UserString', 'Counter', 'OrderedDict', 'ChainMap'] -# For backwards compatibility, continue to make the collections ABCs -# available through the collections module. -from _collections_abc import * import _collections_abc -__all__ += _collections_abc.__all__ - -from _collections import deque, defaultdict from operator import itemgetter as _itemgetter, eq as _eq from keyword import iskeyword as _iskeyword import sys as _sys @@ -16,10 +26,55 @@ from _weakref import proxy as _proxy from itertools import repeat as _repeat, chain as _chain, starmap as _starmap from reprlib import recursive_repr as _recursive_repr +try: + from _collections import deque +except ImportError: + pass +else: + _collections_abc.MutableSequence.register(deque) + +try: + from _collections import defaultdict +except ImportError: + pass + + +def __getattr__(name): + # For backwards compatibility, continue to make the collections ABCs + # through Python 3.6 available through the collections module. + # Note, no new collections ABCs were added in Python 3.7 + if name in _collections_abc.__all__: + obj = getattr(_collections_abc, name) + import warnings + warnings.warn("Using or importing the ABCs from 'collections' instead " + "of from 'collections.abc' is deprecated, " + "and in 3.8 it will stop working", + DeprecationWarning, stacklevel=2) + globals()[name] = obj + return obj + raise AttributeError(f'module {__name__!r} has no attribute {name!r}') + ################################################################################ ### OrderedDict ################################################################################ +class _OrderedDictKeysView(_collections_abc.KeysView): + + def __reversed__(self): + yield from reversed(self._mapping) + +class _OrderedDictItemsView(_collections_abc.ItemsView): + + def __reversed__(self): + for key in reversed(self._mapping): + yield (key, self._mapping[key]) + +class _OrderedDictValuesView(_collections_abc.ValuesView): + + def __reversed__(self): + for key in reversed(self._mapping): + yield self._mapping[key] + class _Link(object): __slots__ = 'prev', 'next', 'key', '__weakref__' @@ -38,12 +93,14 @@ class OrderedDict(dict): # Individual links are kept alive by the hard reference in self.__map. # Those hard references disappear when a key is deleted from an OrderedDict. - def __init__(self, *args, **kwds): + def __init__(*args, **kwds): '''Initialize an ordered dictionary. The signature is the same as - regular dictionaries, but keyword arguments are not recommended because - their insertion order is arbitrary. - + regular dictionaries. Keyword argument order is preserved. ''' + if not args: + raise TypeError("descriptor '__init__' of 'OrderedDict' object " + "needs an argument") + self, *args = args if len(args) > 1: raise TypeError('expected at most 1 arguments, got %d' % len(args)) try: @@ -79,6 +136,8 @@ class OrderedDict(dict): link_next = link.next link_prev.next = link_next link_next.prev = link_prev + link.prev = None + link.next = None def __iter__(self): 'od.__iter__() <==> iter(od)' @@ -106,9 +165,9 @@ class OrderedDict(dict): dict.clear(self) def popitem(self, last=True): - '''od.popitem() -> (k, v), return and remove a (key, value) pair. - Pairs are returned in LIFO order if last is true or FIFO order if false. + '''Remove and return a (key, value) pair from the dictionary. + Pairs are returned in LIFO order if last is true or FIFO order if false. ''' if not self: raise KeyError('dictionary is empty') @@ -129,15 +188,14 @@ class OrderedDict(dict): return key, value def move_to_end(self, key, last=True): - '''Move an existing element to the end (or beginning if last==False). - - Raises KeyError if the element does not exist. - When last=True, acts like a fast version of self[key]=self.pop(key). + '''Move an existing element to the end (or beginning if last is false). + Raise KeyError if the element does not exist. ''' link = self.__map[key] link_prev = link.prev link_next = link.next + soft_link = link_next.prev link_prev.next = link_next link_next.prev = link_prev root = self.__root @@ -145,12 +203,14 @@ class OrderedDict(dict): last = root.prev link.prev = last link.next = root - last.next = root.prev = link + root.prev = soft_link + last.next = link else: first = root.next link.prev = root link.next = first - root.next = first.prev = link + first.prev = soft_link + root.next = link def __sizeof__(self): sizeof = _sys.getsizeof @@ -161,11 +221,21 @@ class OrderedDict(dict): size += sizeof(self.__root) * n # proxy objects return size - update = __update = MutableMapping.update - keys = MutableMapping.keys - values = MutableMapping.values - items = MutableMapping.items - __ne__ = MutableMapping.__ne__ + update = __update = _collections_abc.MutableMapping.update + + def keys(self): + "D.keys() -> a set-like object providing a view on D's keys" + return _OrderedDictKeysView(self) + + def items(self): + "D.items() -> a set-like object providing a view on D's items" + return _OrderedDictItemsView(self) + + def values(self): + "D.values() -> an object providing a view on D's values" + return _OrderedDictValuesView(self) + + __ne__ = _collections_abc.MutableMapping.__ne__ __marker = object() @@ -184,7 +254,10 @@ class OrderedDict(dict): return default def setdefault(self, key, default=None): - 'od.setdefault(k[,d]) -> od.get(k,d), also set od[k]=d if k not in od' + '''Insert key with a value of default if key is not in the dictionary. + + Return the value for key if key is in the dictionary, else default. + ''' if key in self: return self[key] self[key] = default @@ -210,9 +283,7 @@ class OrderedDict(dict): @classmethod def fromkeys(cls, iterable, value=None): - '''OD.fromkeys(S[, v]) -> New ordered dictionary with keys from S. - If not specified, the value defaults to None. - + '''Create a new ordered dictionary with keys from iterable and values set to value. ''' self = cls() for key in iterable: @@ -229,72 +300,20 @@ class OrderedDict(dict): return dict.__eq__(self, other) +try: + from _collections import OrderedDict +except ImportError: + # Leave the pure Python version in place. + pass + + ################################################################################ ### namedtuple ################################################################################ -_class_template = """\ -from builtins import property as _property, tuple as _tuple -from operator import itemgetter as _itemgetter -from collections import OrderedDict +_nt_itemgetters = {} -class {typename}(tuple): - '{typename}({arg_list})' - - __slots__ = () - - _fields = {field_names!r} - - def __new__(_cls, {arg_list}): - 'Create new instance of {typename}({arg_list})' - return _tuple.__new__(_cls, ({arg_list})) - - @classmethod - def _make(cls, iterable, new=tuple.__new__, len=len): - 'Make a new {typename} object from a sequence or iterable' - result = new(cls, iterable) - if len(result) != {num_fields:d}: - raise TypeError('Expected {num_fields:d} arguments, got %d' % len(result)) - return result - - def _replace(_self, **kwds): - 'Return a new {typename} object replacing specified fields with new values' - result = _self._make(map(kwds.pop, {field_names!r}, _self)) - if kwds: - raise ValueError('Got unexpected field names: %r' % list(kwds)) - return result - - def __repr__(self): - 'Return a nicely formatted representation string' - return self.__class__.__name__ + '({repr_fmt})' % self - - @property - def __dict__(self): - 'A new OrderedDict mapping field names to their values' - return OrderedDict(zip(self._fields, self)) - - def _asdict(self): - 'Return a new OrderedDict which maps field names to their values.' - return self.__dict__ - - def __getnewargs__(self): - 'Return self as a plain tuple. Used by copy and pickle.' - return tuple(self) - - def __getstate__(self): - 'Exclude the OrderedDict from pickling' - return None - -{field_defs} -""" - -_repr_template = '{name}=%r' - -_field_template = '''\ - {name} = _property(_itemgetter({index:d}), doc='Alias for field number {index:d}') -''' - -def namedtuple(typename, field_names, verbose=False, rename=False): +def namedtuple(typename, field_names, *, rename=False, defaults=None, module=None): """Returns a new subclass of tuple with named fields. >>> Point = namedtuple('Point', ['x', 'y']) @@ -306,7 +325,7 @@ def namedtuple(typename, field_names, verbose=False, rename=False): >>> x, y = p # unpack like a regular tuple >>> x, y (11, 22) - >>> p.x + p.y # fields also accessable by name + >>> p.x + p.y # fields also accessible by name 33 >>> d = p._asdict() # convert to a dictionary >>> d['x'] @@ -323,6 +342,8 @@ def namedtuple(typename, field_names, verbose=False, rename=False): if isinstance(field_names, str): field_names = field_names.replace(',', ' ').split() field_names = list(map(str, field_names)) + typename = _sys.intern(str(typename)) + if rename: seen = set() for index, name in enumerate(field_names): @@ -330,53 +351,130 @@ def namedtuple(typename, field_names, verbose=False, rename=False): or _iskeyword(name) or name.startswith('_') or name in seen): - field_names[index] = '_%d' % index + field_names[index] = f'_{index}' seen.add(name) + for name in [typename] + field_names: + if type(name) is not str: + raise TypeError('Type names and field names must be strings') if not name.isidentifier(): raise ValueError('Type names and field names must be valid ' - 'identifiers: %r' % name) + f'identifiers: {name!r}') if _iskeyword(name): raise ValueError('Type names and field names cannot be a ' - 'keyword: %r' % name) + f'keyword: {name!r}') + seen = set() for name in field_names: if name.startswith('_') and not rename: raise ValueError('Field names cannot start with an underscore: ' - '%r' % name) + f'{name!r}') if name in seen: - raise ValueError('Encountered duplicate field name: %r' % name) + raise ValueError(f'Encountered duplicate field name: {name!r}') seen.add(name) - # Fill-in the class template - class_definition = _class_template.format( - typename = typename, - field_names = tuple(field_names), - num_fields = len(field_names), - arg_list = repr(tuple(field_names)).replace("'", "")[1:-1], - repr_fmt = ', '.join(_repr_template.format(name=name) - for name in field_names), - field_defs = '\n'.join(_field_template.format(index=index, name=name) - for index, name in enumerate(field_names)) - ) + field_defaults = {} + if defaults is not None: + defaults = tuple(defaults) + if len(defaults) > len(field_names): + raise TypeError('Got more default values than field names') + field_defaults = dict(reversed(list(zip(reversed(field_names), + reversed(defaults))))) - # Execute the template string in a temporary namespace and support - # tracing utilities by setting a value for frame.f_globals['__name__'] - namespace = dict(__name__='namedtuple_%s' % typename) - exec(class_definition, namespace) - result = namespace[typename] - result._source = class_definition - if verbose: - print(result._source) + # Variables used in the methods and docstrings + field_names = tuple(map(_sys.intern, field_names)) + num_fields = len(field_names) + arg_list = repr(field_names).replace("'", "")[1:-1] + repr_fmt = '(' + ', '.join(f'{name}=%r' for name in field_names) + ')' + tuple_new = tuple.__new__ + _len = len + + # Create all the named tuple methods to be added to the class namespace + + s = f'def __new__(_cls, {arg_list}): return _tuple_new(_cls, ({arg_list}))' + namespace = {'_tuple_new': tuple_new, '__name__': f'namedtuple_{typename}'} + # Note: exec() has the side-effect of interning the field names + exec(s, namespace) + __new__ = namespace['__new__'] + __new__.__doc__ = f'Create new instance of {typename}({arg_list})' + if defaults is not None: + __new__.__defaults__ = defaults + + @classmethod + def _make(cls, iterable): + result = tuple_new(cls, iterable) + if _len(result) != num_fields: + raise TypeError(f'Expected {num_fields} arguments, got {len(result)}') + return result + + _make.__func__.__doc__ = (f'Make a new {typename} object from a sequence ' + 'or iterable') + + def _replace(_self, **kwds): + result = _self._make(map(kwds.pop, field_names, _self)) + if kwds: + raise ValueError(f'Got unexpected field names: {list(kwds)!r}') + return result + + _replace.__doc__ = (f'Return a new {typename} object replacing specified ' + 'fields with new values') + + def __repr__(self): + 'Return a nicely formatted representation string' + return self.__class__.__name__ + repr_fmt % self + + def _asdict(self): + 'Return a new OrderedDict which maps field names to their values.' + return OrderedDict(zip(self._fields, self)) + + def __getnewargs__(self): + 'Return self as a plain tuple. Used by copy and pickle.' + return tuple(self) + + # Modify function metadata to help with introspection and debugging + + for method in (__new__, _make.__func__, _replace, + __repr__, _asdict, __getnewargs__): + method.__qualname__ = f'{typename}.{method.__name__}' + + # Build-up the class namespace dictionary + # and use type() to build the result class + class_namespace = { + '__doc__': f'{typename}({arg_list})', + '__slots__': (), + '_fields': field_names, + '_fields_defaults': field_defaults, + '__new__': __new__, + '_make': _make, + '_replace': _replace, + '__repr__': __repr__, + '_asdict': _asdict, + '__getnewargs__': __getnewargs__, + } + cache = _nt_itemgetters + for index, name in enumerate(field_names): + try: + itemgetter_object, doc = cache[index] + except KeyError: + itemgetter_object = _itemgetter(index) + doc = f'Alias for field number {index}' + cache[index] = itemgetter_object, doc + class_namespace[name] = property(itemgetter_object, doc=doc) + + result = type(typename, (tuple,), class_namespace) # For pickling to work, the __module__ variable needs to be set to the frame # where the named tuple is created. Bypass this step in environments where # sys._getframe is not defined (Jython for example) or sys._getframe is not - # defined for arguments greater than 0 (IronPython). - try: - result.__module__ = _sys._getframe(1).f_globals.get('__name__', '__main__') - except (AttributeError, ValueError): - pass + # defined for arguments greater than 0 (IronPython), or where the user has + # specified a particular module. + if module is None: + try: + module = _sys._getframe(1).f_globals.get('__name__', '__main__') + except (AttributeError, ValueError): + pass + if module is not None: + result.__module__ = module return result @@ -447,7 +545,7 @@ class Counter(dict): # http://code.activestate.com/recipes/259174/ # Knuth, TAOCP Vol. II section 4.6.3 - def __init__(self, iterable=None, **kwds): + def __init__(*args, **kwds): '''Create a new, empty Counter object. And if given, count elements from an input iterable. Or, initialize the count from another mapping of elements to their counts. @@ -458,8 +556,14 @@ class Counter(dict): >>> c = Counter(a=4, b=2) # a new counter from keyword args ''' - super().__init__() - self.update(iterable, **kwds) + if not args: + raise TypeError("descriptor '__init__' of 'Counter' object " + "needs an argument") + self, *args = args + if len(args) > 1: + raise TypeError('expected at most 1 arguments, got %d' % len(args)) + super(Counter, self).__init__() + self.update(*args, **kwds) def __missing__(self, key): 'The count of elements not in the Counter is zero.' @@ -510,7 +614,7 @@ class Counter(dict): raise NotImplementedError( 'Counter.fromkeys() is undefined. Use Counter(iterable) instead.') - def update(self, iterable=None, **kwds): + def update(*args, **kwds): '''Like dict.update() but add counts instead of replacing them. Source can be an iterable, a dictionary, or another Counter instance. @@ -530,20 +634,27 @@ class Counter(dict): # contexts. Instead, we implement straight-addition. Both the inputs # and outputs are allowed to contain zero and negative counts. + if not args: + raise TypeError("descriptor 'update' of 'Counter' object " + "needs an argument") + self, *args = args + if len(args) > 1: + raise TypeError('expected at most 1 arguments, got %d' % len(args)) + iterable = args[0] if args else None if iterable is not None: - if isinstance(iterable, Mapping): + if isinstance(iterable, _collections_abc.Mapping): if self: self_get = self.get for elem, count in iterable.items(): self[elem] = count + self_get(elem, 0) else: - super().update(iterable) # fast path when counter is empty + super(Counter, self).update(iterable) # fast path when counter is empty else: _count_elements(self, iterable) if kwds: self.update(kwds) - def subtract(self, iterable=None, **kwds): + def subtract(*args, **kwds): '''Like dict.update() but subtracts counts instead of replacing them. Counts can be reduced below zero. Both the inputs and outputs are allowed to contain zero and negative counts. @@ -559,9 +670,16 @@ class Counter(dict): -1 ''' + if not args: + raise TypeError("descriptor 'subtract' of 'Counter' object " + "needs an argument") + self, *args = args + if len(args) > 1: + raise TypeError('expected at most 1 arguments, got %d' % len(args)) + iterable = args[0] if args else None if iterable is not None: self_get = self.get - if isinstance(iterable, Mapping): + if isinstance(iterable, _collections_abc.Mapping): for elem, count in iterable.items(): self[elem] = self_get(elem, 0) - count else: @@ -678,14 +796,22 @@ class Counter(dict): def __pos__(self): 'Adds an empty counter, effectively stripping negative and zero counts' - return self + Counter() + result = Counter() + for elem, count in self.items(): + if count > 0: + result[elem] = count + return result def __neg__(self): '''Subtracts from an empty counter. Strips positive and zero counts, and flips the sign on negative counts. ''' - return Counter() - self + result = Counter() + for elem, count in self.items(): + if count < 0: + result[elem] = 0 - count + return result def _keep_positive(self): '''Internal method to strip elements with a negative or zero count''' @@ -752,15 +878,16 @@ class Counter(dict): ######################################################################## -### ChainMap (helper for configparser and string.Template) +### ChainMap ######################################################################## -class ChainMap(MutableMapping): +class ChainMap(_collections_abc.MutableMapping): ''' A ChainMap groups multiple dicts (or other mappings) together to create a single, updateable view. The underlying mappings are stored in a list. That list is public and can - accessed or updated using the *maps* attribute. There is no other state. + be accessed or updated using the *maps* attribute. There is no other + state. Lookups search the underlying mappings successively until a key is found. In contrast, writes, updates, and deletions only operate on the first @@ -793,7 +920,10 @@ class ChainMap(MutableMapping): return len(set().union(*self.maps)) # reuses stored hash values if possible def __iter__(self): - return iter(set().union(*self.maps)) + d = {} + for mapping in reversed(self.maps): + d.update(mapping) # reuses stored hash values if possible + return iter(d) def __contains__(self, key): return any(key in m for m in self.maps) @@ -818,9 +948,8 @@ class ChainMap(MutableMapping): __copy__ = copy def new_child(self, m=None): # like Django's Context.push() - ''' - New ChainMap with a new map followed by all previous maps. If no - map is provided, an empty dict is used. + '''New ChainMap with a new map followed by all previous maps. + If no map is provided, an empty dict is used. ''' if m is None: m = {} @@ -863,10 +992,25 @@ class ChainMap(MutableMapping): ### UserDict ################################################################################ -class UserDict(MutableMapping): +class UserDict(_collections_abc.MutableMapping): # Start by filling-out the abstract methods - def __init__(self, dict=None, **kwargs): + def __init__(*args, **kwargs): + if not args: + raise TypeError("descriptor '__init__' of 'UserDict' object " + "needs an argument") + self, *args = args + if len(args) > 1: + raise TypeError('expected at most 1 arguments, got %d' % len(args)) + if args: + dict = args[0] + elif 'dict' in kwargs: + dict = kwargs.pop('dict') + import warnings + warnings.warn("Passing 'dict' as keyword argument is deprecated", + DeprecationWarning, stacklevel=2) + else: + dict = None self.data = {} if dict is not None: self.update(dict) @@ -915,7 +1059,7 @@ class UserDict(MutableMapping): ### UserList ################################################################################ -class UserList(MutableSequence): +class UserList(_collections_abc.MutableSequence): """A more or less complete user-defined wrapper around list objects.""" def __init__(self, initlist=None): self.data = [] @@ -931,7 +1075,6 @@ class UserList(MutableSequence): def __lt__(self, other): return self.data < self.__cast(other) def __le__(self, other): return self.data <= self.__cast(other) def __eq__(self, other): return self.data == self.__cast(other) - def __ne__(self, other): return self.data != self.__cast(other) def __gt__(self, other): return self.data > self.__cast(other) def __ge__(self, other): return self.data >= self.__cast(other) def __cast(self, other): @@ -989,7 +1132,7 @@ class UserList(MutableSequence): ### UserString ################################################################################ -class UserString(Sequence): +class UserString(_collections_abc.Sequence): def __init__(self, seq): if isinstance(seq, str): self.data = seq @@ -1003,15 +1146,13 @@ class UserString(Sequence): def __float__(self): return float(self.data) def __complex__(self): return complex(self.data) def __hash__(self): return hash(self.data) + def __getnewargs__(self): + return (self.data[:],) def __eq__(self, string): if isinstance(string, UserString): return self.data == string.data return self.data == string - def __ne__(self, string): - if isinstance(string, UserString): - return self.data != string.data - return self.data != string def __lt__(self, string): if isinstance(string, UserString): return self.data < string.data @@ -1051,9 +1192,13 @@ class UserString(Sequence): __rmul__ = __mul__ def __mod__(self, args): return self.__class__(self.data % args) + def __rmod__(self, format): + return self.__class__(format % args) # the following methods are defined in alphabetical order: def capitalize(self): return self.__class__(self.data.capitalize()) + def casefold(self): + return self.__class__(self.data.casefold()) def center(self, width, *args): return self.__class__(self.data.center(width, *args)) def count(self, sub, start=0, end=_sys.maxsize): @@ -1076,15 +1221,19 @@ class UserString(Sequence): return self.data.find(sub, start, end) def format(self, *args, **kwds): return self.data.format(*args, **kwds) + def format_map(self, mapping): + return self.data.format_map(mapping) def index(self, sub, start=0, end=_sys.maxsize): return self.data.index(sub, start, end) def isalpha(self): return self.data.isalpha() def isalnum(self): return self.data.isalnum() + def isascii(self): return self.data.isascii() def isdecimal(self): return self.data.isdecimal() def isdigit(self): return self.data.isdigit() def isidentifier(self): return self.data.isidentifier() def islower(self): return self.data.islower() def isnumeric(self): return self.data.isnumeric() + def isprintable(self): return self.data.isprintable() def isspace(self): return self.data.isspace() def istitle(self): return self.data.istitle() def isupper(self): return self.data.isupper() @@ -1093,6 +1242,7 @@ class UserString(Sequence): return self.__class__(self.data.ljust(width, *args)) def lower(self): return self.__class__(self.data.lower()) def lstrip(self, chars=None): return self.__class__(self.data.lstrip(chars)) + maketrans = str.maketrans def partition(self, sep): return self.data.partition(sep) def replace(self, old, new, maxsplit=-1): diff --git a/python/testData/MockSdk3.4/Lib/collections/abc.py b/python/testData/MockSdk3.7/Lib/collections/abc.py similarity index 100% rename from python/testData/MockSdk3.4/Lib/collections/abc.py rename to python/testData/MockSdk3.7/Lib/collections/abc.py diff --git a/python/testData/MockSdk3.4/Lib/datetime.py b/python/testData/MockSdk3.7/Lib/datetime.py similarity index 72% rename from python/testData/MockSdk3.4/Lib/datetime.py rename to python/testData/MockSdk3.7/Lib/datetime.py index 1789714034c9..12a0f1489feb 100644 --- a/python/testData/MockSdk3.4/Lib/datetime.py +++ b/python/testData/MockSdk3.7/Lib/datetime.py @@ -6,13 +6,14 @@ time zone and DST data sources. import time as _time import math as _math +import sys def _cmp(x, y): return 0 if x == y else 1 if x > y else -1 MINYEAR = 1 MAXYEAR = 9999 -_MAXORDINAL = 3652059 # date.max.toordinal() +_MAXORDINAL = 3652059 # date.max.toordinal() # Utility functions, adapted from Python's Demo/classes/Dates.py, which # also assumes the current Gregorian calendar indefinitely extended in @@ -26,7 +27,7 @@ _MAXORDINAL = 3652059 # date.max.toordinal() # -1 is a placeholder for indexing purposes. _DAYS_IN_MONTH = [-1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] -_DAYS_BEFORE_MONTH = [-1] # -1 is a placeholder for indexing purposes. +_DAYS_BEFORE_MONTH = [-1] # -1 is a placeholder for indexing purposes. dbm = 0 for dim in _DAYS_IN_MONTH[1:]: _DAYS_BEFORE_MONTH.append(dbm) @@ -152,19 +153,51 @@ def _build_struct_time(y, m, d, hh, mm, ss, dstflag): dnum = _days_before_month(y, m) + d return _time.struct_time((y, m, d, hh, mm, ss, wday, dnum, dstflag)) -def _format_time(hh, mm, ss, us): - # Skip trailing microseconds when us==0. - result = "%02d:%02d:%02d" % (hh, mm, ss) - if us: - result += ".%06d" % us - return result +def _format_time(hh, mm, ss, us, timespec='auto'): + specs = { + 'hours': '{:02d}', + 'minutes': '{:02d}:{:02d}', + 'seconds': '{:02d}:{:02d}:{:02d}', + 'milliseconds': '{:02d}:{:02d}:{:02d}.{:03d}', + 'microseconds': '{:02d}:{:02d}:{:02d}.{:06d}' + } + + if timespec == 'auto': + # Skip trailing microseconds when us==0. + timespec = 'microseconds' if us else 'seconds' + elif timespec == 'milliseconds': + us //= 1000 + try: + fmt = specs[timespec] + except KeyError: + raise ValueError('Unknown timespec value') + else: + return fmt.format(hh, mm, ss, us) + +def _format_offset(off): + s = '' + if off is not None: + if off.days < 0: + sign = "-" + off = -off + else: + sign = "+" + hh, mm = divmod(off, timedelta(hours=1)) + mm, ss = divmod(mm, timedelta(minutes=1)) + s += "%s%02d:%02d" % (sign, hh, mm) + if ss or ss.microseconds: + s += ":%02d" % ss.seconds + + if ss.microseconds: + s += '.%06d' % ss.microseconds + return s # Correctly substitute for %z and %Z escapes in strftime formats. def _wrap_strftime(object, format, timetuple): # Don't call utcoffset() or tzname() unless actually needed. - freplace = None # the string to use for %f - zreplace = None # the string to use for %z - Zreplace = None # the string to use for %Z + freplace = None # the string to use for %f + zreplace = None # the string to use for %z + Zreplace = None # the string to use for %Z # Scan format for %z and %Z escapes, replacing as needed. newformat = [] @@ -192,10 +225,16 @@ def _wrap_strftime(object, format, timetuple): if offset.days < 0: offset = -offset sign = '-' - h, m = divmod(offset, timedelta(hours=1)) - assert not m % timedelta(minutes=1), "whole minute" - m //= timedelta(minutes=1) - zreplace = '%c%02d%02d' % (sign, h, m) + h, rest = divmod(offset, timedelta(hours=1)) + m, rest = divmod(rest, timedelta(minutes=1)) + s = rest.seconds + u = offset.microseconds + if u: + zreplace = '%c%02d%02d%02d.%06d' % (sign, h, m, s, u) + elif s: + zreplace = '%c%02d%02d%02d' % (sign, h, m, s) + else: + zreplace = '%c%02d%02d' % (sign, h, m) assert '%' not in zreplace newformat.append(zreplace) elif ch == 'Z': @@ -217,10 +256,101 @@ def _wrap_strftime(object, format, timetuple): newformat = "".join(newformat) return _time.strftime(newformat, timetuple) -def _call_tzinfo_method(tzinfo, methname, tzinfoarg): - if tzinfo is None: - return None - return getattr(tzinfo, methname)(tzinfoarg) +# Helpers for parsing the result of isoformat() +def _parse_isoformat_date(dtstr): + # It is assumed that this function will only be called with a + # string of length exactly 10, and (though this is not used) ASCII-only + year = int(dtstr[0:4]) + if dtstr[4] != '-': + raise ValueError('Invalid date separator: %s' % dtstr[4]) + + month = int(dtstr[5:7]) + + if dtstr[7] != '-': + raise ValueError('Invalid date separator') + + day = int(dtstr[8:10]) + + return [year, month, day] + +def _parse_hh_mm_ss_ff(tstr): + # Parses things of the form HH[:MM[:SS[.fff[fff]]]] + len_str = len(tstr) + + time_comps = [0, 0, 0, 0] + pos = 0 + for comp in range(0, 3): + if (len_str - pos) < 2: + raise ValueError('Incomplete time component') + + time_comps[comp] = int(tstr[pos:pos+2]) + + pos += 2 + next_char = tstr[pos:pos+1] + + if not next_char or comp >= 2: + break + + if next_char != ':': + raise ValueError('Invalid time separator: %c' % next_char) + + pos += 1 + + if pos < len_str: + if tstr[pos] != '.': + raise ValueError('Invalid microsecond component') + else: + pos += 1 + + len_remainder = len_str - pos + if len_remainder not in (3, 6): + raise ValueError('Invalid microsecond component') + + time_comps[3] = int(tstr[pos:]) + if len_remainder == 3: + time_comps[3] *= 1000 + + return time_comps + +def _parse_isoformat_time(tstr): + # Format supported is HH[:MM[:SS[.fff[fff]]]][+HH:MM[:SS[.ffffff]]] + len_str = len(tstr) + if len_str < 2: + raise ValueError('Isoformat time too short') + + # This is equivalent to re.search('[+-]', tstr), but faster + tz_pos = (tstr.find('-') + 1 or tstr.find('+') + 1) + timestr = tstr[:tz_pos-1] if tz_pos > 0 else tstr + + time_comps = _parse_hh_mm_ss_ff(timestr) + + tzi = None + if tz_pos > 0: + tzstr = tstr[tz_pos:] + + # Valid time zone strings are: + # HH:MM len: 5 + # HH:MM:SS len: 8 + # HH:MM:SS.ffffff len: 15 + + if len(tzstr) not in (5, 8, 15): + raise ValueError('Malformed time zone string') + + tz_comps = _parse_hh_mm_ss_ff(tzstr) + if all(x == 0 for x in tz_comps): + tzi = timezone.utc + else: + tzsign = -1 if tstr[tz_pos - 1] == '-' else 1 + + td = timedelta(hours=tz_comps[0], minutes=tz_comps[1], + seconds=tz_comps[2], microseconds=tz_comps[3]) + + tzi = timezone(tzsign * td) + + time_comps.append(tzi) + + return time_comps + # Just raise TypeError if the arg isn't None or a string. def _check_tzname(name): @@ -232,7 +362,7 @@ def _check_tzname(name): # offset is what it returned. # If offset isn't None or timedelta, raises TypeError. # If offset is None, returns None. -# Else offset is checked for being in range, and a whole # of minutes. +# Else offset is checked for being in range. # If it is, its integer value is returned. Else ValueError is raised. def _check_utc_offset(name, offset): assert name in ("utcoffset", "dst") @@ -241,17 +371,32 @@ def _check_utc_offset(name, offset): if not isinstance(offset, timedelta): raise TypeError("tzinfo.%s() must return None " "or timedelta, not '%s'" % (name, type(offset))) - if offset % timedelta(minutes=1) or offset.microseconds: - raise ValueError("tzinfo.%s() must return a whole number " - "of minutes, got %s" % (name, offset)) if not -timedelta(1) < offset < timedelta(1): - raise ValueError("%s()=%s, must be must be strictly between" - " -timedelta(hours=24) and timedelta(hours=24)" - % (name, offset)) + raise ValueError("%s()=%s, must be strictly between " + "-timedelta(hours=24) and timedelta(hours=24)" % + (name, offset)) + +def _check_int_field(value): + if isinstance(value, int): + return value + if not isinstance(value, float): + try: + value = value.__int__() + except AttributeError: + pass + else: + if isinstance(value, int): + return value + raise TypeError('__int__ returned non-int (type %s)' % + type(value).__name__) + raise TypeError('an integer is required (got type %s)' % + type(value).__name__) + raise TypeError('integer argument expected, got float') def _check_date_fields(year, month, day): - if not isinstance(year, int): - raise TypeError('int expected') + year = _check_int_field(year) + month = _check_int_field(month) + day = _check_int_field(day) if not MINYEAR <= year <= MAXYEAR: raise ValueError('year must be in %d..%d' % (MINYEAR, MAXYEAR), year) if not 1 <= month <= 12: @@ -259,10 +404,13 @@ def _check_date_fields(year, month, day): dim = _days_in_month(year, month) if not 1 <= day <= dim: raise ValueError('day must be in 1..%d' % dim, day) + return year, month, day -def _check_time_fields(hour, minute, second, microsecond): - if not isinstance(hour, int): - raise TypeError('int expected') +def _check_time_fields(hour, minute, second, microsecond, fold): + hour = _check_int_field(hour) + minute = _check_int_field(minute) + second = _check_int_field(second) + microsecond = _check_int_field(microsecond) if not 0 <= hour <= 23: raise ValueError('hour must be in 0..23', hour) if not 0 <= minute <= 59: @@ -271,6 +419,9 @@ def _check_time_fields(hour, minute, second, microsecond): raise ValueError('second must be in 0..59', second) if not 0 <= microsecond <= 999999: raise ValueError('microsecond must be in 0..999999', microsecond) + if fold not in (0, 1): + raise ValueError('fold must be either 0 or 1', fold) + return hour, minute, second, microsecond, fold def _check_tzinfo_arg(tz): if tz is not None and not isinstance(tz, tzinfo): @@ -280,6 +431,26 @@ def _cmperror(x, y): raise TypeError("can't compare '%s' to '%s'" % ( type(x).__name__, type(y).__name__)) +def _divide_and_round(a, b): + """divide a by b and round result to the nearest integer + + When the ratio is exactly half-way between two integers, + the even integer is returned. + """ + # Based on the reference implementation for divmod_near + # in Objects/longobject.c. + q, r = divmod(a, b) + # round up if either r / b > 0.5, or r / b == 0.5 and q is odd. + # The expression r / b > 0.5 is equivalent to 2 * r > b if b is + # positive, 2 * r < b if b negative. + r *= 2 + greater_than_half = r > b if b > 0 else r < b + if greater_than_half or r == b and q % 2 == 1: + q += 1 + + return q + + class timedelta: """Represent the difference between two datetime objects. @@ -297,7 +468,7 @@ class timedelta: Representation: (days, seconds, microseconds). Why? Because I felt like it. """ - __slots__ = '_days', '_seconds', '_microseconds' + __slots__ = '_days', '_seconds', '_microseconds', '_hashcode' def __new__(cls, days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0): @@ -363,38 +534,26 @@ class timedelta: # secondsfrac isn't referenced again if isinstance(microseconds, float): - microseconds += usdouble - microseconds = round(microseconds, 0) - seconds, microseconds = divmod(microseconds, 1e6) - assert microseconds == int(microseconds) - assert seconds == int(seconds) - days, seconds = divmod(seconds, 24.*3600.) - assert days == int(days) - assert seconds == int(seconds) - d += int(days) - s += int(seconds) # can't overflow - assert isinstance(s, int) - assert abs(s) <= 3 * 24 * 3600 - else: + microseconds = round(microseconds + usdouble) seconds, microseconds = divmod(microseconds, 1000000) days, seconds = divmod(seconds, 24*3600) d += days - s += int(seconds) # can't overflow - assert isinstance(s, int) - assert abs(s) <= 3 * 24 * 3600 - microseconds = float(microseconds) - microseconds += usdouble - microseconds = round(microseconds, 0) + s += seconds + else: + microseconds = int(microseconds) + seconds, microseconds = divmod(microseconds, 1000000) + days, seconds = divmod(seconds, 24*3600) + d += days + s += seconds + microseconds = round(microseconds + usdouble) + assert isinstance(s, int) + assert isinstance(microseconds, int) assert abs(s) <= 3 * 24 * 3600 assert abs(microseconds) < 3.1e6 # Just a little bit of carrying possible for microseconds and seconds. - assert isinstance(microseconds, float) - assert int(microseconds) == microseconds - us = int(microseconds) - seconds, us = divmod(us, 1000000) - s += seconds # cant't overflow - assert isinstance(s, int) + seconds, us = divmod(microseconds, 1000000) + s += seconds days, s = divmod(s, 24*3600) d += days @@ -402,27 +561,29 @@ class timedelta: assert isinstance(s, int) and 0 <= s < 24*3600 assert isinstance(us, int) and 0 <= us < 1000000 - self = object.__new__(cls) - - self._days = d - self._seconds = s - self._microseconds = us if abs(d) > 999999999: raise OverflowError("timedelta # of days is too large: %d" % d) + self = object.__new__(cls) + self._days = d + self._seconds = s + self._microseconds = us + self._hashcode = -1 return self def __repr__(self): - if self._microseconds: - return "%s(%d, %d, %d)" % ('datetime.' + self.__class__.__name__, - self._days, - self._seconds, - self._microseconds) + args = [] + if self._days: + args.append("days=%d" % self._days) if self._seconds: - return "%s(%d, %d)" % ('datetime.' + self.__class__.__name__, - self._days, - self._seconds) - return "%s(%d)" % ('datetime.' + self.__class__.__name__, self._days) + args.append("seconds=%d" % self._seconds) + if self._microseconds: + args.append("microseconds=%d" % self._microseconds) + if not args: + args.append('0') + return "%s.%s(%s)" % (self.__class__.__module__, + self.__class__.__qualname__, + ', '.join(args)) def __str__(self): mm, ss = divmod(self._seconds, 60) @@ -438,7 +599,7 @@ class timedelta: def total_seconds(self): """Total seconds in the duration.""" - return ((self.days * 86400 + self.seconds)*10**6 + + return ((self.days * 86400 + self.seconds) * 10**6 + self.microseconds) / 10**6 # Read-only field accessors @@ -506,8 +667,9 @@ class timedelta: self._seconds * other, self._microseconds * other) if isinstance(other, float): + usec = self._to_microseconds() a, b = other.as_integer_ratio() - return self * a / b + return timedelta(0, 0, _divide_and_round(usec * a, b)) return NotImplemented __rmul__ = __mul__ @@ -532,10 +694,10 @@ class timedelta: if isinstance(other, timedelta): return usec / other._to_microseconds() if isinstance(other, int): - return timedelta(0, 0, usec / other) + return timedelta(0, 0, _divide_and_round(usec, other)) if isinstance(other, float): a, b = other.as_integer_ratio() - return timedelta(0, 0, b * usec / a) + return timedelta(0, 0, _divide_and_round(b * usec, a)) def __mod__(self, other): if isinstance(other, timedelta): @@ -558,12 +720,6 @@ class timedelta: else: return False - def __ne__(self, other): - if isinstance(other, timedelta): - return self._cmp(other) != 0 - else: - return True - def __le__(self, other): if isinstance(other, timedelta): return self._cmp(other) <= 0 @@ -593,7 +749,9 @@ class timedelta: return _cmp(self._getstate(), other._getstate()) def __hash__(self): - return hash(self._getstate()) + if self._hashcode == -1: + self._hashcode = hash(self._getstate()) + return self._hashcode def __bool__(self): return (self._days != 0 or @@ -626,7 +784,7 @@ class date: Operators: __repr__, __str__ - __cmp__, __hash__ + __eq__, __le__, __lt__, __ge__, __gt__, __hash__ __add__, __radd__, __sub__ (add/radd only with timedelta arg) Methods: @@ -641,7 +799,7 @@ class date: Properties (readonly): year, month, day """ - __slots__ = '_year', '_month', '_day' + __slots__ = '_year', '_month', '_day', '_hashcode' def __new__(cls, year, month=None, day=None): """Constructor. @@ -650,17 +808,19 @@ class date: year, month, day (required, base 1) """ - if (isinstance(year, bytes) and len(year) == 4 and - 1 <= year[2] <= 12 and month is None): # Month is sane + if month is None and isinstance(year, bytes) and len(year) == 4 and \ + 1 <= year[2] <= 12: # Pickle support self = object.__new__(cls) self.__setstate(year) + self._hashcode = -1 return self - _check_date_fields(year, month, day) + year, month, day = _check_date_fields(year, month, day) self = object.__new__(cls) self._year = year self._month = month self._day = day + self._hashcode = -1 return self # Additional constructors @@ -679,7 +839,7 @@ class date: @classmethod def fromordinal(cls, n): - """Contruct a date from a proleptic Gregorian ordinal. + """Construct a date from a proleptic Gregorian ordinal. January 1 of year 1 is day 1. Only the year, month and day are non-zero in the result. @@ -687,6 +847,19 @@ class date: y, m, d = _ord2ymd(n) return cls(y, m, d) + @classmethod + def fromisoformat(cls, date_string): + """Construct a date from the output of date.isoformat().""" + if not isinstance(date_string, str): + raise TypeError('fromisoformat: argument must be str') + + try: + assert len(date_string) == 10 + return cls(*_parse_isoformat_date(date_string)) + except Exception: + raise ValueError('Invalid isoformat string: %s' % date_string) + + # Conversions to string def __repr__(self): @@ -700,10 +873,11 @@ class date: >>> repr(dt) 'datetime.datetime(2010, 1, 1, 0, 0, tzinfo=datetime.timezone.utc)' """ - return "%s(%d, %d, %d)" % ('datetime.' + self.__class__.__name__, - self._year, - self._month, - self._day) + return "%s.%s(%d, %d, %d)" % (self.__class__.__module__, + self.__class__.__qualname__, + self._year, + self._month, + self._day) # XXX These shouldn't depend on time.localtime(), because that # clips the usable dates to [1970 .. 2038). At least ctime() is # easily done without using strftime() -- that's better too because @@ -723,6 +897,8 @@ class date: return _wrap_strftime(self, fmt, self.timetuple()) def __format__(self, fmt): + if not isinstance(fmt, str): + raise TypeError("must be str, not %s" % type(fmt).__name__) if len(fmt) != 0: return self.strftime(fmt) return str(self) @@ -756,7 +932,8 @@ class date: """day (1-31)""" return self._day - # Standard conversions, __cmp__, __hash__ (and helpers) + # Standard conversions, __eq__, __le__, __lt__, __ge__, __gt__, + # __hash__ (and helpers) def timetuple(self): "Return local time tuple compatible with time.localtime()." @@ -779,8 +956,7 @@ class date: month = self._month if day is None: day = self._day - _check_date_fields(year, month, day) - return date(year, month, day) + return type(self)(year, month, day) # Comparisons of date objects with other. @@ -789,11 +965,6 @@ class date: return self._cmp(other) == 0 return NotImplemented - def __ne__(self, other): - if isinstance(other, date): - return self._cmp(other) != 0 - return NotImplemented - def __le__(self, other): if isinstance(other, date): return self._cmp(other) <= 0 @@ -822,7 +993,9 @@ class date: def __hash__(self): "Hash." - return hash(self._getstate()) + if self._hashcode == -1: + self._hashcode = hash(self._getstate()) + return self._hashcode # Computations @@ -869,6 +1042,7 @@ class date: ISO calendar algorithm taken from http://www.phys.uu.nl/~vgent/calendar/isocalendar.htm + (used with permission) """ year = self._year week1monday = _isoweek1monday(year) @@ -892,8 +1066,6 @@ class date: return bytes([yhi, ylo, self._month, self._day]), def __setstate(self, string): - if len(string) != 4 or not (1 <= string[2] <= 12): - raise TypeError("not enough arguments") yhi, ylo, self._month, self._day = string self._year = yhi * 256 + ylo @@ -906,22 +1078,24 @@ date.min = date(1, 1, 1) date.max = date(9999, 12, 31) date.resolution = timedelta(days=1) + class tzinfo: """Abstract base class for time zone info classes. Subclasses must override the name(), utcoffset() and dst() methods. """ __slots__ = () + def tzname(self, dt): "datetime -> string name of time zone." raise NotImplementedError("tzinfo subclass must override tzname()") def utcoffset(self, dt): - "datetime -> minutes east of UTC (negative for west of UTC)" + "datetime -> timedelta, positive for east of UTC, negative for west of UTC" raise NotImplementedError("tzinfo subclass must override utcoffset()") def dst(self, dt): - """datetime -> DST offset in minutes east of UTC. + """datetime -> DST offset as timedelta, positive for east of UTC. Return 0 if DST not in effect. utcoffset() must include the DST offset. @@ -985,7 +1159,7 @@ class time: Operators: __repr__, __str__ - __cmp__, __hash__ + __eq__, __le__, __lt__, __ge__, __gt__, __hash__ Methods: @@ -996,10 +1170,11 @@ class time: dst() Properties (readonly): - hour, minute, second, microsecond, tzinfo + hour, minute, second, microsecond, tzinfo, fold """ + __slots__ = '_hour', '_minute', '_second', '_microsecond', '_tzinfo', '_hashcode', '_fold' - def __new__(cls, hour=0, minute=0, second=0, microsecond=0, tzinfo=None): + def __new__(cls, hour=0, minute=0, second=0, microsecond=0, tzinfo=None, *, fold=0): """Constructor. Arguments: @@ -1007,19 +1182,25 @@ class time: hour, minute (required) second, microsecond (default to zero) tzinfo (default to None) + fold (keyword only, default to zero) """ - self = object.__new__(cls) - if isinstance(hour, bytes) and len(hour) == 6: + if isinstance(hour, bytes) and len(hour) == 6 and hour[0]&0x7F < 24: # Pickle support + self = object.__new__(cls) self.__setstate(hour, minute or None) + self._hashcode = -1 return self + hour, minute, second, microsecond, fold = _check_time_fields( + hour, minute, second, microsecond, fold) _check_tzinfo_arg(tzinfo) - _check_time_fields(hour, minute, second, microsecond) + self = object.__new__(cls) self._hour = hour self._minute = minute self._second = second self._microsecond = microsecond self._tzinfo = tzinfo + self._hashcode = -1 + self._fold = fold return self # Read-only field accessors @@ -1048,6 +1229,10 @@ class time: """timezone info object""" return self._tzinfo + @property + def fold(self): + return self._fold + # Standard conversions, __hash__ (and helpers) # Comparisons of time objects with other. @@ -1058,12 +1243,6 @@ class time: else: return False - def __ne__(self, other): - if isinstance(other, time): - return self._cmp(other, allow_mixed=True) != 0 - else: - return True - def __le__(self, other): if isinstance(other, time): return self._cmp(other) <= 0 @@ -1104,8 +1283,8 @@ class time: if base_compare: return _cmp((self._hour, self._minute, self._second, self._microsecond), - (other._hour, other._minute, other._second, - other._microsecond)) + (other._hour, other._minute, other._second, + other._microsecond)) if myoff is None or otoff is None: if allow_mixed: return 2 # arbitrary non-zero value @@ -1118,34 +1297,31 @@ class time: def __hash__(self): """Hash.""" - tzoff = self.utcoffset() - if not tzoff: # zero or None - return hash(self._getstate()[0]) - h, m = divmod(timedelta(hours=self.hour, minutes=self.minute) - tzoff, - timedelta(hours=1)) - assert not m % timedelta(minutes=1), "whole minute" - m //= timedelta(minutes=1) - if 0 <= h < 24: - return hash(time(h, m, self.second, self.microsecond)) - return hash((h, m, self.second, self.microsecond)) + if self._hashcode == -1: + if self.fold: + t = self.replace(fold=0) + else: + t = self + tzoff = t.utcoffset() + if not tzoff: # zero or None + self._hashcode = hash(t._getstate()[0]) + else: + h, m = divmod(timedelta(hours=self.hour, minutes=self.minute) - tzoff, + timedelta(hours=1)) + assert not m % timedelta(minutes=1), "whole minute" + m //= timedelta(minutes=1) + if 0 <= h < 24: + self._hashcode = hash(time(h, m, self.second, self.microsecond)) + else: + self._hashcode = hash((h, m, self.second, self.microsecond)) + return self._hashcode # Conversion to string - def _tzstr(self, sep=":"): - """Return formatted timezone offset (+xx:xx) or None.""" + def _tzstr(self): + """Return formatted timezone offset (+xx:xx) or an empty string.""" off = self.utcoffset() - if off is not None: - if off.days < 0: - sign = "-" - off = -off - else: - sign = "+" - hh, mm = divmod(off, timedelta(hours=1)) - assert not mm % timedelta(minutes=1), "whole minute" - mm //= timedelta(minutes=1) - assert 0 <= hh < 24 - off = "%s%02d%s%02d" % (sign, hh, sep, mm) - return off + return _format_offset(off) def __repr__(self): """Convert to formal string, for repr().""" @@ -1155,21 +1331,28 @@ class time: s = ", %d" % self._second else: s = "" - s= "%s(%d, %d%s)" % ('datetime.' + self.__class__.__name__, - self._hour, self._minute, s) + s= "%s.%s(%d, %d%s)" % (self.__class__.__module__, + self.__class__.__qualname__, + self._hour, self._minute, s) if self._tzinfo is not None: assert s[-1:] == ")" s = s[:-1] + ", tzinfo=%r" % self._tzinfo + ")" + if self._fold: + assert s[-1:] == ")" + s = s[:-1] + ", fold=1)" return s - def isoformat(self): + def isoformat(self, timespec='auto'): """Return the time formatted according to ISO. - This is 'HH:MM:SS.mmmmmm+zz:zz', or 'HH:MM:SS+zz:zz' if - self.microsecond == 0. + The full format is 'HH:MM:SS.mmmmmm+zz:zz'. By default, the fractional + part is omitted if self.microsecond == 0. + + The optional argument timespec specifies the number of additional + terms of the time to include. """ s = _format_time(self._hour, self._minute, self._second, - self._microsecond) + self._microsecond, timespec) tz = self._tzstr() if tz: s += tz @@ -1177,6 +1360,18 @@ class time: __str__ = isoformat + @classmethod + def fromisoformat(cls, time_string): + """Construct a time from the output of isoformat().""" + if not isinstance(time_string, str): + raise TypeError('fromisoformat: argument must be str') + + try: + return cls(*_parse_isoformat_time(time_string)) + except Exception: + raise ValueError('Invalid isoformat string: %s' % time_string) + + def strftime(self, fmt): """Format using strftime(). The date part of the timestamp passed to underlying strftime should not be used. @@ -1189,6 +1384,8 @@ class time: return _wrap_strftime(self, fmt, timetuple) def __format__(self, fmt): + if not isinstance(fmt, str): + raise TypeError("must be str, not %s" % type(fmt).__name__) if len(fmt) != 0: return self.strftime(fmt) return str(self) @@ -1196,8 +1393,8 @@ class time: # Timezone functions def utcoffset(self): - """Return the timezone offset in minutes east of UTC (negative west of - UTC).""" + """Return the timezone offset as timedelta, positive east of UTC + (negative west of UTC).""" if self._tzinfo is None: return None offset = self._tzinfo.utcoffset(None) @@ -1218,8 +1415,8 @@ class time: return name def dst(self): - """Return 0 if DST is not in effect, or the DST offset (in minutes - eastward) if DST is in effect. + """Return 0 if DST is not in effect, or the DST offset (as timedelta + positive eastward) if DST is in effect. This is purely informational; the DST offset has already been added to the UTC offset returned by utcoffset() if applicable, so there's no @@ -1233,7 +1430,7 @@ class time: return offset def replace(self, hour=None, minute=None, second=None, microsecond=None, - tzinfo=True): + tzinfo=True, *, fold=None): """Return a new time with new values for the specified fields.""" if hour is None: hour = self.hour @@ -1245,22 +1442,19 @@ class time: microsecond = self.microsecond if tzinfo is True: tzinfo = self.tzinfo - _check_time_fields(hour, minute, second, microsecond) - _check_tzinfo_arg(tzinfo) - return time(hour, minute, second, microsecond, tzinfo) - - def __bool__(self): - if self.second or self.microsecond: - return True - offset = self.utcoffset() or timedelta(0) - return timedelta(hours=self.hour, minutes=self.minute) != offset + if fold is None: + fold = self._fold + return type(self)(hour, minute, second, microsecond, tzinfo, fold=fold) # Pickle support. - def _getstate(self): + def _getstate(self, protocol=3): us2, us3 = divmod(self._microsecond, 256) us1, us2 = divmod(us2, 256) - basestate = bytes([self._hour, self._minute, self._second, + h = self._hour + if self._fold and protocol > 3: + h += 128 + basestate = bytes([h, self._minute, self._second, us1, us2, us3]) if self._tzinfo is None: return (basestate,) @@ -1268,18 +1462,23 @@ class time: return (basestate, self._tzinfo) def __setstate(self, string, tzinfo): - if len(string) != 6 or string[0] >= 24: - raise TypeError("an integer is required") - (self._hour, self._minute, self._second, - us1, us2, us3) = string - self._microsecond = (((us1 << 8) | us2) << 8) | us3 - if tzinfo is None or isinstance(tzinfo, _tzinfo_class): - self._tzinfo = tzinfo + if tzinfo is not None and not isinstance(tzinfo, _tzinfo_class): + raise TypeError("bad tzinfo state arg") + h, self._minute, self._second, us1, us2, us3 = string + if h > 127: + self._fold = 1 + self._hour = h - 128 else: - raise TypeError("bad tzinfo state arg %r" % tzinfo) + self._fold = 0 + self._hour = h + self._microsecond = (((us1 << 8) | us2) << 8) | us3 + self._tzinfo = tzinfo + + def __reduce_ex__(self, protocol): + return (time, self._getstate(protocol)) def __reduce__(self): - return (time, self._getstate()) + return self.__reduce_ex__(2) _time_class = time # so functions w/ args named "time" can get at the class @@ -1293,25 +1492,31 @@ class datetime(date): The year, month and day arguments are required. tzinfo may be None, or an instance of a tzinfo subclass. The remaining arguments may be ints. """ + __slots__ = date.__slots__ + time.__slots__ - __slots__ = date.__slots__ + ( - '_hour', '_minute', '_second', - '_microsecond', '_tzinfo') def __new__(cls, year, month=None, day=None, hour=0, minute=0, second=0, - microsecond=0, tzinfo=None): - if isinstance(year, bytes) and len(year) == 10: + microsecond=0, tzinfo=None, *, fold=0): + if isinstance(year, bytes) and len(year) == 10 and 1 <= year[2]&0x7F <= 12: # Pickle support - self = date.__new__(cls, year[:4]) + self = object.__new__(cls) self.__setstate(year, month) + self._hashcode = -1 return self + year, month, day = _check_date_fields(year, month, day) + hour, minute, second, microsecond, fold = _check_time_fields( + hour, minute, second, microsecond, fold) _check_tzinfo_arg(tzinfo) - _check_time_fields(hour, minute, second, microsecond) - self = date.__new__(cls, year, month, day) + self = object.__new__(cls) + self._year = year + self._month = month + self._day = day self._hour = hour self._minute = minute self._second = second self._microsecond = microsecond self._tzinfo = tzinfo + self._hashcode = -1 + self._fold = fold return self # Read-only field accessors @@ -1340,56 +1545,69 @@ class datetime(date): """timezone info object""" return self._tzinfo + @property + def fold(self): + return self._fold + + @classmethod + def _fromtimestamp(cls, t, utc, tz): + """Construct a datetime from a POSIX timestamp (like time.time()). + + A timezone info object may be passed in as well. + """ + frac, t = _math.modf(t) + us = round(frac * 1e6) + if us >= 1000000: + t += 1 + us -= 1000000 + elif us < 0: + t -= 1 + us += 1000000 + + converter = _time.gmtime if utc else _time.localtime + y, m, d, hh, mm, ss, weekday, jday, dst = converter(t) + ss = min(ss, 59) # clamp out leap seconds if the platform has them + result = cls(y, m, d, hh, mm, ss, us, tz) + if tz is None: + # As of version 2015f max fold in IANA database is + # 23 hours at 1969-09-30 13:00:00 in Kwajalein. + # Let's probe 24 hours in the past to detect a transition: + max_fold_seconds = 24 * 3600 + + # On Windows localtime_s throws an OSError for negative values, + # thus we can't perform fold detection for values of time less + # than the max time fold. See comments in _datetimemodule's + # version of this method for more details. + if t < max_fold_seconds and sys.platform.startswith("win"): + return result + + y, m, d, hh, mm, ss = converter(t - max_fold_seconds)[:6] + probe1 = cls(y, m, d, hh, mm, ss, us, tz) + trans = result - probe1 - timedelta(0, max_fold_seconds) + if trans.days < 0: + y, m, d, hh, mm, ss = converter(t + trans // timedelta(0, 1))[:6] + probe2 = cls(y, m, d, hh, mm, ss, us, tz) + if probe2 == result: + result._fold = 1 + else: + result = tz.fromutc(result) + return result + @classmethod def fromtimestamp(cls, t, tz=None): """Construct a datetime from a POSIX timestamp (like time.time()). A timezone info object may be passed in as well. """ - _check_tzinfo_arg(tz) - converter = _time.localtime if tz is None else _time.gmtime - - t, frac = divmod(t, 1.0) - us = int(frac * 1e6) - - # If timestamp is less than one microsecond smaller than a - # full second, us can be rounded up to 1000000. In this case, - # roll over to seconds, otherwise, ValueError is raised - # by the constructor. - if us == 1000000: - t += 1 - us = 0 - y, m, d, hh, mm, ss, weekday, jday, dst = converter(t) - ss = min(ss, 59) # clamp out leap seconds if the platform has them - result = cls(y, m, d, hh, mm, ss, us, tz) - if tz is not None: - result = tz.fromutc(result) - return result + return cls._fromtimestamp(t, tz is not None, tz) @classmethod def utcfromtimestamp(cls, t): - "Construct a UTC datetime from a POSIX timestamp (like time.time())." - t, frac = divmod(t, 1.0) - us = int(frac * 1e6) + """Construct a naive UTC datetime from a POSIX timestamp.""" + return cls._fromtimestamp(t, True, None) - # If timestamp is less than one microsecond smaller than a - # full second, us can be rounded up to 1000000. In this case, - # roll over to seconds, otherwise, ValueError is raised - # by the constructor. - if us == 1000000: - t += 1 - us = 0 - y, m, d, hh, mm, ss, weekday, jday, dst = _time.gmtime(t) - ss = min(ss, 59) # clamp out leap seconds if the platform has them - return cls(y, m, d, hh, mm, ss, us) - - # XXX This is supposed to do better than we *can* do by using time.time(), - # XXX if the platform supports a more accurate way. The C implementation - # XXX uses gettimeofday on platforms that have it, but that isn't - # XXX available from Python. So now() may return different results - # XXX across the implementations. @classmethod def now(cls, tz=None): "Construct a datetime from time.time() and optional time zone info." @@ -1403,15 +1621,42 @@ class datetime(date): return cls.utcfromtimestamp(t) @classmethod - def combine(cls, date, time): + def combine(cls, date, time, tzinfo=True): "Construct a datetime from a given date and a given time." if not isinstance(date, _date_class): raise TypeError("date argument must be a date instance") if not isinstance(time, _time_class): raise TypeError("time argument must be a time instance") + if tzinfo is True: + tzinfo = time.tzinfo return cls(date.year, date.month, date.day, time.hour, time.minute, time.second, time.microsecond, - time.tzinfo) + tzinfo, fold=time.fold) + + @classmethod + def fromisoformat(cls, date_string): + """Construct a datetime from the output of datetime.isoformat().""" + if not isinstance(date_string, str): + raise TypeError('fromisoformat: argument must be str') + + # Split this at the separator + dstr = date_string[0:10] + tstr = date_string[11:] + + try: + date_components = _parse_isoformat_date(dstr) + except ValueError: + raise ValueError('Invalid isoformat string: %s' % date_string) + + if tstr: + try: + time_components = _parse_isoformat_time(tstr) + except ValueError: + raise ValueError('Invalid isoformat string: %s' % date_string) + else: + time_components = [0, 0, 0, 0, None] + + return cls(*(date_components + time_components)) def timetuple(self): "Return local time tuple compatible with time.localtime()." @@ -1426,12 +1671,46 @@ class datetime(date): self.hour, self.minute, self.second, dst) + def _mktime(self): + """Return integer POSIX timestamp.""" + epoch = datetime(1970, 1, 1) + max_fold_seconds = 24 * 3600 + t = (self - epoch) // timedelta(0, 1) + def local(u): + y, m, d, hh, mm, ss = _time.localtime(u)[:6] + return (datetime(y, m, d, hh, mm, ss) - epoch) // timedelta(0, 1) + + # Our goal is to solve t = local(u) for u. + a = local(t) - t + u1 = t - a + t1 = local(u1) + if t1 == t: + # We found one solution, but it may not be the one we need. + # Look for an earlier solution (if `fold` is 0), or a + # later one (if `fold` is 1). + u2 = u1 + (-max_fold_seconds, max_fold_seconds)[self.fold] + b = local(u2) - u2 + if a == b: + return u1 + else: + b = t1 - u1 + assert a != b + u2 = t - b + t2 = local(u2) + if t2 == t: + return u2 + if t1 == t: + return u1 + # We have found both offsets a and b, but neither t - a nor t - b is + # a solution. This means t is in the gap. + return (max, min)[self.fold](u1, u2) + + def timestamp(self): "Return POSIX timestamp as float" if self._tzinfo is None: - return _time.mktime((self.year, self.month, self.day, - self.hour, self.minute, self.second, - -1, -1, -1)) + self.microsecond / 1e6 + s = self._mktime() + return s + self.microsecond / 1e6 else: return (self - _EPOCH).total_seconds() @@ -1450,15 +1729,16 @@ class datetime(date): def time(self): "Return the time part, with tzinfo None." - return time(self.hour, self.minute, self.second, self.microsecond) + return time(self.hour, self.minute, self.second, self.microsecond, fold=self.fold) def timetz(self): "Return the time part, with same tzinfo." return time(self.hour, self.minute, self.second, self.microsecond, - self._tzinfo) + self._tzinfo, fold=self.fold) def replace(self, year=None, month=None, day=None, hour=None, - minute=None, second=None, microsecond=None, tzinfo=True): + minute=None, second=None, microsecond=None, tzinfo=True, + *, fold=None): """Return a new datetime with new values for the specified fields.""" if year is None: year = self.year @@ -1476,51 +1756,50 @@ class datetime(date): microsecond = self.microsecond if tzinfo is True: tzinfo = self.tzinfo - _check_date_fields(year, month, day) - _check_time_fields(hour, minute, second, microsecond) - _check_tzinfo_arg(tzinfo) - return datetime(year, month, day, hour, minute, second, - microsecond, tzinfo) + if fold is None: + fold = self.fold + return type(self)(year, month, day, hour, minute, second, + microsecond, tzinfo, fold=fold) + + def _local_timezone(self): + if self.tzinfo is None: + ts = self._mktime() + else: + ts = (self - _EPOCH) // timedelta(seconds=1) + localtm = _time.localtime(ts) + local = datetime(*localtm[:6]) + try: + # Extract TZ data if available + gmtoff = localtm.tm_gmtoff + zone = localtm.tm_zone + except AttributeError: + delta = local - datetime(*_time.gmtime(ts)[:6]) + zone = _time.strftime('%Z', localtm) + tz = timezone(delta, zone) + else: + tz = timezone(timedelta(seconds=gmtoff), zone) + return tz def astimezone(self, tz=None): if tz is None: - if self.tzinfo is None: - raise ValueError("astimezone() requires an aware datetime") - ts = (self - _EPOCH) // timedelta(seconds=1) - localtm = _time.localtime(ts) - local = datetime(*localtm[:6]) - try: - # Extract TZ data if available - gmtoff = localtm.tm_gmtoff - zone = localtm.tm_zone - except AttributeError: - # Compute UTC offset and compare with the value implied - # by tm_isdst. If the values match, use the zone name - # implied by tm_isdst. - delta = local - datetime(*_time.gmtime(ts)[:6]) - dst = _time.daylight and localtm.tm_isdst > 0 - gmtoff = -(_time.altzone if dst else _time.timezone) - if delta == timedelta(seconds=gmtoff): - tz = timezone(delta, _time.tzname[dst]) - else: - tz = timezone(delta) - else: - tz = timezone(timedelta(seconds=gmtoff), zone) - + tz = self._local_timezone() elif not isinstance(tz, tzinfo): raise TypeError("tz argument must be an instance of tzinfo") mytz = self.tzinfo if mytz is None: - raise ValueError("astimezone() requires an aware datetime") + mytz = self._local_timezone() + myoffset = mytz.utcoffset(self) + else: + myoffset = mytz.utcoffset(self) + if myoffset is None: + mytz = self.replace(tzinfo=None)._local_timezone() + myoffset = mytz.utcoffset(self) if tz is mytz: return self # Convert self to UTC, and attach the new time zone object. - myoffset = self.utcoffset() - if myoffset is None: - raise ValueError("astimezone() requires an aware datetime") utc = (self - myoffset).replace(tzinfo=tz) # Convert from UTC to tz's local time. @@ -1538,48 +1817,49 @@ class datetime(date): self._hour, self._minute, self._second, self._year) - def isoformat(self, sep='T'): + def isoformat(self, sep='T', timespec='auto'): """Return the time formatted according to ISO. - This is 'YYYY-MM-DD HH:MM:SS.mmmmmm', or 'YYYY-MM-DD HH:MM:SS' if - self.microsecond == 0. + The full format looks like 'YYYY-MM-DD HH:MM:SS.mmmmmm'. + By default, the fractional part is omitted if self.microsecond == 0. If self.tzinfo is not None, the UTC offset is also attached, giving - 'YYYY-MM-DD HH:MM:SS.mmmmmm+HH:MM' or 'YYYY-MM-DD HH:MM:SS+HH:MM'. + giving a full format of 'YYYY-MM-DD HH:MM:SS.mmmmmm+HH:MM'. Optional argument sep specifies the separator between date and time, default 'T'. + + The optional argument timespec specifies the number of additional + terms of the time to include. """ - s = ("%04d-%02d-%02d%c" % (self._year, self._month, self._day, - sep) + - _format_time(self._hour, self._minute, self._second, - self._microsecond)) + s = ("%04d-%02d-%02d%c" % (self._year, self._month, self._day, sep) + + _format_time(self._hour, self._minute, self._second, + self._microsecond, timespec)) + off = self.utcoffset() - if off is not None: - if off.days < 0: - sign = "-" - off = -off - else: - sign = "+" - hh, mm = divmod(off, timedelta(hours=1)) - assert not mm % timedelta(minutes=1), "whole minute" - mm //= timedelta(minutes=1) - s += "%s%02d:%02d" % (sign, hh, mm) + tz = _format_offset(off) + if tz: + s += tz + return s def __repr__(self): """Convert to formal string, for repr().""" - L = [self._year, self._month, self._day, # These are never zero + L = [self._year, self._month, self._day, # These are never zero self._hour, self._minute, self._second, self._microsecond] if L[-1] == 0: del L[-1] if L[-1] == 0: del L[-1] - s = ", ".join(map(str, L)) - s = "%s(%s)" % ('datetime.' + self.__class__.__name__, s) + s = "%s.%s(%s)" % (self.__class__.__module__, + self.__class__.__qualname__, + ", ".join(map(str, L))) if self._tzinfo is not None: assert s[-1:] == ")" s = s[:-1] + ", tzinfo=%r" % self._tzinfo + ")" + if self._fold: + assert s[-1:] == ")" + s = s[:-1] + ", fold=1)" return s def __str__(self): @@ -1593,7 +1873,7 @@ class datetime(date): return _strptime._strptime_datetime(cls, date_string, format) def utcoffset(self): - """Return the timezone offset in minutes east of UTC (negative west of + """Return the timezone offset as timedelta positive east of UTC (negative west of UTC).""" if self._tzinfo is None: return None @@ -1608,13 +1888,15 @@ class datetime(date): it mean anything in particular. For example, "GMT", "UTC", "-500", "-5:00", "EDT", "US/Eastern", "America/New York" are all valid replies. """ - name = _call_tzinfo_method(self._tzinfo, "tzname", self) + if self._tzinfo is None: + return None + name = self._tzinfo.tzname(self) _check_tzname(name) return name def dst(self): - """Return 0 if DST is not in effect, or the DST offset (in minutes - eastward) if DST is in effect. + """Return 0 if DST is not in effect, or the DST offset (as timedelta + positive eastward) if DST is in effect. This is purely informational; the DST offset has already been added to the UTC offset returned by utcoffset() if applicable, so there's no @@ -1637,14 +1919,6 @@ class datetime(date): else: return False - def __ne__(self, other): - if isinstance(other, datetime): - return self._cmp(other, allow_mixed=True) != 0 - elif not isinstance(other, date): - return NotImplemented - else: - return True - def __le__(self, other): if isinstance(other, datetime): return self._cmp(other) <= 0 @@ -1688,15 +1962,21 @@ class datetime(date): else: myoff = self.utcoffset() otoff = other.utcoffset() + # Assume that allow_mixed means that we are called from __eq__ + if allow_mixed: + if myoff != self.replace(fold=not self.fold).utcoffset(): + return 2 + if otoff != other.replace(fold=not other.fold).utcoffset(): + return 2 base_compare = myoff == otoff if base_compare: return _cmp((self._year, self._month, self._day, self._hour, self._minute, self._second, self._microsecond), - (other._year, other._month, other._day, - other._hour, other._minute, other._second, - other._microsecond)) + (other._year, other._month, other._day, + other._hour, other._minute, other._second, + other._microsecond)) if myoff is None or otoff is None: if allow_mixed: return 2 # arbitrary non-zero value @@ -1754,20 +2034,30 @@ class datetime(date): return base + otoff - myoff def __hash__(self): - tzoff = self.utcoffset() - if tzoff is None: - return hash(self._getstate()[0]) - days = _ymd2ord(self.year, self.month, self.day) - seconds = self.hour * 3600 + self.minute * 60 + self.second - return hash(timedelta(days, seconds, self.microsecond) - tzoff) + if self._hashcode == -1: + if self.fold: + t = self.replace(fold=0) + else: + t = self + tzoff = t.utcoffset() + if tzoff is None: + self._hashcode = hash(t._getstate()[0]) + else: + days = _ymd2ord(self.year, self.month, self.day) + seconds = self.hour * 3600 + self.minute * 60 + self.second + self._hashcode = hash(timedelta(days, seconds, self.microsecond) - tzoff) + return self._hashcode # Pickle support. - def _getstate(self): + def _getstate(self, protocol=3): yhi, ylo = divmod(self._year, 256) us2, us3 = divmod(self._microsecond, 256) us1, us2 = divmod(us2, 256) - basestate = bytes([yhi, ylo, self._month, self._day, + m = self._month + if self._fold and protocol > 3: + m += 128 + basestate = bytes([yhi, ylo, m, self._day, self._hour, self._minute, self._second, us1, us2, us3]) if self._tzinfo is None: @@ -1776,17 +2066,25 @@ class datetime(date): return (basestate, self._tzinfo) def __setstate(self, string, tzinfo): - (yhi, ylo, self._month, self._day, self._hour, + if tzinfo is not None and not isinstance(tzinfo, _tzinfo_class): + raise TypeError("bad tzinfo state arg") + (yhi, ylo, m, self._day, self._hour, self._minute, self._second, us1, us2, us3) = string + if m > 127: + self._fold = 1 + self._month = m - 128 + else: + self._fold = 0 + self._month = m self._year = yhi * 256 + ylo self._microsecond = (((us1 << 8) | us2) << 8) | us3 - if tzinfo is None or isinstance(tzinfo, _tzinfo_class): - self._tzinfo = tzinfo - else: - raise TypeError("bad tzinfo state arg %r" % tzinfo) + self._tzinfo = tzinfo + + def __reduce_ex__(self, protocol): + return (self.__class__, self._getstate(protocol)) def __reduce__(self): - return (self.__class__, self._getstate()) + return self.__reduce_ex__(2) datetime.min = datetime(1, 1, 1) @@ -1799,7 +2097,7 @@ def _isoweek1monday(year): # XXX This could be done more efficiently THURSDAY = 3 firstday = _ymd2ord(year, 1, 1) - firstweekday = (firstday + 6) % 7 # See weekday() above + firstweekday = (firstday + 6) % 7 # See weekday() above week1monday = firstday - firstweekday if firstweekday > THURSDAY: week1monday += 7 @@ -1820,13 +2118,9 @@ class timezone(tzinfo): elif not isinstance(name, str): raise TypeError("name must be a string") if not cls._minoffset <= offset <= cls._maxoffset: - raise ValueError("offset must be a timedelta" - " strictly between -timedelta(hours=24) and" - " timedelta(hours=24).") - if (offset.microseconds != 0 or - offset.seconds % 60 != 0): - raise ValueError("offset must be a timedelta" - " representing a whole number of minutes") + raise ValueError("offset must be a timedelta " + "strictly between -timedelta(hours=24) and " + "timedelta(hours=24).") return cls._create(offset, name) @classmethod @@ -1863,10 +2157,12 @@ class timezone(tzinfo): if self is self.utc: return 'datetime.timezone.utc' if self._name is None: - return "%s(%r)" % ('datetime.' + self.__class__.__name__, - self._offset) - return "%s(%r, %r)" % ('datetime.' + self.__class__.__name__, - self._offset, self._name) + return "%s.%s(%r)" % (self.__class__.__module__, + self.__class__.__qualname__, + self._offset) + return "%s.%s(%r, %r)" % (self.__class__.__module__, + self.__class__.__qualname__, + self._offset, self._name) def __str__(self): return self.tzname(None) @@ -1905,14 +2201,23 @@ class timezone(tzinfo): @staticmethod def _name_from_offset(delta): + if not delta: + return 'UTC' if delta < timedelta(0): sign = '-' delta = -delta else: sign = '+' hours, rest = divmod(delta, timedelta(hours=1)) - minutes = rest // timedelta(minutes=1) - return 'UTC{}{:02d}:{:02d}'.format(sign, hours, minutes) + minutes, rest = divmod(rest, timedelta(minutes=1)) + seconds = rest.seconds + microseconds = rest.microseconds + if microseconds: + return (f'UTC{sign}{hours:02d}:{minutes:02d}:{seconds:02d}' + f'.{microseconds:06d}') + if seconds: + return f'UTC{sign}{hours:02d}:{minutes:02d}:{seconds:02d}' + return f'UTC{sign}{hours:02d}:{minutes:02d}' timezone.utc = timezone._create(timedelta(0)) timezone.min = timezone._create(timezone._minoffset) @@ -2121,14 +2426,15 @@ except ImportError: pass else: # Clean up unused names - del (_DAYNAMES, _DAYS_BEFORE_MONTH, _DAYS_IN_MONTH, - _DI100Y, _DI400Y, _DI4Y, _MAXORDINAL, _MONTHNAMES, - _build_struct_time, _call_tzinfo_method, _check_date_fields, - _check_time_fields, _check_tzinfo_arg, _check_tzname, - _check_utc_offset, _cmp, _cmperror, _date_class, _days_before_month, - _days_before_year, _days_in_month, _format_time, _is_leap, - _isoweek1monday, _math, _ord2ymd, _time, _time_class, _tzinfo_class, - _wrap_strftime, _ymd2ord) + del (_DAYNAMES, _DAYS_BEFORE_MONTH, _DAYS_IN_MONTH, _DI100Y, _DI400Y, + _DI4Y, _EPOCH, _MAXORDINAL, _MONTHNAMES, _build_struct_time, + _check_date_fields, _check_int_field, _check_time_fields, + _check_tzinfo_arg, _check_tzname, _check_utc_offset, _cmp, _cmperror, + _date_class, _days_before_month, _days_before_year, _days_in_month, + _format_time, _format_offset, _is_leap, _isoweek1monday, _math, + _ord2ymd, _time, _time_class, _tzinfo_class, _wrap_strftime, _ymd2ord, + _divide_and_round, _parse_isoformat_date, _parse_isoformat_time, + _parse_hh_mm_ss_ff) # XXX Since import * above excludes names that start with _, # docstring does not get overwritten. In the future, it may be # appropriate to maintain a single module level docstring and diff --git a/python/testData/MockSdk3.7/Lib/genericpath.py b/python/testData/MockSdk3.7/Lib/genericpath.py new file mode 100644 index 000000000000..303b3b349a9f --- /dev/null +++ b/python/testData/MockSdk3.7/Lib/genericpath.py @@ -0,0 +1,151 @@ +""" +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 + +__all__ = ['commonprefix', 'exists', 'getatime', 'getctime', 'getmtime', + 'getsize', 'isdir', 'isfile', 'samefile', 'sameopenfile', + 'samestat'] + + +# Does a path exist? +# This is false for dangling symbolic links on systems that support them. +def exists(path): + """Test whether a path exists. Returns False for broken symbolic links""" + try: + os.stat(path) + except OSError: + return False + return True + + +# This follows symbolic links, so both islink() and isdir() can be true +# for the same path on systems that support symlinks +def isfile(path): + """Test whether a path is a regular file""" + try: + st = os.stat(path) + except OSError: + 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): + """Return true if the pathname refers to an existing directory.""" + try: + st = os.stat(s) + except OSError: + return False + return stat.S_ISDIR(st.st_mode) + + +def getsize(filename): + """Return the size of a file, reported by os.stat().""" + return os.stat(filename).st_size + + +def getmtime(filename): + """Return the last modification time of a file, reported by os.stat().""" + return os.stat(filename).st_mtime + + +def getatime(filename): + """Return the last access time of a file, reported by os.stat().""" + return os.stat(filename).st_atime + + +def getctime(filename): + """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): + "Given a list of pathnames, returns the longest common leading component" + if not m: return '' + # Some people pass in a list of pathname parts to operate in an OS-agnostic + # fashion; don't try to translate in that case as that's an abuse of the + # API and they are already doing what they need to be OS-agnostic and so + # they most likely won't be using an os.PathLike object in the sublists. + if not isinstance(m[0], (list, tuple)): + m = tuple(map(os.fspath, m)) + s1 = min(m) + s2 = max(m) + for i, c in enumerate(s1): + if c != s2[i]: + return s1[:i] + return s1 + +# Are two stat buffers (obtained from stat, fstat or lstat) +# describing the same file? +def samestat(s1, s2): + """Test whether two stat buffers reference the same file""" + return (s1.st_ino == s2.st_ino and + s1.st_dev == s2.st_dev) + + +# Are two filenames really pointing to the same file? +def samefile(f1, f2): + """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, fp2): + """Test whether two open file objects reference the same file""" + s1 = os.fstat(fp1) + s2 = os.fstat(fp2) + return samestat(s1, s2) + + +# 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, sep, altsep, extsep): + """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] + +def _check_arg_types(funcname, *args): + hasstr = hasbytes = False + for s in args: + if isinstance(s, str): + hasstr = True + elif isinstance(s, bytes): + hasbytes = True + else: + raise TypeError('%s() argument must be str or bytes, not %r' % + (funcname, s.__class__.__name__)) from None + if hasstr and hasbytes: + raise TypeError("Can't mix strings and bytes in path components") from None diff --git a/python/testData/MockSdk3.4/Lib/io.py b/python/testData/MockSdk3.7/Lib/io.py similarity index 95% rename from python/testData/MockSdk3.4/Lib/io.py rename to python/testData/MockSdk3.7/Lib/io.py index 8d68f1e42409..968ee5073df1 100644 --- a/python/testData/MockSdk3.4/Lib/io.py +++ b/python/testData/MockSdk3.7/Lib/io.py @@ -19,7 +19,7 @@ streams. BytesIO is a simple stream of in-memory bytes. Another IOBase subclass, TextIOBase, deals with the encoding and decoding of streams into text. TextIOWrapper, which extends it, is a buffered text interface to a buffered raw stream (`BufferedIOBase`). Finally, StringIO -is a in-memory stream for text. +is an in-memory stream for text. Argument names are not part of the specification, and only the arguments of open() are intended to be used as keyword arguments. @@ -90,3 +90,10 @@ for klass in (BytesIO, BufferedReader, BufferedWriter, BufferedRandom, for klass in (StringIO, TextIOWrapper): TextIOBase.register(klass) del klass + +try: + from _io import _WindowsConsoleIO +except ImportError: + pass +else: + RawIOBase.register(_WindowsConsoleIO) diff --git a/python/testData/MockSdk3.7/Lib/ntpath.py b/python/testData/MockSdk3.7/Lib/ntpath.py new file mode 100644 index 000000000000..f0e03a2f496a --- /dev/null +++ b/python/testData/MockSdk3.7/Lib/ntpath.py @@ -0,0 +1,669 @@ +# Module 'ntpath' -- common operations on WinNT/Win95 pathnames +"""Common pathname manipulations, WindowsNT/95 version. + +Instead of importing this module directly, import os and refer to this +module as os.path. +""" + +# strings representing various path-related bits and pieces +# These are primarily for export; internally, they are hardcoded. +# Should be set before imports for resolving cyclic dependency. +curdir = '.' +pardir = '..' +extsep = '.' +sep = '\\' +pathsep = ';' +altsep = '/' +defpath = '.;C:\\bin' +devnull = 'nul' + +import os +import sys +import stat +import genericpath +from genericpath import * + +__all__ = ["normcase","isabs","join","splitdrive","split","splitext", + "basename","dirname","commonprefix","getsize","getmtime", + "getatime","getctime", "islink","exists","lexists","isdir","isfile", + "ismount", "expanduser","expandvars","normpath","abspath", + "curdir","pardir","sep","pathsep","defpath","altsep", + "extsep","devnull","realpath","supports_unicode_filenames","relpath", + "samefile", "sameopenfile", "samestat", "commonpath"] + +def _get_bothseps(path): + if isinstance(path, bytes): + return b'\\/' + else: + return '\\/' + +# Normalize the case of a pathname and map slashes to backslashes. +# Other normalizations (such as optimizing '../' away) are not done +# (this is done by normpath). + +def normcase(s): + """Normalize case of pathname. + + Makes all characters lowercase and all slashes into backslashes.""" + s = os.fspath(s) + try: + if isinstance(s, bytes): + return s.replace(b'/', b'\\').lower() + else: + return s.replace('/', '\\').lower() + except (TypeError, AttributeError): + if not isinstance(s, (bytes, str)): + raise TypeError("normcase() argument must be str or bytes, " + "not %r" % s.__class__.__name__) from None + raise + + +# Return whether a path is absolute. +# Trivial in Posix, harder on Windows. +# For Windows it is absolute if it starts with a slash or backslash (current +# volume), or if a pathname after the volume-letter-and-colon or UNC-resource +# starts with a slash or backslash. + +def isabs(s): + """Test whether a path is absolute""" + s = os.fspath(s) + s = splitdrive(s)[1] + return len(s) > 0 and s[0] in _get_bothseps(s) + + +# Join two (or more) paths. +def join(path, *paths): + path = os.fspath(path) + if isinstance(path, bytes): + sep = b'\\' + seps = b'\\/' + colon = b':' + else: + sep = '\\' + seps = '\\/' + colon = ':' + try: + if not paths: + path[:0] + sep #23780: Ensure compatible data type even if p is null. + result_drive, result_path = splitdrive(path) + for p in map(os.fspath, paths): + p_drive, p_path = splitdrive(p) + if p_path and p_path[0] in seps: + # Second path is absolute + if p_drive or not result_drive: + result_drive = p_drive + result_path = p_path + continue + elif p_drive and p_drive != result_drive: + if p_drive.lower() != result_drive.lower(): + # Different drives => ignore the first path entirely + result_drive = p_drive + result_path = p_path + continue + # Same drive in different case + result_drive = p_drive + # Second path is relative to the first + if result_path and result_path[-1] not in seps: + result_path = result_path + sep + result_path = result_path + p_path + ## add separator between UNC and non-absolute path + if (result_path and result_path[0] not in seps and + result_drive and result_drive[-1:] != colon): + return result_drive + sep + result_path + return result_drive + result_path + except (TypeError, AttributeError, BytesWarning): + genericpath._check_arg_types('join', path, *paths) + raise + + +# Split a path in a drive specification (a drive letter followed by a +# colon) and the path specification. +# It is always true that drivespec + pathspec == p +def splitdrive(p): + """Split a pathname into drive/UNC sharepoint and relative path specifiers. + Returns a 2-tuple (drive_or_unc, path); either part may be empty. + + If you assign + result = splitdrive(p) + It is always true that: + result[0] + result[1] == p + + If the path contained a drive letter, drive_or_unc will contain everything + up to and including the colon. e.g. splitdrive("c:/dir") returns ("c:", "/dir") + + If the path contained a UNC path, the drive_or_unc will contain the host name + and share up to but not including the fourth directory separator character. + e.g. splitdrive("//host/computer/dir") returns ("//host/computer", "/dir") + + Paths cannot contain both a drive letter and a UNC path. + + """ + p = os.fspath(p) + if len(p) >= 2: + if isinstance(p, bytes): + sep = b'\\' + altsep = b'/' + colon = b':' + else: + sep = '\\' + altsep = '/' + colon = ':' + normp = p.replace(altsep, sep) + if (normp[0:2] == sep*2) and (normp[2:3] != sep): + # is a UNC path: + # vvvvvvvvvvvvvvvvvvvv drive letter or UNC path + # \\machine\mountpoint\directory\etc\... + # directory ^^^^^^^^^^^^^^^ + index = normp.find(sep, 2) + if index == -1: + return p[:0], p + index2 = normp.find(sep, index + 1) + # a UNC path can't have two slashes in a row + # (after the initial two) + if index2 == index + 1: + return p[:0], p + if index2 == -1: + index2 = len(p) + return p[:index2], p[index2:] + if normp[1:2] == colon: + return p[:2], p[2:] + return p[:0], p + + +# Split a path in head (everything up to the last '/') and tail (the +# rest). After the trailing '/' is stripped, the invariant +# join(head, tail) == p holds. +# The resulting head won't end in '/' unless it is the root. + +def split(p): + """Split a pathname. + + Return tuple (head, tail) where tail is everything after the final slash. + Either part may be empty.""" + p = os.fspath(p) + seps = _get_bothseps(p) + d, p = splitdrive(p) + # set i to index beyond p's last slash + i = len(p) + while i and p[i-1] not in seps: + i -= 1 + head, tail = p[:i], p[i:] # now tail has no slashes + # remove trailing slashes from head, unless it's all slashes + head = head.rstrip(seps) or head + return d + 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): + p = os.fspath(p) + if isinstance(p, bytes): + return genericpath._splitext(p, b'\\', b'/', b'.') + else: + return genericpath._splitext(p, '\\', '/', '.') +splitext.__doc__ = genericpath._splitext.__doc__ + + +# Return the tail (basename) part of a path. + +def basename(p): + """Returns the final component of a pathname""" + return split(p)[1] + + +# Return the head (dirname) part of a path. + +def dirname(p): + """Returns the directory component of a pathname""" + return split(p)[0] + +# Is a path a symbolic link? +# This will always return false on systems where os.lstat doesn't exist. + +def islink(path): + """Test whether a path is a symbolic link. + This will always return false for Windows prior to 6.0. + """ + try: + st = os.lstat(path) + except (OSError, AttributeError): + return False + return stat.S_ISLNK(st.st_mode) + +# Being true for dangling symbolic links is also useful. + +def lexists(path): + """Test whether a path exists. Returns True for broken symbolic links""" + try: + st = os.lstat(path) + except OSError: + return False + return True + +# Is a path a mount point? +# Any drive letter root (eg c:\) +# Any share UNC (eg \\server\share) +# Any volume mounted on a filesystem folder +# +# No one method detects all three situations. Historically we've lexically +# detected drive letter roots and share UNCs. The canonical approach to +# detecting mounted volumes (querying the reparse tag) fails for the most +# common case: drive letter roots. The alternative which uses GetVolumePathName +# fails if the drive letter is the result of a SUBST. +try: + from nt import _getvolumepathname +except ImportError: + _getvolumepathname = None +def ismount(path): + """Test whether a path is a mount point (a drive root, the root of a + share, or a mounted volume)""" + path = os.fspath(path) + seps = _get_bothseps(path) + path = abspath(path) + root, rest = splitdrive(path) + if root and root[0] in seps: + return (not rest) or (rest in seps) + if rest in seps: + return True + + if _getvolumepathname: + return path.rstrip(seps) == _getvolumepathname(path).rstrip(seps) + else: + 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): + """Expand ~ and ~user constructs. + + If user or $HOME is unknown, do nothing.""" + path = os.fspath(path) + if isinstance(path, bytes): + tilde = b'~' + else: + tilde = '~' + if not path.startswith(tilde): + return path + i, n = 1, len(path) + while i < n and path[i] not in _get_bothseps(path): + i += 1 + + if 'HOME' in os.environ: + userhome = os.environ['HOME'] + elif 'USERPROFILE' in os.environ: + userhome = os.environ['USERPROFILE'] + elif not 'HOMEPATH' in os.environ: + return path + else: + try: + drive = os.environ['HOMEDRIVE'] + except KeyError: + drive = '' + userhome = join(drive, os.environ['HOMEPATH']) + + if isinstance(path, bytes): + userhome = os.fsencode(userhome) + + if i != 1: #~user + userhome = join(dirname(userhome), path[1:i]) + + return userhome + path[i:] + + +# Expand paths containing shell variable substitutions. +# The following rules apply: +# - no expansion within single quotes +# - '$$' is translated into '$' +# - '%%' is translated into '%' if '%%' are not seen in %var1%%var2% +# - ${varname} is accepted. +# - $varname is accepted. +# - %varname% is accepted. +# - varnames can be made out of letters, digits and the characters '_-' +# (though is not verified in the ${varname} and %varname% cases) +# XXX With COMMAND.COM you can use any characters in a variable name, +# XXX except '^|<>='. + +def expandvars(path): + """Expand shell variables of the forms $var, ${var} and %var%. + + Unknown variables are left unchanged.""" + path = os.fspath(path) + if isinstance(path, bytes): + if b'$' not in path and b'%' not in path: + return path + import string + varchars = bytes(string.ascii_letters + string.digits + '_-', 'ascii') + quote = b'\'' + percent = b'%' + brace = b'{' + rbrace = b'}' + dollar = b'$' + environ = getattr(os, 'environb', None) + else: + if '$' not in path and '%' not in path: + return path + import string + varchars = string.ascii_letters + string.digits + '_-' + quote = '\'' + percent = '%' + brace = '{' + rbrace = '}' + dollar = '$' + environ = os.environ + res = path[:0] + index = 0 + pathlen = len(path) + while index < pathlen: + c = path[index:index+1] + if c == quote: # no expansion within single quotes + path = path[index + 1:] + pathlen = len(path) + try: + index = path.index(c) + res += c + path[:index + 1] + except ValueError: + res += c + path + index = pathlen - 1 + elif c == percent: # variable or '%' + if path[index + 1:index + 2] == percent: + res += c + index += 1 + else: + path = path[index+1:] + pathlen = len(path) + try: + index = path.index(percent) + except ValueError: + res += percent + path + index = pathlen - 1 + else: + var = path[:index] + try: + if environ is None: + value = os.fsencode(os.environ[os.fsdecode(var)]) + else: + value = environ[var] + except KeyError: + value = percent + var + percent + res += value + elif c == dollar: # variable or '$$' + if path[index + 1:index + 2] == dollar: + res += c + index += 1 + elif path[index + 1:index + 2] == brace: + path = path[index+2:] + pathlen = len(path) + try: + index = path.index(rbrace) + except ValueError: + res += dollar + brace + path + index = pathlen - 1 + else: + var = path[:index] + try: + if environ is None: + value = os.fsencode(os.environ[os.fsdecode(var)]) + else: + value = environ[var] + except KeyError: + value = dollar + brace + var + rbrace + res += value + else: + var = path[:0] + index += 1 + c = path[index:index + 1] + while c and c in varchars: + var += c + index += 1 + c = path[index:index + 1] + try: + if environ is None: + value = os.fsencode(os.environ[os.fsdecode(var)]) + else: + value = environ[var] + except KeyError: + value = dollar + var + res += value + if c: + index -= 1 + else: + res += c + index += 1 + return res + + +# Normalize a path, e.g. A//B, A/./B and A/foo/../B all become A\B. +# Previously, this function also truncated pathnames to 8+3 format, +# but as this module is called "ntpath", that's obviously wrong! + +def normpath(path): + """Normalize path, eliminating double slashes, etc.""" + path = os.fspath(path) + if isinstance(path, bytes): + sep = b'\\' + altsep = b'/' + curdir = b'.' + pardir = b'..' + special_prefixes = (b'\\\\.\\', b'\\\\?\\') + else: + sep = '\\' + altsep = '/' + curdir = '.' + pardir = '..' + special_prefixes = ('\\\\.\\', '\\\\?\\') + if path.startswith(special_prefixes): + # in the case of paths with these prefixes: + # \\.\ -> device names + # \\?\ -> literal paths + # do not do any normalization, but return the path unchanged + return path + path = path.replace(altsep, sep) + prefix, path = splitdrive(path) + + # collapse initial backslashes + if path.startswith(sep): + prefix += sep + path = path.lstrip(sep) + + comps = path.split(sep) + i = 0 + while i < len(comps): + if not comps[i] or comps[i] == curdir: + del comps[i] + elif comps[i] == pardir: + if i > 0 and comps[i-1] != pardir: + del comps[i-1:i+1] + i -= 1 + elif i == 0 and prefix.endswith(sep): + del comps[i] + else: + i += 1 + else: + i += 1 + # If the path is now empty, substitute '.' + if not prefix and not comps: + comps.append(curdir) + return prefix + sep.join(comps) + +def _abspath_fallback(path): + """Return the absolute version of a path as a fallback function in case + `nt._getfullpathname` is not available or raises OSError. See bpo-31047 for + more. + + """ + + path = os.fspath(path) + if not isabs(path): + if isinstance(path, bytes): + cwd = os.getcwdb() + else: + cwd = os.getcwd() + path = join(cwd, path) + return normpath(path) + +# Return an absolute path. +try: + from nt import _getfullpathname + +except ImportError: # not running on Windows - mock up something sensible + abspath = _abspath_fallback + +else: # use native Windows method on Windows + def abspath(path): + """Return the absolute version of a path.""" + try: + return _getfullpathname(path) + except OSError: + return _abspath_fallback(path) + +# realpath is a no-op on systems without islink support +realpath = abspath +# Win9x family and earlier have no Unicode filename support. +supports_unicode_filenames = (hasattr(sys, "getwindowsversion") and + sys.getwindowsversion()[3] >= 2) + +def relpath(path, start=None): + """Return a relative version of a path""" + path = os.fspath(path) + if isinstance(path, bytes): + sep = b'\\' + curdir = b'.' + pardir = b'..' + else: + sep = '\\' + curdir = '.' + pardir = '..' + + if start is None: + start = curdir + + if not path: + raise ValueError("no path specified") + + start = os.fspath(start) + try: + start_abs = abspath(normpath(start)) + path_abs = abspath(normpath(path)) + start_drive, start_rest = splitdrive(start_abs) + path_drive, path_rest = splitdrive(path_abs) + if normcase(start_drive) != normcase(path_drive): + raise ValueError("path is on mount %r, start on mount %r" % ( + path_drive, start_drive)) + + start_list = [x for x in start_rest.split(sep) if x] + path_list = [x for x in path_rest.split(sep) if x] + # Work out how much of the filepath is shared by start and path. + i = 0 + for e1, e2 in zip(start_list, path_list): + if normcase(e1) != normcase(e2): + break + i += 1 + + rel_list = [pardir] * (len(start_list)-i) + path_list[i:] + if not rel_list: + return curdir + return join(*rel_list) + except (TypeError, ValueError, AttributeError, BytesWarning, DeprecationWarning): + genericpath._check_arg_types('relpath', path, start) + raise + + +# Return the longest common sub-path of the sequence of paths given as input. +# The function is case-insensitive and 'separator-insensitive', i.e. if the +# only difference between two paths is the use of '\' versus '/' as separator, +# they are deemed to be equal. +# +# However, the returned path will have the standard '\' separator (even if the +# given paths had the alternative '/' separator) and will have the case of the +# first path given in the sequence. Additionally, any trailing separator is +# stripped from the returned path. + +def commonpath(paths): + """Given a sequence of path names, returns the longest common sub-path.""" + + if not paths: + raise ValueError('commonpath() arg is an empty sequence') + + paths = tuple(map(os.fspath, paths)) + if isinstance(paths[0], bytes): + sep = b'\\' + altsep = b'/' + curdir = b'.' + else: + sep = '\\' + altsep = '/' + curdir = '.' + + try: + drivesplits = [splitdrive(p.replace(altsep, sep).lower()) for p in paths] + split_paths = [p.split(sep) for d, p in drivesplits] + + try: + isabs, = set(p[:1] == sep for d, p in drivesplits) + except ValueError: + raise ValueError("Can't mix absolute and relative paths") from None + + # Check that all drive letters or UNC paths match. The check is made only + # now otherwise type errors for mixing strings and bytes would not be + # caught. + if len(set(d for d, p in drivesplits)) != 1: + raise ValueError("Paths don't have the same drive") + + drive, path = splitdrive(paths[0].replace(altsep, sep)) + common = path.split(sep) + common = [c for c in common if c and c != curdir] + + split_paths = [[c for c in s if c and c != curdir] for s in split_paths] + s1 = min(split_paths) + s2 = max(split_paths) + for i, c in enumerate(s1): + if c != s2[i]: + common = common[:i] + break + else: + common = common[:len(s1)] + + prefix = drive + sep if isabs else drive + return prefix + sep.join(common) + except (TypeError, AttributeError): + genericpath._check_arg_types('commonpath', *paths) + raise + + +# determine if two files are in fact the same file +try: + # GetFinalPathNameByHandle is available starting with Windows 6.0. + # Windows XP and non-Windows OS'es will mock _getfinalpathname. + if sys.getwindowsversion()[:2] >= (6, 0): + from nt import _getfinalpathname + else: + raise ImportError +except (AttributeError, ImportError): + # On Windows XP and earlier, two files are the same if their absolute + # pathnames are the same. + # Non-Windows operating systems fake this method with an XP + # approximation. + def _getfinalpathname(f): + return normcase(abspath(f)) + + +try: + # The genericpath.isdir implementation uses os.stat and checks the mode + # attribute to tell whether or not the path is a directory. + # This is overkill on Windows - just pass the path to GetFileAttributes + # and check the attribute from there. + from nt import _isdir as isdir +except ImportError: + # Use genericpath.isdir as imported above. + pass diff --git a/python/testData/MockSdk3.4/Lib/numbers.py b/python/testData/MockSdk3.7/Lib/numbers.py similarity index 97% rename from python/testData/MockSdk3.4/Lib/numbers.py rename to python/testData/MockSdk3.7/Lib/numbers.py index b206457dfc04..ed815ef41ebe 100644 --- a/python/testData/MockSdk3.4/Lib/numbers.py +++ b/python/testData/MockSdk3.7/Lib/numbers.py @@ -35,7 +35,7 @@ class Complex(Number): In short, those are: a conversion to complex, .real, .imag, +, -, *, /, abs(), .conjugate, ==, and !=. - If it is given heterogenous arguments, and doesn't have special + If it is given heterogeneous arguments, and doesn't have special knowledge about them, it should fall back to the builtin complex type as described below. """ @@ -141,11 +141,6 @@ class Complex(Number): """self == other""" raise NotImplementedError - def __ne__(self, other): - """self != other""" - # The default __ne__ doesn't negate __eq__ until 3.0. - return not (self == other) - Complex.register(complex) diff --git a/python/testData/MockSdk3.7/Lib/os.py b/python/testData/MockSdk3.7/Lib/os.py new file mode 100644 index 000000000000..499e6285618f --- /dev/null +++ b/python/testData/MockSdk3.7/Lib/os.py @@ -0,0 +1,1078 @@ +r"""OS routines for NT or Posix depending on what system we're on. + +This exports: + - all functions from posix or nt, e.g. unlink, stat, etc. + - os.path is either posixpath or ntpath + - os.name is either 'posix' or 'nt' + - os.curdir is a string representing the current directory (always '.') + - os.pardir is a string representing the parent directory (always '..') + - os.sep is the (or a most common) pathname separator ('/' or '\\') + - os.extsep is the extension separator (always '.') + - os.altsep is the alternate pathname separator (None or '/') + - os.pathsep is the component separator used in $PATH etc + - os.linesep is the line separator in text files ('\r' or '\n' or '\r\n') + - os.defpath is the default search path for executables + - os.devnull is the file path of the null device ('/dev/null', etc.) + +Programs that import and use 'os' stand a better chance of being +portable between different platforms. Of course, they must then +only use functions that are defined by all platforms (e.g., unlink +and opendir), and leave all pathname manipulation to os.path +(e.g., split and join). +""" + +#' +import abc +import sys +import stat as st + +_names = sys.builtin_module_names + +# Note: more names are added to __all__ later. +__all__ = ["altsep", "curdir", "pardir", "sep", "pathsep", "linesep", + "defpath", "name", "path", "devnull", "SEEK_SET", "SEEK_CUR", + "SEEK_END", "fsencode", "fsdecode", "get_exec_path", "fdopen", + "popen", "extsep"] + +def _exists(name): + return name in globals() + +def _get_exports_list(module): + try: + return list(module.__all__) + except AttributeError: + return [n for n in dir(module) if n[0] != '_'] + +# Any new dependencies of the os module and/or changes in path separator +# requires updating importlib as well. +if 'posix' in _names: + name = 'posix' + linesep = '\n' + from posix import * + try: + from posix import _exit + __all__.append('_exit') + except ImportError: + pass + import posixpath as path + + try: + from posix import _have_functions + except ImportError: + pass + + import posix + __all__.extend(_get_exports_list(posix)) + del posix + +elif 'nt' in _names: + name = 'nt' + linesep = '\r\n' + from nt import * + try: + from nt import _exit + __all__.append('_exit') + except ImportError: + pass + import ntpath as path + + import nt + __all__.extend(_get_exports_list(nt)) + del nt + + try: + from nt import _have_functions + except ImportError: + pass + +else: + raise ImportError('no os specific module found') + +sys.modules['os.path'] = path +from os.path import (curdir, pardir, sep, pathsep, defpath, extsep, altsep, + devnull) + +del _names + + +if _exists("_have_functions"): + _globals = globals() + def _add(str, fn): + if (fn in _globals) and (str in _have_functions): + _set.add(_globals[fn]) + + _set = set() + _add("HAVE_FACCESSAT", "access") + _add("HAVE_FCHMODAT", "chmod") + _add("HAVE_FCHOWNAT", "chown") + _add("HAVE_FSTATAT", "stat") + _add("HAVE_FUTIMESAT", "utime") + _add("HAVE_LINKAT", "link") + _add("HAVE_MKDIRAT", "mkdir") + _add("HAVE_MKFIFOAT", "mkfifo") + _add("HAVE_MKNODAT", "mknod") + _add("HAVE_OPENAT", "open") + _add("HAVE_READLINKAT", "readlink") + _add("HAVE_RENAMEAT", "rename") + _add("HAVE_SYMLINKAT", "symlink") + _add("HAVE_UNLINKAT", "unlink") + _add("HAVE_UNLINKAT", "rmdir") + _add("HAVE_UTIMENSAT", "utime") + supports_dir_fd = _set + + _set = set() + _add("HAVE_FACCESSAT", "access") + supports_effective_ids = _set + + _set = set() + _add("HAVE_FCHDIR", "chdir") + _add("HAVE_FCHMOD", "chmod") + _add("HAVE_FCHOWN", "chown") + _add("HAVE_FDOPENDIR", "listdir") + _add("HAVE_FDOPENDIR", "scandir") + _add("HAVE_FEXECVE", "execve") + _set.add(stat) # fstat always works + _add("HAVE_FTRUNCATE", "truncate") + _add("HAVE_FUTIMENS", "utime") + _add("HAVE_FUTIMES", "utime") + _add("HAVE_FPATHCONF", "pathconf") + if _exists("statvfs") and _exists("fstatvfs"): # mac os x10.3 + _add("HAVE_FSTATVFS", "statvfs") + supports_fd = _set + + _set = set() + _add("HAVE_FACCESSAT", "access") + # Some platforms don't support lchmod(). Often the function exists + # anyway, as a stub that always returns ENOSUP or perhaps EOPNOTSUPP. + # (No, I don't know why that's a good design.) ./configure will detect + # this and reject it--so HAVE_LCHMOD still won't be defined on such + # platforms. This is Very Helpful. + # + # However, sometimes platforms without a working lchmod() *do* have + # fchmodat(). (Examples: Linux kernel 3.2 with glibc 2.15, + # OpenIndiana 3.x.) And fchmodat() has a flag that theoretically makes + # it behave like lchmod(). So in theory it would be a suitable + # replacement for lchmod(). But when lchmod() doesn't work, fchmodat()'s + # flag doesn't work *either*. Sadly ./configure isn't sophisticated + # enough to detect this condition--it only determines whether or not + # fchmodat() minimally works. + # + # Therefore we simply ignore fchmodat() when deciding whether or not + # os.chmod supports follow_symlinks. Just checking lchmod() is + # sufficient. After all--if you have a working fchmodat(), your + # lchmod() almost certainly works too. + # + # _add("HAVE_FCHMODAT", "chmod") + _add("HAVE_FCHOWNAT", "chown") + _add("HAVE_FSTATAT", "stat") + _add("HAVE_LCHFLAGS", "chflags") + _add("HAVE_LCHMOD", "chmod") + if _exists("lchown"): # mac os x10.3 + _add("HAVE_LCHOWN", "chown") + _add("HAVE_LINKAT", "link") + _add("HAVE_LUTIMES", "utime") + _add("HAVE_LSTAT", "stat") + _add("HAVE_FSTATAT", "stat") + _add("HAVE_UTIMENSAT", "utime") + _add("MS_WINDOWS", "stat") + supports_follow_symlinks = _set + + del _set + del _have_functions + del _globals + del _add + + +# Python uses fixed values for the SEEK_ constants; they are mapped +# to native constants if necessary in posixmodule.c +# Other possible SEEK values are directly imported from posixmodule.c +SEEK_SET = 0 +SEEK_CUR = 1 +SEEK_END = 2 + +# Super directory utilities. +# (Inspired by Eric Raymond; the doc strings are mostly his) + +def makedirs(name, mode=0o777, exist_ok=False): + """makedirs(name [, mode=0o777][, exist_ok=False]) + + Super-mkdir; create a leaf directory and all intermediate ones. Works like + mkdir, except that any intermediate path segment (not just the rightmost) + will be created if it does not exist. If the target directory already + exists, raise an OSError if exist_ok is False. Otherwise no exception is + raised. This is recursive. + + """ + head, tail = path.split(name) + if not tail: + head, tail = path.split(head) + if head and tail and not path.exists(head): + try: + makedirs(head, exist_ok=exist_ok) + except FileExistsError: + # Defeats race condition when another thread created the path + pass + cdir = curdir + if isinstance(tail, bytes): + cdir = bytes(curdir, 'ASCII') + if tail == cdir: # xxx/newdir/. exists if xxx/newdir exists + return + try: + mkdir(name, mode) + except OSError: + # Cannot rely on checking for EEXIST, since the operating system + # could give priority to other errors like EACCES or EROFS + if not exist_ok or not path.isdir(name): + raise + +def removedirs(name): + """removedirs(name) + + Super-rmdir; remove a leaf directory and all empty intermediate + ones. Works like rmdir except that, if the leaf directory is + successfully removed, directories corresponding to rightmost path + segments will be pruned away until either the whole path is + consumed or an error occurs. Errors during this latter phase are + ignored -- they generally mean that a directory was not empty. + + """ + rmdir(name) + head, tail = path.split(name) + if not tail: + head, tail = path.split(head) + while head and tail: + try: + rmdir(head) + except OSError: + break + head, tail = path.split(head) + +def renames(old, new): + """renames(old, new) + + Super-rename; create directories as necessary and delete any left + empty. Works like rename, except creation of any intermediate + directories needed to make the new pathname good is attempted + first. After the rename, directories corresponding to rightmost + path segments of the old name will be pruned until either the + whole path is consumed or a nonempty directory is found. + + Note: this function can fail with the new directory structure made + if you lack permissions needed to unlink the leaf directory or + file. + + """ + head, tail = path.split(new) + if head and tail and not path.exists(head): + makedirs(head) + rename(old, new) + head, tail = path.split(old) + if head and tail: + try: + removedirs(head) + except OSError: + pass + +__all__.extend(["makedirs", "removedirs", "renames"]) + +def walk(top, topdown=True, onerror=None, followlinks=False): + """Directory tree generator. + + For each directory in the directory tree rooted at top (including top + itself, but excluding '.' and '..'), yields a 3-tuple + + dirpath, dirnames, filenames + + dirpath is a string, the path to the directory. dirnames is a list of + the names of the subdirectories in dirpath (excluding '.' and '..'). + filenames is a list of the names of the non-directory files in dirpath. + Note that the names in the lists are just names, with no path components. + To get a full path (which begins with top) to a file or directory in + dirpath, do os.path.join(dirpath, name). + + If optional arg 'topdown' is true or not specified, the triple for a + directory is generated before the triples for any of its subdirectories + (directories are generated top down). If topdown is false, the triple + for a directory is generated after the triples for all of its + subdirectories (directories are generated bottom up). + + When topdown is true, the caller can modify the dirnames list in-place + (e.g., via del or slice assignment), and walk will only recurse into the + subdirectories whose names remain in dirnames; this can be used to prune the + search, or to impose a specific order of visiting. Modifying dirnames when + topdown is false is ineffective, since the directories in dirnames have + already been generated by the time dirnames itself is generated. No matter + the value of topdown, the list of subdirectories is retrieved before the + tuples for the directory and its subdirectories are generated. + + By default errors from the os.scandir() call are ignored. If + optional arg 'onerror' is specified, it should be a function; it + will be called with one argument, an OSError instance. It can + report the error to continue with the walk, or raise the exception + to abort the walk. Note that the filename is available as the + filename attribute of the exception object. + + By default, os.walk does not follow symbolic links to subdirectories on + systems that support them. In order to get this functionality, set the + optional argument 'followlinks' to true. + + Caution: if you pass a relative pathname for top, don't change the + current working directory between resumptions of walk. walk never + changes the current directory, and assumes that the client doesn't + either. + + Example: + + import os + from os.path import join, getsize + for root, dirs, files in os.walk('python/Lib/email'): + print(root, "consumes", end="") + print(sum([getsize(join(root, name)) for name in files]), end="") + print("bytes in", len(files), "non-directory files") + if 'CVS' in dirs: + dirs.remove('CVS') # don't visit CVS directories + + """ + top = fspath(top) + dirs = [] + nondirs = [] + walk_dirs = [] + + # We may not have read permission for top, in which case we can't + # get a list of the files the directory contains. os.walk + # always suppressed the exception then, rather than blow up for a + # minor reason when (say) a thousand readable directories are still + # left to visit. That logic is copied here. + try: + # Note that scandir is global in this module due + # to earlier import-*. + scandir_it = scandir(top) + except OSError as error: + if onerror is not None: + onerror(error) + return + + with scandir_it: + while True: + try: + try: + entry = next(scandir_it) + except StopIteration: + break + except OSError as error: + if onerror is not None: + onerror(error) + return + + try: + is_dir = entry.is_dir() + except OSError: + # If is_dir() raises an OSError, consider that the entry is not + # a directory, same behaviour than os.path.isdir(). + is_dir = False + + if is_dir: + dirs.append(entry.name) + else: + nondirs.append(entry.name) + + if not topdown and is_dir: + # Bottom-up: recurse into sub-directory, but exclude symlinks to + # directories if followlinks is False + if followlinks: + walk_into = True + else: + try: + is_symlink = entry.is_symlink() + except OSError: + # If is_symlink() raises an OSError, consider that the + # entry is not a symbolic link, same behaviour than + # os.path.islink(). + is_symlink = False + walk_into = not is_symlink + + if walk_into: + walk_dirs.append(entry.path) + + # Yield before recursion if going top down + if topdown: + yield top, dirs, nondirs + + # Recurse into sub-directories + islink, join = path.islink, path.join + for dirname in dirs: + new_path = join(top, dirname) + # Issue #23605: os.path.islink() is used instead of caching + # entry.is_symlink() result during the loop on os.scandir() because + # the caller can replace the directory entry during the "yield" + # above. + if followlinks or not islink(new_path): + yield from walk(new_path, topdown, onerror, followlinks) + else: + # Recurse into sub-directories + for new_path in walk_dirs: + yield from walk(new_path, topdown, onerror, followlinks) + # Yield after recursion if going bottom up + yield top, dirs, nondirs + +__all__.append("walk") + +if {open, stat} <= supports_dir_fd and {scandir, stat} <= supports_fd: + + def fwalk(top=".", topdown=True, onerror=None, *, follow_symlinks=False, dir_fd=None): + """Directory tree generator. + + This behaves exactly like walk(), except that it yields a 4-tuple + + dirpath, dirnames, filenames, dirfd + + `dirpath`, `dirnames` and `filenames` are identical to walk() output, + and `dirfd` is a file descriptor referring to the directory `dirpath`. + + The advantage of fwalk() over walk() is that it's safe against symlink + races (when follow_symlinks is False). + + If dir_fd is not None, it should be a file descriptor open to a directory, + and top should be relative; top will then be relative to that directory. + (dir_fd is always supported for fwalk.) + + Caution: + Since fwalk() yields file descriptors, those are only valid until the + next iteration step, so you should dup() them if you want to keep them + for a longer period. + + Example: + + import os + for root, dirs, files, rootfd in os.fwalk('python/Lib/email'): + print(root, "consumes", end="") + print(sum([os.stat(name, dir_fd=rootfd).st_size for name in files]), + end="") + print("bytes in", len(files), "non-directory files") + if 'CVS' in dirs: + dirs.remove('CVS') # don't visit CVS directories + """ + if not isinstance(top, int) or not hasattr(top, '__index__'): + top = fspath(top) + # Note: To guard against symlink races, we use the standard + # lstat()/open()/fstat() trick. + if not follow_symlinks: + orig_st = stat(top, follow_symlinks=False, dir_fd=dir_fd) + topfd = open(top, O_RDONLY, dir_fd=dir_fd) + try: + if (follow_symlinks or (st.S_ISDIR(orig_st.st_mode) and + path.samestat(orig_st, stat(topfd)))): + yield from _fwalk(topfd, top, isinstance(top, bytes), + topdown, onerror, follow_symlinks) + finally: + close(topfd) + + def _fwalk(topfd, toppath, isbytes, topdown, onerror, follow_symlinks): + # Note: This uses O(depth of the directory tree) file descriptors: if + # necessary, it can be adapted to only require O(1) FDs, see issue + # #13734. + + scandir_it = scandir(topfd) + dirs = [] + nondirs = [] + entries = None if topdown or follow_symlinks else [] + for entry in scandir_it: + name = entry.name + if isbytes: + name = fsencode(name) + try: + if entry.is_dir(): + dirs.append(name) + if entries is not None: + entries.append(entry) + else: + nondirs.append(name) + except OSError: + try: + # Add dangling symlinks, ignore disappeared files + if entry.is_symlink(): + nondirs.append(name) + except OSError: + pass + + if topdown: + yield toppath, dirs, nondirs, topfd + + for name in dirs if entries is None else zip(dirs, entries): + try: + if not follow_symlinks: + if topdown: + orig_st = stat(name, dir_fd=topfd, follow_symlinks=False) + else: + assert entries is not None + name, entry = name + orig_st = entry.stat(follow_symlinks=False) + dirfd = open(name, O_RDONLY, dir_fd=topfd) + except OSError as err: + if onerror is not None: + onerror(err) + continue + try: + if follow_symlinks or path.samestat(orig_st, stat(dirfd)): + dirpath = path.join(toppath, name) + yield from _fwalk(dirfd, dirpath, isbytes, + topdown, onerror, follow_symlinks) + finally: + close(dirfd) + + if not topdown: + yield toppath, dirs, nondirs, topfd + + __all__.append("fwalk") + +# Make sure os.environ exists, at least +try: + environ +except NameError: + environ = {} + +def execl(file, *args): + """execl(file, *args) + + Execute the executable file with argument list args, replacing the + current process. """ + execv(file, args) + +def execle(file, *args): + """execle(file, *args, env) + + Execute the executable file with argument list args and + environment env, replacing the current process. """ + env = args[-1] + execve(file, args[:-1], env) + +def execlp(file, *args): + """execlp(file, *args) + + Execute the executable file (which is searched for along $PATH) + with argument list args, replacing the current process. """ + execvp(file, args) + +def execlpe(file, *args): + """execlpe(file, *args, env) + + Execute the executable file (which is searched for along $PATH) + with argument list args and environment env, replacing the current + process. """ + env = args[-1] + execvpe(file, args[:-1], env) + +def execvp(file, args): + """execvp(file, args) + + Execute the executable file (which is searched for along $PATH) + with argument list args, replacing the current process. + args may be a list or tuple of strings. """ + _execvpe(file, args) + +def execvpe(file, args, env): + """execvpe(file, args, env) + + Execute the executable file (which is searched for along $PATH) + with argument list args and environment env , replacing the + current process. + args may be a list or tuple of strings. """ + _execvpe(file, args, env) + +__all__.extend(["execl","execle","execlp","execlpe","execvp","execvpe"]) + +def _execvpe(file, args, env=None): + if env is not None: + exec_func = execve + argrest = (args, env) + else: + exec_func = execv + argrest = (args,) + env = environ + + if path.dirname(file): + exec_func(file, *argrest) + return + saved_exc = None + path_list = get_exec_path(env) + if name != 'nt': + file = fsencode(file) + path_list = map(fsencode, path_list) + for dir in path_list: + fullname = path.join(dir, file) + try: + exec_func(fullname, *argrest) + except (FileNotFoundError, NotADirectoryError) as e: + last_exc = e + except OSError as e: + last_exc = e + if saved_exc is None: + saved_exc = e + if saved_exc is not None: + raise saved_exc + raise last_exc + + +def get_exec_path(env=None): + """Returns the sequence of directories that will be searched for the + named executable (similar to a shell) when launching a process. + + *env* must be an environment variable dict or None. If *env* is None, + os.environ will be used. + """ + # Use a local import instead of a global import to limit the number of + # modules loaded at startup: the os module is always loaded at startup by + # Python. It may also avoid a bootstrap issue. + import warnings + + if env is None: + env = environ + + # {b'PATH': ...}.get('PATH') and {'PATH': ...}.get(b'PATH') emit a + # BytesWarning when using python -b or python -bb: ignore the warning + with warnings.catch_warnings(): + warnings.simplefilter("ignore", BytesWarning) + + try: + path_list = env.get('PATH') + except TypeError: + path_list = None + + if supports_bytes_environ: + try: + path_listb = env[b'PATH'] + except (KeyError, TypeError): + pass + else: + if path_list is not None: + raise ValueError( + "env cannot contain 'PATH' and b'PATH' keys") + path_list = path_listb + + if path_list is not None and isinstance(path_list, bytes): + path_list = fsdecode(path_list) + + if path_list is None: + path_list = defpath + return path_list.split(pathsep) + + +# Change environ to automatically call putenv(), unsetenv if they exist. +from _collections_abc import MutableMapping + +class _Environ(MutableMapping): + def __init__(self, data, encodekey, decodekey, encodevalue, decodevalue, putenv, unsetenv): + self.encodekey = encodekey + self.decodekey = decodekey + self.encodevalue = encodevalue + self.decodevalue = decodevalue + self.putenv = putenv + self.unsetenv = unsetenv + self._data = data + + def __getitem__(self, key): + try: + value = self._data[self.encodekey(key)] + except KeyError: + # raise KeyError with the original key value + raise KeyError(key) from None + return self.decodevalue(value) + + def __setitem__(self, key, value): + key = self.encodekey(key) + value = self.encodevalue(value) + self.putenv(key, value) + self._data[key] = value + + def __delitem__(self, key): + encodedkey = self.encodekey(key) + self.unsetenv(encodedkey) + try: + del self._data[encodedkey] + except KeyError: + # raise KeyError with the original key value + raise KeyError(key) from None + + def __iter__(self): + # list() from dict object is an atomic operation + keys = list(self._data) + for key in keys: + yield self.decodekey(key) + + def __len__(self): + return len(self._data) + + def __repr__(self): + return 'environ({{{}}})'.format(', '.join( + ('{!r}: {!r}'.format(self.decodekey(key), self.decodevalue(value)) + for key, value in self._data.items()))) + + def copy(self): + return dict(self) + + def setdefault(self, key, value): + if key not in self: + self[key] = value + return self[key] + +try: + _putenv = putenv +except NameError: + _putenv = lambda key, value: None +else: + if "putenv" not in __all__: + __all__.append("putenv") + +try: + _unsetenv = unsetenv +except NameError: + _unsetenv = lambda key: _putenv(key, "") +else: + if "unsetenv" not in __all__: + __all__.append("unsetenv") + +def _createenviron(): + if name == 'nt': + # Where Env Var Names Must Be UPPERCASE + def check_str(value): + if not isinstance(value, str): + raise TypeError("str expected, not %s" % type(value).__name__) + return value + encode = check_str + decode = str + def encodekey(key): + return encode(key).upper() + data = {} + for key, value in environ.items(): + data[encodekey(key)] = value + else: + # Where Env Var Names Can Be Mixed Case + encoding = sys.getfilesystemencoding() + def encode(value): + if not isinstance(value, str): + raise TypeError("str expected, not %s" % type(value).__name__) + return value.encode(encoding, 'surrogateescape') + def decode(value): + return value.decode(encoding, 'surrogateescape') + encodekey = encode + data = environ + return _Environ(data, + encodekey, decode, + encode, decode, + _putenv, _unsetenv) + +# unicode environ +environ = _createenviron() +del _createenviron + + +def getenv(key, default=None): + """Get an environment variable, return None if it doesn't exist. + The optional second argument can specify an alternate default. + key, default and the result are str.""" + return environ.get(key, default) + +supports_bytes_environ = (name != 'nt') +__all__.extend(("getenv", "supports_bytes_environ")) + +if supports_bytes_environ: + def _check_bytes(value): + if not isinstance(value, bytes): + raise TypeError("bytes expected, not %s" % type(value).__name__) + return value + + # bytes environ + environb = _Environ(environ._data, + _check_bytes, bytes, + _check_bytes, bytes, + _putenv, _unsetenv) + del _check_bytes + + def getenvb(key, default=None): + """Get an environment variable, return None if it doesn't exist. + The optional second argument can specify an alternate default. + key, default and the result are bytes.""" + return environb.get(key, default) + + __all__.extend(("environb", "getenvb")) + +def _fscodec(): + encoding = sys.getfilesystemencoding() + errors = sys.getfilesystemencodeerrors() + + def fsencode(filename): + """Encode filename (an os.PathLike, bytes, or str) to the filesystem + encoding with 'surrogateescape' error handler, return bytes unchanged. + On Windows, use 'strict' error handler if the file system encoding is + 'mbcs' (which is the default encoding). + """ + filename = fspath(filename) # Does type-checking of `filename`. + if isinstance(filename, str): + return filename.encode(encoding, errors) + else: + return filename + + def fsdecode(filename): + """Decode filename (an os.PathLike, bytes, or str) from the filesystem + encoding with 'surrogateescape' error handler, return str unchanged. On + Windows, use 'strict' error handler if the file system encoding is + 'mbcs' (which is the default encoding). + """ + filename = fspath(filename) # Does type-checking of `filename`. + if isinstance(filename, bytes): + return filename.decode(encoding, errors) + else: + return filename + + return fsencode, fsdecode + +fsencode, fsdecode = _fscodec() +del _fscodec + +# Supply spawn*() (probably only for Unix) +if _exists("fork") and not _exists("spawnv") and _exists("execv"): + + P_WAIT = 0 + P_NOWAIT = P_NOWAITO = 1 + + __all__.extend(["P_WAIT", "P_NOWAIT", "P_NOWAITO"]) + + # XXX Should we support P_DETACH? I suppose it could fork()**2 + # and close the std I/O streams. Also, P_OVERLAY is the same + # as execv*()? + + def _spawnvef(mode, file, args, env, func): + # Internal helper; func is the exec*() function to use + if not isinstance(args, (tuple, list)): + raise TypeError('argv must be a tuple or a list') + if not args or not args[0]: + raise ValueError('argv first element cannot be empty') + pid = fork() + if not pid: + # Child + try: + if env is None: + func(file, args) + else: + func(file, args, env) + except: + _exit(127) + else: + # Parent + if mode == P_NOWAIT: + return pid # Caller is responsible for waiting! + while 1: + wpid, sts = waitpid(pid, 0) + if WIFSTOPPED(sts): + continue + elif WIFSIGNALED(sts): + return -WTERMSIG(sts) + elif WIFEXITED(sts): + return WEXITSTATUS(sts) + else: + raise OSError("Not stopped, signaled or exited???") + + def spawnv(mode, file, args): + """spawnv(mode, file, args) -> integer + +Execute file with arguments from args in a subprocess. +If mode == P_NOWAIT return the pid of the process. +If mode == P_WAIT return the process's exit code if it exits normally; +otherwise return -SIG, where SIG is the signal that killed it. """ + return _spawnvef(mode, file, args, None, execv) + + def spawnve(mode, file, args, env): + """spawnve(mode, file, args, env) -> integer + +Execute file with arguments from args in a subprocess with the +specified environment. +If mode == P_NOWAIT return the pid of the process. +If mode == P_WAIT return the process's exit code if it exits normally; +otherwise return -SIG, where SIG is the signal that killed it. """ + return _spawnvef(mode, file, args, env, execve) + + # Note: spawnvp[e] isn't currently supported on Windows + + def spawnvp(mode, file, args): + """spawnvp(mode, file, args) -> integer + +Execute file (which is looked for along $PATH) with arguments from +args in a subprocess. +If mode == P_NOWAIT return the pid of the process. +If mode == P_WAIT return the process's exit code if it exits normally; +otherwise return -SIG, where SIG is the signal that killed it. """ + return _spawnvef(mode, file, args, None, execvp) + + def spawnvpe(mode, file, args, env): + """spawnvpe(mode, file, args, env) -> integer + +Execute file (which is looked for along $PATH) with arguments from +args in a subprocess with the supplied environment. +If mode == P_NOWAIT return the pid of the process. +If mode == P_WAIT return the process's exit code if it exits normally; +otherwise return -SIG, where SIG is the signal that killed it. """ + return _spawnvef(mode, file, args, env, execvpe) + + + __all__.extend(["spawnv", "spawnve", "spawnvp", "spawnvpe"]) + + +if _exists("spawnv"): + # These aren't supplied by the basic Windows code + # but can be easily implemented in Python + + def spawnl(mode, file, *args): + """spawnl(mode, file, *args) -> integer + +Execute file with arguments from args in a subprocess. +If mode == P_NOWAIT return the pid of the process. +If mode == P_WAIT return the process's exit code if it exits normally; +otherwise return -SIG, where SIG is the signal that killed it. """ + return spawnv(mode, file, args) + + def spawnle(mode, file, *args): + """spawnle(mode, file, *args, env) -> integer + +Execute file with arguments from args in a subprocess with the +supplied environment. +If mode == P_NOWAIT return the pid of the process. +If mode == P_WAIT return the process's exit code if it exits normally; +otherwise return -SIG, where SIG is the signal that killed it. """ + env = args[-1] + return spawnve(mode, file, args[:-1], env) + + + __all__.extend(["spawnl", "spawnle"]) + + +if _exists("spawnvp"): + # At the moment, Windows doesn't implement spawnvp[e], + # so it won't have spawnlp[e] either. + def spawnlp(mode, file, *args): + """spawnlp(mode, file, *args) -> integer + +Execute file (which is looked for along $PATH) with arguments from +args in a subprocess with the supplied environment. +If mode == P_NOWAIT return the pid of the process. +If mode == P_WAIT return the process's exit code if it exits normally; +otherwise return -SIG, where SIG is the signal that killed it. """ + return spawnvp(mode, file, args) + + def spawnlpe(mode, file, *args): + """spawnlpe(mode, file, *args, env) -> integer + +Execute file (which is looked for along $PATH) with arguments from +args in a subprocess with the supplied environment. +If mode == P_NOWAIT return the pid of the process. +If mode == P_WAIT return the process's exit code if it exits normally; +otherwise return -SIG, where SIG is the signal that killed it. """ + env = args[-1] + return spawnvpe(mode, file, args[:-1], env) + + + __all__.extend(["spawnlp", "spawnlpe"]) + + +# Supply os.popen() +def popen(cmd, mode="r", buffering=-1): + if not isinstance(cmd, str): + raise TypeError("invalid cmd type (%s, expected string)" % type(cmd)) + if mode not in ("r", "w"): + raise ValueError("invalid mode %r" % mode) + if buffering == 0 or buffering is None: + raise ValueError("popen() does not support unbuffered streams") + import subprocess, io + if mode == "r": + proc = subprocess.Popen(cmd, + shell=True, + stdout=subprocess.PIPE, + bufsize=buffering) + return _wrap_close(io.TextIOWrapper(proc.stdout), proc) + else: + proc = subprocess.Popen(cmd, + shell=True, + stdin=subprocess.PIPE, + bufsize=buffering) + return _wrap_close(io.TextIOWrapper(proc.stdin), proc) + +# Helper for popen() -- a proxy for a file whose close waits for the process +class _wrap_close: + def __init__(self, stream, proc): + self._stream = stream + self._proc = proc + def close(self): + self._stream.close() + returncode = self._proc.wait() + if returncode == 0: + return None + if name == 'nt': + return returncode + else: + return returncode << 8 # Shift left to match old behavior + def __enter__(self): + return self + def __exit__(self, *args): + self.close() + def __getattr__(self, name): + return getattr(self._stream, name) + def __iter__(self): + return iter(self._stream) + +# Supply os.fdopen() +def fdopen(fd, *args, **kwargs): + if not isinstance(fd, int): + raise TypeError("invalid fd type (%s, expected integer)" % type(fd)) + import io + return io.open(fd, *args, **kwargs) + + +# For testing purposes, make sure the function is available when the C +# implementation exists. +def _fspath(path): + """Return the path representation of a path-like object. + + If str or bytes is passed in, it is returned unchanged. Otherwise the + os.PathLike interface is used to get the path representation. If the + path representation is not str or bytes, TypeError is raised. If the + provided path is not str, bytes, or os.PathLike, TypeError is raised. + """ + if isinstance(path, (str, bytes)): + return path + + # Work from the object's type to match method resolution of other magic + # methods. + path_type = type(path) + try: + path_repr = path_type.__fspath__(path) + except AttributeError: + if hasattr(path_type, '__fspath__'): + raise + else: + raise TypeError("expected str, bytes or os.PathLike object, " + "not " + path_type.__name__) + if isinstance(path_repr, (str, bytes)): + return path_repr + else: + raise TypeError("expected {}.__fspath__() to return str or bytes, " + "not {}".format(path_type.__name__, + type(path_repr).__name__)) + +# If there is no C implementation, make the pure Python version the +# implementation as transparently as possible. +if not _exists('fspath'): + fspath = _fspath + fspath.__name__ = "fspath" + + +class PathLike(abc.ABC): + + """Abstract base class for implementing the file system path protocol.""" + + @abc.abstractmethod + def __fspath__(self): + """Return the file system path representation of the object.""" + raise NotImplementedError + + @classmethod + def __subclasshook__(cls, subclass): + return hasattr(subclass, '__fspath__') diff --git a/python/testData/MockSdk3.4/Lib/re.py b/python/testData/MockSdk3.7/Lib/re.py similarity index 76% rename from python/testData/MockSdk3.4/Lib/re.py rename to python/testData/MockSdk3.7/Lib/re.py index 2e4d87c3f5c8..94d486579e08 100644 --- a/python/testData/MockSdk3.4/Lib/re.py +++ b/python/testData/MockSdk3.7/Lib/re.py @@ -92,8 +92,8 @@ This module exports the following functions: subn Same as sub, but also return the number of substitutions made. split Split a string by the occurrences of a pattern. findall Find all occurrences of a pattern in a string. - finditer Return an iterator yielding a match object for each match. - compile Compile a pattern into a RegexObject. + finditer Return an iterator yielding a Match object for each match. + compile Compile a pattern into a Pattern object. purge Clear the regular expression cache. escape Backslash all non-alphanumerics in a string. @@ -119,30 +119,47 @@ This module also defines an exception 'error'. """ -import sys +import enum import sre_compile import sre_parse +import functools +try: + import _locale +except ImportError: + _locale = None + # public symbols -__all__ = [ "match", "fullmatch", "search", "sub", "subn", "split", "findall", - "compile", "purge", "template", "escape", "A", "I", "L", "M", "S", "X", - "U", "ASCII", "IGNORECASE", "LOCALE", "MULTILINE", "DOTALL", "VERBOSE", - "UNICODE", "error" ] +__all__ = [ + "match", "fullmatch", "search", "sub", "subn", "split", + "findall", "finditer", "compile", "purge", "template", "escape", + "error", "Pattern", "Match", "A", "I", "L", "M", "S", "X", "U", + "ASCII", "IGNORECASE", "LOCALE", "MULTILINE", "DOTALL", "VERBOSE", + "UNICODE", +] __version__ = "2.2.1" -# flags -A = ASCII = sre_compile.SRE_FLAG_ASCII # assume ascii "locale" -I = IGNORECASE = sre_compile.SRE_FLAG_IGNORECASE # ignore case -L = LOCALE = sre_compile.SRE_FLAG_LOCALE # assume current 8-bit locale -U = UNICODE = sre_compile.SRE_FLAG_UNICODE # assume unicode "locale" -M = MULTILINE = sre_compile.SRE_FLAG_MULTILINE # make anchors look for newline -S = DOTALL = sre_compile.SRE_FLAG_DOTALL # make dot match newline -X = VERBOSE = sre_compile.SRE_FLAG_VERBOSE # ignore whitespace and comments - -# sre extensions (experimental, don't rely on these) -T = TEMPLATE = sre_compile.SRE_FLAG_TEMPLATE # disable backtracking -DEBUG = sre_compile.SRE_FLAG_DEBUG # dump pattern after compilation +class RegexFlag(enum.IntFlag): + ASCII = sre_compile.SRE_FLAG_ASCII # assume ascii "locale" + IGNORECASE = sre_compile.SRE_FLAG_IGNORECASE # ignore case + LOCALE = sre_compile.SRE_FLAG_LOCALE # assume current 8-bit locale + UNICODE = sre_compile.SRE_FLAG_UNICODE # assume unicode "locale" + MULTILINE = sre_compile.SRE_FLAG_MULTILINE # make anchors look for newline + DOTALL = sre_compile.SRE_FLAG_DOTALL # make dot match newline + VERBOSE = sre_compile.SRE_FLAG_VERBOSE # ignore whitespace and comments + A = ASCII + I = IGNORECASE + L = LOCALE + U = UNICODE + M = MULTILINE + S = DOTALL + X = VERBOSE + # sre extensions (experimental, don't rely on these) + TEMPLATE = sre_compile.SRE_FLAG_TEMPLATE # disable backtracking + T = TEMPLATE + DEBUG = sre_compile.SRE_FLAG_DEBUG # dump pattern after compilation +globals().update(RegexFlag.__members__) # sre exception error = sre_compile.error @@ -152,17 +169,17 @@ error = sre_compile.error def match(pattern, string, flags=0): """Try to apply the pattern at the start of the string, returning - a match object, or None if no match was found.""" + a Match object, or None if no match was found.""" return _compile(pattern, flags).match(string) def fullmatch(pattern, string, flags=0): """Try to apply the pattern to all of the string, returning - a match object, or None if no match was found.""" + a Match object, or None if no match was found.""" return _compile(pattern, flags).fullmatch(string) def search(pattern, string, flags=0): """Scan through string looking for a match to the pattern, returning - a match object, or None if no match was found.""" + a Match object, or None if no match was found.""" return _compile(pattern, flags).search(string) def sub(pattern, repl, string, count=0, flags=0): @@ -170,7 +187,7 @@ def sub(pattern, repl, string, count=0, flags=0): non-overlapping occurrences of the pattern in string by the replacement repl. repl can be either a string or a callable; if a string, backslash escapes in it are processed. If it is - a callable, it's passed the match object and must return + a callable, it's passed the Match object and must return a replacement string to be used.""" return _compile(pattern, flags).sub(repl, string, count) @@ -181,7 +198,7 @@ def subn(pattern, repl, string, count=0, flags=0): string by the replacement repl. number is the number of substitutions that were made. repl can be either a string or a callable; if a string, backslash escapes in it are processed. - If it is a callable, it's passed the match object and must + If it is a callable, it's passed the Match object and must return a replacement string to be used.""" return _compile(pattern, flags).subn(repl, string, count) @@ -205,112 +222,90 @@ def findall(pattern, string, flags=0): Empty matches are included in the result.""" return _compile(pattern, flags).findall(string) -if sys.hexversion >= 0x02020000: - __all__.append("finditer") - def finditer(pattern, string, flags=0): - """Return an iterator over all non-overlapping matches in the - string. For each match, the iterator returns a match object. +def finditer(pattern, string, flags=0): + """Return an iterator over all non-overlapping matches in the + string. For each match, the iterator returns a Match object. - Empty matches are included in the result.""" - return _compile(pattern, flags).finditer(string) + Empty matches are included in the result.""" + return _compile(pattern, flags).finditer(string) def compile(pattern, flags=0): - "Compile a regular expression pattern, returning a pattern object." + "Compile a regular expression pattern, returning a Pattern object." return _compile(pattern, flags) def purge(): "Clear the regular expression caches" _cache.clear() - _cache_repl.clear() + _compile_repl.cache_clear() def template(pattern, flags=0): - "Compile a template pattern, returning a pattern object" + "Compile a template pattern, returning a Pattern object" return _compile(pattern, flags|T) -_alphanum_str = frozenset( - "_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890") -_alphanum_bytes = frozenset( - b"_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890") +# SPECIAL_CHARS +# closing ')', '}' and ']' +# '-' (a range in character set) +# '&', '~', (extended character set operations) +# '#' (comment) and WHITESPACE (ignored) in verbose mode +_special_chars_map = {i: '\\' + chr(i) for i in b'()[]{}?*+-|^$\\.&~# \t\n\r\v\f'} def escape(pattern): """ - Escape all the characters in pattern except ASCII letters, numbers and '_'. + Escape special characters in a string. """ if isinstance(pattern, str): - alphanum = _alphanum_str - s = list(pattern) - for i, c in enumerate(pattern): - if c not in alphanum: - if c == "\000": - s[i] = "\\000" - else: - s[i] = "\\" + c - return "".join(s) + return pattern.translate(_special_chars_map) else: - alphanum = _alphanum_bytes - s = [] - esc = ord(b"\\") - for c in pattern: - if c in alphanum: - s.append(c) - else: - if c == 0: - s.extend(b"\\000") - else: - s.append(esc) - s.append(c) - return bytes(s) + pattern = str(pattern, 'latin1') + return pattern.translate(_special_chars_map).encode('latin1') + +Pattern = type(sre_compile.compile('', 0)) +Match = type(sre_compile.compile('', 0).match('')) # -------------------------------------------------------------------- # internals -_cache = {} -_cache_repl = {} - -_pattern_type = type(sre_compile.compile("", 0)) +_cache = {} # ordered! _MAXCACHE = 512 def _compile(pattern, flags): # internal: compile pattern - bypass_cache = flags & DEBUG - if not bypass_cache: - try: - return _cache[type(pattern), pattern, flags] - except KeyError: - pass - if isinstance(pattern, _pattern_type): + if isinstance(flags, RegexFlag): + flags = flags.value + try: + return _cache[type(pattern), pattern, flags] + except KeyError: + pass + if isinstance(pattern, Pattern): if flags: raise ValueError( - "Cannot process flags argument with a compiled pattern") + "cannot process flags argument with a compiled pattern") return pattern if not sre_compile.isstring(pattern): raise TypeError("first argument must be string or compiled pattern") p = sre_compile.compile(pattern, flags) - if not bypass_cache: + if not (flags & DEBUG): if len(_cache) >= _MAXCACHE: - _cache.clear() + # Drop the oldest item + try: + del _cache[next(iter(_cache))] + except (StopIteration, RuntimeError, KeyError): + pass _cache[type(pattern), pattern, flags] = p return p +@functools.lru_cache(_MAXCACHE) def _compile_repl(repl, pattern): # internal: compile replacement pattern - try: - return _cache_repl[repl, pattern] - except KeyError: - pass - p = sre_parse.parse_template(repl, pattern) - if len(_cache_repl) >= _MAXCACHE: - _cache_repl.clear() - _cache_repl[repl, pattern] = p - return p + return sre_parse.parse_template(repl, pattern) def _expand(pattern, match, template): - # internal: match.expand implementation hook + # internal: Match.expand implementation hook template = sre_parse.parse_template(template, pattern) return sre_parse.expand_template(template, match) def _subx(pattern, template): - # internal: pattern.sub/subn implementation helper + # internal: Pattern.sub/subn implementation helper template = _compile_repl(template, pattern) if not template[0] and len(template[1]) == 1: # literal replacement @@ -326,7 +321,7 @@ import copyreg def _pickle(p): return _compile, (p.pattern, p.flags) -copyreg.pickle(_pattern_type, _pickle, _compile) +copyreg.pickle(Pattern, _pickle, _compile) # -------------------------------------------------------------------- # experimental stuff (see python-dev discussions for details) @@ -334,16 +329,19 @@ copyreg.pickle(_pattern_type, _pickle, _compile) class Scanner: def __init__(self, lexicon, flags=0): from sre_constants import BRANCH, SUBPATTERN + if isinstance(flags, RegexFlag): + flags = flags.value self.lexicon = lexicon # combine phrases into a compound pattern p = [] s = sre_parse.Pattern() s.flags = flags for phrase, action in lexicon: + gid = s.opengroup() p.append(sre_parse.SubPattern(s, [ - (SUBPATTERN, (len(p)+1, sre_parse.parse(phrase, flags))), + (SUBPATTERN, (gid, 0, 0, sre_parse.parse(phrase, flags))), ])) - s.groups = len(p)+1 + s.closegroup(gid, p[-1]) p = sre_parse.SubPattern(s, [(BRANCH, (None, p))]) self.scanner = sre_compile.compile(p) def scan(self, string): @@ -351,7 +349,7 @@ class Scanner: append = result.append match = self.scanner.scanner(string).match i = 0 - while 1: + while True: m = match() if not m: break diff --git a/python/testData/MockSdk3.4/bin/python3.4 b/python/testData/MockSdk3.7/bin/python3.7 similarity index 100% rename from python/testData/MockSdk3.4/bin/python3.4 rename to python/testData/MockSdk3.7/bin/python3.7 diff --git a/python/testData/MockSdk3.4/python_stubs/_io.py b/python/testData/MockSdk3.7/python_stubs/_io.py similarity index 80% rename from python/testData/MockSdk3.4/python_stubs/_io.py rename to python/testData/MockSdk3.7/python_stubs/_io.py index 868bcb114d8a..dc877209e8d4 100644 --- a/python/testData/MockSdk3.4/python_stubs/_io.py +++ b/python/testData/MockSdk3.7/python_stubs/_io.py @@ -1,7 +1,7 @@ # encoding: utf-8 # module _io calls itself io # from (built-in) -# by generator 1.138 +# by generator 1.145 """ The io module provides the Python interfaces to stream handling. The builtin open function is defined in this module. @@ -9,7 +9,7 @@ builtin open function is defined in this module. At the top of the I/O hierarchy is the abstract base class IOBase. It defines the basic interface to a stream. Note, however, that there is no separation between reading and writing to streams; implementations are -allowed to raise an IOError if they do not support a given operation. +allowed to raise an OSError if they do not support a given operation. Extending IOBase is RawIOBase which deals simply with the reading and writing of raw bytes to a stream. FileIO subclasses RawIOBase to provide @@ -24,7 +24,7 @@ streams. BytesIO is a simple stream of in-memory bytes. Another IOBase subclass, TextIOBase, deals with the encoding and decoding of streams into text. TextIOWrapper, which extends it, is a buffered text interface to a buffered raw stream (`BufferedIOBase`). Finally, StringIO -is a in-memory stream for text. +is an in-memory stream for text. Argument names are not part of the specification, and only the arguments of open() are intended to be used as keyword arguments. @@ -47,10 +47,7 @@ DEFAULT_BUFFER_SIZE = 8192 def open(name, mode=None, buffering=None): # known case of _io.open """ - open(file, mode='r', buffering=-1, encoding=None, - errors=None, newline=None, closefd=True, opener=None) -> file object - - Open file and return a stream. Raise IOError upon failure. + Open file and return a stream. Raise OSError upon failure. file is either a text or byte string giving the name (and the path if the file isn't in the current working directory) of the file to @@ -195,8 +192,9 @@ class _IOBase(object): called. The basic type used for binary data read from or written to a file is - bytes. bytearrays are accepted too, and in some cases (such as - readinto) needed. Text I/O classes work with str data. + bytes. Other bytes-like objects are accepted as method arguments too. + In some cases (such as readinto), a writable object is required. Text + I/O classes work with str data. Note that calling any method (except additional calls to close(), which are ignored) on a closed stream should raise a ValueError. @@ -223,7 +221,7 @@ class _IOBase(object): """ Returns underlying file descriptor if one exists. - An IOError is raised if the IO object does not use a file descriptor. + OSError is raised if the IO object does not use a file descriptor. """ pass @@ -247,7 +245,7 @@ class _IOBase(object): """ Return whether object was opened for reading. - If False, read() will raise UnsupportedOperation. + If False, read() will raise OSError. """ pass @@ -255,7 +253,7 @@ class _IOBase(object): """ Read and return a line from the stream. - If limit is specified, at most limit bytes will be read. + If size is specified, at most size bytes will be read. The line terminator is always b'\n' for binary files; for text files, the newlines argument to open can be used to select the line @@ -293,7 +291,7 @@ class _IOBase(object): """ Return whether object supports random access. - If False, seek(), tell() and truncate() will raise UnsupportedOperation. + If False, seek(), tell() and truncate() will raise OSError. This method may need to do a test seek(). """ pass @@ -315,7 +313,7 @@ class _IOBase(object): """ Return whether object was opened for writing. - If False, write() will raise UnsupportedOperation. + If False, write() will raise OSError. """ pass @@ -335,7 +333,6 @@ class _IOBase(object): pass def __del__(self, *args, **kwargs): # real signature unknown - """ """ pass def __enter__(self, *args, **kwargs): # real signature unknown @@ -425,12 +422,15 @@ class _BufferedIOBase(_IOBase): def readinto(self, *args, **kwargs): # real signature unknown pass + def readinto1(self, *args, **kwargs): # real signature unknown + pass + def write(self, *args, **kwargs): # real signature unknown """ Write the given buffer to the IO stream. - Returns the number of bytes written, which is never less than - len(b). + Returns the number of bytes written, which is always the length of b + in bytes. Raises BlockingIOError if the buffer is full and the underlying raw stream cannot accept more data at the moment. @@ -479,6 +479,9 @@ class BufferedRandom(_BufferedIOBase): def readinto(self, *args, **kwargs): # real signature unknown pass + def readinto1(self, *args, **kwargs): # real signature unknown + pass + def readline(self, *args, **kwargs): # real signature unknown pass @@ -569,6 +572,9 @@ class BufferedReader(_BufferedIOBase): def readinto(self, *args, **kwargs): # real signature unknown pass + def readinto1(self, *args, **kwargs): # real signature unknown + pass + def readline(self, *args, **kwargs): # real signature unknown pass @@ -584,9 +590,6 @@ class BufferedReader(_BufferedIOBase): def truncate(self, *args, **kwargs): # real signature unknown pass - def writable(self, *args, **kwargs): # real signature unknown - pass - def _dealloc_warn(self, *args, **kwargs): # real signature unknown pass @@ -660,6 +663,9 @@ class BufferedRWPair(_BufferedIOBase): def readinto(self, *args, **kwargs): # real signature unknown pass + def readinto1(self, *args, **kwargs): # real signature unknown + pass + def writable(self, *args, **kwargs): # real signature unknown pass @@ -704,9 +710,6 @@ class BufferedWriter(_BufferedIOBase): def isatty(self, *args, **kwargs): # real signature unknown pass - def readable(self, *args, **kwargs): # real signature unknown - pass - def seek(self, *args, **kwargs): # real signature unknown pass @@ -759,99 +762,85 @@ class BufferedWriter(_BufferedIOBase): class BytesIO(_BufferedIOBase): - """ - BytesIO([buffer]) -> object - - Create a buffered I/O implementation using an in-memory bytes - buffer, ready for reading and writing. - """ - def close(self): # real signature unknown; restored from __doc__ - """ close() -> None. Disable all I/O operations. """ + """ Buffered I/O implementation using an in-memory bytes buffer. """ + def close(self, *args, **kwargs): # real signature unknown + """ Disable all I/O operations. """ pass - def flush(self): # real signature unknown; restored from __doc__ - """ flush() -> None. Does nothing. """ + def flush(self, *args, **kwargs): # real signature unknown + """ Does nothing. """ pass - def getbuffer(self): # real signature unknown; restored from __doc__ + def getbuffer(self, *args, **kwargs): # real signature unknown + """ Get a read-write view over the contents of the BytesIO object. """ + pass + + def getvalue(self, *args, **kwargs): # real signature unknown + """ Retrieve the entire contents of the BytesIO object. """ + pass + + def isatty(self, *args, **kwargs): # real signature unknown """ - getbuffer() -> bytes. + Always returns False. - Get a read-write view over the contents of the BytesIO object. + BytesIO objects are not connected to a TTY-like device. """ pass - def getvalue(self): # real signature unknown; restored from __doc__ + def read(self, *args, **kwargs): # real signature unknown """ - getvalue() -> bytes. - - Retrieve the entire contents of the BytesIO object. - """ - pass - - def isatty(self): # real signature unknown; restored from __doc__ - """ - isatty() -> False. - - Always returns False since BytesIO objects are not connected - to a tty-like device. - """ - pass - - def read(self, size=None): # real signature unknown; restored from __doc__ - """ - read([size]) -> read at most size bytes, returned as a string. + Read at most size bytes, returned as a bytes object. If the size argument is negative, read until EOF is reached. - Return an empty string at EOF. + Return an empty bytes object at EOF. """ pass - def read1(self, size): # real signature unknown; restored from __doc__ + def read1(self, *args, **kwargs): # real signature unknown """ - read1(size) -> read at most size bytes, returned as a string. + Read at most size bytes, returned as a bytes object. If the size argument is negative or omitted, read until EOF is reached. - Return an empty string at EOF. + Return an empty bytes object at EOF. """ pass - def readable(self): # real signature unknown; restored from __doc__ - """ readable() -> bool. Returns True if the IO object can be read. """ + def readable(self, *args, **kwargs): # real signature unknown + """ Returns True if the IO object can be read. """ pass - def readinto(self, bytearray): # real signature unknown; restored from __doc__ + def readinto(self, *args, **kwargs): # real signature unknown """ - readinto(bytearray) -> int. Read up to len(b) bytes into b. + Read bytes into buffer. Returns number of bytes read (0 for EOF), or None if the object - is set not to block as has no data to read. + is set not to block and has no data to read. """ pass - def readline(self, size=None): # real signature unknown; restored from __doc__ + def readline(self, *args, **kwargs): # real signature unknown """ - readline([size]) -> next line from the file, as a string. + Next line from the file, as a bytes object. Retain newline. A non-negative size argument limits the maximum number of bytes to return (an incomplete line may be returned then). - Return an empty string at EOF. + Return an empty bytes object at EOF. """ pass - def readlines(self, size=None): # real signature unknown; restored from __doc__ + def readlines(self, *args, **kwargs): # real signature unknown """ - readlines([size]) -> list of strings, each a line from the file. + List of bytes objects, each a line from the file. Call readline() repeatedly and return a list of the lines so read. The optional size argument, if given, is an approximate bound on the total number of bytes in the lines returned. """ - return [] + pass - def seek(self, pos, whence=0): # real signature unknown; restored from __doc__ + def seek(self, *args, **kwargs): # real signature unknown """ - seek(pos, whence=0) -> int. Change stream position. + Change stream position. Seek to byte offset pos relative to position indicated by whence: 0 Start of stream (the default). pos should be >= 0; @@ -861,49 +850,49 @@ class BytesIO(_BufferedIOBase): """ pass - def seekable(self): # real signature unknown; restored from __doc__ - """ seekable() -> bool. Returns True if the IO object can be seeked. """ + def seekable(self, *args, **kwargs): # real signature unknown + """ Returns True if the IO object can be seeked. """ pass - def tell(self): # real signature unknown; restored from __doc__ - """ tell() -> current file position, an integer """ + def tell(self, *args, **kwargs): # real signature unknown + """ Current file position, an integer. """ pass - def truncate(self, size=None): # real signature unknown; restored from __doc__ + def truncate(self, *args, **kwargs): # real signature unknown """ - truncate([size]) -> int. Truncate the file to at most size bytes. + Truncate the file to at most size bytes. Size defaults to the current file position, as returned by tell(). The current file position is unchanged. Returns the new size. """ pass - def writable(self): # real signature unknown; restored from __doc__ - """ writable() -> bool. Returns True if the IO object can be written. """ + def writable(self, *args, **kwargs): # real signature unknown + """ Returns True if the IO object can be written. """ pass - def write(self, bytes): # real signature unknown; restored from __doc__ + def write(self, *args, **kwargs): # real signature unknown """ - write(bytes) -> int. Write bytes to file. + Write bytes to file. Return the number of bytes written. """ pass - def writelines(self, sequence_of_strings): # real signature unknown; restored from __doc__ + def writelines(self, *args, **kwargs): # real signature unknown """ - writelines(sequence_of_strings) -> None. Write strings to the file. + Write lines to the file. - Note that newlines are not added. The sequence can be any iterable - object producing strings. This is equivalent to calling write() for - each string. + Note that newlines are not added. lines can be any iterable object + producing bytes-like objects. This is equivalent to calling write() for + each element. """ pass def __getstate__(self, *args, **kwargs): # real signature unknown pass - def __init__(self, buffer=None): # real signature unknown; restored from __doc__ + def __init__(self, *args, **kwargs): # real signature unknown pass def __iter__(self, *args, **kwargs): # real signature unknown @@ -939,18 +928,24 @@ class _RawIOBase(_IOBase): """ Read until EOF, using multiple read() call. """ pass + def readinto(self, *args, **kwargs): # real signature unknown + pass + + def write(self, *args, **kwargs): # real signature unknown + pass + def __init__(self, *args, **kwargs): # real signature unknown pass class FileIO(_RawIOBase): """ - file(name: str[, mode: str][, opener: None]) -> file IO object + Open a file. - Open a file. The mode can be 'r', 'w', 'x' or 'a' for reading (default), + The mode can be 'r' (default), 'w', 'x' or 'a' for reading, writing, exclusive creation or appending. The file will be created if it doesn't exist when opened for writing or appending; it will be truncated - when opened for writing. A `FileExistsError` will be raised if it already + when opened for writing. A FileExistsError will be raised if it already exists when opened for creating. Opening a file for creating implies writing so this mode behaves in a similar way to 'w'.Add a '+' to the mode to allow simultaneous reading and writing. A custom opener can be used by @@ -961,91 +956,94 @@ class FileIO(_RawIOBase): """ def close(self): # real signature unknown; restored from __doc__ """ - close() -> None. Close the file. + Close the file. A closed file cannot be used for further I/O operations. close() may be - called more than once without error. Changes the fileno to -1. + called more than once without error. """ pass - def fileno(self): # real signature unknown; restored from __doc__ - """ - fileno() -> int. "file descriptor". - - This is needed for lower-level file interfaces, such the fcntl module. - """ + def fileno(self, *args, **kwargs): # real signature unknown + """ Return the underlying file descriptor (an integer). """ pass - def isatty(self): # real signature unknown; restored from __doc__ - """ isatty() -> bool. True if the file is connected to a tty device. """ + def isatty(self, *args, **kwargs): # real signature unknown + """ True if the file is connected to a TTY device. """ pass def read(self, size=-1): # known case of _io.FileIO.read """ - read(size: int) -> bytes. read at most size bytes, returned as bytes. + Read at most size bytes, returned as bytes. - Only makes one system call, so less data may be returned than requested + Only makes one system call, so less data may be returned than requested. In non-blocking mode, returns None if no data is available. - On end-of-file, returns ''. + Return an empty bytes object at EOF. """ return "" - def readable(self): # real signature unknown; restored from __doc__ - """ readable() -> bool. True if file was opened in a read mode. """ + def readable(self, *args, **kwargs): # real signature unknown + """ True if file was opened in a read mode. """ pass - def readall(self): # real signature unknown; restored from __doc__ + def readall(self, *args, **kwargs): # real signature unknown """ - readall() -> bytes. read all data from the file, returned as bytes. + Read all data from the file, returned as bytes. In non-blocking mode, returns as much as is immediately available, - or None if no data is available. On end-of-file, returns ''. + or None if no data is available. Return an empty bytes object at EOF. """ pass def readinto(self): # real signature unknown; restored from __doc__ - """ readinto() -> Same as RawIOBase.readinto(). """ + """ Same as RawIOBase.readinto(). """ pass - def seek(self, offset, whence=None): # real signature unknown; restored from __doc__ + def seek(self, *args, **kwargs): # real signature unknown """ - seek(offset: int[, whence: int]) -> None. Move to new file position. + Move to new file position and return the file position. Argument offset is a byte count. Optional argument whence defaults to - 0 (offset from start of file, offset should be >= 0); other values are 1 - (move relative to current position, positive or negative), and 2 (move - relative to end of file, usually negative, although many platforms allow - seeking beyond the end of a file). + SEEK_SET or 0 (offset from start of file, offset should be >= 0); other values + are SEEK_CUR or 1 (move relative to current position, positive or negative), + and SEEK_END or 2 (move relative to end of file, usually negative, although + many platforms allow seeking beyond the end of a file). + Note that not all file objects are seekable. """ pass - def seekable(self): # real signature unknown; restored from __doc__ - """ seekable() -> bool. True if file supports random-access. """ + def seekable(self, *args, **kwargs): # real signature unknown + """ True if file supports random-access. """ pass - def tell(self): # real signature unknown; restored from __doc__ - """ tell() -> int. Current file position """ - pass - - def truncate(self, size=None): # real signature unknown; restored from __doc__ + def tell(self, *args, **kwargs): # real signature unknown """ - truncate([size: int]) -> None. Truncate the file to at most size bytes. + Current file position. - Size defaults to the current file position, as returned by tell().The current file position is changed to the value of size. + Can raise OSError for non seekable files. """ pass - def writable(self): # real signature unknown; restored from __doc__ - """ writable() -> bool. True if file was opened in a write mode. """ + def truncate(self, *args, **kwargs): # real signature unknown + """ + Truncate the file to at most size bytes and return the truncated size. + + Size defaults to the current file position, as returned by tell(). + The current file position is changed to the value of size. + """ pass - def write(self, b): # real signature unknown; restored from __doc__ + def writable(self, *args, **kwargs): # real signature unknown + """ True if file was opened in a write mode. """ + pass + + def write(self, *args, **kwargs): # real signature unknown """ - write(b: bytes) -> int. Write bytes b to file, return number written. + Write buffer b to file, return number of bytes written. Only makes one system call, so not all of the data may be written. - The number of bytes actually written is returned. + The number of bytes actually written is returned. In non-blocking mode, + returns None if the write would block. """ pass @@ -1075,20 +1073,23 @@ class FileIO(_RawIOBase): """True if the file is closed""" closefd = property(lambda self: object(), lambda self, v: None, lambda self: None) # default - """True if the file descriptor will be closed""" + """True if the file descriptor will be closed by close().""" mode = property(lambda self: object(), lambda self, v: None, lambda self: None) # default """String giving the file mode""" + _blksize = property(lambda self: object(), lambda self, v: None, lambda self: None) # default + _finalizing = property(lambda self: object(), lambda self, v: None, lambda self: None) # default class IncrementalNewlineDecoder(object): """ - Codec used when reading a file in universal newlines mode. It wraps - another incremental decoder, translating \r\n and \r into \n. It also - records the types of newlines encountered. When used with + Codec used when reading a file in universal newlines mode. + + It wraps another incremental decoder, translating \r\n and \r into \n. + It also records the types of newlines encountered. When used with translate=False, it ensures that the newline sequence is returned in one piece. When used with decoder=None, it expects unicode strings as decode input and translates newlines without first invoking an external @@ -1194,8 +1195,10 @@ class StringIO(_TextIOBase): """ def close(self, *args, **kwargs): # real signature unknown """ - Close the IO object. Attempting any further operation after the - object is closed will raise a ValueError. + Close the IO object. + + Attempting any further operation after the object is closed + will raise a ValueError. This method has no effect if the file is already closed. """ @@ -1207,15 +1210,15 @@ class StringIO(_TextIOBase): def read(self, *args, **kwargs): # real signature unknown """ - Read at most n characters, returned as a string. + Read at most size characters, returned as a string. If the argument is negative or omitted, read until EOF is reached. Return an empty string at EOF. """ pass - def readable(self): # real signature unknown; restored from __doc__ - """ readable() -> bool. Returns True if the IO object can be read. """ + def readable(self, *args, **kwargs): # real signature unknown + """ Returns True if the IO object can be read. """ pass def readline(self, *args, **kwargs): # real signature unknown @@ -1238,8 +1241,8 @@ class StringIO(_TextIOBase): """ pass - def seekable(self): # real signature unknown; restored from __doc__ - """ seekable() -> bool. Returns True if the IO object can be seeked. """ + def seekable(self, *args, **kwargs): # real signature unknown + """ Returns True if the IO object can be seeked. """ pass def tell(self, *args, **kwargs): # real signature unknown @@ -1256,8 +1259,8 @@ class StringIO(_TextIOBase): """ pass - def writable(self): # real signature unknown; restored from __doc__ - """ writable() -> bool. Returns True if the IO object can be written. """ + def writable(self, *args, **kwargs): # real signature unknown + """ Returns True if the IO object can be written. """ pass def write(self, *args, **kwargs): # real signature unknown @@ -1350,6 +1353,14 @@ class TextIOWrapper(_TextIOBase): def readline(self, *args, **kwargs): # real signature unknown pass + def reconfigure(self, *args, **kwargs): # real signature unknown + """ + Reconfigure the text stream with new parameters. + + This also does an implicit stream flush. + """ + pass + def seek(self, *args, **kwargs): # real signature unknown pass @@ -1401,13 +1412,15 @@ class TextIOWrapper(_TextIOBase): newlines = property(lambda self: object(), lambda self, v: None, lambda self: None) # default + write_through = property(lambda self: object(), lambda self, v: None, lambda self: None) # default + _CHUNK_SIZE = property(lambda self: object(), lambda self, v: None, lambda self: None) # default _finalizing = property(lambda self: object(), lambda self, v: None, lambda self: None) # default -class UnsupportedOperation(ValueError, OSError): +class UnsupportedOperation(OSError, ValueError): # no doc def __init__(self, *args, **kwargs): # real signature unknown pass @@ -1417,6 +1430,109 @@ class UnsupportedOperation(ValueError, OSError): +class _WindowsConsoleIO(_RawIOBase): + """ + Open a console buffer by file descriptor. + + The mode can be 'rb' (default), or 'wb' for reading or writing bytes. All + other mode characters will be ignored. Mode 'b' will be assumed if it is + omitted. The *opener* parameter is always ignored. + """ + def close(self): # real signature unknown; restored from __doc__ + """ + Close the handle. + + A closed handle cannot be used for further I/O operations. close() may be + called more than once without error. + """ + pass + + def fileno(self, *args, **kwargs): # real signature unknown + """ + Return the underlying file descriptor (an integer). + + fileno is only set when a file descriptor is used to open + one of the standard streams. + """ + pass + + def isatty(self, *args, **kwargs): # real signature unknown + """ Always True. """ + pass + + def read(self, *args, **kwargs): # real signature unknown + """ + Read at most size bytes, returned as bytes. + + Only makes one system call when size is a positive integer, + so less data may be returned than requested. + Return an empty bytes object at EOF. + """ + pass + + def readable(self, *args, **kwargs): # real signature unknown + """ True if console is an input buffer. """ + pass + + def readall(self, *args, **kwargs): # real signature unknown + """ + Read all data from the console, returned as bytes. + + Return an empty bytes object at EOF. + """ + pass + + def readinto(self): # real signature unknown; restored from __doc__ + """ Same as RawIOBase.readinto(). """ + pass + + def writable(self, *args, **kwargs): # real signature unknown + """ True if console is an output buffer. """ + pass + + def write(self, *args, **kwargs): # real signature unknown + """ + Write buffer b to file, return number of bytes written. + + Only makes one system call, so not all of the data may be written. + The number of bytes actually written is returned. + """ + pass + + def __getattribute__(self, *args, **kwargs): # real signature unknown + """ Return getattr(self, name). """ + pass + + def __getstate__(self, *args, **kwargs): # real signature unknown + pass + + def __init__(self, *args, **kwargs): # real signature unknown + pass + + @staticmethod # known case of __new__ + def __new__(*args, **kwargs): # real signature unknown + """ Create and return a new object. See help(type) for accurate signature. """ + pass + + def __repr__(self, *args, **kwargs): # real signature unknown + """ Return repr(self). """ + pass + + closed = property(lambda self: object(), lambda self, v: None, lambda self: None) # default + """True if the file is closed""" + + closefd = property(lambda self: object(), lambda self, v: None, lambda self: None) # default + """True if the file descriptor will be closed by close().""" + + mode = property(lambda self: object(), lambda self, v: None, lambda self: None) # default + """String giving the file mode""" + + _blksize = property(lambda self: object(), lambda self, v: None, lambda self: None) # default + + _finalizing = property(lambda self: object(), lambda self, v: None, lambda self: None) # default + + + class __loader__(object): """ Meta path import for built-in modules. @@ -1424,6 +1540,16 @@ class __loader__(object): All methods are either class or static methods to avoid the need to instantiate the class. """ + @classmethod + def create_module(cls, *args, **kwargs): # real signature unknown + """ Create a built-in module """ + pass + + @classmethod + def exec_module(cls, *args, **kwargs): # real signature unknown + """ Exec a built-in module """ + pass + @classmethod def find_module(cls, *args, **kwargs): # real signature unknown """ @@ -1456,7 +1582,11 @@ class __loader__(object): @classmethod def load_module(cls, *args, **kwargs): # real signature unknown - """ Load a built-in module. """ + """ + Load the specified module into sys.modules and return it. + + This method is deprecated. Use loader.exec_module instead. + """ pass def module_repr(module): # reliably restored by inspect diff --git a/python/testData/MockSdk3.4/python_stubs/builtins.py b/python/testData/MockSdk3.7/python_stubs/builtins.py similarity index 87% rename from python/testData/MockSdk3.4/python_stubs/builtins.py rename to python/testData/MockSdk3.7/python_stubs/builtins.py index 259e9a1fff1a..e4517191ca09 100644 --- a/python/testData/MockSdk3.4/python_stubs/builtins.py +++ b/python/testData/MockSdk3.7/python_stubs/builtins.py @@ -24,7 +24,7 @@ def abs(*args, **kwargs): # real signature unknown def all(*args, **kwargs): # real signature unknown """ Return True if bool(x) is True for all values x in the iterable. - + If the iterable is empty, return True. """ pass @@ -32,7 +32,7 @@ def all(*args, **kwargs): # real signature unknown def any(*args, **kwargs): # real signature unknown """ Return True if bool(x) is True for any x in the iterable. - + If the iterable is empty, return False. """ pass @@ -48,7 +48,7 @@ def ascii(*args, **kwargs): # real signature unknown """ pass -def bin(*args, **kwargs): # real signature unknown; NOTE: unreliably restored from __doc__ +def bin(*args, **kwargs): # real signature unknown; NOTE: unreliably restored from __doc__ """ Return the binary representation of an integer. @@ -57,10 +57,21 @@ def bin(*args, **kwargs): # real signature unknown; NOTE: unreliably restored fr """ pass +def breakpoint(*args, **kws): # real signature unknown; restored from __doc__ + """ + breakpoint(*args, **kws) + + Call sys.breakpointhook(*args, **kws). sys.breakpointhook() must accept + whatever arguments are passed. + + By default, this drops you into the pdb debugger. + """ + pass + def callable(i_e_, some_kind_of_function): # real signature unknown; restored from __doc__ """ Return whether the object is callable (i.e., some kind of function). - + Note that classes are callable, as are instances of classes with a __call__() method. """ @@ -133,7 +144,7 @@ def divmod(x, y): # known case of builtins.divmod def eval(*args, **kwargs): # real signature unknown """ Evaluate the given source in the context of globals and locals. - + The source may be a string representing a Python expression or a code object as returned by compile(). The globals must be a dictionary and locals can be any mapping, @@ -161,7 +172,9 @@ def format(*args, **kwargs): # real signature unknown """ Return value.__format__(format_spec) - format_spec defaults to the empty string + format_spec defaults to the empty string. + See the Format Specification Mini-Language section of help('FORMATTING') for + details. """ pass @@ -178,7 +191,7 @@ def getattr(object, name, default=None): # known special case of getattr def globals(*args, **kwargs): # real signature unknown """ Return the dictionary containing the current scope's global variables. - + NOTE: Updates to this dictionary *will* affect name lookups in the current global scope and vice-versa. """ @@ -187,7 +200,7 @@ def globals(*args, **kwargs): # real signature unknown def hasattr(*args, **kwargs): # real signature unknown """ Return whether the object has an attribute with the given name. - + This is done by calling getattr(obj, name) and catching AttributeError. """ pass @@ -213,7 +226,7 @@ def help(): # real signature unknown; restored from __doc__ """ pass -def hex(*args, **kwargs): # real signature unknown; NOTE: unreliably restored from __doc__ +def hex(*args, **kwargs): # real signature unknown; NOTE: unreliably restored from __doc__ """ Return the hexadecimal representation of an integer. @@ -234,10 +247,10 @@ def id(*args, **kwargs): # real signature unknown def input(*args, **kwargs): # real signature unknown """ Read a string from standard input. The trailing newline is stripped. - + The prompt string, if given, is printed to standard output without a trailing newline before reading input. - + If the user hits EOF (*nix: Ctrl-D, Windows: Ctrl-Z+Return), raise EOFError. On *nix systems, readline is used if available. """ @@ -246,7 +259,7 @@ def input(*args, **kwargs): # real signature unknown def isinstance(x, A_tuple): # real signature unknown; restored from __doc__ """ Return whether an object is an instance of a class or of a subclass thereof. - + A tuple, as in ``isinstance(x, (A, B, ...))``, may be given as the target to check against. This is equivalent to ``isinstance(x, A) or isinstance(x, B) or ...`` etc. @@ -328,7 +341,7 @@ def next(iterator, default=None): # real signature unknown; restored from __doc_ """ pass -def oct(*args, **kwargs): # real signature unknown; NOTE: unreliably restored from __doc__ +def oct(*args, **kwargs): # real signature unknown; NOTE: unreliably restored from __doc__ """ Return the octal representation of an integer. @@ -339,7 +352,7 @@ def oct(*args, **kwargs): # real signature unknown; NOTE: unreliably restored fr def open(file, mode='r', buffering=None, encoding=None, errors=None, newline=None, closefd=True): # known special case of open """ - Open file and return a stream. Raise IOError upon failure. + Open file and return a stream. Raise OSError upon failure. file is either a text or byte string giving the name (and the path if the file isn't in the current working directory) of the file to @@ -467,7 +480,7 @@ def ord(*args, **kwargs): # real signature unknown def pow(*args, **kwargs): # real signature unknown """ Equivalent to x**y (with two arguments) or x**y % z (with three arguments) - + Some types, such as ints, are able to use a more efficient algorithm when invoked using the three argument form. """ @@ -492,20 +505,19 @@ def quit(*args, **kwargs): # real signature unknown def repr(obj): # real signature unknown; restored from __doc__ """ Return the canonical string representation of the object. - + For many object types, including most builtins, eval(repr(obj)) == obj. """ pass -def round(number, ndigits=None): # real signature unknown; restored from __doc__ +def round(*args, **kwargs): # real signature unknown """ - round(number[, ndigits]) -> number + Round a number to a given precision in decimal digits. - Round a number to a given precision in decimal digits (default 0 digits). - This returns an int when called with one argument, otherwise the - same type as the number. ndigits may be negative. + The return value is an integer if ndigits is omitted or None. Otherwise + the return value has the same type as the number. ndigits may be negative. """ - return 0 + pass def setattr(x, y, v): # real signature unknown; restored from __doc__ """ @@ -518,8 +530,8 @@ def setattr(x, y, v): # real signature unknown; restored from __doc__ def sorted(*args, **kwargs): # real signature unknown """ Return a new list containing all items from the iterable in ascending order. - - A custom key function can be supplied to customise the sort order, and the + + A custom key function can be supplied to customize the sort order, and the reverse flag can be set to request the result in descending order. """ pass @@ -556,7 +568,7 @@ def __import__(name, globals=None, locals=None, fromlist=(), level=0): # real si __import__(name, globals=None, locals=None, fromlist=(), level=0) -> module Import a module. Because this function is meant for use by the Python - interpreter and not for general use it is better to use + interpreter and not for general use, it is better to use importlib.import_module() to programmatically import a module. The globals argument is only used to determine the context; @@ -565,8 +577,8 @@ def __import__(name, globals=None, locals=None, fromlist=(), level=0): # real si empty list to emulate ``import name''. When importing a module from a package, note that __import__('A.B', ...) returns package A when fromlist is empty, but its submodule B when - fromlist is not empty. Level is used to determine whether to perform - absolute or relative imports. 0 is absolute while a positive number + fromlist is not empty. The level argument is used to determine whether to + perform absolute or relative imports: 0 is absolute, while a positive number is the number of parent directories to search relative to the current module. """ pass @@ -723,19 +735,16 @@ class object: """ Implement delattr(self, name). """ pass - def __dir__(self): # real signature unknown; restored from __doc__ - """ - __dir__() -> list - default dir() implementation - """ - return [] + def __dir__(self, *args, **kwargs): # real signature unknown + """ Default dir() implementation. """ + pass def __eq__(self, *args, **kwargs): # real signature unknown """ Return self==value. """ pass def __format__(self, *args, **kwargs): # real signature unknown - """ default object formatter """ + """ Default object formatter. """ pass def __getattribute__(self, *args, **kwargs): # real signature unknown @@ -754,6 +763,15 @@ class object: """ Return hash(self). """ pass + def __init_subclass__(self, *args, **kwargs): # real signature unknown + """ + This method is called when a class is subclassed. + + The default implementation does nothing. It may be + overridden to extend subclasses. + """ + pass + def __init__(self): # known special case of object.__init__ """ Initialize self. See help(type(self)) for accurate signature. """ pass @@ -776,11 +794,11 @@ class object: pass def __reduce_ex__(self, *args, **kwargs): # real signature unknown - """ helper for pickle """ + """ Helper for pickle. """ pass def __reduce__(self, *args, **kwargs): # real signature unknown - """ helper for pickle """ + """ Helper for pickle. """ pass def __repr__(self, *args, **kwargs): # real signature unknown @@ -791,12 +809,9 @@ class object: """ Implement setattr(self, name, value). """ pass - def __sizeof__(self): # real signature unknown; restored from __doc__ - """ - __sizeof__() -> int - size of object in memory, in bytes - """ - return 0 + def __sizeof__(self, *args, **kwargs): # real signature unknown + """ Size of object in memory, in bytes. """ + pass def __str__(self, *args, **kwargs): # real signature unknown """ Return str(self). """ @@ -923,7 +938,7 @@ class AttributeError(Exception): pass -class OSError(Exception): +class WindowsError(Exception): """ Base class for I/O related errors. """ def __init__(self, *args, **kwargs): # real signature unknown pass @@ -954,12 +969,18 @@ class OSError(Exception): strerror = property(lambda self: object(), lambda self, v: None, lambda self: None) # default """exception strerror""" + winerror = property(lambda self: object(), lambda self, v: None, lambda self: None) # default + """Win32 exception code""" -IOError = OSError + +OSError = WindowsError -EnvironmentError = OSError +IOError = WindowsError + + +EnvironmentError = WindowsError class BlockingIOError(OSError): @@ -970,7 +991,7 @@ class BlockingIOError(OSError): class int(object): """ - int(x=0) -> integer + int([x]) -> integer int(x, base=10) -> integer Convert a number or string to an integer, or return 0 if no arguments @@ -987,59 +1008,57 @@ class int(object): """ def bit_length(self): # real signature unknown; restored from __doc__ """ - int.bit_length() -> int - Number of bits necessary to represent self in binary. + >>> bin(37) '0b100101' >>> (37).bit_length() 6 """ - return 0 + pass def conjugate(self, *args, **kwargs): # real signature unknown """ Returns self, the complex conjugate of any int. """ pass @classmethod # known case - def from_bytes(cls, bytes, byteorder, *args, **kwargs): # real signature unknown; NOTE: unreliably restored from __doc__ + def from_bytes(cls, *args, **kwargs): # real signature unknown """ - int.from_bytes(bytes, byteorder, *, signed=False) -> int - Return the integer represented by the given array of bytes. - The bytes argument must be a bytes-like object (e.g. bytes or bytearray). - - The byteorder argument determines the byte order used to represent the - integer. If byteorder is 'big', the most significant byte is at the - beginning of the byte array. If byteorder is 'little', the most - significant byte is at the end of the byte array. To request the native - byte order of the host system, use `sys.byteorder' as the byte order value. - - The signed keyword-only argument indicates whether two's complement is - used to represent the integer. + bytes + Holds the array of bytes to convert. The argument must either + support the buffer protocol or be an iterable object producing bytes. + Bytes and bytearray are examples of built-in objects that support the + buffer protocol. + byteorder + The byte order used to represent the integer. If byteorder is 'big', + the most significant byte is at the beginning of the byte array. If + byteorder is 'little', the most significant byte is at the end of the + byte array. To request the native byte order of the host system, use + `sys.byteorder' as the byte order value. + signed + Indicates whether two's complement is used to represent the integer. """ pass - def to_bytes(self, length, byteorder, *args, **kwargs): # real signature unknown; NOTE: unreliably restored from __doc__ + def to_bytes(self, *args, **kwargs): # real signature unknown """ - int.to_bytes(length, byteorder, *, signed=False) -> bytes - Return an array of bytes representing an integer. - The integer is represented using length bytes. An OverflowError is - raised if the integer is not representable with the given number of - bytes. - - The byteorder argument determines the byte order used to represent the - integer. If byteorder is 'big', the most significant byte is at the - beginning of the byte array. If byteorder is 'little', the most - significant byte is at the end of the byte array. To request the native - byte order of the host system, use `sys.byteorder' as the byte order value. - - The signed keyword-only argument determines whether two's complement is - used to represent the integer. If signed is False and a negative integer - is given, an OverflowError is raised. + length + Length of bytes object to use. An OverflowError is raised if the + integer is not representable with the given number of bytes. + byteorder + The byte order used to represent the integer. If byteorder is 'big', + the most significant byte is at the beginning of the byte array. If + byteorder is 'little', the most significant byte is at the end of the + byte array. To request the native byte order of the host system, use + `sys.byteorder' as the byte order value. + signed + Determines whether two's complement is used to represent the integer. + If signed is False and a negative integer is given, an OverflowError + is raised. """ pass @@ -1111,7 +1130,7 @@ class int(object): def __init__(self, x, base=10): # known special case of int.__init__ """ - int(x=0) -> integer + int([x]) -> integer int(x, base=10) -> integer Convert a number or string to an integer, or return 0 if no arguments @@ -1250,7 +1269,7 @@ class int(object): pass def __sizeof__(self, *args, **kwargs): # real signature unknown - """ Returns size in memory, in bytes """ + """ Returns size in memory, in bytes. """ pass def __str__(self, *args, **kwargs): # real signature unknown @@ -1476,10 +1495,10 @@ class bytearray(object): return 0 @classmethod # known case - def fromhex(cls, *args, **kwargs): # real signature unknown; NOTE: unreliably restored from __doc__ + def fromhex(cls, *args, **kwargs): # real signature unknown; NOTE: unreliably restored from __doc__ """ Create a bytearray object from a string of hexadecimal numbers. - + Spaces between two numbers are accepted. Example: bytearray.fromhex('B9 01EF') -> bytearray(b'\\xb9\\x01\\xef') """ @@ -1488,7 +1507,7 @@ class bytearray(object): def hex(self): # real signature unknown; restored from __doc__ """ B.hex() -> string - + Create a string of hexadecimal numbers from a bytearray object. Example: bytearray([0xb9, 0x01, 0xef]).hex() -> 'b901ef'. """ @@ -1498,14 +1517,18 @@ class bytearray(object): """ B.index(sub[, start[, end]]) -> int - Like B.find() but raise ValueError when the subsection is not found. + Return the lowest index in B where subsection sub is found, + such that sub is contained within B[start,end]. Optional + arguments start and end are interpreted as in slice notation. + + Raises ValueError when the subsection is not found. """ return 0 def insert(self, *args, **kwargs): # real signature unknown """ Insert a single item into the bytearray before the given index. - + index The index where the value is to be inserted. item @@ -1531,6 +1554,15 @@ class bytearray(object): """ return False + def isascii(self): # real signature unknown; restored from __doc__ + """ + B.isascii() -> bool + + Return True if B is empty or all characters in B are ASCII, + False otherwise. + """ + return False + def isdigit(self): # real signature unknown; restored from __doc__ """ B.isdigit() -> bool @@ -1583,7 +1615,7 @@ class bytearray(object): Concatenate any number of bytes/bytearray objects. The bytearray whose method is called is inserted in between each pair. - + The result is returned as a new bytearray object. """ pass @@ -1620,7 +1652,7 @@ class bytearray(object): The returned table will be one where each byte in frm is mapped to the byte at the same position in to. - + The bytes objects frm and to must be of the same length. """ pass @@ -1631,10 +1663,10 @@ class bytearray(object): This will search for the separator sep in the bytearray. If the separator is found, returns a 3-tuple containing the part before the separator, the - separator itself, and the part after it. - - If the separator is not found, returns a 3-tuple containing the original - bytearray object and two empty bytearray objects. + separator itself, and the part after it as new bytearray objects. + + If the separator is not found, returns a 3-tuple containing the copy of the + original bytearray object and two empty bytearray objects. """ pass @@ -1662,11 +1694,11 @@ class bytearray(object): def replace(self, *args, **kwargs): # real signature unknown """ Return a copy with all occurrences of substring old replaced by new. - + count Maximum number of occurrences to replace. -1 (the default value) means replace all occurrences. - + If the optional argument count is given, only the first count occurrences are replaced. """ @@ -1692,7 +1724,11 @@ class bytearray(object): """ B.rindex(sub[, start[, end]]) -> int - Like B.rfind() but raise ValueError when the subsection is not found. + Return the highest index in B where subsection sub is found, + such that sub is contained within B[start,end]. Optional + arguments start and end are interpreted as in slice notation. + + Raise ValueError when the subsection is not found. """ return 0 @@ -1707,14 +1743,15 @@ class bytearray(object): def rpartition(self, *args, **kwargs): # real signature unknown """ - Partition the bytes into three parts using the given separator. + Partition the bytearray into three parts using the given separator. - This will search for the separator sep in the bytearray, starting and the end. + This will search for the separator sep in the bytearray, starting at the end. If the separator is found, returns a 3-tuple containing the part before the - separator, the separator itself, and the part after it. - + separator, the separator itself, and the part after it as new bytearray + objects. + If the separator is not found, returns a 3-tuple containing two empty bytearray - objects and the original bytearray object. + objects and the copy of the original bytearray object. """ pass @@ -1729,7 +1766,7 @@ class bytearray(object): maxsplit Maximum number of splits to do. -1 (the default value) means no limit. - + Splitting is done starting at the end of the bytearray and working to the front. """ pass @@ -1802,15 +1839,14 @@ class bytearray(object): """ pass - def translate(self, table, deletechars=None): # real signature unknown; restored from __doc__ + def translate(self, *args, **kwargs): # real signature unknown """ - translate(table, [deletechars]) Return a copy with each character mapped by the given translation table. table Translation table, which must be a bytes object of length 256. - - All characters occurring in the optional argument deletechars are removed. + + All characters occurring in the optional argument delete are removed. The remaining characters are mapped through the given translation table. """ pass @@ -1919,7 +1955,7 @@ class bytearray(object): pass def __mul__(self, *args, **kwargs): # real signature unknown - """ Return self*value.n """ + """ Return self*value. """ pass @staticmethod # known case of __new__ @@ -1948,7 +1984,7 @@ class bytearray(object): pass def __rmul__(self, *args, **kwargs): # real signature unknown - """ Return self*value. """ + """ Return value*self. """ pass def __setitem__(self, *args, **kwargs): # real signature unknown @@ -2002,8 +2038,8 @@ class bytes(object): """ B.count(sub[, start[, end]]) -> int - Return the number of non-overlapping occurrences of substring sub in - string B[start:end]. Optional arguments start and end are interpreted + Return the number of non-overlapping occurrences of subsection sub in + bytes B[start:end]. Optional arguments start and end are interpreted as in slice notation. """ return 0 @@ -2047,8 +2083,8 @@ class bytes(object): """ B.find(sub[, start[, end]]) -> int - Return the lowest index in B where substring sub is found, - such that sub is contained within B[start:end]. Optional + Return the lowest index in B where subsection sub is found, + such that sub is contained within B[start,end]. Optional arguments start and end are interpreted as in slice notation. Return -1 on failure. @@ -2056,10 +2092,10 @@ class bytes(object): return 0 @classmethod # known case - def fromhex(cls, *args, **kwargs): # real signature unknown; NOTE: unreliably restored from __doc__ + def fromhex(cls, *args, **kwargs): # real signature unknown; NOTE: unreliably restored from __doc__ """ Create a bytes object from a string of hexadecimal numbers. - + Spaces between two numbers are accepted. Example: bytes.fromhex('B9 01EF') -> b'\\xb9\\x01\\xef'. """ @@ -2068,7 +2104,7 @@ class bytes(object): def hex(self): # real signature unknown; restored from __doc__ """ B.hex() -> string - + Create a string of hexadecimal numbers from a bytes object. Example: b'\xb9\x01\xef'.hex() -> 'b901ef'. """ @@ -2078,7 +2114,11 @@ class bytes(object): """ B.index(sub[, start[, end]]) -> int - Like B.find() but raise ValueError when the substring is not found. + Return the lowest index in B where subsection sub is found, + such that sub is contained within B[start,end]. Optional + arguments start and end are interpreted as in slice notation. + + Raises ValueError when the subsection is not found. """ return 0 @@ -2100,6 +2140,15 @@ class bytes(object): """ return False + def isascii(self): # real signature unknown; restored from __doc__ + """ + B.isascii() -> bool + + Return True if B is empty or all characters in B are ASCII, + False otherwise. + """ + return False + def isdigit(self): # real signature unknown; restored from __doc__ """ B.isdigit() -> bool @@ -2147,14 +2196,14 @@ class bytes(object): """ return False - def join(self, *args, **kwargs): # real signature unknown; NOTE: unreliably restored from __doc__ + def join(self, *args, **kwargs): # real signature unknown; NOTE: unreliably restored from __doc__ """ Concatenate any number of bytes objects. The bytes whose method is called is inserted in between each pair. - + The result is returned as a new bytes object. - + Example: b'.'.join([b'ab', b'pq', b'rs']) -> b'ab.pq.rs'. """ pass @@ -2179,7 +2228,7 @@ class bytes(object): def lstrip(self, *args, **kwargs): # real signature unknown """ Strip leading bytes contained in the argument. - + If the argument is omitted or None, strip leading ASCII whitespace. """ pass @@ -2191,7 +2240,7 @@ class bytes(object): The returned table will be one where each byte in frm is mapped to the byte at the same position in to. - + The bytes objects frm and to must be of the same length. """ pass @@ -2203,7 +2252,7 @@ class bytes(object): This will search for the separator sep in the bytes. If the separator is found, returns a 3-tuple containing the part before the separator, the separator itself, and the part after it. - + If the separator is not found, returns a 3-tuple containing the original bytes object and two empty bytes objects. """ @@ -2216,7 +2265,7 @@ class bytes(object): count Maximum number of occurrences to replace. -1 (the default value) means replace all occurrences. - + If the optional argument count is given, only the first count occurrences are replaced. """ @@ -2226,8 +2275,8 @@ class bytes(object): """ B.rfind(sub[, start[, end]]) -> int - Return the highest index in B where substring sub is found, - such that sub is contained within B[start:end]. Optional + Return the highest index in B where subsection sub is found, + such that sub is contained within B[start,end]. Optional arguments start and end are interpreted as in slice notation. Return -1 on failure. @@ -2238,7 +2287,11 @@ class bytes(object): """ B.rindex(sub[, start[, end]]) -> int - Like B.rfind() but raise ValueError when the substring is not found. + Return the highest index in B where subsection sub is found, + such that sub is contained within B[start,end]. Optional + arguments start and end are interpreted as in slice notation. + + Raise ValueError when the subsection is not found. """ return 0 @@ -2255,10 +2308,10 @@ class bytes(object): """ Partition the bytes into three parts using the given separator. - This will search for the separator sep in the bytes, starting and the end. If + This will search for the separator sep in the bytes, starting at the end. If the separator is found, returns a 3-tuple containing the part before the separator, the separator itself, and the part after it. - + If the separator is not found, returns a 3-tuple containing two empty bytes objects and the original bytes object. """ @@ -2275,7 +2328,7 @@ class bytes(object): maxsplit Maximum number of splits to do. -1 (the default value) means no limit. - + Splitting is done starting at the end of the bytes and working to the front. """ pass @@ -2283,7 +2336,7 @@ class bytes(object): def rstrip(self, *args, **kwargs): # real signature unknown """ Strip trailing bytes contained in the argument. - + If the argument is omitted or None, strip trailing ASCII whitespace. """ pass @@ -2325,7 +2378,7 @@ class bytes(object): def strip(self, *args, **kwargs): # real signature unknown """ Strip leading and trailing bytes contained in the argument. - + If the argument is omitted or None, strip leading and trailing ASCII whitespace. """ pass @@ -2348,15 +2401,14 @@ class bytes(object): """ pass - def translate(self, table, deletechars=None): # real signature unknown; restored from __doc__ + def translate(self, *args, **kwargs): # real signature unknown """ - translate(table, [deletechars]) Return a copy with each character mapped by the given translation table. table Translation table, which must be a bytes object of length 256. - - All characters occurring in the optional argument deletechars are removed. + + All characters occurring in the optional argument delete are removed. The remaining characters are mapped through the given translation table. """ pass @@ -2451,7 +2503,7 @@ class bytes(object): pass def __mul__(self, *args, **kwargs): # real signature unknown - """ Return self*value.n """ + """ Return self*value. """ pass @staticmethod # known case of __new__ @@ -2472,7 +2524,7 @@ class bytes(object): pass def __rmul__(self, *args, **kwargs): # real signature unknown - """ Return self*value. """ + """ Return value*self. """ pass def __str__(self, *args, **kwargs): # real signature unknown @@ -2522,8 +2574,9 @@ class classmethod(object): To declare a class method, use this idiom: class C: - def f(cls, arg1, arg2, ...): ... - f = classmethod(f) + @classmethod + def f(cls, arg1, arg2, ...): + ... It can be called either on the class (e.g. C.f()) or on an instance (e.g. C().f()). The instance is ignored except for its class. @@ -2555,9 +2608,8 @@ class classmethod(object): class complex(object): """ - complex(real[, imag]) -> complex number - Create a complex number from a real part and an optional imaginary part. + This is equivalent to (real + imag*1j) where imag defaults to 0. """ def conjugate(self): # real signature unknown; restored from __doc__ @@ -2623,7 +2675,7 @@ class complex(object): """ Return hash(self). """ pass - def __init__(self, real, imag=None): # real signature unknown; restored from __doc__ + def __init__(self, *args, **kwargs): # real signature unknown pass def __int__(self, *args, **kwargs): # real signature unknown @@ -2780,11 +2832,11 @@ class dict(object): @staticmethod # known case def fromkeys(*args, **kwargs): # real signature unknown - """ Returns a new dict with keys from iterable and values equal to value. """ + """ Create a new dictionary with keys from iterable and values set to value. """ pass - def get(self, k, d=None): # real signature unknown; restored from __doc__ - """ D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None. """ + def get(self, *args, **kwargs): # real signature unknown + """ Return the value for key if key is in the dictionary, else default. """ pass def items(self): # real signature unknown; restored from __doc__ @@ -2809,8 +2861,12 @@ class dict(object): """ pass - def setdefault(self, k, d=None): # real signature unknown; restored from __doc__ - """ D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D """ + def setdefault(self, *args, **kwargs): # real signature unknown + """ + Insert key with a value of default if key is not in the dictionary. + + Return the value for key if key is in the dictionary, else default. + """ pass def update(self, E=None, **F): # known special case of dict.update @@ -2827,7 +2883,7 @@ class dict(object): pass def __contains__(self, *args, **kwargs): # real signature unknown - """ True if D has a key k, else False. """ + """ True if the dictionary has the specified key, else False. """ pass def __delitem__(self, *args, **kwargs): # real signature unknown @@ -2911,11 +2967,14 @@ class dict(object): class enumerate(object): """ - enumerate(iterable[, start]) -> iterator for index, value of iterable + Return an enumerate object. + + iterable + an object supporting iteration + + The enumerate object yields pairs containing a count (from start, which + defaults to zero) and a value yielded by the iterable argument. - Return an enumerate object. iterable must be another object that supports - iteration. The enumerate object yields pairs containing a count (from - start, which defaults to zero) and a value yielded by the iterable argument. enumerate is useful for obtaining an indexed list: (0, seq[0]), (1, seq[1]), (2, seq[2]), ... """ @@ -3001,17 +3060,14 @@ class filter(object): class float(object): - """ - float(x) -> floating point number - - Convert a string or number to a floating point number, if possible. - """ + """ Convert a string or number to a floating point number, if possible. """ def as_integer_ratio(self): # real signature unknown; restored from __doc__ """ - float.as_integer_ratio() -> (int, int) + Return integer ratio. + + Return a pair of integers, whose ratio is exactly equal to the original float + and with a positive denominator. - Return a pair of integers, whose ratio is exactly equal to the original - float and with a positive denominator. Raise OverflowError on infinities and a ValueError on NaNs. >>> (10.0).as_integer_ratio() @@ -3028,29 +3084,27 @@ class float(object): pass @staticmethod # known case - def fromhex(string): # real signature unknown; restored from __doc__ + def fromhex(*args, **kwargs): # real signature unknown; NOTE: unreliably restored from __doc__ """ - float.fromhex(string) -> float - Create a floating-point number from a hexadecimal string. + >>> float.fromhex('0x1.ffffp10') 2047.984375 >>> float.fromhex('-0x1p-1074') -5e-324 """ - return 0.0 + pass def hex(self): # real signature unknown; restored from __doc__ """ - float.hex() -> string - Return a hexadecimal representation of a floating-point number. + >>> (-0.1).hex() '-0x1.999999999999ap-4' >>> 3.14159.hex() '0x1.921f9f01b866ep+1' """ - return "" + pass def is_integer(self, *args, **kwargs): # real signature unknown """ Return True if the float is an integer. """ @@ -3084,30 +3138,28 @@ class float(object): """ Return self//value. """ pass - def __format__(self, format_spec): # real signature unknown; restored from __doc__ - """ - float.__format__(format_spec) -> string - - Formats the float according to format_spec. - """ - return "" + def __format__(self, *args, **kwargs): # real signature unknown + """ Formats the float according to format_spec. """ + pass def __getattribute__(self, *args, **kwargs): # real signature unknown """ Return getattr(self, name). """ pass - def __getformat__(self, typestr): # real signature unknown; restored from __doc__ + def __getformat__(self, *args, **kwargs): # real signature unknown """ - float.__getformat__(typestr) -> string + You probably don't want to use this function. - You probably don't want to use this function. It exists mainly to be - used in Python's test suite. + typestr + Must be 'double' or 'float'. - typestr must be 'double' or 'float'. This function returns whichever of - 'unknown', 'IEEE, big-endian' or 'IEEE, little-endian' best describes the - format of floating point numbers used by the C type named by typestr. + It exists mainly to be used in Python's test suite. + + This function returns whichever of 'unknown', 'IEEE, big-endian' or 'IEEE, + little-endian' best describes the format of floating point numbers used by the + C type named by typestr. """ - return "" + pass def __getnewargs__(self, *args, **kwargs): # real signature unknown pass @@ -3124,7 +3176,7 @@ class float(object): """ Return hash(self). """ pass - def __init__(self, x): # real signature unknown; restored from __doc__ + def __init__(self, *args, **kwargs): # real signature unknown pass def __int__(self, *args, **kwargs): # real signature unknown @@ -3195,6 +3247,7 @@ class float(object): def __round__(self, *args, **kwargs): # real signature unknown """ Return the Integral closest to x, rounding half toward even. + When an argument is passed, work like built-in round(x, ndigits). """ pass @@ -3211,16 +3264,18 @@ class float(object): """ Return value/self. """ pass - def __setformat__(self, typestr, fmt): # real signature unknown; restored from __doc__ + def __set_format__(self, *args, **kwargs): # real signature unknown """ - float.__setformat__(typestr, fmt) -> None + You probably don't want to use this function. - You probably don't want to use this function. It exists mainly to be - used in Python's test suite. + typestr + Must be 'double' or 'float'. + fmt + Must be one of 'unknown', 'IEEE, big-endian' or 'IEEE, little-endian', + and in addition can only be one of the latter two if it appears to + match the underlying C reality. - typestr must be 'double' or 'float'. fmt must be one of 'unknown', - 'IEEE, big-endian' or 'IEEE, little-endian', and in addition can only be - one of the latter two if it appears to match the underlying C reality. + It exists mainly to be used in Python's test suite. Override the automatic determination of C-level floating point type. This affects how floats are converted to and from binary strings. @@ -3445,6 +3500,9 @@ class ImportError(Exception): def __init__(self, *args, **kwargs): # real signature unknown pass + def __reduce__(self, *args, **kwargs): # real signature unknown + pass + def __str__(self, *args, **kwargs): # real signature unknown """ Return str(self). """ pass @@ -3563,60 +3621,65 @@ class KeyError(LookupError): class list(object): """ - list() -> new empty list - list(iterable) -> new list initialized from iterable's items + Built-in mutable sequence. + + If no argument is given, the constructor creates a new empty list. + The argument must be an iterable if specified. """ - def append(self, p_object): # real signature unknown; restored from __doc__ - """ L.append(object) -> None -- append object to end """ + def append(self, *args, **kwargs): # real signature unknown + """ Append object to the end of the list. """ pass - def clear(self): # real signature unknown; restored from __doc__ - """ L.clear() -> None -- remove all items from L """ + def clear(self, *args, **kwargs): # real signature unknown + """ Remove all items from list. """ pass - def copy(self): # real signature unknown; restored from __doc__ - """ L.copy() -> list -- a shallow copy of L """ - return [] - - def count(self, value): # real signature unknown; restored from __doc__ - """ L.count(value) -> integer -- return number of occurrences of value """ - return 0 - - def extend(self, iterable): # real signature unknown; restored from __doc__ - """ L.extend(iterable) -> None -- extend list by appending elements from the iterable """ + def copy(self, *args, **kwargs): # real signature unknown + """ Return a shallow copy of the list. """ pass - def index(self, value, start=None, stop=None): # real signature unknown; restored from __doc__ + def count(self, *args, **kwargs): # real signature unknown + """ Return number of occurrences of value. """ + pass + + def extend(self, *args, **kwargs): # real signature unknown + """ Extend list by appending elements from the iterable. """ + pass + + def index(self, *args, **kwargs): # real signature unknown """ - L.index(value, [start, [stop]]) -> integer -- return first index of value. + Return first index of value. + Raises ValueError if the value is not present. """ - return 0 - - def insert(self, index, p_object): # real signature unknown; restored from __doc__ - """ L.insert(index, object) -- insert object before index """ pass - def pop(self, index=None): # real signature unknown; restored from __doc__ + def insert(self, *args, **kwargs): # real signature unknown + """ Insert object before index. """ + pass + + def pop(self, *args, **kwargs): # real signature unknown """ - L.pop([index]) -> item -- remove and return item at index (default last). + Remove and return item at index (default last). + Raises IndexError if list is empty or index is out of range. """ pass - def remove(self, value): # real signature unknown; restored from __doc__ + def remove(self, *args, **kwargs): # real signature unknown """ - L.remove(value) -> None -- remove first occurrence of value. + Remove first occurrence of value. + Raises ValueError if the value is not present. """ pass - def reverse(self): # real signature unknown; restored from __doc__ - """ L.reverse() -- reverse *IN PLACE* """ + def reverse(self, *args, **kwargs): # real signature unknown + """ Reverse *IN PLACE*. """ pass - def sort(self, key=None, reverse=False): # real signature unknown; restored from __doc__ - """ L.sort(key=None, reverse=False) -> None -- stable sort *IN PLACE* """ + def sort(self, *args, **kwargs): # real signature unknown + """ Stable sort *IN PLACE*. """ pass def __add__(self, *args, **kwargs): # real signature unknown @@ -3661,8 +3724,10 @@ class list(object): def __init__(self, seq=()): # known special case of list.__init__ """ - list() -> new empty list - list(iterable) -> new list initialized from iterable's items + Built-in mutable sequence. + + If no argument is given, the constructor creates a new empty list. + The argument must be an iterable if specified. # (copied from class doc) """ pass @@ -3684,7 +3749,7 @@ class list(object): pass def __mul__(self, *args, **kwargs): # real signature unknown - """ Return self*value.n """ + """ Return self*value. """ pass @staticmethod # known case of __new__ @@ -3700,20 +3765,20 @@ class list(object): """ Return repr(self). """ pass - def __reversed__(self): # real signature unknown; restored from __doc__ - """ L.__reversed__() -- return a reverse iterator over the list """ + def __reversed__(self, *args, **kwargs): # real signature unknown + """ Return a reverse iterator over the list. """ pass def __rmul__(self, *args, **kwargs): # real signature unknown - """ Return self*value. """ + """ Return value*self. """ pass def __setitem__(self, *args, **kwargs): # real signature unknown """ Set self[key] to value. """ pass - def __sizeof__(self): # real signature unknown; restored from __doc__ - """ L.__sizeof__() -- size of L in memory, in bytes """ + def __sizeof__(self, *args, **kwargs): # real signature unknown + """ Return the size of the list in memory, in bytes. """ pass __hash__ = None @@ -3893,6 +3958,12 @@ class memoryview(object): +class ModuleNotFoundError(ImportError): + """ Module not found. """ + def __init__(self, *args, **kwargs): # real signature unknown + pass + + class NameError(Exception): """ Name not found globally. """ def __init__(self, *args, **kwargs): # real signature unknown @@ -3971,11 +4042,18 @@ class ProcessLookupError(OSError): class property(object): """ - property(fget=None, fset=None, fdel=None, doc=None) -> property attribute + Property attribute. - fget is a function to be used for getting an attribute value, and likewise - fset is a function for setting, and fdel a function for del'ing, an - attribute. Typical use is to define a managed attribute x: + fget + function to be used for getting an attribute value + fset + function to be used for setting an attribute value + fdel + function to be used for del'ing an attribute + doc + docstring + + Typical use is to define a managed attribute x: class C(object): def getx(self): return self._x @@ -4023,11 +4101,18 @@ class property(object): def __init__(self, fget=None, fset=None, fdel=None, doc=None): # known special case of property.__init__ """ - property(fget=None, fset=None, fdel=None, doc=None) -> property attribute + Property attribute. - fget is a function to be used for getting an attribute value, and likewise - fset is a function for setting, and fdel a function for del'ing, an - attribute. Typical use is to define a managed attribute x: + fget + function to be used for getting an attribute value + fset + function to be used for setting an attribute value + fdel + function to be used for del'ing an attribute + doc + docstring + + Typical use is to define a managed attribute x: class C(object): def getx(self): return self._x @@ -4048,7 +4133,6 @@ class property(object): @x.deleter def x(self): del self._x - # (copied from class doc) """ pass @@ -4094,6 +4178,10 @@ class range(object): """ return 0 + def __bool__(self, *args, **kwargs): # real signature unknown + """ self != 0 """ + pass + def __contains__(self, *args, **kwargs): # real signature unknown """ Return key in self. """ pass @@ -4203,16 +4291,12 @@ class ResourceWarning(Warning): class reversed(object): - """ - reversed(sequence) -> reverse iterator over values of the sequence - - Return a reverse iterator - """ + """ Return a reverse iterator over the values of the given sequence. """ def __getattribute__(self, *args, **kwargs): # real signature unknown """ Return getattr(self, name). """ pass - def __init__(self, sequence): # real signature unknown; restored from __doc__ + def __init__(self, *args, **kwargs): # real signature unknown pass def __iter__(self, *args, **kwargs): # real signature unknown @@ -4561,8 +4645,9 @@ class staticmethod(object): To declare a static method, use this idiom: class C: - def f(arg1, arg2, ...): ... - f = staticmethod(f) + @staticmethod + def f(arg1, arg2, ...): + ... It can be called either on the class (e.g. C.f()) or on an instance (e.g. C().f()). The instance is ignored except for its class. @@ -4624,31 +4709,26 @@ class str(object): encoding defaults to sys.getdefaultencoding(). errors defaults to 'strict'. """ - def capitalize(self): # real signature unknown; restored from __doc__ + def capitalize(self, *args, **kwargs): # real signature unknown """ - S.capitalize() -> str + Return a capitalized version of the string. - Return a capitalized version of S, i.e. make the first character - have upper case and the rest lower case. + More specifically, make the first character have upper case and the rest lower + case. """ - return "" + pass - def casefold(self): # real signature unknown; restored from __doc__ - """ - S.casefold() -> str - - Return a version of S suitable for caseless comparisons. - """ - return "" + def casefold(self, *args, **kwargs): # real signature unknown + """ Return a version of the string suitable for caseless comparisons. """ + pass - def center(self, width, fillchar=None): # real signature unknown; restored from __doc__ + def center(self, *args, **kwargs): # real signature unknown """ - S.center(width[, fillchar]) -> str + Return a centered string of length width. - Return S centered in a string of length width. Padding is - done using the specified fill character (default is a space) + Padding is done using the specified fill character (default is a space). """ - return "" + pass def count(self, sub, start=None, end=None): # real signature unknown; restored from __doc__ """ @@ -4660,18 +4740,20 @@ class str(object): """ return 0 - def encode(self, encoding='utf-8', errors='strict'): # real signature unknown; restored from __doc__ + def encode(self, *args, **kwargs): # real signature unknown """ - S.encode(encoding='utf-8', errors='strict') -> bytes + Encode the string using the codec registered for encoding. - Encode S using the codec registered for encoding. Default encoding - is 'utf-8'. errors may be given to set a different error - handling scheme. Default is 'strict' meaning that encoding errors raise - a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and - 'xmlcharrefreplace' as well as any other name registered with - codecs.register_error that can handle UnicodeEncodeErrors. + encoding + The encoding in which to encode the string. + errors + The error handling scheme to use for encoding errors. + The default is 'strict' meaning that encoding errors raise a + UnicodeEncodeError. Other possible values are 'ignore', 'replace' and + 'xmlcharrefreplace' as well as any other name registered with + codecs.register_error that can handle UnicodeEncodeErrors. """ - return b"" + pass def endswith(self, suffix, start=None, end=None): # real signature unknown; restored from __doc__ """ @@ -4684,14 +4766,13 @@ class str(object): """ return False - def expandtabs(self, tabsize=8): # real signature unknown; restored from __doc__ + def expandtabs(self, *args, **kwargs): # real signature unknown """ - S.expandtabs(tabsize=8) -> str + Return a copy where all tab characters are expanded using spaces. - Return a copy of S where all tab characters are expanded using spaces. If tabsize is not given, a tab size of 8 characters is assumed. """ - return "" + pass def find(self, sub, start=None, end=None): # real signature unknown; restored from __doc__ """ @@ -4727,148 +4808,152 @@ class str(object): """ S.index(sub[, start[, end]]) -> int - Like S.find() but raise ValueError when the substring is not found. + Return the lowest index in S where substring sub is found, + such that sub is contained within S[start:end]. Optional + arguments start and end are interpreted as in slice notation. + + Raises ValueError when the substring is not found. """ return 0 - def isalnum(self): # real signature unknown; restored from __doc__ + def isalnum(self, *args, **kwargs): # real signature unknown """ - S.isalnum() -> bool + Return True if the string is an alpha-numeric string, False otherwise. - Return True if all characters in S are alphanumeric - and there is at least one character in S, False otherwise. + A string is alpha-numeric if all characters in the string are alpha-numeric and + there is at least one character in the string. """ - return False + pass - def isalpha(self): # real signature unknown; restored from __doc__ + def isalpha(self, *args, **kwargs): # real signature unknown """ - S.isalpha() -> bool + Return True if the string is an alphabetic string, False otherwise. - Return True if all characters in S are alphabetic - and there is at least one character in S, False otherwise. + A string is alphabetic if all characters in the string are alphabetic and there + is at least one character in the string. """ - return False + pass - def isdecimal(self): # real signature unknown; restored from __doc__ + def isascii(self, *args, **kwargs): # real signature unknown """ - S.isdecimal() -> bool + Return True if all characters in the string are ASCII, False otherwise. - Return True if there are only decimal characters in S, - False otherwise. + ASCII characters have code points in the range U+0000-U+007F. + Empty string is ASCII too. """ - return False + pass - def isdigit(self): # real signature unknown; restored from __doc__ + def isdecimal(self, *args, **kwargs): # real signature unknown """ - S.isdigit() -> bool + Return True if the string is a decimal string, False otherwise. - Return True if all characters in S are digits - and there is at least one character in S, False otherwise. + A string is a decimal string if all characters in the string are decimal and + there is at least one character in the string. """ - return False + pass - def isidentifier(self): # real signature unknown; restored from __doc__ + def isdigit(self, *args, **kwargs): # real signature unknown """ - S.isidentifier() -> bool + Return True if the string is a digit string, False otherwise. - Return True if S is a valid identifier according - to the language definition. - - Use keyword.iskeyword() to test for reserved identifiers - such as "def" and "class". + A string is a digit string if all characters in the string are digits and there + is at least one character in the string. """ - return False + pass - def islower(self): # real signature unknown; restored from __doc__ + def isidentifier(self, *args, **kwargs): # real signature unknown """ - S.islower() -> bool + Return True if the string is a valid Python identifier, False otherwise. - Return True if all cased characters in S are lowercase and there is - at least one cased character in S, False otherwise. + Use keyword.iskeyword() to test for reserved identifiers such as "def" and + "class". """ - return False + pass - def isnumeric(self): # real signature unknown; restored from __doc__ + def islower(self, *args, **kwargs): # real signature unknown """ - S.isnumeric() -> bool + Return True if the string is a lowercase string, False otherwise. - Return True if there are only numeric characters in S, - False otherwise. + A string is lowercase if all cased characters in the string are lowercase and + there is at least one cased character in the string. """ - return False + pass - def isprintable(self): # real signature unknown; restored from __doc__ + def isnumeric(self, *args, **kwargs): # real signature unknown """ - S.isprintable() -> bool + Return True if the string is a numeric string, False otherwise. - Return True if all characters in S are considered - printable in repr() or S is empty, False otherwise. + A string is numeric if all characters in the string are numeric and there is at + least one character in the string. """ - return False + pass - def isspace(self): # real signature unknown; restored from __doc__ + def isprintable(self, *args, **kwargs): # real signature unknown """ - S.isspace() -> bool + Return True if the string is printable, False otherwise. - Return True if all characters in S are whitespace - and there is at least one character in S, False otherwise. + A string is printable if all of its characters are considered printable in + repr() or if it is empty. """ - return False + pass - def istitle(self): # real signature unknown; restored from __doc__ + def isspace(self, *args, **kwargs): # real signature unknown """ - S.istitle() -> bool + Return True if the string is a whitespace string, False otherwise. - Return True if S is a titlecased string and there is at least one - character in S, i.e. upper- and titlecase characters may only + A string is whitespace if all characters in the string are whitespace and there + is at least one character in the string. + """ + pass + + def istitle(self, *args, **kwargs): # real signature unknown + """ + Return True if the string is a title-cased string, False otherwise. + + In a title-cased string, upper- and title-case characters may only follow uncased characters and lowercase characters only cased ones. - Return False otherwise. """ - return False + pass - def isupper(self): # real signature unknown; restored from __doc__ + def isupper(self, *args, **kwargs): # real signature unknown """ - S.isupper() -> bool + Return True if the string is an uppercase string, False otherwise. - Return True if all cased characters in S are uppercase and there is - at least one cased character in S, False otherwise. + A string is uppercase if all cased characters in the string are uppercase and + there is at least one cased character in the string. """ - return False + pass - def join(self, iterable): # real signature unknown; restored from __doc__ + def join(self, ab=None, pq=None, rs=None): # real signature unknown; restored from __doc__ """ - S.join(iterable) -> str + Concatenate any number of strings. - Return a string which is the concatenation of the strings in the - iterable. The separator between elements is S. + The string whose method is called is inserted in between each given string. + The result is returned as a new string. + + Example: '.'.join(['ab', 'pq', 'rs']) -> 'ab.pq.rs' """ - return "" + pass - def ljust(self, width, fillchar=None): # real signature unknown; restored from __doc__ + def ljust(self, *args, **kwargs): # real signature unknown """ - S.ljust(width[, fillchar]) -> str + Return a left-justified string of length width. - Return S left-justified in a Unicode string of length width. Padding is - done using the specified fill character (default is a space). + Padding is done using the specified fill character (default is a space). """ - return "" + pass - def lower(self): # real signature unknown; restored from __doc__ - """ - S.lower() -> str - - Return a copy of the string S converted to lowercase. - """ - return "" + def lower(self, *args, **kwargs): # real signature unknown + """ Return a copy of the string converted to lowercase. """ + pass - def lstrip(self, chars=None): # real signature unknown; restored from __doc__ + def lstrip(self, *args, **kwargs): # real signature unknown """ - S.lstrip([chars]) -> str + Return a copy of the string with leading whitespace removed. - Return a copy of the string S with leading whitespace removed. If chars is given and not None, remove characters in chars instead. """ - return "" + pass def maketrans(self, *args, **kwargs): # real signature unknown """ @@ -4884,25 +4969,31 @@ class str(object): """ pass - def partition(self, sep): # real signature unknown; restored from __doc__ + def partition(self, *args, **kwargs): # real signature unknown """ - S.partition(sep) -> (head, sep, tail) + Partition the string into three parts using the given separator. - Search for the separator sep in S, and return the part before it, - the separator itself, and the part after it. If the separator is not - found, return S and two empty strings. + This will search for the separator in the string. If the separator is found, + returns a 3-tuple containing the part before the separator, the separator + itself, and the part after it. + + If the separator is not found, returns a 3-tuple containing the original string + and two empty strings. """ pass - def replace(self, old, new, count=None): # real signature unknown; restored from __doc__ + def replace(self, *args, **kwargs): # real signature unknown """ - S.replace(old, new[, count]) -> str + Return a copy with all occurrences of substring old replaced by new. - Return a copy of S with all occurrences of substring - old replaced by new. If the optional argument count is - given, only the first count occurrences are replaced. + count + Maximum number of occurrences to replace. + -1 (the default value) means replace all occurrences. + + If the optional argument count is given, only the first count occurrences are + replaced. """ - return "" + pass def rfind(self, sub, start=None, end=None): # real signature unknown; restored from __doc__ """ @@ -4920,71 +5011,81 @@ class str(object): """ S.rindex(sub[, start[, end]]) -> int - Like S.rfind() but raise ValueError when the substring is not found. + Return the highest index in S where substring sub is found, + such that sub is contained within S[start:end]. Optional + arguments start and end are interpreted as in slice notation. + + Raises ValueError when the substring is not found. """ return 0 - def rjust(self, width, fillchar=None): # real signature unknown; restored from __doc__ + def rjust(self, *args, **kwargs): # real signature unknown """ - S.rjust(width[, fillchar]) -> str + Return a right-justified string of length width. - Return S right-justified in a string of length width. Padding is - done using the specified fill character (default is a space). - """ - return "" - - def rpartition(self, sep): # real signature unknown; restored from __doc__ - """ - S.rpartition(sep) -> (head, sep, tail) - - Search for the separator sep in S, starting at the end of S, and return - the part before it, the separator itself, and the part after it. If the - separator is not found, return two empty strings and S. + Padding is done using the specified fill character (default is a space). """ pass - def rsplit(self, sep=None, maxsplit=-1): # real signature unknown; restored from __doc__ + def rpartition(self, *args, **kwargs): # real signature unknown """ - S.rsplit(sep=None, maxsplit=-1) -> list of strings + Partition the string into three parts using the given separator. - Return a list of the words in S, using sep as the - delimiter string, starting at the end of the string and - working to the front. If maxsplit is given, at most maxsplit - splits are done. If sep is not specified, any whitespace string - is a separator. + This will search for the separator in the string, starting at the end. If + the separator is found, returns a 3-tuple containing the part before the + separator, the separator itself, and the part after it. + + If the separator is not found, returns a 3-tuple containing two empty strings + and the original string. """ - return [] + pass - def rstrip(self, chars=None): # real signature unknown; restored from __doc__ + def rsplit(self, *args, **kwargs): # real signature unknown """ - S.rstrip([chars]) -> str + Return a list of the words in the string, using sep as the delimiter string. + + sep + The delimiter according which to split the string. + None (the default value) means split according to any whitespace, + and discard empty strings from the result. + maxsplit + Maximum number of splits to do. + -1 (the default value) means no limit. + + Splits are done starting at the end of the string and working to the front. + """ + pass + + def rstrip(self, *args, **kwargs): # real signature unknown + """ + Return a copy of the string with trailing whitespace removed. - Return a copy of the string S with trailing whitespace removed. If chars is given and not None, remove characters in chars instead. """ - return "" + pass - def split(self, sep=None, maxsplit=-1): # real signature unknown; restored from __doc__ + def split(self, *args, **kwargs): # real signature unknown """ - S.split(sep=None, maxsplit=-1) -> list of strings + Return a list of the words in the string, using sep as the delimiter string. - Return a list of the words in S, using sep as the - delimiter string. If maxsplit is given, at most maxsplit - splits are done. If sep is not specified or is None, any - whitespace string is a separator and empty strings are - removed from the result. + sep + The delimiter according which to split the string. + None (the default value) means split according to any whitespace, + and discard empty strings from the result. + maxsplit + Maximum number of splits to do. + -1 (the default value) means no limit. """ - return [] + pass - def splitlines(self, keepends=None): # real signature unknown; restored from __doc__ + def splitlines(self, *args, **kwargs): # real signature unknown """ - S.splitlines([keepends]) -> list of strings + Return a list of the lines in the string, breaking at line boundaries. - Return a list of the lines in S, breaking at line boundaries. - Line breaks are not included in the resulting list unless keepends - is given and true. + Line breaks are not included in the resulting list unless keepends is given and + true. """ - return [] + pass def startswith(self, prefix, start=None, end=None): # real signature unknown; restored from __doc__ """ @@ -4997,63 +5098,52 @@ class str(object): """ return False - def strip(self, chars=None): # real signature unknown; restored from __doc__ + def strip(self, *args, **kwargs): # real signature unknown """ - S.strip([chars]) -> str + Return a copy of the string with leading and trailing whitespace remove. - Return a copy of the string S with leading and trailing - whitespace removed. If chars is given and not None, remove characters in chars instead. """ - return "" + pass - def swapcase(self): # real signature unknown; restored from __doc__ - """ - S.swapcase() -> str - - Return a copy of S with uppercase characters converted to lowercase - and vice versa. - """ - return "" + def swapcase(self, *args, **kwargs): # real signature unknown + """ Convert uppercase characters to lowercase and lowercase characters to uppercase. """ + pass - def title(self): # real signature unknown; restored from __doc__ + def title(self, *args, **kwargs): # real signature unknown """ - S.title() -> str + Return a version of the string where each word is titlecased. - Return a titlecased version of S, i.e. words start with title case - characters, all remaining cased characters have lower case. + More specifically, words start with uppercased characters and all remaining + cased characters have lower case. """ - return "" + pass - def translate(self, table): # real signature unknown; restored from __doc__ + def translate(self, *args, **kwargs): # real signature unknown """ - S.translate(table) -> str + Replace each character in the string using the given translation table. - Return a copy of the string S in which each character has been mapped - through the given translation table. The table must implement - lookup/indexing via __getitem__, for instance a dictionary or list, - mapping Unicode ordinals to Unicode ordinals, strings, or None. If - this operation raises LookupError, the character is left untouched. - Characters mapped to None are deleted. + table + Translation table, which must be a mapping of Unicode ordinals to + Unicode ordinals, strings, or None. + + The table must implement lookup/indexing via __getitem__, for instance a + dictionary or list. If this operation raises LookupError, the character is + left untouched. Characters mapped to None are deleted. """ - return "" + pass - def upper(self): # real signature unknown; restored from __doc__ - """ - S.upper() -> str - - Return a copy of S converted to uppercase. - """ - return "" + def upper(self, *args, **kwargs): # real signature unknown + """ Return a copy of the string converted to uppercase. """ + pass - def zfill(self, width): # real signature unknown; restored from __doc__ + def zfill(self, *args, **kwargs): # real signature unknown """ - S.zfill(width) -> str + Pad a numeric string with zeros on the left, to fill a field of the given width. - Pad a numeric string S with zeros on the left, to fill a field - of the specified width. The string S is never truncated. + The string is never truncated. """ - return "" + pass def __add__(self, *args, **kwargs): # real signature unknown """ Return self+value. """ @@ -5067,13 +5157,9 @@ class str(object): """ Return self==value. """ pass - def __format__(self, format_spec): # real signature unknown; restored from __doc__ - """ - S.__format__(format_spec) -> str - - Return a formatted version of S as described by format_spec. - """ - return "" + def __format__(self, *args, **kwargs): # real signature unknown + """ Return a formatted version of the string as described by format_spec. """ + pass def __getattribute__(self, *args, **kwargs): # real signature unknown """ Return getattr(self, name). """ @@ -5135,7 +5221,7 @@ class str(object): pass def __mul__(self, *args, **kwargs): # real signature unknown - """ Return self*value.n """ + """ Return self*value. """ pass @staticmethod # known case of __new__ @@ -5156,11 +5242,11 @@ class str(object): pass def __rmul__(self, *args, **kwargs): # real signature unknown - """ Return self*value. """ + """ Return value*self. """ pass - def __sizeof__(self): # real signature unknown; restored from __doc__ - """ S.__sizeof__() -> size of S in memory, in bytes """ + def __sizeof__(self, *args, **kwargs): # real signature unknown + """ Return the size of the string in memory, in bytes. """ pass def __str__(self, *args, **kwargs): # real signature unknown @@ -5292,21 +5378,24 @@ class TimeoutError(OSError): class tuple(object): """ - tuple() -> empty tuple - tuple(iterable) -> tuple initialized from iterable's items + Built-in immutable sequence. + + If no argument is given, the constructor returns an empty tuple. + If iterable is specified the tuple is initialized from iterable's items. If the argument is a tuple, the return value is the same object. """ - def count(self, value): # real signature unknown; restored from __doc__ - """ T.count(value) -> integer -- return number of occurrences of value """ - return 0 + def count(self, *args, **kwargs): # real signature unknown + """ Return number of occurrences of value. """ + pass - def index(self, value, start=None, stop=None): # real signature unknown; restored from __doc__ + def index(self, *args, **kwargs): # real signature unknown """ - T.index(value, [start, [stop]]) -> integer -- return first index of value. + Return first index of value. + Raises ValueError if the value is not present. """ - return 0 + pass def __add__(self, *args, **kwargs): # real signature unknown """ Return self+value. """ @@ -5345,8 +5434,10 @@ class tuple(object): def __init__(self, seq=()): # known special case of tuple.__init__ """ - tuple() -> empty tuple - tuple(iterable) -> tuple initialized from iterable's items + Built-in immutable sequence. + + If no argument is given, the constructor returns an empty tuple. + If iterable is specified the tuple is initialized from iterable's items. If the argument is a tuple, the return value is the same object. # (copied from class doc) @@ -5370,7 +5461,7 @@ class tuple(object): pass def __mul__(self, *args, **kwargs): # real signature unknown - """ Return self*value.n """ + """ Return self*value. """ pass @staticmethod # known case of __new__ @@ -5387,7 +5478,7 @@ class tuple(object): pass def __rmul__(self, *args, **kwargs): # real signature unknown - """ Return self*value. """ + """ Return value*self. """ pass @@ -5397,12 +5488,9 @@ class type(object): type(object) -> the object's type type(name, bases, dict) -> a new type """ - def mro(self): # real signature unknown; restored from __doc__ - """ - mro() -> list - return a type's method resolution order - """ - return [] + def mro(self, *args, **kwargs): # real signature unknown + """ Return a type's method resolution order. """ + pass def __call__(self, *args, **kwargs): # real signature unknown """ Call self as a function. """ @@ -5412,12 +5500,9 @@ class type(object): """ Implement delattr(self, name). """ pass - def __dir__(self): # real signature unknown; restored from __doc__ - """ - __dir__() -> list - specialized __dir__ implementation for types - """ - return [] + def __dir__(self, *args, **kwargs): # real signature unknown + """ Specialized __dir__ implementation for types. """ + pass def __getattribute__(self, *args, **kwargs): # real signature unknown """ Return getattr(self, name). """ @@ -5432,12 +5517,9 @@ class type(object): """ pass - def __instancecheck__(self): # real signature unknown; restored from __doc__ - """ - __instancecheck__() -> bool - check if an object is an instance - """ - return False + def __instancecheck__(self, *args, **kwargs): # real signature unknown + """ Check if an object is an instance. """ + pass @staticmethod # known case of __new__ def __new__(*args, **kwargs): # real signature unknown @@ -5459,23 +5541,17 @@ class type(object): """ Implement setattr(self, name, value). """ pass - def __sizeof__(self): # real signature unknown; restored from __doc__ - """ - __sizeof__() -> int - return memory consumption of the type object - """ - return 0 + def __sizeof__(self, *args, **kwargs): # real signature unknown + """ Return memory consumption of the type object. """ + pass - def __subclasscheck__(self): # real signature unknown; restored from __doc__ - """ - __subclasscheck__() -> bool - check if a class is a subclass - """ - return False + def __subclasscheck__(self, *args, **kwargs): # real signature unknown + """ Check if a class is a subclass. """ + pass - def __subclasses__(self): # real signature unknown; restored from __doc__ - """ __subclasses__() -> list of immediate subclasses """ - return [] + def __subclasses__(self, *args, **kwargs): # real signature unknown + """ Return a list of immediate subclasses. """ + pass __abstractmethods__ = property(lambda self: object(), lambda self, v: None, lambda self: None) # default @@ -5487,7 +5563,7 @@ class type(object): __basicsize__ = 864 __dictoffset__ = 264 __dict__ = None # (!) real value is '' - __flags__ = 2148291584 + __flags__ = -2146675712 __itemsize__ = 40 __mro__ = ( None, # (!) forward: type, real value is '' @@ -5749,7 +5825,7 @@ class __loader__(object): def load_module(self, *args, **kwargs): # real signature unknown """ Load the specified module into sys.modules and return it. - + This method is deprecated. Use loader.exec_module instead. """ pass diff --git a/python/testData/MockSdk3.4/python_stubs/sys.py b/python/testData/MockSdk3.7/python_stubs/sys.py similarity index 76% rename from python/testData/MockSdk3.4/python_stubs/sys.py rename to python/testData/MockSdk3.7/python_stubs/sys.py index d9ded608eeb6..bd1637f014a2 100644 --- a/python/testData/MockSdk3.4/python_stubs/sys.py +++ b/python/testData/MockSdk3.7/python_stubs/sys.py @@ -1,7 +1,7 @@ # encoding: utf-8 # module sys # from (built-in) -# by generator 1.138 +# by generator 1.145 """ This module provides access to some objects used or maintained by the interpreter and to functions that interact strongly with the interpreter. @@ -42,12 +42,15 @@ hexversion -- version information encoded as a single integer implementation -- Python implementation information. int_info -- a struct sequence with information about the int implementation. maxsize -- the largest supported length of containers. -maxunicode -- the value of the largest Unicode codepoint +maxunicode -- the value of the largest Unicode code point platform -- platform identifier prefix -- prefix used to find the Python library thread_info -- a struct sequence with information about the thread implementation. version -- the version of this interpreter as a string version_info -- version information as a named tuple +dllhandle -- [Windows only] integer handle of the Python DLL +winver -- [Windows only] version number of the Python DLL +_enablelegacywindowsfsencoding -- [Windows only] __stdin__ -- the original stdin; don't touch! __stdout__ -- the original stdout; don't touch! __stderr__ -- the original stderr; don't touch! @@ -76,44 +79,53 @@ settrace() -- set the global debug tracing function # Variables with simple values -abiflags = 'm' - api_version = 1013 -base_exec_prefix = '/Library/Frameworks/Python.framework/Versions/3.4' +base_exec_prefix = 'C:\\Python37' -base_prefix = '/Library/Frameworks/Python.framework/Versions/3.4' +base_prefix = 'C:\\Python37' byteorder = 'little' -copyright = 'Copyright (c) 2001-2014 Python Software Foundation.\nAll Rights Reserved.\n\nCopyright (c) 2000 BeOpen.com.\nAll Rights Reserved.\n\nCopyright (c) 1995-2001 Corporation for National Research Initiatives.\nAll Rights Reserved.\n\nCopyright (c) 1991-1995 Stichting Mathematisch Centrum, Amsterdam.\nAll Rights Reserved.' +copyright = 'Copyright (c) 2001-2018 Python Software Foundation.\nAll Rights Reserved.\n\nCopyright (c) 2000 BeOpen.com.\nAll Rights Reserved.\n\nCopyright (c) 1995-2001 Corporation for National Research Initiatives.\nAll Rights Reserved.\n\nCopyright (c) 1991-1995 Stichting Mathematisch Centrum, Amsterdam.\nAll Rights Reserved.' + +dllhandle = 140705079492608 dont_write_bytecode = True -executable = '/Users/vlan/.virtualenvs/obraz-py3.4/bin/python' +executable = 'C:\\Python37\\python.exe' -exec_prefix = '/Users/vlan/.virtualenvs/obraz-py3.4' +exec_prefix = 'C:\\Python37' float_repr_style = 'short' -hexversion = 50594544 +hexversion = 50790896 maxsize = 9223372036854775807 maxunicode = 1114111 -platform = 'darwin' +platform = 'win32' -prefix = '/Users/vlan/.virtualenvs/obraz-py3.4' +prefix = 'C:\\Python37' -version = '3.4.2 (v3.4.2:ab2c023a9432, Oct 5 2014, 20:42:22) \n[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)]' +version = '3.7.1 (v3.7.1:260ec2c36a, Oct 20 2018, 14:57:15) [MSC v.1915 64 bit (AMD64)]' -_home = '/Library/Frameworks/Python.framework/Versions/3.4/bin' +winver = '3.7' -__egginsert = 0 -__plen = 5 +_framework = '' + +_home = None # functions +def breakpointhook(*args, **kws): # real signature unknown; restored from __doc__ + """ + breakpointhook(*args, **kws) + + This hook function is called by built-in breakpoint(). + """ + pass + def callstats(): # real signature unknown; restored from __doc__ """ callstats() -> tuple of integers @@ -208,14 +220,14 @@ def getdefaultencoding(): # real signature unknown; restored from __doc__ """ return "" -def getdlopenflags(): # real signature unknown; restored from __doc__ +def getfilesystemencodeerrors(): # real signature unknown; restored from __doc__ """ - getdlopenflags() -> int + getfilesystemencodeerrors() -> string - Return the current value of the flags that are used for dlopen calls. - The flag constants are defined in the os module. + Return the error mode used to convert Unicode filenames in + operating system filenames. """ - return 0 + return "" def getfilesystemencoding(): # real signature unknown; restored from __doc__ """ @@ -276,6 +288,42 @@ def gettrace(): # real signature unknown; restored from __doc__ """ pass +def getwindowsversion(): # real signature unknown; restored from __doc__ + """ + getwindowsversion() + + Return information about the running version of Windows as a named tuple. + The members are named: major, minor, build, platform, service_pack, + service_pack_major, service_pack_minor, suite_mask, and product_type. For + backward compatibility, only the first 5 items are available by indexing. + All elements are numbers, except service_pack and platform_type which are + strings, and platform_version which is a 3-tuple. Platform is always 2. + Product_type may be 1 for a workstation, 2 for a domain controller, 3 for a + server. Platform_version is a 3-tuple containing a version number that is + intended for identifying the OS rather than feature detection. + """ + pass + +def get_asyncgen_hooks(): # real signature unknown; restored from __doc__ + """ + get_asyncgen_hooks() + + Return a namedtuple of installed asynchronous generators hooks (firstiter, finalizer). + """ + pass + +def get_coroutine_origin_tracking_depth(*args, **kwargs): # real signature unknown + """ Check status of origin tracking for coroutine objects in this thread. """ + pass + +def get_coroutine_wrapper(): # real signature unknown; restored from __doc__ + """ + get_coroutine_wrapper() + + Return the wrapper for coroutine objects set by sys.set_coroutine_wrapper. + """ + pass + def intern(string): # real signature unknown; restored from __doc__ """ intern(string) -> string @@ -287,6 +335,13 @@ def intern(string): # real signature unknown; restored from __doc__ """ return "" +def is_finalizing(): # real signature unknown; restored from __doc__ + """ + is_finalizing() + Return True if Python is exiting. + """ + pass + def setcheckinterval(n): # real signature unknown; restored from __doc__ """ setcheckinterval(n) @@ -296,19 +351,6 @@ def setcheckinterval(n): # real signature unknown; restored from __doc__ """ pass -def setdlopenflags(n): # real signature unknown; restored from __doc__ - """ - setdlopenflags(n) -> None - - Set the flags used by the interpreter for dlopen calls, such as when the - interpreter loads extension modules. Among other things, this will enable - a lazy resolving of symbols when importing a module, if called as - sys.setdlopenflags(0). To share symbols across extension modules, call as - sys.setdlopenflags(os.RTLD_GLOBAL). Symbolic names for the flag modules - can be found in the os module (RTLD_xxx constants, e.g. os.RTLD_LAZY). - """ - pass - def setprofile(function): # real signature unknown; restored from __doc__ """ setprofile(function) @@ -352,6 +394,32 @@ def settrace(function): # real signature unknown; restored from __doc__ """ pass +def set_asyncgen_hooks(*args, **kwargs): # real signature unknown; NOTE: unreliably restored from __doc__ + """ + set_asyncgen_hooks(*, firstiter=None, finalizer=None) + + Set a finalizer for async generators objects. + """ + pass + +def set_coroutine_origin_tracking_depth(*args, **kwargs): # real signature unknown + """ + Enable or disable origin tracking for coroutine objects in this thread. + + Coroutine objects will track 'depth' frames of traceback information about + where they came from, available in their cr_origin attribute. Set depth of 0 + to disable. + """ + pass + +def set_coroutine_wrapper(wrapper): # real signature unknown; restored from __doc__ + """ + set_coroutine_wrapper(wrapper) + + Set a wrapper for coroutine objects. + """ + pass + def _clear_type_cache(): # real signature unknown; restored from __doc__ """ _clear_type_cache() -> None @@ -382,6 +450,18 @@ def _debugmallocstats(): # real signature unknown; restored from __doc__ """ pass +def _enablelegacywindowsfsencoding(): # real signature unknown; restored from __doc__ + """ + _enablelegacywindowsfsencoding() + + Changes the default filesystem encoding to mbcs:replace for consistency + with earlier versions of Python. See PEP 529 for more information. + + This is equivalent to defining the PYTHONLEGACYWINDOWSFSENCODING + environment variable before launching Python. + """ + pass + def _getframe(depth=None): # real signature unknown; restored from __doc__ """ _getframe([depth]) -> frameobject @@ -396,6 +476,14 @@ def _getframe(depth=None): # real signature unknown; restored from __doc__ """ pass +def __breakpointhook__(*args, **kwargs): # real signature unknown + """ + breakpointhook(*args, **kws) + + This hook function is called by built-in breakpoint(). + """ + pass + def __displayhook__(*args, **kwargs): # real signature unknown """ displayhook(object) -> None @@ -425,6 +513,16 @@ class __loader__(object): All methods are either class or static methods to avoid the need to instantiate the class. """ + @classmethod + def create_module(cls, *args, **kwargs): # real signature unknown + """ Create a built-in module """ + pass + + @classmethod + def exec_module(cls, *args, **kwargs): # real signature unknown + """ Exec a built-in module """ + pass + @classmethod def find_module(cls, *args, **kwargs): # real signature unknown """ @@ -457,7 +555,11 @@ class __loader__(object): @classmethod def load_module(cls, *args, **kwargs): # real signature unknown - """ Load a built-in module. """ + """ + Load the specified module into sys.modules and return it. + + This method is deprecated. Use loader.exec_module instead. + """ pass def module_repr(module): # reliably restored by inspect @@ -498,6 +600,8 @@ flags = ( 0, 1, 0, + False, + 0, ) float_info = ( @@ -542,12 +646,12 @@ meta_path = [ modules = {} # real value of type skipped path = [ - '/Users/vlan/src/idea/out/classes/production/python-helpers', - '/Library/Frameworks/Python.framework/Versions/3.4/lib/python34.zip', - '/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4', - '/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/plat-darwin', - '/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/lib-dynload', - '/Users/vlan/.virtualenvs/obraz-py3.4/lib/python3.4/site-packages', + 'C:\\Projects\\IDEA\\out\\classes\\production\\intellij.python.helpers', + 'C:\\Python37\\python37.zip', + 'C:\\Python37\\DLLs', + 'C:\\Python37\\lib', + 'C:\\Python37', + 'C:\\Python37\\lib\\site-packages', ] path_hooks = [ @@ -557,39 +661,39 @@ path_hooks = [ path_importer_cache = {} # real value of type skipped -stderr = None # (!) forward: __stderr__, real value is '' +stderr = None # (!) real value is '' stdin = None # (!) forward: __stdin__, real value is '' stdout = None # (!) forward: __stdout__, real value is '' thread_info = ( - 'pthread', - 'mutex+cond', + 'nt', + None, None, ) version_info = ( 3, - 4, - 2, + 7, + 1, 'final', 0, ) warnoptions = [] -_mercurial = ( +_git = ( 'CPython', - 'v3.4.2', - 'ab2c023a9432', + 'v3.7.1', + '260ec2c36a', ) _xoptions = {} __spec__ = None # (!) real value is '' -__stderr__ = None # (!) real value is '' +__stderr__ = stderr __stdin__ = None # (!) real value is '' diff --git a/python/testSrc/com/jetbrains/python/fixtures/PyTestCase.java b/python/testSrc/com/jetbrains/python/fixtures/PyTestCase.java index c472e3aed72d..787c322eab88 100644 --- a/python/testSrc/com/jetbrains/python/fixtures/PyTestCase.java +++ b/python/testSrc/com/jetbrains/python/fixtures/PyTestCase.java @@ -76,7 +76,7 @@ import java.util.*; @TestDataPath("$CONTENT_ROOT/../testData/") public abstract class PyTestCase extends UsefulTestCase { public static final String PYTHON_2_MOCK_SDK = "2.7"; - public static final String PYTHON_3_MOCK_SDK = "3.4"; + public static final String PYTHON_3_MOCK_SDK = "3.7"; protected static final PyLightProjectDescriptor ourPyDescriptor = new PyLightProjectDescriptor(PYTHON_2_MOCK_SDK); protected static final PyLightProjectDescriptor ourPy3Descriptor = new PyLightProjectDescriptor(PYTHON_3_MOCK_SDK);