]> granicus.if.org Git - python/commitdiff
- Issue #2385: distutils.core.run_script() makes __file__ available, so the
authorFred Drake <fdrake@acm.org>
Fri, 4 Apr 2008 05:41:30 +0000 (05:41 +0000)
committerFred Drake <fdrake@acm.org>
Fri, 4 Apr 2008 05:41:30 +0000 (05:41 +0000)
  controlled environment will more closely mirror the typical script
  environment.  This supports setup.py scripts that refer to data files.

Lib/distutils/core.py
Lib/distutils/tests/test_core.py [new file with mode: 0644]
Misc/NEWS

index c40dd0a7aa37e81bef2ae438587eaf5323f0f281..640d6b5955010c03a6b9cead424e1d5d8b6b9bdc 100644 (file)
@@ -210,8 +210,9 @@ def run_setup (script_name, script_args=None, stop_after="run"):
     _setup_stop_after = stop_after
 
     save_argv = sys.argv
-    g = {}
+    g = {'__file__': script_name}
     l = {}
+    os.chdir(os.path.dirname(script_name) or os.curdir)
     try:
         try:
             sys.argv[0] = script_name
diff --git a/Lib/distutils/tests/test_core.py b/Lib/distutils/tests/test_core.py
new file mode 100644 (file)
index 0000000..6de58d9
--- /dev/null
@@ -0,0 +1,40 @@
+"""Tests for distutils.core."""
+
+import StringIO
+import distutils.core
+import os
+import test.test_support
+import unittest
+
+
+# setup script that uses __file__
+setup_using___file__ = """\
+
+__file__
+
+from distutils.core import setup
+setup()
+"""
+
+
+class CoreTestCase(unittest.TestCase):
+
+    def tearDown(self):
+        os.remove(test.test_support.TESTFN)
+
+    def write_setup(self, text):
+        return fn
+
+    def test_run_setup_provides_file(self):
+        # Make sure the script can use __file__; if that's missing, the test
+        # setup.py script will raise NameError.
+        fn = test.test_support.TESTFN
+        open(fn, "w").write(setup_using___file__)
+        distutils.core.run_setup(fn)
+
+
+def test_suite():
+    return unittest.makeSuite(CoreTestCase)
+
+if __name__ == "__main__":
+    unittest.main(defaultTest="test_suite")
index 8742cb449a72b836ac7146cc21f3c8c66692bf16..c07f546b9128bda946e0391899fb3eb3b9d476de 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -18,6 +18,10 @@ Extensions Modules
 Library
 -------
 
+- Issue #2385: distutils.core.run_script() makes __file__ available, so the
+  controlled environment will more closely mirror the typical script
+  environment.  This supports setup.py scripts that refer to data files.
+
 Tests
 -----