<commit_before>/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkPythonUtil.cxx
  Language:  C++
  Date:      $Date$
  Version:   $Revision$


Copyright (c) 1993-2000 <NAME>, <NAME>, <NAME> 
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

 * Redistributions of source code must retain the above copyright notice,
   this list of conditions and the following disclaimer.

 * Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.

 * Neither name of <NAME>, <NAME>, or <NAME> nor the names
   of any contributors may be used to endorse or promote products derived
   from this software without specific prior written permission.

 * Modified source versions must be plainly marked as such, and must not be
   misrepresented as being the original software.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

=========================================================================*/

#include "vtkObject.h"
#include "vtkPythonUtil.h"

//#define VTKPYTHONDEBUG

//--------------------------------------------------------------------
class vtkPythonUtil
{
public:
  vtkPythonUtil();
  ~vtkPythonUtil();

  PyObject *PointerDict;
  PyObject *TypeDict;
};

//--------------------------------------------------------------------
vtkPythonUtil *vtkPythonHash = NULL;

//--------------------------------------------------------------------
vtkPythonUtil::vtkPythonUtil()
{
  this->PointerDict = PyDict_New();
  this->TypeDict = PyDict_New();
}

//--------------------------------------------------------------------
vtkPythonUtil::~vtkPythonUtil()
{
  Py_DECREF(this->PointerDict);
  Py_DECREF(this->TypeDict);
}

//--------------------------------------------------------------------
void vtkPythonAddTypeToHash(PyTypeObject *typeObject, char *type)
{
  if (vtkPythonHash == NULL)
    {
    vtkPythonHash = new vtkPythonUtil();
    }

#ifdef VTKPYTHONDEBUG
  //  vtkGenericWarningMacro("Adding an type " << type << " to hash ptr");
#endif  

  // lets make sure it isn't already there
  if (PyDict_GetItemString(vtkPythonHash->TypeDict,type))
    {
#ifdef VTKPYTHONDEBUG
    vtkGenericWarningMacro("Attempt to add type to the hash when already there!!!");
#endif
    return;
    }

  PyObject *pyType = PyInt_FromLong((long)typeObject);
  PyDict_SetItemString(vtkPythonHash->TypeDict,type,pyType);
  Py_DECREF(pyType);

#ifdef VTKPYTHONDEBUG
  //  vtkGenericWarningMacro("Added type to hash type = " << typeObject);
#endif  
}  

//--------------------------------------------------------------------
void vtkPythonAddObjectToHash(PyObject *obj, vtkObject *ptr)
{
  if (vtkPythonHash == NULL)
    {
    vtkPythonHash = new vtkPythonUtil();
    }

#ifdef VTKPYTHONDEBUG
  vtkGenericWarningMacro("Adding an object to hash ptr = " << ptr);
#endif  

  ((PyVTKObject *)obj)->ptr = (vtkObject *)ptr;
  PyObject *pyPtr = PyInt_FromLong((long)ptr);
  PyDict_SetItem(vtkPythonHash->PointerDict,pyPtr,obj);
  Py_DECREF(pyPtr);

#ifdef VTKPYTHONDEBUG
  vtkGenericWarningMacro("Added object to hash obj= " << obj << " " 
			 << ((PyVTKObject *)obj)->ptr);
);
#endif  
}  

//--------------------------------------------------------------------
void vtkPythonDeleteObjectFromHash(PyObject *obj)
{
#ifdef VTKPYTHONDEBUG
  vtkGenericWarningMacro("Deleting an object from hash obj = " << obj << " "
			 << ((PyVTKObject *)obj)->ptr);
);
#endif  

  void *ptr = (void *)((PyVTKObject *)obj)->ptr;
  PyObject *pyPtr = PyInt_FromLong((long)ptr);
  PyDict_DelItem(vtkPythonHash->PointerDict,pyPtr);
  Py_DECREF(pyPtr);  
}

//--------------------------------------------------------------------
PyObject *vtkPythonGetObjectFromPointer(vtkObject *ptr)
{
#ifdef VTKPYTHONDEBUG
  vtkGenericWarningMacro("Checking into pointer " << ptr);
#endif
  
  PyObject *pyPtr = PyInt_FromLong((long)ptr);
  PyObject *obj = PyDict_GetItem(vtkPythonHash->PointerDict,pyPtr);
  Py_DECREF(pyPtr);

#ifdef VTKPYTHONDEBUG
  vtkGenericWarningMacro("Checking into pointer " << ptr << " obj = " << obj);
#endif

  if (obj == 0)
    {
    PyObject *pyType = PyDict_GetItemString(vtkPythonHash->TypeDict,
			     (char *)((vtkObject *)ptr)->GetClassName());
    PyTypeObject *typeObject = (PyTypeObject *)PyInt_AsLong(pyType);
    obj = PyObject_NEW(PyObject, typeObject);
    if (obj == NULL)
      {
      return NULL;
      }
    vtkPythonAddObjectToHash(obj,ptr);
    ((vtkObject *)ptr)->Register(NULL);
    }

  return obj;
}

//--------------------------------------------------------------------
vtkObject *vtkPythonGetPointerFromObject(PyObject *obj, char *result_type)<commit_msg>ERR: clean up memory leaks created by my last set of modifications<commit_after>/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkPythonUtil.cxx
  Language:  C++
  Date:      $Date$
  Version:   $Revision$


Copyright (c) 1993-2000 <NAME>, <NAME>, <NAME> 
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

 * Redistributions of source code must retain the above copyright notice,
   this list of conditions and the following disclaimer.

 * Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.

 * Neither name of <NAME>, <NAME>, or <NAME> nor the names
   of any contributors may be used to endorse or promote products derived
   from this software without specific prior written permission.

 * Modified source versions must be plainly marked as such, and must not be
   misrepresented as being the original software.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

=========================================================================*/

#include "vtkObject.h"
#include "vtkPythonUtil.h"

//#define VTKPYTHONDEBUG

//--------------------------------------------------------------------
class vtkPythonUtil
{
public:
  vtkPythonUtil();
  ~vtkPythonUtil();

  PyObject *PointerDict;
  PyObject *TypeDict;
};

//--------------------------------------------------------------------
vtkPythonUtil *vtkPythonHash = NULL;

//--------------------------------------------------------------------
vtkPythonUtil::vtkPythonUtil()
{
  this->PointerDict = PyDict_New();
  this->TypeDict = PyDict_New();
}

//--------------------------------------------------------------------
vtkPythonUtil::~vtkPythonUtil()
{
  Py_DECREF(this->PointerDict);
  Py_DECREF(this->TypeDict);
}

//--------------------------------------------------------------------
void vtkPythonAddTypeToHash(PyTypeObject *typeObject, char *type)
{
  if (vtkPythonHash == NULL)
    {
    vtkPythonHash = new vtkPythonUtil();
    }

#ifdef VTKPYTHONDEBUG
  //  vtkGenericWarningMacro("Adding an type " << type << " to hash ptr");
#endif  

  // lets make sure it isn't already there
  if (PyDict_GetItemString(vtkPythonHash->TypeDict,type))
    {
#ifdef VTKPYTHONDEBUG
    vtkGenericWarningMacro("Attempt to add type to the hash when already there!!!");
#endif
    return;
    }

  PyObject *pyType = PyInt_FromLong((long)typeObject);
  PyDict_SetItemString(vtkPythonHash->TypeDict,type,pyType);
  Py_DECREF(pyType);

#ifdef VTKPYTHONDEBUG
  //  vtkGenericWarningMacro("Added type to hash type = " << typeObject);
#endif  
}  

//--------------------------------------------------------------------
void vtkPythonAddObjectToHash(PyObject *obj, vtkObject *ptr)
{
  if (vtkPythonHash == NULL)
    {
    vtkPythonHash = new vtkPythonUtil();
    }

#ifdef VTKPYTHONDEBUG
  vtkGenericWarningMacro("Adding an object to hash ptr = " << ptr);
#endif  

  ((PyVTKObject *)obj)->ptr = (vtkObject *)ptr;
  PyObject *pyPtr1 = PyInt_FromLong((long)ptr);
  PyObject *pyPtr2 = PyInt_FromLong((long)obj);
  PyDict_SetItem(vtkPythonHash->PointerDict,pyPtr1,pyPtr2);
  Py_DECREF(pyPtr1);
  Py_DECREF(pyPtr2);

#ifdef VTKPYTHONDEBUG
  vtkGenericWarningMacro("Added object to hash obj= " << obj << " " 
			 << ((PyVTKObject *)obj)->ptr);
#endif  
}  

//--------------------------------------------------------------------
void vtkPythonDeleteObjectFromHash(PyObject *obj)
{
#ifdef VTKPYTHONDEBUG
  vtkGenericWarningMacro("Deleting an object from hash obj = " << obj << " "
			 << ((PyVTKObject *)obj)->ptr);
#endif  

  void *ptr = (void *)((PyVTKObject *)obj)->ptr;
  PyObject *pyPtr = PyInt_FromLong((long)ptr);
  PyDict_DelItem(vtkPythonHash->PointerDict,pyPtr);
  Py_DECREF(pyPtr);  
}

//--------------------------------------------------------------------
PyObject *vtkPythonGetObjectFromPointer(vtkObject *ptr)
{
#ifdef VTKPYTHONDEBUG
  vtkGenericWarningMacro("Checking into pointer " << ptr);
#endif
  
  PyObject *pyPtr1 = PyInt_FromLong((long)ptr);
  PyObject *pyPtr2 = PyDict_GetItem(vtkPythonHash->PointerDict,pyPtr1);
  Py_DECREF(pyPtr1);

  PyObject *obj = 0;
  if (pyPtr2)
    {
    obj = (PyObject *)PyInt_AsLong(pyPtr2);
    }

#ifdef VTKPYTHONDEBUG
  vtkGenericWarningMacro("Checking into pointer " << ptr << " obj = " << obj);
#endif

  if (obj == 0)
    {
    PyObject *pyType = PyDict_GetItemString(vtkPythonHash->TypeDict,
			     (char *)((vtkObject *)ptr)->GetClassName());
    PyTypeObject *typeObject = (PyTypeObject *)PyInt_AsLong(pyType);
    obj = PyObject_NEW(PyObject, typeObject);
    if (obj == NULL)
      {
      return NULL;
      }
    vtkPythonAddObjectToHash(obj,ptr);
    ((vtkObject *)ptr)->Register(NULL);
    }

  return obj;
}

//--------------------------------------------------------------------
vtkObject *vtkPythonGetPointerFromObject(PyObject *obj, char *result_type)
{'fraction_non_alphanumeric': 0.22332397332397333, 'fraction_numerical': 0.00272025272025272, 'mean_word_length': 2.7465483234714005, 'pattern_counts': {'":': 0, '<': 71, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7023974', 'n_tokens_mistral': 3356, 'n_tokens_neox': 3157, 'n_words': 940}