Changeset 30

Show
Ignore:
Timestamp:
05/22/07 16:36:02 (2 years ago)
Author:
hines
Message:

avoid hoc_execerror when array index error. No checking yet for the
hoc Matrix.

Files:

Legend:

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

    r28 r30  
    3939extern PyObject* nrnpy_cas(PyObject*, PyObject*); 
    4040extern int section_object_seen; 
     41extern int vector_capacity(void*); 
    4142/* 
    4243Because python types have so many methods, attempt to do all set and get 
     
    656657} 
    657658 
     659static Symbol* sym_vec_x; 
     660static Symbol* sym_mat_x; 
     661 
     662static int araychk(Arrayinfo* a, PyHocObject* po, int ix) { 
     663        assert(a->nsub > po->nindex_); 
     664        int n; 
     665        // Hoc Vector and Matrix are special cases because the sub[] 
     666        // do not get filled in til just before hoc_araypt is called. 
     667        // at least check the vector 
     668        if (po->sym_ == sym_vec_x) { 
     669                n = vector_capacity(po->ho_->u.this_pointer); 
     670        }else if (po->sym_ == sym_mat_x) { 
     671                if (ix >= 0) return 0; 
     672        }else{ 
     673                n = a->sub[po->nindex_]; 
     674        } 
     675        if (ix < 0 || n <= ix) { 
     676//printf("ix=%d nsub=%d nindex=%d sub[nindex]=%d\n", ix, a->nsub, po->nindex_, a->sub[po->nindex_]); 
     677                char e[200]; 
     678                sprintf(e, "%s%s%s", po->ho_?hoc_object_name(po->ho_):"", 
     679                        (po->ho_ && po->sym_) ? "." : "", 
     680                        po->sym_ ? po->sym_->name : ""); 
     681                PyErr_SetString(PyExc_IndexError, e); 
     682                return -1; 
     683        } 
     684        return 0; 
     685} 
     686 
    658687/* 
    659688Had better be an array. But the same ambiguity as with getattro 
     
    672701        assert(po->type_ == 3); 
    673702        Arrayinfo* a = hocobj_aray(po->sym_, po->ho_); 
     703        if (araychk(a, po, ix)) { 
     704                return NULL; 
     705        } 
    674706        if (a->nsub - 1 >  po->nindex_) { // another intermediate 
    675707                PyHocObject* ponew = intermediate(po, po->sym_, ix); 
     
    726758        if (a->nsub - 1 !=  po->nindex_) { 
    727759                PyErr_SetString(PyExc_TypeError, "wrong number of subscripts"); 
     760                return -1; 
     761        } 
     762        if (araychk(a, po, i)) { 
    728763                return -1; 
    729764        } 
     
    10261061        PyModule_AddObject(m, "HocObject", (PyObject*)hocobject_type); 
    10271062 
     1063        Symbol* s; 
     1064        s = hoc_lookup("Vector"); assert(s); 
     1065        sym_vec_x = hoc_table_lookup("x", s->u.ctemplate->symtable); assert(sym_vec_x); 
     1066        s = hoc_lookup("Matrix"); assert(s); 
     1067        sym_mat_x = hoc_table_lookup("x", s->u.ctemplate->symtable); assert(sym_mat_x); 
     1068 
    10281069#ifdef WITH_NUMPY 
    10291070