[Python-ideas] Changing optimisation level from a script

M.-A. Lemburg mal at egenix.com
Sat Sep 10 16:00:04 EDT 2016


In eGenix PyRun we expose a new helper in the sys module
for this:

--- ./Python/sysmodule.c 2015-12-07 02:39:11.000000000 +0100
+++ ./Python/sysmodule.c        2016-05-03 19:22:35.793193862 +0200
@@ -1205,6 +1205,50 @@
 Return True if Python is exiting.");


+/*** PyRun Extension ***************************************************/
+
+#define SYS_SETFLAG(c_flag, flag_name)         \
+    if (strcmp(flagname, flag_name) == 0) {    \
+       old_value = c_flag;                     \
+       if (value != -1)                        \
+           c_flag = value;                     \
+    } else
+
+static PyObject *
+sys_setflag(PyObject* self, PyObject* args)
+{
+    char *flagname;
+    int value = -1, old_value = value;
+
+    if (!PyArg_ParseTuple(args, "s|i", &flagname, &value))
+        goto onError;
+
+    SYS_SETFLAG(Py_DebugFlag, "debug")
+    SYS_SETFLAG(Py_OptimizeFlag, "optimize")
+    SYS_SETFLAG(Py_DontWriteBytecodeFlag, "dont_write_bytecode")
+    SYS_SETFLAG(Py_VerboseFlag, "verbose")
+    SYS_SETFLAG(Py_HashRandomizationFlag, "hash_randomization")
+    SYS_SETFLAG(Py_InspectFlag, "inspect")
+    {
+        PyErr_SetString(PyExc_ValueError,
+                        "unknown flag name");
+       goto onError;
+    }
+    return PyLong_FromLong((long)old_value);
+
+ onError:
+    return NULL;
+}
+
+#undef SYS_SETFLAG
+
+PyDoc_STRVAR(sys_setflag__doc__,
+"_setflag(flagname, value) -> old_value\n\
+Set the given interpreter flag and return its previous value.");
+
+/*** End of PyRun Extension
***********************************************/
+
+
 static PyMethodDef sys_methods[] = {
     /* Might as well keep this in alphabetic order */
     {"callstats", (PyCFunction)PyEval_GetCallStats, METH_NOARGS,
@@ -1268,6 +1312,7 @@
     {"setdlopenflags", sys_setdlopenflags, METH_VARARGS,
      setdlopenflags_doc},
 #endif
+    {"_setflag", sys_setflag, METH_VARARGS, sys_setflag__doc__},
     {"setprofile",      sys_setprofile, METH_O, setprofile_doc},
     {"getprofile",      sys_getprofile, METH_NOARGS, getprofile_doc},
     {"setrecursionlimit", sys_setrecursionlimit, METH_VARARGS,

We need this for similar reasons, since PyRun does the command
line processing in Python rather than in C.

The helper doesn't only expose the optimize flag, but also
several other flags.


On 10.09.2016 02:04, Damien George wrote:
>> What is to stop you from adding this to micropython as a library
>> extension?
> 
> That's what I would like to do, and usually we do just go ahead and
> add our own extensions, trying to be Pythonic as possible :)
> 
> But we do that a lot and sometimes I think it would be good to discuss
> with upstream (ie python-dev/python-ideas) about adding new
> functions/classes so that MicroPython doesn't diverge too much (eg
> Paul Sokolovsky had a discussion about time.sleep_ms, time.sleep_us
> etc, which was fruitful).  sys.optimize(value) is a pretty simple
> addition so I thought it would be a straight forward discussion to
> have here.
> 
> I guess my main question to this list is: if CPython were to add a
> function to change the optimisation level at runtime, what would it
> look like?  I don't want to push CPython to actually add such a thing
> if it's not seen as a useful addition.  Instead I want to see how
> others would implement it if they needed to.
> 
> A nice outcome would be that MicroPython adds the function now (eg
> sys.optimize), and in the future if CPython finds that it needs it, it
> also adds the same function with the same name/module/signature.
> Hence why I would like to add it correctly into MicroPython from the
> very beginning.
> 
> Alternatively we can just use the micropython module and add
> micropython.optimize(value), and then if CPython does add it later on
> we'll have to change how we do it.
> 
>> I've never felt the urge to do this and I don't think I've
>> ever heard it requested before. If you think it should be in the
>> stdlib, given the timing of the 3.6 feature freeze the earliest time
>> it could land would be Python 3.7.
> 
> No, I definitely don't want to make a rush for 3.6.  I guess the
> timing of this post was not the best considering the 3.6 release :)
> 
>> Finally, let's not define APIs with
>> British spelling. :-)
> 
> Haha, the spelling never even crossed my mind!  Well, sys.opt() is
> definitely too short and confusing, so "optimize" would be the choice.
> 
> Cheers,
> Damien.
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
> 

-- 
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Experts (#1, Sep 10 2016)
>>> Python Projects, Coaching and Consulting ...  http://www.egenix.com/
>>> Python Database Interfaces ...           http://products.egenix.com/
>>> Plone/Zope Database Interfaces ...           http://zope.egenix.com/
________________________________________________________________________

::: We implement business ideas - efficiently in both time and costs :::

   eGenix.com Software, Skills and Services GmbH  Pastor-Loeh-Str.48
    D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
           Registered at Amtsgericht Duesseldorf: HRB 46611
               http://www.egenix.com/company/contact/
                      http://www.malemburg.com/



More information about the Python-ideas mailing list