Changeset 391 for python/trunk/Modules/_sre.c
- Timestamp:
- Mar 19, 2014, 11:31:01 PM (11 years ago)
- Location:
- python/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
python/trunk
-
Property svn:mergeinfo
set to
/python/vendor/Python-2.7.6 merged eligible /python/vendor/current merged eligible
-
Property svn:mergeinfo
set to
-
python/trunk/Modules/_sre.c
r2 r391 173 173 #if defined(HAVE_UNICODE) 174 174 175 #define SRE_UNI_IS_DIGIT(ch) Py_UNICODE_ISD IGIT((Py_UNICODE)(ch))175 #define SRE_UNI_IS_DIGIT(ch) Py_UNICODE_ISDECIMAL((Py_UNICODE)(ch)) 176 176 #define SRE_UNI_IS_SPACE(ch) Py_UNICODE_ISSPACE((Py_UNICODE)(ch)) 177 177 #define SRE_UNI_IS_LINEBREAK(ch) Py_UNICODE_ISLINEBREAK((Py_UNICODE)(ch)) … … 273 273 void* stack; 274 274 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)); 276 276 stack = PyMem_REALLOC(state->data_stack, cursize); 277 277 if (!stack) { … … 460 460 else { 461 461 /* <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)))) 463 463 return ok; 464 464 set += 8; … … 499 499 set += 64; 500 500 if (block >=0 && 501 (set[block*8 + ((ch & 255)>>5)] & (1 << (ch & 31))))501 (set[block*8 + ((ch & 255)>>5)] & (1u << (ch & 31)))) 502 502 return ok; 503 503 set += count*8; … … 525 525 526 526 /* adjust end */ 527 if (maxcount < end - ptr && maxcount != 65535)527 if (maxcount < end - ptr && maxcount != SRE_MAXREPEAT) 528 528 end = ptr + maxcount; 529 529 … … 593 593 break; 594 594 } 595 TRACE(("|%p|%p|COUNT % d\n", pattern, ptr,595 TRACE(("|%p|%p|COUNT %" PY_FORMAT_SIZE_T "d\n", pattern, ptr, 596 596 (SRE_CHAR*) state->ptr - ptr)); 597 597 return (SRE_CHAR*) state->ptr - ptr; 598 598 } 599 599 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)); 601 602 return ptr - (SRE_CHAR*) state->ptr; 602 603 } … … 685 686 do { \ 686 687 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", \ 688 690 SFY(type), alloc_pos, sizeof(type))); \ 689 if (s tate->data_stack_size < alloc_pos+sizeof(type)) { \691 if (sizeof(type) > state->data_stack_size - alloc_pos) { \ 690 692 int j = data_stack_grow(state, sizeof(type)); \ 691 693 if (j < 0) return j; \ … … 699 701 #define DATA_STACK_LOOKUP_AT(state, type, ptr, pos) \ 700 702 do { \ 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)); \ 702 704 ptr = (type*)(state->data_stack+pos); \ 703 705 } while (0) … … 705 707 #define DATA_STACK_PUSH(state, data, size) \ 706 708 do { \ 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", \ 708 711 data, state->data_stack_base, size)); \ 709 if (s tate->data_stack_size < state->data_stack_base+size) { \712 if (size > state->data_stack_size - state->data_stack_base) { \ 710 713 int j = data_stack_grow(state, size); \ 711 714 if (j < 0) return j; \ … … 719 722 #define DATA_STACK_POP(state, data, size, discard) \ 720 723 do { \ 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", \ 722 726 data, state->data_stack_base-size, size)); \ 723 727 memcpy(data, state->data_stack+state->data_stack_base-size, size); \ … … 728 732 #define DATA_STACK_POP_DISCARD(state, size) \ 729 733 do { \ 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", \ 731 736 state->data_stack_base-size, size)); \ 732 737 state->data_stack_base -= size; \ … … 832 837 /* <INFO> <1=skip> <2=flags> <3=min> ... */ 833 838 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])); 836 842 RETURN_FAILURE; 837 843 } … … 1029 1035 ctx->pattern[1], ctx->pattern[2])); 1030 1036 1031 if ( ctx->ptr + ctx->pattern[1] > end)1037 if ((Py_ssize_t) ctx->pattern[1] > end - ctx->ptr) 1032 1038 RETURN_FAILURE; /* cannot match */ 1033 1039 … … 1112 1118 ctx->pattern[1], ctx->pattern[2])); 1113 1119 1114 if ( ctx->ptr + ctx->pattern[1] > end)1120 if ((Py_ssize_t) ctx->pattern[1] > end - ctx->ptr) 1115 1121 RETURN_FAILURE; /* cannot match */ 1116 1122 … … 1140 1146 /* general case */ 1141 1147 LASTMARK_SAVE(); 1142 while ((Py_ssize_t)ctx->pattern[2] == 655351148 while ((Py_ssize_t)ctx->pattern[2] == SRE_MAXREPEAT 1143 1149 || ctx->count <= (Py_ssize_t)ctx->pattern[2]) { 1144 1150 state->ptr = ctx->ptr; … … 1208 1214 ctx->count = ctx->u.rep->count+1; 1209 1215 1210 TRACE(("|%p|%p|MAX_UNTIL % d\n", ctx->pattern,1216 TRACE(("|%p|%p|MAX_UNTIL %" PY_FORMAT_SIZE_T "d\n", ctx->pattern, 1211 1217 ctx->ptr, ctx->count)); 1212 1218 1213 if (ctx->count < ctx->u.rep->pattern[1]) {1219 if (ctx->count < (Py_ssize_t) ctx->u.rep->pattern[1]) { 1214 1220 /* not enough matches */ 1215 1221 ctx->u.rep->count = ctx->count; … … 1225 1231 } 1226 1232 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) && 1229 1235 state->ptr != ctx->u.rep->last_ptr) { 1230 1236 /* we may have enough matches, but if we can … … 1271 1277 ctx->count = ctx->u.rep->count+1; 1272 1278 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, 1274 1280 ctx->ptr, ctx->count, ctx->u.rep->pattern)); 1275 1281 1276 if (ctx->count < ctx->u.rep->pattern[1]) {1282 if (ctx->count < (Py_ssize_t) ctx->u.rep->pattern[1]) { 1277 1283 /* not enough matches */ 1278 1284 ctx->u.rep->count = ctx->count; … … 1303 1309 LASTMARK_RESTORE(); 1304 1310 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) 1307 1314 RETURN_FAILURE; 1308 1315 1309 1316 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; 1310 1320 DO_JUMP(JUMP_MIN_UNTIL_3,jump_min_until_3, 1311 1321 ctx->u.rep->pattern+3); 1322 DATA_POP(&ctx->u.rep->last_ptr); 1312 1323 if (ret) { 1313 1324 RETURN_ON_ERROR(ret); … … 1479 1490 goto jump_assert_not; 1480 1491 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)); 1482 1494 break; 1483 1495 } … … 1528 1540 } 1529 1541 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)); 1531 1544 TRACE(("charset = %p\n", charset)); 1532 1545 … … 1637 1650 sre_codesize(PyObject* self, PyObject *unused) 1638 1651 { 1639 return Py _BuildValue("l",sizeof(SRE_CODE));1652 return PyInt_FromSize_t(sizeof(SRE_CODE)); 1640 1653 } 1641 1654 … … 1687 1700 /* unicode strings doesn't always support the buffer interface */ 1688 1701 ptr = (void*) PyUnicode_AS_DATA(string); 1689 bytes = PyUnicode_GET_DATA_SIZE(string);1702 /* bytes = PyUnicode_GET_DATA_SIZE(string); */ 1690 1703 size = PyUnicode_GET_SIZE(string); 1691 1704 charsize = sizeof(Py_UNICODE); … … 2449 2462 2450 2463 if (subn) 2451 return Py_BuildValue("N i", item, n);2464 return Py_BuildValue("Nn", item, n); 2452 2465 2453 2466 return item; … … 2547 2560 "search(string[, pos[, endpos]]) --> match object or None.\n\ 2548 2561 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."); 2550 2563 2551 2564 PyDoc_STRVAR(pattern_split_doc, … … 2599 2612 }; 2600 2613 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) 2615 static 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 }; 2633 2622 2634 2623 statichere PyTypeObject Pattern_Type = { … … 2637 2626 sizeof(PatternObject), sizeof(SRE_CODE), 2638 2627 (destructor)pattern_dealloc, /*tp_dealloc*/ 2639 0, /*tp_print*/2640 (getattrfunc)pattern_getattr, /*tp_getattr*/2628 0, /* tp_print */ 2629 0, /* tp_getattrn */ 2641 2630 0, /* tp_setattr */ 2642 2631 0, /* tp_compare */ … … 2651 2640 0, /* tp_setattro */ 2652 2641 0, /* tp_as_buffer */ 2653 Py_TPFLAGS_ HAVE_WEAKREFS,/* tp_flags */2642 Py_TPFLAGS_DEFAULT, /* tp_flags */ 2654 2643 pattern_doc, /* tp_doc */ 2655 2644 0, /* tp_traverse */ … … 2657 2646 0, /* tp_richcompare */ 2658 2647 offsetof(PatternObject, weakreflist), /* tp_weaklistoffset */ 2648 0, /* tp_iter */ 2649 0, /* tp_iternext */ 2650 pattern_methods, /* tp_methods */ 2651 pattern_members, /* tp_members */ 2659 2652 }; 2660 2653 … … 2696 2689 unsigned long value = PyInt_Check(o) ? (unsigned long)PyInt_AsLong(o) 2697 2690 : 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 } 2698 2698 self->code[i] = (SRE_CODE) value; 2699 2699 if ((unsigned long) self->code[i] != value) { … … 2765 2765 #define VTRACE(v) printf v 2766 2766 #else 2767 #define VTRACE(v) 2767 #define VTRACE(v) do {} while(0) /* do nothing */ 2768 2768 #endif 2769 2769 … … 2793 2793 VTRACE(("%lu (skip to %p)\n", \ 2794 2794 (unsigned long)skip, code+skip)); \ 2795 if ( code+skip-adj < code || code+skip-adj > end)\2795 if (skip-adj > end-code) \ 2796 2796 FAIL; \ 2797 2797 code++; \ … … 2826 2826 case SRE_OP_CHARSET: 2827 2827 offset = 32/sizeof(SRE_CODE); /* 32-byte bitmap */ 2828 if ( code+offset < code || code+offset > end)2828 if (offset > end-code) 2829 2829 FAIL; 2830 2830 code += offset; … … 2834 2834 GET_ARG; /* Number of blocks */ 2835 2835 offset = 256/sizeof(SRE_CODE); /* 256-byte table */ 2836 if ( code+offset < code || code+offset > end)2836 if (offset > end-code) 2837 2837 FAIL; 2838 2838 /* Make sure that each byte points to a valid block */ … … 2843 2843 code += offset; 2844 2844 offset = arg * 32/sizeof(SRE_CODE); /* 32-byte bitmap times arg */ 2845 if ( code+offset < code || code+offset > end)2845 if (offset > end-code) 2846 2846 FAIL; 2847 2847 code += offset; … … 2968 2968 If SRE_INFO_PREFIX or SRE_INFO_CHARSET is in the flags, 2969 2969 more follows. */ 2970 SRE_CODE flags, min, max,i;2970 SRE_CODE flags, i; 2971 2971 SRE_CODE *newcode; 2972 2972 GET_SKIP; 2973 2973 newcode = code+skip-1; 2974 2974 GET_ARG; flags = arg; 2975 GET_ARG; min = arg;2976 GET_ARG; max = arg;2975 GET_ARG; /* min */ 2976 GET_ARG; /* max */ 2977 2977 /* Check that only valid flags are present */ 2978 2978 if ((flags & ~(SRE_INFO_PREFIX | … … 2990 2990 /* Validate the prefix */ 2991 2991 if (flags & SRE_INFO_PREFIX) { 2992 SRE_CODE prefix_len , prefix_skip;2992 SRE_CODE prefix_len; 2993 2993 GET_ARG; prefix_len = arg; 2994 GET_ARG; prefix_skip = arg;2994 GET_ARG; /* prefix skip */ 2995 2995 /* Here comes the prefix string */ 2996 if ( code+prefix_len < code || code+prefix_len > newcode)2996 if (prefix_len > newcode-code) 2997 2997 FAIL; 2998 2998 code += prefix_len; 2999 2999 /* And here comes the overlap table */ 3000 if ( code+prefix_len < code || code+prefix_len > newcode)3000 if (prefix_len > newcode-code) 3001 3001 FAIL; 3002 3002 /* Each overlap value should be < prefix_len */ … … 3056 3056 if (min > max) 3057 3057 FAIL; 3058 #ifdef Py_UNICODE_WIDE 3059 if (max > 65535) 3058 if (max > SRE_MAXREPEAT) 3060 3059 FAIL; 3061 #endif3062 3060 if (!_validate_inner(code, code+skip-4, groups)) 3063 3061 FAIL; … … 3077 3075 if (min > max) 3078 3076 FAIL; 3079 #ifdef Py_UNICODE_WIDE 3080 if (max > 65535) 3077 if (max > SRE_MAXREPEAT) 3081 3078 FAIL; 3082 #endif3083 3079 if (!_validate_inner(code, code+skip-3, groups)) 3084 3080 FAIL; … … 3131 3127 for a JUMP opcode preceding our skip target. 3132 3128 */ 3133 if (skip >= 3 && code+skip-3 >=code &&3129 if (skip >= 3 && skip-3 < end-code && 3134 3130 code[skip-3] == SRE_OP_JUMP) 3135 3131 { … … 3407 3403 3408 3404 /* 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]); 3410 3406 } 3411 3407 … … 3430 3426 3431 3427 /* 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]); 3433 3429 } 3434 3430 … … 3563 3559 } 3564 3560 3561 PyDoc_STRVAR(match_doc, 3562 "The result of re.match() and re.search().\n\ 3563 Match objects always have a boolean value of True."); 3564 3565 PyDoc_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 3570 PyDoc_STRVAR(match_start_doc, 3571 "start([group=0]) -> int.\n\ 3572 Return index of the start of the substring matched by group."); 3573 3574 PyDoc_STRVAR(match_end_doc, 3575 "end([group=0]) -> int.\n\ 3576 Return index of the end of the substring matched by group."); 3577 3578 PyDoc_STRVAR(match_span_doc, 3579 "span([group]) -> tuple.\n\ 3580 For MatchObject m, return the 2-tuple (m.start(group), m.end(group))."); 3581 3582 PyDoc_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 3588 PyDoc_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 3594 PyDoc_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 3565 3599 static 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}, 3573 3609 {"__copy__", (PyCFunction) match_copy, METH_NOARGS}, 3574 3610 {"__deepcopy__", (PyCFunction) match_deepcopy, METH_O}, … … 3576 3612 }; 3577 3613 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 } 3614 static PyObject * 3615 match_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 3623 static PyObject * 3624 match_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 3638 static PyObject * 3639 match_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 3648 static 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) 3656 static 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 3641 3664 3642 3665 /* FIXME: implement setattr("string", None) as a special case (to 3643 3666 detach the associated string, if any */ 3644 3667 3645 static herePyTypeObject Match_Type = {3646 Py Object_HEAD_INIT(NULL)3647 0,"_" SRE_MODULE ".SRE_Match",3668 static PyTypeObject Match_Type = { 3669 PyVarObject_HEAD_INIT(NULL, 0) 3670 "_" SRE_MODULE ".SRE_Match", 3648 3671 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 */ 3652 3698 }; 3653 3699 … … 3798 3844 }; 3799 3845 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) 3847 static PyMemberDef scanner_members[] = { 3848 {"pattern", T_OBJECT, SCAN_OFF(pattern), READONLY}, 3849 {NULL} /* Sentinel */ 3850 }; 3820 3851 3821 3852 statichere PyTypeObject Scanner_Type = { … … 3824 3855 sizeof(ScannerObject), 0, 3825 3856 (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 */ 3828 3882 }; 3829 3883 … … 3877 3931 3878 3932 /* 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; 3881 3936 3882 3937 m = Py_InitModule("_" SRE_MODULE, _functions); … … 3897 3952 } 3898 3953 3954 x = PyLong_FromUnsignedLong(SRE_MAXREPEAT); 3955 if (x) { 3956 PyDict_SetItemString(d, "MAXREPEAT", x); 3957 Py_DECREF(x); 3958 } 3959 3899 3960 x = PyString_FromString(copyright); 3900 3961 if (x) {
Note:
See TracChangeset
for help on using the changeset viewer.