From a029d36e342e9915667d2decf660664363259d4c Mon Sep 17 00:00:00 2001 From: jiangcheng Date: Tue, 22 Jun 2021 14:48:48 +0800 Subject: [PATCH] [Cherry-pick] solve ANSI escape sequences print error in cmd and powershell (#33689) (#33715) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 解决windows cmd和powershell显示乱码的问题 --- python/paddle/fluid/dygraph/varbase_patch_methods.py | 4 ++++ python/paddle/utils/deprecated.py | 5 +++++ 2 files changed, 9 insertions(+) diff --git a/python/paddle/fluid/dygraph/varbase_patch_methods.py b/python/paddle/fluid/dygraph/varbase_patch_methods.py index 644e25ab9183b..17cd499bfee5f 100644 --- a/python/paddle/fluid/dygraph/varbase_patch_methods.py +++ b/python/paddle/fluid/dygraph/varbase_patch_methods.py @@ -16,6 +16,7 @@ import numpy as np import warnings import weakref +import sys import paddle from .. import framework @@ -372,6 +373,9 @@ def grad(self): """ msg = "tensor.grad will return the tensor value of the gradient." warning_msg = "\033[93m\nWarning:\n%s \033[0m" % (msg) + # ensure ANSI escape sequences print correctly in cmd and powershell + if sys.platform.lower() == 'win32': + warning_msg = "\nWarning:\n%s " % (msg) warnings.warn(warning_msg) return self._grad_ivar() diff --git a/python/paddle/utils/deprecated.py b/python/paddle/utils/deprecated.py index e3839d9767d21..b17bd70c91af2 100755 --- a/python/paddle/utils/deprecated.py +++ b/python/paddle/utils/deprecated.py @@ -18,6 +18,7 @@ import warnings import functools import paddle +import sys __all__ = [] @@ -99,6 +100,10 @@ def wrapper(*args, **kwargs): func.__module__, func.__name__)) warningmsg = "\033[93m\nWarning:\n%s \033[0m" % (msg) + # ensure ANSI escape sequences print correctly in cmd and powershell + if sys.platform.lower() == 'win32': + warningmsg = "\nWarning:\n%s " % (msg) + v_current = [int(i) for i in paddle.__version__.split(".")] v_current += [0] * (4 - len(v_current)) v_since = [int(i) for i in _since.split(".")]