From cef87f4241fb19789a36f45df2f8ebdd51f835e2 Mon Sep 17 00:00:00 2001 From: TuxPowered Date: Sat, 14 Nov 2015 03:00:29 -0800 Subject: [PATCH 1/3] Force quoted string to ascii If setting variables to non-ascii values (such as reading data from a database) your script will fail due to UnicodeEncodeError. My forcing the encoding of the string at the 'quote' to ascii and ignoring non-ascii chars, we are able to prevent abnormal script exits due to quoted string chars. --- asterisk/agi.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/asterisk/agi.py b/asterisk/agi.py index 8065cde..72f94da 100644 --- a/asterisk/agi.py +++ b/asterisk/agi.py @@ -113,7 +113,7 @@ class AGI: sys.stderr.write('\n') def _quote(self, string): - return ''.join(['"', str(string), '"']) + return ''.join(['"', string.encode('ascii', 'ignore'), '"']) def _handle_sighup(self, signum, frame): """Handle the SIGHUP signal""" From 1481fb215f1bf22e404a329e764597e750615c24 Mon Sep 17 00:00:00 2001 From: TuxPowered Date: Sat, 14 Nov 2015 12:26:07 -0800 Subject: [PATCH 2/3] Updated string quoting Converts passed integers and bool to string, used to prevent string.encode() from erroring. --- asterisk/agi.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/asterisk/agi.py b/asterisk/agi.py index 72f94da..d1dc39a 100644 --- a/asterisk/agi.py +++ b/asterisk/agi.py @@ -113,6 +113,9 @@ class AGI: sys.stderr.write('\n') def _quote(self, string): + """ provides double quotes to string, converts int/bool to string """ + if isinstance(string, int): + string = str(string) return ''.join(['"', string.encode('ascii', 'ignore'), '"']) def _handle_sighup(self, signum, frame): From 6b2c4dac7b92d9a5812eb010699f352c7fd15a56 Mon Sep 17 00:00:00 2001 From: TuxPowered Date: Sat, 14 Nov 2015 13:13:57 -0800 Subject: [PATCH 3/3] Convert Float to string Corrected where float values passed to _quote need to be converted to string to support .encode() --- asterisk/agi.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/asterisk/agi.py b/asterisk/agi.py index d1dc39a..aa0ba35 100644 --- a/asterisk/agi.py +++ b/asterisk/agi.py @@ -116,6 +116,8 @@ class AGI: """ provides double quotes to string, converts int/bool to string """ if isinstance(string, int): string = str(string) + if isinstance(string, float): + string = str(string) return ''.join(['"', string.encode('ascii', 'ignore'), '"']) def _handle_sighup(self, signum, frame):