From 76afbd9aa41bf34f488a7a1e759622c7f1830cff Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Tue, 20 Aug 2002 17:29:29 +0000 Subject: Fix some endcase bugs in unicode rfind()/rindex() and endswith(). These were reported and fixed by Inyeol Lee in SF bug 595350. The endswith() bug was already fixed in 2.3, but this adds some more test cases. --- Lib/test/test_unicode.py | 6 ++++++ Misc/ACKS | 1 + Objects/stringobject.c | 2 +- Objects/unicodeobject.c | 6 +++--- 4 files changed, 11 insertions(+), 4 deletions(-) diff --git a/Lib/test/test_unicode.py b/Lib/test/test_unicode.py index f5f4245..4efa39c 100644 --- a/Lib/test/test_unicode.py +++ b/Lib/test/test_unicode.py @@ -107,6 +107,10 @@ test('find', u'abcdefghiabc', 9, u'abc', 1) test('find', u'abcdefghiabc', -1, u'def', 4) test('rfind', u'abcdefghiabc', 9, u'abc') +test('rfind', 'abcdefghiabc', 9, u'abc') +test('rfind', 'abcdefghiabc', 12, u'') +test('rfind', u'abcdefghiabc', 12, '') +test('rfind', u'abcdefghiabc', 12, u'') test('lower', u'HeLLo', u'hello') test('lower', u'hello', u'hello') @@ -241,6 +245,8 @@ test('endswith', u'helloworld', False, u'lowo', 4, 7) test('endswith', u'helloworld', False, u'lowo', 3, 8) test('endswith', u'ab', False, u'ab', 0, 1) test('endswith', u'ab', False, u'ab', 0, 0) +test('endswith', 'helloworld', True, u'd') +test('endswith', 'helloworld', False, u'l') test('expandtabs', u'abc\rab\tdef\ng\thi', u'abc\rab def\ng hi') test('expandtabs', u'abc\rab\tdef\ng\thi', u'abc\rab def\ng hi', 8) diff --git a/Misc/ACKS b/Misc/ACKS index 13a797b..b77244e 100644 --- a/Misc/ACKS +++ b/Misc/ACKS @@ -292,6 +292,7 @@ Soren Larsen Piers Lauder Chris Lawrence Christopher Lee +Inyeol Lee Luc Lefebvre Kip Lehman Marc-Andre Lemburg diff --git a/Objects/stringobject.c b/Objects/stringobject.c index 7a48627..9f41317 100644 --- a/Objects/stringobject.c +++ b/Objects/stringobject.c @@ -1536,7 +1536,7 @@ string_find_internal(PyStringObject *self, PyObject *args, int dir) } #ifdef Py_USING_UNICODE else if (PyUnicode_Check(subobj)) - return PyUnicode_Find((PyObject *)self, subobj, i, last, 1); + return PyUnicode_Find((PyObject *)self, subobj, i, last, dir); #endif else if (PyObject_AsCharBuffer(subobj, &sub, &n)) return -2; diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index 4ac12a0..b264936 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -2891,9 +2891,6 @@ int findstring(PyUnicodeObject *self, if (start < 0) start = 0; - if (substring->length == 0) - return start; - if (end > self->length) end = self->length; if (end < 0) @@ -2901,6 +2898,9 @@ int findstring(PyUnicodeObject *self, if (end < 0) end = 0; + if (substring->length == 0) + return (direction > 0) ? start : end; + end -= substring->length; if (direction < 0) { -- cgit v0.12 root/Lib/compiler/misc.py
blob: b4bbe78050c261b60ef44f51f10bba3422a8b3e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import types

def flatten(tup):
    elts = []
    for elt in tup:
        if type(elt) == types.TupleType:
            elts = elts + flatten(elt)
        else:
            elts.append(elt)
    return elts

class Set:
    def __init__(self):
        self.elts = {}
    def __len__(self):
        return len(self.elts)
    def __contains__(self, elt):
        return self.elts.has_key(elt)
    def add(self, elt):
        self.elts[elt] = elt
    def elements(self):
        return self.elts.keys()
    def has_elt(self, elt):
        return self.elts.has_key(elt)
    def remove(self, elt):
        del self.elts[elt]
    def copy(self):
        c = Set()
        c.elts.update(self.elts)
        return c

class Stack:
    def __init__(self):
        self.stack = []
        self.pop = self.stack.pop
    def __len__(self):
        return len(self.stack)
    def push(self, elt):
        self.stack.append(elt)
    def top(self):
        return self.stack[-1]
    def __getitem__(self, index): # needed by visitContinue()
        return self.stack[index]

MANGLE_LEN = 256 # magic constant from compile.c

def mangle(name, klass):
    if not name.startswith('__'):
        return name
    if len(name) + 2 >= MANGLE_LEN:
        return name
    if name.endswith('__'):
        return name
    try:
        i = 0
        while klass[i] == '_':
            i = i + 1
    except IndexError:
        return name
    klass = klass[i:]

    tlen = len(klass) + len(name)
    if tlen > MANGLE_LEN:
        klass = klass[:MANGLE_LEN-tlen]

    return "_%s%s" % (klass, name)

def set_filename(filename, tree):
    """Set the filename attribute to filename on every node in tree"""
    worklist = [tree]
    while worklist:
        node = worklist.pop(0)
        node.filename = filename
        worklist.extend(node.getChildNodes())