[Python-Dev] setting class attributes from C?

Fred L. Drake, Jr. fdrake@acm.org
Tue, 5 Mar 2002 00:08:48 -0500


Has anyone tried setting class attributes on a new-style class in C?
The file sandbox/datetime/datetime.py has code that does this:

class datetime(basetime):
    ...

datetime.min = datetime(...)
datetime.max = datetime(...)

I can easily add descriptors in the C version so that min and max are
defined on instances, but using PyObject_SetAttrString() using the new
class:

    tmp = create_datetime(...);
    if (datetime_min == NULL)
        return;
    if (PyObject_SetAttrString((PyObject *) &PyDateTime_Type,
                               "min", datetime_min) < 0)
        return;

produces this exception:

Traceback (most recent call last):
  File "test_cdatetime.py", line 9, in ?
    from _datetime import datetime, MINYEAR, MAXYEAR
TypeError: can't set attributes of built-in/extension type 'datetime.datetime'

(where _datetime is the C extension that implements the C version of
the type).

Any ideas?


  -Fred

-- 
Fred L. Drake, Jr.  <fdrake at acm.org>
PythonLabs at Zope Corporation