1.1 --- a/lib/UserDict.py Sat Dec 03 17:11:59 2016 +0100
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,176 +0,0 @@
1.4 -"""A more or less complete user-defined wrapper around dictionary objects."""
1.5 -
1.6 -class UserDict:
1.7 - def __init__(self, dict=None, **kwargs):
1.8 - self.data = {}
1.9 - if dict is not None:
1.10 - self.update(dict)
1.11 - if len(kwargs):
1.12 - self.update(kwargs)
1.13 - def __repr__(self): return repr(self.data)
1.14 - def __cmp__(self, dict):
1.15 - if isinstance(dict, UserDict):
1.16 - return cmp(self.data, dict.data)
1.17 - else:
1.18 - return cmp(self.data, dict)
1.19 - __hash__ = None # Avoid Py3k warning
1.20 - def __len__(self): return len(self.data)
1.21 - def __getitem__(self, key):
1.22 - if key in self.data:
1.23 - return self.data[key]
1.24 - if hasattr(self.__class__, "__missing__"):
1.25 - return self.__class__.__missing__(self, key)
1.26 - raise KeyError(key)
1.27 - def __setitem__(self, key, item): self.data[key] = item
1.28 - def __delitem__(self, key): del self.data[key]
1.29 - def clear(self): self.data.clear()
1.30 - def copy(self):
1.31 - if self.__class__ is UserDict:
1.32 - return UserDict(self.data.copy())
1.33 - import copy
1.34 - data = self.data
1.35 - try:
1.36 - self.data = {}
1.37 - c = copy.copy(self)
1.38 - finally:
1.39 - self.data = data
1.40 - c.update(self)
1.41 - return c
1.42 - def keys(self): return self.data.keys()
1.43 - def items(self): return self.data.items()
1.44 - def iteritems(self): return self.data.iteritems()
1.45 - def iterkeys(self): return self.data.iterkeys()
1.46 - def itervalues(self): return self.data.itervalues()
1.47 - def values(self): return self.data.values()
1.48 - def has_key(self, key): return key in self.data
1.49 - def update(self, dict=None, **kwargs):
1.50 - if dict is None:
1.51 - pass
1.52 - elif isinstance(dict, UserDict):
1.53 - self.data.update(dict.data)
1.54 - elif isinstance(dict, type({})) or not hasattr(dict, 'items'):
1.55 - self.data.update(dict)
1.56 - else:
1.57 - for k, v in dict.items():
1.58 - self[k] = v
1.59 - if len(kwargs):
1.60 - self.data.update(kwargs)
1.61 - def get(self, key, failobj=None):
1.62 - if key not in self:
1.63 - return failobj
1.64 - return self[key]
1.65 - def setdefault(self, key, failobj=None):
1.66 - if key not in self:
1.67 - self[key] = failobj
1.68 - return self[key]
1.69 - def pop(self, key, *args):
1.70 - return self.data.pop(key, *args)
1.71 - def popitem(self):
1.72 - return self.data.popitem()
1.73 - def __contains__(self, key):
1.74 - return key in self.data
1.75 - @classmethod
1.76 - def fromkeys(cls, iterable, value=None):
1.77 - d = cls()
1.78 - for key in iterable:
1.79 - d[key] = value
1.80 - return d
1.81 -
1.82 -class IterableUserDict(UserDict):
1.83 - def __iter__(self):
1.84 - return iter(self.data)
1.85 -
1.86 -class DictMixin:
1.87 - # Mixin defining all dictionary methods for classes that already have
1.88 - # a minimum dictionary interface including getitem, setitem, delitem,
1.89 - # and keys. Without knowledge of the subclass constructor, the mixin
1.90 - # does not define __init__() or copy(). In addition to the four base
1.91 - # methods, progressively more efficiency comes with defining
1.92 - # __contains__(), __iter__(), and iteritems().
1.93 -
1.94 - # second level definitions support higher levels
1.95 - def __iter__(self):
1.96 - for k in self.keys():
1.97 - yield k
1.98 - def has_key(self, key):
1.99 - try:
1.100 - self[key]
1.101 - except KeyError:
1.102 - return False
1.103 - return True
1.104 - def __contains__(self, key):
1.105 - return self.has_key(key)
1.106 -
1.107 - # third level takes advantage of second level definitions
1.108 - def iteritems(self):
1.109 - for k in self:
1.110 - yield (k, self[k])
1.111 - def iterkeys(self):
1.112 - return self.__iter__()
1.113 -
1.114 - # fourth level uses definitions from lower levels
1.115 - def itervalues(self):
1.116 - for _, v in self.iteritems():
1.117 - yield v
1.118 - def values(self):
1.119 - return [v for _, v in self.iteritems()]
1.120 - def items(self):
1.121 - return list(self.iteritems())
1.122 - def clear(self):
1.123 - for key in self.keys():
1.124 - del self[key]
1.125 - def setdefault(self, key, default=None):
1.126 - try:
1.127 - return self[key]
1.128 - except KeyError:
1.129 - self[key] = default
1.130 - return default
1.131 - def pop(self, key, *args):
1.132 - if len(args) > 1:
1.133 - raise TypeError, "pop expected at most 2 arguments, got "\
1.134 - + repr(1 + len(args))
1.135 - try:
1.136 - value = self[key]
1.137 - except KeyError:
1.138 - if args:
1.139 - return args[0]
1.140 - raise
1.141 - del self[key]
1.142 - return value
1.143 - def popitem(self):
1.144 - try:
1.145 - k, v = self.iteritems().next()
1.146 - except StopIteration:
1.147 - raise KeyError, 'container is empty'
1.148 - del self[k]
1.149 - return (k, v)
1.150 - def update(self, other=None, **kwargs):
1.151 - # Make progressively weaker assumptions about "other"
1.152 - if other is None:
1.153 - pass
1.154 - elif hasattr(other, 'iteritems'): # iteritems saves memory and lookups
1.155 - for k, v in other.iteritems():
1.156 - self[k] = v
1.157 - elif hasattr(other, 'keys'):
1.158 - for k in other.keys():
1.159 - self[k] = other[k]
1.160 - else:
1.161 - for k, v in other:
1.162 - self[k] = v
1.163 - if kwargs:
1.164 - self.update(kwargs)
1.165 - def get(self, key, default=None):
1.166 - try:
1.167 - return self[key]
1.168 - except KeyError:
1.169 - return default
1.170 - def __repr__(self):
1.171 - return repr(dict(self.iteritems()))
1.172 - def __cmp__(self, other):
1.173 - if other is None:
1.174 - return 1
1.175 - if isinstance(other, DictMixin):
1.176 - other = dict(other.iteritems())
1.177 - return cmp(dict(self.iteritems()), other)
1.178 - def __len__(self):
1.179 - return len(self.keys())
2.1 --- a/lib/_hashlib.py Sat Dec 03 17:11:59 2016 +0100
2.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
2.3 @@ -1,24 +0,0 @@
2.4 -#!/usr/bin/env python
2.5 -
2.6 -def new(s=None):
2.7 - pass
2.8 -
2.9 -def openssl_md5(s=None):
2.10 - pass
2.11 -
2.12 -def openssl_sha1(s=None):
2.13 - pass
2.14 -
2.15 -def openssl_sha224(s=None):
2.16 - pass
2.17 -
2.18 -def openssl_sha256(s=None):
2.19 - pass
2.20 -
2.21 -def openssl_sha384(s=None):
2.22 - pass
2.23 -
2.24 -def openssl_sha512(s=None):
2.25 - pass
2.26 -
2.27 -# vim: tabstop=4 expandtab shiftwidth=4
3.1 --- a/lib/_random.py Sat Dec 03 17:11:59 2016 +0100
3.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
3.3 @@ -1,30 +0,0 @@
3.4 -#!/usr/bin/env python
3.5 -
3.6 -"""
3.7 -Random numbers.
3.8 -
3.9 -Copyright (C) 2012 Paul Boddie <paul@boddie.org.uk>
3.10 -
3.11 -This program is free software; you can redistribute it and/or modify it under
3.12 -the terms of the GNU General Public License as published by the Free Software
3.13 -Foundation; either version 3 of the License, or (at your option) any later
3.14 -version.
3.15 -
3.16 -This program is distributed in the hope that it will be useful, but WITHOUT
3.17 -ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
3.18 -FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
3.19 -details.
3.20 -
3.21 -You should have received a copy of the GNU General Public License along with
3.22 -this program. If not, see <http://www.gnu.org/licenses/>.
3.23 -"""
3.24 -
3.25 -class Random:
3.26 - def getrandbits(self, k): pass
3.27 - def getstate(self): pass
3.28 - def jumpahead(self, x): pass
3.29 - def random(self): pass
3.30 - def seed(self, n=None): pass
3.31 - def setstate(self, state): pass
3.32 -
3.33 -# vim: tabstop=4 expandtab shiftwidth=4
4.1 --- a/lib/_sre.py Sat Dec 03 17:11:59 2016 +0100
4.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
4.3 @@ -1,44 +0,0 @@
4.4 -#!/usr/bin/env python
4.5 -
4.6 -CODESIZE = 4
4.7 -MAGIC = 20140812
4.8 -
4.9 -class RegexObject:
4.10 -
4.11 - def __init__(self, flags, groups, groupindex, pattern):
4.12 - self.flags = flags
4.13 - self.groups = groups
4.14 - self.groupindex = groupindex
4.15 - self.pattern = pattern
4.16 -
4.17 - def search(self, string, pos=None, endpos=None): pass
4.18 - def match(self, string, pos=None, endpos=None): pass
4.19 - def split(self, string, maxsplit=0): pass
4.20 - def findall(self, string, pos=None, endpos=None): pass
4.21 - def finditer(self, string, pos=None, endpos=None): pass
4.22 - def sub(self, repl, string, count=0): pass
4.23 - def subn(self, repl, string, count=0): pass
4.24 -
4.25 -class MatchObject:
4.26 -
4.27 - def __init__(self, pos, endpos, lastindex, lastgroup, re, string):
4.28 - self.pos = pos
4.29 - self.endpos = endpos
4.30 - self.lastindex = lastindex
4.31 - self.lastgroup = lastgroup
4.32 - self.re = re
4.33 - self.string = string
4.34 -
4.35 - def expand(self, template): pass
4.36 - def group(self, *groups): pass
4.37 - def groups(self, default=None): pass
4.38 - def groupdict(self, default=None): pass
4.39 - def start(self, group=None): pass
4.40 - def end(self, group=None): pass
4.41 - def span(self, group=None): pass
4.42 -
4.43 -def compile(pattern, flags=0): pass
4.44 -def getcodesize(): pass
4.45 -def getlower(c): pass
4.46 -
4.47 -# vim: tabstop=4 expandtab shiftwidth=4
5.1 --- a/lib/_weakref.py Sat Dec 03 17:11:59 2016 +0100
5.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
5.3 @@ -1,29 +0,0 @@
5.4 -#!/usr/bin/env python
5.5 -
5.6 -"""
5.7 -Weak references.
5.8 -
5.9 -Copyright (C) 2012 Paul Boddie <paul@boddie.org.uk>
5.10 -
5.11 -This program is free software; you can redistribute it and/or modify it under
5.12 -the terms of the GNU General Public License as published by the Free Software
5.13 -Foundation; either version 3 of the License, or (at your option) any later
5.14 -version.
5.15 -
5.16 -This program is distributed in the hope that it will be useful, but WITHOUT
5.17 -ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
5.18 -FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
5.19 -details.
5.20 -
5.21 -You should have received a copy of the GNU General Public License along with
5.22 -this program. If not, see <http://www.gnu.org/licenses/>.
5.23 -"""
5.24 -
5.25 -class ref:
5.26 - pass
5.27 -
5.28 -def getweakrefcount(obj): pass
5.29 -def getweakrefs(obj): pass
5.30 -def proxy(obj, callback=None): pass
5.31 -
5.32 -# vim: tabstop=4 expandtab shiftwidth=4
6.1 --- a/lib/array.py Sat Dec 03 17:11:59 2016 +0100
6.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
6.3 @@ -1,53 +0,0 @@
6.4 -#!/usr/bin/env python
6.5 -
6.6 -"""
6.7 -Array functions and objects.
6.8 -
6.9 -Copyright (C) 2011, 2014 Paul Boddie <paul@boddie.org.uk>
6.10 -
6.11 -This program is free software; you can redistribute it and/or modify it under
6.12 -the terms of the GNU General Public License as published by the Free Software
6.13 -Foundation; either version 3 of the License, or (at your option) any later
6.14 -version.
6.15 -
6.16 -This program is distributed in the hope that it will be useful, but WITHOUT
6.17 -ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
6.18 -FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
6.19 -details.
6.20 -
6.21 -You should have received a copy of the GNU General Public License along with
6.22 -this program. If not, see <http://www.gnu.org/licenses/>.
6.23 -"""
6.24 -
6.25 -class array:
6.26 -
6.27 - """
6.28 - An array of primitive objects.
6.29 - NOTE: In principle, arrays of full instances could be supported with
6.30 - NOTE: knowledge of the size of each instance.
6.31 - """
6.32 -
6.33 - def __init__(self, typecode, itemsize):
6.34 - self.typecode = typecode
6.35 - self.itemsize = itemsize
6.36 -
6.37 - def append(self, value): pass
6.38 - def buffer_info(self): pass
6.39 - def byteswap(self): pass
6.40 - def count(self, value): pass
6.41 - def extend(self, l): pass
6.42 - def fromfile(self, f): pass
6.43 - def fromlist(self, l): pass
6.44 - def fromstring(self, s): pass
6.45 - def index(self, value): pass
6.46 - def insert(self, index, value): pass
6.47 - def pop(self): pass
6.48 - def read(self): pass
6.49 - def remove(self, value): pass
6.50 - def reverse(self): pass
6.51 - def tofile(self): pass
6.52 - def tolist(self): pass
6.53 - def tostring(self): pass
6.54 - def write(self): pass
6.55 -
6.56 -# vim: tabstop=4 expandtab shiftwidth=4
8.1 --- a/lib/collections.py Sat Dec 03 17:11:59 2016 +0100
8.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
8.3 @@ -1,36 +0,0 @@
8.4 -#!/usr/bin/env python
8.5 -
8.6 -"""
8.7 -Collections.
8.8 -
8.9 -Copyright (C) 2013 Paul Boddie <paul@boddie.org.uk>
8.10 -
8.11 -This program is free software; you can redistribute it and/or modify it under
8.12 -the terms of the GNU General Public License as published by the Free Software
8.13 -Foundation; either version 3 of the License, or (at your option) any later
8.14 -version.
8.15 -
8.16 -This program is distributed in the hope that it will be useful, but WITHOUT
8.17 -ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
8.18 -FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
8.19 -details.
8.20 -
8.21 -You should have received a copy of the GNU General Public License along with
8.22 -this program. If not, see <http://www.gnu.org/licenses/>.
8.23 -"""
8.24 -
8.25 -class defaultdict(dict):
8.26 - pass
8.27 -
8.28 -class deque(object):
8.29 - def append(x): pass
8.30 - def appendleft(x): pass
8.31 - def clear(): pass
8.32 - def extend(iterable): pass
8.33 - def extendleft(iterable): pass
8.34 - def pop(): pass
8.35 - def popleft(): pass
8.36 - def remove(value): pass
8.37 - def rotate(n): pass
8.38 -
8.39 -# vim: tabstop=4 expandtab shiftwidth=4
9.1 --- a/lib/copy.py Sat Dec 03 17:11:59 2016 +0100
9.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
9.3 @@ -1,25 +0,0 @@
9.4 -#!/usr/bin/env python
9.5 -
9.6 -"""
9.7 -Copying functions.
9.8 -
9.9 -Copyright (C) 2015 Paul Boddie <paul@boddie.org.uk>
9.10 -
9.11 -This program is free software; you can redistribute it and/or modify it under
9.12 -the terms of the GNU General Public License as published by the Free Software
9.13 -Foundation; either version 3 of the License, or (at your option) any later
9.14 -version.
9.15 -
9.16 -This program is distributed in the hope that it will be useful, but WITHOUT
9.17 -ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
9.18 -FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
9.19 -details.
9.20 -
9.21 -You should have received a copy of the GNU General Public License along with
9.22 -this program. If not, see <http://www.gnu.org/licenses/>.
9.23 -"""
9.24 -
9.25 -def copy(x): pass
9.26 -def deepcopy(x): pass
9.27 -
9.28 -# vim: tabstop=4 expandtab shiftwidth=4
10.1 --- a/lib/exceptions.py Sat Dec 03 17:11:59 2016 +0100
10.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
10.3 @@ -1,1 +0,0 @@
10.4 -from __builtins__.exception import *
11.1 --- a/lib/genericos.py Sat Dec 03 17:11:59 2016 +0100
11.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
11.3 @@ -1,48 +0,0 @@
11.4 -#!/usr/bin/env python
11.5 -
11.6 -from posix.io import fstat
11.7 -from posix.filesys import listdir, lstat, readlink
11.8 -from posix.process import getcwd, getcwdu, getuid
11.9 -
11.10 -class OSError(EnvironmentError):
11.11 - pass
11.12 -
11.13 -error = OSError
11.14 -environ = {}
11.15 -
11.16 -def execl(file, *args): pass
11.17 -def execle(file, *args): pass
11.18 -def execlp(file, *args): pass
11.19 -def execlpe(file, *args): pass
11.20 -def execv(path, args): pass
11.21 -def execve(path, args, env): pass
11.22 -def execvp(file, args): pass
11.23 -def execvpe(file, args, env): pass
11.24 -def getenv(key, default=None): pass
11.25 -def makedirs(name, mode=511): pass
11.26 -def popen2(cmd, mode='t', bufsize=-1): pass
11.27 -def popen3(cmd, mode='t', bufsize=-1): pass
11.28 -def popen4(cmd, mode='t', bufsize=-1): pass
11.29 -def removedirs(name): pass
11.30 -def renames(old, new): pass
11.31 -def spawnl(mode, file, *args): pass
11.32 -def spawnle(mode, file, *args): pass
11.33 -def spawnlp(mode, file, *args): pass
11.34 -def spawnlpe(mode, file, *args): pass
11.35 -def spawnv(mode, file, args): pass
11.36 -def spawnve(mode, file, args, env): pass
11.37 -def spawnvp(mode, file, args): pass
11.38 -def spawnvpe(mode, file, args, env): pass
11.39 -def walk(top, topdown=True, onerror=None, followlinks=False): pass
11.40 -
11.41 -P_WAIT = 0
11.42 -P_NOWAIT = P_NOWAITO = 1
11.43 -
11.44 -SEEK_CUR = 1
11.45 -SEEK_END = 2
11.46 -SEEK_SET = 0
11.47 -
11.48 -name = 'posix'
11.49 -linesep = '\n'
11.50 -
11.51 -# vim: tabstop=4 expandtab shiftwidth=4
12.1 --- a/lib/genericpath.py Sat Dec 03 17:11:59 2016 +0100
12.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
12.3 @@ -1,105 +0,0 @@
12.4 -"""
12.5 -Path operations common to more than one OS
12.6 -Do not use directly. The OS specific modules import the appropriate
12.7 -functions from this module themselves.
12.8 -"""
12.9 -from os import error, stat
12.10 -from stat import *
12.11 -
12.12 -__all__ = ['commonprefix', 'exists', 'getatime', 'getctime', 'getmtime',
12.13 - 'getsize', 'isdir', 'isfile']
12.14 -
12.15 -
12.16 -# Does a path exist?
12.17 -# This is false for dangling symbolic links on systems that support them.
12.18 -def exists(path):
12.19 - """Test whether a path exists. Returns False for broken symbolic links"""
12.20 - try:
12.21 - stat(path)
12.22 - except error:
12.23 - return False
12.24 - return True
12.25 -
12.26 -
12.27 -# This follows symbolic links, so both islink() and isdir() can be true
12.28 -# for the same path ono systems that support symlinks
12.29 -def isfile(path):
12.30 - """Test whether a path is a regular file"""
12.31 - try:
12.32 - st = stat(path)
12.33 - except error:
12.34 - return False
12.35 - return stat.S_ISREG(st.st_mode)
12.36 -
12.37 -
12.38 -# Is a path a directory?
12.39 -# This follows symbolic links, so both islink() and isdir()
12.40 -# can be true for the same path on systems that support symlinks
12.41 -def isdir(s):
12.42 - """Return true if the pathname refers to an existing directory."""
12.43 - try:
12.44 - st = stat(s)
12.45 - except error:
12.46 - return False
12.47 - return stat.S_ISDIR(st.st_mode)
12.48 -
12.49 -
12.50 -def getsize(filename):
12.51 - """Return the size of a file, reported by os.stat()."""
12.52 - return stat(filename).st_size
12.53 -
12.54 -
12.55 -def getmtime(filename):
12.56 - """Return the last modification time of a file, reported by os.stat()."""
12.57 - return stat(filename).st_mtime
12.58 -
12.59 -
12.60 -def getatime(filename):
12.61 - """Return the last access time of a file, reported by os.stat()."""
12.62 - return stat(filename).st_atime
12.63 -
12.64 -
12.65 -def getctime(filename):
12.66 - """Return the metadata change time of a file, reported by os.stat()."""
12.67 - return stat(filename).st_ctime
12.68 -
12.69 -
12.70 -# Return the longest prefix of all list elements.
12.71 -def commonprefix(m):
12.72 - "Given a list of pathnames, returns the longest common leading component"
12.73 - if not m: return ''
12.74 - s1 = min(m)
12.75 - s2 = max(m)
12.76 - for i, c in enumerate(s1):
12.77 - if c != s2[i]:
12.78 - return s1[:i]
12.79 - return s1
12.80 -
12.81 -# Split a path in root and extension.
12.82 -# The extension is everything starting at the last dot in the last
12.83 -# pathname component; the root is everything before that.
12.84 -# It is always true that root + ext == p.
12.85 -
12.86 -# Generic implementation of splitext, to be parametrized with
12.87 -# the separators
12.88 -def _splitext(p, sep, altsep, extsep):
12.89 - """Split the extension from a pathname.
12.90 -
12.91 - Extension is everything from the last dot to the end, ignoring
12.92 - leading dots. Returns "(root, ext)"; ext may be empty."""
12.93 -
12.94 - sepIndex = p.rfind(sep)
12.95 - if altsep:
12.96 - altsepIndex = p.rfind(altsep)
12.97 - sepIndex = max(sepIndex, altsepIndex)
12.98 -
12.99 - dotIndex = p.rfind(extsep)
12.100 - if dotIndex > sepIndex:
12.101 - # skip all leading dots
12.102 - filenameIndex = sepIndex + 1
12.103 - while filenameIndex < dotIndex:
12.104 - if p[filenameIndex] != extsep:
12.105 - return p[:dotIndex], p[dotIndex:]
12.106 - filenameIndex += 1
12.107 -
12.108 - return p, ''
13.1 --- a/lib/itertools.py Sat Dec 03 17:11:59 2016 +0100
13.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
13.3 @@ -1,199 +0,0 @@
13.4 -#!/usr/bin/env python
13.5 -
13.6 -class chain:
13.7 - def __init__(self, *iterables):
13.8 - pass
13.9 -
13.10 - def __iter__(self):
13.11 - pass
13.12 -
13.13 - def from_iterable(self, iterable):
13.14 - pass
13.15 -
13.16 - def next(self):
13.17 - pass
13.18 -
13.19 -class combinations:
13.20 - def __init__(self, iterable, r):
13.21 - pass
13.22 -
13.23 - def __iter__(self):
13.24 - pass
13.25 -
13.26 - def next(self):
13.27 - pass
13.28 -
13.29 -class combinations_with_replacement:
13.30 - def __init__(self, iterable, r):
13.31 - pass
13.32 -
13.33 - def __iter__(self):
13.34 - pass
13.35 -
13.36 - def next(self):
13.37 - pass
13.38 -
13.39 -class compress:
13.40 - def __init__(self, data, selectors):
13.41 - pass
13.42 -
13.43 - def __iter__(self):
13.44 - pass
13.45 -
13.46 - def next(self):
13.47 - pass
13.48 -
13.49 -class count:
13.50 - def __init__(self, start=0, step=1):
13.51 - pass
13.52 -
13.53 - def __iter__(self):
13.54 - pass
13.55 -
13.56 - def next(self):
13.57 - pass
13.58 -
13.59 -class cycle:
13.60 - def __init__(self, iterable):
13.61 - pass
13.62 -
13.63 - def __iter__(self):
13.64 - pass
13.65 -
13.66 - def next(self):
13.67 - pass
13.68 -
13.69 -class dropwhile:
13.70 - def __init__(self, predicate, iterable):
13.71 - pass
13.72 -
13.73 - def __iter__(self):
13.74 - pass
13.75 -
13.76 - def next(self):
13.77 - pass
13.78 -
13.79 -class groupby:
13.80 - def __init__(self, iterable, keyfunc=None):
13.81 - pass
13.82 -
13.83 - def __iter__(self):
13.84 - pass
13.85 -
13.86 - def next(self):
13.87 - pass
13.88 -
13.89 -class ifilter:
13.90 - def __init__(self, function, sequence):
13.91 - pass
13.92 -
13.93 - def __iter__(self):
13.94 - pass
13.95 -
13.96 - def next(self):
13.97 - pass
13.98 -
13.99 -class ifilterfalse:
13.100 - def __init__(self, function, sequence):
13.101 - pass
13.102 -
13.103 - def __iter__(self):
13.104 - pass
13.105 -
13.106 - def next(self):
13.107 - pass
13.108 -
13.109 -class imap:
13.110 - def __init__(self, func, *iterables):
13.111 - pass
13.112 -
13.113 - def __iter__(self):
13.114 - pass
13.115 -
13.116 - def next(self):
13.117 - pass
13.118 -
13.119 -class islice:
13.120 - def __init__(self, iterable, start_or_stop, stop=None, step=None):
13.121 - pass
13.122 -
13.123 - def __iter__(self):
13.124 - pass
13.125 -
13.126 - def next(self):
13.127 - pass
13.128 -
13.129 -class izip:
13.130 - def __init__(self, *args):
13.131 - pass
13.132 -
13.133 - def __iter__(self):
13.134 - pass
13.135 -
13.136 - def next(self):
13.137 - pass
13.138 -
13.139 -class izip_longest:
13.140 - def __init__(self, *args, **kw): #fillvalue=None
13.141 - pass
13.142 -
13.143 - def __iter__(self):
13.144 - pass
13.145 -
13.146 - def next(self):
13.147 - pass
13.148 -
13.149 -class permutations:
13.150 - def __init__(self, iterable, r=None):
13.151 - pass
13.152 -
13.153 - def __iter__(self):
13.154 - pass
13.155 -
13.156 - def next(self):
13.157 - pass
13.158 -
13.159 -class product:
13.160 - def __init__(self, *iterables):
13.161 - pass
13.162 -
13.163 - def __iter__(self):
13.164 - pass
13.165 -
13.166 - def next(self):
13.167 - pass
13.168 -
13.169 -class repeat:
13.170 - def __init__(self, object, times=None):
13.171 - pass
13.172 -
13.173 - def __iter__(self):
13.174 - pass
13.175 -
13.176 - def next(self):
13.177 - pass
13.178 -
13.179 -class starmap:
13.180 - def __init__(self, function, sequence):
13.181 - pass
13.182 -
13.183 - def __iter__(self):
13.184 - pass
13.185 -
13.186 - def next(self):
13.187 - pass
13.188 -
13.189 -class takewhile:
13.190 - def __init__(self, predicate, iterable):
13.191 - pass
13.192 -
13.193 - def __iter__(self):
13.194 - pass
13.195 -
13.196 - def next(self):
13.197 - pass
13.198 -
13.199 -def tee(iterable, n=2):
13.200 - pass
13.201 -
13.202 -# vim: tabstop=4 expandtab shiftwidth=4
14.1 --- a/lib/math.py Sat Dec 03 17:11:59 2016 +0100
14.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
14.3 @@ -1,58 +0,0 @@
14.4 -#!/usr/bin/env python
14.5 -
14.6 -"""
14.7 -Mathematical functions.
14.8 -
14.9 -Copyright (C) 2012 Paul Boddie <paul@boddie.org.uk>
14.10 -
14.11 -This program is free software; you can redistribute it and/or modify it under
14.12 -the terms of the GNU General Public License as published by the Free Software
14.13 -Foundation; either version 3 of the License, or (at your option) any later
14.14 -version.
14.15 -
14.16 -This program is distributed in the hope that it will be useful, but WITHOUT
14.17 -ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14.18 -FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14.19 -details.
14.20 -
14.21 -You should have received a copy of the GNU General Public License along with
14.22 -this program. If not, see <http://www.gnu.org/licenses/>.
14.23 -"""
14.24 -
14.25 -def pow(x, y):
14.26 -
14.27 - "Return x ** y."
14.28 -
14.29 - return x ** y
14.30 -
14.31 -def acos(x): pass
14.32 -def asin(x): pass
14.33 -def atan(x): pass
14.34 -def atan2(y, x): pass
14.35 -def ceil(x): pass
14.36 -def cos(x): pass
14.37 -def cosh(x): pass
14.38 -def degrees(x): pass
14.39 -def exp(x): pass
14.40 -def fabs(x): pass
14.41 -def floor(x): pass
14.42 -def fmod(x, y): pass
14.43 -def frexp(x): pass
14.44 -def hypot(x, y): pass
14.45 -def ldexp(x, i): pass
14.46 -def log(x, base=None): pass
14.47 -def log10(x): pass
14.48 -def modf(x): pass
14.49 -def radians(x): pass
14.50 -def sin(x): pass
14.51 -def sinh(x): pass
14.52 -def sqrt(x): pass
14.53 -def tan(x): pass
14.54 -def tanh(x): pass
14.55 -
14.56 -# NOTE: From Python 2.7 on i386.
14.57 -
14.58 -e = 2.718281828459045
14.59 -pi = 3.141592653589793
14.60 -
14.61 -# vim: tabstop=4 expandtab shiftwidth=4
16.1 --- a/lib/parser.py Sat Dec 03 17:11:59 2016 +0100
16.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
16.3 @@ -1,27 +0,0 @@
16.4 -#!/usr/bin/env python
16.5 -
16.6 -class ASTType:
16.7 - pass
16.8 -
16.9 -class ParserError(Exception):
16.10 - pass
16.11 -
16.12 -class STType:
16.13 - pass
16.14 -
16.15 -def ast2list(ast): pass
16.16 -def ast2tuple(ast): pass
16.17 -def compileast(ast, filename=None): pass
16.18 -def compilest(st): pass
16.19 -def expr(s): pass
16.20 -def isexpr(ast): pass
16.21 -def issuite(ast): pass
16.22 -def sequence2ast(sequence): pass
16.23 -def sequence2st(sequence): pass
16.24 -def st2list(st, line_info=None, col_info=None): pass
16.25 -def st2tuple(st, line_info=None, col_info=None): pass
16.26 -def suite(suite): pass
16.27 -def tuple2ast(sequence): pass
16.28 -def tuple2st(sequence): pass
16.29 -
16.30 -# vim: tabstop=4 expandtab shiftwidth=4
17.1 --- a/lib/posixpath.py Sat Dec 03 17:11:59 2016 +0100
17.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
17.3 @@ -1,420 +0,0 @@
17.4 -"""Common operations on Posix pathnames.
17.5 -
17.6 -Instead of importing this module directly, import os and refer to
17.7 -this module as os.path. The "os.path" name is an alias for this
17.8 -module on Posix systems; on other systems (e.g. Mac, Windows),
17.9 -os.path provides the same operations in a manner specific to that
17.10 -platform, and is an alias to another module (e.g. macpath, ntpath).
17.11 -
17.12 -Some of this can actually be useful on non-Posix systems too, e.g.
17.13 -for manipulation of the pathname component of URLs.
17.14 -"""
17.15 -
17.16 -from genericos import environ, error, fstat, getcwd, getcwdu, getuid, listdir, lstat, readlink
17.17 -import sys
17.18 -import stat
17.19 -import genericpath
17.20 -from genericpath import *
17.21 -
17.22 -try:
17.23 - _unicode = unicode
17.24 -except NameError:
17.25 - # If Python is built without Unicode support, the unicode type
17.26 - # will not exist. Fake one.
17.27 - class _unicode(object):
17.28 - pass
17.29 -
17.30 -__all__ = ["normcase","isabs","join","splitdrive","split","splitext",
17.31 - "basename","dirname","commonprefix","getsize","getmtime",
17.32 - "getatime","getctime","islink","exists","lexists","isdir","isfile",
17.33 - "ismount","walk","expanduser","expandvars","normpath","abspath",
17.34 - "samefile","sameopenfile","samestat",
17.35 - "curdir","pardir","sep","pathsep","defpath","altsep","extsep",
17.36 - "devnull","realpath","supports_unicode_filenames","relpath"]
17.37 -
17.38 -# strings representing various path-related bits and pieces
17.39 -curdir = '.'
17.40 -pardir = '..'
17.41 -extsep = '.'
17.42 -sep = '/'
17.43 -pathsep = ':'
17.44 -defpath = ':/bin:/usr/bin'
17.45 -altsep = None
17.46 -devnull = '/dev/null'
17.47 -
17.48 -# Normalize the case of a pathname. Trivial in Posix, string.lower on Mac.
17.49 -# On MS-DOS this may also turn slashes into backslashes; however, other
17.50 -# normalizations (such as optimizing '../' away) are not allowed
17.51 -# (another function should be defined to do that).
17.52 -
17.53 -def normcase(s):
17.54 - """Normalize case of pathname. Has no effect under Posix"""
17.55 - return s
17.56 -
17.57 -
17.58 -# Return whether a path is absolute.
17.59 -# Trivial in Posix, harder on the Mac or MS-DOS.
17.60 -
17.61 -def isabs(s):
17.62 - """Test whether a path is absolute"""
17.63 - return s.startswith('/')
17.64 -
17.65 -
17.66 -# Join pathnames.
17.67 -# Ignore the previous parts if a part is absolute.
17.68 -# Insert a '/' unless the first part is empty or already ends in '/'.
17.69 -
17.70 -def join(a, *p):
17.71 - """Join two or more pathname components, inserting '/' as needed.
17.72 - If any component is an absolute path, all previous path components
17.73 - will be discarded. An empty last part will result in a path that
17.74 - ends with a separator."""
17.75 - path = a
17.76 - for b in p:
17.77 - if b.startswith('/'):
17.78 - path = b
17.79 - elif path == '' or path.endswith('/'):
17.80 - path += b
17.81 - else:
17.82 - path += '/' + b
17.83 - return path
17.84 -
17.85 -
17.86 -# Split a path in head (everything up to the last '/') and tail (the
17.87 -# rest). If the path ends in '/', tail will be empty. If there is no
17.88 -# '/' in the path, head will be empty.
17.89 -# Trailing '/'es are stripped from head unless it is the root.
17.90 -
17.91 -def split(p):
17.92 - """Split a pathname. Returns tuple "(head, tail)" where "tail" is
17.93 - everything after the final slash. Either part may be empty."""
17.94 - i = p.rfind('/') + 1
17.95 - head, tail = p[:i], p[i:]
17.96 - if head and head != '/'*len(head):
17.97 - head = head.rstrip('/')
17.98 - return head, tail
17.99 -
17.100 -
17.101 -# Split a path in root and extension.
17.102 -# The extension is everything starting at the last dot in the last
17.103 -# pathname component; the root is everything before that.
17.104 -# It is always true that root + ext == p.
17.105 -
17.106 -def splitext(p):
17.107 - return genericpath._splitext(p, sep, altsep, extsep)
17.108 -
17.109 -# Split a pathname into a drive specification and the rest of the
17.110 -# path. Useful on DOS/Windows/NT; on Unix, the drive is always empty.
17.111 -
17.112 -def splitdrive(p):
17.113 - """Split a pathname into drive and path. On Posix, drive is always
17.114 - empty."""
17.115 - return '', p
17.116 -
17.117 -
17.118 -# Return the tail (basename) part of a path, same as split(path)[1].
17.119 -
17.120 -def basename(p):
17.121 - """Returns the final component of a pathname"""
17.122 - i = p.rfind('/') + 1
17.123 - return p[i:]
17.124 -
17.125 -
17.126 -# Return the head (dirname) part of a path, same as split(path)[0].
17.127 -
17.128 -def dirname(p):
17.129 - """Returns the directory component of a pathname"""
17.130 - i = p.rfind('/') + 1
17.131 - head = p[:i]
17.132 - if head and head != '/'*len(head):
17.133 - head = head.rstrip('/')
17.134 - return head
17.135 -
17.136 -
17.137 -# Is a path a symbolic link?
17.138 -# This will always return false on systems where os.lstat doesn't exist.
17.139 -
17.140 -def islink(path):
17.141 - """Test whether a path is a symbolic link"""
17.142 - try:
17.143 - st = lstat(path)
17.144 - except (error, AttributeError):
17.145 - return False
17.146 - return stat.S_ISLNK(st.st_mode)
17.147 -
17.148 -# Being true for dangling symbolic links is also useful.
17.149 -
17.150 -def lexists(path):
17.151 - """Test whether a path exists. Returns True for broken symbolic links"""
17.152 - try:
17.153 - lstat(path)
17.154 - except error:
17.155 - return False
17.156 - return True
17.157 -
17.158 -
17.159 -# Are two filenames really pointing to the same file?
17.160 -
17.161 -def samefile(f1, f2):
17.162 - """Test whether two pathnames reference the same actual file"""
17.163 - s1 = stat(f1)
17.164 - s2 = stat(f2)
17.165 - return samestat(s1, s2)
17.166 -
17.167 -
17.168 -# Are two open files really referencing the same file?
17.169 -# (Not necessarily the same file descriptor!)
17.170 -
17.171 -def sameopenfile(fp1, fp2):
17.172 - """Test whether two open file objects reference the same file"""
17.173 - s1 = fstat(fp1)
17.174 - s2 = fstat(fp2)
17.175 - return samestat(s1, s2)
17.176 -
17.177 -
17.178 -# Are two stat buffers (obtained from stat, fstat or lstat)
17.179 -# describing the same file?
17.180 -
17.181 -def samestat(s1, s2):
17.182 - """Test whether two stat buffers reference the same file"""
17.183 - return s1.st_ino == s2.st_ino and \
17.184 - s1.st_dev == s2.st_dev
17.185 -
17.186 -
17.187 -# Is a path a mount point?
17.188 -# (Does this work for all UNIXes? Is it even guaranteed to work by Posix?)
17.189 -
17.190 -def ismount(path):
17.191 - """Test whether a path is a mount point"""
17.192 - if islink(path):
17.193 - # A symlink can never be a mount point
17.194 - return False
17.195 - try:
17.196 - s1 = lstat(path)
17.197 - s2 = lstat(join(path, '..'))
17.198 - except error:
17.199 - return False # It doesn't exist -- so not a mount point :-)
17.200 - dev1 = s1.st_dev
17.201 - dev2 = s2.st_dev
17.202 - if dev1 != dev2:
17.203 - return True # path/.. on a different device as path
17.204 - ino1 = s1.st_ino
17.205 - ino2 = s2.st_ino
17.206 - if ino1 == ino2:
17.207 - return True # path/.. is the same i-node as path
17.208 - return False
17.209 -
17.210 -
17.211 -# Directory tree walk.
17.212 -# For each directory under top (including top itself, but excluding
17.213 -# '.' and '..'), func(arg, dirname, filenames) is called, where
17.214 -# dirname is the name of the directory and filenames is the list
17.215 -# of files (and subdirectories etc.) in the directory.
17.216 -# The func may modify the filenames list, to implement a filter,
17.217 -# or to impose a different order of visiting.
17.218 -
17.219 -def walk(top, func, arg):
17.220 - """Directory tree walk with callback function.
17.221 -
17.222 - For each directory in the directory tree rooted at top (including top
17.223 - itself, but excluding '.' and '..'), call func(arg, dirname, fnames).
17.224 - dirname is the name of the directory, and fnames a list of the names of
17.225 - the files and subdirectories in dirname (excluding '.' and '..'). func
17.226 - may modify the fnames list in-place (e.g. via del or slice assignment),
17.227 - and walk will only recurse into the subdirectories whose names remain in
17.228 - fnames; this can be used to implement a filter, or to impose a specific
17.229 - order of visiting. No semantics are defined for, or required of, arg,
17.230 - beyond that arg is always passed to func. It can be used, e.g., to pass
17.231 - a filename pattern, or a mutable object designed to accumulate
17.232 - statistics. Passing None for arg is common."""
17.233 - try:
17.234 - names = listdir(top)
17.235 - except error:
17.236 - return
17.237 - func(arg, top, names)
17.238 - for name in names:
17.239 - name = join(top, name)
17.240 - try:
17.241 - st = lstat(name)
17.242 - except error:
17.243 - continue
17.244 - if stat.S_ISDIR(st.st_mode):
17.245 - walk(name, func, arg)
17.246 -
17.247 -
17.248 -# Expand paths beginning with '~' or '~user'.
17.249 -# '~' means $HOME; '~user' means that user's home directory.
17.250 -# If the path doesn't begin with '~', or if the user or $HOME is unknown,
17.251 -# the path is returned unchanged (leaving error reporting to whatever
17.252 -# function is called with the expanded path as argument).
17.253 -# See also module 'glob' for expansion of *, ? and [...] in pathnames.
17.254 -# (A function should also be defined to do full *sh-style environment
17.255 -# variable expansion.)
17.256 -
17.257 -def expanduser(path):
17.258 - """Expand ~ and ~user constructions. If user or $HOME is unknown,
17.259 - do nothing."""
17.260 - if not path.startswith('~'):
17.261 - return path
17.262 - i = path.find('/', 1)
17.263 - if i < 0:
17.264 - i = len(path)
17.265 - if i == 1:
17.266 - if 'HOME' not in environ:
17.267 - import pwd
17.268 - userhome = pwd.getpwuid(getuid()).pw_dir
17.269 - else:
17.270 - userhome = environ['HOME']
17.271 - else:
17.272 - import pwd
17.273 - try:
17.274 - pwent = pwd.getpwnam(path[1:i])
17.275 - except KeyError:
17.276 - return path
17.277 - userhome = pwent.pw_dir
17.278 - userhome = userhome.rstrip('/')
17.279 - return (userhome + path[i:]) or '/'
17.280 -
17.281 -
17.282 -# Expand paths containing shell variable substitutions.
17.283 -# This expands the forms $variable and ${variable} only.
17.284 -# Non-existent variables are left unchanged.
17.285 -
17.286 -_varprog = None
17.287 -
17.288 -def expandvars(path):
17.289 - """Expand shell variables of form $var and ${var}. Unknown variables
17.290 - are left unchanged."""
17.291 - global _varprog
17.292 - if '$' not in path:
17.293 - return path
17.294 - if not _varprog:
17.295 - import re
17.296 - _varprog = re.compile(r'\$(\w+|\{[^}]*\})')
17.297 - i = 0
17.298 - while True:
17.299 - m = _varprog.search(path, i)
17.300 - if not m:
17.301 - break
17.302 - i, j = m.span(0)
17.303 - name = m.group(1)
17.304 - if name.startswith('{') and name.endswith('}'):
17.305 - name = name[1:-1]
17.306 - if name in environ:
17.307 - tail = path[j:]
17.308 - path = path[:i] + environ[name]
17.309 - i = len(path)
17.310 - path += tail
17.311 - else:
17.312 - i = j
17.313 - return path
17.314 -
17.315 -
17.316 -# Normalize a path, e.g. A//B, A/./B and A/foo/../B all become A/B.
17.317 -# It should be understood that this may change the meaning of the path
17.318 -# if it contains symbolic links!
17.319 -
17.320 -def normpath(path):
17.321 - """Normalize path, eliminating double slashes, etc."""
17.322 - # Preserve unicode (if path is unicode)
17.323 - slash, dot = (u'/', u'.') if isinstance(path, _unicode) else ('/', '.')
17.324 - if path == '':
17.325 - return dot
17.326 - initial_slashes = path.startswith('/')
17.327 - # POSIX allows one or two initial slashes, but treats three or more
17.328 - # as single slash.
17.329 - if (initial_slashes and
17.330 - path.startswith('//') and not path.startswith('///')):
17.331 - initial_slashes = 2
17.332 - comps = path.split('/')
17.333 - new_comps = []
17.334 - for comp in comps:
17.335 - if comp in ('', '.'):
17.336 - continue
17.337 - if (comp != '..' or (not initial_slashes and not new_comps) or
17.338 - (new_comps and new_comps[-1] == '..')):
17.339 - new_comps.append(comp)
17.340 - elif new_comps:
17.341 - new_comps.pop()
17.342 - comps = new_comps
17.343 - path = slash.join(comps)
17.344 - if initial_slashes:
17.345 - path = slash*initial_slashes + path
17.346 - return path or dot
17.347 -
17.348 -
17.349 -def abspath(path):
17.350 - """Return an absolute path."""
17.351 - if not isabs(path):
17.352 - if isinstance(path, _unicode):
17.353 - cwd = getcwdu()
17.354 - else:
17.355 - cwd = getcwd()
17.356 - path = join(cwd, path)
17.357 - return normpath(path)
17.358 -
17.359 -
17.360 -# Return a canonical path (i.e. the absolute location of a file on the
17.361 -# filesystem).
17.362 -
17.363 -def realpath(filename):
17.364 - """Return the canonical path of the specified filename, eliminating any
17.365 -symbolic links encountered in the path."""
17.366 - if isabs(filename):
17.367 - bits = ['/'] + filename.split('/')[1:]
17.368 - else:
17.369 - bits = [''] + filename.split('/')
17.370 -
17.371 - for i in range(2, len(bits)+1):
17.372 - component = join(*bits[0:i])
17.373 - # Resolve symbolic links.
17.374 - if islink(component):
17.375 - resolved = _resolve_link(component)
17.376 - if resolved is None:
17.377 - # Infinite loop -- return original component + rest of the path
17.378 - return abspath(join(*([component] + bits[i:])))
17.379 - else:
17.380 - newpath = join(*([resolved] + bits[i:]))
17.381 - return realpath(newpath)
17.382 -
17.383 - return abspath(filename)
17.384 -
17.385 -
17.386 -def _resolve_link(path):
17.387 - """Internal helper function. Takes a path and follows symlinks
17.388 - until we either arrive at something that isn't a symlink, or
17.389 - encounter a path we've seen before (meaning that there's a loop).
17.390 - """
17.391 - paths_seen = set()
17.392 - while islink(path):
17.393 - if path in paths_seen:
17.394 - # Already seen this path, so we must have a symlink loop
17.395 - return None
17.396 - paths_seen.add(path)
17.397 - # Resolve where the link points to
17.398 - resolved = readlink(path)
17.399 - if not isabs(resolved):
17.400 - dir = dirname(path)
17.401 - path = normpath(join(dir, resolved))
17.402 - else:
17.403 - path = normpath(resolved)
17.404 - return path
17.405 -
17.406 -supports_unicode_filenames = (sys.platform == 'darwin')
17.407 -
17.408 -def relpath(path, start=curdir):
17.409 - """Return a relative version of a path"""
17.410 -
17.411 - if not path:
17.412 - raise ValueError("no path specified")
17.413 -
17.414 - start_list = [x for x in abspath(start).split(sep) if x]
17.415 - path_list = [x for x in abspath(path).split(sep) if x]
17.416 -
17.417 - # Work out how much of the filepath is shared by start and path.
17.418 - i = len(commonprefix([start_list, path_list]))
17.419 -
17.420 - rel_list = [pardir] * (len(start_list)-i) + path_list[i:]
17.421 - if not rel_list:
17.422 - return curdir
17.423 - return join(*rel_list)
18.1 --- a/lib/pwd.py Sat Dec 03 17:11:59 2016 +0100
18.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
18.3 @@ -1,22 +0,0 @@
18.4 -#!/usr/bin/env python
18.5 -
18.6 -class struct_passwd:
18.7 -
18.8 - def __init__(self, pw_dir, pw_gecos, pw_gid, pw_name, pw_passwd, pw_shell, pw_uid):
18.9 - self.pw_dir = pw_dir
18.10 - self.pw_gecos = pw_gecos
18.11 - self.pw_gid = pw_gid
18.12 - self.pw_name = pw_name
18.13 - self.pw_passwd = pw_passwd
18.14 - self.pw_shell = pw_shell
18.15 - self.pw_uid = pw_uid
18.16 -
18.17 - n_fields = 7
18.18 - n_sequence_fields = 7
18.19 - n_unnamed_fields = 0
18.20 -
18.21 -def getpwall(): pass
18.22 -def getpwnam(name): pass
18.23 -def getpwuid(uid): pass
18.24 -
18.25 -# vim: tabstop=4 expandtab shiftwidth=4
19.1 --- a/lib/pygame/__init__.py Sat Dec 03 17:11:59 2016 +0100
19.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
19.3 @@ -1,24 +0,0 @@
19.4 -#!/usr/bin/env python
19.5 -
19.6 -"""
19.7 -PyGame compatibility package.
19.8 -
19.9 -Copyright (C) 2011 Paul Boddie <paul@boddie.org.uk>
19.10 -
19.11 -This program is free software; you can redistribute it and/or modify it under
19.12 -the terms of the GNU General Public License as published by the Free Software
19.13 -Foundation; either version 3 of the License, or (at your option) any later
19.14 -version.
19.15 -
19.16 -This program is distributed in the hope that it will be useful, but WITHOUT
19.17 -ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19.18 -FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
19.19 -details.
19.20 -
19.21 -You should have received a copy of the GNU General Public License along with
19.22 -this program. If not, see <http://www.gnu.org/licenses/>.
19.23 -"""
19.24 -
19.25 -from pygame.constants import *
19.26 -
19.27 -# vim: tabstop=4 expandtab shiftwidth=4
21.1 --- a/lib/random.py Sat Dec 03 17:11:59 2016 +0100
21.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
21.3 @@ -1,11 +0,0 @@
21.4 -#!/usr/bin/env python
21.5 -
21.6 -from _random import Random
21.7 -
21.8 -def random():
21.9 - pass
21.10 -
21.11 -def randrange(start, stop=None, step=1):
21.12 - pass
21.13 -
21.14 -# vim: tabstop=4 expandtab shiftwidth=4
22.1 --- a/lib/re.py Sat Dec 03 17:11:59 2016 +0100
22.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
22.3 @@ -1,315 +0,0 @@
22.4 -#
22.5 -# Secret Labs' Regular Expression Engine
22.6 -#
22.7 -# re-compatible interface for the sre matching engine
22.8 -#
22.9 -# Copyright (c) 1998-2001 by Secret Labs AB. All rights reserved.
22.10 -#
22.11 -# This version of the SRE library can be redistributed under CNRI's
22.12 -# Python 1.6 license. For any other use, please contact Secret Labs
22.13 -# AB (info@pythonware.com).
22.14 -#
22.15 -# Portions of this engine have been developed in cooperation with
22.16 -# CNRI. Hewlett-Packard provided funding for 1.6 integration and
22.17 -# other compatibility work.
22.18 -#
22.19 -
22.20 -r"""Support for regular expressions (RE).
22.21 -
22.22 -This module provides regular expression matching operations similar to
22.23 -those found in Perl. It supports both 8-bit and Unicode strings; both
22.24 -the pattern and the strings being processed can contain null bytes and
22.25 -characters outside the US ASCII range.
22.26 -
22.27 -Regular expressions can contain both special and ordinary characters.
22.28 -Most ordinary characters, like "A", "a", or "0", are the simplest
22.29 -regular expressions; they simply match themselves. You can
22.30 -concatenate ordinary characters, so last matches the string 'last'.
22.31 -
22.32 -The special characters are:
22.33 - "." Matches any character except a newline.
22.34 - "^" Matches the start of the string.
22.35 - "$" Matches the end of the string or just before the newline at
22.36 - the end of the string.
22.37 - "*" Matches 0 or more (greedy) repetitions of the preceding RE.
22.38 - Greedy means that it will match as many repetitions as possible.
22.39 - "+" Matches 1 or more (greedy) repetitions of the preceding RE.
22.40 - "?" Matches 0 or 1 (greedy) of the preceding RE.
22.41 - *?,+?,?? Non-greedy versions of the previous three special characters.
22.42 - {m,n} Matches from m to n repetitions of the preceding RE.
22.43 - {m,n}? Non-greedy version of the above.
22.44 - "\\" Either escapes special characters or signals a special sequence.
22.45 - [] Indicates a set of characters.
22.46 - A "^" as the first character indicates a complementing set.
22.47 - "|" A|B, creates an RE that will match either A or B.
22.48 - (...) Matches the RE inside the parentheses.
22.49 - The contents can be retrieved or matched later in the string.
22.50 - (?iLmsux) Set the I, L, M, S, U, or X flag for the RE (see below).
22.51 - (?:...) Non-grouping version of regular parentheses.
22.52 - (?P<name>...) The substring matched by the group is accessible by name.
22.53 - (?P=name) Matches the text matched earlier by the group named name.
22.54 - (?#...) A comment; ignored.
22.55 - (?=...) Matches if ... matches next, but doesn't consume the string.
22.56 - (?!...) Matches if ... doesn't match next.
22.57 - (?<=...) Matches if preceded by ... (must be fixed length).
22.58 - (?<!...) Matches if not preceded by ... (must be fixed length).
22.59 - (?(id/name)yes|no) Matches yes pattern if the group with id/name matched,
22.60 - the (optional) no pattern otherwise.
22.61 -
22.62 -The special sequences consist of "\\" and a character from the list
22.63 -below. If the ordinary character is not on the list, then the
22.64 -resulting RE will match the second character.
22.65 - \number Matches the contents of the group of the same number.
22.66 - \A Matches only at the start of the string.
22.67 - \Z Matches only at the end of the string.
22.68 - \b Matches the empty string, but only at the start or end of a word.
22.69 - \B Matches the empty string, but not at the start or end of a word.
22.70 - \d Matches any decimal digit; equivalent to the set [0-9].
22.71 - \D Matches any non-digit character; equivalent to the set [^0-9].
22.72 - \s Matches any whitespace character; equivalent to [ \t\n\r\f\v].
22.73 - \S Matches any non-whitespace character; equiv. to [^ \t\n\r\f\v].
22.74 - \w Matches any alphanumeric character; equivalent to [a-zA-Z0-9_].
22.75 - With LOCALE, it will match the set [0-9_] plus characters defined
22.76 - as letters for the current locale.
22.77 - \W Matches the complement of \w.
22.78 - \\ Matches a literal backslash.
22.79 -
22.80 -This module exports the following functions:
22.81 - match Match a regular expression pattern to the beginning of a string.
22.82 - search Search a string for the presence of a pattern.
22.83 - sub Substitute occurrences of a pattern found in a string.
22.84 - subn Same as sub, but also return the number of substitutions made.
22.85 - split Split a string by the occurrences of a pattern.
22.86 - findall Find all occurrences of a pattern in a string.
22.87 - finditer Return an iterator yielding a match object for each match.
22.88 - compile Compile a pattern into a RegexObject.
22.89 - purge Clear the regular expression cache.
22.90 - escape Backslash all non-alphanumerics in a string.
22.91 -
22.92 -Some of the functions in this module takes flags as optional parameters:
22.93 - I IGNORECASE Perform case-insensitive matching.
22.94 - L LOCALE Make \w, \W, \b, \B, dependent on the current locale.
22.95 - M MULTILINE "^" matches the beginning of lines (after a newline)
22.96 - as well as the string.
22.97 - "$" matches the end of lines (before a newline) as well
22.98 - as the end of the string.
22.99 - S DOTALL "." matches any character at all, including the newline.
22.100 - X VERBOSE Ignore whitespace and comments for nicer looking RE's.
22.101 - U UNICODE Make \w, \W, \b, \B, dependent on the Unicode locale.
22.102 -
22.103 -This module also defines an exception 'error'.
22.104 -
22.105 -"""
22.106 -
22.107 -from sre_constants import BRANCH, SUBPATTERN
22.108 -import sys
22.109 -import sre_compile
22.110 -import sre_parse
22.111 -
22.112 -# public symbols
22.113 -__all__ = [ "match", "search", "sub", "subn", "split", "findall",
22.114 - "compile", "purge", "template", "escape", "I", "L", "M", "S", "X",
22.115 - "U", "IGNORECASE", "LOCALE", "MULTILINE", "DOTALL", "VERBOSE",
22.116 - "UNICODE", "error" ]
22.117 -
22.118 -__version__ = "2.2.1"
22.119 -
22.120 -# flags
22.121 -I = IGNORECASE = sre_compile.SRE_FLAG_IGNORECASE # ignore case
22.122 -L = LOCALE = sre_compile.SRE_FLAG_LOCALE # assume current 8-bit locale
22.123 -U = UNICODE = sre_compile.SRE_FLAG_UNICODE # assume unicode locale
22.124 -M = MULTILINE = sre_compile.SRE_FLAG_MULTILINE # make anchors look for newline
22.125 -S = DOTALL = sre_compile.SRE_FLAG_DOTALL # make dot match newline
22.126 -X = VERBOSE = sre_compile.SRE_FLAG_VERBOSE # ignore whitespace and comments
22.127 -
22.128 -# sre extensions (experimental, don't rely on these)
22.129 -T = TEMPLATE = sre_compile.SRE_FLAG_TEMPLATE # disable backtracking
22.130 -DEBUG = sre_compile.SRE_FLAG_DEBUG # dump pattern after compilation
22.131 -
22.132 -# sre exception
22.133 -error = sre_compile.error
22.134 -
22.135 -# --------------------------------------------------------------------
22.136 -# public interface
22.137 -
22.138 -def match(pattern, string, flags=0):
22.139 - """Try to apply the pattern at the start of the string, returning
22.140 - a match object, or None if no match was found."""
22.141 - return _compile(pattern, flags).match(string)
22.142 -
22.143 -def search(pattern, string, flags=0):
22.144 - """Scan through string looking for a match to the pattern, returning
22.145 - a match object, or None if no match was found."""
22.146 - return _compile(pattern, flags).search(string)
22.147 -
22.148 -def sub(pattern, repl, string, count=0, flags=0):
22.149 - """Return the string obtained by replacing the leftmost
22.150 - non-overlapping occurrences of the pattern in string by the
22.151 - replacement repl. repl can be either a string or a callable;
22.152 - if a string, backslash escapes in it are processed. If it is
22.153 - a callable, it's passed the match object and must return
22.154 - a replacement string to be used."""
22.155 - return _compile(pattern, flags).sub(repl, string, count)
22.156 -
22.157 -def subn(pattern, repl, string, count=0, flags=0):
22.158 - """Return a 2-tuple containing (new_string, number).
22.159 - new_string is the string obtained by replacing the leftmost
22.160 - non-overlapping occurrences of the pattern in the source
22.161 - string by the replacement repl. number is the number of
22.162 - substitutions that were made. repl can be either a string or a
22.163 - callable; if a string, backslash escapes in it are processed.
22.164 - If it is a callable, it's passed the match object and must
22.165 - return a replacement string to be used."""
22.166 - return _compile(pattern, flags).subn(repl, string, count)
22.167 -
22.168 -def split(pattern, string, maxsplit=0, flags=0):
22.169 - """Split the source string by the occurrences of the pattern,
22.170 - returning a list containing the resulting substrings."""
22.171 - return _compile(pattern, flags).split(string, maxsplit)
22.172 -
22.173 -def findall(pattern, string, flags=0):
22.174 - """Return a list of all non-overlapping matches in the string.
22.175 -
22.176 - If one or more groups are present in the pattern, return a
22.177 - list of groups; this will be a list of tuples if the pattern
22.178 - has more than one group.
22.179 -
22.180 - Empty matches are included in the result."""
22.181 - return _compile(pattern, flags).findall(string)
22.182 -
22.183 -if sys.hexversion >= 0x02020000:
22.184 - __all__.append("finditer")
22.185 - def finditer(pattern, string, flags=0):
22.186 - """Return an iterator over all non-overlapping matches in the
22.187 - string. For each match, the iterator returns a match object.
22.188 -
22.189 - Empty matches are included in the result."""
22.190 - return _compile(pattern, flags).finditer(string)
22.191 -
22.192 -def compile(pattern, flags=0):
22.193 - "Compile a regular expression pattern, returning a pattern object."
22.194 - return _compile(pattern, flags)
22.195 -
22.196 -def purge():
22.197 - "Clear the regular expression cache"
22.198 - _cache.clear()
22.199 - _cache_repl.clear()
22.200 -
22.201 -def template(pattern, flags=0):
22.202 - "Compile a template pattern, returning a pattern object"
22.203 - return _compile(pattern, flags|T)
22.204 -
22.205 -_alphanum = frozenset(
22.206 - "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
22.207 -
22.208 -def escape(pattern):
22.209 - "Escape all non-alphanumeric characters in pattern."
22.210 - s = list(pattern)
22.211 - alphanum = _alphanum
22.212 - for i, c in enumerate(pattern):
22.213 - if c not in alphanum:
22.214 - if c == "\000":
22.215 - s[i] = "\\000"
22.216 - else:
22.217 - s[i] = "\\" + c
22.218 - return pattern[:0].join(s)
22.219 -
22.220 -# --------------------------------------------------------------------
22.221 -# internals
22.222 -
22.223 -_cache = {}
22.224 -_cache_repl = {}
22.225 -
22.226 -_pattern_type = type(sre_compile.compile("", 0))
22.227 -
22.228 -_MAXCACHE = 100
22.229 -
22.230 -def _compile(*key):
22.231 - # internal: compile pattern
22.232 - cachekey = (type(key[0]),) + key
22.233 - p = _cache.get(cachekey)
22.234 - if p is not None:
22.235 - return p
22.236 - pattern, flags = key
22.237 - if isinstance(pattern, _pattern_type):
22.238 - if flags:
22.239 - raise ValueError('Cannot process flags argument with a compiled pattern')
22.240 - return pattern
22.241 - if not sre_compile.isstring(pattern):
22.242 - raise TypeError, "first argument must be string or compiled pattern"
22.243 - try:
22.244 - p = sre_compile.compile(pattern, flags)
22.245 - except error, v:
22.246 - raise error, v # invalid expression
22.247 - if len(_cache) >= _MAXCACHE:
22.248 - _cache.clear()
22.249 - _cache[cachekey] = p
22.250 - return p
22.251 -
22.252 -def _compile_repl(*key):
22.253 - # internal: compile replacement pattern
22.254 - p = _cache_repl.get(key)
22.255 - if p is not None:
22.256 - return p
22.257 - repl, pattern = key
22.258 - try:
22.259 - p = sre_parse.parse_template(repl, pattern)
22.260 - except error, v:
22.261 - raise error, v # invalid expression
22.262 - if len(_cache_repl) >= _MAXCACHE:
22.263 - _cache_repl.clear()
22.264 - _cache_repl[key] = p
22.265 - return p
22.266 -
22.267 -def _expand(pattern, match, template):
22.268 - # internal: match.expand implementation hook
22.269 - template = sre_parse.parse_template(template, pattern)
22.270 - return sre_parse.expand_template(template, match)
22.271 -
22.272 -def _subx(pattern, template):
22.273 - # internal: pattern.sub/subn implementation helper
22.274 - template = _compile_repl(template, pattern)
22.275 - if not template[0] and len(template[1]) == 1:
22.276 - # literal replacement
22.277 - return template[1][0]
22.278 - def filter(match, template=template):
22.279 - return sre_parse.expand_template(template, match)
22.280 - return filter
22.281 -
22.282 -# --------------------------------------------------------------------
22.283 -# experimental stuff (see python-dev discussions for details)
22.284 -
22.285 -class Scanner:
22.286 - def __init__(self, lexicon, flags=0):
22.287 - self.lexicon = lexicon
22.288 - # combine phrases into a compound pattern
22.289 - p = []
22.290 - s = sre_parse.Pattern()
22.291 - s.flags = flags
22.292 - for phrase, action in lexicon:
22.293 - p.append(sre_parse.SubPattern(s, [
22.294 - (SUBPATTERN, (len(p)+1, sre_parse.parse(phrase, flags))),
22.295 - ]))
22.296 - s.groups = len(p)+1
22.297 - p = sre_parse.SubPattern(s, [(BRANCH, (None, p))])
22.298 - self.scanner = sre_compile.compile(p)
22.299 - def scan(self, string):
22.300 - result = []
22.301 - append = result.append
22.302 - match = self.scanner.scanner(string).match
22.303 - i = 0
22.304 - while 1:
22.305 - m = match()
22.306 - if not m:
22.307 - break
22.308 - j = m.end()
22.309 - if i == j:
22.310 - break
22.311 - action = self.lexicon[m.lastindex-1][1]
22.312 - if hasattr(action, '__call__'):
22.313 - self.match = m
22.314 - action = action(self, m.group())
22.315 - if action is not None:
22.316 - append(action)
22.317 - i = j
22.318 - return result, string[i:]
23.1 --- a/lib/sre_constants.py Sat Dec 03 17:11:59 2016 +0100
23.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
23.3 @@ -1,216 +0,0 @@
23.4 -#
23.5 -# Secret Labs' Regular Expression Engine
23.6 -#
23.7 -# various symbols used by the regular expression engine.
23.8 -#
23.9 -# Copyright (c) 1998-2001 by Secret Labs AB. All rights reserved.
23.10 -#
23.11 -# See the sre.py file for information on usage and redistribution.
23.12 -#
23.13 -
23.14 -"""Internal support module for sre"""
23.15 -
23.16 -# update when constants are added or removed
23.17 -
23.18 -MAGIC = 20031017
23.19 -
23.20 -# max code word in this release
23.21 -
23.22 -MAXREPEAT = 65535
23.23 -
23.24 -# SRE standard exception (access as sre.error)
23.25 -# should this really be here?
23.26 -
23.27 -class error(Exception):
23.28 - pass
23.29 -
23.30 -# operators
23.31 -
23.32 -FAILURE = "failure"
23.33 -SUCCESS = "success"
23.34 -
23.35 -ANY = "any"
23.36 -ANY_ALL = "any_all"
23.37 -ASSERT = "assert"
23.38 -ASSERT_NOT = "assert_not"
23.39 -AT = "at"
23.40 -BIGCHARSET = "bigcharset"
23.41 -BRANCH = "branch"
23.42 -CALL = "call"
23.43 -CATEGORY = "category"
23.44 -CHARSET = "charset"
23.45 -GROUPREF = "groupref"
23.46 -GROUPREF_IGNORE = "groupref_ignore"
23.47 -GROUPREF_EXISTS = "groupref_exists"
23.48 -IN = "in"
23.49 -IN_IGNORE = "in_ignore"
23.50 -INFO = "info"
23.51 -JUMP = "jump"
23.52 -LITERAL = "literal"
23.53 -LITERAL_IGNORE = "literal_ignore"
23.54 -MARK = "mark"
23.55 -MAX_REPEAT = "max_repeat"
23.56 -MAX_UNTIL = "max_until"
23.57 -MIN_REPEAT = "min_repeat"
23.58 -MIN_UNTIL = "min_until"
23.59 -NEGATE = "negate"
23.60 -NOT_LITERAL = "not_literal"
23.61 -NOT_LITERAL_IGNORE = "not_literal_ignore"
23.62 -RANGE = "range"
23.63 -REPEAT = "repeat"
23.64 -REPEAT_ONE = "repeat_one"
23.65 -SUBPATTERN = "subpattern"
23.66 -MIN_REPEAT_ONE = "min_repeat_one"
23.67 -
23.68 -# positions
23.69 -AT_BEGINNING = "at_beginning"
23.70 -AT_BEGINNING_LINE = "at_beginning_line"
23.71 -AT_BEGINNING_STRING = "at_beginning_string"
23.72 -AT_BOUNDARY = "at_boundary"
23.73 -AT_NON_BOUNDARY = "at_non_boundary"
23.74 -AT_END = "at_end"
23.75 -AT_END_LINE = "at_end_line"
23.76 -AT_END_STRING = "at_end_string"
23.77 -AT_LOC_BOUNDARY = "at_loc_boundary"
23.78 -AT_LOC_NON_BOUNDARY = "at_loc_non_boundary"
23.79 -AT_UNI_BOUNDARY = "at_uni_boundary"
23.80 -AT_UNI_NON_BOUNDARY = "at_uni_non_boundary"
23.81 -
23.82 -# categories
23.83 -CATEGORY_DIGIT = "category_digit"
23.84 -CATEGORY_NOT_DIGIT = "category_not_digit"
23.85 -CATEGORY_SPACE = "category_space"
23.86 -CATEGORY_NOT_SPACE = "category_not_space"
23.87 -CATEGORY_WORD = "category_word"
23.88 -CATEGORY_NOT_WORD = "category_not_word"
23.89 -CATEGORY_LINEBREAK = "category_linebreak"
23.90 -CATEGORY_NOT_LINEBREAK = "category_not_linebreak"
23.91 -CATEGORY_LOC_WORD = "category_loc_word"
23.92 -CATEGORY_LOC_NOT_WORD = "category_loc_not_word"
23.93 -CATEGORY_UNI_DIGIT = "category_uni_digit"
23.94 -CATEGORY_UNI_NOT_DIGIT = "category_uni_not_digit"
23.95 -CATEGORY_UNI_SPACE = "category_uni_space"
23.96 -CATEGORY_UNI_NOT_SPACE = "category_uni_not_space"
23.97 -CATEGORY_UNI_WORD = "category_uni_word"
23.98 -CATEGORY_UNI_NOT_WORD = "category_uni_not_word"
23.99 -CATEGORY_UNI_LINEBREAK = "category_uni_linebreak"
23.100 -CATEGORY_UNI_NOT_LINEBREAK = "category_uni_not_linebreak"
23.101 -
23.102 -OPCODES = [
23.103 -
23.104 - # failure=0 success=1 (just because it looks better that way :-)
23.105 - FAILURE, SUCCESS,
23.106 -
23.107 - ANY, ANY_ALL,
23.108 - ASSERT, ASSERT_NOT,
23.109 - AT,
23.110 - BRANCH,
23.111 - CALL,
23.112 - CATEGORY,
23.113 - CHARSET, BIGCHARSET,
23.114 - GROUPREF, GROUPREF_EXISTS, GROUPREF_IGNORE,
23.115 - IN, IN_IGNORE,
23.116 - INFO,
23.117 - JUMP,
23.118 - LITERAL, LITERAL_IGNORE,
23.119 - MARK,
23.120 - MAX_UNTIL,
23.121 - MIN_UNTIL,
23.122 - NOT_LITERAL, NOT_LITERAL_IGNORE,
23.123 - NEGATE,
23.124 - RANGE,
23.125 - REPEAT,
23.126 - REPEAT_ONE,
23.127 - SUBPATTERN,
23.128 - MIN_REPEAT_ONE
23.129 -
23.130 -]
23.131 -
23.132 -ATCODES = [
23.133 - AT_BEGINNING, AT_BEGINNING_LINE, AT_BEGINNING_STRING, AT_BOUNDARY,
23.134 - AT_NON_BOUNDARY, AT_END, AT_END_LINE, AT_END_STRING,
23.135 - AT_LOC_BOUNDARY, AT_LOC_NON_BOUNDARY, AT_UNI_BOUNDARY,
23.136 - AT_UNI_NON_BOUNDARY
23.137 -]
23.138 -
23.139 -CHCODES = [
23.140 - CATEGORY_DIGIT, CATEGORY_NOT_DIGIT, CATEGORY_SPACE,
23.141 - CATEGORY_NOT_SPACE, CATEGORY_WORD, CATEGORY_NOT_WORD,
23.142 - CATEGORY_LINEBREAK, CATEGORY_NOT_LINEBREAK, CATEGORY_LOC_WORD,
23.143 - CATEGORY_LOC_NOT_WORD, CATEGORY_UNI_DIGIT, CATEGORY_UNI_NOT_DIGIT,
23.144 - CATEGORY_UNI_SPACE, CATEGORY_UNI_NOT_SPACE, CATEGORY_UNI_WORD,
23.145 - CATEGORY_UNI_NOT_WORD, CATEGORY_UNI_LINEBREAK,
23.146 - CATEGORY_UNI_NOT_LINEBREAK
23.147 -]
23.148 -
23.149 -def makedict(list):
23.150 - d = {}
23.151 - i = 0
23.152 - for item in list:
23.153 - d[item] = i
23.154 - i = i + 1
23.155 - return d
23.156 -
23.157 -OPCODES = makedict(OPCODES)
23.158 -ATCODES = makedict(ATCODES)
23.159 -CHCODES = makedict(CHCODES)
23.160 -
23.161 -# replacement operations for "ignore case" mode
23.162 -OP_IGNORE = {
23.163 - GROUPREF: GROUPREF_IGNORE,
23.164 - IN: IN_IGNORE,
23.165 - LITERAL: LITERAL_IGNORE,
23.166 - NOT_LITERAL: NOT_LITERAL_IGNORE
23.167 -}
23.168 -
23.169 -AT_MULTILINE = {
23.170 - AT_BEGINNING: AT_BEGINNING_LINE,
23.171 - AT_END: AT_END_LINE
23.172 -}
23.173 -
23.174 -AT_LOCALE = {
23.175 - AT_BOUNDARY: AT_LOC_BOUNDARY,
23.176 - AT_NON_BOUNDARY: AT_LOC_NON_BOUNDARY
23.177 -}
23.178 -
23.179 -AT_UNICODE = {
23.180 - AT_BOUNDARY: AT_UNI_BOUNDARY,
23.181 - AT_NON_BOUNDARY: AT_UNI_NON_BOUNDARY
23.182 -}
23.183 -
23.184 -CH_LOCALE = {
23.185 - CATEGORY_DIGIT: CATEGORY_DIGIT,
23.186 - CATEGORY_NOT_DIGIT: CATEGORY_NOT_DIGIT,
23.187 - CATEGORY_SPACE: CATEGORY_SPACE,
23.188 - CATEGORY_NOT_SPACE: CATEGORY_NOT_SPACE,
23.189 - CATEGORY_WORD: CATEGORY_LOC_WORD,
23.190 - CATEGORY_NOT_WORD: CATEGORY_LOC_NOT_WORD,
23.191 - CATEGORY_LINEBREAK: CATEGORY_LINEBREAK,
23.192 - CATEGORY_NOT_LINEBREAK: CATEGORY_NOT_LINEBREAK
23.193 -}
23.194 -
23.195 -CH_UNICODE = {
23.196 - CATEGORY_DIGIT: CATEGORY_UNI_DIGIT,
23.197 - CATEGORY_NOT_DIGIT: CATEGORY_UNI_NOT_DIGIT,
23.198 - CATEGORY_SPACE: CATEGORY_UNI_SPACE,
23.199 - CATEGORY_NOT_SPACE: CATEGORY_UNI_NOT_SPACE,
23.200 - CATEGORY_WORD: CATEGORY_UNI_WORD,
23.201 - CATEGORY_NOT_WORD: CATEGORY_UNI_NOT_WORD,
23.202 - CATEGORY_LINEBREAK: CATEGORY_UNI_LINEBREAK,
23.203 - CATEGORY_NOT_LINEBREAK: CATEGORY_UNI_NOT_LINEBREAK
23.204 -}
23.205 -
23.206 -# flags
23.207 -SRE_FLAG_TEMPLATE = 1 # template mode (disable backtracking)
23.208 -SRE_FLAG_IGNORECASE = 2 # case insensitive
23.209 -SRE_FLAG_LOCALE = 4 # honour system locale
23.210 -SRE_FLAG_MULTILINE = 8 # treat target as multiline string
23.211 -SRE_FLAG_DOTALL = 16 # treat target as a single string
23.212 -SRE_FLAG_UNICODE = 32 # use unicode locale
23.213 -SRE_FLAG_VERBOSE = 64 # ignore whitespace and comments
23.214 -SRE_FLAG_DEBUG = 128 # debugging
23.215 -
23.216 -# flags for INFO primitive
23.217 -SRE_INFO_PREFIX = 1 # has prefix
23.218 -SRE_INFO_LITERAL = 2 # entire pattern is literal (given by prefix)
23.219 -SRE_INFO_CHARSET = 4 # pattern starts with character from given set
24.1 --- a/lib/sre_parse.py Sat Dec 03 17:11:59 2016 +0100
24.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
24.3 @@ -1,791 +0,0 @@
24.4 -#
24.5 -# Secret Labs' Regular Expression Engine
24.6 -#
24.7 -# convert re-style regular expression to sre pattern
24.8 -#
24.9 -# Copyright (c) 1998-2001 by Secret Labs AB. All rights reserved.
24.10 -#
24.11 -# See the sre.py file for information on usage and redistribution.
24.12 -#
24.13 -
24.14 -"""Internal support module for sre"""
24.15 -
24.16 -# XXX: show string offset and offending character for all errors
24.17 -
24.18 -import sys
24.19 -
24.20 -from sre_constants import *
24.21 -
24.22 -SPECIAL_CHARS = ".\\[{()*+?^$|"
24.23 -REPEAT_CHARS = "*+?{"
24.24 -
24.25 -DIGITS = set("0123456789")
24.26 -
24.27 -OCTDIGITS = set("01234567")
24.28 -HEXDIGITS = set("0123456789abcdefABCDEF")
24.29 -
24.30 -WHITESPACE = set(" \t\n\r\v\f")
24.31 -
24.32 -ESCAPES = {
24.33 - r"\a": (LITERAL, ord("\a")),
24.34 - r"\b": (LITERAL, ord("\b")),
24.35 - r"\f": (LITERAL, ord("\f")),
24.36 - r"\n": (LITERAL, ord("\n")),
24.37 - r"\r": (LITERAL, ord("\r")),
24.38 - r"\t": (LITERAL, ord("\t")),
24.39 - r"\v": (LITERAL, ord("\v")),
24.40 - r"\\": (LITERAL, ord("\\"))
24.41 -}
24.42 -
24.43 -CATEGORIES = {
24.44 - r"\A": (AT, AT_BEGINNING_STRING), # start of string
24.45 - r"\b": (AT, AT_BOUNDARY),
24.46 - r"\B": (AT, AT_NON_BOUNDARY),
24.47 - r"\d": (IN, [(CATEGORY, CATEGORY_DIGIT)]),
24.48 - r"\D": (IN, [(CATEGORY, CATEGORY_NOT_DIGIT)]),
24.49 - r"\s": (IN, [(CATEGORY, CATEGORY_SPACE)]),
24.50 - r"\S": (IN, [(CATEGORY, CATEGORY_NOT_SPACE)]),
24.51 - r"\w": (IN, [(CATEGORY, CATEGORY_WORD)]),
24.52 - r"\W": (IN, [(CATEGORY, CATEGORY_NOT_WORD)]),
24.53 - r"\Z": (AT, AT_END_STRING), # end of string
24.54 -}
24.55 -
24.56 -FLAGS = {
24.57 - # standard flags
24.58 - "i": SRE_FLAG_IGNORECASE,
24.59 - "L": SRE_FLAG_LOCALE,
24.60 - "m": SRE_FLAG_MULTILINE,
24.61 - "s": SRE_FLAG_DOTALL,
24.62 - "x": SRE_FLAG_VERBOSE,
24.63 - # extensions
24.64 - "t": SRE_FLAG_TEMPLATE,
24.65 - "u": SRE_FLAG_UNICODE,
24.66 -}
24.67 -
24.68 -class Pattern:
24.69 - # master pattern object. keeps track of global attributes
24.70 - def __init__(self):
24.71 - self.flags = 0
24.72 - self.open = []
24.73 - self.groups = 1
24.74 - self.groupdict = {}
24.75 - self.str = None
24.76 - def opengroup(self, name=None):
24.77 - gid = self.groups
24.78 - self.groups = gid + 1
24.79 - if name is not None:
24.80 - ogid = self.groupdict.get(name, None)
24.81 - if ogid is not None:
24.82 - raise error, ("redefinition of group name %s as group %d; "
24.83 - "was group %d" % (repr(name), gid, ogid))
24.84 - self.groupdict[name] = gid
24.85 - self.open.append(gid)
24.86 - return gid
24.87 - def closegroup(self, gid):
24.88 - self.open.remove(gid)
24.89 - def checkgroup(self, gid):
24.90 - return gid < self.groups and gid not in self.open
24.91 -
24.92 -class SubPattern:
24.93 - # a subpattern, in intermediate form
24.94 - def __init__(self, pattern, data=None):
24.95 - self.pattern = pattern
24.96 - if data is None:
24.97 - data = []
24.98 - self.data = data
24.99 - self.width = None
24.100 - def dump(self, level=0):
24.101 - nl = 1
24.102 - seqtypes = type(()), type([])
24.103 - for op, av in self.data:
24.104 - print level*" " + op,; nl = 0
24.105 - if op == "in":
24.106 - # member sublanguage
24.107 - print; nl = 1
24.108 - for op, a in av:
24.109 - print (level+1)*" " + op, a
24.110 - elif op == "branch":
24.111 - print; nl = 1
24.112 - i = 0
24.113 - for a in av[1]:
24.114 - if i > 0:
24.115 - print level*" " + "or"
24.116 - a.dump(level+1); nl = 1
24.117 - i = i + 1
24.118 - elif type(av) in seqtypes:
24.119 - for a in av:
24.120 - if isinstance(a, SubPattern):
24.121 - if not nl: print
24.122 - a.dump(level+1); nl = 1
24.123 - else:
24.124 - print a, ; nl = 0
24.125 - else:
24.126 - print av, ; nl = 0
24.127 - if not nl: print
24.128 - def __repr__(self):
24.129 - return repr(self.data)
24.130 - def __len__(self):
24.131 - return len(self.data)
24.132 - def __delitem__(self, index):
24.133 - del self.data[index]
24.134 - def __getitem__(self, index):
24.135 - if isinstance(index, slice):
24.136 - return SubPattern(self.pattern, self.data[index])
24.137 - return self.data[index]
24.138 - def __setitem__(self, index, code):
24.139 - self.data[index] = code
24.140 - def insert(self, index, code):
24.141 - self.data.insert(index, code)
24.142 - def append(self, code):
24.143 - self.data.append(code)
24.144 - def getwidth(self):
24.145 - # determine the width (min, max) for this subpattern
24.146 - if self.width:
24.147 - return self.width
24.148 - lo = hi = 0L
24.149 - UNITCODES = (ANY, RANGE, IN, LITERAL, NOT_LITERAL, CATEGORY)
24.150 - REPEATCODES = (MIN_REPEAT, MAX_REPEAT)
24.151 - for op, av in self.data:
24.152 - if op is BRANCH:
24.153 - i = sys.maxint
24.154 - j = 0
24.155 - for av in av[1]:
24.156 - l, h = av.getwidth()
24.157 - i = min(i, l)
24.158 - j = max(j, h)
24.159 - lo = lo + i
24.160 - hi = hi + j
24.161 - elif op is CALL:
24.162 - i, j = av.getwidth()
24.163 - lo = lo + i
24.164 - hi = hi + j
24.165 - elif op is SUBPATTERN:
24.166 - i, j = av[1].getwidth()
24.167 - lo = lo + i
24.168 - hi = hi + j
24.169 - elif op in REPEATCODES:
24.170 - i, j = av[2].getwidth()
24.171 - lo = lo + long(i) * av[0]
24.172 - hi = hi + long(j) * av[1]
24.173 - elif op in UNITCODES:
24.174 - lo = lo + 1
24.175 - hi = hi + 1
24.176 - elif op == SUCCESS:
24.177 - break
24.178 - self.width = int(min(lo, sys.maxint)), int(min(hi, sys.maxint))
24.179 - return self.width
24.180 -
24.181 -class Tokenizer:
24.182 - def __init__(self, string):
24.183 - self.string = string
24.184 - self.index = 0
24.185 - self.__next()
24.186 - def __next(self):
24.187 - if self.index >= len(self.string):
24.188 - self.next = None
24.189 - return
24.190 - char = self.string[self.index]
24.191 - if char[0] == "\\":
24.192 - try:
24.193 - c = self.string[self.index + 1]
24.194 - except IndexError:
24.195 - raise error, "bogus escape (end of line)"
24.196 - char = char + c
24.197 - self.index = self.index + len(char)
24.198 - self.next = char
24.199 - def match(self, char, skip=1):
24.200 - if char == self.next:
24.201 - if skip:
24.202 - self.__next()
24.203 - return 1
24.204 - return 0
24.205 - def get(self):
24.206 - this = self.next
24.207 - self.__next()
24.208 - return this
24.209 - def tell(self):
24.210 - return self.index, self.next
24.211 - def seek(self, index):
24.212 - self.index, self.next = index
24.213 -
24.214 -def isident(char):
24.215 - return "a" <= char <= "z" or "A" <= char <= "Z" or char == "_"
24.216 -
24.217 -def isdigit(char):
24.218 - return "0" <= char <= "9"
24.219 -
24.220 -def isname(name):
24.221 - # check that group name is a valid string
24.222 - if not isident(name[0]):
24.223 - return False
24.224 - for char in name[1:]:
24.225 - if not isident(char) and not isdigit(char):
24.226 - return False
24.227 - return True
24.228 -
24.229 -def _class_escape(source, escape):
24.230 - # handle escape code inside character class
24.231 - code = ESCAPES.get(escape)
24.232 - if code:
24.233 - return code
24.234 - code = CATEGORIES.get(escape)
24.235 - if code:
24.236 - return code
24.237 - try:
24.238 - c = escape[1:2]
24.239 - if c == "x":
24.240 - # hexadecimal escape (exactly two digits)
24.241 - while source.next in HEXDIGITS and len(escape) < 4:
24.242 - escape = escape + source.get()
24.243 - escape = escape[2:]
24.244 - if len(escape) != 2:
24.245 - raise error, "bogus escape: %s" % repr("\\" + escape)
24.246 - return LITERAL, int(escape, 16) & 0xff
24.247 - elif c in OCTDIGITS:
24.248 - # octal escape (up to three digits)
24.249 - while source.next in OCTDIGITS and len(escape) < 4:
24.250 - escape = escape + source.get()
24.251 - escape = escape[1:]
24.252 - return LITERAL, int(escape, 8) & 0xff
24.253 - elif c in DIGITS:
24.254 - raise error, "bogus escape: %s" % repr(escape)
24.255 - if len(escape) == 2:
24.256 - return LITERAL, ord(escape[1])
24.257 - except ValueError:
24.258 - pass
24.259 - raise error, "bogus escape: %s" % repr(escape)
24.260 -
24.261 -def _escape(source, escape, state):
24.262 - # handle escape code in expression
24.263 - code = CATEGORIES.get(escape)
24.264 - if code:
24.265 - return code
24.266 - code = ESCAPES.get(escape)
24.267 - if code:
24.268 - return code
24.269 - try:
24.270 - c = escape[1:2]
24.271 - if c == "x":
24.272 - # hexadecimal escape
24.273 - while source.next in HEXDIGITS and len(escape) < 4:
24.274 - escape = escape + source.get()
24.275 - if len(escape) != 4:
24.276 - raise ValueError
24.277 - return LITERAL, int(escape[2:], 16) & 0xff
24.278 - elif c == "0":
24.279 - # octal escape
24.280 - while source.next in OCTDIGITS and len(escape) < 4:
24.281 - escape = escape + source.get()
24.282 - return LITERAL, int(escape[1:], 8) & 0xff
24.283 - elif c in DIGITS:
24.284 - # octal escape *or* decimal group reference (sigh)
24.285 - if source.next in DIGITS:
24.286 - escape = escape + source.get()
24.287 - if (escape[1] in OCTDIGITS and escape[2] in OCTDIGITS and
24.288 - source.next in OCTDIGITS):
24.289 - # got three octal digits; this is an octal escape
24.290 - escape = escape + source.get()
24.291 - return LITERAL, int(escape[1:], 8) & 0xff
24.292 - # not an octal escape, so this is a group reference
24.293 - group = int(escape[1:])
24.294 - if group < state.groups:
24.295 - if not state.checkgroup(group):
24.296 - raise error, "cannot refer to open group"
24.297 - return GROUPREF, group
24.298 - raise ValueError
24.299 - if len(escape) == 2:
24.300 - return LITERAL, ord(escape[1])
24.301 - except ValueError:
24.302 - pass
24.303 - raise error, "bogus escape: %s" % repr(escape)
24.304 -
24.305 -def _parse_sub(source, state, nested=1):
24.306 - # parse an alternation: a|b|c
24.307 -
24.308 - items = []
24.309 - itemsappend = items.append
24.310 - sourcematch = source.match
24.311 - while 1:
24.312 - itemsappend(_parse(source, state))
24.313 - if sourcematch("|"):
24.314 - continue
24.315 - if not nested:
24.316 - break
24.317 - if not source.next or sourcematch(")", 0):
24.318 - break
24.319 - else:
24.320 - raise error, "pattern not properly closed"
24.321 -
24.322 - if len(items) == 1:
24.323 - return items[0]
24.324 -
24.325 - subpattern = SubPattern(state)
24.326 - subpatternappend = subpattern.append
24.327 -
24.328 - # check if all items share a common prefix
24.329 - while 1:
24.330 - prefix = None
24.331 - for item in items:
24.332 - if not item:
24.333 - break
24.334 - if prefix is None:
24.335 - prefix = item[0]
24.336 - elif item[0] != prefix:
24.337 - break
24.338 - else:
24.339 - # all subitems start with a common "prefix".
24.340 - # move it out of the branch
24.341 - for item in items:
24.342 - del item[0]
24.343 - subpatternappend(prefix)
24.344 - continue # check next one
24.345 - break
24.346 -
24.347 - # check if the branch can be replaced by a character set
24.348 - for item in items:
24.349 - if len(item) != 1 or item[0][0] != LITERAL:
24.350 - break
24.351 - else:
24.352 - # we can store this as a character set instead of a
24.353 - # branch (the compiler may optimize this even more)
24.354 - set = []
24.355 - setappend = set.append
24.356 - for item in items:
24.357 - setappend(item[0])
24.358 - subpatternappend((IN, set))
24.359 - return subpattern
24.360 -
24.361 - subpattern.append((BRANCH, (None, items)))
24.362 - return subpattern
24.363 -
24.364 -def _parse_sub_cond(source, state, condgroup):
24.365 - item_yes = _parse(source, state)
24.366 - if source.match("|"):
24.367 - item_no = _parse(source, state)
24.368 - if source.match("|"):
24.369 - raise error, "conditional backref with more than two branches"
24.370 - else:
24.371 - item_no = None
24.372 - if source.next and not source.match(")", 0):
24.373 - raise error, "pattern not properly closed"
24.374 - subpattern = SubPattern(state)
24.375 - subpattern.append((GROUPREF_EXISTS, (condgroup, item_yes, item_no)))
24.376 - return subpattern
24.377 -
24.378 -_PATTERNENDERS = set("|)")
24.379 -_ASSERTCHARS = set("=!<")
24.380 -_LOOKBEHINDASSERTCHARS = set("=!")
24.381 -_REPEATCODES = set([MIN_REPEAT, MAX_REPEAT])
24.382 -
24.383 -def _parse(source, state):
24.384 - # parse a simple pattern
24.385 - subpattern = SubPattern(state)
24.386 -
24.387 - # precompute constants into local variables
24.388 - subpatternappend = subpattern.append
24.389 - sourceget = source.get
24.390 - sourcematch = source.match
24.391 - _len = len
24.392 - PATTERNENDERS = _PATTERNENDERS
24.393 - ASSERTCHARS = _ASSERTCHARS
24.394 - LOOKBEHINDASSERTCHARS = _LOOKBEHINDASSERTCHARS
24.395 - REPEATCODES = _REPEATCODES
24.396 -
24.397 - while 1:
24.398 -
24.399 - if source.next in PATTERNENDERS:
24.400 - break # end of subpattern
24.401 - this = sourceget()
24.402 - if this is None:
24.403 - break # end of pattern
24.404 -
24.405 - if state.flags & SRE_FLAG_VERBOSE:
24.406 - # skip whitespace and comments
24.407 - if this in WHITESPACE:
24.408 - continue
24.409 - if this == "#":
24.410 - while 1:
24.411 - this = sourceget()
24.412 - if this in (None, "\n"):
24.413 - break
24.414 - continue
24.415 -
24.416 - if this and this[0] not in SPECIAL_CHARS:
24.417 - subpatternappend((LITERAL, ord(this)))
24.418 -
24.419 - elif this == "[":
24.420 - # character set
24.421 - set = []
24.422 - setappend = set.append
24.423 -## if sourcematch(":"):
24.424 -## pass # handle character classes
24.425 - if sourcematch("^"):
24.426 - setappend((NEGATE, None))
24.427 - # check remaining characters
24.428 - start = set[:]
24.429 - while 1:
24.430 - this = sourceget()
24.431 - if this == "]" and set != start:
24.432 - break
24.433 - elif this and this[0] == "\\":
24.434 - code1 = _class_escape(source, this)
24.435 - elif this:
24.436 - code1 = LITERAL, ord(this)
24.437 - else:
24.438 - raise error, "unexpected end of regular expression"
24.439 - if sourcematch("-"):
24.440 - # potential range
24.441 - this = sourceget()
24.442 - if this == "]":
24.443 - if code1[0] is IN:
24.444 - code1 = code1[1][0]
24.445 - setappend(code1)
24.446 - setappend((LITERAL, ord("-")))
24.447 - break
24.448 - elif this:
24.449 - if this[0] == "\\":
24.450 - code2 = _class_escape(source, this)
24.451 - else:
24.452 - code2 = LITERAL, ord(this)
24.453 - if code1[0] != LITERAL or code2[0] != LITERAL:
24.454 - raise error, "bad character range"
24.455 - lo = code1[1]
24.456 - hi = code2[1]
24.457 - if hi < lo:
24.458 - raise error, "bad character range"
24.459 - setappend((RANGE, (lo, hi)))
24.460 - else:
24.461 - raise error, "unexpected end of regular expression"
24.462 - else:
24.463 - if code1[0] is IN:
24.464 - code1 = code1[1][0]
24.465 - setappend(code1)
24.466 -
24.467 - # XXX: <fl> should move set optimization to compiler!
24.468 - if _len(set)==1 and set[0][0] is LITERAL:
24.469 - subpatternappend(set[0]) # optimization
24.470 - elif _len(set)==2 and set[0][0] is NEGATE and set[1][0] is LITERAL:
24.471 - subpatternappend((NOT_LITERAL, set[1][1])) # optimization
24.472 - else:
24.473 - # XXX: <fl> should add charmap optimization here
24.474 - subpatternappend((IN, set))
24.475 -
24.476 - elif this and this[0] in REPEAT_CHARS:
24.477 - # repeat previous item
24.478 - if this == "?":
24.479 - min, max = 0, 1
24.480 - elif this == "*":
24.481 - min, max = 0, MAXREPEAT
24.482 -
24.483 - elif this == "+":
24.484 - min, max = 1, MAXREPEAT
24.485 - elif this == "{":
24.486 - if source.next == "}":
24.487 - subpatternappend((LITERAL, ord(this)))
24.488 - continue
24.489 - here = source.tell()
24.490 - min, max = 0, MAXREPEAT
24.491 - lo = hi = ""
24.492 - while source.next in DIGITS:
24.493 - lo = lo + source.get()
24.494 - if sourcematch(","):
24.495 - while source.next in DIGITS:
24.496 - hi = hi + sourceget()
24.497 - else:
24.498 - hi = lo
24.499 - if not sourcematch("}"):
24.500 - subpatternappend((LITERAL, ord(this)))
24.501 - source.seek(here)
24.502 - continue
24.503 - if lo:
24.504 - min = int(lo)
24.505 - if hi:
24.506 - max = int(hi)
24.507 - if max < min:
24.508 - raise error, "bad repeat interval"
24.509 - else:
24.510 - raise error, "not supported"
24.511 - # figure out which item to repeat
24.512 - if subpattern:
24.513 - item = subpattern[-1:]
24.514 - else:
24.515 - item = None
24.516 - if not item or (_len(item) == 1 and item[0][0] == AT):
24.517 - raise error, "nothing to repeat"
24.518 - if item[0][0] in REPEATCODES:
24.519 - raise error, "multiple repeat"
24.520 - if sourcematch("?"):
24.521 - subpattern[-1] = (MIN_REPEAT, (min, max, item))
24.522 - else:
24.523 - subpattern[-1] = (MAX_REPEAT, (min, max, item))
24.524 -
24.525 - elif this == ".":
24.526 - subpatternappend((ANY, None))
24.527 -
24.528 - elif this == "(":
24.529 - group = 1
24.530 - name = None
24.531 - condgroup = None
24.532 - if sourcematch("?"):
24.533 - group = 0
24.534 - # options
24.535 - if sourcematch("P"):
24.536 - # python extensions
24.537 - if sourcematch("<"):
24.538 - # named group: skip forward to end of name
24.539 - name = ""
24.540 - while 1:
24.541 - char = sourceget()
24.542 - if char is None:
24.543 - raise error, "unterminated name"
24.544 - if char == ">":
24.545 - break
24.546 - name = name + char
24.547 - group = 1
24.548 - if not isname(name):
24.549 - raise error, "bad character in group name"
24.550 - elif sourcematch("="):
24.551 - # named backreference
24.552 - name = ""
24.553 - while 1:
24.554 - char = sourceget()
24.555 - if char is None:
24.556 - raise error, "unterminated name"
24.557 - if char == ")":
24.558 - break
24.559 - name = name + char
24.560 - if not isname(name):
24.561 - raise error, "bad character in group name"
24.562 - gid = state.groupdict.get(name)
24.563 - if gid is None:
24.564 - raise error, "unknown group name"
24.565 - subpatternappend((GROUPREF, gid))
24.566 - continue
24.567 - else:
24.568 - char = sourceget()
24.569 - if char is None:
24.570 - raise error, "unexpected end of pattern"
24.571 - raise error, "unknown specifier: ?P%s" % char
24.572 - elif sourcematch(":"):
24.573 - # non-capturing group
24.574 - group = 2
24.575 - elif sourcematch("#"):
24.576 - # comment
24.577 - while 1:
24.578 - if source.next is None or source.next == ")":
24.579 - break
24.580 - sourceget()
24.581 - if not sourcematch(")"):
24.582 - raise error, "unbalanced parenthesis"
24.583 - continue
24.584 - elif source.next in ASSERTCHARS:
24.585 - # lookahead assertions
24.586 - char = sourceget()
24.587 - dir = 1
24.588 - if char == "<":
24.589 - if source.next not in LOOKBEHINDASSERTCHARS:
24.590 - raise error, "syntax error"
24.591 - dir = -1 # lookbehind
24.592 - char = sourceget()
24.593 - p = _parse_sub(source, state)
24.594 - if not sourcematch(")"):
24.595 - raise error, "unbalanced parenthesis"
24.596 - if char == "=":
24.597 - subpatternappend((ASSERT, (dir, p)))
24.598 - else:
24.599 - subpatternappend((ASSERT_NOT, (dir, p)))
24.600 - continue
24.601 - elif sourcematch("("):
24.602 - # conditional backreference group
24.603 - condname = ""
24.604 - while 1:
24.605 - char = sourceget()
24.606 - if char is None:
24.607 - raise error, "unterminated name"
24.608 - if char == ")":
24.609 - break
24.610 - condname = condname + char
24.611 - group = 2
24.612 - if isname(condname):
24.613 - condgroup = state.groupdict.get(condname)
24.614 - if condgroup is None:
24.615 - raise error, "unknown group name"
24.616 - else:
24.617 - try:
24.618 - condgroup = int(condname)
24.619 - except ValueError:
24.620 - raise error, "bad character in group name"
24.621 - else:
24.622 - # flags
24.623 - if not source.next in FLAGS:
24.624 - raise error, "unexpected end of pattern"
24.625 - while source.next in FLAGS:
24.626 - state.flags = state.flags | FLAGS[sourceget()]
24.627 - if group:
24.628 - # parse group contents
24.629 - if group == 2:
24.630 - # anonymous group
24.631 - group = None
24.632 - else:
24.633 - group = state.opengroup(name)
24.634 - if condgroup:
24.635 - p = _parse_sub_cond(source, state, condgroup)
24.636 - else:
24.637 - p = _parse_sub(source, state)
24.638 - if not sourcematch(")"):
24.639 - raise error, "unbalanced parenthesis"
24.640 - if group is not None:
24.641 - state.closegroup(group)
24.642 - subpatternappend((SUBPATTERN, (group, p)))
24.643 - else:
24.644 - while 1:
24.645 - char = sourceget()
24.646 - if char is None:
24.647 - raise error, "unexpected end of pattern"
24.648 - if char == ")":
24.649 - break
24.650 - raise error, "unknown extension"
24.651 -
24.652 - elif this == "^":
24.653 - subpatternappend((AT, AT_BEGINNING))
24.654 -
24.655 - elif this == "$":
24.656 - subpattern.append((AT, AT_END))
24.657 -
24.658 - elif this and this[0] == "\\":
24.659 - code = _escape(source, this, state)
24.660 - subpatternappend(code)
24.661 -
24.662 - else:
24.663 - raise error, "parser error"
24.664 -
24.665 - return subpattern
24.666 -
24.667 -def parse(str, flags=0, pattern=None):
24.668 - # parse 're' pattern into list of (opcode, argument) tuples
24.669 -
24.670 - source = Tokenizer(str)
24.671 -
24.672 - if pattern is None:
24.673 - pattern = Pattern()
24.674 - pattern.flags = flags
24.675 - pattern.str = str
24.676 -
24.677 - p = _parse_sub(source, pattern, 0)
24.678 -
24.679 - tail = source.get()
24.680 - if tail == ")":
24.681 - raise error, "unbalanced parenthesis"
24.682 - elif tail:
24.683 - raise error, "bogus characters at end of regular expression"
24.684 -
24.685 - if flags & SRE_FLAG_DEBUG:
24.686 - p.dump()
24.687 -
24.688 - if not (flags & SRE_FLAG_VERBOSE) and p.pattern.flags & SRE_FLAG_VERBOSE:
24.689 - # the VERBOSE flag was switched on inside the pattern. to be
24.690 - # on the safe side, we'll parse the whole thing again...
24.691 - return parse(str, p.pattern.flags)
24.692 -
24.693 - return p
24.694 -
24.695 -def parse_template(source, pattern):
24.696 - # parse 're' replacement string into list of literals and
24.697 - # group references
24.698 - s = Tokenizer(source)
24.699 - sget = s.get
24.700 - p = []
24.701 - a = p.append
24.702 - def literal(literal, p=p, pappend=a):
24.703 - if p and p[-1][0] is LITERAL:
24.704 - p[-1] = LITERAL, p[-1][1] + literal
24.705 - else:
24.706 - pappend((LITERAL, literal))
24.707 - sep = source[:0]
24.708 - if type(sep) is type(""):
24.709 - makechar = chr
24.710 - else:
24.711 - makechar = unichr
24.712 - while 1:
24.713 - this = sget()
24.714 - if this is None:
24.715 - break # end of replacement string
24.716 - if this and this[0] == "\\":
24.717 - # group
24.718 - c = this[1:2]
24.719 - if c == "g":
24.720 - name = ""
24.721 - if s.match("<"):
24.722 - while 1:
24.723 - char = sget()
24.724 - if char is None:
24.725 - raise error, "unterminated group name"
24.726 - if char == ">":
24.727 - break
24.728 - name = name + char
24.729 - if not name:
24.730 - raise error, "bad group name"
24.731 - try:
24.732 - index = int(name)
24.733 - if index < 0:
24.734 - raise error, "negative group number"
24.735 - except ValueError:
24.736 - if not isname(name):
24.737 - raise error, "bad character in group name"
24.738 - try:
24.739 - index = pattern.groupindex[name]
24.740 - except KeyError:
24.741 - raise IndexError, "unknown group name"
24.742 - a((MARK, index))
24.743 - elif c == "0":
24.744 - if s.next in OCTDIGITS:
24.745 - this = this + sget()
24.746 - if s.next in OCTDIGITS:
24.747 - this = this + sget()
24.748 - literal(makechar(int(this[1:], 8) & 0xff))
24.749 - elif c in DIGITS:
24.750 - isoctal = False
24.751 - if s.next in DIGITS:
24.752 - this = this + sget()
24.753 - if (c in OCTDIGITS and this[2] in OCTDIGITS and
24.754 - s.next in OCTDIGITS):
24.755 - this = this + sget()
24.756 - isoctal = True
24.757 - literal(makechar(int(this[1:], 8) & 0xff))
24.758 - if not isoctal:
24.759 - a((MARK, int(this[1:])))
24.760 - else:
24.761 - try:
24.762 - this = makechar(ESCAPES[this][1])
24.763 - except KeyError:
24.764 - pass
24.765 - literal(this)
24.766 - else:
24.767 - literal(this)
24.768 - # convert template to groups and literals lists
24.769 - i = 0
24.770 - groups = []
24.771 - groupsappend = groups.append
24.772 - literals = [None] * len(p)
24.773 - for c, s in p:
24.774 - if c is MARK:
24.775 - groupsappend((i, s))
24.776 - # literal[i] is already None
24.777 - else:
24.778 - literals[i] = s
24.779 - i = i + 1
24.780 - return groups, literals
24.781 -
24.782 -def expand_template(template, match):
24.783 - g = match.group
24.784 - sep = match.string[:0]
24.785 - groups, literals = template
24.786 - literals = literals[:]
24.787 - try:
24.788 - for index, group in groups:
24.789 - literals[index] = s = g(group)
24.790 - if s is None:
24.791 - raise error, "unmatched group"
24.792 - except IndexError:
24.793 - raise error, "invalid group reference"
24.794 - return sep.join(literals)
25.1 --- a/lib/threading.py Sat Dec 03 17:11:59 2016 +0100
25.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
25.3 @@ -1,825 +0,0 @@
25.4 -#!/usr/bin/env python
25.5 -
25.6 -"""
25.7 -Thread module emulating a subset of Java's threading model.
25.8 -
25.9 -See docs/COPYING.txt and docs/LICENCE-python.txt for copyright and licensing
25.10 -information for Python standard library modules.
25.11 -"""
25.12 -
25.13 -import sys as _sys
25.14 -
25.15 -try:
25.16 - import thread
25.17 -except ImportError:
25.18 - del _sys.modules[__name__]
25.19 - raise
25.20 -
25.21 -from time import time as _time, sleep as _sleep
25.22 -from traceback import format_exc as _format_exc
25.23 -from collections import deque
25.24 -
25.25 -# Rename some stuff so "from threading import *" is safe
25.26 -__all__ = ['activeCount', 'Condition', 'currentThread', 'enumerate', 'Event',
25.27 - 'Lock', 'RLock', 'Semaphore', 'BoundedSemaphore', 'Thread',
25.28 - 'Timer', 'setprofile', 'settrace', 'local', 'stack_size']
25.29 -
25.30 -_start_new_thread = thread.start_new_thread
25.31 -_allocate_lock = thread.allocate_lock
25.32 -_get_ident = thread.get_ident
25.33 -ThreadError = thread.error
25.34 -del thread
25.35 -
25.36 -
25.37 -# Support for profile and trace hooks
25.38 -
25.39 -_profile_hook = None
25.40 -_trace_hook = None
25.41 -
25.42 -def setprofile(func):
25.43 - global _profile_hook
25.44 - _profile_hook = func
25.45 -
25.46 -def settrace(func):
25.47 - global _trace_hook
25.48 - _trace_hook = func
25.49 -
25.50 -# Synchronization classes
25.51 -
25.52 -Lock = _allocate_lock
25.53 -
25.54 -def RLock(*args, **kwargs):
25.55 - return _RLock(*args, **kwargs)
25.56 -
25.57 -class _RLock:
25.58 -
25.59 - def __init__(self):
25.60 - self.__block = _allocate_lock()
25.61 - self.__owner = None
25.62 - self.__count = 0
25.63 -
25.64 - def __repr__(self):
25.65 - owner = self.__owner
25.66 - return "<%s(%s, %d)>" % (
25.67 - self.__class__.__name__,
25.68 - owner and owner.getName(),
25.69 - self.__count)
25.70 -
25.71 - def acquire(self, blocking=1):
25.72 - me = currentThread()
25.73 - if self.__owner is me:
25.74 - self.__count = self.__count + 1
25.75 - if __debug__:
25.76 - self._note("%s.acquire(%s): recursive success", self, blocking)
25.77 - return 1
25.78 - rc = self.__block.acquire(blocking)
25.79 - if rc:
25.80 - self.__owner = me
25.81 - self.__count = 1
25.82 - if __debug__:
25.83 - self._note("%s.acquire(%s): initial success", self, blocking)
25.84 - else:
25.85 - if __debug__:
25.86 - self._note("%s.acquire(%s): failure", self, blocking)
25.87 - return rc
25.88 -
25.89 - __enter__ = acquire
25.90 -
25.91 - def release(self):
25.92 - if self.__owner is not currentThread():
25.93 - raise RuntimeError("cannot release un-aquired lock")
25.94 - self.__count = count = self.__count - 1
25.95 - if not count:
25.96 - self.__owner = None
25.97 - self.__block.release()
25.98 - if __debug__:
25.99 - self._note("%s.release(): final release", self)
25.100 - else:
25.101 - if __debug__:
25.102 - self._note("%s.release(): non-final release", self)
25.103 -
25.104 - def __exit__(self, t, v, tb):
25.105 - self.release()
25.106 -
25.107 - # Internal methods used by condition variables
25.108 -
25.109 - def _acquire_restore(self, (count, owner)):
25.110 - self.__block.acquire()
25.111 - self.__count = count
25.112 - self.__owner = owner
25.113 - if __debug__:
25.114 - self._note("%s._acquire_restore()", self)
25.115 -
25.116 - def _release_save(self):
25.117 - if __debug__:
25.118 - self._note("%s._release_save()", self)
25.119 - count = self.__count
25.120 - self.__count = 0
25.121 - owner = self.__owner
25.122 - self.__owner = None
25.123 - self.__block.release()
25.124 - return (count, owner)
25.125 -
25.126 - def _is_owned(self):
25.127 - return self.__owner is currentThread()
25.128 -
25.129 -
25.130 -def Condition(*args, **kwargs):
25.131 - return _Condition(*args, **kwargs)
25.132 -
25.133 -class _Condition:
25.134 -
25.135 - def __init__(self, lock=None):
25.136 - if lock is None:
25.137 - lock = RLock()
25.138 - self.__lock = lock
25.139 - # Export the lock's acquire() and release() methods
25.140 - self.acquire = lock.acquire
25.141 - self.release = lock.release
25.142 - # If the lock defines _release_save() and/or _acquire_restore(),
25.143 - # these override the default implementations (which just call
25.144 - # release() and acquire() on the lock). Ditto for _is_owned().
25.145 - try:
25.146 - self._release_save = lock._release_save
25.147 - except AttributeError:
25.148 - pass
25.149 - try:
25.150 - self._acquire_restore = lock._acquire_restore
25.151 - except AttributeError:
25.152 - pass
25.153 - try:
25.154 - self._is_owned = lock._is_owned
25.155 - except AttributeError:
25.156 - pass
25.157 - self.__waiters = []
25.158 -
25.159 - def __enter__(self):
25.160 - return self.__lock.__enter__()
25.161 -
25.162 - def __exit__(self, *args):
25.163 - return self.__lock.__exit__(*args)
25.164 -
25.165 - def __repr__(self):
25.166 - return "<Condition(%s, %d)>" % (self.__lock, len(self.__waiters))
25.167 -
25.168 - def _release_save(self):
25.169 - self.__lock.release() # No state to save
25.170 -
25.171 - def _acquire_restore(self, x):
25.172 - self.__lock.acquire() # Ignore saved state
25.173 -
25.174 - def _is_owned(self):
25.175 - # Return True if lock is owned by currentThread.
25.176 - # This method is called only if __lock doesn't have _is_owned().
25.177 - if self.__lock.acquire(0):
25.178 - self.__lock.release()
25.179 - return False
25.180 - else:
25.181 - return True
25.182 -
25.183 - def wait(self, timeout=None):
25.184 - if not self._is_owned():
25.185 - raise RuntimeError("cannot wait on un-aquired lock")
25.186 - waiter = _allocate_lock()
25.187 - waiter.acquire()
25.188 - self.__waiters.append(waiter)
25.189 - saved_state = self._release_save()
25.190 - try: # restore state no matter what (e.g., KeyboardInterrupt)
25.191 - if timeout is None:
25.192 - waiter.acquire()
25.193 - if __debug__:
25.194 - self._note("%s.wait(): got it", self)
25.195 - else:
25.196 - # Balancing act: We can't afford a pure busy loop, so we
25.197 - # have to sleep; but if we sleep the whole timeout time,
25.198 - # we'll be unresponsive. The scheme here sleeps very
25.199 - # little at first, longer as time goes on, but never longer
25.200 - # than 20 times per second (or the timeout time remaining).
25.201 - endtime = _time() + timeout
25.202 - delay = 0.0005 # 500 us -> initial delay of 1 ms
25.203 - while True:
25.204 - gotit = waiter.acquire(0)
25.205 - if gotit:
25.206 - break
25.207 - remaining = endtime - _time()
25.208 - if remaining <= 0:
25.209 - break
25.210 - delay = min(delay * 2, remaining, .05)
25.211 - _sleep(delay)
25.212 - if not gotit:
25.213 - if __debug__:
25.214 - self._note("%s.wait(%s): timed out", self, timeout)
25.215 - try:
25.216 - self.__waiters.remove(waiter)
25.217 - except ValueError:
25.218 - pass
25.219 - else:
25.220 - if __debug__:
25.221 - self._note("%s.wait(%s): got it", self, timeout)
25.222 - finally:
25.223 - self._acquire_restore(saved_state)
25.224 -
25.225 - def notify(self, n=1):
25.226 - if not self._is_owned():
25.227 - raise RuntimeError("cannot notify on un-aquired lock")
25.228 - __waiters = self.__waiters
25.229 - waiters = __waiters[:n]
25.230 - if not waiters:
25.231 - if __debug__:
25.232 - self._note("%s.notify(): no waiters", self)
25.233 - return
25.234 - self._note("%s.notify(): notifying %d waiter%s", self, n,
25.235 - n!=1 and "s" or "")
25.236 - for waiter in waiters:
25.237 - waiter.release()
25.238 - try:
25.239 - __waiters.remove(waiter)
25.240 - except ValueError:
25.241 - pass
25.242 -
25.243 - def notifyAll(self):
25.244 - self.notify(len(self.__waiters))
25.245 -
25.246 -
25.247 -def Semaphore(*args, **kwargs):
25.248 - return _Semaphore(*args, **kwargs)
25.249 -
25.250 -class _Semaphore:
25.251 -
25.252 - # After Tim Peters' semaphore class, but not quite the same (no maximum)
25.253 -
25.254 - def __init__(self, value=1):
25.255 - if value < 0:
25.256 - raise ValueError("semaphore initial value must be >= 0")
25.257 - self.__cond = Condition(Lock())
25.258 - self.__value = value
25.259 -
25.260 - def acquire(self, blocking=1):
25.261 - rc = False
25.262 - self.__cond.acquire()
25.263 - while self.__value == 0:
25.264 - if not blocking:
25.265 - break
25.266 - if __debug__:
25.267 - self._note("%s.acquire(%s): blocked waiting, value=%s",
25.268 - self, blocking, self.__value)
25.269 - self.__cond.wait()
25.270 - else:
25.271 - self.__value = self.__value - 1
25.272 - if __debug__:
25.273 - self._note("%s.acquire: success, value=%s",
25.274 - self, self.__value)
25.275 - rc = True
25.276 - self.__cond.release()
25.277 - return rc
25.278 -
25.279 - __enter__ = acquire
25.280 -
25.281 - def release(self):
25.282 - self.__cond.acquire()
25.283 - self.__value = self.__value + 1
25.284 - if __debug__:
25.285 - self._note("%s.release: success, value=%s",
25.286 - self, self.__value)
25.287 - self.__cond.notify()
25.288 - self.__cond.release()
25.289 -
25.290 - def __exit__(self, t, v, tb):
25.291 - self.release()
25.292 -
25.293 -
25.294 -def BoundedSemaphore(*args, **kwargs):
25.295 - return _BoundedSemaphore(*args, **kwargs)
25.296 -
25.297 -class _BoundedSemaphore(_Semaphore):
25.298 - """Semaphore that checks that # releases is <= # acquires"""
25.299 - def __init__(self, value=1):
25.300 - _Semaphore.__init__(self, value)
25.301 - self._initial_value = value
25.302 -
25.303 - def release(self):
25.304 - if self._Semaphore__value >= self._initial_value:
25.305 - raise ValueError, "Semaphore released too many times"
25.306 - return _Semaphore.release(self)
25.307 -
25.308 -
25.309 -def Event(*args, **kwargs):
25.310 - return _Event(*args, **kwargs)
25.311 -
25.312 -class _Event:
25.313 -
25.314 - # After Tim Peters' event class (without is_posted())
25.315 -
25.316 - def __init__(self):
25.317 - self.__cond = Condition(Lock())
25.318 - self.__flag = False
25.319 -
25.320 - def isSet(self):
25.321 - return self.__flag
25.322 -
25.323 - def set(self):
25.324 - self.__cond.acquire()
25.325 - try:
25.326 - self.__flag = True
25.327 - self.__cond.notifyAll()
25.328 - finally:
25.329 - self.__cond.release()
25.330 -
25.331 - def clear(self):
25.332 - self.__cond.acquire()
25.333 - try:
25.334 - self.__flag = False
25.335 - finally:
25.336 - self.__cond.release()
25.337 -
25.338 - def wait(self, timeout=None):
25.339 - self.__cond.acquire()
25.340 - try:
25.341 - if not self.__flag:
25.342 - self.__cond.wait(timeout)
25.343 - finally:
25.344 - self.__cond.release()
25.345 -
25.346 -# Helper to generate new thread names
25.347 -_counter = 0
25.348 -def _newname(template="Thread-%d"):
25.349 - global _counter
25.350 - _counter = _counter + 1
25.351 - return template % _counter
25.352 -
25.353 -# Active thread administration
25.354 -_active_limbo_lock = _allocate_lock()
25.355 -_active = {} # maps thread id to Thread object
25.356 -_limbo = {}
25.357 -
25.358 -
25.359 -# Main class for threads
25.360 -
25.361 -class Thread:
25.362 -
25.363 - __initialized = False
25.364 - # Need to store a reference to sys.exc_info for printing
25.365 - # out exceptions when a thread tries to use a global var. during interp.
25.366 - # shutdown and thus raises an exception about trying to perform some
25.367 - # operation on/with a NoneType
25.368 - __exc_info = _sys.exc_info
25.369 -
25.370 - def __init__(self, group=None, target=None, name=None,
25.371 - args=(), kwargs=None):
25.372 - assert group is None, "group argument must be None for now"
25.373 - if kwargs is None:
25.374 - kwargs = {}
25.375 - self.__target = target
25.376 - self.__name = str(name or _newname())
25.377 - self.__args = args
25.378 - self.__kwargs = kwargs
25.379 - self.__daemonic = self._set_daemon()
25.380 - self.__started = False
25.381 - self.__stopped = False
25.382 - self.__block = Condition(Lock())
25.383 - self.__initialized = True
25.384 - # sys.stderr is not stored in the class like
25.385 - # sys.exc_info since it can be changed between instances
25.386 - self.__stderr = _sys.stderr
25.387 -
25.388 - def _set_daemon(self):
25.389 - # Overridden in _MainThread and _DummyThread
25.390 - return currentThread().isDaemon()
25.391 -
25.392 - def __repr__(self):
25.393 - assert self.__initialized, "Thread.__init__() was not called"
25.394 - status = "initial"
25.395 - if self.__started:
25.396 - status = "started"
25.397 - if self.__stopped:
25.398 - status = "stopped"
25.399 - if self.__daemonic:
25.400 - status = status + " daemon"
25.401 - return "<%s(%s, %s)>" % (self.__class__.__name__, self.__name, status)
25.402 -
25.403 - def start(self):
25.404 - if not self.__initialized:
25.405 - raise RuntimeError("thread.__init__() not called")
25.406 - if self.__started:
25.407 - raise RuntimeError("thread already started")
25.408 - if __debug__:
25.409 - self._note("%s.start(): starting thread", self)
25.410 - _active_limbo_lock.acquire()
25.411 - _limbo[self] = self
25.412 - _active_limbo_lock.release()
25.413 - _start_new_thread(self.__bootstrap, ())
25.414 - self.__started = True
25.415 - _sleep(0.000001) # 1 usec, to let the thread run (Solaris hack)
25.416 -
25.417 - def run(self):
25.418 - if self.__target:
25.419 - self.__target(*self.__args, **self.__kwargs)
25.420 -
25.421 - def __bootstrap(self):
25.422 - # Wrapper around the real bootstrap code that ignores
25.423 - # exceptions during interpreter cleanup. Those typically
25.424 - # happen when a daemon thread wakes up at an unfortunate
25.425 - # moment, finds the world around it destroyed, and raises some
25.426 - # random exception *** while trying to report the exception in
25.427 - # __bootstrap_inner() below ***. Those random exceptions
25.428 - # don't help anybody, and they confuse users, so we suppress
25.429 - # them. We suppress them only when it appears that the world
25.430 - # indeed has already been destroyed, so that exceptions in
25.431 - # __bootstrap_inner() during normal business hours are properly
25.432 - # reported. Also, we only suppress them for daemonic threads;
25.433 - # if a non-daemonic encounters this, something else is wrong.
25.434 - try:
25.435 - self.__bootstrap_inner()
25.436 - except:
25.437 - if self.__daemonic and _sys is None:
25.438 - return
25.439 - raise
25.440 -
25.441 - def __bootstrap_inner(self):
25.442 - try:
25.443 - self.__started = True
25.444 - _active_limbo_lock.acquire()
25.445 - _active[_get_ident()] = self
25.446 - del _limbo[self]
25.447 - _active_limbo_lock.release()
25.448 - if __debug__:
25.449 - self._note("%s.__bootstrap(): thread started", self)
25.450 -
25.451 - if _trace_hook:
25.452 - self._note("%s.__bootstrap(): registering trace hook", self)
25.453 - _sys.settrace(_trace_hook)
25.454 - if _profile_hook:
25.455 - self._note("%s.__bootstrap(): registering profile hook", self)
25.456 - _sys.setprofile(_profile_hook)
25.457 -
25.458 - try:
25.459 - self.run()
25.460 - except SystemExit:
25.461 - if __debug__:
25.462 - self._note("%s.__bootstrap(): raised SystemExit", self)
25.463 - except:
25.464 - if __debug__:
25.465 - self._note("%s.__bootstrap(): unhandled exception", self)
25.466 - # If sys.stderr is no more (most likely from interpreter
25.467 - # shutdown) use self.__stderr. Otherwise still use sys (as in
25.468 - # _sys) in case sys.stderr was redefined since the creation of
25.469 - # self.
25.470 - if _sys:
25.471 - _sys.stderr.write("Exception in thread %s:\n%s\n" %
25.472 - (self.getName(), _format_exc()))
25.473 - else:
25.474 - # Do the best job possible w/o a huge amt. of code to
25.475 - # approximate a traceback (code ideas from
25.476 - # Lib/traceback.py)
25.477 - exc_type, exc_value, exc_tb = self.__exc_info()
25.478 - try:
25.479 - print>>self.__stderr, (
25.480 - "Exception in thread " + self.getName() +
25.481 - " (most likely raised during interpreter shutdown):")
25.482 - print>>self.__stderr, (
25.483 - "Traceback (most recent call last):")
25.484 - while exc_tb:
25.485 - print>>self.__stderr, (
25.486 - ' File "%s", line %s, in %s' %
25.487 - (exc_tb.tb_frame.f_code.co_filename,
25.488 - exc_tb.tb_lineno,
25.489 - exc_tb.tb_frame.f_code.co_name))
25.490 - exc_tb = exc_tb.tb_next
25.491 - print>>self.__stderr, ("%s: %s" % (exc_type, exc_value))
25.492 - # Make sure that exc_tb gets deleted since it is a memory
25.493 - # hog; deleting everything else is just for thoroughness
25.494 - finally:
25.495 - del exc_type, exc_value, exc_tb
25.496 - else:
25.497 - if __debug__:
25.498 - self._note("%s.__bootstrap(): normal return", self)
25.499 - finally:
25.500 - _active_limbo_lock.acquire()
25.501 - try:
25.502 - self.__stop()
25.503 - try:
25.504 - # We don't call self.__delete() because it also
25.505 - # grabs _active_limbo_lock.
25.506 - del _active[_get_ident()]
25.507 - except:
25.508 - pass
25.509 - finally:
25.510 - _active_limbo_lock.release()
25.511 -
25.512 - def __stop(self):
25.513 - self.__block.acquire()
25.514 - self.__stopped = True
25.515 - self.__block.notifyAll()
25.516 - self.__block.release()
25.517 -
25.518 - def __delete(self):
25.519 - "Remove current thread from the dict of currently running threads."
25.520 -
25.521 - # Notes about running with dummy_thread:
25.522 - #
25.523 - # Must take care to not raise an exception if dummy_thread is being
25.524 - # used (and thus this module is being used as an instance of
25.525 - # dummy_threading). dummy_thread.get_ident() always returns -1 since
25.526 - # there is only one thread if dummy_thread is being used. Thus
25.527 - # len(_active) is always <= 1 here, and any Thread instance created
25.528 - # overwrites the (if any) thread currently registered in _active.
25.529 - #
25.530 - # An instance of _MainThread is always created by 'threading'. This
25.531 - # gets overwritten the instant an instance of Thread is created; both
25.532 - # threads return -1 from dummy_thread.get_ident() and thus have the
25.533 - # same key in the dict. So when the _MainThread instance created by
25.534 - # 'threading' tries to clean itself up when atexit calls this method
25.535 - # it gets a KeyError if another Thread instance was created.
25.536 - #
25.537 - # This all means that KeyError from trying to delete something from
25.538 - # _active if dummy_threading is being used is a red herring. But
25.539 - # since it isn't if dummy_threading is *not* being used then don't
25.540 - # hide the exception.
25.541 -
25.542 - _active_limbo_lock.acquire()
25.543 - try:
25.544 - try:
25.545 - del _active[_get_ident()]
25.546 - except KeyError:
25.547 - if 'dummy_threading' not in _sys.modules:
25.548 - raise
25.549 - finally:
25.550 - _active_limbo_lock.release()
25.551 -
25.552 - def join(self, timeout=None):
25.553 - if not self.__initialized:
25.554 - raise RuntimeError("Thread.__init__() not called")
25.555 - if not self.__started:
25.556 - raise RuntimeError("cannot join thread before it is started")
25.557 - if self is currentThread():
25.558 - raise RuntimeError("cannot join current thread")
25.559 -
25.560 - if __debug__:
25.561 - if not self.__stopped:
25.562 - self._note("%s.join(): waiting until thread stops", self)
25.563 - self.__block.acquire()
25.564 - try:
25.565 - if timeout is None:
25.566 - while not self.__stopped:
25.567 - self.__block.wait()
25.568 - if __debug__:
25.569 - self._note("%s.join(): thread stopped", self)
25.570 - else:
25.571 - deadline = _time() + timeout
25.572 - while not self.__stopped:
25.573 - delay = deadline - _time()
25.574 - if delay <= 0:
25.575 - if __debug__:
25.576 - self._note("%s.join(): timed out", self)
25.577 - break
25.578 - self.__block.wait(delay)
25.579 - else:
25.580 - if __debug__:
25.581 - self._note("%s.join(): thread stopped", self)
25.582 - finally:
25.583 - self.__block.release()
25.584 -
25.585 - def getName(self):
25.586 - assert self.__initialized, "Thread.__init__() not called"
25.587 - return self.__name
25.588 -
25.589 - def setName(self, name):
25.590 - assert self.__initialized, "Thread.__init__() not called"
25.591 - self.__name = str(name)
25.592 -
25.593 - def isAlive(self):
25.594 - assert self.__initialized, "Thread.__init__() not called"
25.595 - return self.__started and not self.__stopped
25.596 -
25.597 - def isDaemon(self):
25.598 - assert self.__initialized, "Thread.__init__() not called"
25.599 - return self.__daemonic
25.600 -
25.601 - def setDaemon(self, daemonic):
25.602 - if not self.__initialized:
25.603 - raise RuntimeError("Thread.__init__() not called")
25.604 - if self.__started:
25.605 - raise RuntimeError("cannot set daemon status of active thread");
25.606 - self.__daemonic = daemonic
25.607 -
25.608 -# The timer class was contributed by Itamar Shtull-Trauring
25.609 -
25.610 -def Timer(*args, **kwargs):
25.611 - return _Timer(*args, **kwargs)
25.612 -
25.613 -class _Timer(Thread):
25.614 - """Call a function after a specified number of seconds:
25.615 -
25.616 - t = Timer(30.0, f, args=[], kwargs={})
25.617 - t.start()
25.618 - t.cancel() # stop the timer's action if it's still waiting
25.619 - """
25.620 -
25.621 - def __init__(self, interval, function, args=[], kwargs={}):
25.622 - Thread.__init__(self)
25.623 - self.interval = interval
25.624 - self.function = function
25.625 - self.args = args
25.626 - self.kwargs = kwargs
25.627 - self.finished = Event()
25.628 -
25.629 - def cancel(self):
25.630 - """Stop the timer if it hasn't finished yet"""
25.631 - self.finished.set()
25.632 -
25.633 - def run(self):
25.634 - self.finished.wait(self.interval)
25.635 - if not self.finished.isSet():
25.636 - self.function(*self.args, **self.kwargs)
25.637 - self.finished.set()
25.638 -
25.639 -# Special thread class to represent the main thread
25.640 -# This is garbage collected through an exit handler
25.641 -
25.642 -class _MainThread(Thread):
25.643 -
25.644 - def __init__(self):
25.645 - Thread.__init__(self, name="MainThread")
25.646 - self._Thread__started = True
25.647 - _active_limbo_lock.acquire()
25.648 - _active[_get_ident()] = self
25.649 - _active_limbo_lock.release()
25.650 -
25.651 - def _set_daemon(self):
25.652 - return False
25.653 -
25.654 - def _exitfunc(self):
25.655 - self._Thread__stop()
25.656 - t = _pickSomeNonDaemonThread()
25.657 - if t:
25.658 - if __debug__:
25.659 - self._note("%s: waiting for other threads", self)
25.660 - while t:
25.661 - t.join()
25.662 - t = _pickSomeNonDaemonThread()
25.663 - if __debug__:
25.664 - self._note("%s: exiting", self)
25.665 - self._Thread__delete()
25.666 -
25.667 -def _pickSomeNonDaemonThread():
25.668 - for t in enumerate():
25.669 - if not t.isDaemon() and t.isAlive():
25.670 - return t
25.671 - return None
25.672 -
25.673 -
25.674 -# Dummy thread class to represent threads not started here.
25.675 -# These aren't garbage collected when they die, nor can they be waited for.
25.676 -# If they invoke anything in threading.py that calls currentThread(), they
25.677 -# leave an entry in the _active dict forever after.
25.678 -# Their purpose is to return *something* from currentThread().
25.679 -# They are marked as daemon threads so we won't wait for them
25.680 -# when we exit (conform previous semantics).
25.681 -
25.682 -class _DummyThread(Thread):
25.683 -
25.684 - def __init__(self):
25.685 - Thread.__init__(self, name=_newname("Dummy-%d"))
25.686 -
25.687 - # Thread.__block consumes an OS-level locking primitive, which
25.688 - # can never be used by a _DummyThread. Since a _DummyThread
25.689 - # instance is immortal, that's bad, so release this resource.
25.690 - del self._Thread__block
25.691 -
25.692 - self._Thread__started = True
25.693 - _active_limbo_lock.acquire()
25.694 - _active[_get_ident()] = self
25.695 - _active_limbo_lock.release()
25.696 -
25.697 - def _set_daemon(self):
25.698 - return True
25.699 -
25.700 - def join(self, timeout=None):
25.701 - assert False, "cannot join a dummy thread"
25.702 -
25.703 -
25.704 -# Global API functions
25.705 -
25.706 -def currentThread():
25.707 - try:
25.708 - return _active[_get_ident()]
25.709 - except KeyError:
25.710 - ##print "currentThread(): no current thread for", _get_ident()
25.711 - return _DummyThread()
25.712 -
25.713 -def activeCount():
25.714 - _active_limbo_lock.acquire()
25.715 - count = len(_active) + len(_limbo)
25.716 - _active_limbo_lock.release()
25.717 - return count
25.718 -
25.719 -def enumerate():
25.720 - _active_limbo_lock.acquire()
25.721 - active = _active.values() + _limbo.values()
25.722 - _active_limbo_lock.release()
25.723 - return active
25.724 -
25.725 -from thread import stack_size
25.726 -
25.727 -# Create the main thread object,
25.728 -# and make it available for the interpreter
25.729 -# (Py_Main) as threading._shutdown.
25.730 -
25.731 -_shutdown = _MainThread()._exitfunc
25.732 -
25.733 -# get thread-local implementation, either from the thread
25.734 -# module, or from the python fallback
25.735 -
25.736 -try:
25.737 - from thread import _local as local
25.738 -except ImportError:
25.739 - from _threading_local import local
25.740 -
25.741 -
25.742 -# Self-test code
25.743 -
25.744 -def _test():
25.745 -
25.746 - class BoundedQueue:
25.747 -
25.748 - def __init__(self, limit):
25.749 - self.mon = RLock()
25.750 - self.rc = Condition(self.mon)
25.751 - self.wc = Condition(self.mon)
25.752 - self.limit = limit
25.753 - self.queue = deque()
25.754 -
25.755 - def put(self, item):
25.756 - self.mon.acquire()
25.757 - while len(self.queue) >= self.limit:
25.758 - self._note("put(%s): queue full", item)
25.759 - self.wc.wait()
25.760 - self.queue.append(item)
25.761 - self._note("put(%s): appended, length now %d",
25.762 - item, len(self.queue))
25.763 - self.rc.notify()
25.764 - self.mon.release()
25.765 -
25.766 - def get(self):
25.767 - self.mon.acquire()
25.768 - while not self.queue:
25.769 - self._note("get(): queue empty")
25.770 - self.rc.wait()
25.771 - item = self.queue.popleft()
25.772 - self._note("get(): got %s, %d left", item, len(self.queue))
25.773 - self.wc.notify()
25.774 - self.mon.release()
25.775 - return item
25.776 -
25.777 - class ProducerThread(Thread):
25.778 -
25.779 - def __init__(self, queue, quota):
25.780 - Thread.__init__(self, name="Producer")
25.781 - self.queue = queue
25.782 - self.quota = quota
25.783 -
25.784 - def run(self):
25.785 - from random import random
25.786 - counter = 0
25.787 - while counter < self.quota:
25.788 - counter = counter + 1
25.789 - self.queue.put("%s.%d" % (self.getName(), counter))
25.790 - _sleep(random() * 0.00001)
25.791 -
25.792 -
25.793 - class ConsumerThread(Thread):
25.794 -
25.795 - def __init__(self, queue, count):
25.796 - Thread.__init__(self, name="Consumer")
25.797 - self.queue = queue
25.798 - self.count = count
25.799 -
25.800 - def run(self):
25.801 - while self.count > 0:
25.802 - item = self.queue.get()
25.803 - print item
25.804 - self.count = self.count - 1
25.805 -
25.806 - NP = 3
25.807 - QL = 4
25.808 - NI = 5
25.809 -
25.810 - Q = BoundedQueue(QL)
25.811 - P = []
25.812 - for i in range(NP):
25.813 - t = ProducerThread(Q, NI)
25.814 - t.setName("Producer-%d" % (i+1))
25.815 - P.append(t)
25.816 - C = ConsumerThread(Q, NI*NP)
25.817 - for t in P:
25.818 - t.start()
25.819 - _sleep(0.000001)
25.820 - C.start()
25.821 - for t in P:
25.822 - t.join()
25.823 - C.join()
25.824 -
25.825 -if __name__ == '__main__':
25.826 - _test()
25.827 -
25.828 -# vim: tabstop=4 expandtab shiftwidth=4
27.1 --- a/lib/types.py Sat Dec 03 17:11:59 2016 +0100
27.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
27.3 @@ -1,70 +0,0 @@
27.4 -#!/usr/bin/env python
27.5 -
27.6 -"""
27.7 -Type objects.
27.8 -
27.9 -Copyright (C) 2012, 2015 Paul Boddie <paul@boddie.org.uk>
27.10 -
27.11 -This program is free software; you can redistribute it and/or modify it under
27.12 -the terms of the GNU General Public License as published by the Free Software
27.13 -Foundation; either version 3 of the License, or (at your option) any later
27.14 -version.
27.15 -
27.16 -This program is distributed in the hope that it will be useful, but WITHOUT
27.17 -ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
27.18 -FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
27.19 -details.
27.20 -
27.21 -You should have received a copy of the GNU General Public License along with
27.22 -this program. If not, see <http://www.gnu.org/licenses/>.
27.23 -"""
27.24 -
27.25 -# Built-in type duplication.
27.26 -
27.27 -type = type
27.28 -NoneType = NoneType
27.29 -NotImplementedType = NotImplementedType
27.30 -
27.31 -# Synonyms for built-in types.
27.32 -
27.33 -BooleanType = bool
27.34 -BufferType = buffer
27.35 -BuiltinFunctionType = function
27.36 -BuiltinMethodType = function
27.37 -ComplexType = complex
27.38 -DictType = dict
27.39 -EllipsisType = ellipsis
27.40 -FileType = file
27.41 -FloatType = float
27.42 -FunctionType = function
27.43 -IntType = int
27.44 -LambdaType = function
27.45 -ListType = list
27.46 -LongType = long
27.47 -MethodType = function
27.48 -ObjectType = object
27.49 -SliceType = slice
27.50 -StringType = str
27.51 -TupleType = tuple
27.52 -UnboundMethodType = function
27.53 -UnicodeType = unicode
27.54 -XRangeType = xrange
27.55 -
27.56 -StringTypes = (StringType, UnicodeType)
27.57 -
27.58 -# Types without special definitions.
27.59 -
27.60 -ClassType = object
27.61 -GeneratorType = object
27.62 -InstanceType = object
27.63 -ModuleType = object
27.64 -TracebackType = object
27.65 -
27.66 -# Implementation-specific definitions not relevant to micropython.
27.67 -
27.68 -DictProxyType = object
27.69 -FrameType = object
27.70 -GetSetDescriptorType = object
27.71 -MemberDescriptorType = object
27.72 -
27.73 -# vim: tabstop=4 expandtab shiftwidth=4