Skip to content

Commit ea239e1

Browse files
committed
Clean up module namespace in resolver, add docstring to clarify what the module is doing
1 parent 32f1cb7 commit ea239e1

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed

fluent.runtime/fluent/runtime/resolver.py

+21-13
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,23 @@
1313
from .utils import reference_to_id, unknown_reference_error_obj
1414

1515

16-
text_type = six.text_type
16+
"""
17+
The classes in this module are used to transform the source
18+
AST to a partially evaluated resolver tree. They're subclasses
19+
to the syntax AST node, and `BaseResolver`. Syntax nodes that
20+
don't require special handling, but have children that need to be
21+
transformed, need to just inherit from their syntax base class and
22+
`BaseResolver`. When adding to the module namespace here, watch
23+
out for naming conflicts with `fluent.syntax.ast`.
1724
18-
# Prevent expansion of too long placeables, for memory DOS protection
19-
MAX_PART_LENGTH = 2500
20-
21-
# Prevent messages with too many sub parts, for CPI DOS protection
22-
MAX_PARTS = 1000
25+
`ResolverEnvironment` is the `env` passed to the `__call__` method
26+
in the resolver tree. The `CurrentEnvironment` keeps track of the
27+
modifyable state in the resolver environment.
28+
"""
2329

2430

25-
# Unicode bidi isolation characters.
26-
FSI = "\u2068"
27-
PDI = "\u2069"
31+
# Prevent expansion of too long placeables, for memory DOS protection
32+
MAX_PART_LENGTH = 2500
2833

2934

3035
@attr.s
@@ -95,6 +100,9 @@ class Term(FTL.Term, BaseResolver):
95100

96101

97102
class Pattern(FTL.Pattern, BaseResolver):
103+
# Prevent messages with too many sub parts, for CPI DOS protection
104+
MAX_PARTS = 1000
105+
98106
def __init__(self, *args, **kwargs):
99107
super(Pattern, self).__init__(*args, **kwargs)
100108
self.dirty = False
@@ -103,15 +111,15 @@ def __call__(self, env):
103111
if self.dirty:
104112
env.errors.append(FluentCyclicReferenceError("Cyclic reference"))
105113
return FluentNone()
106-
if env.part_count > MAX_PARTS:
114+
if env.part_count > self.MAX_PARTS:
107115
return ""
108116
self.dirty = True
109117
elements = self.elements
110-
remaining_parts = MAX_PARTS - env.part_count
118+
remaining_parts = self.MAX_PARTS - env.part_count
111119
if len(self.elements) > remaining_parts:
112120
elements = elements[:remaining_parts + 1]
113121
env.errors.append(ValueError("Too many parts in message (> {0}), "
114-
"aborting.".format(MAX_PARTS)))
122+
"aborting.".format(self.MAX_PARTS)))
115123
retval = ''.join(
116124
resolve(element(env), env) for element in elements
117125
)
@@ -213,7 +221,7 @@ def __call__(self, env):
213221
FluentReferenceError("Unknown external: {0}".format(name)))
214222
return FluentNoneResolver(name)
215223

216-
if isinstance(arg_val, (FluentType, text_type)):
224+
if isinstance(arg_val, (FluentType, six.text_type)):
217225
return arg_val
218226
env.errors.append(TypeError("Unsupported external type: {0}, {1}"
219227
.format(name, type(arg_val))))

0 commit comments

Comments
 (0)