diff --git a/_py/builtin/builtin31.py b/_py/builtin.py similarity index 57% rename from _py/builtin/builtin31.py rename to _py/builtin.py index 782e85573..b416cf3dd 100644 --- a/_py/builtin/builtin31.py +++ b/_py/builtin.py @@ -1,6 +1,92 @@ -import py import sys +try: + reversed = reversed +except NameError: + def reversed(sequence): + """reversed(sequence) -> reverse iterator over values of the sequence + + Return a reverse iterator + """ + if hasattr(sequence, '__reversed__'): + return sequence.__reversed__() + if not hasattr(sequence, '__getitem__'): + raise TypeError("argument to reversed() must be a sequence") + return reversed_iterator(sequence) + + class reversed_iterator(object): + + def __init__(self, seq): + self.seq = seq + self.remaining = len(seq) + + def __iter__(self): + return self + + def next(self): + i = self.remaining + if i > 0: + i -= 1 + item = self.seq[i] + self.remaining = i + return item + raise StopIteration + + def __length_hint__(self): + return self.remaining + +try: + sorted = sorted +except NameError: + builtin_cmp = cmp # need to use cmp as keyword arg + + def sorted(iterable, cmp=None, key=None, reverse=0): + use_cmp = None + if key is not None: + if cmp is None: + def use_cmp(x, y): + return builtin_cmp(x[0], y[0]) + else: + def use_cmp(x, y): + return cmp(x[0], y[0]) + l = [(key(element), element) for element in iterable] + else: + if cmp is not None: + use_cmp = cmp + l = list(iterable) + if use_cmp is not None: + l.sort(use_cmp) + else: + l.sort() + if reverse: + l.reverse() + if key is not None: + return [element for (_, element) in l] + return l + +try: + set, frozenset = set, frozenset +except NameError: + from sets import set, frozenset + +# pass through +enumerate = enumerate + +try: + BaseException = BaseException +except NameError: + BaseException = Exception + +try: + GeneratorExit = GeneratorExit +except NameError: + class GeneratorExit(Exception): + """ This exception is never raised, it is there to make it possible to + write code compatible with CPython 2.5 even in lower CPython + versions.""" + pass + GeneratorExit.__module__ = 'exceptions' + if sys.version_info >= (3, 0): exec ("print_ = print ; exec_=exec") import builtins @@ -114,4 +200,4 @@ def _tryimport(*names): return __import__(name, None, None, '__doc__') except ImportError: excinfo = sys.exc_info() - py.builtin._reraise(*excinfo) + _reraise(*excinfo) diff --git a/_py/builtin/__init__.py b/_py/builtin/__init__.py deleted file mode 100644 index f9848c1a7..000000000 --- a/_py/builtin/__init__.py +++ /dev/null @@ -1,2 +0,0 @@ -""" backports and additions of builtins """ - diff --git a/_py/builtin/builtin24.py b/_py/builtin/builtin24.py deleted file mode 100644 index 51dab25af..000000000 --- a/_py/builtin/builtin24.py +++ /dev/null @@ -1,71 +0,0 @@ -try: - reversed = reversed -except NameError: - def reversed(sequence): - """reversed(sequence) -> reverse iterator over values of the sequence - - Return a reverse iterator - """ - if hasattr(sequence, '__reversed__'): - return sequence.__reversed__() - if not hasattr(sequence, '__getitem__'): - raise TypeError("argument to reversed() must be a sequence") - return reversed_iterator(sequence) - - class reversed_iterator(object): - - def __init__(self, seq): - self.seq = seq - self.remaining = len(seq) - - def __iter__(self): - return self - - def next(self): - i = self.remaining - if i > 0: - i -= 1 - item = self.seq[i] - self.remaining = i - return item - raise StopIteration - - def __length_hint__(self): - return self.remaining - -try: - sorted = sorted -except NameError: - builtin_cmp = cmp # need to use cmp as keyword arg - - def sorted(iterable, cmp=None, key=None, reverse=0): - use_cmp = None - if key is not None: - if cmp is None: - def use_cmp(x, y): - return builtin_cmp(x[0], y[0]) - else: - def use_cmp(x, y): - return cmp(x[0], y[0]) - l = [(key(element), element) for element in iterable] - else: - if cmp is not None: - use_cmp = cmp - l = list(iterable) - if use_cmp is not None: - l.sort(use_cmp) - else: - l.sort() - if reverse: - l.reverse() - if key is not None: - return [element for (_, element) in l] - return l - -try: - set, frozenset = set, frozenset -except NameError: - from sets import set, frozenset - -# pass through -enumerate = enumerate diff --git a/_py/builtin/builtin25.py b/_py/builtin/builtin25.py deleted file mode 100644 index 5eb98b22d..000000000 --- a/_py/builtin/builtin25.py +++ /dev/null @@ -1,15 +0,0 @@ - -try: - BaseException = BaseException -except NameError: - BaseException = Exception - -try: - GeneratorExit = GeneratorExit -except NameError: - class GeneratorExit(Exception): - """ This exception is never raised, it is there to make it possible to - write code compatible with CPython 2.5 even in lower CPython - versions.""" - pass - GeneratorExit.__module__ = 'exceptions' diff --git a/py/__init__.py b/py/__init__.py index 0156157ea..7484a7dd2 100644 --- a/py/__init__.py +++ b/py/__init__.py @@ -121,26 +121,26 @@ _py.apipkg.initpkg(__name__, dict( # backports and additions of builtins builtin = { '__doc__' : '_py.builtin:__doc__', - 'enumerate' : '_py.builtin.builtin24:enumerate', - 'reversed' : '_py.builtin.builtin24:reversed', - 'sorted' : '_py.builtin.builtin24:sorted', - 'set' : '_py.builtin.builtin24:set', - 'frozenset' : '_py.builtin.builtin24:frozenset', - 'BaseException' : '_py.builtin.builtin25:BaseException', - 'GeneratorExit' : '_py.builtin.builtin25:GeneratorExit', - 'print_' : '_py.builtin.builtin31:print_', - '_reraise' : '_py.builtin.builtin31:_reraise', - '_tryimport' : '_py.builtin.builtin31:_tryimport', - 'exec_' : '_py.builtin.builtin31:exec_', - '_basestring' : '_py.builtin.builtin31:_basestring', - '_totext' : '_py.builtin.builtin31:_totext', - '_isbytes' : '_py.builtin.builtin31:_isbytes', - '_istext' : '_py.builtin.builtin31:_istext', - '_getimself' : '_py.builtin.builtin31:_getimself', - '_getfuncdict' : '_py.builtin.builtin31:_getfuncdict', - 'builtins' : '_py.builtin.builtin31:builtins', - 'execfile' : '_py.builtin.builtin31:execfile', - 'callable' : '_py.builtin.builtin31:callable', + 'enumerate' : '_py.builtin:enumerate', + 'reversed' : '_py.builtin:reversed', + 'sorted' : '_py.builtin:sorted', + 'set' : '_py.builtin:set', + 'frozenset' : '_py.builtin:frozenset', + 'BaseException' : '_py.builtin:BaseException', + 'GeneratorExit' : '_py.builtin:GeneratorExit', + 'print_' : '_py.builtin:print_', + '_reraise' : '_py.builtin:_reraise', + '_tryimport' : '_py.builtin:_tryimport', + 'exec_' : '_py.builtin:exec_', + '_basestring' : '_py.builtin:_basestring', + '_totext' : '_py.builtin:_totext', + '_isbytes' : '_py.builtin:_isbytes', + '_istext' : '_py.builtin:_istext', + '_getimself' : '_py.builtin:_getimself', + '_getfuncdict' : '_py.builtin:_getfuncdict', + 'builtins' : '_py.builtin:builtins', + 'execfile' : '_py.builtin:execfile', + 'callable' : '_py.builtin:callable', }, # input-output helping