From: Ben Darnell Date: Thu, 28 Mar 2013 02:55:04 +0000 (-0400) Subject: Fix options.parse_config_file on Python 3. X-Git-Tag: v3.0.0~7 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=dd388bf18453a6e1e0ece5adfd93af83e7e3d971;p=thirdparty%2Ftornado.git Fix options.parse_config_file on Python 3. Add a test for this function. Closes #702. --- diff --git a/MANIFEST.in b/MANIFEST.in index 6e817251f..73848e386 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -1,12 +1,13 @@ recursive-include demos *.py *.yaml *.html *.css *.js *.xml *.sql README include tornado/ca-certificates.crt include tornado/test/README -include tornado/test/test.crt -include tornado/test/test.key include tornado/test/csv_translations/fr_FR.csv include tornado/test/gettext_translations/fr_FR/LC_MESSAGES/tornado_test.mo include tornado/test/gettext_translations/fr_FR/LC_MESSAGES/tornado_test.po +include tornado/test/options_test.cfg include tornado/test/static/robots.txt include tornado/test/templates/utf8.html +include tornado/test/test.crt +include tornado/test/test.key include runtests.sh global-exclude _auto2to3* \ No newline at end of file diff --git a/setup.py b/setup.py index 56b8e9c57..19741e6e1 100644 --- a/setup.py +++ b/setup.py @@ -38,13 +38,14 @@ distutils.core.setup( # in the sdist tarball) "tornado.test": [ "README", - "test.crt", - "test.key", - "static/robots.txt", - "templates/utf8.html", "csv_translations/fr_FR.csv", "gettext_translations/fr_FR/LC_MESSAGES/tornado_test.mo", "gettext_translations/fr_FR/LC_MESSAGES/tornado_test.po", + "options_test.cfg", + "static/robots.txt", + "templates/utf8.html", + "test.crt", + "test.key", ], }, author="Facebook", diff --git a/tornado/options.py b/tornado/options.py index 45c30f7f5..b96f815d2 100644 --- a/tornado/options.py +++ b/tornado/options.py @@ -69,7 +69,7 @@ import textwrap from tornado.escape import _unicode from tornado.log import define_logging_options from tornado import stack_context -from tornado.util import basestring_type +from tornado.util import basestring_type, exec_in class Error(Exception): @@ -211,7 +211,8 @@ class OptionParser(object): from multiple sources. """ config = {} - execfile(path, config, config) + with open(path) as f: + exec_in(f.read(), config, config) for name in config: if name in self._options: self._options[name].set(config[name]) diff --git a/tornado/test/options_test.cfg b/tornado/test/options_test.cfg new file mode 100644 index 000000000..172714616 --- /dev/null +++ b/tornado/test/options_test.cfg @@ -0,0 +1,2 @@ +port=443 +port=443 \ No newline at end of file diff --git a/tornado/test/options_test.py b/tornado/test/options_test.py index 9ec37ffb5..ba34495d9 100644 --- a/tornado/test/options_test.py +++ b/tornado/test/options_test.py @@ -1,5 +1,6 @@ from __future__ import absolute_import, division, print_function, with_statement +import os import sys from tornado.options import OptionParser, Error @@ -26,6 +27,13 @@ class OptionsTest(unittest.TestCase): options.parse_command_line(["main.py", "--port=443"]) self.assertEqual(options.port, 443) + def test_parse_config_file(self): + options = OptionParser() + options.define("port", default=80) + options.parse_config_file(os.path.join(os.path.dirname(__file__), + "options_test.cfg")) + self.assertEquals(options.port, 443) + def test_parse_callbacks(self): options = OptionParser() self.called = False