You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

189 lines
4.7 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2012 Miguel Angel Ajo <miguelangel@nbee.es>
  5. * Copyright (C) 1992-2012 KiCad Developers, see AUTHORS.txt for contributors.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, you may find one here:
  19. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  20. * or you may search the http://www.gnu.org website for the version 2 license,
  21. * or you may write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  23. */
  24. /**
  25. * @file wx_python_helpers.cpp
  26. * @brief Python wrapping helpers for wx structures/objects
  27. */
  28. #include <Python.h>
  29. #include <wx/intl.h>
  30. #include <wx/string.h>
  31. #include <wx/arrstr.h>
  32. #define WX_DEFAULTENCODING_SIZE 64
  33. static char wxPythonEncoding[WX_DEFAULTENCODING_SIZE] = "ascii";
  34. PyObject* wxArrayString2PyList( const wxArrayString& lst )
  35. {
  36. PyObject* list = PyList_New( 0 );
  37. for( size_t i = 0; i < lst.GetCount(); i++ )
  38. {
  39. #if wxUSE_UNICODE
  40. PyObject* pyStr = PyUnicode_FromWideChar( lst[i].c_str(),
  41. lst[i].Len()
  42. );
  43. #else
  44. PyObject* pyStr = PyString_FromStringAndSize( lst[i].c_str(),
  45. lst[i].Len()
  46. );
  47. #endif
  48. PyList_Append( list, pyStr );
  49. Py_DECREF( pyStr );
  50. }
  51. return list;
  52. }
  53. wxString* newWxStringFromPy( PyObject* src )
  54. {
  55. bool must_unref_str = false;
  56. wxString* result = NULL;
  57. PyObject* obj = src;
  58. #if wxUSE_UNICODE
  59. bool must_unref_obj = false;
  60. // Unicode string to python unicode string
  61. PyObject* uni_str = src;
  62. // if not an str or unicode, try to str(src)
  63. if( !PyString_Check( src ) && !PyUnicode_Check( src ) )
  64. {
  65. obj = PyObject_Str( src );
  66. must_unref_obj = true;
  67. if( PyErr_Occurred() )
  68. return NULL;
  69. }
  70. if( PyString_Check( obj ) )
  71. {
  72. uni_str = PyUnicode_FromEncodedObject( obj, wxPythonEncoding, "strict" );
  73. must_unref_str = true;
  74. if( PyErr_Occurred() )
  75. return NULL;
  76. }
  77. result = new wxString();
  78. size_t len = PyUnicode_GET_SIZE( uni_str );
  79. if( len )
  80. {
  81. PyUnicode_AsWideChar( (PyUnicodeObject*) uni_str,
  82. wxStringBuffer( *result, len ), len );
  83. }
  84. if( must_unref_str )
  85. Py_DECREF( uni_str );
  86. if( must_unref_obj )
  87. Py_DECREF( obj );
  88. #else
  89. // normal string (or object) to normal python string
  90. PyObject* str = src;
  91. if( PyUnicode_Check( src ) ) // if it's unicode convert to normal string
  92. {
  93. str = PyUnicode_AsEncodedString( src, wxPythonEncoding, "strict" );
  94. if( PyErr_Occurred() )
  95. return NULL;
  96. }
  97. else if( !PyString_Check( src ) ) // if it's not a string, str(obj)
  98. {
  99. str = PyObject_Str( src );
  100. must_unref_str = true;
  101. if( PyErr_Occurred() )
  102. return NULL;
  103. }
  104. // get the string pointer and size
  105. char* str_ptr;
  106. Py_ssize_t str_size;
  107. PyString_AsStringAndSize( str, &str_ptr, &str_size );
  108. // build the wxString from our pointer / size
  109. result = new wxString( str_ptr, str_size );
  110. if( must_unref_str )
  111. Py_DECREF( str );
  112. #endif
  113. return result;
  114. }
  115. wxString Py2wxString( PyObject* src )
  116. {
  117. wxString result;
  118. wxString* resPtr = newWxStringFromPy( src );
  119. // In case of exception clear it and return an empty string
  120. if( resPtr==NULL )
  121. {
  122. PyErr_Clear();
  123. return wxEmptyString;
  124. }
  125. result = *resPtr;
  126. delete resPtr;
  127. return result;
  128. }
  129. PyObject* wx2PyString( const wxString& src )
  130. {
  131. PyObject* str;
  132. #if wxUSE_UNICODE
  133. str = PyUnicode_FromWideChar( src.c_str(), src.Len() );
  134. #else
  135. str = PyString_FromStringAndSize( src.c_str(), src.Len() );
  136. #endif
  137. return str;
  138. }
  139. void wxSetDefaultPyEncoding( const char* encoding )
  140. {
  141. strncpy( wxPythonEncoding, encoding, WX_DEFAULTENCODING_SIZE );
  142. }
  143. const char* wxGetDefaultPyEncoding()
  144. {
  145. return wxPythonEncoding;
  146. }