Checking in AMK's latest installement.

(Two small changes to shup up gcc added.)
This commit is contained in:
Guido van Rossum 1997-10-08 02:07:40 +00:00
parent a7a89ebdbe
commit c386107838
2 changed files with 68 additions and 130 deletions

View File

@ -222,7 +222,9 @@ PyPcre_compile(self, args)
{ {
PyMem_DEL(rv); PyMem_DEL(rv);
if (!PyErr_Occurred()) if (!PyErr_Occurred())
PyErr_SetObject(ErrorObject, Py_BuildValue("si", error, erroroffset)); {
PyErr_SetObject(ErrorObject, Py_BuildValue("si", error, erroroffset));
}
return NULL; return NULL;
} }
rv->regex_extra=pcre_study(rv->regex, 0, &error); rv->regex_extra=pcre_study(rv->regex, 0, &error);
@ -243,47 +245,57 @@ PyPcre_compile(self, args)
} }
static PyObject * static PyObject *
PyPcre_expand_escape(self, args) PyPcre_expand_escape(pattern, pattern_len, indexptr, typeptr)
PyObject *self; unsigned char *pattern;
PyObject *args; int pattern_len, *indexptr, *typeptr;
{ {
unsigned char c, *pattern; unsigned char c;
int index, pattern_len; int index = *indexptr;
const int context=REPLACEMENT;
if (!PyArg_ParseTuple(args, "s#i", &pattern, &pattern_len, &index))
return NULL;
if (pattern_len<=index) if (pattern_len<=index)
{ {
PyErr_SetString(ErrorObject, "escape ends too soon"); PyErr_SetString(ErrorObject, "escape ends too soon");
return NULL; return NULL;
} }
c=pattern[index]; index++; c=pattern[index]; index++;
*typeptr=CHAR;
switch (c) switch (c)
{ {
case('t'): case('t'):
return Py_BuildValue("ici", CHAR, (char)9, index); *indexptr=index;
return Py_BuildValue("c", (char)9);
break; break;
case('n'): case('n'):
return Py_BuildValue("ici", CHAR, (char)10, index); *indexptr = index;
return Py_BuildValue("c", (char)10);
break; break;
case('v'): case('v'):
return Py_BuildValue("ici", CHAR, (char)11, index); *indexptr = index;
return Py_BuildValue("c", (char)11);
break; break;
case('r'): case('r'):
return Py_BuildValue("ici", CHAR, (char)13, index); *indexptr = index;
return Py_BuildValue("c", (char)13);
break; break;
case('f'): case('f'):
return Py_BuildValue("ici", CHAR, (char)12, index); *indexptr = index;
return Py_BuildValue("c", (char)12);
break; break;
case('a'): case('a'):
return Py_BuildValue("ici", CHAR, (char)7, index); *indexptr = index;
return Py_BuildValue("c", (char)7);
break; break;
case('b'):
*indexptr=index;
return Py_BuildValue("c", (char)8);
break;
case('x'): case('x'):
{ {
int end, length; int end, length;
unsigned char *string; unsigned char *string;
PyObject *v, *result; PyObject *v;
end=index; end=index;
while (end<pattern_len && while (end<pattern_len &&
@ -312,52 +324,11 @@ PyPcre_expand_escape(self, args)
free(string); free(string);
/* The evaluation raised an exception */ /* The evaluation raised an exception */
if (v==NULL) return NULL; if (v==NULL) return NULL;
result=Py_BuildValue("iOi", CHAR, v, end); *indexptr = end;
Py_DECREF(v); return v;
return result;
} }
break; break;
case('b'):
if (context!=NORMAL)
return Py_BuildValue("ici", CHAR, (char)8, index);
else
{
unsigned char empty_string[1];
empty_string[0]='\0';
return Py_BuildValue("isi", WORD_BOUNDARY, empty_string, index);
}
break;
case('B'):
if (context!=NORMAL)
return Py_BuildValue("ici", CHAR, 'B', index);
else
{
unsigned char empty_string[1];
empty_string[0]='\0';
return Py_BuildValue("isi", NOT_WORD_BOUNDARY, empty_string, index);
}
break;
case('A'):
if (context!=NORMAL)
return Py_BuildValue("ici", CHAR, 'A', index);
else
{
unsigned char empty_string[1];
empty_string[0]='\0';
return Py_BuildValue("isi", BEGINNING_OF_BUFFER, empty_string, index);
}
break;
case('Z'):
if (context!=NORMAL)
return Py_BuildValue("ici", CHAR, 'Z', index);
else
{
unsigned char empty_string[1];
empty_string[0]='\0';
return Py_BuildValue("isi", END_OF_BUFFER, empty_string, index);
}
break;
case('E'): case('G'): case('L'): case('Q'): case('E'): case('G'): case('L'): case('Q'):
case('U'): case('l'): case('u'): case('U'): case('l'): case('u'):
{ {
@ -367,26 +338,6 @@ PyPcre_expand_escape(self, args)
return NULL; return NULL;
} }
case ('w'):
return Py_BuildValue("ici", CHAR, 'w', index);
break;
case ('W'):
return Py_BuildValue("ici", CHAR, 'W', index);
break;
case ('s'):
return Py_BuildValue("ici", CHAR, 's', index);
break;
case ('S'):
return Py_BuildValue("ici", CHAR, 'S', index);
break;
case ('d'):
return Py_BuildValue("ici", CHAR, 'd', index);
break;
case ('D'):
return Py_BuildValue("ici", CHAR, 'D', index);
break;
case('g'): case('g'):
{ {
int end, valid, i; int end, valid, i;
@ -427,9 +378,9 @@ PyPcre_expand_escape(self, args)
return NULL; return NULL;
} }
return Py_BuildValue("is#i", MEMORY_REFERENCE, *typeptr = MEMORY_REFERENCE;
pattern+index, end-index, *indexptr = end+1;
end+1); return Py_BuildValue("s#", pattern+index, end-index);
} }
break; break;
@ -451,7 +402,8 @@ PyPcre_expand_escape(self, args)
PyErr_SetString(ErrorObject, "octal value out of range"); PyErr_SetString(ErrorObject, "octal value out of range");
return NULL; return NULL;
} }
return Py_BuildValue("ici", CHAR, (unsigned char)octval, i); *indexptr = i;
return Py_BuildValue("c", (unsigned char)octval);
} }
break; break;
case('1'): case('2'): case('3'): case('4'): case('1'): case('2'): case('3'): case('4'):
@ -483,17 +435,12 @@ PyPcre_expand_escape(self, args)
PyErr_SetString(ErrorObject, "octal value out of range"); PyErr_SetString(ErrorObject, "octal value out of range");
return NULL; return NULL;
} }
return Py_BuildValue("ici", CHAR, (unsigned char)value, index+3); *indexptr = index+3;
return Py_BuildValue("c", (unsigned char)value);
} }
else else
{ {
/* 2-digit form, so it's a memory reference */ /* 2-digit form, so it's a memory reference */
if (context==CHARCLASS)
{
PyErr_SetString(ErrorObject, "cannot reference a register "
"from inside a character class");
return NULL;
}
value= 10*(pattern[index ]-'0') + value= 10*(pattern[index ]-'0') +
(pattern[index+1]-'0'); (pattern[index+1]-'0');
if (value<1 || EXTRACT_MAX<=value) if (value<1 || EXTRACT_MAX<=value)
@ -501,27 +448,24 @@ PyPcre_expand_escape(self, args)
PyErr_SetString(ErrorObject, "memory reference out of range"); PyErr_SetString(ErrorObject, "memory reference out of range");
return NULL; return NULL;
} }
return Py_BuildValue("iii", MEMORY_REFERENCE, *typeptr = MEMORY_REFERENCE;
value, index+2); *indexptr = index+2;
return Py_BuildValue("i", value);
} }
} }
else else
{ {
/* Single-digit form, like \2, so it's a memory reference */ /* Single-digit form, like \2, so it's a memory reference */
if (context==CHARCLASS) *typeptr = MEMORY_REFERENCE;
{ *indexptr = index+1;
PyErr_SetString(ErrorObject, "cannot reference a register " return Py_BuildValue("i", pattern[index]-'0');
"from inside a character class");
return NULL;
}
return Py_BuildValue("iii", MEMORY_REFERENCE,
pattern[index]-'0', index+1);
} }
} }
break; break;
default: default:
return Py_BuildValue("ici", CHAR, c, index); *indexptr = index;
return Py_BuildValue("c", c);
break; break;
} }
} }
@ -547,7 +491,7 @@ PyPcre_expand(self, args)
{ {
if (repl[i]=='\\') if (repl[i]=='\\')
{ {
PyObject *args, *t, *value; PyObject *value;
int escape_type; int escape_type;
if (start!=i) if (start!=i)
@ -557,18 +501,14 @@ PyPcre_expand(self, args)
total_len += i-start; total_len += i-start;
} }
i++; i++;
args=Py_BuildValue("Oi", repl_obj, i); value=PyPcre_expand_escape(repl, size, &i, &escape_type);
t=PyPcre_expand_escape(NULL, args); if (value==NULL)
Py_DECREF(args);
if (t==NULL)
{ {
/* PyPcre_expand_escape triggered an exception of some sort, /* PyPcre_expand_escape triggered an exception of some sort,
so just return */ so just return */
Py_DECREF(results); Py_DECREF(results);
return NULL; return NULL;
} }
value=PyTuple_GetItem(t, 1);
escape_type=PyInt_AsLong(PyTuple_GetItem(t, 0));
switch (escape_type) switch (escape_type)
{ {
case (CHAR): case (CHAR):
@ -599,7 +539,6 @@ PyPcre_expand(self, args)
PyErr_SetString(ErrorObject, PyErr_SetString(ErrorObject,
message); message);
Py_DECREF(result); Py_DECREF(result);
Py_DECREF(t);
Py_DECREF(results); Py_DECREF(results);
return NULL; return NULL;
} }
@ -610,15 +549,13 @@ PyPcre_expand(self, args)
} }
break; break;
default: default:
Py_DECREF(t);
Py_DECREF(results); Py_DECREF(results);
PyErr_SetString(ErrorObject, PyErr_SetString(ErrorObject,
"bad escape in replacement"); "bad escape in replacement");
return NULL; return NULL;
} }
i=start=PyInt_AsLong(PyTuple_GetItem(t, 2)); start=i;
i--; /* Decrement now, because the 'for' loop will increment it */ i--; /* Decrement now, because the 'for' loop will increment it */
Py_DECREF(t);
} }
} /* endif repl[i]!='\\' */ } /* endif repl[i]!='\\' */
@ -690,7 +627,6 @@ void
initpcre() initpcre()
{ {
PyObject *m, *d; PyObject *m, *d;
int a;
/* Create the module and add the functions */ /* Create the module and add the functions */
m = Py_InitModule("pcre", pcre_methods); m = Py_InitModule("pcre", pcre_methods);

View File

@ -999,6 +999,7 @@ get_group_id(uschar *ptr, char finalchar, char **errorptr)
for(; (*ptr != 0) && (*ptr != finalchar) && for(; (*ptr != 0) && (*ptr != finalchar) &&
(pcre_ctypes[*ptr] & ctype_word); ptr++) (pcre_ctypes[*ptr] & ctype_word); ptr++)
{ {
/* Empty loop body */
} }
if (*ptr==finalchar) if (*ptr==finalchar)
return ptr-start; return ptr-start;
@ -1089,9 +1090,9 @@ else switch (c)
case 'x': case 'x':
{ {
int end, length; int length;
char *string; char *string;
PyObject *v, *result; PyObject *result;
i=1; i=1;
while (ptr[i]!=0 && while (ptr[i]!=0 &&
@ -1116,23 +1117,23 @@ else switch (c)
string[length+4]='\0'; string[length+4]='\0';
memcpy(string+2, ptr, length+1); memcpy(string+2, ptr, length+1);
ptr += length; ptr += length;
v=PyRun_String((char *)string, Py_eval_input, result=PyRun_String((char *)string, Py_eval_input,
PyEval_GetGlobals(), PyEval_GetLocals()); PyEval_GetGlobals(), PyEval_GetLocals());
free(string); free(string);
/* The evaluation raised an exception */ /* The evaluation raised an exception */
if (v==NULL) if (result==NULL)
{ {
*errorptr="exception occurred during evaluation of \\x"; *errorptr="exception occurred during evaluation of \\x";
break; break;
} }
if (PyString_Size(v)!=1) if (PyString_Size(result)!=1)
{ {
Py_DECREF(v); Py_DECREF(result);
*errorptr="\\x string is not one byte in length"; *errorptr="\\x string is not one byte in length";
break; break;
} }
c=*(unsigned char *)PyString_AsString(v); c=*(unsigned char *)PyString_AsString(result);
Py_DECREF(v); Py_DECREF(result);
break; break;
} }
break; break;
@ -1760,20 +1761,20 @@ for (;; ptr++)
goto FAILED; goto FAILED;
} }
string = PyString_FromStringAndSize(ptr, idlen); string = PyString_FromStringAndSize(ptr, idlen);
if (string==NULL) if (string==NULL) {
{
Py_XDECREF(string); Py_XDECREF(string);
*errorptr = "exception raised"; *errorptr = "exception raised";
goto FAILED; goto FAILED;
} }
intobj = PyDict_GetItem(dictionary, string); intobj = PyDict_GetItem(dictionary, string);
if (intobj==NULL) { if (intobj==NULL) {
Py_DECREF(string);
*errorptr = "?P= group identifier isn't defined"; *errorptr = "?P= group identifier isn't defined";
goto FAILED; goto FAILED;
} }
refnum = PyInt_AsLong(intobj); refnum = PyInt_AsLong(intobj);
Py_DECREF(string); Py_DECREF(intobj); Py_DECREF(string); Py_DECREF(intobj);
*code++ = OP_REF; *code++ = OP_REF;
*code++ = refnum; *code++ = refnum;
/* The continue will cause the top-level for() loop to /* The continue will cause the top-level for() loop to
@ -2942,7 +2943,8 @@ if (md->offset_top) free(md->offset_top);
if (md->r1) free(md->r1); if (md->r1) free(md->r1);
if (md->r2) free(md->r2); if (md->r2) free(md->r2);
if (md->eptr) free(md->eptr); if (md->eptr) free(md->eptr);
if (md->ecode) free(md->ecode); if (md->ecode) free(md->ecode);
return 0;
} }
static int grow_stack(match_data *md) static int grow_stack(match_data *md)
@ -2987,7 +2989,7 @@ for (;;)
int min, max, ctype; int min, max, ctype;
register int i; register int i;
register int c; register int c;
BOOL minimize; BOOL minimize = 0;
/* Opening bracket. Check the alternative branches in turn, failing if none /* Opening bracket. Check the alternative branches in turn, failing if none
match. We have to set the start offset if required and there is space match. We have to set the start offset if required and there is space
@ -3000,7 +3002,7 @@ for (;;)
if ((int)*ecode >= OP_BRA) if ((int)*ecode >= OP_BRA)
{ {
int number = (*ecode - OP_BRA) << 1; int number = (*ecode - OP_BRA) << 1;
int save_offset1, save_offset2; int save_offset1 = 0, save_offset2 = 0;
#ifdef DEBUG #ifdef DEBUG
printf("start bracket %d\n", number/2); printf("start bracket %d\n", number/2);
@ -3858,7 +3860,7 @@ for (;;)
if (md->point > save_stack_position) if (md->point > save_stack_position)
{ {
/* If there are still points remaining on the stack, pop the next one off */ /* If there are still points remaining on the stack, pop the next one off */
int start, end, off_num; int off_num;
md->point--; md->point--;
offset_top = md->offset_top[md->point]; offset_top = md->offset_top[md->point];