]> granicus.if.org Git - python/commitdiff
Expose the CO_xxx flags via the "new" module (re-solving a problem "the
authorTim Peters <tim.peters@gmail.com>
Sat, 18 Aug 2001 20:18:49 +0000 (20:18 +0000)
committerTim Peters <tim.peters@gmail.com>
Sat, 18 Aug 2001 20:18:49 +0000 (20:18 +0000)
right way").  Fiddle __future__.py to use them.

Jeremy's pyassem.py may also want to use them (by-hand duplication of
magic numbers is brittle), but leaving that to his judgment.

Beef up __future__'s test to verify the exported feature names appear
correct.

Lib/__future__.py
Lib/test/test___future__.py
Modules/newmodule.c

index ef9fd3607cf552fba6bfa7633f614e54e2b679c7..5a6483832e8c0a33b57bf262e47d410a5bec70df 100644 (file)
@@ -55,14 +55,7 @@ all_feature_names = [
 
 __all__ = ["all_feature_names"] + all_feature_names
 
-
-# The CO_xxx symbols are defined here under the same names used by
-# compile.h, so that an editor search will find them here.  However,
-# they're not exported in __all__, because they don't really belong to
-# this module.
-CO_NESTED            = 0x0010   # nested_scopes
-CO_GENERATOR_ALLOWED = 0x1000   # generators
-CO_FUTURE_DIVISION   = 0x2000   # division
+import new as _new  # for CO_xxx symbols
 
 class _Feature:
     def __init__(self, optionalRelease, mandatoryRelease, compiler_flag):
@@ -93,12 +86,12 @@ class _Feature:
 
 nested_scopes = _Feature((2, 1, 0, "beta",  1),
                          (2, 2, 0, "alpha", 0),
-                         CO_NESTED)
+                         _new.CO_NESTED)
 
 generators = _Feature((2, 2, 0, "alpha", 1),
                       (2, 3, 0, "final", 0),
-                      CO_GENERATOR_ALLOWED)
+                      _new.CO_GENERATOR_ALLOWED)
 
 division = _Feature((2, 2, 0, "alpha", 2),
                     (3, 0, 0, "alpha", 0),
-                    CO_FUTURE_DIVISION)
+                    _new.CO_FUTURE_DIVISION)
index 1897c14b7ffa8e6664491d00d00f9bcfd3964e84..fa8224f7b02b2e1dd36f74c38f39a8d8b36ede56 100644 (file)
@@ -6,6 +6,19 @@ import __future__
 GOOD_SERIALS = ("alpha", "beta", "candidate", "final")
 
 features = __future__.all_feature_names
+
+# Verify that all_feature_names appears correct.
+given_feature_names = features[:]
+for name in dir(__future__):
+    obj = getattr(__future__, name, None)
+    if obj is not None and isinstance(obj, __future__._Feature):
+        verify(name in given_feature_names,
+               "%r should have been in all_feature_names" % name)
+        given_feature_names.remove(name)
+verify(len(given_feature_names) == 0,
+       "all_feature_names has too much: %r" % given_feature_names)
+del given_feature_names
+
 for feature in features:
     value = getattr(__future__, feature)
     if verbose:
index 7b91fb8bc3ef8f0226e10ccead87f17c9ba7cfc8..d1869e5a550d514165228149b2641f924b08543a 100644 (file)
@@ -220,9 +220,38 @@ char new_doc[] =
 \n\
 You need to know a great deal about the interpreter to use this!";
 
+static void
+insertint(PyObject *d, char *name, int value)
+{
+       PyObject *v = PyInt_FromLong((long) value);
+       if (v == NULL) {
+               /* Don't bother reporting this error */
+               PyErr_Clear();
+       }
+       else {
+               PyDict_SetItemString(d, name, v);
+               Py_DECREF(v);
+       }
+}
+
 DL_EXPORT(void)
 initnew(void)
 {
-       Py_InitModule4("new", new_methods, new_doc, (PyObject *)NULL,
-                      PYTHON_API_VERSION);
+       PyObject *m;
+       PyObject *d;
+
+       m = Py_InitModule4("new", new_methods, new_doc, (PyObject *)NULL,
+                           PYTHON_API_VERSION);
+       d = PyModule_GetDict(m);
+
+#define ADDSYM(TOKEN) insertint(d, #TOKEN, TOKEN)
+       ADDSYM(CO_OPTIMIZED);
+       ADDSYM(CO_NEWLOCALS);
+       ADDSYM(CO_VARARGS);
+       ADDSYM(CO_VARKEYWORDS);
+       ADDSYM(CO_NESTED);
+       ADDSYM(CO_GENERATOR);
+       ADDSYM(CO_GENERATOR_ALLOWED);
+       ADDSYM(CO_FUTURE_DIVISION);
+#undef ADDSYM
 }