Commits

Victor Stinner committed 9061b49

sys.flags

Comments (0)

Files changed (5)

  * handle "from math import *"
  * disable optimizations on non-BMP unicode strings by default (enabled with
    pythonenv feature)
+ * add sys.flags.<attr> constants
 
 Version 0.2 (2012-09-02)
 ------------------------
  - sys.version_info >= (3,3)
  - sys.version_info.major == 2
  - (x * 2) * 3 => x * 6, "fast-math" option?
- - sys.flags
 
 later:
 

astoptimizer/config_pythonenv.py

     # when this feature is used, because the behaviour depends on the
     # -Q command line option
 
-    # depend on the Python command line option "-O"
     config.add_constant('__debug__', __debug__)
+    for attr in dir(sys.flags):
+        if attr.startswith(("_", "n_")):
+            continue
+        if attr in ("count", "index"):
+            continue
+        value = getattr(sys.flags, attr)
+        config.add_constant('sys.flags.%s' % attr, value)
 
     if sys.version_info >= (2, 6):
         config.add_constant('sys.dont_write_bytecode', sys.dont_write_bytecode)

astoptimizer/optimizer.py

     def get_qualname(self, name):
         if not self._aliases_enabled:
             return UNSET
-        return self.qualnames.get(name, name)
+        if name in self.qualnames:
+            return self.qualnames[name]
+        fullname = name
+        prefixes = []
+        while '.' in name:
+            prefix, name = name.split(".", 1)
+            prefixes.append(prefix)
+            key = '.'.join(prefixes)
+            if key in self.qualnames:
+                return self.qualnames[key] + '.' + name
+        return fullname
 
     def assign(self, qualname, value):
         if qualname in self._unassigned:
             return
         return new_constant(node, constant)
 
+    def attribute_name(self, node):
+        if isinstance(node.value, ast.Name):
+            name = node.value.id
+        elif isinstance(node.value, ast.Attribute):
+            name = self.attribute_name(node.value)
+            if name is UNSET:
+                return UNSET
+        else:
+            return UNSET
+        return "%s.%s" % (name, node.attr)
+
     def visit_Attribute(self, node):
-        if isinstance(node.value, ast.Name):
-            fullname = "%s.%s" % (node.value.id, node.attr)
-            constant = self.config.get_constant(fullname)
-            if constant is UNSET:
-                return
-            return new_constant(node, constant)
-        return
+        name = self.attribute_name(node)
+        if name is UNSET:
+            return
+        name = self.namespace.get_qualname(name)
+        if name is UNSET:
+            return UNSET
+        constant = self.config.get_constant(name)
+        if constant is UNSET:
+            return
+        return new_constant(node, constant)
 
     def check_format_args(self, format, values):
         if PYTHON2:

astoptimizer/tests.py

         self.assertIn('platform', config.features)
         self.assertIn('pythonbin', config.features)
         self.check('__debug__', self.text_bool(__debug__), config)
+        self.check_not_optimized('sys.flags', config)
+        self.check('sys.flags.optimize', self.text_num(sys.flags.optimize), config)
         self.check('21 / 5', self.text_num(21 / 5), config)
 
     def test_math(self):