Issue #1309 new

Python-3.4 - patch - unrepr does not recognize 'NameConstant'

Artyom Lebedev
created an issue

Tried cherrypy with recently released Python-3.4. So far the only problem was configuration parsing problem. unrepr() method uses ast module which has changed semantic for named constants. Now the corresponding class called NameConstant instead of Name and has 'value' attribute with Python value instead of 'id' attribute with name.

The thrown exception:

Traceback (most recent call last):
  File "<my_prefix>/python/cherrypy/lib/reprconf.py", line 215, in as_dict
    value = unrepr(value)
  File "<my_prefix>/python/cherrypy/lib/reprconf.py", line 453, in unrepr
    return b.build(obj)
  File "<my_prefix>/python/cherrypy/lib/reprconf.py", line 338, in build
    repr(o.__class__.__name__))
TypeError: unrepr does not recognize 'NameConstant'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "emulator.py", line 10, in <module>
    emulator.main.Main()
  File "main.py", line 204, in Main
    cherrypy.config.update(os.path.join(configDir, 'server.conf'))
  File "<my_prefix>/python/cherrypy/_cpconfig.py", line 156, in update
    reprconf.Config.update(self, config)
  File "<my_prefix>/python/cherrypy/lib/reprconf.py", line 158, in update
    config = Parser().dict_from_file(config)
  File "<my_prefix>/python/cherrypy/lib/reprconf.py", line 230, in dict_from_file
    return self.as_dict()
  File "<my_prefix>/python/cherrypy/lib/reprconf.py", line 221, in as_dict
    raise ValueError(msg, x.__class__.__name__, x.args)
ValueError: ("Config error in section: 'global', option: 'log.screen', value: 'True'. Config values must be valid Python.", 'TypeError', ("unrepr does not recognize 'NameConstant'",))

The proposed patch (should be reviewed, I did not dive into idea with modules in the current solution):

diff --git a/lib/reprconf.py b/lib/reprconf.py
index 83ca78f..de00466 100644
--- a/lib/reprconf.py
+++ b/lib/reprconf.py
@@ -415,6 +415,9 @@

         raise TypeError("unrepr could not resolve the name %s" % repr(name))

+    def build_NameConstant(self, o):
+        return o.value
+
     def build_UnaryOp(self, o):
         op, operand = map(self.build, [o.op, o.operand])
         return op(operand)

Comments (0)

  1. Log in to comment