Skip to content

Unable to print special characters to console output #17994

@avanigupta

Description

@avanigupta

Describe the bug

I am from the Azure App Configuration team and I work on the az appconfig module. I noticed that when our output contains characters like or 𐐷 , CLI outputs a question mark character instead: .
Moreover, if we try to print a warning message containing one of these characters, we get an exception. Although this exception doesn't block the operation, it still pollutes the output and cannot be suppressed even if we supply --only-show-errors argument.

This is how we print a warning:

from knack.log import get_logger
logger = get_logger(__name__)
logger.warning(json.dumps(record, ensure_ascii=False))

Errors:
This is the exception:

--- Logging error ---
Traceback (most recent call last):
  File "C:\Python38\lib\logging\__init__.py", line 1084, in emit
    stream.write(msg + self.terminator)
  File "C:\Python38\lib\encodings\cp1252.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\u6c49' in position 127: character maps to <undefined>
Call stack:
  File "C:\Python38\lib\runpy.py", line 194, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Python38\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "d:\code\appconfig\azure-cli\src\azure-cli\azure\cli\__main__.py", line 48, in <module>
    exit_code = cli_main(az_cli, sys.argv[1:])
  File "d:\code\appconfig\azure-cli\src\azure-cli\azure\cli\__main__.py", line 35, in cli_main
    return cli.invoke(args)
  File "D:\Code\AppConfig\azure-cli\env\lib\site-packages\knack\cli.py", line 231, in invoke
    cmd_result = self.invocation.execute(args)
  File "d:\code\appconfig\azure-cli\src\azure-cli-core\azure\cli\core\commands\__init__.py", line 650, in execute
    results, exceptions = self._run_jobs_serially(jobs, ids)
  File "d:\code\appconfig\azure-cli\src\azure-cli-core\azure\cli\core\commands\__init__.py", line 720, in _run_jobs_serially
    results.append(self._run_job(expanded_arg, cmd_copy))
  File "d:\code\appconfig\azure-cli\src\azure-cli-core\azure\cli\core\commands\__init__.py", line 691, in _run_job
    result = cmd_copy(params)
  File "d:\code\appconfig\azure-cli\src\azure-cli-core\azure\cli\core\commands\__init__.py", line 328, in __call__
    return self.handler(*args, **kwargs)
  File "d:\code\appconfig\azure-cli\src\azure-cli-core\azure\cli\core\commands\command_operation.py", line 121, in handler
    return op(**command_args)
  File "d:\code\appconfig\azure-cli\src\azure-cli\azure\cli\command_modules\appconfig\keyvalue.py", line 139, in import_config
    need_kv_change = __print_preview(
  File "d:\code\appconfig\azure-cli\src\azure-cli\azure\cli\command_modules\appconfig\_kv_helpers.py", line 639, in __print_preview
    logger.warning(json.dumps(record, ensure_ascii=False))
Message: '{"key": "Chinese", "value": "�"}'
Arguments: ()

Do you want to continue?
 (y/n): y

To Reproduce:

Expected Behavior

Environment Summary

Windows-10-10.0.19041-SP0
Python 3.6.8
Installer: MSI

azure-cli 2.21.0 *

Extensions:
azure-devops 0.18.0

Additional Context

Metadata

Metadata

Assignees

Labels

CoreCLI core infrastructure

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions