<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div dir="ltr">Hi,</div><div dir="ltr"><br></div><div dir="ltr">not sure has this nothing to do with the error but game to my eye trying to figure out what this bunch of code is really doing...</div><div dir="ltr"><br></div><div dir="ltr">from python re docs:</div><div dir="ltr"><span style="-webkit-text-size-adjust: auto; caret-color: rgb(34, 34, 34); color: rgb(34, 34, 34); font-family: "Lucida Grande", Arial, sans-serif; font-size: 26px; text-align: justify; background-color: rgb(255, 255, 255);"><br></span></div><div dir="ltr"><span style="-webkit-text-size-adjust: auto; caret-color: rgb(34, 34, 34); color: rgb(34, 34, 34); font-family: "Lucida Grande", Arial, sans-serif; font-size: 26px; text-align: justify; background-color: rgb(255, 255, 255);">Regular expressions use the backslash character (</span><code class="docutils literal notranslate" style="-webkit-text-size-adjust: auto; background-color: rgb(236, 240, 243); padding: 0px 1px; font-size: 15.4399995803833px; font-family: monospace, sans-serif; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; caret-color: rgb(34, 34, 34); color: rgb(34, 34, 34); text-align: justify;"><span class="pre" style="-webkit-hyphens: none;">'\'</span></code><span style="-webkit-text-size-adjust: auto; caret-color: rgb(34, 34, 34); color: rgb(34, 34, 34); font-family: "Lucida Grande", Arial, sans-serif; font-size: 26px; text-align: justify; background-color: rgb(255, 255, 255);">) to indicate special forms or to allow special characters to be used without invoking their special meaning. This collides with Python’s usage of the same character for the same purpose in string literals; for example, to match a literal backslash, one might have to write </span><code class="docutils literal notranslate" style="-webkit-text-size-adjust: auto; background-color: rgb(236, 240, 243); padding: 0px 1px; font-size: 15.4399995803833px; font-family: monospace, sans-serif; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; caret-color: rgb(34, 34, 34); color: rgb(34, 34, 34); text-align: justify;"><span class="pre" style="-webkit-hyphens: none;">'\\\\'</span></code><span style="-webkit-text-size-adjust: auto; caret-color: rgb(34, 34, 34); color: rgb(34, 34, 34); font-family: "Lucida Grande", Arial, sans-serif; font-size: 26px; text-align: justify; background-color: rgb(255, 255, 255);"> as the pattern string, because the regular expression must be </span><code class="docutils literal notranslate" style="-webkit-text-size-adjust: auto; background-color: rgb(236, 240, 243); padding: 0px 1px; font-size: 15.4399995803833px; font-family: monospace, sans-serif; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; caret-color: rgb(34, 34, 34); color: rgb(34, 34, 34); text-align: justify;"><span class="pre" style="-webkit-hyphens: none;">\\</span></code><span style="-webkit-text-size-adjust: auto; caret-color: rgb(34, 34, 34); color: rgb(34, 34, 34); font-family: "Lucida Grande", Arial, sans-serif; font-size: 26px; text-align: justify; background-color: rgb(255, 255, 255);">, and each backslash must be expressed as </span><code class="docutils literal notranslate" style="-webkit-text-size-adjust: auto; background-color: rgb(236, 240, 243); padding: 0px 1px; font-size: 15.4399995803833px; font-family: monospace, sans-serif; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; caret-color: rgb(34, 34, 34); color: rgb(34, 34, 34); text-align: justify;"><span class="pre" style="-webkit-hyphens: none;">\\</span></code><span style="-webkit-text-size-adjust: auto; caret-color: rgb(34, 34, 34); color: rgb(34, 34, 34); font-family: "Lucida Grande", Arial, sans-serif; font-size: 26px; text-align: justify; background-color: rgb(255, 255, 255);"> inside a regular Python string literal. Also, please note that any invalid escape sequences in Python’s usage of the backslash in string literals now generate a </span><a class="reference internal" href="https://docs.python.org/3/library/exceptions.html#DeprecationWarning" title="DeprecationWarning" style="-webkit-text-size-adjust: auto; color: rgb(99, 99, 187); text-decoration: none; font-family: "Lucida Grande", Arial, sans-serif; text-align: justify;"><code class="xref py py-exc docutils literal notranslate" style="padding: 0px 1px; font-size: 15.4399995803833px; font-family: monospace, sans-serif; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;"><span class="pre" style="-webkit-hyphens: none;">DeprecationWarning</span></code></a><span style="-webkit-text-size-adjust: auto; caret-color: rgb(34, 34, 34); color: rgb(34, 34, 34); font-family: "Lucida Grande", Arial, sans-serif; font-size: 26px; text-align: justify; background-color: rgb(255, 255, 255);"> and in the future this will become a </span><a class="reference internal" href="https://docs.python.org/3/library/exceptions.html#SyntaxError" title="SyntaxError" style="-webkit-text-size-adjust: auto; color: rgb(99, 99, 187); text-decoration: none; font-family: "Lucida Grande", Arial, sans-serif; text-align: justify;"><code class="xref py py-exc docutils literal notranslate" style="padding: 0px 1px; font-size: 15.4399995803833px; font-family: monospace, sans-serif; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;"><span class="pre" style="-webkit-hyphens: none;">SyntaxError</span></code></a><span style="-webkit-text-size-adjust: auto; caret-color: rgb(34, 34, 34); color: rgb(34, 34, 34); font-family: "Lucida Grande", Arial, sans-serif; font-size: 26px; text-align: justify; background-color: rgb(255, 255, 255);">. This behaviour will happen even if it is a valid escape sequence for a regular expression.</span></div><div dir="ltr"><span style="-webkit-text-size-adjust: auto; caret-color: rgb(34, 34, 34); color: rgb(34, 34, 34); font-family: "Lucida Grande", Arial, sans-serif; font-size: 26px; text-align: justify; background-color: rgb(255, 255, 255);"><br></span></div><div dir="ltr"><span style="-webkit-text-size-adjust: auto; caret-color: rgb(34, 34, 34); color: rgb(34, 34, 34); font-family: "Lucida Grande", Arial, sans-serif; font-size: 26px; text-align: justify; background-color: rgb(255, 255, 255);">The solution is to use Python’s raw string notation for regular expression patterns; backslashes are not handled in any special way in a string literal prefixed with </span><code class="docutils literal notranslate" style="-webkit-text-size-adjust: auto; background-color: rgb(236, 240, 243); padding: 0px 1px; font-size: 15.4399995803833px; font-family: monospace, sans-serif; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; caret-color: rgb(34, 34, 34); color: rgb(34, 34, 34); text-align: justify;"><span class="pre" style="-webkit-hyphens: none;">'r'</span></code><span style="-webkit-text-size-adjust: auto; caret-color: rgb(34, 34, 34); color: rgb(34, 34, 34); font-family: "Lucida Grande", Arial, sans-serif; font-size: 26px; text-align: justify; background-color: rgb(255, 255, 255);">. </span><span style="-webkit-text-size-adjust: auto; caret-color: rgb(34, 34, 34); color: rgb(34, 34, 34); font-family: "Lucida Grande", Arial, sans-serif; font-size: 26px; text-align: justify; background-color: rgb(255, 255, 255);">So </span><code class="docutils literal notranslate" style="-webkit-text-size-adjust: auto; background-color: rgb(236, 240, 243); padding: 0px 1px; font-size: 15.4399995803833px; font-family: monospace, sans-serif; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; caret-color: rgb(34, 34, 34); color: rgb(34, 34, 34); text-align: justify;"><span class="pre" style="-webkit-hyphens: none;">r"\n"</span></code><span style="-webkit-text-size-adjust: auto; caret-color: rgb(34, 34, 34); color: rgb(34, 34, 34); font-family: "Lucida Grande", Arial, sans-serif; font-size: 26px; text-align: justify; background-color: rgb(255, 255, 255);"> is a two-character string containing </span><code class="docutils literal notranslate" style="-webkit-text-size-adjust: auto; background-color: rgb(236, 240, 243); padding: 0px 1px; font-size: 15.4399995803833px; font-family: monospace, sans-serif; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; caret-color: rgb(34, 34, 34); color: rgb(34, 34, 34); text-align: justify;"><span class="pre" style="-webkit-hyphens: none;">'\'</span></code><span style="-webkit-text-size-adjust: auto; caret-color: rgb(34, 34, 34); color: rgb(34, 34, 34); font-family: "Lucida Grande", Arial, sans-serif; font-size: 26px; text-align: justify; background-color: rgb(255, 255, 255);"> and </span><code class="docutils literal notranslate" style="-webkit-text-size-adjust: auto; background-color: rgb(236, 240, 243); padding: 0px 1px; font-size: 15.4399995803833px; font-family: monospace, sans-serif; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; caret-color: rgb(34, 34, 34); color: rgb(34, 34, 34); text-align: justify;"><span class="pre" style="-webkit-hyphens: none;">'n'</span></code><span style="-webkit-text-size-adjust: auto; caret-color: rgb(34, 34, 34); color: rgb(34, 34, 34); font-family: "Lucida Grande", Arial, sans-serif; font-size: 26px; text-align: justify; background-color: rgb(255, 255, 255);">, while </span><code class="docutils literal notranslate" style="-webkit-text-size-adjust: auto; background-color: rgb(236, 240, 243); padding: 0px 1px; font-size: 15.4399995803833px; font-family: monospace, sans-serif; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; caret-color: rgb(34, 34, 34); color: rgb(34, 34, 34); text-align: justify;"><span class="pre" style="-webkit-hyphens: none;">"\n"</span></code><span style="-webkit-text-size-adjust: auto; caret-color: rgb(34, 34, 34); color: rgb(34, 34, 34); font-family: "Lucida Grande", Arial, sans-serif; font-size: 26px; text-align: justify; background-color: rgb(255, 255, 255);">is a one-character string containing a newline.</span></div><div dir="ltr"><br></div><div dir="ltr"><br></div><div dir="ltr">Tomi</div><div dir="ltr"><br>On 16. May 2020, at 1.47, Gordon Yeong <anexiole@gmail.com> wrote:<br><br></div><div dir="ltr"><div dir="auto"><div>It's passed the init. If i did a print (LM_ERR) In the first line of init(), it doesn't show that print. <div dir="auto"><br></div><div dir="auto">If i did a print (LM_ERR)a static string in the get_rn() before the line that calls compile(), i noticed that it does print out. And if i tried to print a static string right after the compile() call, it doesn't print and it calls TypeError.</div><div dir="auto"><br></div><div dir="auto">That's why i suspect something is wrong bit can't tell exactly what....</div><br><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Fri, 15 May 2020, 11:21 pm Ben Newlin, <<a href="mailto:Ben.Newlin@genesys.com">Ben.Newlin@genesys.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div lang="EN-US" link="blue" vlink="purple">
<div class="m_-2158149095968873342WordSection1">
<p class="MsoNormal">Gordon,<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">Are you sure the TypeError is not occurring in mod_init? It looks like you are trying to do a print on a Class definition.<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal"><span style="color:black">Ben Newlin </span><u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<div style="border:none;border-top:solid #b5c4df 1.0pt;padding:3.0pt 0in 0in 0in">
<p class="MsoNormal"><b><span style="font-size:12.0pt;color:black">From: </span></b><span style="font-size:12.0pt;color:black">Users <<a href="mailto:users-bounces@lists.opensips.org" target="_blank" rel="noreferrer">users-bounces@lists.opensips.org</a>> on behalf of Gordon Yeong <<a href="mailto:anexiole@gmail.com" target="_blank" rel="noreferrer">anexiole@gmail.com</a>><br>
<b>Reply-To: </b>OpenSIPS users mailling list <<a href="mailto:users@lists.opensips.org" target="_blank" rel="noreferrer">users@lists.opensips.org</a>><br>
<b>Date: </b>Friday, May 15, 2020 at 1:54 AM<br>
<b>To: </b>OpenSIPS users mailling list <<a href="mailto:users@lists.opensips.org" target="_blank" rel="noreferrer">users@lists.opensips.org</a>><br>
<b>Subject: </b>[OpenSIPS-Users] python module - python_exec() behaviour in opensips<u></u><u></u></span></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<div>
<p class="MsoNormal"><span style="font-family:"Arial",sans-serif;color:#000099">hi guys,<u></u><u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Arial",sans-serif;color:#000099"> Using opensips 3.0 and python3 here.<u></u><u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Arial",sans-serif;color:#000099"><u></u> <u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Arial",sans-serif;color:#000099"> I have a script which parses a number in a string (r'.*rn=\+6114(\d{2})').<u></u><u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Arial",sans-serif;color:#000099"><u></u> <u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Arial",sans-serif;color:#000099">Script is as follows:<u></u><u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Arial",sans-serif;color:#000099">---------- start ---------------<u></u><u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Arial",sans-serif;color:#000099"><u></u> <u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Arial",sans-serif;color:#000099"><u></u> <u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Arial",sans-serif;color:#000099">#!/usr/bin/python3<br>
import re;<br>
import pprint;<br>
<br>
<br>
class test:<br>
<br>
def get_rn():<br>
rnRegExp = rnRegExp = re.compile(r'.*rn=\+6114(\d{2})');<br>
doit = rnRegExp.match( str("sip:+61386947785;npdi;rn=+<a href="mailto:611411@tipt.ns.zippi.vocus.com" target="_blank" rel="noreferrer">611411@tipt.ns.zippi.vocus.com</a>;user=phone"));<br>
<br>
if doit:<br>
pp = pprint.PrettyPrinter(indent=4);<br>
return doit.group(1);<br>
else:<br>
return None;<br>
<br>
result = test.get_rn();<br>
<br>
if result is None:<br>
print("Got no match\n");<br>
else:<br>
print("Result is " + result);<u></u><u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Arial",sans-serif;color:#000099"><u></u> <u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Arial",sans-serif;color:#000099">----------- end --------------<u></u><u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Arial",sans-serif;color:#000099"><u></u> <u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Arial",sans-serif;color:#000099">In my opensips setup, I have the function above looking like this:<u></u><u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Arial",sans-serif;color:#000099"><u></u> <u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Arial",sans-serif;color:#000099">---- start--------<u></u><u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Arial",sans-serif;color:#000099"><br>
from OpenSIPS import LM_ERR<br>
import re;<br>
import pprint;<br>
<br>
def mod_init():<br>
print("In mod_init, SIP MSG is : ");<br>
pp = pprint.PrettyPrinter(indent=4)<br>
pp.pprint(SIPMsg);<br>
return SIPMsg();<br>
<br>
def __init__():<br>
return 1;<br>
<br>
import re;<br>
import pprint;<br>
<br>
class SIPMsg:<br>
<br>
def child_init(self, rank):<br>
return 0<br>
<br>
def get_rn(self, msg, raw_enum_result):<br>
try:<br>
get_rn = re.compile(r'.*rn=\+6114(\d{2})');<br>
<br>
rn = get_rn.match(str(raw_enum_result));<br>
return rn.group(1);<br>
except AttributeError:<br>
# Invalid input string<br>
LM_ERR(" attribute ERRor" );<br>
return '';<br>
except TypeError:<br>
# Invalid input string<br>
LM_ERR(" Type Error" );<br>
return '';<br>
else:<br>
LM_ERR(" general ERRor" );<br>
return '';<u></u><u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Arial",sans-serif;color:#000099"><u></u> <u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Arial",sans-serif;color:#000099">----- end ------<u></u><u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Arial",sans-serif;color:#000099"><u></u> <u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Arial",sans-serif;color:#000099"><u></u> <u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Arial",sans-serif;color:#000099"><u></u> <u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Arial",sans-serif;color:#000099">In my opensips config, I call the get_rn() function via python_exec() as below.<u></u><u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Arial",sans-serif;color:#000099"><u></u> <u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Arial",sans-serif;color:#000099">$var(rn_code)=python_exec("get_rn", $var(raw_string));<u></u><u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Arial",sans-serif;color:#000099"><u></u> <u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Arial",sans-serif;color:#000099">I'm facing an issue here because when I run the script by itself in a command line, it works.<u></u><u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Arial",sans-serif;color:#000099"> No "Error: Text Type" but when my opensips run, it keeps hitting a TypeError.<u></u><u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Arial",sans-serif;color:#000099"> I have even explicitly fed the " rn = get_rn.match(str(raw_enum_result));" with an explicit string (ie. I dont send use the value passed in from the argument BUT instead, just
feed it an explicit string value) and yet the same issue occurs.<u></u><u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Arial",sans-serif;color:#000099"><u></u> <u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Arial",sans-serif;color:#000099">Does processing control belong directly to python3 when we call python_exec() or is there something funny going on here?<u></u><u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Arial",sans-serif;color:#000099"> <u></u><u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Arial",sans-serif;color:#000099">Thank you<u></u><u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Arial",sans-serif;color:#000099"><u></u> <u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Arial",sans-serif;color:#000099"><u></u> <u></u></span></p>
</div>
<div>
<div>
<div>
<div>
<div>
<div>
<div>
<div>
<div>
<div>
<div>
<p class="MsoNormal"><span style="font-family:"Verdana",sans-serif;color:black">Regards,<br>
Gordon </span><u></u><u></u></p>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
_______________________________________________<br>
Users mailing list<br>
<a href="mailto:Users@lists.opensips.org" target="_blank" rel="noreferrer">Users@lists.opensips.org</a><br>
<a href="http://lists.opensips.org/cgi-bin/mailman/listinfo/users" rel="noreferrer noreferrer" target="_blank">http://lists.opensips.org/cgi-bin/mailman/listinfo/users</a><br>
</blockquote></div></div></div>
<span>_______________________________________________</span><br><span>Users mailing list</span><br><span>Users@lists.opensips.org</span><br><span>http://lists.opensips.org/cgi-bin/mailman/listinfo/users</span><br></div></body></html>