Author: Amaury Forgeot d'Arc <[email protected]>
Branch: py3k
Changeset: r54865:7235f7fa634e
Date: 2012-05-01 19:17 +0200
http://bitbucket.org/pypy/pypy/changeset/7235f7fa634e/
Log: cpyext: Update _sre.c used in tests with a recent 3.2 version, and
convert some print statements
diff --git a/pypy/module/cpyext/test/_sre.c b/pypy/module/cpyext/test/_sre.c
--- a/pypy/module/cpyext/test/_sre.c
+++ b/pypy/module/cpyext/test/_sre.c
@@ -58,12 +58,8 @@
/* defining this one enables tracing */
#undef VERBOSE
-#if PY_VERSION_HEX >= 0x01060000
-#if PY_VERSION_HEX < 0x02020000 || defined(Py_USING_UNICODE)
/* defining this enables unicode support (default under 1.6a1 and later) */
#define HAVE_UNICODE
-#endif
-#endif
/* -------------------------------------------------------------------- */
/* optional features */
@@ -71,9 +67,6 @@
/* enables fast searching */
#define USE_FAST_SEARCH
-/* enables aggressive inlining (always on for Visual C) */
-#undef USE_INLINE
-
/* enables copy/deepcopy handling (work in progress) */
#undef USE_BUILTIN_COPY
@@ -1681,41 +1674,44 @@
Py_ssize_t size, bytes;
int charsize;
void* ptr;
-
-#if defined(HAVE_UNICODE)
+ Py_buffer view;
+
+ /* Unicode objects do not support the buffer API. So, get the data
+ directly instead. */
if (PyUnicode_Check(string)) {
- /* unicode strings doesn't always support the buffer interface */
- ptr = (void*) PyUnicode_AS_DATA(string);
- /* bytes = PyUnicode_GET_DATA_SIZE(string); */
- size = PyUnicode_GET_SIZE(string);
- charsize = sizeof(Py_UNICODE);
-
- } else {
-#endif
+ ptr = (void *)PyUnicode_AS_DATA(string);
+ *p_length = PyUnicode_GET_SIZE(string);
+ *p_charsize = sizeof(Py_UNICODE);
+ return ptr;
+ }
/* get pointer to string buffer */
+ view.len = -1;
buffer = Py_TYPE(string)->tp_as_buffer;
- if (!buffer || !buffer->bf_getreadbuffer || !buffer->bf_getsegcount ||
- buffer->bf_getsegcount(string, NULL) != 1) {
- PyErr_SetString(PyExc_TypeError, "expected string or buffer");
- return NULL;
+ if (!buffer || !buffer->bf_getbuffer ||
+ (*buffer->bf_getbuffer)(string, &view, PyBUF_SIMPLE) < 0) {
+ PyErr_SetString(PyExc_TypeError, "expected string or buffer");
+ return NULL;
}
/* determine buffer size */
- bytes = buffer->bf_getreadbuffer(string, 0, &ptr);
+ bytes = view.len;
+ ptr = view.buf;
+
+ /* Release the buffer immediately --- possibly dangerous
+ but doing something else would require some re-factoring
+ */
+ PyBuffer_Release(&view);
+
if (bytes < 0) {
PyErr_SetString(PyExc_TypeError, "buffer has negative size");
return NULL;
}
/* determine character size */
-#if PY_VERSION_HEX >= 0x01060000
size = PyObject_Size(string);
-#else
- size = PyObject_Length(string);
-#endif
-
- if (PyString_Check(string) || bytes == size)
+
+ if (PyBytes_Check(string) || bytes == size)
charsize = 1;
#if defined(HAVE_UNICODE)
else if (bytes == (Py_ssize_t) (size * sizeof(Py_UNICODE)))
@@ -1726,13 +1722,13 @@
return NULL;
}
-#if defined(HAVE_UNICODE)
- }
-#endif
-
*p_length = size;
*p_charsize = charsize;
+ if (ptr == NULL) {
+ PyErr_SetString(PyExc_ValueError,
+ "Buffer is NULL");
+ }
return ptr;
}
@@ -1755,6 +1751,17 @@
if (!ptr)
return NULL;
+ if (charsize == 1 && pattern->charsize > 1) {
+ PyErr_SetString(PyExc_TypeError,
+ "can't use a string pattern on a bytes-like object");
+ return NULL;
+ }
+ if (charsize > 1 && pattern->charsize == 1) {
+ PyErr_SetString(PyExc_TypeError,
+ "can't use a bytes pattern on a string-like object");
+ return NULL;
+ }
+
/* adjust boundaries */
if (start < 0)
start = 0;
@@ -1949,7 +1956,7 @@
if (!args)
return NULL;
- name = PyString_FromString(module);
+ name = PyUnicode_FromString(module);
if (!name)
return NULL;
mod = PyImport_Import(name);
@@ -2607,15 +2614,15 @@
{NULL} /* Sentinel */
};
-statichere PyTypeObject Pattern_Type = {
- PyObject_HEAD_INIT(NULL)
- 0, "_" SRE_MODULE ".SRE_Pattern",
+static PyTypeObject Pattern_Type = {
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "_" SRE_MODULE ".SRE_Pattern",
sizeof(PatternObject), sizeof(SRE_CODE),
- (destructor)pattern_dealloc, /*tp_dealloc*/
- 0, /* tp_print */
- 0, /* tp_getattrn */
+ (destructor)pattern_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
0, /* tp_setattr */
- 0, /* tp_compare */
+ 0, /* tp_reserved */
0, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
@@ -2626,7 +2633,7 @@
0, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT, /* tp_flags */
+ Py_TPFLAGS_DEFAULT, /* tp_flags */
pattern_doc, /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
@@ -2673,8 +2680,7 @@
for (i = 0; i < n; i++) {
PyObject *o = PyList_GET_ITEM(code, i);
- unsigned long value = PyInt_Check(o) ? (unsigned long)PyInt_AsLong(o)
- : PyLong_AsUnsignedLong(o);
+ unsigned long value = PyLong_AsUnsignedLong(o);
self->code[i] = (SRE_CODE) value;
if ((unsigned long) self->code[i] != value) {
PyErr_SetString(PyExc_OverflowError,
@@ -2688,6 +2694,16 @@
return NULL;
}
+ if (pattern == Py_None)
+ self->charsize = -1;
+ else {
+ Py_ssize_t p_length;
+ if (!getstring(pattern, &p_length, &self->charsize)) {
+ Py_DECREF(self);
+ return NULL;
+ }
+ }
+
Py_INCREF(pattern);
self->pattern = pattern;
@@ -2744,7 +2760,7 @@
#if defined(VVERBOSE)
#define VTRACE(v) printf v
#else
-#define VTRACE(v) do {} while(0) /* do nothing */
+#define VTRACE(v)
#endif
/* Report failure */
@@ -2947,13 +2963,13 @@
<INFO> <1=skip> <2=flags> <3=min> <4=max>;
If SRE_INFO_PREFIX or SRE_INFO_CHARSET is in the flags,
more follows. */
- SRE_CODE flags, i;
+ SRE_CODE flags, min, max, i;
SRE_CODE *newcode;
GET_SKIP;
newcode = code+skip-1;
GET_ARG; flags = arg;
- GET_ARG; /* min */
- GET_ARG; /* max */
+ GET_ARG; min = arg;
+ GET_ARG; max = arg;
/* Check that only valid flags are present */
if ((flags & ~(SRE_INFO_PREFIX |
SRE_INFO_LITERAL |
@@ -2969,9 +2985,9 @@
FAIL;
/* Validate the prefix */
if (flags & SRE_INFO_PREFIX) {
- SRE_CODE prefix_len;
+ SRE_CODE prefix_len, prefix_skip;
GET_ARG; prefix_len = arg;
- GET_ARG; /* prefix skip */
+ GET_ARG; prefix_skip = arg;
/* Here comes the prefix string */
if (code+prefix_len < code || code+prefix_len > newcode)
FAIL;
@@ -3221,16 +3237,20 @@
{
Py_ssize_t i;
- if (PyInt_Check(index))
- return PyInt_AsSsize_t(index);
+ if (index == NULL)
+ /* Default value */
+ return 0;
+
+ if (PyLong_Check(index))
+ return PyLong_AsSsize_t(index);
i = -1;
if (self->pattern->groupindex) {
index = PyObject_GetItem(self->pattern->groupindex, index);
if (index) {
- if (PyInt_Check(index) || PyLong_Check(index))
- i = PyInt_AsSsize_t(index);
+ if (PyLong_Check(index))
+ i = PyLong_AsSsize_t(index);
Py_DECREF(index);
} else
PyErr_Clear();
@@ -3371,7 +3391,7 @@
{
Py_ssize_t index;
- PyObject* index_ = Py_False; /* zero */
+ PyObject* index_ = NULL;
if (!PyArg_UnpackTuple(args, "start", 0, 1, &index_))
return NULL;
@@ -3394,7 +3414,7 @@
{
Py_ssize_t index;
- PyObject* index_ = Py_False; /* zero */
+ PyObject* index_ = NULL;
if (!PyArg_UnpackTuple(args, "end", 0, 1, &index_))
return NULL;
@@ -3422,12 +3442,12 @@
if (!pair)
return NULL;
- item = PyInt_FromSsize_t(i1);
+ item = PyLong_FromSsize_t(i1);
if (!item)
goto error;
PyTuple_SET_ITEM(pair, 0, item);
- item = PyInt_FromSsize_t(i2);
+ item = PyLong_FromSsize_t(i2);
if (!item)
goto error;
PyTuple_SET_ITEM(pair, 1, item);
@@ -3444,7 +3464,7 @@
{
Py_ssize_t index;
- PyObject* index_ = Py_False; /* zero */
+ PyObject* index_ = NULL;
if (!PyArg_UnpackTuple(args, "span", 0, 1, &index_))
return NULL;
@@ -3542,7 +3562,7 @@
#endif
}
-static struct PyMethodDef match_methods[] = {
+static PyMethodDef match_methods[] = {
{"group", (PyCFunction) match_group, METH_VARARGS},
{"start", (PyCFunction) match_start, METH_VARARGS},
{"end", (PyCFunction) match_end, METH_VARARGS},
@@ -3605,37 +3625,36 @@
{NULL}
};
-
/* FIXME: implement setattr("string", None) as a special case (to
detach the associated string, if any */
static PyTypeObject Match_Type = {
- PyVarObject_HEAD_INIT(NULL, 0)
+ PyVarObject_HEAD_INIT(NULL,0)
"_" SRE_MODULE ".SRE_Match",
sizeof(MatchObject), sizeof(Py_ssize_t),
- (destructor)match_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_compare */
- 0, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- 0, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT,
- 0, /* tp_doc */
- 0, /* tp_traverse */
- 0, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- 0, /* tp_iter */
- 0, /* tp_iternext */
+ (destructor)match_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ 0, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ 0, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT, /* tp_flags */
+ 0, /* tp_doc */
+ 0, /* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ 0, /* tp_iter */
+ 0, /* tp_iternext */
match_methods, /* tp_methods */
match_members, /* tp_members */
match_getset, /* tp_getset */
@@ -3793,11 +3812,11 @@
{NULL} /* Sentinel */
};
-statichere PyTypeObject Scanner_Type = {
- PyObject_HEAD_INIT(NULL)
- 0, "_" SRE_MODULE ".SRE_Scanner",
+static PyTypeObject Scanner_Type = {
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "_" SRE_MODULE ".SRE_Scanner",
sizeof(ScannerObject), 0,
- (destructor)scanner_dealloc, /*tp_dealloc*/
+ (destructor)scanner_dealloc,/* tp_dealloc */
0, /* tp_print */
0, /* tp_getattr */
0, /* tp_setattr */
@@ -3863,11 +3882,7 @@
{NULL, NULL}
};
-#if PY_VERSION_HEX < 0x02030000
-DL_EXPORT(void) init_sre(void)
-#else
PyMODINIT_FUNC init_sre(void)
-#endif
{
PyObject* m;
PyObject* d;
@@ -3883,19 +3898,19 @@
return;
d = PyModule_GetDict(m);
- x = PyInt_FromLong(SRE_MAGIC);
+ x = PyLong_FromLong(SRE_MAGIC);
if (x) {
PyDict_SetItemString(d, "MAGIC", x);
Py_DECREF(x);
}
- x = PyInt_FromLong(sizeof(SRE_CODE));
+ x = PyLong_FromLong(sizeof(SRE_CODE));
if (x) {
PyDict_SetItemString(d, "CODESIZE", x);
Py_DECREF(x);
}
- x = PyString_FromString(copyright);
+ x = PyUnicode_FromString(copyright);
if (x) {
PyDict_SetItemString(d, "copyright", x);
Py_DECREF(x);
diff --git a/pypy/module/cpyext/test/test_typeobject.py
b/pypy/module/cpyext/test/test_typeobject.py
--- a/pypy/module/cpyext/test/test_typeobject.py
+++ b/pypy/module/cpyext/test/test_typeobject.py
@@ -14,12 +14,12 @@
assert 'foo' in sys.modules
assert "copy" in dir(module.fooType)
obj = module.new()
- print obj.foo
+ print(obj.foo)
assert obj.foo == 42
- print "Obj has type", type(obj)
+ print("Obj has type", type(obj))
assert type(obj) is module.fooType
- print "type of obj has type", type(type(obj))
- print "type of type of obj has type", type(type(type(obj)))
+ print("type of obj has type", type(type(obj)))
+ print("type of type of obj has type", type(type(type(obj))))
assert module.fooType.__doc__ == "foo is for testing."
def test_typeobject_method_descriptor(self):
@@ -36,7 +36,7 @@
assert repr(module.fooType.__call__) == "<slot wrapper '__call__' of
'foo' objects>"
assert obj2(foo=1, bar=2) == dict(foo=1, bar=2)
- print obj.foo
+ print(obj.foo)
assert obj.foo == 42
assert obj.int_member == obj.foo
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit