⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cls_orange.cpp

📁 orange源码 数据挖掘技术
💻 CPP
📖 第 1 页 / 共 3 页
字号:
}




PyObject *Orange_getattr1(TPyOrange *self, PyObject *pyname)
// This is a complete getattr, but without translation of obsolete names.
{ PyTRY
    if (!self)
      PYERROR(PyExc_SystemError, "NULL Orange object", PYNULL);

    if (self->orange_dict) {
      PyObject *res = PyDict_GetItem(self->orange_dict, pyname);
      if (res) {
        Py_INCREF(res);
        return res;
      }
    }
      
    PyObject *res = PyObject_GenericGetAttr((PyObject *)self, pyname);
    if (res)
      return res;

    PyErr_Clear();

    if (!PyString_Check(pyname))
      PYERROR(PyExc_TypeError, "object's attribute name must be a string", PYNULL);
    char *name=PyString_AsString(pyname);

    if (strcmp(name, "__dict__") == 0)
      return PyOrange__dict__(self);

    if (strcmp(name, "__members__") == 0)
      return PyOrange__members__(self);

    if (strcmp(name, "__class__") == 0) {
      Py_INCREF(self->ob_type);
      return (PyObject *)self->ob_type;
    }

    return Orange_getattr1(self, name);
  PyCATCH;
}


inline void PyDict_SIS_Steal(PyObject *dict, const char *name, PyObject *obj) {
  PyDict_SetItemString(dict, name, obj);
  Py_DECREF(obj);
}

PyObject *packOrangeDictionary(PyObject *self)
{
  PyTRY
    PyObject *packed = ((TPyOrange *)self)->orange_dict ? PyDict_Copy(((TPyOrange *)self)->orange_dict) : PyDict_New();

    TOrange *me = (TOrange *)((TPyOrange *)self)->ptr;

    for (const TPropertyDescription *pd = me->classDescription()->properties; pd->name; pd++) {
      if (!pd->readOnly) {
 
  //      const type_info &propertyType = pd->type;

        if (pd->type == &typeid(bool))
          PyDict_SIS_Steal(packed, pd->name, PyInt_FromLong(me->getProperty_bool(pd) ? 1 : 0));

        else if (pd->type == &typeid(int))
          PyDict_SIS_Steal(packed, pd->name, PyInt_FromLong(me->getProperty_int(pd)));

        else if (pd->type == &typeid(float))
          PyDict_SIS_Steal(packed, pd->name, PyFloat_FromDouble(me->getProperty_float(pd)));

        else if (pd->type == &typeid(string)) {
          string value;
          me->getProperty_string(pd, value);
          PyDict_SIS_Steal(packed, pd->name, PyString_FromString(value.c_str()));
        }

        else if (pd->type == &typeid(TValue)) {
          TValue value;
          me->getProperty_TValue(pd, value);
          PyDict_SIS_Steal(packed, pd->name, Value_FromValue(value));
        }

        else if (pd->type == &typeid(TExample)) {
          POrange mlobj;
          me->getProperty_POrange(pd, mlobj);
          if (mlobj)
            PyDict_SIS_Steal(packed, pd->name, Example_FromWrappedExample(PExample(mlobj)));
          else
            PyDict_SetItemString(packed, pd->name, Py_None);
        }
    
        else {
          POrange mlobj;
          me->getProperty_POrange(pd, mlobj);
          PyDict_SIS_Steal(packed, pd->name, (PyObject *)WrapOrange(mlobj));
        }
      }
    }

    return packed;
  PyCATCH
}



ORANGE_API PyObject *Orange__reduce__(PyObject *self, PyObject *, PyObject *)
{
    if (!((TOrangeType *)(self->ob_type))->ot_constructorAllowsEmptyArgs) {
      PyErr_Format(PyExc_TypeError, "instances of type '%s' cannot be pickled", self->ob_type->tp_name);
      return NULL;
    }

    return Py_BuildValue("O()N", self->ob_type, packOrangeDictionary(self));
}



PyObject *objectOnTheFly(PyObject *args, PyTypeObject *objectType)
{
  PyObject *emptyDict = PyDict_New();
  PyObject *targs;
  if (PyTuple_Check(args)) {
    targs = args;
    Py_INCREF(targs);
  }
  else
    targs = Py_BuildValue("(O)", args);

  PyObject *obj = NULL;
  try {
    obj = objectType->tp_new(objectType, targs, emptyDict);
  }
  catch (...) {
    // do nothing; if it failed, the user probably didn't mean it
  }

  // If this failed, maybe the constructor actually expected a tuple...
  if (!obj && PyTuple_Check(args)) {
     PyErr_Clear();
     Py_DECREF(targs);
     targs = Py_BuildValue("(O)", args);
     try {
       obj = objectType->tp_new(objectType, targs, emptyDict);
     }
     catch (...) 
     {}
  }

  if (obj) {
    if (   objectType->tp_init != NULL
        && objectType->tp_init(obj, targs, emptyDict) < 0) {
          Py_DECREF(obj);
          obj = NULL;
    }
  }

  Py_DECREF(emptyDict);
  Py_DECREF(targs);

  return obj;
}


int Orange_setattr1(TPyOrange *self, char *name, PyObject *args)
{
  TOrange *me = (TOrange *)self->ptr;

  const TPropertyDescription *propertyDescription = me->propertyDescription(name, true);
  if (!propertyDescription)
    return 1;

  PyTRY
    if (propertyDescription->readOnly) {
      /* Property might be marked as readOnly, but have a specialized set function.
         The following code is pasted from PyObject_GenericSetAttr.
         If I'd call it here and the attribute is really read-only, PyObject_GenericSetAttr
         would blatantly store it in the dictionary. */
      PyObject *pyname = PyString_FromString(name);
      PyObject *descr = _PyType_Lookup(self->ob_type, pyname);
	    PyObject *f = PYNULL;
	    if (descr != NULL && PyType_HasFeature(descr->ob_type, Py_TPFLAGS_HAVE_CLASS)) {
		    descrsetfunc f = descr->ob_type->tp_descr_set;
		    if (f != NULL && PyDescr_IsData(descr))
			    return f(descr, (PyObject *)self, args);
      }

      PyErr_Format(PyExc_TypeError, "%s.%s: read-only attribute", self->ob_type->tp_name, name);
      return -1;
    }
  
    try {
      const type_info &propertyType = *propertyDescription->type;

      if ((propertyType==typeid(bool)) || (propertyType==typeid(int))) {
        int value;
        if (!PyArg_Parse(args, "i", &value)) {
          PyErr_Format(PyExc_TypeError, "invalid parameter type for %s.%s', (int expected)", self->ob_type->tp_name, name);
          return -1;
        }
        if (propertyType==typeid(bool))
          me->setProperty(name, value!=0);
        else
          me->setProperty(name, value);
        return 0;
      }

      if (propertyType==typeid(float)) {
        float value;
        if (!PyArg_Parse(args, "f", &value)) {
          PyErr_Format(PyExc_TypeError, "invalid parameter type for %s.%s', (float expected)", self->ob_type->tp_name, name);
          return -1;
        }
        me->setProperty(name, value);
        return 0;
      }

      if (propertyType==typeid(string)) {
        char *value;
        if (!PyArg_Parse(args, "s", &value)) {
          PyErr_Format(PyExc_TypeError, "invalid parameter type for %s.%s', (string expected)", self->ob_type->tp_name, name);
          return -1;
        }
        me->setProperty(name, string(value));
        return 0;
      }

      if (propertyType==typeid(TValue)) {
        TValue value;
        if (!convertFromPython(args, value))
          return -1;
        me->setProperty(name, value);
        return 0;
      }

      if (propertyType==typeid(TExample)) {
        if (args==Py_None) {
          me->wr_setProperty(name, POrange());
          return 0;
        }
        else {
          if (!PyOrExample_Check(args)) {
            PyErr_Format(PyExc_TypeError, "invalid parameter type for '%s.%s', (expected 'Example', got '%s')", self->ob_type->tp_name, name, args->ob_type->tp_name);
            return -1;
          }
          me->wr_setProperty(name, POrange(PyExample_AS_Example(args)));
          return 0;
        }
      }

      if (1/*propertyType==typeid(POrange)*/) {
        const type_info *wrappedType = propertyDescription->classDescription->type;

        PyTypeObject *propertyPyType=(PyTypeObject *)FindOrangeType(*wrappedType);
        if (!propertyPyType) {
          PyErr_Format(PyExc_SystemError, "Orange class %s, needed for '%s.%s' not exported to Python", TYPENAME(*wrappedType), self->ob_type->tp_name, name);
          return -1;
        }

        if (args==Py_None) {
          me->wr_setProperty(name, POrange());
          return 0;
        }

        // User might have supplied the correct object
        if (PyObject_TypeCheck(args, propertyPyType)) {
          me->wr_setProperty(name, PyOrange_AS_Orange((TPyOrange *)args));
          return 0;
        }

        // User might have supplied parameters from which we can construct the object
        if (propertyPyType->tp_new) {
          PyObject *obj = objectOnTheFly(args, propertyPyType);
          if (obj) {
            bool success = true;
            try {
              me->wr_setProperty(name, PyOrange_AS_Orange((TPyOrange *)obj));
            }
            catch (...) {
              success = false;
            }
            Py_DECREF(obj);
            if (success)
              return 0;
          }
        }

        PyErr_Format(PyExc_TypeError, "invalid parameter type for '%s.%s', (expected '%s', got '%s')", self->ob_type->tp_name, name, propertyPyType->tp_name, args->ob_type->tp_name);
        return -1;
      }

      PyErr_Format(PyExc_TypeError, "internal Orange error: unrecognized type '%s.%s'", self->ob_type->tp_name, name);
      return -1;
    } catch (exception err)
    {
      PyErr_Format(PyExc_TypeError, "error setting '%s.%s'", self->ob_type->tp_name, name);
      return -1;
    }
  PyCATCH_1
}


int Orange_setattr1(TPyOrange *self, PyObject *pyname, PyObject *args)
// This is a complete setattr, but without translation of obsolete names.
{ 
  if (!self)
    PYERROR(PyExc_SystemError, "NULL Orange object", -1);

  /* We first have to check for a specific handler.
     The following code is pasted from PyObject_GenericSetAttr, but we can't
     call it since it would store *all* attributes in the dictionary. */
  PyObject *descr = _PyType_Lookup(self->ob_type, pyname);
  PyObject *f = PYNULL;
  if (descr != NULL && PyType_HasFeature(descr->ob_type, Py_TPFLAGS_HAVE_CLASS)) {
    descrsetfunc f = descr->ob_type->tp_descr_set;
    if (f != NULL && PyDescr_IsData(descr))
      return f(descr, (PyObject *)self, args);
  }
 
  char *name=PyString_AsString(pyname);
  int res = Orange_setattr1(self, name, args);
  if (res != 1)
    return res;

  return 1; // attribute not set (not even attempted to), try something else
}


PyObject *Orange_new(PyTypeObject *type, PyObject *args, PyObject *keywords)  BASED_ON(ROOT, "()")
{ return WrapNewOrange(mlnew TOrange(), type); }

int Orange_init(PyObject *self, PyObject *, PyObject *keywords)
{ PyTRY
    return ((TPyOrange *)self)->call_constructed || SetAttr_FromDict(self, keywords, true) ? 0 : -1;
  PyCATCH_1
}


void Orange_dealloc(TPyOrange *self)
{
  if (!self->is_reference) {
    PyObject_GC_UnTrack((PyObject *)self);
    mldelete self->ptr;
  }

  // This may cause troubles in multithread use
  if (self->orange_dict) {
    ((TPyOrange_DictProxy *)self->orange_dict)->backlink = NULL;
    Py_DECREF(self->orange_dict);
  }

  self->ob_type->tp_free((PyObject *)self);
}



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -