c++ data types in python script

Arthur Mc Coy 1984docmccoy at gmail.com
Sun Mar 6 03:54:49 EST 2011


Hi people,

I've used SWIG module to embed python inside c++ app. I pass a list of
objects (with lots of different properties of types string, float,
custom types like URL, Software and finally of list of strings).

Now I'm in python. URL and Software has str() method that converts
their value to string recognizable by JSON. But the problem is with
list of strings.

So, as I said I passed std::list<std::string> myObjects to python
function.
Then I iterate it and for each object in myObjects I create a python
copy (serialize it) to be able to put into JSON format and store in
appropriate file.

object has property benchmarks of type list<string>.
I do:
...
class PythonObject:
    def __init__(self, object):
        self.benchmarks = list()
        for s in object.benchmarks:
            self.benchmarks.append(s)
...
and it fails, also I do:
...
class PythonObject:
    def __init__(self, object):
        self.benchmarks = [unicode(s) for s in object.benchmarks]
...
and it fails, also I do:
...
class PythonObject:
    def __init__(self, object):
        for s in object.benchmarks:
            print s[0] + s[1] + s[2]
            print type(s)
...
and it fails printing
   wor
   <type 'str'>
   Segmentation fault (core dumped)
$
also I do:
...
class PythonObject:
    def __init__(self, object):
        self.benchmarks = unicode(object.benchmarks)
...
and it does not fail, instead it puts in JSON this string:
...
"benchmarks": "<mymodule.StringList; proxy of <Swig Object of type
'std::list< std::string, std::allocator< std::string > > *' at
0xb63ed4e8>>",
...
but it is not what I need

What I'm trying to stress is that c++ objects should be converted
(serialized) before putting them into json. Otherwise type errors
occur and process fails.


I love learning python and hope somebody may suggest me or tell
something.

Thank you all anyway!

Arthur



More information about the Python-list mailing list