]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
options: add __file__ to config file parsing
authorBen Darnell <ben@bendarnell.com>
Sun, 3 Apr 2016 20:46:03 +0000 (16:46 -0400)
committerBen Darnell <ben@bendarnell.com>
Sun, 3 Apr 2016 20:46:03 +0000 (16:46 -0400)
Fixes #1666

tornado/options.py
tornado/test/options_test.cfg
tornado/test/options_test.py

index 40169fb8a9b0c522b6a29a423abbcd8318c57d72..84e170bbda30344cde3ea22361999b9f1f6f4c3a 100644 (file)
@@ -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:
index cbac8924717124d7a742cbfe5f92b2ed25339e62..4ead46a49a44ba4b51c60649343c1e8fd6e429c0 100644 (file)
@@ -3,3 +3,5 @@ port=443
 username='李康'
 
 foo_bar='a'
+
+my_path = __file__
index 2f2384b2480b3bf83584db83e68ef0453d729e94..af0fc11701ee2299769a350f8c541852bc9b93c9 100644 (file)
@@ -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()