diff --git a/pcbnew/swig/python_scripting.cpp b/pcbnew/swig/python_scripting.cpp index 484cb55ed5..eefc09a635 100644 --- a/pcbnew/swig/python_scripting.cpp +++ b/pcbnew/swig/python_scripting.cpp @@ -319,6 +319,26 @@ void pcbnewFinishPythonScripting() } +wxString PyEscapeString( const wxString& aSource ) +{ + wxString converted; + + for( wxUniChar c: aSource ) + { + if( c == '\\' ) + converted += "\\\\"; + else if( c == '\'' ) + converted += "\\\'"; + else if( c == '\"' ) + converted += "\\\""; + else + converted += c; + } + + return converted; +} + + void pcbnewUpdatePythonEnvVar( const wxString& aVar, const wxString& aValue ) { char cmd[1024]; @@ -327,11 +347,14 @@ void pcbnewUpdatePythonEnvVar( const wxString& aVar, const wxString& aValue ) if( !Py_IsInitialized() ) return; + wxString escapedVar = PyEscapeString( aVar ); + wxString escapedVal = PyEscapeString( aValue ); + snprintf( cmd, sizeof( cmd ), "# coding=utf-8\n" // The values could potentially be UTF8 "os.environ[\"%s\"]=\"%s\"\n", - TO_UTF8( aVar ), - TO_UTF8( aValue ) ); + TO_UTF8( escapedVar ), + TO_UTF8( escapedVal ) ); PyLOCK lock;