Commit 418baf91 authored by Nick Coghlan's avatar Nick Coghlan

Issue #8202: Set sys.argv[0] to -m rather than -c while searching for the...

Issue #8202: Set sys.argv[0] to -m rather than -c while searching for the module to execute. Also updates all the cmd_line_script tests to validate the setting of sys.path[0] and the current working directory
parent 8cb4ad9d
......@@ -95,8 +95,9 @@ source.
file is not available.
If this option is given, the first element of :data:`sys.argv` will be the
full path to the module file. As with the :option:`-c` option, the current
directory will be added to the start of :data:`sys.path`.
full path to the module file (while the module file is being located, the
first element will be set to ``"-m"``). As with the :option:`-c` option,
the current directory will be added to the start of :data:`sys.path`.
Many standard library modules contain code that is invoked on their execution
as a script. An example is the :mod:`timeit` module::
......
......@@ -86,9 +86,9 @@ def make_zip_script(zip_dir, zip_basename, script_name, name_in_zip=None):
# zip_file.close()
return zip_name, os.path.join(zip_name, name_in_zip)
def make_pkg(pkg_dir):
def make_pkg(pkg_dir, init_source=''):
os.mkdir(pkg_dir)
make_script(pkg_dir, '__init__', '')
make_script(pkg_dir, '__init__', init_source)
def make_zip_pkg(zip_dir, zip_basename, pkg_name, script_basename,
source, depth=1, compiled=False):
......
......@@ -419,21 +419,32 @@ elif sys.platform != 'darwin':
SAVEDCWD = os.getcwd()
@contextlib.contextmanager
def temp_cwd(name='tempcwd', quiet=False):
def temp_cwd(name='tempcwd', quiet=False, path=None):
"""
Context manager that creates a temporary directory and set it as CWD.
Context manager that temporarily changes the CWD.
The new CWD is created in the current directory and it's named *name*.
If *quiet* is False (default) and it's not possible to create or change
the CWD, an error is raised. If it's True, only a warning is raised
and the original CWD is used.
An existing path may be provided as *path*, in which case this
function makes no changes to the file system.
Otherwise, the new CWD is created in the current directory and it's
named *name*. If *quiet* is False (default) and it's not possible to
create or change the CWD, an error is raised. If it's True, only a
warning is raised and the original CWD is used.
"""
saved_dir = os.getcwd()
is_temporary = False
if path is None:
path = name
try:
os.mkdir(name)
is_temporary = True
except OSError:
if not quiet:
raise
warnings.warn('tests may fail, unable to create temp CWD ' + name,
RuntimeWarning, stacklevel=3)
try:
os.mkdir(name)
os.chdir(name)
is_temporary = True
os.chdir(path)
except OSError:
if not quiet:
raise
......
# Tests invocation of the interpreter with various command line arguments
# All tests are executed with environment variables ignored
# Most tests are executed with environment variables ignored
# See test_cmd_line_script.py for testing of script execution
import test.support, unittest
......@@ -7,10 +7,6 @@ import os
import sys
from test.script_helper import spawn_python, kill_python, python_exit_code
# XXX (ncoghlan): there are assorted gratuitous inconsistencies between the
# support code in the Py3k version and the 2.x version that unnecessarily
# complicate test suite merges. See issue 7331
# spawn_python normally enforces use of -E to avoid environmental effects
# but one test checks PYTHONPATH behaviour explicitly
# XXX (ncoghlan): Give script_helper.spawn_python an option to switch
......
This diff is collapsed.
......@@ -12,6 +12,9 @@ What's New in Python 3.2 Alpha 2?
Core and Builtins
-----------------
- Issue #8202: sys.argv[0] is now set to '-m' instead of '-c' when searching
for the module file to be executed with the -m command line option.
- Issue #9599: Create PySys_FormatStdout() and PySys_FormatStderr() functions
to write a message formatted by PyUnicode_FromFormatV() to sys.stdout and
sys.stderr.
......
......@@ -604,10 +604,9 @@ Py_Main(int argc, wchar_t **argv)
}
if (module != NULL) {
/* Backup _PyOS_optind and force sys.argv[0] = '-c'
so that PySys_SetArgv correctly sets sys.path[0] to ''*/
/* Backup _PyOS_optind and force sys.argv[0] = '-m'*/
_PyOS_optind--;
argv[_PyOS_optind] = L"-c";
argv[_PyOS_optind] = L"-m";
}
PySys_SetArgv(argc-_PyOS_optind, argv+_PyOS_optind);
......
......@@ -1723,6 +1723,10 @@ _wrealpath(const wchar_t *path, wchar_t *resolved_path)
}
#endif
#define _HAVE_SCRIPT_ARGUMENT(argc, argv) \
(argc > 0 && argv0 != NULL && \
wcscmp(argv0, L"-c") != 0 && wcscmp(argv0, L"-m") != 0)
void
PySys_SetArgvEx(int argc, wchar_t **argv, int updatepath)
{
......@@ -1747,7 +1751,7 @@ PySys_SetArgvEx(int argc, wchar_t **argv, int updatepath)
wchar_t link[MAXPATHLEN+1];
wchar_t argv0copy[2*MAXPATHLEN+1];
int nr = 0;
if (argc > 0 && argv0 != NULL && wcscmp(argv0, L"-c") != 0)
if (_HAVE_SCRIPT_ARGUMENT(argc, argv))
nr = _Py_wreadlink(argv0, link, MAXPATHLEN);
if (nr > 0) {
/* It's a symlink */
......@@ -1772,7 +1776,7 @@ PySys_SetArgvEx(int argc, wchar_t **argv, int updatepath)
}
#endif /* HAVE_READLINK */
#if SEP == '\\' /* Special case for MS filename syntax */
if (argc > 0 && argv0 != NULL && wcscmp(argv0, L"-c") != 0) {
if (_HAVE_SCRIPT_ARGUMENT(argc, argv)) {
wchar_t *q;
#if defined(MS_WINDOWS) && !defined(MS_WINCE)
/* This code here replaces the first element in argv with the full
......@@ -1798,7 +1802,7 @@ PySys_SetArgvEx(int argc, wchar_t **argv, int updatepath)
}
}
#else /* All other filename syntaxes */
if (argc > 0 && argv0 != NULL && wcscmp(argv0, L"-c") != 0) {
if (_HAVE_SCRIPT_ARGUMENT(argc, argv)) {
#if defined(HAVE_REALPATH)
if (_wrealpath(argv0, fullpath)) {
argv0 = fullpath;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment