Mercurial > cpython-lru_cache-weakref
view Python/structmember.c @ 8358:da4bdceeaad7 legacy-trunk
Another veeeeeery old patch...
Date: Thu, 14 Sep 1995 12:18:20 -0400
From: Alan Morse <alan@dvcorp.com>
To: python-list@cwi.nl
Subject: getargs bug in 1.2 and 1.3 BETA
We have found a bug in the part of the getargs code that we added
and submitted, and which was incorporated into 1.1.
The parsing of "O?" format specifiers is not handled correctly;
there is no "else" for the "if" and therefore it can never fail.
What's worse, the advancing of the varargs pointer is not
handled properly, so from then on it is out of sync, wreaking
all sorts of havoc. (If it had failed properly, then the out-of-sync
varargs would not have been an issue.)
Below is the context diff for the change.
Note that I have made a few stylistic changes beyond adding the
else case, namely:
1) Making the "O" case follow the convention established by the other
format specifiers of getting all their vararg arguments before
performing the test, rather than getting some before and some after
the test passes.
2) Making the logic of the tests parallel, so the "if" part indicates
that the format is accepted and the "else" part indicates that the
format has failed. They were inconsistent with each other and with the
the other format specifiers.
-Alan Morse (amorse@dvcorp.com)
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Fri, 15 May 1998 22:04:07 +0000 |
| parents | 5880384d7c71 |
| children | ff31f2501742 |
line wrap: on
line source
/*********************************************************** Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, The Netherlands. All Rights Reserved Permission to use, copy, modify, and distribute this software and its documentation for any purpose and without fee is hereby granted, provided that the above copyright notice appear in all copies and that both that copyright notice and this permission notice appear in supporting documentation, and that the names of Stichting Mathematisch Centrum or CWI or Corporation for National Research Initiatives or CNRI not be used in advertising or publicity pertaining to distribution of the software without specific, written prior permission. While CWI is the initial source for this software, a modified version is made available by the Corporation for National Research Initiatives (CNRI) at the Internet address ftp://ftp.python.org. STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ******************************************************************/ /* Map C struct members to Python object attributes */ #include "Python.h" #include "structmember.h" static PyObject * listmembers(mlist) struct memberlist *mlist; { int i, n; PyObject *v; for (n = 0; mlist[n].name != NULL; n++) ; v = PyList_New(n); if (v != NULL) { for (i = 0; i < n; i++) PyList_SetItem(v, i, PyString_FromString(mlist[i].name)); if (PyErr_Occurred()) { Py_DECREF(v); v = NULL; } else { PyList_Sort(v); } } return v; } PyObject * PyMember_Get(addr, mlist, name) char *addr; struct memberlist *mlist; char *name; { struct memberlist *l; if (strcmp(name, "__members__") == 0) return listmembers(mlist); for (l = mlist; l->name != NULL; l++) { if (strcmp(l->name, name) == 0) { PyObject *v; addr += l->offset; switch (l->type) { case T_BYTE: v = PyInt_FromLong((long) (((*(char*)addr & 0xff) ^ 0x80) - 0x80)); break; case T_UBYTE: v = PyInt_FromLong((long) *(char*)addr & 0xff); break; case T_SHORT: v = PyInt_FromLong((long) *(short*)addr); break; case T_USHORT: v = PyInt_FromLong((long) *(unsigned short*)addr); break; case T_INT: v = PyInt_FromLong((long) *(int*)addr); break; case T_UINT: v = PyInt_FromLong((long) *(unsigned int*)addr); break; case T_LONG: v = PyInt_FromLong(*(long*)addr); break; case T_ULONG: v = PyLong_FromDouble((double) *(unsigned long*)addr); break; case T_FLOAT: v = PyFloat_FromDouble((double)*(float*)addr); break; case T_DOUBLE: v = PyFloat_FromDouble(*(double*)addr); break; case T_STRING: if (*(char**)addr == NULL) { Py_INCREF(Py_None); v = Py_None; } else v = PyString_FromString(*(char**)addr); break; case T_STRING_INPLACE: v = PyString_FromString((char*)addr); break; #ifdef macintosh case T_PSTRING: if (*(char**)addr == NULL) { Py_INCREF(Py_None); v = Py_None; } else v = PyString_FromStringAndSize( (*(char**)addr)+1, **(unsigned char**)addr); break; case T_PSTRING_INPLACE: v = PyString_FromStringAndSize( ((char*)addr)+1, *(unsigned char*)addr); break; #endif /* macintosh */ case T_CHAR: v = PyString_FromStringAndSize((char*)addr, 1); break; case T_OBJECT: v = *(PyObject **)addr; if (v == NULL) v = Py_None; Py_INCREF(v); break; default: PyErr_SetString(PyExc_SystemError, "bad memberlist type"); v = NULL; } return v; } } PyErr_SetString(PyExc_AttributeError, name); return NULL; } int PyMember_Set(addr, mlist, name, v) char *addr; struct memberlist *mlist; char *name; PyObject *v; { struct memberlist *l; for (l = mlist; l->name != NULL; l++) { if (strcmp(l->name, name) == 0) { #ifdef macintosh if (l->readonly || l->type == T_STRING || l->type == T_PSTRING) { #else if (l->readonly || l->type == T_STRING ) { #endif /* macintosh */ PyErr_SetString(PyExc_TypeError, "readonly attribute"); return -1; } if (v == NULL && l->type != T_OBJECT) { PyErr_SetString(PyExc_TypeError, "can't delete numeric/char attribute"); return -1; } addr += l->offset; switch (l->type) { case T_BYTE: case T_UBYTE: if (!PyInt_Check(v)) { PyErr_BadArgument(); return -1; } *(char*)addr = (char) PyInt_AsLong(v); break; case T_SHORT: case T_USHORT: if (!PyInt_Check(v)) { PyErr_BadArgument(); return -1; } *(short*)addr = (short) PyInt_AsLong(v); break; case T_UINT: case T_INT: if (!PyInt_Check(v)) { PyErr_BadArgument(); return -1; } *(int*)addr = (int) PyInt_AsLong(v); break; case T_LONG: if (!PyInt_Check(v)) { PyErr_BadArgument(); return -1; } *(long*)addr = PyInt_AsLong(v); break; case T_ULONG: if (PyInt_Check(v)) *(long*)addr = PyInt_AsLong(v); else if (PyLong_Check(v)) *(long*)addr = PyLong_AsLong(v); else { PyErr_BadArgument(); return -1; } break; case T_FLOAT: if (PyInt_Check(v)) *(float*)addr = (float) PyInt_AsLong(v); else if (PyFloat_Check(v)) *(float*)addr = (float) PyFloat_AsDouble(v); else { PyErr_BadArgument(); return -1; } break; case T_DOUBLE: if (PyInt_Check(v)) *(double*)addr = (double) PyInt_AsLong(v); else if (PyFloat_Check(v)) *(double*)addr = PyFloat_AsDouble(v); else { PyErr_BadArgument(); return -1; } break; case T_OBJECT: Py_XDECREF(*(PyObject **)addr); Py_XINCREF(v); *(PyObject **)addr = v; break; case T_CHAR: if (PyString_Check(v) && PyString_Size(v) == 1) { *(char*)addr = PyString_AsString(v)[0]; } else { PyErr_BadArgument(); return -1; } default: PyErr_SetString(PyExc_SystemError, "bad memberlist type"); return -1; } return 0; } } PyErr_SetString(PyExc_AttributeError, name); return -1; }
