Ignore:
Timestamp:
Mar 19, 2014, 11:31:01 PM (11 years ago)
Author:
dmik
Message:

python: Merge vendor 2.7.6 to trunk.

Location:
python/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • python/trunk

  • python/trunk/Modules/_sre.c

    r2 r391  
    173173#if defined(HAVE_UNICODE)
    174174
    175 #define SRE_UNI_IS_DIGIT(ch) Py_UNICODE_ISDIGIT((Py_UNICODE)(ch))
     175#define SRE_UNI_IS_DIGIT(ch) Py_UNICODE_ISDECIMAL((Py_UNICODE)(ch))
    176176#define SRE_UNI_IS_SPACE(ch) Py_UNICODE_ISSPACE((Py_UNICODE)(ch))
    177177#define SRE_UNI_IS_LINEBREAK(ch) Py_UNICODE_ISLINEBREAK((Py_UNICODE)(ch))
     
    273273        void* stack;
    274274        cursize = minsize+minsize/4+1024;
    275         TRACE(("allocate/grow stack %d\n", cursize));
     275        TRACE(("allocate/grow stack %" PY_FORMAT_SIZE_T "d\n", cursize));
    276276        stack = PyMem_REALLOC(state->data_stack, cursize);
    277277        if (!stack) {
     
    460460            else {
    461461                /* <CHARSET> <bitmap> (32 bits per code word) */
    462                 if (ch < 256 && (set[ch >> 5] & (1 << (ch & 31))))
     462                if (ch < 256 && (set[ch >> 5] & (1u << (ch & 31))))
    463463                    return ok;
    464464                set += 8;
     
    499499                set += 64;
    500500                if (block >=0 &&
    501                     (set[block*8 + ((ch & 255)>>5)] & (1 << (ch & 31))))
     501                    (set[block*8 + ((ch & 255)>>5)] & (1u << (ch & 31))))
    502502                    return ok;
    503503                set += count*8;
     
    525525
    526526    /* adjust end */
    527     if (maxcount < end - ptr && maxcount != 65535)
     527    if (maxcount < end - ptr && maxcount != SRE_MAXREPEAT)
    528528        end = ptr + maxcount;
    529529
     
    593593                break;
    594594        }
    595         TRACE(("|%p|%p|COUNT %d\n", pattern, ptr,
     595        TRACE(("|%p|%p|COUNT %" PY_FORMAT_SIZE_T "d\n", pattern, ptr,
    596596               (SRE_CHAR*) state->ptr - ptr));
    597597        return (SRE_CHAR*) state->ptr - ptr;
    598598    }
    599599
    600     TRACE(("|%p|%p|COUNT %d\n", pattern, ptr, ptr - (SRE_CHAR*) state->ptr));
     600    TRACE(("|%p|%p|COUNT %" PY_FORMAT_SIZE_T "d\n", pattern, ptr,
     601           ptr - (SRE_CHAR*) state->ptr));
    601602    return ptr - (SRE_CHAR*) state->ptr;
    602603}
     
    685686do { \
    686687    alloc_pos = state->data_stack_base; \
    687     TRACE(("allocating %s in %d (%d)\n", \
     688    TRACE(("allocating %s in %" PY_FORMAT_SIZE_T "d " \
     689           "(%" PY_FORMAT_SIZE_T "d)\n", \
    688690           SFY(type), alloc_pos, sizeof(type))); \
    689     if (state->data_stack_size < alloc_pos+sizeof(type)) { \
     691    if (sizeof(type) > state->data_stack_size - alloc_pos) { \
    690692        int j = data_stack_grow(state, sizeof(type)); \
    691693        if (j < 0) return j; \
     
    699701#define DATA_STACK_LOOKUP_AT(state, type, ptr, pos) \
    700702do { \
    701     TRACE(("looking up %s at %d\n", SFY(type), pos)); \
     703    TRACE(("looking up %s at %" PY_FORMAT_SIZE_T "d\n", SFY(type), pos)); \
    702704    ptr = (type*)(state->data_stack+pos); \
    703705} while (0)
     
    705707#define DATA_STACK_PUSH(state, data, size) \
    706708do { \
    707     TRACE(("copy data in %p to %d (%d)\n", \
     709    TRACE(("copy data in %p to %" PY_FORMAT_SIZE_T "d " \
     710           "(%" PY_FORMAT_SIZE_T "d)\n", \
    708711           data, state->data_stack_base, size)); \
    709     if (state->data_stack_size < state->data_stack_base+size) { \
     712    if (size > state->data_stack_size - state->data_stack_base) { \
    710713        int j = data_stack_grow(state, size); \
    711714        if (j < 0) return j; \
     
    719722#define DATA_STACK_POP(state, data, size, discard) \
    720723do { \
    721     TRACE(("copy data to %p from %d (%d)\n", \
     724    TRACE(("copy data to %p from %" PY_FORMAT_SIZE_T "d " \
     725           "(%" PY_FORMAT_SIZE_T "d)\n", \
    722726           data, state->data_stack_base-size, size)); \
    723727    memcpy(data, state->data_stack+state->data_stack_base-size, size); \
     
    728732#define DATA_STACK_POP_DISCARD(state, size) \
    729733do { \
    730     TRACE(("discard data from %d (%d)\n", \
     734    TRACE(("discard data from %" PY_FORMAT_SIZE_T "d " \
     735           "(%" PY_FORMAT_SIZE_T "d)\n", \
    731736           state->data_stack_base-size, size)); \
    732737    state->data_stack_base -= size; \
     
    832837        /* <INFO> <1=skip> <2=flags> <3=min> ... */
    833838        if (ctx->pattern[3] && (end - ctx->ptr) < ctx->pattern[3]) {
    834             TRACE(("reject (got %d chars, need %d)\n",
    835                    (end - ctx->ptr), ctx->pattern[3]));
     839            TRACE(("reject (got %" PY_FORMAT_SIZE_T "d chars, "
     840                   "need %" PY_FORMAT_SIZE_T "d)\n",
     841                   (end - ctx->ptr), (Py_ssize_t) ctx->pattern[3]));
    836842            RETURN_FAILURE;
    837843        }
     
    10291035                   ctx->pattern[1], ctx->pattern[2]));
    10301036
    1031             if (ctx->ptr + ctx->pattern[1] > end)
     1037            if ((Py_ssize_t) ctx->pattern[1] > end - ctx->ptr)
    10321038                RETURN_FAILURE; /* cannot match */
    10331039
     
    11121118                   ctx->pattern[1], ctx->pattern[2]));
    11131119
    1114             if (ctx->ptr + ctx->pattern[1] > end)
     1120            if ((Py_ssize_t) ctx->pattern[1] > end - ctx->ptr)
    11151121                RETURN_FAILURE; /* cannot match */
    11161122
     
    11401146                /* general case */
    11411147                LASTMARK_SAVE();
    1142                 while ((Py_ssize_t)ctx->pattern[2] == 65535
     1148                while ((Py_ssize_t)ctx->pattern[2] == SRE_MAXREPEAT
    11431149                       || ctx->count <= (Py_ssize_t)ctx->pattern[2]) {
    11441150                    state->ptr = ctx->ptr;
     
    12081214            ctx->count = ctx->u.rep->count+1;
    12091215
    1210             TRACE(("|%p|%p|MAX_UNTIL %d\n", ctx->pattern,
     1216            TRACE(("|%p|%p|MAX_UNTIL %" PY_FORMAT_SIZE_T "d\n", ctx->pattern,
    12111217                   ctx->ptr, ctx->count));
    12121218
    1213             if (ctx->count < ctx->u.rep->pattern[1]) {
     1219            if (ctx->count < (Py_ssize_t) ctx->u.rep->pattern[1]) {
    12141220                /* not enough matches */
    12151221                ctx->u.rep->count = ctx->count;
     
    12251231            }
    12261232
    1227             if ((ctx->count < ctx->u.rep->pattern[2] ||
    1228                 ctx->u.rep->pattern[2] == 65535) &&
     1233            if ((ctx->count < (Py_ssize_t) ctx->u.rep->pattern[2] ||
     1234                ctx->u.rep->pattern[2] == SRE_MAXREPEAT) &&
    12291235                state->ptr != ctx->u.rep->last_ptr) {
    12301236                /* we may have enough matches, but if we can
     
    12711277            ctx->count = ctx->u.rep->count+1;
    12721278
    1273             TRACE(("|%p|%p|MIN_UNTIL %d %p\n", ctx->pattern,
     1279            TRACE(("|%p|%p|MIN_UNTIL %" PY_FORMAT_SIZE_T "d %p\n", ctx->pattern,
    12741280                   ctx->ptr, ctx->count, ctx->u.rep->pattern));
    12751281
    1276             if (ctx->count < ctx->u.rep->pattern[1]) {
     1282            if (ctx->count < (Py_ssize_t) ctx->u.rep->pattern[1]) {
    12771283                /* not enough matches */
    12781284                ctx->u.rep->count = ctx->count;
     
    13031309            LASTMARK_RESTORE();
    13041310
    1305             if (ctx->count >= ctx->u.rep->pattern[2]
    1306                 && ctx->u.rep->pattern[2] != 65535)
     1311            if ((ctx->count >= (Py_ssize_t) ctx->u.rep->pattern[2]
     1312                && ctx->u.rep->pattern[2] != SRE_MAXREPEAT) ||
     1313                state->ptr == ctx->u.rep->last_ptr)
    13071314                RETURN_FAILURE;
    13081315
    13091316            ctx->u.rep->count = ctx->count;
     1317            /* zero-width match protection */
     1318            DATA_PUSH(&ctx->u.rep->last_ptr);
     1319            ctx->u.rep->last_ptr = state->ptr;
    13101320            DO_JUMP(JUMP_MIN_UNTIL_3,jump_min_until_3,
    13111321                    ctx->u.rep->pattern+3);
     1322            DATA_POP(&ctx->u.rep->last_ptr);
    13121323            if (ret) {
    13131324                RETURN_ON_ERROR(ret);
     
    14791490            goto jump_assert_not;
    14801491        case JUMP_NONE:
    1481             TRACE(("|%p|%p|RETURN %d\n", ctx->pattern, ctx->ptr, ret));
     1492            TRACE(("|%p|%p|RETURN %" PY_FORMAT_SIZE_T "d\n", ctx->pattern,
     1493                   ctx->ptr, ret));
    14821494            break;
    14831495    }
     
    15281540    }
    15291541
    1530     TRACE(("prefix = %p %d %d\n", prefix, prefix_len, prefix_skip));
     1542    TRACE(("prefix = %p %" PY_FORMAT_SIZE_T "d %" PY_FORMAT_SIZE_T "d\n",
     1543           prefix, prefix_len, prefix_skip));
    15311544    TRACE(("charset = %p\n", charset));
    15321545
     
    16371650sre_codesize(PyObject* self, PyObject *unused)
    16381651{
    1639     return Py_BuildValue("l", sizeof(SRE_CODE));
     1652    return PyInt_FromSize_t(sizeof(SRE_CODE));
    16401653}
    16411654
     
    16871700        /* unicode strings doesn't always support the buffer interface */
    16881701        ptr = (void*) PyUnicode_AS_DATA(string);
    1689         bytes = PyUnicode_GET_DATA_SIZE(string);
     1702        /* bytes = PyUnicode_GET_DATA_SIZE(string); */
    16901703        size = PyUnicode_GET_SIZE(string);
    16911704        charsize = sizeof(Py_UNICODE);
     
    24492462
    24502463    if (subn)
    2451         return Py_BuildValue("Ni", item, n);
     2464        return Py_BuildValue("Nn", item, n);
    24522465
    24532466    return item;
     
    25472560"search(string[, pos[, endpos]]) --> match object or None.\n\
    25482561    Scan through string looking for a match, and return a corresponding\n\
    2549     MatchObject instance. Return None if no position in the string matches.");
     2562    match object instance. Return None if no position in the string matches.");
    25502563
    25512564PyDoc_STRVAR(pattern_split_doc,
     
    25992612};
    26002613
    2601 static PyObject*
    2602 pattern_getattr(PatternObject* self, char* name)
    2603 {
    2604     PyObject* res;
    2605 
    2606     res = Py_FindMethod(pattern_methods, (PyObject*) self, name);
    2607 
    2608     if (res)
    2609         return res;
    2610 
    2611     PyErr_Clear();
    2612 
    2613     /* attributes */
    2614     if (!strcmp(name, "pattern")) {
    2615         Py_INCREF(self->pattern);
    2616         return self->pattern;
    2617     }
    2618 
    2619     if (!strcmp(name, "flags"))
    2620         return Py_BuildValue("i", self->flags);
    2621 
    2622     if (!strcmp(name, "groups"))
    2623         return Py_BuildValue("i", self->groups);
    2624 
    2625     if (!strcmp(name, "groupindex") && self->groupindex) {
    2626         Py_INCREF(self->groupindex);
    2627         return self->groupindex;
    2628     }
    2629 
    2630     PyErr_SetString(PyExc_AttributeError, name);
    2631     return NULL;
    2632 }
     2614#define PAT_OFF(x) offsetof(PatternObject, x)
     2615static PyMemberDef pattern_members[] = {
     2616    {"pattern",    T_OBJECT,    PAT_OFF(pattern),       READONLY},
     2617    {"flags",      T_INT,       PAT_OFF(flags),         READONLY},
     2618    {"groups",     T_PYSSIZET,  PAT_OFF(groups),        READONLY},
     2619    {"groupindex", T_OBJECT,    PAT_OFF(groupindex),    READONLY},
     2620    {NULL}  /* Sentinel */
     2621};
    26332622
    26342623statichere PyTypeObject Pattern_Type = {
     
    26372626    sizeof(PatternObject), sizeof(SRE_CODE),
    26382627    (destructor)pattern_dealloc, /*tp_dealloc*/
    2639     0, /*tp_print*/
    2640     (getattrfunc)pattern_getattr, /*tp_getattr*/
     2628    0,                                  /* tp_print */
     2629    0,                                  /* tp_getattrn */
    26412630    0,                                  /* tp_setattr */
    26422631    0,                                  /* tp_compare */
     
    26512640    0,                                  /* tp_setattro */
    26522641    0,                                  /* tp_as_buffer */
    2653     Py_TPFLAGS_HAVE_WEAKREFS,           /* tp_flags */
     2642    Py_TPFLAGS_DEFAULT,                 /* tp_flags */
    26542643    pattern_doc,                        /* tp_doc */
    26552644    0,                                  /* tp_traverse */
     
    26572646    0,                                  /* tp_richcompare */
    26582647    offsetof(PatternObject, weakreflist),       /* tp_weaklistoffset */
     2648    0,                                  /* tp_iter */
     2649    0,                                  /* tp_iternext */
     2650    pattern_methods,                    /* tp_methods */
     2651    pattern_members,                    /* tp_members */
    26592652};
    26602653
     
    26962689        unsigned long value = PyInt_Check(o) ? (unsigned long)PyInt_AsLong(o)
    26972690                                              : PyLong_AsUnsignedLong(o);
     2691        if (value == (unsigned long)-1 && PyErr_Occurred()) {
     2692            if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
     2693                PyErr_SetString(PyExc_OverflowError,
     2694                                "regular expression code size limit exceeded");
     2695            }
     2696            break;
     2697        }
    26982698        self->code[i] = (SRE_CODE) value;
    26992699        if ((unsigned long) self->code[i] != value) {
     
    27652765#define VTRACE(v) printf v
    27662766#else
    2767 #define VTRACE(v)
     2767#define VTRACE(v) do {} while(0)  /* do nothing */
    27682768#endif
    27692769
     
    27932793        VTRACE(("%lu (skip to %p)\n",                   \
    27942794               (unsigned long)skip, code+skip));        \
    2795         if (code+skip-adj < code || code+skip-adj > end)\
     2795        if (skip-adj > end-code)                        \
    27962796            FAIL;                                       \
    27972797        code++;                                         \
     
    28262826        case SRE_OP_CHARSET:
    28272827            offset = 32/sizeof(SRE_CODE); /* 32-byte bitmap */
    2828             if (code+offset < code || code+offset > end)
     2828            if (offset > end-code)
    28292829                FAIL;
    28302830            code += offset;
     
    28342834            GET_ARG; /* Number of blocks */
    28352835            offset = 256/sizeof(SRE_CODE); /* 256-byte table */
    2836             if (code+offset < code || code+offset > end)
     2836            if (offset > end-code)
    28372837                FAIL;
    28382838            /* Make sure that each byte points to a valid block */
     
    28432843            code += offset;
    28442844            offset = arg * 32/sizeof(SRE_CODE); /* 32-byte bitmap times arg */
    2845             if (code+offset < code || code+offset > end)
     2845            if (offset > end-code)
    28462846                FAIL;
    28472847            code += offset;
     
    29682968                   If SRE_INFO_PREFIX or SRE_INFO_CHARSET is in the flags,
    29692969                   more follows. */
    2970                 SRE_CODE flags, min, max, i;
     2970                SRE_CODE flags, i;
    29712971                SRE_CODE *newcode;
    29722972                GET_SKIP;
    29732973                newcode = code+skip-1;
    29742974                GET_ARG; flags = arg;
    2975                 GET_ARG; min = arg;
    2976                 GET_ARG; max = arg;
     2975                GET_ARG; /* min */
     2976                GET_ARG; /* max */
    29772977                /* Check that only valid flags are present */
    29782978                if ((flags & ~(SRE_INFO_PREFIX |
     
    29902990                /* Validate the prefix */
    29912991                if (flags & SRE_INFO_PREFIX) {
    2992                     SRE_CODE prefix_len, prefix_skip;
     2992                    SRE_CODE prefix_len;
    29932993                    GET_ARG; prefix_len = arg;
    2994                     GET_ARG; prefix_skip = arg;
     2994                    GET_ARG; /* prefix skip */
    29952995                    /* Here comes the prefix string */
    2996                     if (code+prefix_len < code || code+prefix_len > newcode)
     2996                    if (prefix_len > newcode-code)
    29972997                        FAIL;
    29982998                    code += prefix_len;
    29992999                    /* And here comes the overlap table */
    3000                     if (code+prefix_len < code || code+prefix_len > newcode)
     3000                    if (prefix_len > newcode-code)
    30013001                        FAIL;
    30023002                    /* Each overlap value should be < prefix_len */
     
    30563056                if (min > max)
    30573057                    FAIL;
    3058 #ifdef Py_UNICODE_WIDE
    3059                 if (max > 65535)
     3058                if (max > SRE_MAXREPEAT)
    30603059                    FAIL;
    3061 #endif
    30623060                if (!_validate_inner(code, code+skip-4, groups))
    30633061                    FAIL;
     
    30773075                if (min > max)
    30783076                    FAIL;
    3079 #ifdef Py_UNICODE_WIDE
    3080                 if (max > 65535)
     3077                if (max > SRE_MAXREPEAT)
    30813078                    FAIL;
    3082 #endif
    30833079                if (!_validate_inner(code, code+skip-3, groups))
    30843080                    FAIL;
     
    31313127               for a JUMP opcode preceding our skip target.
    31323128            */
    3133             if (skip >= 3 && code+skip-3 >= code &&
     3129            if (skip >= 3 && skip-3 < end-code &&
    31343130                code[skip-3] == SRE_OP_JUMP)
    31353131            {
     
    34073403
    34083404    /* mark is -1 if group is undefined */
    3409     return Py_BuildValue("i", self->mark[index*2]);
     3405    return PyInt_FromSsize_t(self->mark[index*2]);
    34103406}
    34113407
     
    34303426
    34313427    /* mark is -1 if group is undefined */
    3432     return Py_BuildValue("i", self->mark[index*2+1]);
     3428    return PyInt_FromSsize_t(self->mark[index*2+1]);
    34333429}
    34343430
     
    35633559}
    35643560
     3561PyDoc_STRVAR(match_doc,
     3562"The result of re.match() and re.search().\n\
     3563Match objects always have a boolean value of True.");
     3564
     3565PyDoc_STRVAR(match_group_doc,
     3566"group([group1, ...]) -> str or tuple.\n\
     3567    Return subgroup(s) of the match by indices or names.\n\
     3568    For 0 returns the entire match.");
     3569
     3570PyDoc_STRVAR(match_start_doc,
     3571"start([group=0]) -> int.\n\
     3572    Return index of the start of the substring matched by group.");
     3573
     3574PyDoc_STRVAR(match_end_doc,
     3575"end([group=0]) -> int.\n\
     3576    Return index of the end of the substring matched by group.");
     3577
     3578PyDoc_STRVAR(match_span_doc,
     3579"span([group]) -> tuple.\n\
     3580    For MatchObject m, return the 2-tuple (m.start(group), m.end(group)).");
     3581
     3582PyDoc_STRVAR(match_groups_doc,
     3583"groups([default=None]) -> tuple.\n\
     3584    Return a tuple containing all the subgroups of the match, from 1.\n\
     3585    The default argument is used for groups\n\
     3586    that did not participate in the match");
     3587
     3588PyDoc_STRVAR(match_groupdict_doc,
     3589"groupdict([default=None]) -> dict.\n\
     3590    Return a dictionary containing all the named subgroups of the match,\n\
     3591    keyed by the subgroup name. The default argument is used for groups\n\
     3592    that did not participate in the match");
     3593
     3594PyDoc_STRVAR(match_expand_doc,
     3595"expand(template) -> str.\n\
     3596    Return the string obtained by doing backslash substitution\n\
     3597    on the string template, as done by the sub() method.");
     3598
    35653599static PyMethodDef match_methods[] = {
    3566     {"group", (PyCFunction) match_group, METH_VARARGS},
    3567     {"start", (PyCFunction) match_start, METH_VARARGS},
    3568     {"end", (PyCFunction) match_end, METH_VARARGS},
    3569     {"span", (PyCFunction) match_span, METH_VARARGS},
    3570     {"groups", (PyCFunction) match_groups, METH_VARARGS|METH_KEYWORDS},
    3571     {"groupdict", (PyCFunction) match_groupdict, METH_VARARGS|METH_KEYWORDS},
    3572     {"expand", (PyCFunction) match_expand, METH_O},
     3600    {"group", (PyCFunction) match_group, METH_VARARGS, match_group_doc},
     3601    {"start", (PyCFunction) match_start, METH_VARARGS, match_start_doc},
     3602    {"end", (PyCFunction) match_end, METH_VARARGS, match_end_doc},
     3603    {"span", (PyCFunction) match_span, METH_VARARGS, match_span_doc},
     3604    {"groups", (PyCFunction) match_groups, METH_VARARGS|METH_KEYWORDS,
     3605        match_groups_doc},
     3606    {"groupdict", (PyCFunction) match_groupdict, METH_VARARGS|METH_KEYWORDS,
     3607        match_groupdict_doc},
     3608    {"expand", (PyCFunction) match_expand, METH_O, match_expand_doc},
    35733609    {"__copy__", (PyCFunction) match_copy, METH_NOARGS},
    35743610    {"__deepcopy__", (PyCFunction) match_deepcopy, METH_O},
     
    35763612};
    35773613
    3578 static PyObject*
    3579 match_getattr(MatchObject* self, char* name)
    3580 {
    3581     PyObject* res;
    3582 
    3583     res = Py_FindMethod(match_methods, (PyObject*) self, name);
    3584     if (res)
    3585         return res;
    3586 
    3587     PyErr_Clear();
    3588 
    3589     if (!strcmp(name, "lastindex")) {
    3590         if (self->lastindex >= 0)
    3591             return Py_BuildValue("i", self->lastindex);
    3592         Py_INCREF(Py_None);
    3593         return Py_None;
    3594     }
    3595 
    3596     if (!strcmp(name, "lastgroup")) {
    3597         if (self->pattern->indexgroup && self->lastindex >= 0) {
    3598             PyObject* result = PySequence_GetItem(
    3599                 self->pattern->indexgroup, self->lastindex
    3600                 );
    3601             if (result)
    3602                 return result;
    3603             PyErr_Clear();
    3604         }
    3605         Py_INCREF(Py_None);
    3606         return Py_None;
    3607     }
    3608 
    3609     if (!strcmp(name, "string")) {
    3610         if (self->string) {
    3611             Py_INCREF(self->string);
    3612             return self->string;
    3613         } else {
    3614             Py_INCREF(Py_None);
    3615             return Py_None;
    3616         }
    3617     }
    3618 
    3619     if (!strcmp(name, "regs")) {
    3620         if (self->regs) {
    3621             Py_INCREF(self->regs);
    3622             return self->regs;
    3623         } else
    3624             return match_regs(self);
    3625     }
    3626 
    3627     if (!strcmp(name, "re")) {
    3628         Py_INCREF(self->pattern);
    3629         return (PyObject*) self->pattern;
    3630     }
    3631 
    3632     if (!strcmp(name, "pos"))
    3633         return Py_BuildValue("i", self->pos);
    3634 
    3635     if (!strcmp(name, "endpos"))
    3636         return Py_BuildValue("i", self->endpos);
    3637 
    3638     PyErr_SetString(PyExc_AttributeError, name);
    3639     return NULL;
    3640 }
     3614static PyObject *
     3615match_lastindex_get(MatchObject *self)
     3616{
     3617    if (self->lastindex >= 0)
     3618        return PyInt_FromSsize_t(self->lastindex);
     3619    Py_INCREF(Py_None);
     3620    return Py_None;
     3621}
     3622
     3623static PyObject *
     3624match_lastgroup_get(MatchObject *self)
     3625{
     3626    if (self->pattern->indexgroup && self->lastindex >= 0) {
     3627        PyObject* result = PySequence_GetItem(
     3628            self->pattern->indexgroup, self->lastindex
     3629            );
     3630        if (result)
     3631            return result;
     3632        PyErr_Clear();
     3633    }
     3634    Py_INCREF(Py_None);
     3635    return Py_None;
     3636}
     3637
     3638static PyObject *
     3639match_regs_get(MatchObject *self)
     3640{
     3641    if (self->regs) {
     3642        Py_INCREF(self->regs);
     3643        return self->regs;
     3644    } else
     3645        return match_regs(self);
     3646}
     3647
     3648static PyGetSetDef match_getset[] = {
     3649    {"lastindex", (getter)match_lastindex_get, (setter)NULL},
     3650    {"lastgroup", (getter)match_lastgroup_get, (setter)NULL},
     3651    {"regs",      (getter)match_regs_get,      (setter)NULL},
     3652    {NULL}
     3653};
     3654
     3655#define MATCH_OFF(x) offsetof(MatchObject, x)
     3656static PyMemberDef match_members[] = {
     3657    {"string",  T_OBJECT,   MATCH_OFF(string),  READONLY},
     3658    {"re",      T_OBJECT,   MATCH_OFF(pattern), READONLY},
     3659    {"pos",     T_PYSSIZET, MATCH_OFF(pos),     READONLY},
     3660    {"endpos",  T_PYSSIZET, MATCH_OFF(endpos),  READONLY},
     3661    {NULL}
     3662};
     3663
    36413664
    36423665/* FIXME: implement setattr("string", None) as a special case (to
    36433666   detach the associated string, if any */
    36443667
    3645 statichere PyTypeObject Match_Type = {
    3646     PyObject_HEAD_INIT(NULL)
    3647     0, "_" SRE_MODULE ".SRE_Match",
     3668static PyTypeObject Match_Type = {
     3669    PyVarObject_HEAD_INIT(NULL, 0)
     3670    "_" SRE_MODULE ".SRE_Match",
    36483671    sizeof(MatchObject), sizeof(Py_ssize_t),
    3649     (destructor)match_dealloc, /*tp_dealloc*/
    3650     0, /*tp_print*/
    3651     (getattrfunc)match_getattr /*tp_getattr*/
     3672    (destructor)match_dealloc,  /* tp_dealloc */
     3673    0,                          /* tp_print */
     3674    0,                          /* tp_getattr */
     3675    0,                          /* tp_setattr */
     3676    0,                          /* tp_compare */
     3677    0,                          /* tp_repr */
     3678    0,                          /* tp_as_number */
     3679    0,                          /* tp_as_sequence */
     3680    0,                          /* tp_as_mapping */
     3681    0,                          /* tp_hash */
     3682    0,                          /* tp_call */
     3683    0,                          /* tp_str */
     3684    0,                          /* tp_getattro */
     3685    0,                          /* tp_setattro */
     3686    0,                          /* tp_as_buffer */
     3687    Py_TPFLAGS_DEFAULT,
     3688    match_doc,                  /* tp_doc */
     3689    0,                          /* tp_traverse */
     3690    0,                          /* tp_clear */
     3691    0,                          /* tp_richcompare */
     3692    0,                          /* tp_weaklistoffset */
     3693    0,                          /* tp_iter */
     3694    0,                          /* tp_iternext */
     3695    match_methods,              /* tp_methods */
     3696    match_members,              /* tp_members */
     3697    match_getset,               /* tp_getset */
    36523698};
    36533699
     
    37983844};
    37993845
    3800 static PyObject*
    3801 scanner_getattr(ScannerObject* self, char* name)
    3802 {
    3803     PyObject* res;
    3804 
    3805     res = Py_FindMethod(scanner_methods, (PyObject*) self, name);
    3806     if (res)
    3807         return res;
    3808 
    3809     PyErr_Clear();
    3810 
    3811     /* attributes */
    3812     if (!strcmp(name, "pattern")) {
    3813         Py_INCREF(self->pattern);
    3814         return self->pattern;
    3815     }
    3816 
    3817     PyErr_SetString(PyExc_AttributeError, name);
    3818     return NULL;
    3819 }
     3846#define SCAN_OFF(x) offsetof(ScannerObject, x)
     3847static PyMemberDef scanner_members[] = {
     3848    {"pattern", T_OBJECT,       SCAN_OFF(pattern),      READONLY},
     3849    {NULL}  /* Sentinel */
     3850};
    38203851
    38213852statichere PyTypeObject Scanner_Type = {
     
    38243855    sizeof(ScannerObject), 0,
    38253856    (destructor)scanner_dealloc, /*tp_dealloc*/
    3826     0, /*tp_print*/
    3827     (getattrfunc)scanner_getattr, /*tp_getattr*/
     3857    0,                          /* tp_print */
     3858    0,                          /* tp_getattr */
     3859    0,                          /* tp_setattr */
     3860    0,                          /* tp_reserved */
     3861    0,                          /* tp_repr */
     3862    0,                          /* tp_as_number */
     3863    0,                          /* tp_as_sequence */
     3864    0,                          /* tp_as_mapping */
     3865    0,                          /* tp_hash */
     3866    0,                          /* tp_call */
     3867    0,                          /* tp_str */
     3868    0,                          /* tp_getattro */
     3869    0,                          /* tp_setattro */
     3870    0,                          /* tp_as_buffer */
     3871    Py_TPFLAGS_DEFAULT,         /* tp_flags */
     3872    0,                          /* tp_doc */
     3873    0,                          /* tp_traverse */
     3874    0,                          /* tp_clear */
     3875    0,                          /* tp_richcompare */
     3876    0,                          /* tp_weaklistoffset */
     3877    0,                          /* tp_iter */
     3878    0,                          /* tp_iternext */
     3879    scanner_methods,            /* tp_methods */
     3880    scanner_members,            /* tp_members */
     3881    0,                          /* tp_getset */
    38283882};
    38293883
     
    38773931
    38783932    /* Patch object types */
    3879     Pattern_Type.ob_type = Match_Type.ob_type =
    3880         Scanner_Type.ob_type = &PyType_Type;
     3933    if (PyType_Ready(&Pattern_Type) || PyType_Ready(&Match_Type) ||
     3934        PyType_Ready(&Scanner_Type))
     3935        return;
    38813936
    38823937    m = Py_InitModule("_" SRE_MODULE, _functions);
     
    38973952    }
    38983953
     3954    x = PyLong_FromUnsignedLong(SRE_MAXREPEAT);
     3955    if (x) {
     3956        PyDict_SetItemString(d, "MAXREPEAT", x);
     3957        Py_DECREF(x);
     3958    }
     3959
    38993960    x = PyString_FromString(copyright);
    39003961    if (x) {
Note: See TracChangeset for help on using the changeset viewer.