utf8: use cpython_ext::Str for error strings

Summary:
Error strings were being converted to unicode if they contained certain
characters. This caused python 2 Mercurial to throw various errors when it tried
to turn them into strings to report errors.

Let's return cpython_ext::Str instead of String.

Reviewed By: sfilipco

Differential Revision: D20012188

fbshipit-source-id: af6fa7d98d68e3c188292e4972cfc1bdb758dbdf
This commit is contained in:
Durham Goode 2020-02-21 08:01:22 -08:00 committed by Facebook Github Bot
parent 8086dc29c7
commit 78ada1f780
2 changed files with 25 additions and 7 deletions

View File

@ -36,13 +36,25 @@ pub fn init_module(py: Python, package: &str) -> PyResult<PyModule> {
fn register_error_handlers() {
fn specific_error_handler(py: Python, e: &error::Error) -> Option<PyErr> {
if let Some(e) = e.downcast_ref::<indexedlog::Error>() {
Some(PyErr::new::<IndexedLogError, _>(py, e.to_string()))
Some(PyErr::new::<IndexedLogError, _>(
py,
cpython_ext::Str::from(e.to_string()),
))
} else if let Some(e) = e.downcast_ref::<metalog::Error>() {
Some(PyErr::new::<MetaLogError, _>(py, e.to_string()))
Some(PyErr::new::<MetaLogError, _>(
py,
cpython_ext::Str::from(e.to_string()),
))
} else if let Some(e) = e.downcast_ref::<revisionstore::Error>() {
Some(PyErr::new::<RevisionstoreError, _>(py, e.to_string()))
Some(PyErr::new::<RevisionstoreError, _>(
py,
cpython_ext::Str::from(e.to_string()),
))
} else if let Some(e) = e.downcast_ref::<cpython_ext::Error>() {
Some(PyErr::new::<NonUTF8Path, _>(py, e.to_string()))
Some(PyErr::new::<NonUTF8Path, _>(
py,
cpython_ext::Str::from(e.to_string()),
))
} else {
None
}

View File

@ -1,3 +1,4 @@
# coding=utf-8
# Copyright (c) Facebook, Inc. and its affiliates.
#
# This software may be used and distributed according to the terms of the
@ -16,10 +17,15 @@ feature.require(["no-windows", "no-osx"])
# Test that trying to add invalid utf8 files to the repository will fail.
sh % "hg init"
open("invalid\x80utf8", "w").write("test")
open("\x9d\xc8\xac\xde\xa1\xee", "w").write("test")
sh % "hg status" == r"""
abort: "\x9DȬޡ\xEE" is not a valid UTF-8 path
[255]"""
sh % "hg addremove" == r"""
abort: "invalid\x80utf8" is not a valid UTF-8 path
abort: "\x9DȬޡ\xEE" is not a valid UTF-8 path
[255]"""
sh % "hg commit -m 'adding a filename that is invalid utf8'" == r"""
abort: "invalid\x80utf8" is not a valid UTF-8 path
abort: "\x9DȬޡ\xEE" is not a valid UTF-8 path
[255]"""