Changeset 35

Show
Ignore:
Timestamp:
05/23/07 13:24:31 (2 years ago)
Author:
emuller
Message:

Added HocVector?.fromarray, calls return HocVectors? conditionally
defined PyHocX_Check[Exact] to check for HocObjects?, HocVectors?

HocVector? is now dervied from HocObject

setattro now tries PyObject?_GenericSetAttr as a last resort.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/pygetsetcall/src/nrnpython/nrnpy_hoc.cpp

    r32 r35  
    6868static PyTypeObject* hocvector_type; 
    6969 
     70  // Type checks 
     71#define PyHocObject_Check(op) PyObject_TypeCheck(op, hocobject_type) 
     72#define PyHocObject_CheckExact(op) ((op)->ob_type == hocobject_type) 
     73#define PyHocVector_Check(op) PyObject_TypeCheck(op, hocvector_type) 
     74#define PyHocVector_CheckExact(op) ((op)->ob_type == hocvector_type) 
     75 
     76 
    7077static PyObject* nrnexec(PyObject* self, PyObject* args) { 
    7178        const char* cmd; 
     
    261268        case OBJECTVAR: 
    262269        case OBJECTTMP: 
    263                 result = hocobj_new(hocobject_type, 0, 0); 
    264                 d = hoc_objpop(); 
     270                d = hoc_objpop(); 
     271 
     272                if (is_obj_type(*d,"Vector")){ 
     273                  result = hocobj_new(hocvector_type, 0, 0); 
     274                } 
     275                else { 
     276                  result = hocobj_new(hocobject_type, 0, 0); 
     277                } 
     278 
    265279                ((PyHocObject*)result)->ho_ = *d; 
    266280                ((PyHocObject*)result)->type_ = 1; 
     
    295309        case OBJECTVAR: 
    296310                PyHocObject* pho; 
    297                 if (PyArg_Parse(po, "O!", hocobject_type, &pho) == 1) { 
     311                if (PyHocObject_Check(po)) { 
     312                        pho = (PyHocObject*)po; 
    298313                        Object** pobj = hoc_objpop(); 
    299314                        if (pho->sym_) { 
     
    359374                } 
    360375        }else{ 
    361                 return NULL; 
     376            PyErr_SetString(PyExc_TypeError, "object not callable"); 
     377            return NULL; 
     378 
    362379        } 
    363380} 
     
    463480 
    464481        if (self->type_ == 1 && !self->ho_) { 
    465                 Py_DECREF(name); 
    466                 PyErr_SetString(PyExc_TypeError, "not a compound type"); 
    467                 //return Py_BuildValue(""); 
    468                 return NULL; 
     482          //Py_DECREF(name); 
     483          //PyErr_SetString(PyExc_TypeError, "symbol not yet defined"); 
     484          //return NULL; 
     485 
     486          return PyObject_GenericGetAttr((PyObject*)self, name); 
     487 
    469488        } 
    470489        Symbol* sym = getsym(n, self->ho_, 0); 
     
    590609 
    591610        if (self->type_ == 1 && !self->ho_) { 
    592                 return 1; 
     611          return PyObject_GenericSetAttr((PyObject*)self, name, value); 
     612          //return 1; 
    593613        } 
    594614        Py_INCREF(name); 
    595615        char* n = PyString_AsString(name); 
    596616//printf("hocobj_setattro %s\n", n); 
    597         Symbol* sym = getsym(n, self->ho_, 1); 
     617        Symbol* sym = getsym(n, self->ho_, 0); 
    598618        Py_DECREF(name); 
    599619        if (!sym) { 
    600                 return -1; 
     620          //return -1; 
     621          return PyObject_GenericSetAttr((PyObject*)self, name, value); 
    601622        } 
    602623        if (self->ho_) { // use the component fork. 
     
    861882  } 
    862883  else { 
    863     PyErr_SetString(PyExc_TypeError, "expected HocVector."); 
     884    PyErr_SetString(PyExc_TypeError, "expected HocVector for self."); 
    864885    return NULL; 
    865886  } 
     
    876897 
    877898} 
     899 
     900 
     901static PyObject* hocvec_fromnumpy(PyObject* self, PyObject* args) { 
     902   
     903  
     904 
     905#ifdef WITH_NUMPY 
     906 
     907  PyHocObject * ho = (PyHocObject*)self; 
     908  PyArrayObject *pa = NULL; 
     909 
     910  int size; 
     911 
     912  if (!PyArg_ParseTuple(args, "O!",&PyArray_Type,&pa)) { 
     913    return NULL; 
     914  } 
     915 
     916 // check the array arg has the correct type and dimensions 
     917 
     918  if (pa->nd != 1 || pa->descr->type_num != PyArray_DOUBLE) { 
     919    PyErr_SetString(PyExc_TypeError,"array arg 'a' not of correct type: (1D,PyArray_DOUBLE)"); 
     920    return false; 
     921  } 
     922 
     923 
     924  if (is_obj_type(ho->ho_,"Vector")) { 
     925    // we can deal with vectors 
     926 
     927    size = pa->dimensions[0]; 
     928 
     929    Vect* v = (Vect*)ho->ho_->u.this_pointer; 
     930    v->resize(size); 
     931    int i; 
     932 
     933    for (i=0;i<size;i++) { 
     934      (*v)[i] = *(double*)(pa->data + i*pa->strides[0]); 
     935    } 
     936 
     937    return Py_BuildValue(""); 
     938 
     939  } 
     940  else { 
     941    PyErr_SetString(PyExc_TypeError, "expected HocVector for self."); 
     942    return NULL; 
     943  } 
     944 
     945 
     946#else 
     947 
     948 PyErr_SetString(PyExc_RuntimeError, "numpy support was not enabled at build time."); 
     949 return NULL; 
     950 
     951 
     952#endif //WITH_NUMPY 
     953 
     954 
     955} 
     956 
     957 
    878958 
    879959 
     
    10731153static PyMethodDef hocvec_methods[] = { 
    10741154  {"toarray",hocvec_tonumpy,METH_VARARGS,"toarray(self) returns a numpy array of self."}, 
     1155  {"fromarray",hocvec_fromnumpy,METH_VARARGS,"fromarray(self,a) sets vector contents using array (sizes must match)."}, 
    10751156  {NULL, NULL, 0, NULL} 
    10761157}; 
     
    11551236    0,//hocobj_members,             /* tp_members */ 
    11561237    0,                         /* tp_getset */ 
    1157     0,                         /* tp_base */ 
     1238    &nrnpy_HocObjectType,       /* tp_base */ 
    11581239    0,                         /* tp_dict */ 
    11591240    0,                         /* tp_descr_get */ 
     
    11691250 
    11701251 
     1252 
     1253 
    11711254myPyMODINIT_FUNC nrnpy_hoc() { 
    11721255        PyObject* m;