Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7d4bb9f

Browse files
committed
Add -E command line switch (ignore environment variables like PYTHONHOME
and PYTHONPATH).
1 parent f973c6d commit 7d4bb9f

12 files changed

Lines changed: 59 additions & 34 deletions

File tree

Include/pydebug.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ extern DL_IMPORT(int) Py_UseClassExceptionsFlag;
1414
extern DL_IMPORT(int) Py_FrozenFlag;
1515
extern DL_IMPORT(int) Py_TabcheckFlag;
1616
extern DL_IMPORT(int) Py_UnicodeFlag;
17+
extern DL_IMPORT(int) Py_IgnoreEnvironmentFlag;
18+
19+
/* this is a wrapper around getenv() the pays attention to
20+
Py_IgnoreEnvironmentFlag. It should be used for getting variables like
21+
PYTHONPATH and PYTHONHOME from the environment */
22+
#define Py_GETENV(s) (Py_IgnoreEnvironmentFlag ? NULL : getenv(s))
1723

1824
DL_IMPORT(void) Py_FatalError(char *message);
1925

Makefile.pre.in

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -281,12 +281,12 @@ $(PYTHON): Modules/$(MAINOBJ) $(LDLIBRARY)
281281
$(LDLIBRARY) $(LIBS) $(MODLIBS) $(SYSLIBS) $(LDLAST)
282282

283283
platform: $(PYTHON)
284-
./$(PYTHON) -c 'import sys ; from distutils.util import get_platform ; print get_platform()+"-"+sys.version[0:3]' >platform
284+
./$(PYTHON) -E -c 'import sys ; from distutils.util import get_platform ; print get_platform()+"-"+sys.version[0:3]' >platform
285285

286286

287287
# Build the shared modules
288288
sharedmods: $(PYTHON)
289-
PYTHONPATH= ./$(PYTHON) $(srcdir)/setup.py build
289+
./$(PYTHON) -E $(srcdir)/setup.py build
290290

291291
# buildno should really depend on something like LIBRARY_SRC
292292
buildno: $(PARSER_OBJS) \
@@ -466,26 +466,26 @@ $(LIBRARY_OBJS) $(MODOBJS) Modules/$(MAINOBJ): $(PYTHON_HEADERS)
466466
# Test the interpreter (twice, once without .pyc files, once with)
467467
TESTOPTS= -l
468468
TESTPROG= $(srcdir)/Lib/test/regrtest.py
469-
TESTPYTHON= ./$(PYTHON) -tt
469+
TESTPYTHON= ./$(PYTHON) -E -tt
470470
test: all platform
471471
-find $(srcdir)/Lib -name '*.py[co]' -print | xargs rm -f
472-
-PYTHONPATH= $(TESTPYTHON) $(TESTPROG) $(TESTOPTS)
473-
PYTHONPATH= $(TESTPYTHON) $(TESTPROG) $(TESTOPTS)
472+
-$(TESTPYTHON) $(TESTPROG) $(TESTOPTS)
473+
$(TESTPYTHON) $(TESTPROG) $(TESTOPTS)
474474

475475
QUICKTESTOPTS= $(TESTOPTS) -x test_thread test_signal test_strftime \
476476
test_unicodedata test_re test_sre test_select test_poll \
477477
test_linuxaudiodev test_sunaudiodev
478478
quicktest: all platform
479479
-find $(srcdir)/Lib -name '*.py[co]' -print | xargs rm -f
480-
-PYTHONPATH= $(TESTPYTHON) $(TESTPROG) $(QUICKTESTOPTS)
481-
PYTHONPATH= $(TESTPYTHON) $(TESTPROG) $(QUICKTESTOPTS)
480+
-$(TESTPYTHON) $(TESTPROG) $(QUICKTESTOPTS)
481+
$(TESTPYTHON) $(TESTPROG) $(QUICKTESTOPTS)
482482

483483
MEMTESTOPTS= $(QUICKTESTOPTS) -x test_dl test___all__ test_fork1 \
484484
test_longexp
485485
memtest: all platform
486486
-rm -f $(srcdir)/Lib/test/*.py[co]
487-
-PYTHONPATH= $(TESTPYTHON) $(TESTPROG) $(MEMTESTOPTS)
488-
PYTHONPATH= $(TESTPYTHON) $(TESTPROG) $(MEMTESTOPTS)
487+
-$(TESTPYTHON) $(TESTPROG) $(MEMTESTOPTS)
488+
$(TESTPYTHON) $(TESTPROG) $(MEMTESTOPTS)
489489

490490
# Install everything
491491
install: altinstall bininstall maninstall
@@ -708,7 +708,7 @@ libainstall: all
708708
# Install the dynamically loadable modules
709709
# This goes into $(exec_prefix)
710710
sharedinstall:
711-
PYTHONPATH= ./$(PYTHON) $(srcdir)/setup.py install \
711+
./$(PYTHON) -E $(srcdir)/setup.py install \
712712
--install-platlib=$(DESTSHARED)
713713

714714
# Build the toplevel Makefile

Misc/python.man

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ python \- an interpreted, interactive, object-oriented programming language
1616
.B \-S
1717
]
1818
[
19+
.B \-E
20+
]
21+
[
1922
.B \-t
2023
]
2124
[
@@ -99,6 +102,10 @@ and the site-dependent manipulations of
99102
.I sys.path
100103
that it entails.
101104
.TP
105+
.B \-E
106+
Ignore environment variables like PYTHONPATH and PYTHONHOME that modify
107+
the behavior of the interpreter.
108+
.TP
102109
.B \-t
103110
Issue a warning when a source file mixes tabs and spaces for
104111
indentation in a way that makes it depend on the worth of a tab

Modules/getpath.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ calculate_path(void)
365365
static char delimiter[2] = {DELIM, '\0'};
366366
static char separator[2] = {SEP, '\0'};
367367
char *pythonpath = PYTHONPATH;
368-
char *rtpypath = getenv("PYTHONPATH");
368+
char *rtpypath = Py_GETENV("PYTHONPATH");
369369
char *home = Py_GetPythonHome();
370370
char *path = getenv("PATH");
371371
char *prog = Py_GetProgramName();

Modules/main.c

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ static char **orig_argv;
2828
static int orig_argc;
2929

3030
/* command line options */
31-
#define BASE_OPTS "c:diOStuUvxXhVW:"
31+
#define BASE_OPTS "c:diOSEtuUvxXhVW:"
3232

3333
#ifndef RISCOS
3434
#define PROGRAM_OPTS BASE_OPTS
@@ -53,6 +53,7 @@ Options and arguments (and corresponding environment variables):\n\
5353
-O : optimize generated bytecode (a tad; also PYTHONOPTIMIZE=x)\n\
5454
-OO : remove doc-strings in addition to the -O optimizations\n\
5555
-S : don't imply 'import site' on initialization\n\
56+
-E : ignore environment variables (such as PYTHONPATH)\n\
5657
-t : issue warnings about inconsistent tab usage (-tt: issue errors)\n\
5758
";
5859
static char *usage_mid = "\
@@ -108,6 +109,8 @@ Py_Main(int argc, char **argv)
108109
int stdin_is_interactive = 0;
109110
int help = 0;
110111
int version = 0;
112+
int saw_inspect_flag = 0;
113+
int saw_unbuffered_flag = 0;
111114
PyCompilerFlags cf;
112115

113116
orig_argc = argc; /* For Py_GetArgcArgv() */
@@ -117,11 +120,6 @@ Py_Main(int argc, char **argv)
117120
Py_RISCOSWimpFlag = 0;
118121
#endif
119122

120-
if ((p = getenv("PYTHONINSPECT")) && *p != '\0')
121-
inspect = 1;
122-
if ((p = getenv("PYTHONUNBUFFERED")) && *p != '\0')
123-
unbuffered = 1;
124-
125123
PySys_ResetWarnOptions();
126124

127125
while ((c = _PyOS_GetOpt(argc, argv, PROGRAM_OPTS)) != EOF) {
@@ -146,6 +144,7 @@ Py_Main(int argc, char **argv)
146144

147145
case 'i':
148146
inspect++;
147+
saw_inspect_flag = 1;
149148
Py_InteractiveFlag++;
150149
break;
151150

@@ -157,12 +156,17 @@ Py_Main(int argc, char **argv)
157156
Py_NoSiteFlag++;
158157
break;
159158

159+
case 'E':
160+
Py_IgnoreEnvironmentFlag++;
161+
break;
162+
160163
case 't':
161164
Py_TabcheckFlag++;
162165
break;
163166

164167
case 'u':
165168
unbuffered++;
169+
saw_unbuffered_flag = 1;
166170
break;
167171

168172
case 'v':
@@ -210,6 +214,13 @@ Py_Main(int argc, char **argv)
210214
exit(0);
211215
}
212216

217+
if (!saw_inspect_flag &&
218+
(p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
219+
inspect = 1;
220+
if (!saw_unbuffered_flag &&
221+
(p = Py_GETENV("PYTHONUNBUFFERED")) && *p != '\0')
222+
unbuffered = 1;
223+
213224
if (command == NULL && _PyOS_optind < argc &&
214225
strcmp(argv[_PyOS_optind], "-") != 0)
215226
{
@@ -307,7 +318,7 @@ Py_Main(int argc, char **argv)
307318
}
308319
else {
309320
if (filename == NULL && stdin_is_interactive) {
310-
char *startup = getenv("PYTHONSTARTUP");
321+
char *startup = Py_GETENV("PYTHONSTARTUP");
311322
if (startup != NULL && startup[0] != '\0') {
312323
FILE *fp = fopen(startup, "r");
313324
if (fp != NULL) {

Modules/timemodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,7 @@ inittime(void)
594594
m = Py_InitModule3("time", time_methods, module_doc);
595595
d = PyModule_GetDict(m);
596596
/* Accept 2-digit dates unless PYTHONY2K is set and non-empty */
597-
p = getenv("PYTHONY2K");
597+
p = Py_GETENV("PYTHONY2K");
598598
ins(d, "accept2dyear", PyInt_FromLong((long) (!p || !*p)));
599599
/* Squirrel away the module's dictionary for the y2k check */
600600
Py_INCREF(d);

PC/getpathp.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ calculate_path(void)
425425
char *buf;
426426
size_t bufsz;
427427
char *pythonhome = Py_GetPythonHome();
428-
char *envpath = getenv("PYTHONPATH");
428+
char *envpath = Py_GETENV("PYTHONPATH");
429429

430430
#ifdef MS_WIN32
431431
int skiphome, skipdefault;

PC/os2vacpp/getpathp.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ calculate_path(void)
289289
char *buf;
290290
int bufsz;
291291
char *pythonhome = Py_GetPythonHome();
292-
char *envpath = getenv("PYTHONPATH");
292+
char *envpath = Py_GETENV("PYTHONPATH");
293293
#ifdef MS_WIN32
294294
char *machinepath, *userpath;
295295

Python/frozenmain.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ Py_FrozenMain(int argc, char **argv)
3030

3131
Py_FrozenFlag = 1; /* Suppress errors from getpath.c */
3232

33-
if ((p = getenv("PYTHONINSPECT")) && *p != '\0')
33+
if ((p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
3434
inspect = 1;
35-
if ((p = getenv("PYTHONUNBUFFERED")) && *p != '\0')
35+
if ((p = Py_GETENV("PYTHONUNBUFFERED")) && *p != '\0')
3636
unbuffered = 1;
3737

3838
if (unbuffered) {

Python/import.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1069,7 +1069,7 @@ case_ok(char *buf, int len, int namelen, char *name)
10691069
char tempbuf[MAX_PATH];
10701070
#endif
10711071

1072-
if (getenv("PYTHONCASEOK") != NULL)
1072+
if (Py_GETENV("PYTHONCASEOK") != NULL)
10731073
return 1;
10741074

10751075
#ifdef __CYGWIN__
@@ -1092,7 +1092,7 @@ case_ok(char *buf, int len, int namelen, char *name)
10921092
struct ffblk ffblk;
10931093
int done;
10941094

1095-
if (getenv("PYTHONCASEOK") != NULL)
1095+
if (Py_GETENV("PYTHONCASEOK") != NULL)
10961096
return 1;
10971097

10981098
done = findfirst(buf, &ffblk, FA_ARCH|FA_RDONLY|FA_HIDDEN|FA_DIREC);
@@ -1109,7 +1109,7 @@ case_ok(char *buf, int len, int namelen, char *name)
11091109
FSSpec fss;
11101110
OSErr err;
11111111

1112-
if (getenv("PYTHONCASEOK") != NULL)
1112+
if (Py_GETENV("PYTHONCASEOK") != NULL)
11131113
return 1;
11141114

11151115
#ifndef USE_GUSI1
@@ -1147,7 +1147,7 @@ case_ok(char *buf, int len, int namelen, char *name)
11471147
char dirname[MAXPATHLEN + 1];
11481148
const int dirlen = len - namelen - 1; /* don't want trailing SEP */
11491149

1150-
if (getenv("PYTHONCASEOK") != NULL)
1150+
if (Py_GETENV("PYTHONCASEOK") != NULL)
11511151
return 1;
11521152

11531153
/* Copy the dir component into dirname; substitute "." if empty */

0 commit comments

Comments
 (0)