second commit

This commit is contained in:
2024-12-27 22:31:23 +09:00
parent 2353324570
commit 10a0f110ca
8819 changed files with 1307198 additions and 28 deletions

View File

@ -0,0 +1,72 @@
"""
A resurrection of some old functions from Python 2 for use in Python 3. These
should be used sparingly, to help with porting efforts, since code using them
is no longer standard Python 3 code.
This module provides the following:
1. Implementations of these builtin functions which have no equivalent on Py3:
- apply
- chr
- cmp
- execfile
2. Aliases:
- intern <- sys.intern
- raw_input <- input
- reduce <- functools.reduce
- reload <- imp.reload
- unichr <- chr
- unicode <- str
- xrange <- range
3. List-producing versions of the corresponding Python 3 iterator-producing functions:
- filter
- map
- range
- zip
4. Forward-ported Py2 types:
- basestring
- dict
- str
- long
- unicode
"""
from future.utils import PY3
from past.builtins.noniterators import (filter, map, range, reduce, zip)
# from past.builtins.misc import (ascii, hex, input, oct, open)
if PY3:
from past.types import (basestring,
olddict as dict,
oldstr as str,
long,
unicode)
else:
from __builtin__ import (basestring, dict, str, long, unicode)
from past.builtins.misc import (apply, chr, cmp, execfile, intern, oct,
raw_input, reload, unichr, unicode, xrange)
from past import utils
if utils.PY3:
# We only import names that shadow the builtins on Py3. No other namespace
# pollution on Py3.
# Only shadow builtins on Py3; no new names
__all__ = ['filter', 'map', 'range', 'reduce', 'zip',
'basestring', 'dict', 'str', 'long', 'unicode',
'apply', 'chr', 'cmp', 'execfile', 'intern', 'raw_input',
'reload', 'unichr', 'xrange'
]
else:
# No namespace pollution on Py2
__all__ = []

View File

@ -0,0 +1,162 @@
from __future__ import unicode_literals
import inspect
import sys
import math
import numbers
from future.utils import PY2, PY3, exec_
if PY2:
from collections import Mapping
else:
from collections.abc import Mapping
if PY3:
import builtins
from collections.abc import Mapping
def apply(f, *args, **kw):
return f(*args, **kw)
from past.builtins import str as oldstr
def chr(i):
"""
Return a byte-string of one character with ordinal i; 0 <= i <= 256
"""
return oldstr(bytes((i,)))
def cmp(x, y):
"""
cmp(x, y) -> integer
Return negative if x<y, zero if x==y, positive if x>y.
Python2 had looser comparison allowing cmp None and non Numerical types and collections.
Try to match the old behavior
"""
if isinstance(x, set) and isinstance(y, set):
raise TypeError('cannot compare sets using cmp()',)
try:
if isinstance(x, numbers.Number) and math.isnan(x):
if not isinstance(y, numbers.Number):
raise TypeError('cannot compare float("nan"), {type_y} with cmp'.format(type_y=type(y)))
if isinstance(y, int):
return 1
else:
return -1
if isinstance(y, numbers.Number) and math.isnan(y):
if not isinstance(x, numbers.Number):
raise TypeError('cannot compare {type_x}, float("nan") with cmp'.format(type_x=type(x)))
if isinstance(x, int):
return -1
else:
return 1
return (x > y) - (x < y)
except TypeError:
if x == y:
return 0
type_order = [
type(None),
numbers.Number,
dict, list,
set,
(str, bytes),
]
x_type_index = y_type_index = None
for i, type_match in enumerate(type_order):
if isinstance(x, type_match):
x_type_index = i
if isinstance(y, type_match):
y_type_index = i
if cmp(x_type_index, y_type_index) == 0:
if isinstance(x, bytes) and isinstance(y, str):
return cmp(x.decode('ascii'), y)
if isinstance(y, bytes) and isinstance(x, str):
return cmp(x, y.decode('ascii'))
elif isinstance(x, list):
# if both arguments are lists take the comparison of the first non equal value
for x_elem, y_elem in zip(x, y):
elem_cmp_val = cmp(x_elem, y_elem)
if elem_cmp_val != 0:
return elem_cmp_val
# if all elements are equal, return equal/0
return 0
elif isinstance(x, dict):
if len(x) != len(y):
return cmp(len(x), len(y))
else:
x_key = min(a for a in x if a not in y or x[a] != y[a])
y_key = min(b for b in y if b not in x or x[b] != y[b])
if x_key != y_key:
return cmp(x_key, y_key)
else:
return cmp(x[x_key], y[y_key])
return cmp(x_type_index, y_type_index)
from sys import intern
def oct(number):
"""oct(number) -> string
Return the octal representation of an integer
"""
return '0' + builtins.oct(number)[2:]
raw_input = input
# imp was deprecated in python 3.6
if sys.version_info >= (3, 6):
from importlib import reload
else:
# for python2, python3 <= 3.4
from imp import reload
unicode = str
unichr = chr
xrange = range
else:
import __builtin__
from collections import Mapping
apply = __builtin__.apply
chr = __builtin__.chr
cmp = __builtin__.cmp
execfile = __builtin__.execfile
intern = __builtin__.intern
oct = __builtin__.oct
raw_input = __builtin__.raw_input
reload = __builtin__.reload
unicode = __builtin__.unicode
unichr = __builtin__.unichr
xrange = __builtin__.xrange
if PY3:
def execfile(filename, myglobals=None, mylocals=None):
"""
Read and execute a Python script from a file in the given namespaces.
The globals and locals are dictionaries, defaulting to the current
globals and locals. If only globals is given, locals defaults to it.
"""
if myglobals is None:
# There seems to be no alternative to frame hacking here.
caller_frame = inspect.stack()[1]
myglobals = caller_frame[0].f_globals
mylocals = caller_frame[0].f_locals
elif mylocals is None:
# Only if myglobals is given do we set mylocals to it.
mylocals = myglobals
if not isinstance(myglobals, Mapping):
raise TypeError('globals must be a mapping')
if not isinstance(mylocals, Mapping):
raise TypeError('locals must be a mapping')
with open(filename, "rb") as fin:
source = fin.read()
code = compile(source, filename, "exec")
exec_(code, myglobals, mylocals)
if PY3:
__all__ = ['apply', 'chr', 'cmp', 'execfile', 'intern', 'raw_input',
'reload', 'unichr', 'unicode', 'xrange']
else:
__all__ = []

View File

@ -0,0 +1,272 @@
"""
This module is designed to be used as follows::
from past.builtins.noniterators import filter, map, range, reduce, zip
And then, for example::
assert isinstance(range(5), list)
The list-producing functions this brings in are::
- ``filter``
- ``map``
- ``range``
- ``reduce``
- ``zip``
"""
from __future__ import division, absolute_import, print_function
from itertools import chain, starmap
import itertools # since zip_longest doesn't exist on Py2
from past.types import basestring
from past.utils import PY3
def flatmap(f, items):
return chain.from_iterable(map(f, items))
if PY3:
import builtins
# list-producing versions of the major Python iterating functions
def oldfilter(*args):
"""
filter(function or None, sequence) -> list, tuple, or string
Return those items of sequence for which function(item) is true.
If function is None, return the items that are true. If sequence
is a tuple or string, return the same type, else return a list.
"""
mytype = type(args[1])
if isinstance(args[1], basestring):
return mytype().join(builtins.filter(*args))
elif isinstance(args[1], (tuple, list)):
return mytype(builtins.filter(*args))
else:
# Fall back to list. Is this the right thing to do?
return list(builtins.filter(*args))
# This is surprisingly difficult to get right. For example, the
# solutions here fail with the test cases in the docstring below:
# http://stackoverflow.com/questions/8072755/
def oldmap(func, *iterables):
"""
map(function, sequence[, sequence, ...]) -> list
Return a list of the results of applying the function to the
items of the argument sequence(s). If more than one sequence is
given, the function is called with an argument list consisting of
the corresponding item of each sequence, substituting None for
missing values when not all sequences have the same length. If
the function is None, return a list of the items of the sequence
(or a list of tuples if more than one sequence).
Test cases:
>>> oldmap(None, 'hello world')
['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd']
>>> oldmap(None, range(4))
[0, 1, 2, 3]
More test cases are in test_past.test_builtins.
"""
zipped = itertools.zip_longest(*iterables)
l = list(zipped)
if len(l) == 0:
return []
if func is None:
result = l
else:
result = list(starmap(func, l))
# Inspect to see whether it's a simple sequence of tuples
try:
if max([len(item) for item in result]) == 1:
return list(chain.from_iterable(result))
# return list(flatmap(func, result))
except TypeError as e:
# Simple objects like ints have no len()
pass
return result
############################
### For reference, the source code for Py2.7 map function:
# static PyObject *
# builtin_map(PyObject *self, PyObject *args)
# {
# typedef struct {
# PyObject *it; /* the iterator object */
# int saw_StopIteration; /* bool: did the iterator end? */
# } sequence;
#
# PyObject *func, *result;
# sequence *seqs = NULL, *sqp;
# Py_ssize_t n, len;
# register int i, j;
#
# n = PyTuple_Size(args);
# if (n < 2) {
# PyErr_SetString(PyExc_TypeError,
# "map() requires at least two args");
# return NULL;
# }
#
# func = PyTuple_GetItem(args, 0);
# n--;
#
# if (func == Py_None) {
# if (PyErr_WarnPy3k("map(None, ...) not supported in 3.x; "
# "use list(...)", 1) < 0)
# return NULL;
# if (n == 1) {
# /* map(None, S) is the same as list(S). */
# return PySequence_List(PyTuple_GetItem(args, 1));
# }
# }
#
# /* Get space for sequence descriptors. Must NULL out the iterator
# * pointers so that jumping to Fail_2 later doesn't see trash.
# */
# if ((seqs = PyMem_NEW(sequence, n)) == NULL) {
# PyErr_NoMemory();
# return NULL;
# }
# for (i = 0; i < n; ++i) {
# seqs[i].it = (PyObject*)NULL;
# seqs[i].saw_StopIteration = 0;
# }
#
# /* Do a first pass to obtain iterators for the arguments, and set len
# * to the largest of their lengths.
# */
# len = 0;
# for (i = 0, sqp = seqs; i < n; ++i, ++sqp) {
# PyObject *curseq;
# Py_ssize_t curlen;
#
# /* Get iterator. */
# curseq = PyTuple_GetItem(args, i+1);
# sqp->it = PyObject_GetIter(curseq);
# if (sqp->it == NULL) {
# static char errmsg[] =
# "argument %d to map() must support iteration";
# char errbuf[sizeof(errmsg) + 25];
# PyOS_snprintf(errbuf, sizeof(errbuf), errmsg, i+2);
# PyErr_SetString(PyExc_TypeError, errbuf);
# goto Fail_2;
# }
#
# /* Update len. */
# curlen = _PyObject_LengthHint(curseq, 8);
# if (curlen > len)
# len = curlen;
# }
#
# /* Get space for the result list. */
# if ((result = (PyObject *) PyList_New(len)) == NULL)
# goto Fail_2;
#
# /* Iterate over the sequences until all have stopped. */
# for (i = 0; ; ++i) {
# PyObject *alist, *item=NULL, *value;
# int numactive = 0;
#
# if (func == Py_None && n == 1)
# alist = NULL;
# else if ((alist = PyTuple_New(n)) == NULL)
# goto Fail_1;
#
# for (j = 0, sqp = seqs; j < n; ++j, ++sqp) {
# if (sqp->saw_StopIteration) {
# Py_INCREF(Py_None);
# item = Py_None;
# }
# else {
# item = PyIter_Next(sqp->it);
# if (item)
# ++numactive;
# else {
# if (PyErr_Occurred()) {
# Py_XDECREF(alist);
# goto Fail_1;
# }
# Py_INCREF(Py_None);
# item = Py_None;
# sqp->saw_StopIteration = 1;
# }
# }
# if (alist)
# PyTuple_SET_ITEM(alist, j, item);
# else
# break;
# }
#
# if (!alist)
# alist = item;
#
# if (numactive == 0) {
# Py_DECREF(alist);
# break;
# }
#
# if (func == Py_None)
# value = alist;
# else {
# value = PyEval_CallObject(func, alist);
# Py_DECREF(alist);
# if (value == NULL)
# goto Fail_1;
# }
# if (i >= len) {
# int status = PyList_Append(result, value);
# Py_DECREF(value);
# if (status < 0)
# goto Fail_1;
# }
# else if (PyList_SetItem(result, i, value) < 0)
# goto Fail_1;
# }
#
# if (i < len && PyList_SetSlice(result, i, len, NULL) < 0)
# goto Fail_1;
#
# goto Succeed;
#
# Fail_1:
# Py_DECREF(result);
# Fail_2:
# result = NULL;
# Succeed:
# assert(seqs);
# for (i = 0; i < n; ++i)
# Py_XDECREF(seqs[i].it);
# PyMem_DEL(seqs);
# return result;
# }
def oldrange(*args, **kwargs):
return list(builtins.range(*args, **kwargs))
def oldzip(*args, **kwargs):
return list(builtins.zip(*args, **kwargs))
filter = oldfilter
map = oldmap
range = oldrange
from functools import reduce
zip = oldzip
__all__ = ['filter', 'map', 'range', 'reduce', 'zip']
else:
import __builtin__
# Python 2-builtin ranges produce lists
filter = __builtin__.filter
map = __builtin__.map
range = __builtin__.range
reduce = __builtin__.reduce
zip = __builtin__.zip
__all__ = []