Skip to content

Commit 758df14

Browse files
author
georg.brandl
committed
Patch #602345 by Neal Norwitz and me: add -B option and PYTHONDONTWRITEBYTECODE envvar to skip writing bytecode.
git-svn-id: http://svn.python.org/projects/python/trunk@59824 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent 6302f15 commit 758df14

8 files changed

Lines changed: 56 additions & 8 deletions

File tree

Doc/library/sys.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,17 @@ always available.
507507
when Python is started with the -3 option.
508508

509509

510+
.. data:: dont_write_bytecode
511+
512+
If this is true, Python won't try to write ``.pyc`` or ``.pyo`` files on the
513+
import of source modules. This value is initially set to ``True`` or ``False``
514+
depending on the ``-B`` command line option and the ``PYTHONDONTWRITEBYTECODE``
515+
environment variable, but you can set it yourself to control bytecode file
516+
generation.
517+
518+
.. versionadded:: 2.6
519+
520+
510521
.. function:: setcheckinterval(interval)
511522

512523
Set the interpreter's "check interval". This integer value determines how often

Doc/using/cmdline.rst

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,14 @@ Generic options
145145
Miscellaneous options
146146
~~~~~~~~~~~~~~~~~~~~~
147147

148+
.. cmdoption:: -B
149+
150+
If given, Python won't try to write ``.pyc`` or ``.pyo`` files on the
151+
import of source modules. See also :envvar:`PYTHONDONTWRITEBYTECODE`.
152+
153+
.. versionadded:: 2.6
154+
155+
148156
.. cmdoption:: -d
149157

150158
Turn on parser debugging output (for wizards only, depending on compilation
@@ -411,3 +419,10 @@ Environment variables
411419
If this is set, Python ignores case in :keyword:`import` statements. This
412420
only works on Windows.
413421

422+
423+
.. envvar:: PYTHONDONTWRITEBYTECODE
424+
425+
If given, Python won't try to write ``.pyc`` or ``.pyo`` files on the
426+
import of source modules.
427+
428+
.. versionadded:: 2.6

Include/pydebug.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ PyAPI_DATA(int) Py_TabcheckFlag;
1717
PyAPI_DATA(int) Py_UnicodeFlag;
1818
PyAPI_DATA(int) Py_IgnoreEnvironmentFlag;
1919
PyAPI_DATA(int) Py_DivisionWarningFlag;
20+
PyAPI_DATA(int) Py_DontWriteBytecodeFlag;
2021
/* _XXX Py_QnewFlag should go away in 3.0. It's true iff -Qnew is passed,
2122
on the command line, and is used in 2.2 by ceval.c to make all "/" divisions
2223
true divisions (which they will be in 3.0). */

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ What's New in Python 2.6 alpha 1?
1212
Core and builtins
1313
-----------------
1414

15+
- Patch #602345: Add -B command line option, PYTHONDONTWRITEBYTECODE envvar
16+
and sys.dont_write_bytecode attribute. All these can be set to forbid Python
17+
to attempt to write compiled bytecode files.
18+
1519
- Improve some exception messages when Windows fails to load an extension
1620
module. Now we get for example '%1 is not a valid Win32 application' instead
1721
of 'error code 193'.

Modules/main.c

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ static char **orig_argv;
4040
static int orig_argc;
4141

4242
/* command line options */
43-
#define BASE_OPTS "3c:dEhim:OQ:StuUvVW:xX?"
43+
#define BASE_OPTS "3Bc:dEhim:OQ:StuUvVW:xX?"
4444

4545
#ifndef RISCOS
4646
#define PROGRAM_OPTS BASE_OPTS
@@ -59,39 +59,42 @@ static char *usage_line =
5959
/* Long usage message, split into parts < 512 bytes */
6060
static char *usage_1 = "\
6161
Options and arguments (and corresponding environment variables):\n\
62+
-B : don't write .py[co] files on import; also PYTHONDONTWRITEBYTECODE=x\n\
6263
-c cmd : program passed in as string (terminates option list)\n\
6364
-d : debug output from parser; also PYTHONDEBUG=x\n\
6465
-E : ignore environment variables (such as PYTHONPATH)\n\
6566
-h : print this help message and exit (also --help)\n\
6667
-i : inspect interactively after running script; forces a prompt even\n\
67-
if stdin does not appear to be a terminal; also PYTHONINSPECT=x\n\
6868
";
6969
static char *usage_2 = "\
70+
if stdin does not appear to be a terminal; also PYTHONINSPECT=x\n\
7071
-m mod : run library module as a script (terminates option list)\n\
7172
-O : optimize generated bytecode slightly; also PYTHONOPTIMIZE=x\n\
7273
-OO : remove doc-strings in addition to the -O optimizations\n\
7374
-Q arg : division options: -Qold (default), -Qwarn, -Qwarnall, -Qnew\n\
7475
-S : don't imply 'import site' on initialization\n\
7576
-t : issue warnings about inconsistent tab usage (-tt: issue errors)\n\
76-
-u : unbuffered binary stdout and stderr; also PYTHONUNBUFFERED=x\n\
7777
";
7878
static char *usage_3 = "\
79+
-u : unbuffered binary stdout and stderr; also PYTHONUNBUFFERED=x\n\
7980
see man page for details on internal buffering relating to '-u'\n\
8081
-v : verbose (trace import statements); also PYTHONVERBOSE=x\n\
8182
can be supplied multiple times to increase verbosity\n\
8283
-V : print the Python version number and exit (also --version)\n\
8384
-W arg : warning control; arg is action:message:category:module:lineno\n\
8485
-x : skip first line of source, allowing use of non-Unix forms of #!cmd\n\
86+
";
87+
static char *usage_4 = "\
8588
-3 : warn about Python 3.x incompatibilities\n\
8689
file : program read from script file\n\
8790
- : program read from stdin (default; interactive mode if a tty)\n\
88-
";
89-
static char *usage_4 = "\
9091
arg ...: arguments passed to program in sys.argv[1:]\n\n\
9192
Other environment variables:\n\
9293
PYTHONSTARTUP: file executed on interactive startup (no default)\n\
9394
PYTHONPATH : '%c'-separated list of directories prefixed to the\n\
9495
default module search path. The result is sys.path.\n\
96+
";
97+
static char *usage_5 = "\
9598
PYTHONHOME : alternate <prefix> directory (or <prefix>%c<exec_prefix>).\n\
9699
The default module search path uses %s.\n\
97100
PYTHONCASEOK : ignore case in 'import' statements (Windows).\n\
@@ -110,7 +113,8 @@ usage(int exitcode, char* program)
110113
fprintf(f, usage_1);
111114
fprintf(f, usage_2);
112115
fprintf(f, usage_3);
113-
fprintf(f, usage_4, DELIM, DELIM, PYTHONHOMEHELP);
116+
fprintf(f, usage_4, DELIM);
117+
fprintf(f, usage_5, DELIM, PYTHONHOMEHELP);
114118
}
115119
#if defined(__VMS)
116120
if (exitcode == 0) {
@@ -337,6 +341,10 @@ Py_Main(int argc, char **argv)
337341
Py_OptimizeFlag++;
338342
break;
339343

344+
case 'B':
345+
Py_DontWriteBytecodeFlag++;
346+
break;
347+
340348
case 'S':
341349
Py_NoSiteFlag++;
342350
break;

Python/import.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -950,8 +950,11 @@ load_source_module(char *name, char *pathname, FILE *fp)
950950
if (Py_VerboseFlag)
951951
PySys_WriteStderr("import %s # from %s\n",
952952
name, pathname);
953-
if (cpathname)
954-
write_compiled_module(co, cpathname, mtime);
953+
if (cpathname) {
954+
PyObject *ro = PySys_GetObject("dont_write_bytecode");
955+
if (ro == NULL || !PyObject_IsTrue(ro))
956+
write_compiled_module(co, cpathname, mtime);
957+
}
955958
}
956959
m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, pathname);
957960
Py_DECREF(co);

Python/pythonrun.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ int Py_VerboseFlag; /* Needed by import.c */
7272
int Py_InteractiveFlag; /* Needed by Py_FdIsInteractive() below */
7373
int Py_InspectFlag; /* Needed to determine whether to exit at SystemError */
7474
int Py_NoSiteFlag; /* Suppress 'import site' */
75+
int Py_DontWriteBytecodeFlag; /* Suppress writing bytecode files (*.py[co]) */
7576
int Py_UseClassExceptionsFlag = 1; /* Needed by bltinmodule.c: deprecated */
7677
int Py_FrozenFlag; /* Needed by getpath.c */
7778
int Py_UnicodeFlag = 0; /* Needed by compile.c */
@@ -172,6 +173,8 @@ Py_InitializeEx(int install_sigs)
172173
Py_VerboseFlag = add_flag(Py_VerboseFlag, p);
173174
if ((p = Py_GETENV("PYTHONOPTIMIZE")) && *p != '\0')
174175
Py_OptimizeFlag = add_flag(Py_OptimizeFlag, p);
176+
if ((p = Py_GETENV("PYTHONDONTWRITEBYTECODE")) && *p != '\0')
177+
Py_DontWriteBytecodeFlag = add_flag(Py_DontWriteBytecodeFlag, p);
175178

176179
interp = PyInterpreterState_New();
177180
if (interp == NULL)

Python/sysmodule.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1128,6 +1128,9 @@ _PySys_Init(void)
11281128
v = Py_BuildValue("(ssz)", "CPython", branch, svn_revision);
11291129
PyDict_SetItemString(sysdict, "subversion", v);
11301130
Py_XDECREF(v);
1131+
PyDict_SetItemString(sysdict, "dont_write_bytecode",
1132+
v = PyBool_FromLong(Py_DontWriteBytecodeFlag));
1133+
Py_XDECREF(v);
11311134
/*
11321135
* These release level checks are mutually exclusive and cover
11331136
* the field, so don't get too fancy with the pre-processor!

0 commit comments

Comments
 (0)