Mercurial > p > roundup > code
annotate roundup/cgi/PageTemplates/PythonExpr.py @ 5525:bb7865241f8a
Make CSV import/export compatible across Python versions (also RDBMS journals) (issue 2550976, issue 2550975).
The roundup-admin export and import commands are used for migrating
between different database backends. It is desirable that they should
be usable also for migrations between Python 2 and Python 3, and in
some cases (e.g. with the anydbm backend) this may be required.
To be usable for such migrations, the format of the generated CSV
files needs to be stable, meaning the same as currently used with
Python 2. The export process uses repr() to produce the fields in the
CSV files and eval() to convert them back to Python data structures.
repr() of strings with non-ASCII characters produces different results
for Python 2 and Python 3.
This patch adds repr_export and eval_import functions to
roundup/anypy/strings.py which provide the required operations that
are just repr() and eval() in Python 2, but are more complicated in
Python 3 to use data representations compatible with Python 2. These
functions are then used in the required places for export and import.
repr() and eval() are also used in storing the dict of changed values
in the journal for the RDBMS backends. It is similarly desirable that
the database be compatible between Python 2 and Python 3, so that
export and import do not need to be used for a migration between
Python versions for non-anydbm back ends. Thus, this patch changes
rdbms_common.py in the places involved in storing journals in the
database, not just in those involved in import/export.
Given this patch, import/export with non-ASCII characters appear based
on some limited testing to work across Python versions, and an
instance using the sqlite backend appears to be compatible between
Python versions without needing import/export, *if* the sessions/otks
databases (which use anydbm) are deleted when changing Python version.
| author | Joseph Myers <jsm@polyomino.org.uk> |
|---|---|
| date | Sun, 02 Sep 2018 23:48:04 +0000 |
| parents | f8673e720f30 |
| children | e70885fe72a4 |
| rev | line source |
|---|---|
| 1049 | 1 ############################################################################## |
| 2 # | |
| 3 # Copyright (c) 2001 Zope Corporation and Contributors. All Rights Reserved. | |
|
2349
b43efe461b3e
update PageTemplates to latest Zope codebase
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
4 # |
| 1049 | 5 # This software is subject to the provisions of the Zope Public License, |
| 6 # Version 2.0 (ZPL). A copy of the ZPL should accompany this distribution. | |
| 7 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED | |
| 8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
| 9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS | |
| 10 # FOR A PARTICULAR PURPOSE | |
|
2349
b43efe461b3e
update PageTemplates to latest Zope codebase
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
11 # |
|
b43efe461b3e
update PageTemplates to latest Zope codebase
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
12 ############################################################################## |
|
b43efe461b3e
update PageTemplates to latest Zope codebase
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
13 # Modified for Roundup: |
| 1049 | 14 # |
|
2349
b43efe461b3e
update PageTemplates to latest Zope codebase
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
15 # 1. more informative traceback info |
| 1049 | 16 |
| 17 """Generic Python Expression Handler | |
|
2349
b43efe461b3e
update PageTemplates to latest Zope codebase
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
18 """ |
|
1071
c08b3820edd1
Adhering to ZPL
Richard Jones <richard@users.sourceforge.net>
parents:
1065
diff
changeset
|
19 |
|
5388
d26921b851c3
Python 3 preparation: make relative imports explicit.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5384
diff
changeset
|
20 from .TALES import CompilerError |
| 1049 | 21 from sys import exc_info |
| 22 | |
| 23 class getSecurityManager: | |
| 24 '''Null security manager''' | |
| 25 def validate(self, *args, **kwargs): | |
| 26 return 1 | |
| 27 addContext = removeContext = validateValue = validate | |
| 28 | |
| 29 class PythonExpr: | |
| 30 def __init__(self, name, expr, engine): | |
|
2349
b43efe461b3e
update PageTemplates to latest Zope codebase
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
31 self.expr = expr = expr.strip().replace('\n', ' ') |
| 1049 | 32 try: |
| 33 d = {} | |
|
5384
c4774b9f483c
Python 3 preparation: convert exec to a function.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5378
diff
changeset
|
34 exec('def f():\n return %s\n' % expr.strip(), d) |
| 1049 | 35 self._f = d['f'] |
| 36 except: | |
|
5378
35ea9b1efc14
Python 3 preparation: "raise" syntax.
Joseph Myers <jsm@polyomino.org.uk>
parents:
4570
diff
changeset
|
37 raise CompilerError(('Python expression error:\n' |
|
35ea9b1efc14
Python 3 preparation: "raise" syntax.
Joseph Myers <jsm@polyomino.org.uk>
parents:
4570
diff
changeset
|
38 '%s: %s') % exc_info()[:2]) |
| 1049 | 39 self._get_used_names() |
| 40 | |
| 41 def _get_used_names(self): | |
| 42 self._f_varnames = vnames = [] | |
|
5389
f8673e720f30
Python 3 preparation: update function attribute names.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5388
diff
changeset
|
43 for vname in self._f.__code__.co_names: |
| 1049 | 44 if vname[0] not in '$_': |
| 45 vnames.append(vname) | |
| 46 | |
|
2349
b43efe461b3e
update PageTemplates to latest Zope codebase
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
47 def _bind_used_names(self, econtext, _marker=[]): |
| 1049 | 48 # Bind template variables |
|
2349
b43efe461b3e
update PageTemplates to latest Zope codebase
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
49 names = {'CONTEXTS': econtext.contexts} |
| 1049 | 50 vars = econtext.vars |
|
1257
93b80ad11ca8
merged Zope Collector #372 fix from ZPT CVS trunk
Richard Jones <richard@users.sourceforge.net>
parents:
1071
diff
changeset
|
51 getType = econtext.getCompiler().getTypes().get |
| 1049 | 52 for vname in self._f_varnames: |
|
2349
b43efe461b3e
update PageTemplates to latest Zope codebase
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
53 val = vars.get(vname, _marker) |
|
b43efe461b3e
update PageTemplates to latest Zope codebase
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
54 if val is _marker: |
| 1049 | 55 has = val = getType(vname) |
| 56 if has: | |
| 57 val = ExprTypeProxy(vname, val, econtext) | |
|
2349
b43efe461b3e
update PageTemplates to latest Zope codebase
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
58 names[vname] = val |
|
b43efe461b3e
update PageTemplates to latest Zope codebase
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
59 else: |
| 1049 | 60 names[vname] = val |
| 61 return names | |
| 62 | |
| 63 def __call__(self, econtext): | |
|
1065
0f9aa62917bd
much nicer error messages when there's a templating error
Richard Jones <richard@users.sourceforge.net>
parents:
1049
diff
changeset
|
64 __traceback_info__ = 'python expression "%s"'%self.expr |
| 1049 | 65 f = self._f |
|
5389
f8673e720f30
Python 3 preparation: update function attribute names.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5388
diff
changeset
|
66 f.__globals__.update(self._bind_used_names(econtext)) |
| 1049 | 67 return f() |
| 68 | |
| 69 def __str__(self): | |
| 70 return 'Python expression "%s"' % self.expr | |
| 71 def __repr__(self): | |
| 72 return '<PythonExpr %s>' % self.expr | |
| 73 | |
| 74 class ExprTypeProxy: | |
| 75 '''Class that proxies access to an expression type handler''' | |
| 76 def __init__(self, name, handler, econtext): | |
| 77 self._name = name | |
| 78 self._handler = handler | |
| 79 self._econtext = econtext | |
| 80 def __call__(self, text): | |
| 81 return self._handler(self._name, text, | |
|
1257
93b80ad11ca8
merged Zope Collector #372 fix from ZPT CVS trunk
Richard Jones <richard@users.sourceforge.net>
parents:
1071
diff
changeset
|
82 self._econtext.getCompiler())(self._econtext) |
| 1049 | 83 |
