aboutsummaryrefslogtreecommitdiffstats
path: root/libshiboken
diff options
context:
space:
mode:
authorHugo Parente Lima <hugo.pl@gmail.com>2011-10-20 19:56:48 -0200
committerHugo Parente Lima <hugo.pl@gmail.com>2012-03-08 16:18:31 -0300
commitb64c2001d14b8b1102af6b6029eb9c317e5d8008 (patch)
tree7f71a6115dc1467fd181784f2fd0cf55f5ce5ef0 /libshiboken
parentdc022d65ce87882fdf6add900038e957f399453b (diff)
Make converter branch work on a Python3.2 setup.
Diffstat (limited to 'libshiboken')
-rw-r--r--libshiboken/sbkconverter.cpp2
-rw-r--r--libshiboken/sbkconverter_p.h28
-rw-r--r--libshiboken/sbkpython.h1
-rw-r--r--libshiboken/sbkstring.cpp27
-rw-r--r--libshiboken/sbkstring.h3
5 files changed, 41 insertions, 20 deletions
diff --git a/libshiboken/sbkconverter.cpp b/libshiboken/sbkconverter.cpp
index b5223cc8a..79a2f65fa 100644
--- a/libshiboken/sbkconverter.cpp
+++ b/libshiboken/sbkconverter.cpp
@@ -222,7 +222,7 @@ void nonePythonToCppNullPtr(PyObject*, void* cppOut)
void* cppPointer(PyTypeObject* desiredType, SbkObject* pyIn)
{
assert(pyIn);
- SbkObjectType* inType = (SbkObjectType*)pyIn->ob_type;
+ SbkObjectType* inType = (SbkObjectType*)Py_TYPE(pyIn);
if (ObjectType::hasCast(inType))
return ObjectType::cast(inType, pyIn, desiredType);
return Object::cppPointer(pyIn, desiredType);
diff --git a/libshiboken/sbkconverter_p.h b/libshiboken/sbkconverter_p.h
index 8abdc297a..d12f1edc1 100644
--- a/libshiboken/sbkconverter_p.h
+++ b/libshiboken/sbkconverter_p.h
@@ -23,10 +23,11 @@
#ifndef SBK_CONVERTER_P_H
#define SBK_CONVERTER_P_H
-#include <Python.h>
+#include "sbkpython.h"
+#include "sbkconverter.h"
+#include "sbkstring.h"
#include <list>
#include <limits>
-#include "sbkconverter.h"
#include "sbkdbg.h"
@@ -169,10 +170,6 @@ struct TwoPrimitive : OnePrimitive<T>
// Integers --------------------------------------------------------------------------------
-// Note: if there wasn't for the old-style classes, a simple PyNumber_Check would suffice.
-#define SbkNumber_Check(X) (PyNumber_Check(X) \
- && (!PyInstance_Check(X) || PyObject_HasAttrString(X, "__trunc__")))
-
template <typename INT>
struct IntPrimitive : TwoPrimitive<INT>
{
@@ -327,11 +324,12 @@ struct CharPrimitive : IntPrimitive<CHAR>
{
static void toCpp(PyObject* pyIn, void* cppOut)
{
- *((CHAR*)cppOut) = (CHAR) PyString_AS_STRING(pyIn)[0];
+
+ *((CHAR*)cppOut) = (CHAR) Shiboken::String::toCString(pyIn)[0];
}
static PythonToCppFunc isConvertible(PyObject* pyIn)
{
- if (PyString_Check(pyIn) && PyString_Size(pyIn) == 1)
+ if (Shiboken::String::checkChar(pyIn))
return toCpp;
return 0;
}
@@ -361,7 +359,7 @@ template <> struct Primitive<unsigned char> : CharPrimitive<unsigned char> {};
template <> struct Primitive<char> : CharPrimitive<char> {
using CharPrimitive<char>::toPython;
static PyObject* toPython(const void* cppIn) {
- return PyString_FromFormat("%c", *((const char*)cppIn));
+ return Shiboken::String::fromCString((const char*)cppIn, 1);
}
};
@@ -376,7 +374,7 @@ struct Primitive<const char*> : TwoPrimitive<const char*>
{
if (!cppIn)
Py_RETURN_NONE;
- return PyString_FromString((const char*)cppIn);
+ return Shiboken::String::fromCString((const char*)cppIn);
}
static void toCpp(PyObject* pyIn, void* cppOut)
{
@@ -390,11 +388,11 @@ struct Primitive<const char*> : TwoPrimitive<const char*>
}
static void otherToCpp(PyObject* pyIn, void* cppOut)
{
- *((const char**)cppOut) = (const char*) PyString_AsString(pyIn);
+ *((const char**)cppOut) = (const char*) Shiboken::String::toCString(pyIn);
}
static PythonToCppFunc isOtherConvertible(PyObject* pyIn)
{
- if (PyString_Check(pyIn))
+ if (Shiboken::String::check(pyIn))
return otherToCpp;
return 0;
}
@@ -405,7 +403,7 @@ struct Primitive<std::string> : TwoPrimitive<std::string>
{
static PyObject* toPython(const void* cppIn)
{
- return PyString_FromString(((std::string*)cppIn)->c_str());
+ return Shiboken::String::fromCString(((std::string*)cppIn)->c_str());
}
static void toCpp(PyObject* pyIn, void* cppOut)
{
@@ -419,11 +417,11 @@ struct Primitive<std::string> : TwoPrimitive<std::string>
}
static void otherToCpp(PyObject* pyIn, void* cppOut)
{
- *((std::string*)cppOut) = std::string(PyString_AsString(pyIn));
+ *((std::string*)cppOut) = Shiboken::String::toCString(pyIn);
}
static PythonToCppFunc isOtherConvertible(PyObject* pyIn)
{
- if (PyString_Check(pyIn))
+ if (Shiboken::String::check(pyIn))
return otherToCpp;
return 0;
}
diff --git a/libshiboken/sbkpython.h b/libshiboken/sbkpython.h
index 850f122ff..90df2d4c2 100644
--- a/libshiboken/sbkpython.h
+++ b/libshiboken/sbkpython.h
@@ -41,6 +41,7 @@
#define SBK_NB_BOOL(x) (x).nb_bool
#define SBK_PyMethod_New PyMethod_New
#define PyInt_AsSsize_t(x) PyLong_AsSsize_t(x)
+ #define PyString_Type PyUnicode_Type
#else
// Note: if there wasn't for the old-style classes, only a PyNumber_Check would suffice.
diff --git a/libshiboken/sbkstring.cpp b/libshiboken/sbkstring.cpp
index de6842dce..9ea5f6993 100644
--- a/libshiboken/sbkstring.cpp
+++ b/libshiboken/sbkstring.cpp
@@ -21,6 +21,7 @@
*/
#include "sbkstring.h"
+#include "autodecref.h"
namespace Shiboken
{
@@ -68,16 +69,36 @@ PyObject* fromCString(const char* value)
#endif
}
-const char* toCString(PyObject* str)
+PyObject* fromCString(const char* value, int len)
+{
+#ifdef IS_PY3K
+ return PyUnicode_FromStringAndSize(value, len);
+#else
+ return PyBytes_FromStringAndSize(value, len);
+#endif
+}
+
+const char* toCString(PyObject* str, Py_ssize_t* len)
{
if (str == Py_None)
return NULL;
#ifdef IS_PY3K
- if (PyUnicode_Check(str))
+ if (PyUnicode_Check(str)) {
+ if (len) {
+ // We need to encode the unicode string into utf8 to know the size of returned char*.
+ Shiboken::AutoDecRef uniStr(PyUnicode_AsUTF8String(str));
+ *len = PyBytes_GET_SIZE(uniStr.object());
+ }
+ // Return unicode from str instead of uniStr, because the lifetime of the returned pointer
+ // depends on the lifetime of str.
return _PyUnicode_AsString(str);
+ }
#endif
- if (PyBytes_Check(str))
+ if (PyBytes_Check(str)) {
+ if (len)
+ *len = PyBytes_GET_SIZE(str);
return PyBytes_AS_STRING(str);
+ }
return 0;
}
diff --git a/libshiboken/sbkstring.h b/libshiboken/sbkstring.h
index d6bc6502f..112e71495 100644
--- a/libshiboken/sbkstring.h
+++ b/libshiboken/sbkstring.h
@@ -41,7 +41,8 @@ namespace String
LIBSHIBOKEN_API bool checkChar(PyObject* obj);
LIBSHIBOKEN_API bool isConvertible(PyObject* obj);
LIBSHIBOKEN_API PyObject* fromCString(const char* value);
- LIBSHIBOKEN_API const char* toCString(PyObject* str);
+ LIBSHIBOKEN_API PyObject* fromCString(const char* value, int len);
+ LIBSHIBOKEN_API const char* toCString(PyObject* str, Py_ssize_t* len = 0);
LIBSHIBOKEN_API bool concat(PyObject** val1, PyObject* val2);
LIBSHIBOKEN_API PyObject* fromFormat(const char* format, ...);
LIBSHIBOKEN_API PyObject* fromStringAndSize(const char* str, Py_ssize_t size);