File:  [CENS] / python / pyGiNaC / wrappers2 / symbol_py.cpp
Revision 1.2: download - view: text, annotated - select for diffs - revision graph
Thu Apr 19 10:00:35 2001 UTC (16 years, 7 months ago) by pearu
Branches: MAIN
CVS tags: HEAD
*** empty log message ***

/*
# This file is part of the PyGiNaC package.
# http://cens.ioc.ee/projects/pyginac/
#
# $Revision: 1.2 $
# $Id: symbol_py.cpp,v 1.2 2001-04-19 10:00:35 pearu Exp $
#
# Copyright 2001 Pearu Peterson all rights reserved,
# Pearu Peterson <pearu@cens.ioc.ee>
# Permission to use, modify, and distribute this software is given under the
# terms of the LGPL.  See http://www.fsf.org
#
# NO WARRANTY IS EXPRESSED OR IMPLIED.  USE AT YOUR OWN RISK.
#
*/

#include "symbol_py.hpp"
#include "python_repr.hpp"

namespace {
 PyObject* get_module_dict(const char* module_name)
  {
      py::ref module_obj(PyImport_ImportModule((char*) module_name));
      PyObject* module_dict = PyModule_GetDict(module_obj.get());
      if (module_dict == 0) throw py::import_error();
      return module_dict;
  }
PyObject* get_py_class(const std::string& module_name,
		       const std::string& py_class_name)
{
  static std::string err;
  PyObject* module_dict = get_module_dict(const_cast<char*>(module_name.c_str()));
  PyObject* py_class = PyDict_GetItemString(module_dict, const_cast<char*>(py_class_name.c_str()));
  if (py_class == 0) {
    err = std::string("module ") + module_name + " has no attribute " + py_class_name;
    PyErr_SetString(PyExc_RuntimeError, const_cast<char*>(err.c_str()));
    throw py::import_error();
  }
  Py_INCREF(py_class);
  return py_class;
}
}

BOOST_PYTHON_MODULE_INIT(symbol)
{
  try
    {
      py::module_builder this_module("symbol");

      py::import_converters< basic_py::basic > class_basic_converters("_basic", "basic");
      //py::detail::extension_class<GiNaC::basic, basic_py::basic> dummy_class();
      py::class_builder<GiNaC::basic, basic_py::basic> dummy_basic_class(this_module, "_dummy_basic"); // that will define from_python() needed below
      PyObject * py_class = get_py_class("_basic","basic");
      py::detail::extension_class<GiNaC::basic, basic_py::basic> * b_cl = BOOST_PYTHON_CONVERSION::from_python(py_class, py::type< py::reference<py::detail::extension_class<GiNaC::basic, basic_py::basic> > >()).get();

      
      py::class_builder<symbol_py::symbol> symbol_w_class(this_module, "_symbol_w");
      py::class_builder<GiNaC::symbol, symbol_py::symbol> symbol_class(this_module, "symbol");
 
      py::export_converters(symbol_class);
      symbol_class.declare_base(symbol_w_class);

      symbol_class.declare_base(*b_cl);

      symbol_class.def(py::constructor<>());
      
      //symbol_class.def(&basic_py::basic::python_repr, "__repr__");
    }
  catch(...)
    {
      py::handle_exception();
    }
}

namespace symbol_py {

  std::string symbol::python_repr(void) const
  {
    std::ostrstream os;
    GiNaC::python_repr(os, GiNaC::ex(*this));
    os << std::ends;
    return os.str();
  }

}

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>