Commit 530e3b55 authored by Ben Boeckel's avatar Ben Boeckel
Browse files

Merge remote-tracking branch 'gl/dgobbi/python-py3k' into release-6.3

* gl/dgobbi/python-py3k:
  Convert the PythonInterpreter module for Python 3.
  Fix for Python 3.5 compilation on Linux, Win32.
parents 849afc86 056d5d74
......@@ -88,21 +88,30 @@ public:
Py_INCREF(this->InteractiveConsole);
Py_INCREF(this->InteractiveConsoleLocals);
PyRun_SimpleString(
const_cast<char*>("del __vtkConsole; del __vtkConsoleLocals"));
PyRun_SimpleString("del __vtkConsole; del __vtkConsoleLocals");
// Maybe we need an API to enable developers to set the prompts.
PyObject* ps1 = PySys_GetObject(const_cast<char*>("ps1"));
PyObject* ps1 = PySys_GetObject("ps1");
if (!ps1)
{
PySys_SetObject(const_cast<char*>("ps1"), ps1 = PyString_FromString(">>> "));
#if PY_VERSION_HEX >= 0x03000000
ps1 = PyUnicode_FromString(">>> ");
#else
ps1 = PyString_FromString(">>> ");
#endif
PySys_SetObject("ps1", ps1);
Py_XDECREF(ps1);
}
PyObject* ps2 = PySys_GetObject(const_cast<char*>("ps2"));
PyObject* ps2 = PySys_GetObject("ps2");
if (!ps2)
{
PySys_SetObject(const_cast<char*>("ps2"), ps2 = PyString_FromString("... "));
#if PY_VERSION_HEX >= 0x03000000
ps2 = PyUnicode_FromString("... ");
#else
ps2 = PyString_FromString("... ");
#endif
PySys_SetObject("ps2", ps2);
Py_XDECREF(ps2);
}
......@@ -200,10 +209,19 @@ int vtkPythonInteractiveInterpreter::RunStringWithConsoleLocals(const char* scri
}
Py_DECREF(result);
#if PY_VERSION_HEX >= 0x03000000
PyObject *f = PySys_GetObject("stdout");
if (f == 0 || PyFile_WriteString("\n", f) != 0)
{
PyErr_Clear();
}
#else
if (Py_FlushLine())
{
PyErr_Clear();
}
#endif
return 0;
}
......
......@@ -27,6 +27,15 @@
#include <vector>
#include <signal.h>
#if PY_VERSION_HEX >= 0x03000000
#if defined(__APPLE__) && PY_VERSION_HEX < 0x03050000
extern "C" {
extern wchar_t*
_Py_DecodeUTF8_surrogateescape(const char *s, Py_ssize_t size);
}
#endif
#endif
namespace
{
class StringPool
......@@ -61,7 +70,11 @@ namespace
inline void vtkPrependPythonPath(const char* pathtoadd)
{
PyObject* path = PySys_GetObject(const_cast<char*>("path"));
#if PY_VERSION_HEX >= 0x03000000
PyObject* newpath = PyUnicode_FromString(pathtoadd);
#else
PyObject* newpath = PyString_FromString(pathtoadd);
#endif
PyList_Insert(path, 0, newpath);
Py_DECREF(newpath);
}
......@@ -188,7 +201,27 @@ void vtkPythonInterpreter::SetProgramName(const char* programname)
// string in static storage whose contents will not change for the duration of
// the program's execution. No code in the Python interpreter will change the
// contents of this storage.
#if PY_VERSION_HEX >= 0x03000000
wchar_t *argv0;
const std::string& av0 = pool.Strings.back();
#if PY_VERSION_HEX >= 0x03050000
argv0 = Py_DecodeLocale(av0.c_str(), NULL);
#elif defined(__APPLE__)
argv0 = _Py_DecodeUTF8_surrogateescape(av0.data(), av0.length());
#else
argv0 = _Py_char2wchar(av0.c_str(), NULL);
#endif
if (argv0 == 0)
{
fprintf(stderr, "Fatal vtkpython error: "
"unable to decode the program name\n");
static wchar_t empty[1] = { 0 };
argv0 = empty;
}
Py_SetProgramName(argv0);
#else
Py_SetProgramName(pool.Strings.back());
#endif
}
}
......@@ -225,7 +258,61 @@ int vtkPythonInterpreter::PyMain(int argc, char** argv)
vtkPythonInterpreter::SetProgramName(argv[0]);
}
vtkPythonInterpreter::Initialize(1);
#if PY_VERSION_HEX >= 0x03000000
wchar_t *argv0;
#if PY_VERSION_HEX >= 0x03050000
argv0 = Py_DecodeLocale(argv[0], NULL);
#elif defined(__APPLE__)
argv0 = _Py_DecodeUTF8_surrogateescape(argv[0], strlen(argv[0]));
#else
argv0 = _Py_char2wchar(argv[0], NULL);
#endif
if (argv0 == 0)
{
static wchar_t empty[1] = { 0 };
argv0 = empty;
}
// Need two copies of args, because programs might modify the first
wchar_t **argvWide = new wchar_t *[argc];
wchar_t **argvWide2 = new wchar_t *[argc];
for (int i = 0; i < argc; i++)
{
#if PY_VERSION_HEX >= 0x03050000
argv0 = Py_DecodeLocale(argv[i], NULL);
#elif defined(__APPLE__)
argvWide[i] = _Py_DecodeUTF8_surrogateescape(argv[i], strlen(argv[i]));
#else
argvWide[i] = _Py_char2wchar(argv[i], NULL);
#endif
argvWide2[i] = argvWide[i];
if (argvWide[i] == 0)
{
fprintf(stderr, "Fatal vtkpython error: "
"unable to decode the command line argument #%i\n",
i + 1);
for (int k = 0; k < i; k++)
{
PyMem_Free(argvWide2[i]);
}
PyMem_Free(argv0);
delete [] argvWide;
delete [] argvWide2;
return 1;
}
}
int res = Py_Main(argc, argvWide);
PyMem_Free(argv0);
for (int i = 0; i < argc; i++)
{
PyMem_Free(argvWide2[i]);
}
delete [] argvWide;
delete [] argvWide2;
return res;
#else
return Py_Main(argc, argv);
#endif
}
//----------------------------------------------------------------------------
......
......@@ -89,9 +89,12 @@ static PyMemberDef vtkPythonStdStreamCaptureHelperMembers[] = {
};
static PyTypeObject vtkPythonStdStreamCaptureHelperType = {
PyObject_HEAD_INIT(NULL)
0, // ob_size
const_cast<char*>("vtkPythonStdStreamCaptureHelper"), // tp_name
#if PY_VERSION_HEX >= 0x02060000
PyVarObject_HEAD_INIT(&PyType_Type, 0)
#else
PyObject_HEAD_INIT(&PyType_Type) 0,
#endif
"vtkPythonStdStreamCaptureHelper", // tp_name
sizeof(vtkPythonStdStreamCaptureHelper), // tp_basicsize
0, // tp_itemsize
0, // tp_dealloc
......@@ -110,7 +113,7 @@ static PyTypeObject vtkPythonStdStreamCaptureHelperType = {
PyObject_GenericSetAttr, // tp_setattro
0, // tp_as_buffer
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, // tp_flags
const_cast<char*>("vtkPythonStdStreamCaptureHelper"), // tp_doc
"vtkPythonStdStreamCaptureHelper", // tp_doc
0, // tp_traverse
0, // tp_clear
0, // tp_richcompare
......@@ -135,11 +138,12 @@ static PyTypeObject vtkPythonStdStreamCaptureHelperType = {
0, // PyObject *tp_cache;
0, // PyObject *tp_subclasses;
0, // PyObject *tp_weaklist;
#if PY_VERSION_HEX >= 0x02060000 // tp_version_tag shows up in python 2.6
0, // tp_del
0 // tp_version_tag
#elif PY_VERSION_HEX >= 0x02030000 // tp_del shows up in python 2.3
0 // tp_del
#if PY_VERSION_HEX >= 0x02060000
0, // tp_version_tag
#endif
#if PY_VERSION_HEX >= 0x03040000
0, // tp_finalize
#endif
};
......@@ -155,11 +159,11 @@ static PyObject* vtkWrite(PyObject* self, PyObject* args)
char *string;
// const_cast since older versions of python are not const correct.
if (wrapper && PyArg_ParseTuple(args, const_cast<char*>("s"), &string))
if (wrapper && PyArg_ParseTuple(args, "s", &string))
{
wrapper->Write(string);
}
return Py_BuildValue(const_cast<char*>(""));
return Py_BuildValue("");
}
static PyObject* vtkRead(PyObject* self, PyObject* args)
......@@ -178,7 +182,7 @@ static PyObject* vtkRead(PyObject* self, PyObject* args)
{
ret = wrapper->Read();
}
return Py_BuildValue(const_cast<char*>("s"), const_cast<char*>(ret.c_str()));
return Py_BuildValue("s", ret.c_str());
}
static PyObject* vtkFlush(PyObject* self, PyObject* args)
......@@ -195,7 +199,7 @@ static PyObject* vtkFlush(PyObject* self, PyObject* args)
{
wrapper->Flush();
}
return Py_BuildValue(const_cast<char*>(""));
return Py_BuildValue("");
}
static PyObject* vtkClose(PyObject* self, PyObject* args)
......@@ -212,7 +216,7 @@ static PyObject* vtkClose(PyObject* self, PyObject* args)
{
wrapper->Close();
}
return Py_BuildValue(const_cast<char*>(""));
return Py_BuildValue("");
}
static vtkPythonStdStreamCaptureHelper* NewPythonStdStreamCaptureHelper(bool for_stderr=false)
......
......@@ -89,12 +89,14 @@ extern "C" {
#endif
}
#if defined(VTK_PY3K) && defined(__APPLE__)
#ifdef VTK_PY3K
#if defined(__APPLE__) && PY_VERSION_HEX < 0x03050000
extern "C" {
extern wchar_t*
_Py_DecodeUTF8_surrogateescape(const char *s, Py_ssize_t size);
}
#endif
#endif
static void vtkPythonAppInitEnableMSVCDebugHook();
static void vtkPythonAppInitPrependPath(const char* self_dir);
......@@ -161,7 +163,9 @@ int main(int argc, char **argv)
std::string av0 = vtksys::SystemTools::CollapseFullPath(argv[0]);
#ifdef VTK_PY3K
wchar_t *argv0;
#ifdef __APPLE__
#if PY_VERSION_HEX >= 0x03050000
argv0 = Py_DecodeLocale(av0.c_str(), NULL);
#elif defined(__APPLE__)
argv0 = _Py_DecodeUTF8_surrogateescape(av0.data(), av0.length());
#else
argv0 = _Py_char2wchar(av0.c_str(), NULL);
......@@ -218,7 +222,9 @@ int main(int argc, char **argv)
wchar_t **argvWide2 = new wchar_t *[argc];
for (int i = 0; i < argc; i++)
{
#ifdef __APPLE__
#if PY_VERSION_HEX >= 0x03050000
argvWide[i] = Py_DecodeLocale(argv[i], NULL);
#elif defined(__APPLE__)
argvWide[i] = _Py_DecodeUTF8_surrogateescape(argv[i], strlen(argv[i]));
#else
argvWide[i] = _Py_char2wchar(argv[i], NULL);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment