Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » Developing U++ » UppHub » PythonPP - tiny C++ wrappers for Python C API
PythonPP - tiny C++ wrappers for Python C API [message #31876] Mon, 04 April 2011 06:26 Go to next message
Novo is currently offline  Novo
Messages: 1358
Registered: December 2006
Ultimate Contributor
PythonPP is an initial version of a port to UPP of tiny C++ wrappers for Python C API, which I developed a long time ago. They are somewhat similar to the wrappers in the BOOST library, although they are significantly less complicated.

If you are interested in such kind of code I can keep porting/improving it. It basically allows to call Python scripts from C++, or to expose C++ code to Python. The whole UPP can be exposed to Python Smile

EDIT: Update file with source code.
  • Attachment: PythonPP.zip
    (Size: 28.87KB, Downloaded 379 times)


Regards,
Novo

[Updated on: Tue, 12 April 2011 16:43]

Report message to a moderator

Re: PythonPP - tiny C++ wrappers for Python C API [message #31885 is a reply to message #31876] Mon, 04 April 2011 18:22 Go to previous messageGo to next message
nlneilson is currently offline  nlneilson
Messages: 644
Registered: January 2010
Location: U.S. California. Mojave &...
Contributor
It looks interesting.

I looked for some kind of Help or Readme file for an explanation of how this can be used, capabilities or limitations (API?).

I have used sockets for interaction between Python, C++, and Java.

Your PythonPP may be an easier/cleaner/more efficient way in certain situations.

Neil
Re: PythonPP - tiny C++ wrappers for Python C API [message #31894 is a reply to message #31885] Mon, 04 April 2011 22:44 Go to previous messageGo to next message
Novo is currently offline  Novo
Messages: 1358
Registered: December 2006
Ultimate Contributor
nlneilson wrote on Mon, 04 April 2011 12:22

It looks interesting.

I looked for some kind of Help or Readme file for an explanation of how this can be used, capabilities or limitations (API?).

I have used sockets for interaction between Python, C++, and Java.

Your PythonPP may be an easier/cleaner/more efficient way in certain situations.

Neil


PythonPP can do pretty much the same as Boost.Python. It serves the same purpose - to make using of Python's API easier. Capabilities are similar. PythonPP is probably missing some advanced features of Boost.Python, but they can be added.

If you want to use raw Python C API you will need to learn how to deal with dozens of functions, internal structures, and reference counting. PythonPP is hiding all this complexity from you, and it is making your code safer and readable.

I'll try to make more examples tonight Smile


Regards,
Novo
Re: PythonPP - tiny C++ wrappers for Python C API [message #31896 is a reply to message #31894] Tue, 05 April 2011 02:50 Go to previous messageGo to next message
Novo is currently offline  Novo
Messages: 1358
Registered: December 2006
Ultimate Contributor
A simple example of calling Python from C++:
#include <PythonPP/PythonPP.h>
#include <PythonPP/PythonPP_emb.h>

using namespace Upp;

CONSOLE_APP_MAIN
{
	pythonpp::Engine eng;
	eng.ExecuteStr("print 'Hello World!'");
}


Regards,
Novo
Re: PythonPP - tiny C++ wrappers for Python C API [message #31897 is a reply to message #31876] Tue, 05 April 2011 06:19 Go to previous messageGo to next message
nlneilson is currently offline  nlneilson
Messages: 644
Registered: January 2010
Location: U.S. California. Mojave &...
Contributor
PythonPP_emb.cpp
c:\myapps\pythonpp\PythonPP_error.h(25) : fatal error C1083: Cannot open include file: 'Python.h': No
	 such file or directory
c:\myapps\pythonpp\PythonPP_error.h(25) : fatal error C1083: Cannot open include file: 'Python.h': No
	 such file or directory
PythonPP_ext.cpp
PythonPP: 2 file(s) built in (0:01.30), 652 msecs / file, duration = 1344 msecs, parallelization 100%

I unzipped your PythonPP.zip
Put PythonPP and PythonPPApp in MyApps
errors
Copied everything from PythonPP into PythonPPApp
same errors as above

In PythonPP.upp
library(LINUX) python2.6;
Apparently this is set up for Linux only with Python 2.x

I tried it on Win XP where I just have Python 3.2
My problem not your set up.

Neil

[Updated on: Tue, 05 April 2011 06:39]

Report message to a moderator

Re: PythonPP - tiny C++ wrappers for Python C API [message #31898 is a reply to message #31897] Tue, 05 April 2011 07:02 Go to previous messageGo to next message
nlneilson is currently offline  nlneilson
Messages: 644
Registered: January 2010
Location: U.S. California. Mojave &...
Contributor
In Ubuntu 10.10 with the default Python 2.6
----- Core ( SSE2 GCC SHARED LINUX POSIX ) (1 / 3)
----- PythonPP ( SSE2 GCC SHARED LINUX POSIX ) (2 / 3)
PythonPP_emb.cpp
PythonPP_ext.cpp
In file included from /home/neil/upp/MyApps/PythonPP/PythonPP_object.h:10,
                 from /home/neil/upp/MyApps/PythonPP/PythonPP_pdt.h:10,
                 from /home/neil/upp/MyApps/PythonPP/PythonPP.h:10,
                 from /home/neil/upp/MyApps/PythonPP/PythonPP_emb.h:23,
                 from /home/neil/upp/MyApps/PythonPP/PythonPP_emb.cpp:1:
/home/neil/upp/MyApps/PythonPP/PythonPP_error.h:25: fatal error: Python.h: No such file or
	 directory
compilation terminated.
In file included from /home/neil/upp/MyApps/PythonPP/PythonPP_object.h:10,
                 from /home/neil/upp/MyApps/PythonPP/PythonPP_seq.h:10,
                 from /home/neil/upp/MyApps/PythonPP/PythonPP_ext.h:10,
                 from /home/neil/upp/MyApps/PythonPP/PythonPP_ext.cpp:1:
/home/neil/upp/MyApps/PythonPP/PythonPP_error.h:25: fatal error: Python.h: No such file or
	 directory
compilation terminated.
PythonPP: 2 file(s) built in (0:00.20), 104 msecs / file, duration = 249 msecs, paralleliz
	ation 90%

There were errors. (0:00.43)
Re: PythonPP - tiny C++ wrappers for Python C API [message #31903 is a reply to message #31898] Tue, 05 April 2011 16:44 Go to previous messageGo to next message
Novo is currently offline  Novo
Messages: 1358
Registered: December 2006
Ultimate Contributor
On Linux you need to install python-dev (it will install v2.6 on my system). After that add /usr/include/python2.6 to include directories for GCC build method.

Unfortunately, there is no generic libpython library. They all have versions.

I haven't tried to compile PythonPP on Windows. It is an initial version.


Regards,
Novo
Re: PythonPP - tiny C++ wrappers for Python C API [message #31964 is a reply to message #31903] Tue, 12 April 2011 09:25 Go to previous messageGo to next message
kohait00 is currently offline  kohait00
Messages: 939
Registered: July 2009
Location: Germany
Experienced Contributor
hi novo

i'd like to compare the features / API invocation of PythonPP to that one of boost.Python. could you help here? since there is no API reference

yesterday i have added a BoostPyTest to bazaar. could you provide a similar one?
Re: PythonPP - tiny C++ wrappers for Python C API [message #31969 is a reply to message #31964] Tue, 12 April 2011 16:48 Go to previous messageGo to next message
Novo is currently offline  Novo
Messages: 1358
Registered: December 2006
Ultimate Contributor
kohait00 wrote on Tue, 12 April 2011 03:25

hi novo

i'd like to compare the features / API invocation of PythonPP to that one of boost.Python. could you help here? since there is no API reference

yesterday i have added a BoostPyTest to bazaar. could you provide a similar one?


Hi kohait00,
I updated source code of PythonPP. I also added an example of a module, which unfortunately doesn't work yet.

#include <Core/Core.h>
#include <PythonPP/PythonPP.h>
#include <PythonPP/PythonPP_ext.h>

//////////////////////////////////////////////////////////////////////////////
// Compatibility macros
//
// From Python 2.2 to 2.3, the way to export the module init function
// has changed. These macros keep the code compatible to both ways.
//
#if PY_VERSION_HEX >= 0x02030000
#  define PYDBAPI_MODINIT_FUNC(name)         PyMODINIT_FUNC name(void)
#else
#  define PYDBAPI_MODINIT_FUNC(name)         DL_EXPORT(void) name(void)
#endif

using namespace Upp;

namespace python
{

	class Test : public pythonpp::ExtObject<Test>
	{
	public:
		Test();
		
		pythonpp::Object Foo ( const pythonpp::Tuple& args );
		pythonpp::Object Boo ( const pythonpp::Tuple& args );
	};

	Test::Test()
	{
		ROAttr( "__class__", GetTypeObject() );
		PrepareForPython(this); 
	}
	
	pythonpp::Object Test::Foo ( const pythonpp::Tuple& /*args*/ )
	{
		Cout() << "This is a UPP call !" << EOL;
	}

	pythonpp::Object Test::Boo ( const pythonpp::Tuple& /*args*/ )
	{
		Cout() << "This is Boo !" << EOL;
	}
}

///////////////////////////////////////////////////////////////////////////////

static struct PyMethodDef methods[] = {
	{ NULL, NULL }
};

///////////////////////////////////////////////////////////////////////////////
// Module initialization
PYDBAPI_MODINIT_FUNC ( initPythonPPModule )
{
	pythonpp::ModuleExt::Declare ( "PythonPPModule", methods );
	PyObject *module = pythonpp::ModuleExt::GetPyModule();

	// Declare class Test.
	python::Test::
		Def ( "Foot", &python::Test::Foo, "Method #1" ).
		Def ( "Boo", &python::Test::Boo, "Method #2" );
	python::Test::Declare ( "PythonPPModule.Test" );

	if ( PyType_Ready ( &python::Test::GetType() ) == -1 )
	{
		return;
	}

	if ( PyModule_AddObject ( module, const_cast<char*> ( "Test" ), ( PyObject* ) &python::Test::GetType() ) == -1 )
	{
		return;
	}
}



I spent a couple of evenings trying to bring it back to life and understood that it is more complicated than I though.

Unless there is some kind of restriction the best way to go is Boost.Python. Smile


Regards,
Novo
Re: PythonPP - tiny C++ wrappers for Python C API [message #31970 is a reply to message #31969] Tue, 12 April 2011 17:17 Go to previous messageGo to next message
kohait00 is currently offline  kohait00
Messages: 939
Registered: July 2009
Location: Germany
Experienced Contributor
iäm actually open to any alternatives..so no plans for further PythonPP development?

if so, i'd love to have some help in 'exposing' upp to python..via boost then.
Re: PythonPP - tiny C++ wrappers for Python C API [message #31981 is a reply to message #31970] Thu, 14 April 2011 05:35 Go to previous messageGo to next message
Novo is currently offline  Novo
Messages: 1358
Registered: December 2006
Ultimate Contributor
kohait00 wrote on Tue, 12 April 2011 11:17

iäm actually open to any alternatives..so no plans for further PythonPP development?


My memory is getting back slowly. Besides just bringing PythonPP back to life it requires complete redesign of marshalling code. Old code never looks good.

To be fair, I already promised a lot to many people. Before PythonPP I promised a firebird driver for UPP. I think I will switch to firebird now.

Quote:


if so, i'd love to have some help in 'exposing' upp to python..via boost then.


Of course, Boost is the best, unless there are political issues related to it. IMHO you shouldn't have any problem using it because it was around for a decade and you should be able to find a lot of examples on internet. This can be an inspirational reading


Regards,
Novo
Re: PythonPP - tiny C++ wrappers for Python C API [message #31982 is a reply to message #31981] Thu, 14 April 2011 07:25 Go to previous message
kohait00 is currently offline  kohait00
Messages: 939
Registered: July 2009
Location: Germany
Experienced Contributor
Well there are plenty examples on using python but when it comes to embedding python there is almost but no quality information available.my current boost py test package is evolving slowly.but I am on the way.often problems arise around ownership issues since most info leaves it to python but when it comes to exposing existing upp instances almost no examples.

So far my biggest concern is to choose the right upp representation for the python data types.which is ready for the trivial types but cumbersome for the containers.see my boost python thread
Thanks


Previous Topic: LogCtrl, a LOG/RLOG redirector to a DocEdit
Next Topic: "Alternative Multithreading" revisited
Goto Forum:
  


Current Time: Thu Mar 28 10:18:25 CET 2024

Total time taken to generate the page: 0.01477 seconds