cpython/Python/structmember.c
Christian Heimes 1af737cd1e Merged revisions 60176-60209 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk

........
  r60178 | georg.brandl | 2008-01-21 22:05:49 +0100 (Mon, 21 Jan 2008) | 2 lines

  #1715: include sub-extension modules in pydoc text output.
........
  r60179 | georg.brandl | 2008-01-21 22:14:21 +0100 (Mon, 21 Jan 2008) | 2 lines

  Add a "const" to make gcc happy.
........
  r60180 | georg.brandl | 2008-01-21 22:19:07 +0100 (Mon, 21 Jan 2008) | 2 lines

  Add the correct build dir when building with pydebug.
........
  r60181 | georg.brandl | 2008-01-21 22:23:15 +0100 (Mon, 21 Jan 2008) | 3 lines

  Patch #1720595: add T_BOOL to the range of structmember types.
  Patch by Angelo Mottola, reviewed by MvL, tests by me.
........
  r60182 | georg.brandl | 2008-01-21 22:28:32 +0100 (Mon, 21 Jan 2008) | 2 lines

  Reformat some ugly code.
........
  r60187 | brett.cannon | 2008-01-22 00:50:16 +0100 (Tue, 22 Jan 2008) | 4 lines

  Make's MAKEFLAGS variable is set to a string containing the single-letter
  arguments to Make. This means there are no hyphens. Fix the '-s' check to
  silence distutils to now work.
........
  r60188 | gregory.p.smith | 2008-01-22 01:19:41 +0100 (Tue, 22 Jan 2008) | 3 lines

  accepts and closes issue #1221598: adds an optional callback to ftplib.FTP
  storbinary() and storlines() methods.
........
  r60189 | gregory.p.smith | 2008-01-22 02:12:02 +0100 (Tue, 22 Jan 2008) | 2 lines

  Replace spam.acquire() try: ... finally: spam.release() with "with spam:"
........
  r60190 | gregory.p.smith | 2008-01-22 02:20:42 +0100 (Tue, 22 Jan 2008) | 4 lines

  - Fix Issue #1703448: A joined thread could show up in the
    threading.enumerate() list after the join() for a brief period until
    it actually exited.
........
  r60193 | georg.brandl | 2008-01-22 08:53:31 +0100 (Tue, 22 Jan 2008) | 2 lines

  Fix \xhh specs, #1889.
........
  r60198 | christian.heimes | 2008-01-22 16:01:25 +0100 (Tue, 22 Jan 2008) | 1 line

  Fixed a missing (X) in define
........
  r60199 | christian.heimes | 2008-01-22 16:25:18 +0100 (Tue, 22 Jan 2008) | 2 lines

  Don't repeat yourself
  Added the macros PyModule_AddIntMacro and PyModule_AddStringMacro. They shorten PyModule_AddIntConstant(m, "AF_INET", AF_INET) to PyModule_AddIntMacro(m, AF_INET)
........
  r60201 | raymond.hettinger | 2008-01-22 20:51:41 +0100 (Tue, 22 Jan 2008) | 1 line

  Document when to use izip_longest().
........
  r60202 | georg.brandl | 2008-01-22 20:56:03 +0100 (Tue, 22 Jan 2008) | 2 lines

  Fix for #1087741 patch.
........
  r60203 | raymond.hettinger | 2008-01-22 21:18:53 +0100 (Tue, 22 Jan 2008) | 1 line

  Give zip() the same guarantee as izip() for left-to-right evaluation.
........
  r60204 | raymond.hettinger | 2008-01-22 23:09:26 +0100 (Tue, 22 Jan 2008) | 1 line

  Improve variable name in sample code
........
  r60205 | gregory.p.smith | 2008-01-23 00:15:34 +0100 (Wed, 23 Jan 2008) | 2 lines

  docstring and comment updates suggested by Giampaolo Rodola'
........
  r60207 | raymond.hettinger | 2008-01-23 01:04:40 +0100 (Wed, 23 Jan 2008) | 1 line

  Let pprint() support sets and frozensets (suggested by David Mertz).
........
  r60208 | guido.van.rossum | 2008-01-23 02:18:27 +0100 (Wed, 23 Jan 2008) | 4 lines

  I'm tired of these tests breaking at Google due to our large number of
  users and groups in LDAP/NIS.  So I'm limiting the extra-heavy part of
  the tests to passwd/group files with at most 1000 entries.
........
2008-01-23 08:24:23 +00:00

279 lines
6.6 KiB
C

/* Map C struct members to Python object attributes */
#include "Python.h"
#include "structmember.h"
PyObject *
PyMember_GetOne(const char *addr, PyMemberDef *l)
{
PyObject *v;
addr += l->offset;
switch (l->type) {
case T_BOOL:
v = PyBool_FromLong(*(char*)addr);
break;
case T_BYTE:
v = PyLong_FromLong(*(char*)addr);
break;
case T_UBYTE:
v = PyLong_FromUnsignedLong(*(unsigned char*)addr);
break;
case T_SHORT:
v = PyLong_FromLong(*(short*)addr);
break;
case T_USHORT:
v = PyLong_FromUnsignedLong(*(unsigned short*)addr);
break;
case T_INT:
v = PyLong_FromLong(*(int*)addr);
break;
case T_UINT:
v = PyLong_FromUnsignedLong(*(unsigned int*)addr);
break;
case T_LONG:
v = PyLong_FromLong(*(long*)addr);
break;
case T_ULONG:
v = PyLong_FromUnsignedLong(*(unsigned long*)addr);
break;
case T_PYSSIZET:
v = PyLong_FromSsize_t(*(Py_ssize_t*)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 = PyUnicode_FromString(*(char**)addr);
break;
case T_STRING_INPLACE:
v = PyUnicode_FromString((char*)addr);
break;
case T_CHAR:
v = PyUnicode_FromStringAndSize((char*)addr, 1);
break;
case T_OBJECT:
v = *(PyObject **)addr;
if (v == NULL)
v = Py_None;
Py_INCREF(v);
break;
case T_OBJECT_EX:
v = *(PyObject **)addr;
if (v == NULL)
PyErr_SetString(PyExc_AttributeError, l->name);
Py_XINCREF(v);
break;
#ifdef HAVE_LONG_LONG
case T_LONGLONG:
v = PyLong_FromLongLong(*(PY_LONG_LONG *)addr);
break;
case T_ULONGLONG:
v = PyLong_FromUnsignedLongLong(*(unsigned PY_LONG_LONG *)addr);
break;
#endif /* HAVE_LONG_LONG */
case T_NONE:
v = Py_None;
Py_INCREF(v);
break;
default:
PyErr_SetString(PyExc_SystemError, "bad memberdescr type");
v = NULL;
}
return v;
}
#define WARN(msg) \
do { \
if (PyErr_WarnEx(PyExc_RuntimeWarning, msg, 1) < 0) \
return -1; \
} while (0)
int
PyMember_SetOne(char *addr, PyMemberDef *l, PyObject *v)
{
PyObject *oldv;
if ((l->flags & READONLY) || l->type == T_STRING)
{
PyErr_SetString(PyExc_AttributeError, "readonly attribute");
return -1;
}
if (v == NULL && l->type != T_OBJECT_EX && l->type != T_OBJECT) {
PyErr_SetString(PyExc_TypeError,
"can't delete numeric/char attribute");
return -1;
}
addr += l->offset;
switch (l->type) {
case T_BOOL:{
if (!PyBool_Check(v)) {
PyErr_SetString(PyExc_TypeError,
"attribute value type must be bool");
return -1;
}
if (v == Py_True)
*(char*)addr = (char) 1;
else
*(char*)addr = (char) 0;
break;
}
case T_BYTE:{
long long_val = PyLong_AsLong(v);
if ((long_val == -1) && PyErr_Occurred())
return -1;
*(char*)addr = (char)long_val;
/* XXX: For compatibility, only warn about truncations
for now. */
if ((long_val > CHAR_MAX) || (long_val < CHAR_MIN))
WARN("Truncation of value to char");
break;
}
case T_UBYTE:{
long long_val = PyLong_AsLong(v);
if ((long_val == -1) && PyErr_Occurred())
return -1;
*(unsigned char*)addr = (unsigned char)long_val;
if ((long_val > UCHAR_MAX) || (long_val < 0))
WARN("Truncation of value to unsigned char");
break;
}
case T_SHORT:{
long long_val = PyLong_AsLong(v);
if ((long_val == -1) && PyErr_Occurred())
return -1;
*(short*)addr = (short)long_val;
if ((long_val > SHRT_MAX) || (long_val < SHRT_MIN))
WARN("Truncation of value to short");
break;
}
case T_USHORT:{
long long_val = PyLong_AsLong(v);
if ((long_val == -1) && PyErr_Occurred())
return -1;
*(unsigned short*)addr = (unsigned short)long_val;
if ((long_val > USHRT_MAX) || (long_val < 0))
WARN("Truncation of value to unsigned short");
break;
}
case T_INT:{
long long_val = PyLong_AsLong(v);
if ((long_val == -1) && PyErr_Occurred())
return -1;
*(int *)addr = (int)long_val;
if ((long_val > INT_MAX) || (long_val < INT_MIN))
WARN("Truncation of value to int");
break;
}
case T_UINT:{
unsigned long ulong_val = PyLong_AsUnsignedLong(v);
if ((ulong_val == (unsigned int)-1) && PyErr_Occurred()) {
/* XXX: For compatibility, accept negative int values
as well. */
PyErr_Clear();
ulong_val = PyLong_AsLong(v);
if ((ulong_val == (unsigned int)-1) && PyErr_Occurred())
return -1;
*(unsigned int *)addr = (unsigned int)ulong_val;
WARN("Writing negative value into unsigned field");
} else
*(unsigned int *)addr = (unsigned int)ulong_val;
if (ulong_val > UINT_MAX)
WARN("Truncation of value to unsigned int");
break;
}
case T_LONG:{
*(long*)addr = PyLong_AsLong(v);
if ((*(long*)addr == -1) && PyErr_Occurred())
return -1;
break;
}
case T_ULONG:{
*(unsigned long*)addr = PyLong_AsUnsignedLong(v);
if ((*(unsigned long*)addr == (unsigned long)-1)
&& PyErr_Occurred()) {
/* XXX: For compatibility, accept negative int values
as well. */
PyErr_Clear();
*(unsigned long*)addr = PyLong_AsLong(v);
if ((*(unsigned long*)addr == (unsigned int)-1)
&& PyErr_Occurred())
return -1;
WARN("Writing negative value into unsigned field");
}
break;
}
case T_PYSSIZET:{
*(Py_ssize_t*)addr = PyLong_AsSsize_t(v);
if ((*(Py_ssize_t*)addr == (Py_ssize_t)-1)
&& PyErr_Occurred())
return -1;
break;
}
case T_FLOAT:{
double double_val = PyFloat_AsDouble(v);
if ((double_val == -1) && PyErr_Occurred())
return -1;
*(float*)addr = (float)double_val;
break;
}
case T_DOUBLE:
*(double*)addr = PyFloat_AsDouble(v);
if ((*(double*)addr == -1) && PyErr_Occurred())
return -1;
break;
case T_OBJECT:
case T_OBJECT_EX:
Py_XINCREF(v);
oldv = *(PyObject **)addr;
*(PyObject **)addr = v;
Py_XDECREF(oldv);
break;
case T_CHAR:
if (PyUnicode_Check(v) && PyUnicode_GetSize(v) == 1) {
*(char*)addr = PyUnicode_AsString(v)[0];
}
else {
PyErr_BadArgument();
return -1;
}
break;
#ifdef HAVE_LONG_LONG
case T_LONGLONG:{
PY_LONG_LONG value;
*(PY_LONG_LONG*)addr = value = PyLong_AsLongLong(v);
if ((value == -1) && PyErr_Occurred())
return -1;
break;
}
case T_ULONGLONG:{
unsigned PY_LONG_LONG value;
/* ??? PyLong_AsLongLong accepts an int, but PyLong_AsUnsignedLongLong
doesn't ??? */
if (PyLong_Check(v))
*(unsigned PY_LONG_LONG*)addr = value = PyLong_AsUnsignedLongLong(v);
else
*(unsigned PY_LONG_LONG*)addr = value = PyLong_AsLong(v);
if ((value == (unsigned PY_LONG_LONG)-1) && PyErr_Occurred())
return -1;
break;
}
#endif /* HAVE_LONG_LONG */
default:
PyErr_Format(PyExc_SystemError,
"bad memberdescr type for %s", l->name);
return -1;
}
return 0;
}