Commits

Takayuki Shimizukawa committed 1e780c3

conf.py now accept CRLF end-of-line.

Comments (0)

Files changed (4)

 
 from sphinx.errors import ConfigError
 from sphinx.locale import l_
-from sphinx.util.osutil import make_filename, fs_encoding
-from sphinx.util.pycompat import bytes, b, convert_with_2to3
+from sphinx.util.osutil import make_filename
+from sphinx.util.pycompat import bytes, b, execfile_
 
 nonascii_re = re.compile(b(r'[\x80-\xff]'))
 
                 # we promise to have the config dir as current dir while the
                 # config file is executed
                 os.chdir(dirname)
-                # get config source -- 'b' is a no-op under 2.x, while 'U' is
-                # ignored under 3.x (but 3.x compile() accepts \r\n newlines)
-                f = open(filename, 'rbU')
                 try:
-                    source = f.read()
-                finally:
-                    f.close()
-                try:
-                    # compile to a code object, handle syntax errors
-                    config_file_enc = config_file.encode(fs_encoding)
-                    try:
-                        code = compile(source, config_file_enc, 'exec')
-                    except SyntaxError:
-                        if convert_with_2to3:
-                            # maybe the file uses 2.x syntax; try to refactor to
-                            # 3.x syntax using 2to3
-                            source = convert_with_2to3(config_file)
-                            code = compile(source, config_file_enc, 'exec')
-                        else:
-                            raise
-                    exec code in config
+                    execfile_(filename, config)
                 except SyntaxError, err:
                     raise ConfigError(CONFIG_SYNTAX_ERROR % err)
             finally:

sphinx/util/pycompat.py

         return s.encode('ascii', 'backslashreplace')
 
 
+def execfile_(filepath, _globals):
+    from sphinx.util.osutil import fs_encoding
+    # get config source -- 'b' is a no-op under 2.x, while 'U' is
+    # ignored under 3.x (but 3.x compile() accepts \r\n newlines)
+    f = open(filepath, 'rbU')
+    try:
+        source = f.read()
+    finally:
+        f.close()
+
+    # py25,py26,py31 accept only LF eol instead of CRLF
+    if sys.version_info[:2] in ((2, 5), (2, 6), (3, 1)):
+        source = source.replace(b('\r\n'), b('\n'))
+
+    # compile to a code object, handle syntax errors
+    filepath_enc = filepath.encode(fs_encoding)
+    try:
+        code = compile(source, filepath_enc, 'exec')
+    except SyntaxError:
+        if convert_with_2to3:
+            # maybe the file uses 2.x syntax; try to refactor to
+            # 3.x syntax using 2to3
+            source = convert_with_2to3(filepath)
+            code = compile(source, filepath_enc, 'exec')
+        else:
+            raise
+    exec code in _globals
+
+
 try:
     from html import escape as htmlescape
 except ImportError:

tests/test_config.py

 import sphinx
 from sphinx.config import Config
 from sphinx.errors import ExtensionError, ConfigError, VersionRequirementError
+from sphinx.util.pycompat import b
 
 
 @with_app(confoverrides={'master_doc': 'master', 'nonexisting_value': 'True',
 def test_needs_sphinx():
     raises(VersionRequirementError, TestApp,
            confoverrides={'needs_sphinx': '9.9'})
+
+
+@with_tempdir
+def test_config_eol(tmpdir):
+    # test config file's eol patterns: LF, CRLF
+    configfile = tmpdir / 'conf.py'
+    for eol in ('\n', '\r\n'):
+        configfile.write_bytes(b('project = "spam"' + eol))
+        cfg = Config(tmpdir, 'conf.py', {}, None)
+        cfg.init_values()
+        assert cfg.project == u'spam'

tests/test_quickstart.py

 
 from sphinx import quickstart as qs
 from sphinx.util.console import nocolor, coloron
+from sphinx.util.pycompat import execfile_
 
 def setup_module():
     nocolor()
     conffile = tempdir / 'conf.py'
     assert conffile.isfile()
     ns = {}
-    f = open(conffile, 'rbU')
-    try:
-        code = compile(f.read(), conffile, 'exec')
-    finally:
-        f.close()
-    exec code in ns
+    execfile_(conffile, ns)
     assert ns['extensions'] == []
     assert ns['templates_path'] == ['_templates']
     assert ns['source_suffix'] == '.rst'
     conffile = tempdir / 'source' / 'conf.py'
     assert conffile.isfile()
     ns = {}
-    f = open(conffile, 'rbU')
-    try:
-        code = compile(f.read(), conffile, 'exec')
-    finally:
-        f.close()
-    exec code in ns
+    execfile_(conffile, ns)
     assert ns['extensions'] == ['sphinx.ext.autodoc', 'sphinx.ext.doctest']
     assert ns['templates_path'] == ['.templates']
     assert ns['source_suffix'] == '.txt'