Cpp + Python: static data dynamic initialization in *nix shared lib?

Alf P. Steinbach /Usenet alf.p.steinbach+usenet at gmail.com
Tue Jul 13 17:22:24 EDT 2010


* sturlamolden, on 13.07.2010 22:06:
> On 13 Jul, 21:39, "Alf P. Steinbach /Usenet"<alf.p.steinbach
> +use... at gmail.com>  wrote:
>
>> Thanks! It seems that SCXX does those things that I've been planning to do but
>> haven't got around to (wrapping standard Python types), while what it doesn't do
>> (abstracting away all those tables etc. and mapping Python calls to C++ calls)
>> is what I've been working on. Which could be a Very Nice combination except that
>> I'm assuming Py3, while SCXX seems to be Py2 only. :-(
>
> I'd suggest PyCXX instead. http://cxx.sourceforge.net
>
> SCXX is a tiny wrapper mostly used in scipy.weave to inline C++ in
> Python.

Thanks. I looked up your URL, and PyCXX design goals seem to be much like what 
I'm doing, but different in some crucial ways. It's probably great, but it's not 
to my taste.

E.g. the PyCXX String class interface:


explicit 	String( PyObject *pyob, bool owned = false )
	String( const Object &ob )
	String()
	String( const char *latin1 )
	String( const char *latin1, Py_ssize_t size )
	String( const std::string &latin1 )
	String( const std::string &v, const char *encoding, const char *error=NULL )
	String( const char *s, const char *encoding, const char *error=NULL )
	String( const char *s, Py_ssize_t len, const char *encoding, const char 
*error=NULL )
String & 	operator=( const Object &o )
String & 	operator=( PyObject *p )
String & 	operator=( const unicodestring &v )
size_type 	size() const
size_type 	capacity() const
unicodestring 	as_unicodestring() const
std::string 	operator std::string() const
String 	encode( const char *encoding, const char *error="strict" )
std::string 	as_std_string( const char *encoding=NULL, const char 
*error="strict" ) const


In C++ the only way to portably specify a string literal with national 
characters, is as a wide string literal. Otherwise the result depends on the 
source code encoding. Yet PyCXX's String does not support wchar_t.

Also, things like the 'owned' option is just asking for trouble.

I chose this example because a Python string wrapper is the only object wrapper 
(apart from a general PyPtr) that I've implemented so far. The PyCXX string 
wrapper fails the design criterions of general usability (including in 
particular lack of wide char support) and safety (in particular the 'owned' 
option). And it's underdocumented, like, what encoding does the operator 
std::string() produce?

The details don't matter though. I'm sure that PyCXX is Very Nice for its 
purpose, as is e.g. Boost.Python (which is Very Very Nice for its purpose). :-)


Cheers, & thanks for the link,

- Alf

-- 
blog at <url: http://alfps.wordpress.com>



More information about the Python-list mailing list