From richardmartindwan at yahoo.co.uk Mon Jan 1 22:58:22 2007 From: richardmartindwan at yahoo.co.uk (Richard Dwan) Date: Mon, 1 Jan 2007 21:58:22 +0000 (GMT) Subject: [C++-sig] Newbie query about secure embedded python Message-ID: <20070101215822.7422.qmail@web27603.mail.ukl.yahoo.com> Hello, First let me apologise if this question is obvious - I've never embedded python before and I am deciding of it meets my needs. My question is; is there a secure python interpreter that prevents malicious code from using C/C++ modules or built-in functions from causing damage to a users system. My objective is to have an embedded interpreted language to drive all non CPU intensive application behaviour with user / downloadable scripts to customise the UI / scripting processing. My concern with a simply embedding python is that this would give arbitrary scripts the ability to make system level changes (e.g. destructive abilities using file access). As I wish to encourage scripts to be shared across the Internet this could not be tolerated. Python provides a very good script language to which I can expose application specific functions/objects using the already documented methods. To use Python with the above security restrictions I would need to be able to disable all file / system built-ins when running the interpreter across user scripts. In addition, importing C/C++ functions would have to be disabled as well. As a complication, a set of 'approved C/C++ functions' such as numpy would need to be supported in order to allow the user to do 'useful processing' within the scripts driving the application. My naive solution would be to customise the 'PyImport_Import' and 'PyObject_CallObject' routines used with user scripts so that: --- an imported C/C++ module would be checked against an approved list to prevent arbitrary C/C++ code from being executed --- built-in function calls would be checked against an approved list to prevent system damaging calls from being made --- the application API that is exposed to the user code must not expose to scripts a way of damaging anything more than the data currently being 'processed' --- the application would have to ensure that user code is only executed within the secure interpreter scope (e.g. not use callbacks to user script code outside the secure interpreter) Has the necessary secure python interpreter already been created / a work in progress? Many thanks for any advice you can give me, Richard Send instant messages to your online friends http://uk.messenger.yahoo.com From roman.yakovenko at gmail.com Tue Jan 2 13:04:16 2007 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Tue, 2 Jan 2007 14:04:16 +0200 Subject: [C++-sig] Nice example of embedding Stackless Python into C++ using Boost.Python Message-ID: <7465b6170701020404w1f399f8fr6ab1c741b1ae47a@mail.gmail.com> Hi. During my reading about Stackless Python I found this non-trivial embedding example: http://svn.python.org/view/stackless/sandbox/examples/embedding/watchdog-cpp/main.cpp?rev=52182&view=markup -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ From erik.terwiel at planet.nl Tue Jan 2 09:17:23 2007 From: erik.terwiel at planet.nl (Erik) Date: Tue, 02 Jan 2007 09:17:23 +0100 Subject: [C++-sig] (no subject) Message-ID: <000301c72e46$706dc920$0201a8c0@echo> I read a lot of the html pages on installing boost etc. Still a lot of confusion. Here is what I want: I have old, stable, wonderful C-code I want to use in Python projects. So I encapsulate the C-code in some C++ stuf and try to compile it into a DLL. 1: I DL-ed boost_1_33_1.exe (I use WinXP) and unpacked to D:\boost 2: I started my MS Visual C++ 2005 and told the Studio that my python2.4 includes and libs are at c:\python24\include and c:\python24\libs 3: I found the project in D:\boost\libs\python\build\VisualStudio and Studio converted that to the 8.0 version of my VC. 4: I compiled and got stuff in D:\boost\libs\python\build\bin-stage: 208.896 boost_python.dll 99.090 boost_python.exp 166.216 boost_python.lib 532.480 boost_python_debug.dll 99.102 boost_python_debug.exp 168.490 boost_python_debug.lib 6.450.176 boost_python_debug.pdb (the numbers are sizes) Am I now set up to start on my own projects ? Do I need bjam from here on ? or can I continue to do my own projects in VC ? do I need all the stuff in the folders like D:\boost\libs\* (except D:\boost\libs\python) ? they do not seem to be used till now... here is an example of mine, taken from the Visual C++ workspace. However, I get the compile log (http://213.10.133.192/BuildLog.htm) ======================================== #include #include namespace { // Avoid cluttering the global namespace. int my_int; /* a global integer: or outside namespace ? */ double calc ( double f) { my_int = (int) (f/2); // printf( "Half of %f is %d\n", f, my_int ); return f/2; } } #include using namespace boost::python; BOOST_PYTHON_MODULE( half ) { def("calc", calc ); } From erik.terwiel at planet.nl Tue Jan 2 09:18:30 2007 From: erik.terwiel at planet.nl (Erik) Date: Tue, 02 Jan 2007 09:18:30 +0100 Subject: [C++-sig] confusion about how to use Boost Message-ID: <000401c72e46$9978fce0$0201a8c0@echo> I read a lot of the html pages on installing boost etc. Still a lot of confusion. Here is what I want: I have old, stable, wonderful C-code I want to use in Python projects. So I encapsulate the C-code in some C++ stuf and try to compile it into a DLL. 1: I DL-ed boost_1_33_1.exe (I use WinXP) and unpacked to D:\boost 2: I started my MS Visual C++ 2005 and told the Studio that my python2.4 includes and libs are at c:\python24\include and c:\python24\libs 3: I found the project in D:\boost\libs\python\build\VisualStudio and Studio converted that to the 8.0 version of my VC. 4: I compiled and got stuff in D:\boost\libs\python\build\bin-stage: 208.896 boost_python.dll 99.090 boost_python.exp 166.216 boost_python.lib 532.480 boost_python_debug.dll 99.102 boost_python_debug.exp 168.490 boost_python_debug.lib 6.450.176 boost_python_debug.pdb (the numbers are sizes) Am I now set up to start on my own projects ? Do I need bjam from here on ? or can I continue to do my own projects in VC ? do I need all the stuff in the folders like D:\boost\libs\* (except D:\boost\libs\python) ? they do not seem to be used till now... here is an example of mine, taken from the Visual C++ workspace. However, I get the compile log (http://213.10.133.192/BuildLog.htm) ======================================== #include #include namespace { // Avoid cluttering the global namespace. int my_int; /* a global integer: or outside namespace ? */ double calc ( double f) { my_int = (int) (f/2); // printf( "Half of %f is %d\n", f, my_int ); return f/2; } } #include using namespace boost::python; BOOST_PYTHON_MODULE( half ) { def("calc", calc ); } From roman.yakovenko at gmail.com Tue Jan 2 14:27:00 2007 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Tue, 2 Jan 2007 15:27:00 +0200 Subject: [C++-sig] (no subject) In-Reply-To: <000301c72e46$706dc920$0201a8c0@echo> References: <000301c72e46$706dc920$0201a8c0@echo> Message-ID: <7465b6170701020527m33301bfdk892012c6d921ea9d@mail.gmail.com> On 1/2/07, Erik wrote: > I read a lot of the html pages on installing boost etc. Still a lot of > confusion. > > Here is what I want: > I have old, stable, wonderful C-code I want to use in Python projects. > So I encapsulate the C-code in some C++ stuf and try to compile it > into a DLL. > 1: I DL-ed boost_1_33_1.exe (I use WinXP) and unpacked to D:\boost > 2: I started my MS Visual C++ 2005 and told the Studio that my > python2.4 includes and libs are at c:\python24\include and > c:\python24\libs > 3: I found the project in D:\boost\libs\python\build\VisualStudio and > Studio converted that to the 8.0 version of my VC. Mistake. The only way to build Boost.Python is to use bjam. The other way is to get binaries from http://www.boost-consulting.com/download.html > 4: I compiled and got stuff in D:\boost\libs\python\build\bin-stage: > 208.896 boost_python.dll > 99.090 boost_python.exp > 166.216 boost_python.lib > 532.480 boost_python_debug.dll > 99.102 boost_python_debug.exp > 168.490 boost_python_debug.lib > 6.450.176 boost_python_debug.pdb > (the numbers are sizes) > > Am I now set up to start on my own projects ? You'd better rebuild it with bjam or just download the binaries > Do I need bjam from here on ? or can I continue to do my own projects > in VC ? You are free to use whatever you want after you built it. > do I need all the stuff in the folders like D:\boost\libs\* (except > D:\boost\libs\python) ? they do not seem to be used till now... If you don't use them than no, you don't need them. > > > here is an example of mine, taken from the Visual C++ workspace. > However, I get the compile log (http://213.10.133.192/BuildLog.htm) You need to rebuild Boost.Python. The code looks correct. -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ From ngoodspeed at solidworks.com Tue Jan 2 15:07:25 2007 From: ngoodspeed at solidworks.com (Nat Goodspeed) Date: Tue, 2 Jan 2007 09:07:25 -0500 Subject: [C++-sig] confusion about how to use Boost Message-ID: <94F7A8DD4408D8499C6812FE42E2D4B75A24EA@corp-mail4.solidworks.swk> > -----Original Message----- > From: c++-sig-bounces at python.org [mailto:c++-sig-bounces at python.org] On > Behalf Of Roman Yakovenko > Sent: Tuesday, January 02, 2007 8:27 AM > To: Development of Python/C++ integration > Subject: Re: [C++-sig] (no subject) > > On 1/2/07, Erik wrote: > > 1: I DL-ed boost_1_33_1.exe (I use WinXP) and unpacked to D:\boost > > 2: I started my MS Visual C++ 2005 and told the Studio that my > > python2.4 includes and libs are at c:\python24\include and > > c:\python24\libs > > 3: I found the project in D:\boost\libs\python\build\VisualStudio and > > Studio converted that to the 8.0 version of my VC. > > Mistake. The only way to build Boost.Python is to use bjam. The other way > is to > get binaries from http://www.boost-consulting.com/download.html [Nat] ? We have a shipping product that incorporates Boost.Python. On Windows we include the Boost.Python project in our solution. On the Mac we include a Boost.Python target in our Xcode project. When we first downloaded Boost (many Boost releases ago) we initially tried to use bjam. We got lots of errors and were nonplussed by the requirement to learn an obscure new language to try to resolve them. We were relieved to discover that building desired Boost libraries in your IDE is a recommended approach, and we've never looked back. Has something changed since then? From a_cancoban at hotmail.com Tue Jan 2 23:50:55 2007 From: a_cancoban at hotmail.com (aslihan canoban) Date: Tue, 2 Jan 2007 22:50:55 +0000 Subject: [C++-sig] (no subject) Message-ID: Hi Ercan. May this mail is not about c++.. that I dont know well. This is about us :P Ya ben neden t?rk?e konu?muyorum yaa.. Ercan ben asl?, fen lisesinden. Bug?n bayram ya.. aniden akl?ma geldin.. seni buldum bak hemen :P e?er mailimi oursan.. g?r??mek istiyorum seninle, y?llar ge?ti bak aradan.. ?zledim galiba :D ?ok ?pt?m can?m.. bye _________________________________________________________________ Kendinizi ifade edin: giri? sayfan?z? Live.com ile istedi?iniz bi?imde tasarlay?n. http://www.live.com/getstarted -------------- next part -------------- An HTML attachment was scrubbed... URL: From birgir-s at online.no Wed Jan 3 15:45:29 2007 From: birgir-s at online.no (Birgir Sigurjonsson) Date: Wed, 3 Jan 2007 15:45:29 +0100 (CET) Subject: [C++-sig] problems with embedded python Message-ID: <22237271.1167835529891.JavaMail.adm-moff@moffice2.nsc.no> Hi, I am still trying to get embedded boost.python to work correctly. Below is my code. The problem is that when the following statment in main.cpp is executed a135->load("/tmp/xyz.xyz"); I get Abort after the following line is printed out Python running: 6--> I think somehow that the a135 that is returned by the following line in main.cppboost::shared_ptr a135 = AbstractLoaderFactory::create("Ascii135"); is not complete or corrupted. It might be that my AbstractLoaderFactoryWrap is not 100% correct. When I comment out the line a135->load("/tmp/xyz.xyz"); this is printed to the console -->Importing plugins import python_plugins.ascii135loader as ipack Python running: 1--> Ascii135Loader AbstractLoaderFactor ctor registry = 0x543340 Python running: 4--> Ascii135LoaderFactory(AbstractLoaderFactory) __init__ > True -->Done Importing plugins hello world registry = 0x543340 registry = 0x543340 Ascii135 registry = 0x543340 registry = 0x543340 Creating AbstractLoader = Ascii135 Python running: 5--> Ascii135LoaderFactory::make constructor AbstractLoader::AbstractLoader Python running: 2--> Ascii135Loader(AbstractLoader) __init__ > Python running: 6--> calling Py_Finalize() Done destructor AbstractLoader::~AbstractLoader destructor AbstractLoader::~AbstractLoader Any idea what I am doing wrong? Best regards, Birgir Sigurjonsson. -------- framework/abstractloader.h ---------------- #ifndef FRAMEWORK_ABSTRACTLOADER_H #define FRAMEWORK_ABSTRACTLOADER_H #include #include #include class AbstractLoader : private boost::noncopyable { public: AbstractLoader(); bool load(const std::string& path); virtual ~AbstractLoader(); virtual bool load_impl(const std::string& path) = 0; }; #endif -------- framework/abstractloader.cpp ---------------- #include AbstractLoader::AbstractLoader(){ std::cout << "constructor AbstractLoader::AbstractLoader\n"; } AbstractLoader::~AbstractLoader() { std::cout << "destructor AbstractLoader::~AbstractLoader\n"; } bool AbstractLoader::load(const std::string& path) { return load_impl(path); } -------- framework/abstractloaderfactory.h ---------------- #ifndef FILE_IMPORT_FRAMEWORK_ABSTRACTLOADERFACTORY_H #define FILE_IMPORT_FRAMEWORK_ABSTRACTLOADERFACTORY_H #include #include #include #include #include #include class NoSuchLoader : std::exception { public: NoSuchLoader(const std::string path) throw (); virtual ~NoSuchLoader () throw (); virtual const char * what () const throw (); private: std::string m_path; }; template struct first_from_pair : public std::unary_function { typename Pair::first_type operator() (const Pair& pair) const { return pair.first; } }; class AbstractLoaderFactory : private boost::noncopyable { private: typedef std::map Registry; typedef first_from_pair RegistryKeyFunction; public: typedef boost::transform_iterator iterator; AbstractLoaderFactory(const std::string& key); virtual ~AbstractLoaderFactory(); static boost::shared_ptr create(const std::string &key); static iterator begin(); static iterator end(); virtual AbstractLoader* make() = 0; protected: void unregister(const std::string &key); private: static Registry& registry(); }; #endif -------- framework/abstractloaderfactory.h ---------------- #include #include #include #include NoSuchLoader::NoSuchLoader(const std::string path) throw (): m_path(path) { } NoSuchLoader::~NoSuchLoader () throw () { } const char * NoSuchLoader::what () const throw () { return m_path.c_str(); } AbstractLoaderFactory::AbstractLoaderFactory(const std::string& key) { bool inserted; Registry::iterator pos; std::cerr << "AbstractLoaderFactor ctor\n"; boost::tie(pos,inserted) = registry().insert(std::make_pair(key, this)); assert(inserted); } AbstractLoaderFactory::~AbstractLoaderFactory() { } void AbstractLoaderFactory::unregister(const std::string& key) { size_t elements = registry().erase(key); assert(elements == 1); } boost::shared_ptr AbstractLoaderFactory::create (const std::string &key) { Registry::const_iterator p = registry().find(key); if (p == registry().end()) { throw NoSuchLoader(key); } std::cout << "Creating AbstractLoader = "<< key << "\n"; AbstractLoaderFactory * alf = (*p).second; AbstractLoader* a = alf->make(); boost::shared_ptr concrete_loader(a); return concrete_loader; } AbstractLoaderFactory::Registry & AbstractLoaderFactory::registry() { static Registry* registry = new Registry; std::cerr << "registry = " << registry << std::endl; return *registry; } AbstractLoaderFactory::iterator AbstractLoaderFactory::begin() { return iterator(registry().begin()); } AbstractLoaderFactory::iterator AbstractLoaderFactory::end() { return iterator(registry().end()); } ----- plugins/python_plugins/asci135loader.py def loadPackage(pack): success = False print '-->Importing plugins' import_pack_as_ipack = 'import python_plugins.' + pack + ' as ipack' print import_pack_as_ipack try: exec import_pack_as_ipack success = True except ImportError, e: print e success = False return success success = loadPackage('ascii135loader') print success print '-->Done Importing plugins' ----- plugins/python_plugins/asci135loader.py from import_framework import AbstractLoader, AbstractLoaderFactory print 'Python running: 1--> Ascii135Loader' class Ascii135Loader(AbstractLoader): def __init__(self): super(Ascii135Loader, self).__init__() print 'Python running: 2--> Ascii135Loader(AbstractLoader) __init__' print self.__init__ def load_impl(self): print self.load_impl print 'Python running: 3--> Ascii135Loader(AbstractLoader) load_impl HARD WORK' return True class Ascii135LoaderFactory(AbstractLoaderFactory): def __init__(self, key): super(Ascii135LoaderFactory, self).__init__(key) print 'Python running: 4--> Ascii135LoaderFactory(AbstractLoaderFactory) __init__' print self.__init__ def make(self): print 'Python running: 5--> Ascii135LoaderFactory::make' something = Ascii135Loader() print 'Python running: 6--> ',something return something register_this = Ascii135LoaderFactory("Ascii135") ------------ main.cpp -------- #include #include #include #include #include int main(int argc, char *argv[]) { Py_Initialize(); boost::python::object main_module(( boost::python::handle<>(boost::python::borrowed(PyImport_AddModule("__main__"))))); boost::python::object main_namespace = main_module.attr("__dict__"); boost::python::handle<> ignored (( PyRun_String( "import sys\n" "sys.path.append('/disk/armamix/p4workspaces/birgirs/sandbox/file_import/plugins')\n" "sys.path.append('/disk/armamix/p4workspaces/birgirs/install/sandbox/file_import/linux-amd64-gcc_3_2-debug/lib')\n" "import pluginloader\n" "print 'hello world'" , Py_file_input, main_namespace.ptr(), main_namespace.ptr()) )); AbstractLoaderFactory::iterator p = AbstractLoaderFactory::begin(); AbstractLoaderFactory::iterator p_end = AbstractLoaderFactory::end(); while(p != p_end) { std::cout << *p << "\n"; ++p; } boost::shared_ptr a135 = AbstractLoaderFactory::create("Ascii135"); // a135->load("/tmp/xyz.xyz"); std::cout << "calling Py_Finalize()\n"; Py_Finalize(); std::cout << "Done\n"; return 0; } From birgir.sigurjonsson at roxar.com Wed Jan 3 18:00:54 2007 From: birgir.sigurjonsson at roxar.com (Birgir Sigurjonsson) Date: Wed, 03 Jan 2007 18:00:54 +0100 Subject: [C++-sig] problems with embedded python In-Reply-To: <22237271.1167835529891.JavaMail.adm-moff@moffice2.nsc.no> References: <22237271.1167835529891.JavaMail.adm-moff@moffice2.nsc.no> Message-ID: <459BE146.6060403@roxar.com> Hei I am sorry that I missed one file. Birgir Sigurjonsson. ------- import_framework/pythonframework.cpp ------------ #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace boost::python; class AbstractLoaderWrap : public AbstractLoader, public wrapper { public: AbstractLoaderWrap(PyObject* self_) : m_self(self_) { Py_INCREF(m_self); } ~AbstractLoaderWrap () { Py_DECREF(m_self); } bool load_impl(const std::string& path) { return this->get_override("load_impl")(); } PyObject *m_self; }; class AbstractLoaderFactoryWrap : public AbstractLoaderFactory, public wrapper { public: AbstractLoaderFactoryWrap(const std::string& key) : AbstractLoaderFactory(key) {} AbstractLoader* make() { return this->get_override("make")(); } }; BOOST_PYTHON_MODULE(import_framework) { class_, boost::noncopyable>("AbstractLoader") .def("load", &AbstractLoader::load) .def("load_impl", pure_virtual(&AbstractLoader::load_impl)) ; implicitly_convertible, std::auto_ptr >(); class_("AbstractLoaderFactory", init()) .def("make", pure_virtual(&AbstractLoaderFactory::make), return_value_policy()) ; } Birgir Sigurjonsson wrote: > Hi, I am still trying to get embedded boost.python to work > correctly. Below is my code. > > The problem is that when the following statment in main.cpp > is executed > > a135->load("/tmp/xyz.xyz"); > > I get Abort after the following line is printed out > Python running: 6--> > > I think somehow that the a135 that is returned by the following line in main.cppboost::shared_ptr a135 = AbstractLoaderFactory::create("Ascii135"); > > is not complete or corrupted. > > It might be that my AbstractLoaderFactoryWrap is not 100% correct. > > > When I comment out the line a135->load("/tmp/xyz.xyz"); this is printed to the console > > -->Importing plugins > import python_plugins.ascii135loader as ipack > Python running: 1--> Ascii135Loader > AbstractLoaderFactor ctor > registry = 0x543340 > Python running: 4--> Ascii135LoaderFactory(AbstractLoaderFactory) __init__ > > > True > -->Done Importing plugins > hello world > registry = 0x543340 > registry = 0x543340 > Ascii135 > registry = 0x543340 > registry = 0x543340 > Creating AbstractLoader = Ascii135 > Python running: 5--> Ascii135LoaderFactory::make > constructor AbstractLoader::AbstractLoader > Python running: 2--> Ascii135Loader(AbstractLoader) __init__ > > > Python running: 6--> > calling Py_Finalize() > Done > destructor AbstractLoader::~AbstractLoader > destructor AbstractLoader::~AbstractLoader > > Any idea what I am doing wrong? > > Best regards, > Birgir Sigurjonsson. > > > -------- framework/abstractloader.h ---------------- > #ifndef FRAMEWORK_ABSTRACTLOADER_H > #define FRAMEWORK_ABSTRACTLOADER_H > > #include > #include > #include > > class AbstractLoader : private boost::noncopyable { > public: > AbstractLoader(); > bool load(const std::string& path); > virtual ~AbstractLoader(); > virtual bool load_impl(const std::string& path) = 0; > }; > #endif > > -------- framework/abstractloader.cpp ---------------- > #include > > AbstractLoader::AbstractLoader(){ > std::cout << "constructor AbstractLoader::AbstractLoader\n"; > } > > AbstractLoader::~AbstractLoader() { > std::cout << "destructor AbstractLoader::~AbstractLoader\n"; > } > > bool AbstractLoader::load(const std::string& path) { > return load_impl(path); > } > > -------- framework/abstractloaderfactory.h ---------------- > #ifndef FILE_IMPORT_FRAMEWORK_ABSTRACTLOADERFACTORY_H > #define FILE_IMPORT_FRAMEWORK_ABSTRACTLOADERFACTORY_H > > #include > #include > #include > #include > #include > #include > > class NoSuchLoader : std::exception { > public: > NoSuchLoader(const std::string path) throw (); > virtual ~NoSuchLoader () throw (); > > virtual const char * what () const throw (); > > private: > > std::string m_path; > }; > > template > struct first_from_pair : public std::unary_function { > typename Pair::first_type operator() (const Pair& pair) const { > return pair.first; > } > }; > > class AbstractLoaderFactory : private boost::noncopyable { > private: > typedef std::map Registry; > typedef first_from_pair RegistryKeyFunction; > > public: > > typedef boost::transform_iterator iterator; > > AbstractLoaderFactory(const std::string& key); > virtual ~AbstractLoaderFactory(); > > static boost::shared_ptr create(const std::string &key); > > static iterator begin(); > static iterator end(); > > virtual AbstractLoader* make() = 0; > > protected: > void unregister(const std::string &key); > > private: > static Registry& registry(); > }; > > #endif > > -------- framework/abstractloaderfactory.h ---------------- > #include > #include > #include > #include > > NoSuchLoader::NoSuchLoader(const std::string path) throw (): > m_path(path) { > } > > NoSuchLoader::~NoSuchLoader () throw () { > } > > const char * NoSuchLoader::what () const throw () { > return m_path.c_str(); > } > > AbstractLoaderFactory::AbstractLoaderFactory(const std::string& key) { > bool inserted; > Registry::iterator pos; > std::cerr << "AbstractLoaderFactor ctor\n"; > boost::tie(pos,inserted) = registry().insert(std::make_pair(key, this)); > assert(inserted); > } > > AbstractLoaderFactory::~AbstractLoaderFactory() { > } > > void AbstractLoaderFactory::unregister(const std::string& key) { > size_t elements = registry().erase(key); > assert(elements == 1); > } > > boost::shared_ptr > AbstractLoaderFactory::create (const std::string &key) { > Registry::const_iterator p = registry().find(key); > > if (p == registry().end()) { > throw NoSuchLoader(key); > } > std::cout << "Creating AbstractLoader = "<< key << "\n"; > AbstractLoaderFactory * alf = (*p).second; > > AbstractLoader* a = alf->make(); > > boost::shared_ptr concrete_loader(a); > return concrete_loader; > } > > > AbstractLoaderFactory::Registry & > AbstractLoaderFactory::registry() { > static Registry* registry = new Registry; > std::cerr << "registry = " << registry << std::endl; > return *registry; > } > > AbstractLoaderFactory::iterator > AbstractLoaderFactory::begin() { > return iterator(registry().begin()); > } > > AbstractLoaderFactory::iterator > AbstractLoaderFactory::end() { > return iterator(registry().end()); > } > > ----- plugins/python_plugins/asci135loader.py > def loadPackage(pack): > success = False > > print '-->Importing plugins' > import_pack_as_ipack = 'import python_plugins.' + pack + ' as ipack' > print import_pack_as_ipack > try: > exec import_pack_as_ipack > success = True > except ImportError, e: > print e > success = False > return success > > > success = loadPackage('ascii135loader') > > print success > print '-->Done Importing plugins' > > > ----- plugins/python_plugins/asci135loader.py > from import_framework import AbstractLoader, AbstractLoaderFactory > > print 'Python running: 1--> Ascii135Loader' > > class Ascii135Loader(AbstractLoader): > def __init__(self): > super(Ascii135Loader, self).__init__() > print 'Python running: 2--> Ascii135Loader(AbstractLoader) __init__' > print self.__init__ > def load_impl(self): > print self.load_impl > print 'Python running: 3--> Ascii135Loader(AbstractLoader) load_impl HARD WORK' > return True > > class Ascii135LoaderFactory(AbstractLoaderFactory): > def __init__(self, key): > super(Ascii135LoaderFactory, self).__init__(key) > print 'Python running: 4--> Ascii135LoaderFactory(AbstractLoaderFactory) __init__' > print self.__init__ > > def make(self): > print 'Python running: 5--> Ascii135LoaderFactory::make' > something = Ascii135Loader() > print 'Python running: 6--> ',something > return something > > register_this = Ascii135LoaderFactory("Ascii135") > > ------------ main.cpp -------- > #include > #include > #include > #include > > #include > > int main(int argc, char *argv[]) { > Py_Initialize(); > > boost::python::object main_module(( > boost::python::handle<>(boost::python::borrowed(PyImport_AddModule("__main__"))))); > boost::python::object main_namespace = main_module.attr("__dict__"); > > boost::python::handle<> ignored (( > PyRun_String( "import sys\n" > "sys.path.append('/disk/armamix/p4workspaces/birgirs/sandbox/file_import/plugins')\n" > "sys.path.append('/disk/armamix/p4workspaces/birgirs/install/sandbox/file_import/linux-amd64-gcc_3_2-debug/lib')\n" > "import pluginloader\n" > "print 'hello world'" , > Py_file_input, > main_namespace.ptr(), > main_namespace.ptr()) > )); > > AbstractLoaderFactory::iterator p = AbstractLoaderFactory::begin(); > AbstractLoaderFactory::iterator p_end = AbstractLoaderFactory::end(); > while(p != p_end) { > std::cout << *p << "\n"; > ++p; > } > > boost::shared_ptr a135 = AbstractLoaderFactory::create("Ascii135"); > // a135->load("/tmp/xyz.xyz"); > > std::cout << "calling Py_Finalize()\n"; > Py_Finalize(); > std::cout << "Done\n"; > return 0; > } > > > ------------------------------------------------------------------------ > > _______________________________________________ > C++-sig mailing list > C++-sig at python.org > http://mail.python.org/mailman/listinfo/c++-sig DISCLAIMER: This message contains information that may be privileged or confidential and is the property of the Roxar Group. It is intended only for the person to whom it is addressed. If you are not the intended recipient, you are not authorised to read, print, retain, copy, disseminate, distribute, or use this message or any part thereof. If you receive this message in error, please notify the sender immediately and delete all copies of this message. From roman.yakovenko at gmail.com Wed Jan 3 19:17:59 2007 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Wed, 3 Jan 2007 20:17:59 +0200 Subject: [C++-sig] problems with embedded python In-Reply-To: <459BE146.6060403@roxar.com> References: <22237271.1167835529891.JavaMail.adm-moff@moffice2.nsc.no> <459BE146.6060403@roxar.com> Message-ID: <7465b6170701031017r1c43d1c6n432babed29409322@mail.gmail.com> If you really want to get some help you will have to create small example, that reproduce the problem. From my experience no one will read the code you posted here. -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ From roman.yakovenko at gmail.com Thu Jan 4 07:37:21 2007 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Thu, 4 Jan 2007 08:37:21 +0200 Subject: [C++-sig] GCC-XML installer Message-ID: <7465b6170701032237n74d8264dvb3ebfef5e5cacef1@mail.gmail.com> Hello. I created some kind of cross platform installer for GCC-XML. Download: http://language-binding.net/pygccxml/download.html#gcc-xml What is GCC-XML? GCC-XML generates an XML description of a C++ program from GCC's internal representation. GCC-XML, via pygccxml, used as C++ parser for Py++ - Boost.Python code generator. If you already have Boost.Python installed it should take only few minutes to install Py++ :-). Next step is to create stand-alone distribution\installer for Boost.Python :-) -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ From kim.branson at gmail.com Thu Jan 4 09:11:49 2007 From: kim.branson at gmail.com (Kim Branson) Date: Thu, 4 Jan 2007 00:11:49 -0800 Subject: [C++-sig] passing wrapped c++ objects to boost wrapped function Message-ID: <61738A5C-AB24-4610-A47D-E118975ACE5B@stanford.edu> Hi, i'm trying to glue together some c++ code using python as an intermediate layer. i have python bindings for a library, and i can create python wrapped instances of this object in python mol = OEMol() in python this is a proxy of the C++ instance. I have other c++ code that uses an OEMol interface like so. int numberOfAtoms(OEMol &mol) { int numAtoms = 0 ; numAtoms = mol.NumAtoms(); return numAtoms; } i created a simple wrapper for it. #include "functions.cpp" #include #include using namespace boost::python; BOOST_PYTHON_MODULE(test) { def("numberOfAtoms", numberOfAtoms); def("finalBondIndex",finalBondIndex); } trying to pass a mol object into it from python produces the following result. Traceback (most recent call last): File "functionsTest.py", line 35, in ? numatoms = test.numberOfAtoms(mol); Boost.Python.ArgumentError: Python argument types in test.numberOfAtoms(OEMol) did not match C++ signature: numberOfAtoms(OEChem::OEMol {lvalue}) I assume this is failing because the boost wrapped function does not get OEChem::OEMol, but the python object PyObject? How can i wrap the function to solve this? I'm unsure on what exactly is being passed in. cheers Kim -------------- next part -------------- An HTML attachment was scrubbed... URL: From birgir-s at online.no Thu Jan 4 14:22:09 2007 From: birgir-s at online.no (Birgir Sigurjonsson) Date: Thu, 4 Jan 2007 14:22:09 +0100 (CET) Subject: [C++-sig] problems with embedded python and pure virtual functions Message-ID: <5910186.1167916929707.JavaMail.adm-moff@moffice2.nsc.no> Hi, I am having problem calling a pure virtual function that is overritten in Python. My apology, if this post is to long but I am not sure how to formulate my question nor do I know what part of my code is needed to for you all to help me. I can create my AbstractLoader and call its load function. But then load calls load_impl I get Abort in the console. Therfor I think that there is something wrong with my wrapper and or the auto_ptr. I have stepped through the code in debugger and here is the call stack: ZN5boost6python23throw_error_already_setEv, FP=7fbfffe960 boost::python::expect_non_null<_object>, FP=7fbfffe980 boost::python::detail::manage_ptr<_object>, FP=7fbfffea50 boost::python::handle<_object>::handle<_object>, FP=7fbfffea80 boost::python::detail::method_result::method_result, FP=7fbfffeaa0 boost::python::override::operator (), FP=7fbfffeb00 AbstractLoaderWrap::load_impl, FP=7fbfffeb60 AbstractLoader::load, FP=7fbfffeb90 Here is my wrapper: class AbstractLoaderWrap : public AbstractLoader, public wrapper { public: AbstractLoaderWrap(PyObject* self_) : m_self(self_) { Py_INCREF(m_self); } virtual ~AbstractLoaderWrap () { Py_DECREF(m_self); } virtual bool load_impl(const std::string& path) { return this->get_override("load_impl")(path); } PyObject *m_self; }; BOOST_PYTHON_MODULE(import_framework) { class_, boost::noncopyable>("AbstractLoader") .def("load_impl", pure_virtual(&AbstractLoader::load_impl)) ; implicitly_convertible, std::auto_ptr >(); class_("AbstractLoaderFactory", init()) .def("make", pure_virtual(&AbstractLoaderFactory::make), return_value_policy()) ; } Here is my base class: class AbstractLoader : private boost::noncopyable { public: AbstractLoader(); bool load(const std::string& path); virtual ~AbstractLoader(); virtual bool load_impl(const std::string& path) = 0; }; bool AbstractLoader::load(const std::string& path) { std::cout << "AbstractLoader::load -" << path << "\n"; bool status = load_impl(path); return status; } Any help appreciated, Birgir Sigurjonsson. From roman.yakovenko at gmail.com Thu Jan 4 14:27:53 2007 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Thu, 4 Jan 2007 15:27:53 +0200 Subject: [C++-sig] problems with embedded python and pure virtual functions In-Reply-To: <5910186.1167916929707.JavaMail.adm-moff@moffice2.nsc.no> References: <5910186.1167916929707.JavaMail.adm-moff@moffice2.nsc.no> Message-ID: <7465b6170701040527l4a42fa30g80838c5a593c6e86@mail.gmail.com> On 1/4/07, Birgir Sigurjonsson wrote: > Hi, I am having problem calling a pure virtual function that > is overritten in Python. My apology, if this post is to long > but I am not sure how to formulate my question nor do I know > what part of my code is needed to for you all to help me. > > I can create my AbstractLoader and call its load function. > But then load calls load_impl I get Abort in the console. > The most important part of the code is missing. In most cases ( 99% ) the main reason is that the derived class, defined in Python, did not call __init__ method of base class, defined in C++. -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ From roman.yakovenko at gmail.com Thu Jan 4 14:29:02 2007 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Thu, 4 Jan 2007 15:29:02 +0200 Subject: [C++-sig] passing wrapped c++ objects to boost wrapped function In-Reply-To: <61738A5C-AB24-4610-A47D-E118975ACE5B@stanford.edu> References: <61738A5C-AB24-4610-A47D-E118975ACE5B@stanford.edu> Message-ID: <7465b6170701040529g7d098ef3q4ff1f425916568ac@mail.gmail.com> On 1/4/07, Kim Branson wrote: > Hi, > > i'm trying to glue together some c++ code using python as an intermediate > layer. > > i have python bindings for a library, and i can create python wrapped > instances of this object in python > > mol = OEMol() > in python this is a proxy of the C++ instance. > _0030dfd0_p_OEMolWrapper> > > I have other c++ code that uses an OEMol interface like so. > > int numberOfAtoms(OEMol &mol) > { > int numAtoms = 0 ; > numAtoms = mol.NumAtoms(); > return numAtoms; > } > > i created a simple wrapper for it. > > #include "functions.cpp" > #include > #include > using namespace boost::python; > > BOOST_PYTHON_MODULE(test) > { > def("numberOfAtoms", numberOfAtoms); > def("finalBondIndex",finalBondIndex); > } > > trying to pass a mol object into it from python produces the following > result. > > Traceback (most recent call last): > File "functionsTest.py", line 35, in ? > numatoms = test.numberOfAtoms(mol); > Boost.Python.ArgumentError: Python argument types in > test.numberOfAtoms(OEMol) > did not match C++ signature: > numberOfAtoms(OEChem::OEMol {lvalue}) > > I assume this is failing because the boost wrapped function does not get > OEChem::OEMol, but the python object PyObject? > > How can i wrap the function to solve this? I'm unsure on what exactly is > being passed in. > Can you post complete minimal example? -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ From birgir-s at online.no Thu Jan 4 14:49:12 2007 From: birgir-s at online.no (Birgir Sigurjonsson) Date: Thu, 4 Jan 2007 14:49:12 +0100 (CET) Subject: [C++-sig] problems with embedded python and pure virtual functions In-Reply-To: <459D04E4.4070802@roxar.com> References: <5910186.1167916929707.JavaMail.adm-moff@moffice2.nsc.no> <7465b6170701040527l4a42fa30g80838c5a593c6e86@mail.gmail.com> <459D04E4.4070802@roxar.com> Message-ID: <30624226.1167918552256.JavaMail.adm-moff@moffice2.nsc.no> Hei here is the python code: from import_framework import AbstractLoader, AbstractLoaderFactory print 'Python running: --> Ascii135Loader script' class Ascii135Loader(AbstractLoader): def __init__(self): super(Ascii135Loader, self).__init__() print self.__init__ def load_impl(self): print 'load_impl -- doing som hard work' return True class Ascii135LoaderFactory(AbstractLoaderFactory): def __init__(self, key): super(Ascii135LoaderFactory, self).__init__(key) print 'Python running: 4--> Ascii135LoaderFactory(AbstractLoaderFactory) __init__' print self.__init__ def make(self): print 'Python running: 5--> Ascii135LoaderFactory::make' return Ascii135Loader() register_this = Ascii135LoaderFactory("Ascii135") And I can see that the AbstractLoader constructor is called because I have a print out there Best regards, Birgir. > > Roman Yakovenko wrote: > > On 1/4/07, Birgir Sigurjonsson wrote: > > > >>Hi, I am having problem calling a pure virtual function that > >>is overritten in Python. My apology, if this post is to long > >>but I am not sure how to formulate my question nor do I know > >>what part of my code is needed to for you all to help me. > >> > >>I can create my AbstractLoader and call its load function. > >>But then load calls load_impl I get Abort in the console. > >> > > > > > > The most important part of the code is missing. In most cases ( 99% ) the > > main reason is that the derived class, defined in Python, did not call __init__ > > method of base class, defined in C++. > > > From seefeld at sympatico.ca Thu Jan 4 14:47:12 2007 From: seefeld at sympatico.ca (Stefan Seefeld) Date: Thu, 04 Jan 2007 08:47:12 -0500 Subject: [C++-sig] problems with embedded python and pure virtual functions In-Reply-To: <5910186.1167916929707.JavaMail.adm-moff@moffice2.nsc.no> References: <5910186.1167916929707.JavaMail.adm-moff@moffice2.nsc.no> Message-ID: <459D0560.1080303@sympatico.ca> Birgir Sigurjonsson wrote: > Hi, I am having problem calling a pure virtual function that > is overritten in Python. My apology, if this post is to long > but I am not sure how to formulate my question nor do I know > what part of my code is needed to for you all to help me. > > I can create my AbstractLoader and call its load function. > But then load calls load_impl I get Abort in the console. > > Therfor I think that there is something wrong with my > wrapper and or the auto_ptr. > > I have stepped through the code in debugger and here is the call stack: > > ZN5boost6python23throw_error_already_setEv, FP=7fbfffe960 It appears the python runtime raises an exception at some point that you don't handle, resulting in it getting propagated as a C++ exception of type boost::python::error_already_set up to the toplevel, leading to the abort. To handle it, wrap your function that runs the embedded python script into something similar to this (warning: completely untested !) : using boost::python; void handle_error() { PyObject* type, *value, *traceback; PyErr_Fetch(&type, &value, &traceback); handle<> ty(type), v(value), tr(traceback); str format("exception type: %sn"); format += "exception value: %sn"; format += "traceback:n%s" ; str msg = format % make_tuple(ty, v, tr); std::cerr << extract(msg) << std::endl; } try {...} catch (boost::python::error_already_set const &) { handle_error(); } That may give you a clue about what goes wrong. HTH, Stefan -- ...ich hab' noch einen Koffer in Berlin... From kim.branson at gmail.com Thu Jan 4 17:49:06 2007 From: kim.branson at gmail.com (Kim Branson) Date: Thu, 4 Jan 2007 08:49:06 -0800 Subject: [C++-sig] passing wrapped c++ objects to boost wrapped function Message-ID: <108D414A-34B7-4257-B422-62F54F23AE2E@stanford.edu> Hi all heres the complete minimal example. cheers kim #########test.cpp #include "functions.cpp" #include #include using namespace boost::python; BOOST_PYTHON_MODULE(test) { def("numberOfAtoms", numberOfAtoms); def("finalBondIndex",finalBondIndex); } ######functions.cpp /* * functions.cpp * OEMolTest */ #include "functions.h" int numberOfAtoms(OEMol &mol) { int numAtoms = 0 ; numAtoms = mol.NumAtoms(); return numAtoms; } int finalBondIndex(OEMol &mol) { int numOfBonds = 0 ; int index =0 ; OEIter bond; for ( bond=mol.GetBonds(); bond; ++bond ) { index = bond->GetEndIdx(); numOfBonds++; } return index ; } #########functions.h /* * functions.h * OEMolTest */ # include "oechem.h" # include "oesystem.h" using namespace OEChem ; using namespace OESystem; int numberOfAtoms(OEMol &mol); int finalBondIndex(OEMol &mol); #########Heres the python test case. openeye.oechem is a wrapped lib that provides OEMol objects #the same as the c++ code expects to be passed. #!/usr/bin/python from openeye.oechem import * import test mol = OEMol() print "mol is ",mol print type(mol) ifs = oemolistream("ligand.mol2") OEReadMolecule(ifs,mol) print "loaded mol" print "loaded mol is ",mol numatoms =0 numbonds =0 print "Call tes.numberOfAtoms with no arguments" try: numatoms = test.numberOfAtoms(); except: print "Caught exception " print "Call test.finalBondIndex with no arguments" try: numbonds = test.finalBondIndex() except: print "Caught exception " print "Call test.numberOfAtoms with OEMol argument" numatoms = test.numberOfAtoms(mol); print("Number of atoms is %i \n" %(numatoms)) #ligandObject = ligand.Ligand(mol) #####Jamfile # This is the top of our own project tree project-root ; # Include definitions needed for Python modules import python ; extension test # Declare a Python extension called test : test.cpp # source # requirements and dependencies for Boost.Python extensions