[PyKDE] sip undefined type

Torsten Marek shlomme at gmx.net
Wed Feb 16 21:58:13 GMT 2005


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Phil Thompson schrieb:
|>Sirs:
|>
|>I am trying to wrap the xbase c++ library so I can handle ca clipper xbase
|>files from python.  (If anyone out there has a better suggestion I am all
|>ears.)
|>
|>When I try to run sip against my wrapper class I get an error similar to
|>the
|>following (different type, same error)
|>
|>sip -c . pytest.sip
|>sip: std::string is undefined
|>
|>This actual error is from running the following sip file, against the
|>following c++ code.
|>
|>I have written a small test class, a rather crude stack in c++ and was
|>able to
|>wrap it with sip.  When I added a string variable, it died with the above
|>error.
|>**********************************************
|>pytest.sip
|>
|>%Module pytest 0
|>
|>class Stack
|>{
|>
|>%TypeHeaderCode
|>#include "test4.h"
|>%End
|>
|>public:
|>  std::string foo;
|>  Stack();
|>
|>  void push(const int);
|>  int pop();
|>
|>*************************************************
|>
|>test4.h
|>#include <string>
|>
|>class Stack
|>{
|>private:
|>  int count;
|>  int data[100];
|>
|>public:
|>  std::string foo;
|>  Stack();
|>
|>  void push(const int item);
|>  int pop();
|>
|>};
|>
|>***************************************************************
|>
|>test4.cpp
|>#include "test4.h"
|>
|>Stack::Stack()
|>{
|>  count = 0;
|>};
|>
|>
|>void Stack::push(const int item)
|>{
|>  data[count] = item;
|>  count++;
|>
|>};
|>
|>int Stack::pop()
|>{
|>  count--;
|>  return data[count];
|>};
|>*****************************************************************
|>
|>This code compiles and links just fine in c++
|>I can create a lib file and call it from a c++ program.
|>If I remove the string foo; line, I can sip it and use it from python.
|>
|>I am an excelent programmer in clipper, a passable programer in python and
|>an
|>amature in c++.
|>
|>I would be more than glad to rtfm if I had a hint about which fm and where
|>to
|>look.  I have looked thru the last 6 months or so of the mailing list
|>archives without any hints.
|>
|>It would seem as if there is something I am missing somewhere, simple and
|>obvious that I am looking past.
|
|
| SIP doesn't know anything about std::string - you have to wrap it.
|

Hi,

that's a simple wrapper for std::string that allows you to transparently use
this class; you might want to remove the UTF-8 stuff and use your own encoding
or just plain ASCII.

greets

Torsten

== code ==

%MappedType std::string
{
%TypeHeaderCode
#include <string>
%End

%ConvertFromTypeCode
~    // convert an std::string to a Python (unicode) string
~    PyObject* newstring;
~    newstring = PyUnicode_DecodeUTF8(sipCpp->c_str(), sipCpp->length(), NULL);
~    if(newstring == NULL) {
~        PyErr_Clear();
~        newstring = PyString_FromString(sipCpp->c_str());
~    }
~    return newstring;
%End

%ConvertToTypeCode
	// Allow a Python string (or a unicode string) whenever a string is
	// expected.
~    // If argument is a Unicode string, just decode it to UTF-8
~    // If argument is a Python string, assume it's UTF-8
	if (sipIsErr == NULL)
		return (PyString_Check(sipPy) || PyUnicode_Check(sipPy));
	if (sipPy == Py_None) {
		*sipCppPtr = new std::string;
		return 1;
	}
	if (PyUnicode_Check(sipPy))	{
~        PyObject* s = PyUnicode_AsEncodedString(sipPy, "UTF-8", "");
~        *sipCppPtr = new std::string(PyString_AS_STRING(s));
~        Py_DECREF(s);
~        return 1;
	}
	if (PyString_Check(sipPy)) {
~        *sipCppPtr = new std::string(PyString_AS_STRING(sipPy));
~    	return 1;
	}
~    return 0;
%End
};


- --
Torsten Marek <shlomme at gmx.net>
ID: A244C858 -- FP: 1902 0002 5DFC 856B F146  894C 7CC5 451E A244 C858
Keyserver: subkeys.pgp.net

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.0 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFCE8H1fMVFHqJEyFgRAkUXAJ0brDY4ad81tfDmq0uyZABPkJi/iwCfVnUb
CYuJIzoXg6CrAjVn+nzMYWI=
=wnQH
-----END PGP SIGNATURE-----




More information about the PyQt mailing list