2003-04-08 19:54:14 +00:00
|
|
|
/*
|
2004-09-18 18:47:03 +00:00
|
|
|
* $Id$
|
2003-04-08 19:54:14 +00:00
|
|
|
*
|
|
|
|
|
* ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
|
|
|
|
|
*
|
|
|
|
|
* 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
|
|
|
|
|
* of the License, or (at your option) any later version. The Blender
|
|
|
|
|
* Foundation also sells licenses for use in proprietary software under
|
|
|
|
|
* the Blender License. See http://www.blender.org/BL/ for information
|
|
|
|
|
* about this.
|
|
|
|
|
*
|
|
|
|
|
* 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.
|
|
|
|
|
*
|
|
|
|
|
* This is a new part of Blender.
|
|
|
|
|
*
|
|
|
|
|
* Contributor(s): Willian P. Germano
|
|
|
|
|
*
|
|
|
|
|
* ***** END GPL/BL DUAL LICENSE BLOCK *****
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "constant.h"
|
|
|
|
|
|
2004-09-25 20:30:40 +00:00
|
|
|
/* This file is heavily based on the old bpython Constant object code in
|
2003-04-08 19:54:14 +00:00
|
|
|
Blender */
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
/* Python constant_Type callback function prototypes: */
|
|
|
|
|
/*****************************************************************************/
|
2004-09-25 20:30:40 +00:00
|
|
|
static void constant_dealloc( BPy_constant * cam );
|
|
|
|
|
static PyObject *constant_getAttr( BPy_constant * cam, char *name );
|
|
|
|
|
static PyObject *constant_repr( BPy_constant * cam );
|
|
|
|
|
static int constantLength( BPy_constant * self );
|
|
|
|
|
static PyObject *constantSubscript( BPy_constant * self, PyObject * key );
|
|
|
|
|
static int constantAssSubscript( BPy_constant * self, PyObject * who,
|
|
|
|
|
PyObject * cares );
|
2003-04-08 19:54:14 +00:00
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
/* Python constant_Type Mapping Methods table: */
|
|
|
|
|
/*****************************************************************************/
|
2004-09-25 20:30:40 +00:00
|
|
|
static PyMappingMethods constantAsMapping = {
|
|
|
|
|
( inquiry ) constantLength, /* mp_length */
|
|
|
|
|
( binaryfunc ) constantSubscript, /* mp_subscript */
|
|
|
|
|
( objobjargproc ) constantAssSubscript, /* mp_ass_subscript */
|
2003-04-08 19:54:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
/* Python constant_Type structure definition: */
|
|
|
|
|
/*****************************************************************************/
|
2004-09-25 20:30:40 +00:00
|
|
|
PyTypeObject constant_Type = {
|
|
|
|
|
PyObject_HEAD_INIT( NULL )
|
|
|
|
|
0, /* ob_size */
|
|
|
|
|
"Blender constant", /* tp_name */
|
|
|
|
|
sizeof( BPy_constant ), /* tp_basicsize */
|
|
|
|
|
0, /* tp_itemsize */
|
|
|
|
|
/* methods */
|
|
|
|
|
( destructor ) constant_dealloc, /* tp_dealloc */
|
|
|
|
|
0, /* tp_print */
|
|
|
|
|
( getattrfunc ) constant_getAttr, /* tp_getattr */
|
|
|
|
|
0, /* tp_setattr */
|
|
|
|
|
0, /* tp_compare */
|
|
|
|
|
( reprfunc ) constant_repr, /* tp_repr */
|
|
|
|
|
0, /* tp_as_number */
|
|
|
|
|
0, /* tp_as_sequence */
|
|
|
|
|
&constantAsMapping, /* tp_as_mapping */
|
|
|
|
|
0, /* tp_as_hash */
|
|
|
|
|
0, 0, 0, 0, 0, 0,
|
|
|
|
|
0, /* tp_doc */
|
|
|
|
|
0, 0, 0, 0, 0, 0,
|
|
|
|
|
0, /* tp_methods */
|
|
|
|
|
0, /* tp_members */
|
2003-04-08 19:54:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
/* Function: constant_New */
|
|
|
|
|
/*****************************************************************************/
|
2004-09-25 20:30:40 +00:00
|
|
|
static PyObject *new_const( void );
|
2003-04-08 19:54:14 +00:00
|
|
|
|
2004-09-25 20:30:40 +00:00
|
|
|
PyObject *M_constant_New( void )
|
|
|
|
|
{ /* can't be static, we call it in other files */
|
|
|
|
|
return new_const( );
|
2003-04-08 19:54:14 +00:00
|
|
|
}
|
|
|
|
|
|
2004-09-25 20:30:40 +00:00
|
|
|
static PyObject *new_const( void )
|
|
|
|
|
{ /* this is the static one */
|
|
|
|
|
BPy_constant *constant;
|
|
|
|
|
|
|
|
|
|
constant_Type.ob_type = &PyType_Type;
|
2003-04-08 19:54:14 +00:00
|
|
|
|
2004-09-25 20:30:40 +00:00
|
|
|
constant =
|
|
|
|
|
( BPy_constant * ) PyObject_NEW( BPy_constant,
|
|
|
|
|
&constant_Type );
|
2003-06-02 20:15:50 +00:00
|
|
|
|
2004-09-25 20:30:40 +00:00
|
|
|
if( constant == NULL )
|
|
|
|
|
return ( EXPP_ReturnPyObjError( PyExc_MemoryError,
|
|
|
|
|
"couldn't create constant object" ) );
|
2003-04-08 19:54:14 +00:00
|
|
|
|
2004-09-25 20:30:40 +00:00
|
|
|
if( ( constant->dict = PyDict_New( ) ) == NULL )
|
|
|
|
|
return ( EXPP_ReturnPyObjError( PyExc_MemoryError,
|
|
|
|
|
"couldn't create constant object's dictionary" ) );
|
2003-04-08 19:54:14 +00:00
|
|
|
|
2004-09-25 20:30:40 +00:00
|
|
|
return ( PyObject * ) constant;
|
2003-04-08 19:54:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
2003-06-28 07:38:21 +00:00
|
|
|
/* Python BPy_constant methods: */
|
2003-04-08 19:54:14 +00:00
|
|
|
/*****************************************************************************/
|
2004-09-25 20:30:40 +00:00
|
|
|
int constant_insert( BPy_constant * self, char *name, PyObject * value )
|
2003-04-08 19:54:14 +00:00
|
|
|
{
|
2004-09-25 20:30:40 +00:00
|
|
|
return PyDict_SetItemString( self->dict, name, value );
|
2003-04-08 19:54:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
2004-09-25 20:30:40 +00:00
|
|
|
/* Function: constant_dealloc */
|
2003-06-28 07:38:21 +00:00
|
|
|
/* Description: This is a callback function for the BPy_constant type. It is */
|
2003-04-08 19:54:14 +00:00
|
|
|
/* the destructor function. */
|
|
|
|
|
/*****************************************************************************/
|
2004-09-25 20:30:40 +00:00
|
|
|
static void constant_dealloc( BPy_constant * self )
|
2003-04-08 19:54:14 +00:00
|
|
|
{
|
2004-09-25 20:30:40 +00:00
|
|
|
Py_DECREF( self->dict );
|
|
|
|
|
PyObject_DEL( self );
|
2003-04-08 19:54:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
2004-09-25 20:30:40 +00:00
|
|
|
/* Function: constant_getAttr */
|
2003-06-28 07:38:21 +00:00
|
|
|
/* Description: This is a callback function for the BPy_constant type. It is */
|
|
|
|
|
/* the function that accesses BPy_constant member variables and */
|
2003-04-08 19:54:14 +00:00
|
|
|
/* methods. */
|
|
|
|
|
/*****************************************************************************/
|
2004-09-25 20:30:40 +00:00
|
|
|
static PyObject *constant_getAttr( BPy_constant * self, char *name )
|
2003-04-08 19:54:14 +00:00
|
|
|
{
|
2004-09-25 20:30:40 +00:00
|
|
|
if( self->dict ) {
|
|
|
|
|
PyObject *v;
|
|
|
|
|
|
|
|
|
|
if( !strcmp( name, "__members__" ) )
|
|
|
|
|
return PyDict_Keys( self->dict );
|
|
|
|
|
|
|
|
|
|
v = PyDict_GetItemString( self->dict, name );
|
|
|
|
|
if( v ) {
|
|
|
|
|
Py_INCREF( v ); /* was a borrowed ref */
|
|
|
|
|
return v;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ( EXPP_ReturnPyObjError( PyExc_AttributeError,
|
|
|
|
|
"attribute not found" ) );
|
|
|
|
|
}
|
|
|
|
|
return ( EXPP_ReturnPyObjError( PyExc_RuntimeError,
|
|
|
|
|
"constant object lacks a dictionary" ) );
|
2003-04-08 19:54:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
/* Section: Sequence Mapping */
|
|
|
|
|
/* These functions provide code to access constant objects as */
|
|
|
|
|
/* mappings. */
|
|
|
|
|
/*****************************************************************************/
|
2004-09-25 20:30:40 +00:00
|
|
|
static int constantLength( BPy_constant * self )
|
2003-04-08 19:54:14 +00:00
|
|
|
{
|
2004-09-25 20:30:40 +00:00
|
|
|
return 0;
|
2003-04-08 19:54:14 +00:00
|
|
|
}
|
|
|
|
|
|
2004-09-25 20:30:40 +00:00
|
|
|
static PyObject *constantSubscript( BPy_constant * self, PyObject * key )
|
2003-04-08 19:54:14 +00:00
|
|
|
{
|
2004-09-25 20:30:40 +00:00
|
|
|
if( self->dict ) {
|
|
|
|
|
PyObject *v = PyDict_GetItem( self->dict, key );
|
2003-05-23 04:34:55 +00:00
|
|
|
|
2004-09-25 20:30:40 +00:00
|
|
|
if( v ) {
|
|
|
|
|
Py_INCREF( v );
|
|
|
|
|
return v;
|
|
|
|
|
}
|
|
|
|
|
}
|
2003-04-08 19:54:14 +00:00
|
|
|
|
2004-09-25 20:30:40 +00:00
|
|
|
return NULL;
|
2003-04-08 19:54:14 +00:00
|
|
|
}
|
|
|
|
|
|
2004-09-25 20:30:40 +00:00
|
|
|
static int constantAssSubscript( BPy_constant * self, PyObject * who,
|
|
|
|
|
PyObject * cares )
|
2003-04-08 19:54:14 +00:00
|
|
|
{
|
2004-09-25 20:30:40 +00:00
|
|
|
/* no user assignments allowed */
|
|
|
|
|
return 0;
|
2003-04-08 19:54:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
2003-06-28 07:38:21 +00:00
|
|
|
/* Function: constant_repr */
|
|
|
|
|
/* Description: This is a callback function for the BPy_constant type. It */
|
2003-04-08 19:54:14 +00:00
|
|
|
/* builds a meaninful string to represent constant objects. */
|
|
|
|
|
/*****************************************************************************/
|
2004-09-25 20:30:40 +00:00
|
|
|
static PyObject *constant_repr( BPy_constant * self )
|
2003-04-08 19:54:14 +00:00
|
|
|
{
|
2004-09-25 20:30:40 +00:00
|
|
|
PyObject *repr = PyObject_Repr( self->dict );
|
|
|
|
|
return repr;
|
2003-04-08 19:54:14 +00:00
|
|
|
}
|