Skip to content

Commit 86ee1ac

Browse files
committed
Merge pull request astropy#58 from embray/issue-2749
Test and possible workaround for Astropy astropy#2749
2 parents f9981a7 + 8ff0c9c commit 86ee1ac

4 files changed

Lines changed: 89 additions & 24 deletions

File tree

‎CHANGES.rst‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
0.4.1 (unreleased)
88
------------------
99

10-
- Nothing changed yet.
10+
- Fixed crash that could result from users with missing/misconfigured
11+
locale settings. [#58]
1112

1213

1314
0.4 (2014-07-15)

‎ah_bootstrap.py‎

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -429,32 +429,50 @@ def _check_submodule_using_git(path):
429429
'`git submodule status` command:\n{0}'.format(str(e)))
430430

431431

432-
stdio_encoding = locale.getdefaultlocale()[1]
432+
# Can fail of the default locale is not configured properly. See
433+
# https://github.com/astropy/astropy/issues/2749. For the purposes under
434+
# consideration 'latin1' is an acceptable fallback.
435+
try:
436+
stdio_encoding = locale.getdefaultlocale()[1] or 'latin1'
437+
except ValueError:
438+
# Due to an OSX oddity locale.getdefaultlocale() can also crash
439+
# depending on the user's locale/language settings. See:
440+
# http://bugs.python.org/issue18378
441+
stdio_encoding = 'latin1'
433442

434443
if p.returncode != 0 or stderr:
435-
stderr = stderr.decode(stdio_encoding)
436444
# Unfortunately the return code alone cannot be relied on, as
437445
# earlier versions of git returned 0 even if the requested submodule
438446
# does not exist
439-
log.debug('git submodule command failed '
440-
'unexpectedly:\n{0}'.format(stderr))
441-
return False
442-
else:
443-
stdout = stdout.decode(stdio_encoding)
444-
# The stdout should only contain one line--the status of the
445-
# requested submodule
446-
m = _git_submodule_status_re.match(stdout)
447-
if m:
448-
# Yes, the path *is* a git submodule
449-
_update_submodule(m.group('submodule'), m.group('status'))
450-
return True
451-
else:
452-
log.warn(
453-
'Unexpected output from `git submodule status`:\n{0}\n'
454-
'Will attempt import from {1!r} regardless.'.format(
455-
stdout, path))
447+
stderr = stderr.decode(stdio_encoding)
448+
449+
# This is a warning that occurs in perl (from running git submodule)
450+
# which only occurs with a malformatted locale setting which can
451+
# happen sometimes on OSX. See again
452+
# https://github.com/astropy/astropy/issues/2749
453+
perl_warning = ('perl: warning: Falling back to the standard locale '
454+
'("C").')
455+
if not stderr.strip().endswith(perl_warning):
456+
# Some other uknown error condition occurred
457+
log.warn('git submodule command failed '
458+
'unexpectedly:\n{0}'.format(stderr))
456459
return False
457460

461+
stdout = stdout.decode(stdio_encoding)
462+
# The stdout should only contain one line--the status of the
463+
# requested submodule
464+
m = _git_submodule_status_re.match(stdout)
465+
if m:
466+
# Yes, the path *is* a git submodule
467+
_update_submodule(m.group('submodule'), m.group('status'))
468+
return True
469+
else:
470+
log.warn(
471+
'Unexpected output from `git submodule status`:\n{0}\n'
472+
'Will attempt import from {1!r} regardless.'.format(
473+
stdout, path))
474+
return False
475+
458476

459477
def _check_submodule_no_git(path):
460478
"""
@@ -498,9 +516,9 @@ def _check_submodule_no_git(path):
498516
try:
499517
cfg.readfp(gitmodules_fileobj)
500518
except Exception as exc:
501-
log.warning('Malformatted .gitmodules file: {0}\n'
502-
'{1} cannot be assumed to be a git submodule.'.format(
503-
exc, path))
519+
log.warn('Malformatted .gitmodules file: {0}\n'
520+
'{1} cannot be assumed to be a git submodule.'.format(
521+
exc, path))
504522
return False
505523

506524
for section in cfg.sections():

‎astropy_helpers/setup_helpers.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,17 @@
5454
import sphinx
5555
from sphinx.setup_command import BuildDoc as SphinxBuildDoc
5656
_module_state['have_sphinx'] = True
57+
except ValueError as e:
58+
# This can occur deep in the bowels of Sphinx's imports by way of docutils
59+
# and an occurence of this bug: http://bugs.python.org/issue18378
60+
# In this case sphinx is effectively unusable
61+
if 'unknown locale' in e.args[0]:
62+
log.warn(
63+
"Possible misconfiguration of one of the environment variables "
64+
"LC_ALL, LC_CTYPES, LANG, or LANGUAGE. For an example of how to "
65+
"configure your system's language environment on OSX see "
66+
"http://blog.remibergsma.com/2012/07/10/"
67+
"setting-locales-correctly-on-mac-osx-terminal-application/")
5768
except ImportError:
5869
pass
5970
except SyntaxError:

‎astropy_helpers/tests/test_ah_bootstrap.py‎

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import glob
22
import os
33
import textwrap
4+
import sys
45

56
from setuptools.package_index import PackageIndex
67
from setuptools.sandbox import run_setup
@@ -26,12 +27,14 @@
2627
ah_bootstrap.DIST_NAME = 'astropy-helpers-test'
2728
ah_bootstrap.PACKAGE_NAME = '_astropy_helpers_test_'
2829
ah_bootstrap.AUTO_UPGRADE = False
30+
ah_bootstrap.DOWNLOAD_IF_NEEDED = False
2931
try:
3032
ah_bootstrap.use_astropy_helpers({args})
3133
finally:
3234
ah_bootstrap.DIST_NAME = 'astropy-helpers'
3335
ah_bootstrap.PACKAGE_NAME = 'astropy_helpers'
3436
ah_bootstrap.AUTO_UPGRADE = True
37+
ah_bootstrap.DOWNLOAD_IF_NEEDED = True
3538
3639
import _astropy_helpers_test_
3740
filename = os.path.abspath(_astropy_helpers_test_.__file__)
@@ -91,6 +94,37 @@ def test_bootstrap_from_submodule(tmpdir, testpackage, capsys):
9194
assert a == b
9295

9396

97+
def test_bootstrap_from_submodule_no_locale(tmpdir, testpackage, capsys,
98+
monkeypatch):
99+
"""
100+
Regression test for https://github.com/astropy/astropy/issues/2749
101+
102+
Runs test_bootstrap_from_submodule but with missing locale/langauge
103+
settings.
104+
"""
105+
106+
for varname in ('LC_ALL', 'LC_CTYPE', 'LANG', 'LANGUAGE'):
107+
monkeypatch.delenv(varname, raising=False)
108+
109+
test_bootstrap_from_submodule(tmpdir, testpackage, capsys)
110+
111+
112+
def test_bootstrap_from_submodule_bad_locale(tmpdir, testpackage, capsys,
113+
monkeypatch):
114+
"""
115+
Additional regression test for
116+
https://github.com/astropy/astropy/issues/2749
117+
"""
118+
119+
for varname in ('LC_ALL', 'LC_CTYPE', 'LANG', 'LANGUAGE'):
120+
monkeypatch.delenv(varname, raising=False)
121+
122+
# Test also with bad LC_CTYPE a la http://bugs.python.org/issue18378
123+
monkeypatch.setenv('LC_CTYPE', 'UTF-8')
124+
125+
test_bootstrap_from_submodule(tmpdir, testpackage, capsys)
126+
127+
94128
def test_check_submodule_no_git(tmpdir, testpackage):
95129
"""
96130
Tests that when importing astropy_helpers from a submodule, it is still
@@ -242,7 +276,8 @@ def test_download_if_needed(tmpdir, testpackage, capsys):
242276
dist_dir = testpackage.join('dist')
243277

244278
with source.as_cwd():
245-
source.join('setup.py').write(TEST_SETUP_PY.format(args=''))
279+
source.join('setup.py').write(TEST_SETUP_PY.format(
280+
args='download_if_needed=True'))
246281
source.join('setup.cfg').write(textwrap.dedent("""\
247282
[easy_install]
248283
find_links = {find_links}

0 commit comments

Comments
 (0)