Changeset 36 for branches

Show
Ignore:
Timestamp:
05/24/07 17:41:53 (2 years ago)
Author:
hines
Message:

handle equivalence between python None and hoc NULLobject
a hoc object can be any python object (in the hoc world it is type
PythonObject?) including numbers and strings. (But remember that if the
python object is of type HocObject, then in the hoc world it will be
a native hoc object such as Vector, List, etc. instead of PythonObject?)

Files:

Legend:

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

    r34 r36  
    4343extern Symbol* nrnpy_pyobj_sym_; 
    4444extern PyObject* nrnpy_hoc2pyobject(Object*); 
    45  
     45extern void nrnpy_pyobject_in_objptr(Object**, PyObject*); 
    4646/* 
    4747Because python types have so many methods, attempt to do all set and get 
     
    267267                ho = *d; 
    268268//printf("Py2Nrn %lx %lx\n", (long)ho->ctemplate->sym, (long)nrnpy_pyobj_sym_); 
    269                 if (ho->ctemplate->sym == nrnpy_pyobj_sym_) { 
     269                if (!ho) { 
     270                        result = Py_BuildValue(""); 
     271                }else if (ho->ctemplate->sym == nrnpy_pyobj_sym_) { 
    270272                        result = nrnpy_hoc2pyobject(ho); 
    271273                }else{ 
     
    539541                        hoc_pc = pcsav; 
    540542                        Object* ho = *hoc_objpop(); 
    541                         if (ho->ctemplate->sym == nrnpy_pyobj_sym_) { 
     543                        if (!ho) { 
     544                                result = Py_BuildValue(""); 
     545                        }else if (ho->ctemplate->sym == nrnpy_pyobj_sym_) { 
    542546                                result = nrnpy_hoc2pyobject(ho); 
    543547                        }else{ 
     
    658662                Object** op; 
    659663                op = hoc_objpop(); 
    660                 if (PyArg_Parse(value, "O!", hocobject_type, &po) == 1) { 
    661                         if (po->sym_) { 
     664//              if (PyArg_Parse(value, "O!", hocobject_type, &po) == 1) { 
     665                PyObject* po; 
     666                PyHocObject* pho; 
     667                if (PyArg_Parse(value, "O", &po) == 1) { 
     668                        if (po == Py_None) { 
     669                                hoc_obj_unref(*op); 
     670                                *op = 0; 
     671                        }else if (PyObject_TypeCheck(po, hocobject_type)) { 
     672                                pho = (PyHocObject*)po; 
     673                                if (pho->sym_) { 
    662674 PyErr_SetString(PyExc_TypeError, "argument cannot be a hoc object intermediate"); 
    663                                 return -1; 
     675                                        return -1; 
     676                                } 
     677                                hoc_obj_ref(pho->ho_); 
     678                                hoc_obj_unref(*op); 
     679                                *op = pho->ho_; 
     680                        }else{ // it is a PythonObject in hoc 
     681                                nrnpy_pyobject_in_objptr(op, po); 
    664682                        } 
    665                         hoc_obj_ref(po->ho_); 
    666                         hoc_obj_unref(*op); 
    667                         *op = po->ho_; 
    668683                }else{ 
    669684                        err = 1; 
  • branches/py2nrn/src/nrnpython/nrnpy_p2h.cpp

    r24 r36  
    1212extern Object* hoc_new_object(Symbol*, void*); 
    1313PyObject* nrnpy_hoc2pyobject(Object*); 
    14  
     14void nrnpy_pyobject_in_objptr(Object**, PyObject*); 
    1515extern Symbol* nrnpy_pyobj_sym_; 
    1616void (*nrnpy_py2n_component)(Object*, Symbol*, int, int); 
     
    5959PyObject* nrnpy_hoc2pyobject(Object* ho) { 
    6060        return ((Py2Nrn*)ho->u.this_pointer)->po_; 
     61} 
     62 
     63void nrnpy_pyobject_in_objptr(Object** ho, PyObject* po) { 
     64        Py2Nrn* pn = new Py2Nrn(); 
     65        pn->po_ = po; 
     66        Py_INCREF(po); 
     67        pn->type_ = 1; 
     68        Object* on = hoc_new_object(nrnpy_pyobj_sym_, (void*)pn); 
     69        hoc_obj_ref(on); 
     70        hoc_obj_unref(*ho); 
     71        *ho = on; 
    6172} 
    6273