bytes optimization

This commit is contained in:
Stuart D. Gathman
2020-06-18 16:40:54 -04:00
parent 4c7c76fca4
commit 879e65bc31
4 changed files with 24 additions and 16 deletions
+5 -2
View File
@@ -181,12 +181,12 @@ def noreply(func):
wrapper.milter_protocol = nr_mask
return wrapper
## Function decorator to set encoding error strategy.
## Function decorator to set decoding error strategy.
# Current RFCs define UTF-8 as the standard encoding for SMTP
# envelope and header fields. By default, Milter.Base decodes
# envelope and header values with errors='surrogateescape'.
# This decorator can change the error strategy to, e.g., 'ignore' or 'replace'.
def encodingerror(strategy):
def decode(strategy):
def setstrategy(func):
func.error_strategy = strategy
return func
@@ -391,6 +391,9 @@ class Base(object):
def header_bytes(self,fld,val):
try:
e = getattr(self.header,'error_strategy','surrogateescape')
if e == 'bytes':
self.header_bytes = self.header
return self.header(fld,val)
s = val.decode(encoding='utf-8',errors=e)
except UnicodeDecodeError: s = val
return self.header(fld,s)
+1 -1
View File
@@ -183,7 +183,7 @@ class TestBase(object):
if rc != Milter.CONTINUE: return rc
# header
for h,val in msg.items():
rc = self.header(h,val)
rc = self.header_bytes(h,val.encode())
if rc != Milter.CONTINUE: return rc
# eoh
self._stage = Milter.M_EOH