📄 cls_orange.cpp
字号:
PyObject *Orange_getattr(TPyOrange *self, PyObject *name)
// This calls getattr1; first with the given, than with the translated name
{
PyTRY
PyObject *res = Orange_getattr1(self, name);
if (!res) {
PyObject *translation = PyOrange_translateObsolete((PyObject *)self, name);
if (translation) {
PyErr_Clear();
res = Orange_getattr1(self, translation);
Py_DECREF(translation);
}
}
return res;
PyCATCH
}
int Orange_setattrLow(TPyOrange *self, PyObject *pyname, PyObject *args, bool warn)
// This calls setattr1; first with the given, than with the translated name
{ PyTRY
if (!PyString_Check(pyname))
PYERROR(PyExc_AttributeError, "object's attribute name must be string", -1);
// Try to set it as C++ class member
int res = Orange_setattr1(self, pyname, args);
if (res!=1)
return res;
PyErr_Clear();
// Try to translate it as an obsolete alias for C++ class member
PyObject *translation = PyOrange_translateObsolete((PyObject *)self, pyname);
if (translation) {
char sbuf[255];
char *name = PyString_AsString(pyname);
char *transname = PyString_AsString(translation);
sprintf(sbuf, "'%s' is an (obsolete) alias for '%s'", name, transname);
if (PyErr_Warn(PyExc_OrangeAttributeWarning, sbuf))
return -1;
res = Orange_setattr1(self, translation, args);
Py_DECREF(translation);
return res;
}
// Use instance's dictionary
char *name = PyString_AsString(pyname);
if (args) {
/* Issue a warning unless name the name is in 'recognized_list' in some of the ancestors
or the instance's class only derived from some Orange's class, but is written in Python */
if (warn && PyOrange_CheckType(self->ob_type)) {
char **recognized = NULL;
for(PyTypeObject *otype = self->ob_type; otype && (!recognized || !*recognized); otype = otype->tp_base) {
recognized = PyOrange_CheckType(otype) ? ((TOrangeType *)otype)->ot_recognizedattributes : NULL;
if (recognized)
for(; *recognized && strcmp(*recognized, name); recognized++);
}
if (!recognized || !*recognized) {
char sbuf[255];
sprintf(sbuf, "'%s' is not a builtin attribute of '%s'", name, self->ob_type->tp_name);
if (PyErr_Warn(PyExc_OrangeAttributeWarning, sbuf))
return -1;
}
}
if (!self->orange_dict)
self->orange_dict = PyOrange_DictProxy_New(self);
return PyDict_SetItem(self->orange_dict, pyname, args);
}
else {
if (self->orange_dict)
return PyDict_DelItem(self->orange_dict, pyname);
else {
PyErr_Format(PyExc_AttributeError, "instance of '%s' has no attribute '%s'", self->ob_type->tp_name, name);
return -1;
}
}
PyCATCH_1
}
int Orange_setattr(TPyOrange *self, PyObject *pyname, PyObject *args)
{ return Orange_setattrLow(self, pyname, args, true); }
PyObject *callbackOutput(PyObject *self, PyObject *args, PyObject *kwds,
char *formatname1, char *formatname2, PyTypeObject *toBase)
{
PyObject *output;
char os1[256] = "__output_";
strcat(os1, formatname1);
char os2[256] = "__output_";
if (formatname2)
strcat(os2, formatname2);
for(PyTypeObject *type = self->ob_type;;type = type->tp_base) {
PyObject *type_py = (PyObject *)type;
if (PyObject_HasAttrString(type_py, os1)) {
output = PyObject_GetAttrString(type_py, os1);
break;
}
char os2[256] = "__output_";
if (formatname2 && PyObject_HasAttrString(type_py, os2)) {
output = PyObject_GetAttrString(type_py, os2);
break;
}
if (type==toBase)
return PYNULL;
}
PyObject *function = PyMethod_Function(output);
PyObject *result;
if (!args)
result = PyObject_CallFunction(function, "O", self);
else {
PyObject *margs = PyTuple_New(1+PyTuple_Size(args));
Py_INCREF(self);
PyTuple_SetItem(margs, 0, self);
for(int i = 0, e = PyTuple_Size(args); i<e; i++) {
PyObject *t = PyTuple_GetItem(args, i);
Py_INCREF(t);
PyTuple_SetItem(margs, i+1, t);
}
result = PyObject_Call(function, margs, kwds);
Py_DECREF(margs);
}
Py_DECREF(output);
return result;
}
char const *getName(TPyOrange *self)
{ static char *namebuf = NULL;
if (namebuf) {
delete namebuf;
namebuf = NULL;
}
PyObject *pystr = PyString_FromString("name");
PyObject *pyname = Orange_getattr(self, pystr);
if (!pyname) {
PyErr_Clear();
return NULL;
}
Py_DECREF(pystr);
if (!PyString_Check(pyname)) {
pystr = PyObject_Repr(pyname);
Py_DECREF(pyname);
pyname = pystr;
}
const int sze = PyString_Size(pyname);
if (sze) {
namebuf = mlnew char[PyString_Size(pyname)+1];
strcpy(namebuf, PyString_AsString(pyname));
}
Py_DECREF(pyname);
return namebuf;
}
PyObject *Orange_repr(TPyOrange *self)
{ PyTRY
PyObject *result = callbackOutput((PyObject *)self, NULL, NULL, "repr", "str");
if (result)
return result;
const char *tp_name = self->ob_type->tp_name + (strncmp(self->ob_type->tp_name, "orange.", 7) ? 0 : 7);
const char *name = getName(self);
return name ? PyString_FromFormat("%s '%s'", tp_name, name)
: PyString_FromFormat("<%s instance at %p>", tp_name, self->ptr);
PyCATCH
}
PyObject *Orange_str(TPyOrange *self)
{ PyTRY
PyObject *result = callbackOutput((PyObject *)self, NULL, NULL, "str", "repr");
if (result)
return result;
const char *tp_name = self->ob_type->tp_name + (strncmp(self->ob_type->tp_name, "orange.", 7) ? 0 : 7);
const char *name = getName(self);
return name ? PyString_FromFormat("%s '%s'", tp_name, name)
: PyString_FromFormat("<%s instance at %p>", tp_name, self->ptr);
PyCATCH
}
int Orange_nonzero(PyObject *self)
{ PyTRY
return PyOrange_AS_Orange(self) ? 1 : 0;
PyCATCH_1
}
int Orange_hash(TPyOrange *self)
{ return _Py_HashPointer(self); }
PyObject *Orange_setattr_force(TPyOrange *self, PyObject *args) PYARGS(METH_VARARGS, "(name, value) -> None") //>setattr
{
PyObject *pyname, *pyvalue;
if (!PyArg_ParseTuple(args, "OO:Orange.setattr", &pyname, &pyvalue))
return PYNULL;
if (!PyString_Check(pyname))
PYERROR(PyExc_TypeError, "attribute name must be a string", PYNULL);
if (Orange_setattrLow(self, pyname, pyvalue, false) == -1)
return PYNULL;
RETURN_NONE;
}
PyObject *Orange_clone(TPyOrange *self) PYARGS(METH_NOARGS, "() -> a sensibly deep copy of the object")
{
return WrapOrange(POrange(CLONE(TOrange, ((TOrange *)self->ptr))));
}
PyObject *Orange_reference(TPyOrange *self) PYARGS(METH_NOARGS, "() -> reference; Returns unique id for an object")
{ PyTRY
return PyInt_FromLong(long(self->ptr));
PyCATCH
}
PyObject *Orange_typeid(TPyOrange *self) PYARGS(METH_NOARGS, "() -> int; Returns unique id for object's type")
{ PyTRY
return PyInt_FromLong(long(&typeid(*self->ptr)));
PyCATCH
}
PyObject *Orange_dump(PyObject *self, PyObject *args, PyObject *kwd) PYARGS(METH_VARARGS | METH_KEYWORDS, "(formatname, ...) -> string; Prints the object into string")
{ PyTRY
if (!args || !PyTuple_Size(args)) {
PyErr_Format(PyExc_AttributeError, "missing arguments for '%s'.output", self->ob_type->tp_name);
return PYNULL;
}
PyObject *stype = PyTuple_GetItem(args, 0);
if (!PyString_Check(stype)) {
PyErr_Format(PyExc_AttributeError, "invalid format argument for '%s'.output", self->ob_type->tp_name);
return PYNULL;
}
char *formatname = PyString_AsString(stype);
PyObject *margs = PyTuple_New(PyTuple_Size(args)-1);
for (int i = 1, e = PyTuple_Size(args); i<e; i++) {
PyObject *t = PyTuple_GetItem(args, i);
Py_INCREF(t);
PyTuple_SetItem(margs, i-1, t);
}
PyObject *result = callbackOutput(self, margs, kwd, formatname);
if (!result && !PyErr_Occurred())
PyErr_Format(PyExc_AttributeError, "Class '%s' cannot be dumped as '%s'", self->ob_type->tp_name, formatname);
Py_DECREF(margs);
return result;
PyCATCH
}
PyObject *Orange_write(PyObject *self, PyObject *args, PyObject *kwd) PYARGS(METH_VARARGS | METH_KEYWORDS, "(formatname, file, ...) -> string; Writes the object to a file")
{ PyTRY
if (!args || PyTuple_Size(args)<2) {
PyErr_Format(PyExc_AttributeError, "missing arguments for '%s'.output", self->ob_type->tp_name);
return PYNULL;
}
PyObject *stype = PyTuple_GetItem(args, 0);
if (!PyString_Check(stype)) {
PyErr_Format(PyExc_AttributeError, "invalid format argument for '%s'.output", self->ob_type->tp_name);
return PYNULL;
}
char *formatname = PyString_AsString(stype);
PyObject *margs = PyTuple_New(PyTuple_Size(args)-2);
for (int i = 2, e = PyTuple_Size(args); i<e; i++) {
PyObject *t = PyTuple_GetItem(args, i);
Py_INCREF(t);
PyTuple_SetItem(margs, i-2, t);
}
PyObject *result = callbackOutput(self, margs, kwd, formatname);
Py_DECREF(margs);
if (!result)
return PYNULL;
PyObject *pfile = PyTuple_GetItem(args, 1);
if (pfile)
if (PyFile_Check(pfile))
Py_INCREF(pfile);
else
if (PyString_Check(pfile))
pfile = PyFile_FromString(PyString_AsString(pfile), "wb");
else
pfile = NULL;
if (!pfile) {
PyErr_Format(PyExc_AttributeError, "invalid format argument for '%s'.output", self->ob_type->tp_name);
Py_DECREF(result);
return PYNULL;
}
int succ = PyFile_WriteObject(result, pfile, Py_PRINT_RAW);
Py_DECREF(result);
Py_DECREF(pfile);
if (succ<0) {
if (!PyErr_Occurred())
PyErr_Format(PyExc_AttributeError, "Class '%s' cannot be written as '%s'", self->ob_type->tp_name, formatname);
return PYNULL;
}
else
RETURN_NONE;
PyCATCH
}
#include <typeinfo>
#include <string>
bool convertFromPythonWithML(PyObject *obj, string &str, const TOrangeType &base)
{ if (PyString_Check(obj))
str=PyString_AsString(obj);
else if (PyObject_TypeCheck(obj, (PyTypeObject *)const_cast<TOrangeType *>(&base)))
str = string(getName((TPyOrange *)obj));
else
PYERROR(PyExc_TypeError, "invalid argument type", false);
return true;
}
#include "cls_orange.px"
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -