Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 378e37d

Browse files
committed
Replace use of str() with six.text_type() for Py2&3 compatibility
Version 1 of the PyQt APIs return QVariant types that must be cast to Python strings before use. To catch this `get_window_title()` in `backend_qt5.py` wrapped the call to `self.window.windowTitle()` with `str()`. This fails on Python 2 if the window title contains unicode characters. This is replaced with `six.text_type` for Py2&3 compatibility with unicode types. A number of other locations in the same file were also using `str()` for potentially unicode data. These are also fixed in this commit. Fixes #4275.
1 parent 174fc94 commit 378e37d

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

lib/matplotlib/backends/backend_qt5.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ def _create_qApp():
137137
if display is None or not re.search(':\d', display):
138138
raise RuntimeError('Invalid DISPLAY variable')
139139

140-
qApp = QtWidgets.QApplication([str(" ")])
140+
qApp = QtWidgets.QApplication([six.text_type(" ")])
141141
qApp.lastWindowClosed.connect(qApp.quit)
142142
else:
143143
qApp = app
@@ -560,7 +560,7 @@ def destroy(self, *args):
560560
self.window.close()
561561

562562
def get_window_title(self):
563-
return str(self.window.windowTitle())
563+
return six.text_type(self.window.windowTitle())
564564

565565
def set_window_title(self, title):
566566
self.window.setWindowTitle(title)
@@ -733,7 +733,7 @@ def save_figure(self, *args):
733733
self.canvas.print_figure(six.text_type(fname))
734734
except Exception as e:
735735
QtWidgets.QMessageBox.critical(
736-
self, "Error saving file", str(e),
736+
self, "Error saving file", six.text_type(e),
737737
QtWidgets.QMessageBox.Ok, QtWidgets.QMessageBox.NoButton)
738738

739739

@@ -849,7 +849,7 @@ def exception_handler(type, value, tb):
849849
if hasattr(value, 'strerror') and value.strerror is not None:
850850
msg += value.strerror
851851
else:
852-
msg += str(value)
852+
msg += six.text_type(value)
853853

854854
if len(msg):
855855
error_msg_qt(msg)

0 commit comments

Comments
 (0)