D3447: rust: make exit handling consistent with `hg`

indygreg (Gregory Szorc) phabricator at mercurial-scm.org
Sun May 6 00:11:24 EDT 2018


indygreg created this revision.
Herald added subscribers: mercurial-devel, kevincox, durin42.
Herald added a reviewer: hg-reviewers.

REVISION SUMMARY
  Now that dispatch.run() handles special exit cases and always returns
  an exit code, Rust's (formerly lacking) invocation of dispatch.run() can
  handle the return value with minimal hassle.
  
  In addition, we change Rust to exit 1 instead of 255 in the case
  of unhandled errors, as that is actually what Python does.
  
  This fixes a few test failures when running the test suite with rhg.

REPOSITORY
  rHG Mercurial

REVISION DETAIL
  https://phab.mercurial-scm.org/D3447

AFFECTED FILES
  rust/hgcli/src/main.rs

CHANGE DETAILS

diff --git a/rust/hgcli/src/main.rs b/rust/hgcli/src/main.rs
--- a/rust/hgcli/src/main.rs
+++ b/rust/hgcli/src/main.rs
@@ -9,7 +9,7 @@
 extern crate cpython;
 extern crate python27_sys;
 
-use cpython::{NoArgs, ObjectProtocol, PyModule, PyResult, Python};
+use cpython::{NoArgs, ObjectProtocol, PyModule, PyObject, PyResult, Python};
 use libc::{c_char, c_int};
 
 use std::env;
@@ -190,13 +190,21 @@
         // Investigate if we can intercept sys.exit() or SystemExit() to
         // ensure we handle process exit.
         result = match run_py(&env, py) {
-            // Print unhandled exceptions and exit code 255, as this is what
+            // Print unhandled exceptions and exit code 1, as this is what
             // `python` does.
             Err(err) => {
                 err.print(py);
-                Err(255)
+                Err(1)
             }
-            Ok(()) => Ok(()),
+            Ok(value) => {
+                // dispatch.run() should return an integer exit code
+                // between 0 and 255, inclusive.
+                let code = match value.extract::<i32>(py) {
+                    Ok(value) => value,
+                    Err(_) => panic!("non-integer value returned by dispatch.run()"),
+                };
+                Err(code)
+            }
         };
     }
 
@@ -207,7 +215,7 @@
     result
 }
 
-fn run_py(env: &Environment, py: Python) -> PyResult<()> {
+fn run_py(env: &Environment, py: Python) -> PyResult<PyObject> {
     let sys_mod = py.import("sys").unwrap();
 
     update_encoding(py, &sys_mod);
@@ -218,9 +226,7 @@
     demand_mod.call(py, "enable", NoArgs, None)?;
 
     let dispatch_mod = py.import("mercurial.dispatch")?;
-    dispatch_mod.call(py, "run", NoArgs, None)?;
-
-    Ok(())
+    dispatch_mod.call(py, "run", NoArgs, None)
 }
 
 fn main() {



To: indygreg, #hg-reviewers
Cc: durin42, kevincox, mercurial-devel


More information about the Mercurial-devel mailing list