Repository: ESMCI/cime

Issue:
acme.bldlog isn't logging the build when DEBUG=True
If I set DEBUG=True, then any error messages that arise in building acme disappear. 

Reproducable Example (on anlworkstation)
I introduced a fake subroutine into src/drivers/mct/main/cplcomp_exchange_mod.F90
Running without DEBUG I get the expected results:
```
./create_newcase --case TEST_X --res ne30_g16 --compset X --handle-preexisting-dirs r
cd TEST_X
./case.setup
./case.build
...
Building acme with output to /homes/sarich/acme/scratch/TEST_X/bld/acme.bldlog.171122-110524 
atm_comp_mct.F90:(.text+0x39): undefined reference to `fake_subroutine_'

Component acme exe build complete with 3 warnings
ERROR: BUILD FAIL: buildexe failed, cat /homes/sarich/acme/scratch/TEST_X/bld/acme.bldlog.171122-110524
```
(and acme.bldlog.17112-110524 has a nice listing of the build and the error)

However, if I'm using DEBUG I get no indication of what went wrong:
```
./create_newcase --case TEST_X_DEBUG --res ne30_g16 --compset X --handle-preexisting-dirs r
cd TEST_X_DEBUG
./xmlchange DEBUG=True
./case.setup
./case.build
...
Building acme with output to /homes/sarich/acme/scratch/TEST_X_DEBUG/bld/acme.bldlog.171122-112042 
ERROR: BUILD FAIL: buildexe failed, cat /homes/sarich/acme/scratch/TEST_X_DEBUG/bld/acme.bldlog.171122-112042
```

cat /homes/sarich/acme/scratch/TEST_X_DEBUG/bld/acme.bldlog.171122-112042
`Building a single executable version of target coupled model
`



Discussion:
using DEBUG turns on compiler warnings, gfortran 6.2.0 on anlworkstation is printing out
a bunch of these warnings:
```

/sandbox/sarich/cime/src/drivers/mct/main/cime_comp_mod.F90:471:26:

    integer  :: ka,km,k1,k2,k3         ! aVect field indices
                          1
Warning: Unused PRIVATE module variable ‘k2’ declared at (1) [-Wunused-value]
/sandbox/sarich/cime/src/drivers/mct/main/cime_comp_mod.F90:471:29:

    integer  :: ka,km,k1,k2,k3         ! aVect field indices
                             1
Warning: Unused PRIVATE module variable ‘k3’ declared at (1) [-Wunused-value]
/sandbox/sarich/cime/src/drivers/mct/main/cime_comp_mod.F90:471:17:

    integer  :: ka,km,k1,k2,k3         ! aVect field indices
                 1
Warning: Unused PRIVATE module variable ‘ka’ declared at (1) [-Wunused-value]
/sandbox/sarich/cime/src/drivers/mct/main/cime_comp_mod.F90:471:26:

    integer  :: ka,km,k1,k2,k3         ! aVect field indices
                          1
Warning: Unused PRIVATE module variable ‘k2’ declared at (1) [-Wunused-value]
/sandbox/sarich/cime/src/drivers/mct/main/cime_comp_mod.F90:471:29:

    integer  :: ka,km,k1,k2,k3         ! aVect field indices
                             1
Warning: Unused PRIVATE module variable ‘k3’ declared at (1) [-Wunused-value]
/sandbox/sarich/cime/src/drivers/mct/main/cime_comp_mod.F90:471:17:

    integer  :: ka,km,k1,k2,k3         ! aVect field indices
                 1
```
The backticks are unicode characters and somehow causing the error messages to get lost.

Resolving patch:
diff --git a/scripts/lib/CIME/utils.py b/scripts/lib/CIME/utils.py
--- a/scripts/lib/CIME/utils.py
+++ b/scripts/lib/CIME/utils.py
@@ -74,7 +74,12 @@ def expect(condition, error_msg, exc_type=SystemExit, error_prefix="ERROR:"):
         if logger.isEnabledFor(logging.DEBUG):
             import pdb
             pdb.set_trace()
-        raise exc_type(error_prefix + " " + error_msg)
+        try:
+            msg = str(error_prefix + " " + error_msg)
+        except UnicodeEncodeError:
+            msg = (error_prefix + " " + error_msg).encode('utf-8')
+        raise exc_type(msg)
+
 
 def id_generator(size=6, chars=string.ascii_lowercase + string.digits):
     return ''.join(random.choice(chars) for _ in range(size))


Test patch: