libpython2.5.a contains quite a lot of .data that doesn't look like it
needs to be writable (my minimal interpreter binary has 105KB of
writable allocated data). A lot of these symbols look like they could
just be tagged const with no other changes to the interpreter; some of
them would require a proliferation of constness. I'm a bit new at this,
though, and it's possible that I'm wrong about some/most/all of these,
so I'm wondering what people think.

Attached is a patch which adds const to the easy ones:
  * Docstrings for extension functions (PyDoc_VAR in Python.h)
  * ascii->digit lookup table (_PyLong_DigitValue in longobject.c)
  * The copyright notice (cprt in getcopyright.c)

There are no errors or new warnings introduced in my build by this
patch, but I'm only compiling the interpreter itself. If anyone can
suggest a reason why any of those shouldn't be const, please do :)

Things that take up space that might be const-able with more effort, or
might not, I can't tell:
  * Token names (_PyParser_TokenNames in tokenizer.c)
  * Module function tables (and ensuing propagation of constness into
    PyModule_Init etc)
  * All the type and exception objects

Things that take up space but probably aren't const-able unless someone
who knows more than me has an idea:
  * Standard slot definitions (slotdefs in typeobject.c, but the
    interned string pointers get added at runtime)
  * The generated tables for the grammar (but the accelerator seems to
    want to change these at init time)

There's probably other things, but I didn't go through the entire symbol
table, just started with the big ones :)

So, er, is this a remotely sane thing to be doing, and does anyone have
suggestions? :)

-- 
Torne Wuff
[EMAIL PROTECTED]
Index: quilted/Include/Python.h
===================================================================
--- quilted.orig/Include/Python.h	2008-09-01 16:00:58.000000000 +0100
+++ quilted/Include/Python.h	2008-09-01 16:01:57.000000000 +0100
@@ -162,7 +162,7 @@
 #endif
 
 /* Define macros for inline documentation. */
-#define PyDoc_VAR(name) static char name[]
+#define PyDoc_VAR(name) static const char name[]
 #define PyDoc_STRVAR(name,str) PyDoc_VAR(name) = PyDoc_STR(str)
 #ifdef WITH_DOC_STRINGS
 #define PyDoc_STR(str) str
Index: quilted/Objects/longobject.c
===================================================================
--- quilted.orig/Objects/longobject.c	2008-09-01 16:00:58.000000000 +0100
+++ quilted/Objects/longobject.c	2008-09-01 16:01:57.000000000 +0100
@@ -1340,7 +1340,7 @@
  * Note that when converting a base B string, a char c is a legitimate
  * base B digit iff _PyLong_DigitValue[Py_CHARMASK(c)] < B.
  */
-int _PyLong_DigitValue[256] = {
+const int _PyLong_DigitValue[256] = {
 	37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
 	37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
 	37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
Index: quilted/Python/getcopyright.c
===================================================================
--- quilted.orig/Python/getcopyright.c	2008-09-01 16:00:58.000000000 +0100
+++ quilted/Python/getcopyright.c	2008-09-01 16:01:57.000000000 +0100
@@ -2,7 +2,7 @@
 
 #include "Python.h"
 
-static char cprt[] = 
+static const char cprt[] = 
 "\
 Copyright (c) 2001-2008 Python Software Foundation.\n\
 All Rights Reserved.\n\
Index: quilted/Include/longobject.h
===================================================================
--- quilted.orig/Include/longobject.h	2008-09-01 16:00:58.000000000 +0100
+++ quilted/Include/longobject.h	2008-09-01 16:01:57.000000000 +0100
@@ -25,7 +25,7 @@
 PyAPI_FUNC(Py_ssize_t) _PyLong_AsSsize_t(PyObject *);
 PyAPI_FUNC(PyObject *) _PyLong_FromSize_t(size_t);
 PyAPI_FUNC(PyObject *) _PyLong_FromSsize_t(Py_ssize_t);
-PyAPI_DATA(int) _PyLong_DigitValue[256];
+PyAPI_DATA(const int) _PyLong_DigitValue[256];
 
 /* _PyLong_AsScaledDouble returns a double x and an exponent e such that
    the true value is approximately equal to x * 2**(SHIFT*e).  e is >= 0.
_______________________________________________
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to