From: Ben Darnell Date: Sun, 3 Apr 2016 20:46:03 +0000 (-0400) Subject: options: add __file__ to config file parsing X-Git-Tag: v4.4.0b1~37 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=50f9eedd89f26fc86974461b6ddd84343504be93;p=thirdparty%2Ftornado.git options: add __file__ to config file parsing Fixes #1666 --- diff --git a/tornado/options.py b/tornado/options.py index 40169fb8a..84e170bbd 100644 --- a/tornado/options.py +++ b/tornado/options.py @@ -308,8 +308,12 @@ class OptionParser(object): .. versionchanged:: 4.1 Config files are now always interpreted as utf-8 instead of the system default encoding. + + .. versionchanged:: 4.4 + The special variable ``__file__`` is available inside config + files, specifying the absolute path to the config file itself. """ - config = {} + config = {'__file__': os.path.abspath(path)} with open(path, 'rb') as f: exec_in(native_str(f.read()), config, config) for name in config: diff --git a/tornado/test/options_test.cfg b/tornado/test/options_test.cfg index cbac89247..4ead46a49 100644 --- a/tornado/test/options_test.cfg +++ b/tornado/test/options_test.cfg @@ -3,3 +3,5 @@ port=443 username='李康' foo_bar='a' + +my_path = __file__ diff --git a/tornado/test/options_test.py b/tornado/test/options_test.py index 2f2384b24..af0fc1170 100644 --- a/tornado/test/options_test.py +++ b/tornado/test/options_test.py @@ -34,10 +34,13 @@ class OptionsTest(unittest.TestCase): options = OptionParser() options.define("port", default=80) options.define("username", default='foo') - options.parse_config_file(os.path.join(os.path.dirname(__file__), - "options_test.cfg")) - self.assertEquals(options.port, 443) + options.define("my_path") + config_path = os.path.join(os.path.dirname(__file__), + "options_test.cfg") + options.parse_config_file(config_path) + self.assertEqual(options.port, 443) self.assertEqual(options.username, "李康") + self.assertEqual(options.my_path, config_path) def test_parse_callbacks(self): options = OptionParser()