Skip to content

Commit

Permalink
Fix #4472 - Modify Python in typemaps for path to convert from stri…
Browse files Browse the repository at this point in the history
…ng with Py3 support (type of str changed from Py2 to Py3)
  • Loading branch information
jmarrec committed Oct 19, 2021
1 parent d92b642 commit dde0966
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/utilities/core/Path.i
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,12 @@ namespace openstudio {
}else{
SWIG_exception_fail(SWIG_ValueError, "Invalid null reference openstudio::path const &");
}
} else if(PyUnicode_Check($input)) {
// Python 3
std::string s(PyUnicode_AsUTF8($input));
$1 = openstudio::toPath(s);
} else if(PyString_Check($input)) {
// Python2, PyString_Check does PyBytes_Check
std::string s(PyString_AsString($input));
$1 = openstudio::toPath(s);
} else {
Expand All @@ -317,7 +322,7 @@ namespace openstudio {
}

%typemap(typecheck, precedence=SWIG_TYPECHECK_STRING) (path) {
bool stringType = PyString_Check($input);
bool stringType = PyString_Check($input) || PyUnicode_Check($input);
bool pathType = false;
if (!stringType){
void *vptr = 0;
Expand Down Expand Up @@ -347,7 +352,12 @@ namespace openstudio {
}else{
SWIG_exception_fail(SWIG_ValueError, "Invalid null reference openstudio::path const &");
}
} else if(PyUnicode_Check($input)) {
// Python 3
std::string s(PyUnicode_AsUTF8($input));
$1 = new openstudio::path(openstudio::toPath(s));
} else if(PyString_Check($input)) {
// Python2, PyString_Check does PyBytes_Check
std::string s(PyString_AsString($input));
$1 = new openstudio::path(openstudio::toPath(s));
} else {
Expand All @@ -356,7 +366,7 @@ namespace openstudio {
}

%typemap(typecheck, precedence=SWIG_TYPECHECK_STRING) (const path&) {
bool stringType = PyString_Check($input);
bool stringType = PyString_Check($input) || PyUnicode_Check($input);
bool pathType = false;
if (!stringType){
void *vptr = 0;
Expand Down

0 comments on commit dde0966

Please # to comment.