2002-10-12 11:37:38 +00:00
|
|
|
/**
|
|
|
|
* $Id$
|
2008-04-16 22:40:48 +00:00
|
|
|
* ***** BEGIN GPL LICENSE BLOCK *****
|
2002-10-12 11:37:38 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
2008-04-16 22:40:48 +00:00
|
|
|
* of the License, or (at your option) any later version.
|
2002-10-12 11:37:38 +00:00
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software Foundation,
|
|
|
|
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
*
|
|
|
|
* The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* The Original Code is: all of this file.
|
|
|
|
*
|
|
|
|
* Contributor(s): none yet.
|
|
|
|
*
|
2008-04-16 22:40:48 +00:00
|
|
|
* ***** END GPL LICENSE BLOCK *****
|
2002-10-12 11:37:38 +00:00
|
|
|
*/
|
|
|
|
|
2002-11-25 15:29:57 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include <config.h>
|
|
|
|
#endif
|
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
#ifndef NO_EXP_PYTHON_EMBEDDING
|
|
|
|
|
|
|
|
/*------------------------------
|
|
|
|
* PyObjectPlus cpp
|
|
|
|
*
|
|
|
|
* C++ library routines for Crawl 3.2
|
|
|
|
*
|
|
|
|
* Derived from work by
|
|
|
|
* David Redish
|
|
|
|
* graduate student
|
|
|
|
* Computer Science Department
|
|
|
|
* Carnegie Mellon University (CMU)
|
|
|
|
* Center for the Neural Basis of Cognition (CNBC)
|
|
|
|
* http://www.python.org/doc/PyCPP.html
|
|
|
|
*
|
|
|
|
------------------------------*/
|
2005-03-25 10:33:39 +00:00
|
|
|
#include <MT_assert.h>
|
2002-10-12 11:37:38 +00:00
|
|
|
#include "stdlib.h"
|
|
|
|
#include "PyObjectPlus.h"
|
|
|
|
#include "STR_String.h"
|
|
|
|
/*------------------------------
|
|
|
|
* PyObjectPlus Type -- Every class, even the abstract one should have a Type
|
|
|
|
------------------------------*/
|
|
|
|
|
|
|
|
PyTypeObject PyObjectPlus::Type = {
|
|
|
|
PyObject_HEAD_INIT(&PyType_Type)
|
|
|
|
0, /*ob_size*/
|
|
|
|
"PyObjectPlus", /*tp_name*/
|
|
|
|
sizeof(PyObjectPlus), /*tp_basicsize*/
|
|
|
|
0, /*tp_itemsize*/
|
|
|
|
/* methods */
|
|
|
|
PyDestructor, /*tp_dealloc*/
|
|
|
|
0, /*tp_print*/
|
|
|
|
__getattr, /*tp_getattr*/
|
|
|
|
__setattr, /*tp_setattr*/
|
|
|
|
0, /*tp_compare*/
|
|
|
|
__repr, /*tp_repr*/
|
|
|
|
0, /*tp_as_number*/
|
|
|
|
0, /*tp_as_sequence*/
|
|
|
|
0, /*tp_as_mapping*/
|
|
|
|
0, /*tp_hash*/
|
|
|
|
0, /*tp_call */
|
|
|
|
};
|
|
|
|
|
2005-12-20 09:13:06 +00:00
|
|
|
PyObjectPlus::~PyObjectPlus()
|
|
|
|
{
|
2006-01-30 20:33:59 +00:00
|
|
|
if (ob_refcnt)
|
|
|
|
{
|
|
|
|
_Py_ForgetReference(this);
|
|
|
|
}
|
2005-12-20 09:13:06 +00:00
|
|
|
// assert(ob_refcnt==0);
|
|
|
|
}
|
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
PyObjectPlus::PyObjectPlus(PyTypeObject *T) // constructor
|
|
|
|
{
|
2005-03-25 10:33:39 +00:00
|
|
|
MT_assert(T != NULL);
|
2002-10-12 11:37:38 +00:00
|
|
|
this->ob_type = T;
|
|
|
|
_Py_NewReference(this);
|
|
|
|
};
|
|
|
|
|
|
|
|
/*------------------------------
|
|
|
|
* PyObjectPlus Methods -- Every class, even the abstract one should have a Methods
|
|
|
|
------------------------------*/
|
|
|
|
PyMethodDef PyObjectPlus::Methods[] = {
|
2008-08-14 03:23:36 +00:00
|
|
|
{"isA", (PyCFunction) sPy_isA, METH_VARARGS},
|
2002-10-12 11:37:38 +00:00
|
|
|
{NULL, NULL} /* Sentinel */
|
|
|
|
};
|
|
|
|
|
|
|
|
/*------------------------------
|
|
|
|
* PyObjectPlus Parents -- Every class, even the abstract one should have parents
|
|
|
|
------------------------------*/
|
|
|
|
PyParentObject PyObjectPlus::Parents[] = {&PyObjectPlus::Type, NULL};
|
|
|
|
|
|
|
|
/*------------------------------
|
|
|
|
* PyObjectPlus attributes -- attributes
|
|
|
|
------------------------------*/
|
2004-05-16 13:05:15 +00:00
|
|
|
PyObject *PyObjectPlus::_getattr(const STR_String& attr)
|
2002-10-12 11:37:38 +00:00
|
|
|
{
|
2004-05-21 09:18:42 +00:00
|
|
|
if (attr == "__doc__" && GetType()->tp_doc)
|
|
|
|
return PyString_FromString(GetType()->tp_doc);
|
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
//if (streq(attr, "type"))
|
|
|
|
// return Py_BuildValue("s", (*(GetParents()))->tp_name);
|
|
|
|
|
2004-05-16 13:05:15 +00:00
|
|
|
return Py_FindMethod(Methods, this, const_cast<char *>(attr.ReadPtr()));
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
|
2004-11-22 10:19:19 +00:00
|
|
|
int PyObjectPlus::_delattr(const STR_String& attr)
|
|
|
|
{
|
2008-09-03 06:43:07 +00:00
|
|
|
PyErr_SetString(PyExc_AttributeError, "attribute cant be deleted");
|
2004-11-22 10:19:19 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2004-05-16 13:05:15 +00:00
|
|
|
int PyObjectPlus::_setattr(const STR_String& attr, PyObject *value)
|
2002-10-12 11:37:38 +00:00
|
|
|
{
|
|
|
|
//return PyObject::_setattr(attr,value);
|
|
|
|
//cerr << "Unknown attribute" << endl;
|
2008-09-03 06:43:07 +00:00
|
|
|
PyErr_SetString(PyExc_AttributeError, "attribute cant be set");
|
|
|
|
return 1;
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*------------------------------
|
|
|
|
* PyObjectPlus repr -- representations
|
|
|
|
------------------------------*/
|
|
|
|
PyObject *PyObjectPlus::_repr(void)
|
|
|
|
{
|
2008-08-14 03:23:36 +00:00
|
|
|
PyErr_SetString(PyExc_SystemError, "Representation not overridden by object.");
|
|
|
|
return NULL;
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*------------------------------
|
|
|
|
* PyObjectPlus isA -- the isA functions
|
|
|
|
------------------------------*/
|
|
|
|
bool PyObjectPlus::isA(PyTypeObject *T) // if called with a Type, use "typename"
|
|
|
|
{
|
|
|
|
return isA(T->tp_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool PyObjectPlus::isA(const char *mytypename) // check typename of each parent
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
PyParentObject P;
|
|
|
|
PyParentObject *Ps = GetParents();
|
|
|
|
|
|
|
|
for (P = Ps[i=0]; P != NULL; P = Ps[i++])
|
|
|
|
{
|
|
|
|
if (STR_String(P->tp_name) == STR_String(mytypename) )
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
PyObject *PyObjectPlus::Py_isA(PyObject *args) // Python wrapper for isA
|
|
|
|
{
|
|
|
|
char *mytypename;
|
2008-08-14 03:23:36 +00:00
|
|
|
if (!PyArg_ParseTuple(args, "s", &mytypename))
|
|
|
|
return NULL;
|
2002-10-12 11:37:38 +00:00
|
|
|
if(isA(mytypename))
|
2008-08-14 03:23:36 +00:00
|
|
|
Py_RETURN_TRUE;
|
2002-10-12 11:37:38 +00:00
|
|
|
else
|
2008-08-14 03:23:36 +00:00
|
|
|
Py_RETURN_FALSE;
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif //NO_EXP_PYTHON_EMBEDDING
|
|
|
|
|