Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ensure ANSI escape sequences print correctly in cmd and powershell #33656

Closed

Conversation

thisjiang
Copy link
Contributor

@thisjiang thisjiang commented Jun 18, 2021

PR types

Bug fixes

PR changes

APIs

Describe

os.system('')版本

参考how-to-print-colored-text-to-the-terminal

\033[开头为ANSI转义代码(ANSI escape sequences),在windows cmd和powershell上直接print('\033[93m\nWarning:\n%s \033[0m' % ('Test'))会乱码,通过以下代码可解决该问题:

import sys, os
if sys.platform.lower() == 'win32':
    os.system('')

Q:why does os.system("") cause color codes to work?
A:It's not python's implementation, in C running printf(fmt, ...); with ASNI codes in windows after calling system(""); (include <stdlib.h>) does prints the color text

bj-4a8251c287aef586061d9dc5ca595cd9a81dafdf
bj-72216ed6242790ae2208810f832f576a5fbfb6dc
bj-cd74edf9a85f4cfc47aabe8c16c75a5fc9f1c426

subprocess.call('', shell=True)版本

os.system('')版本下PR-CI-Window一直挂,原因是Timeout。查找时发现可能是因为windows下在python中调用os.system()执行系统命令时的弹出框,为解决该问题同时兼容python2和python3,使用subprocess.call替换os.system('')

0621anaconda
0621cmd
0621powershell

win32下不显示颜色

subprocess.call版本在PR-CI-CPU-Py2下也直接挂,原因是subprocess.call调用将被废弃,但最新的subprocess.run只支持python3.5+版本,因此权衡之下干脆对于cmd和powershell不显示颜色。

@paddle-bot-old
Copy link

Thanks for your contribution!
Please wait for the result of CI firstly. See Paddle CI Manual for details.

zhhsplendid
zhhsplendid previously approved these changes Jun 18, 2021
Copy link
Member

@zhhsplendid zhhsplendid left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants