]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Patch #602345 by Neal Norwitz and me: add -B option and PYTHONDONTWRITEBYTECODE envva...
authorGeorg Brandl <georg@python.org>
Mon, 7 Jan 2008 17:09:35 +0000 (17:09 +0000)
committerGeorg Brandl <georg@python.org>
Mon, 7 Jan 2008 17:09:35 +0000 (17:09 +0000)
Doc/library/sys.rst
Doc/using/cmdline.rst
Include/pydebug.h
Misc/NEWS
Modules/main.c
Python/import.c
Python/pythonrun.c
Python/sysmodule.c

index 24ce2078b39d921c40ec230301b826c9c570b2ad..a98c5d4054a227dc403fa8caf91962a33220ce6d 100644 (file)
@@ -507,6 +507,17 @@ always available.
    when Python is started with the -3 option.
 
 
+.. data:: dont_write_bytecode
+
+   If this is true, Python won't try to write ``.pyc`` or ``.pyo`` files on the
+   import of source modules.  This value is initially set to ``True`` or ``False``
+   depending on the ``-B`` command line option and the ``PYTHONDONTWRITEBYTECODE``
+   environment variable, but you can set it yourself to control bytecode file
+   generation.
+
+   .. versionadded:: 2.6
+
+
 .. function:: setcheckinterval(interval)
 
    Set the interpreter's "check interval".  This integer value determines how often
index 5f6bda51c4e00ec945266b4b9bb5823f2af9aadb..b113c8f4489832442d4f7e31e1f209167fd2ecb3 100644 (file)
@@ -145,6 +145,14 @@ Generic options
 Miscellaneous options
 ~~~~~~~~~~~~~~~~~~~~~
 
+.. cmdoption:: -B
+
+   If given, Python won't try to write ``.pyc`` or ``.pyo`` files on the
+   import of source modules.  See also :envvar:`PYTHONDONTWRITEBYTECODE`.
+
+   .. versionadded:: 2.6
+
+
 .. cmdoption:: -d
 
    Turn on parser debugging output (for wizards only, depending on compilation
@@ -411,3 +419,10 @@ Environment variables
    If this is set, Python ignores case in :keyword:`import` statements.  This
    only works on Windows.
 
+
+.. envvar:: PYTHONDONTWRITEBYTECODE
+
+   If given, Python won't try to write ``.pyc`` or ``.pyo`` files on the
+   import of source modules.
+
+   .. versionadded:: 2.6
index 3282dc10ec467c4f3b134d75d22d2072ec41ded6..1d24853191d74fea3328371e0c064b1b89a9af55 100644 (file)
@@ -17,6 +17,7 @@ PyAPI_DATA(int) Py_TabcheckFlag;
 PyAPI_DATA(int) Py_UnicodeFlag;
 PyAPI_DATA(int) Py_IgnoreEnvironmentFlag;
 PyAPI_DATA(int) Py_DivisionWarningFlag;
+PyAPI_DATA(int) Py_DontWriteBytecodeFlag;
 /* _XXX Py_QnewFlag should go away in 3.0.  It's true iff -Qnew is passed,
   on the command line, and is used in 2.2 by ceval.c to make all "/" divisions
   true divisions (which they will be in 3.0). */
index 3896dd86beaae6246d33b828e893d88011d9f51a..dbc0034c13a2289476e1f57e2c9885befa7407e4 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,10 @@ What's New in Python 2.6 alpha 1?
 Core and builtins
 -----------------
 
+- Patch #602345: Add -B command line option, PYTHONDONTWRITEBYTECODE envvar
+  and sys.dont_write_bytecode attribute. All these can be set to forbid Python
+  to attempt to write compiled bytecode files.
+
 - Improve some exception messages when Windows fails to load an extension
   module. Now we get for example '%1 is not a valid Win32 application' instead
   of 'error code 193'.
index 26f8780e72b5a5c49dececcb83cf322fbc70a402..71c9c5909fb680e590968ac6888decb07ff45c58 100644 (file)
@@ -40,7 +40,7 @@ static char **orig_argv;
 static int  orig_argc;
 
 /* command line options */
-#define BASE_OPTS "3c:dEhim:OQ:StuUvVW:xX?"
+#define BASE_OPTS "3Bc:dEhim:OQ:StuUvVW:xX?"
 
 #ifndef RISCOS
 #define PROGRAM_OPTS BASE_OPTS
@@ -59,39 +59,42 @@ static char *usage_line =
 /* Long usage message, split into parts < 512 bytes */
 static char *usage_1 = "\
 Options and arguments (and corresponding environment variables):\n\
+-B     : don't write .py[co] files on import; also PYTHONDONTWRITEBYTECODE=x\n\
 -c cmd : program passed in as string (terminates option list)\n\
 -d     : debug output from parser; also PYTHONDEBUG=x\n\
 -E     : ignore environment variables (such as PYTHONPATH)\n\
 -h     : print this help message and exit (also --help)\n\
 -i     : inspect interactively after running script; forces a prompt even\n\
-         if stdin does not appear to be a terminal; also PYTHONINSPECT=x\n\
 ";
 static char *usage_2 = "\
+         if stdin does not appear to be a terminal; also PYTHONINSPECT=x\n\
 -m mod : run library module as a script (terminates option list)\n\
 -O     : optimize generated bytecode slightly; also PYTHONOPTIMIZE=x\n\
 -OO    : remove doc-strings in addition to the -O optimizations\n\
 -Q arg : division options: -Qold (default), -Qwarn, -Qwarnall, -Qnew\n\
 -S     : don't imply 'import site' on initialization\n\
 -t     : issue warnings about inconsistent tab usage (-tt: issue errors)\n\
--u     : unbuffered binary stdout and stderr; also PYTHONUNBUFFERED=x\n\
 ";
 static char *usage_3 = "\
+-u     : unbuffered binary stdout and stderr; also PYTHONUNBUFFERED=x\n\
          see man page for details on internal buffering relating to '-u'\n\
 -v     : verbose (trace import statements); also PYTHONVERBOSE=x\n\
          can be supplied multiple times to increase verbosity\n\
 -V     : print the Python version number and exit (also --version)\n\
 -W arg : warning control; arg is action:message:category:module:lineno\n\
 -x     : skip first line of source, allowing use of non-Unix forms of #!cmd\n\
+";
+static char *usage_4 = "\
 -3     : warn about Python 3.x incompatibilities\n\
 file   : program read from script file\n\
 -      : program read from stdin (default; interactive mode if a tty)\n\
-";
-static char *usage_4 = "\
 arg ...: arguments passed to program in sys.argv[1:]\n\n\
 Other environment variables:\n\
 PYTHONSTARTUP: file executed on interactive startup (no default)\n\
 PYTHONPATH   : '%c'-separated list of directories prefixed to the\n\
                default module search path.  The result is sys.path.\n\
+";
+static char *usage_5 = "\
 PYTHONHOME   : alternate <prefix> directory (or <prefix>%c<exec_prefix>).\n\
                The default module search path uses %s.\n\
 PYTHONCASEOK : ignore case in 'import' statements (Windows).\n\
@@ -110,7 +113,8 @@ usage(int exitcode, char* program)
                fprintf(f, usage_1);
                fprintf(f, usage_2);
                fprintf(f, usage_3);
-               fprintf(f, usage_4, DELIM, DELIM, PYTHONHOMEHELP);
+               fprintf(f, usage_4, DELIM);
+               fprintf(f, usage_5, DELIM, PYTHONHOMEHELP);
        }
 #if defined(__VMS)
        if (exitcode == 0) {
@@ -337,6 +341,10 @@ Py_Main(int argc, char **argv)
                        Py_OptimizeFlag++;
                        break;
 
+               case 'B':
+                       Py_DontWriteBytecodeFlag++;
+                       break;
+
                case 'S':
                        Py_NoSiteFlag++;
                        break;
index e191bd576a82f77424c603f55fb933f03fd16bb2..8094f7d477911bcab916d786e001a97264f89d67 100644 (file)
@@ -950,8 +950,11 @@ load_source_module(char *name, char *pathname, FILE *fp)
                if (Py_VerboseFlag)
                        PySys_WriteStderr("import %s # from %s\n",
                                name, pathname);
-               if (cpathname)
-                       write_compiled_module(co, cpathname, mtime);
+               if (cpathname) {
+                       PyObject *ro = PySys_GetObject("dont_write_bytecode");
+                       if (ro == NULL || !PyObject_IsTrue(ro))
+                               write_compiled_module(co, cpathname, mtime);
+               }
        }
        m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, pathname);
        Py_DECREF(co);
index adae679057b61fcbdce22dc6eed6b7206b4c4335..4939f5489083312d667abc7884678ff2e03db78b 100644 (file)
@@ -72,6 +72,7 @@ int Py_VerboseFlag; /* Needed by import.c */
 int Py_InteractiveFlag; /* Needed by Py_FdIsInteractive() below */
 int Py_InspectFlag; /* Needed to determine whether to exit at SystemError */
 int Py_NoSiteFlag; /* Suppress 'import site' */
+int Py_DontWriteBytecodeFlag; /* Suppress writing bytecode files (*.py[co]) */
 int Py_UseClassExceptionsFlag = 1; /* Needed by bltinmodule.c: deprecated */
 int Py_FrozenFlag; /* Needed by getpath.c */
 int Py_UnicodeFlag = 0; /* Needed by compile.c */
@@ -172,6 +173,8 @@ Py_InitializeEx(int install_sigs)
                Py_VerboseFlag = add_flag(Py_VerboseFlag, p);
        if ((p = Py_GETENV("PYTHONOPTIMIZE")) && *p != '\0')
                Py_OptimizeFlag = add_flag(Py_OptimizeFlag, p);
+       if ((p = Py_GETENV("PYTHONDONTWRITEBYTECODE")) && *p != '\0')
+               Py_DontWriteBytecodeFlag = add_flag(Py_DontWriteBytecodeFlag, p);
 
        interp = PyInterpreterState_New();
        if (interp == NULL)
index 2a6a8c3e57a6b2a6eea50dbc03261b12bb73fc24..bd551b5abdf0c03c0a359a69d07a786af7ccbd78 100644 (file)
@@ -1128,6 +1128,9 @@ _PySys_Init(void)
        v = Py_BuildValue("(ssz)", "CPython", branch, svn_revision);
        PyDict_SetItemString(sysdict, "subversion", v);
        Py_XDECREF(v);
+        PyDict_SetItemString(sysdict, "dont_write_bytecode",
+                             v = PyBool_FromLong(Py_DontWriteBytecodeFlag));
+        Py_XDECREF(v);
        /*
         * These release level checks are mutually exclusive and cover
         * the field, so don't get too fancy with the pre-processor!