<div dir="ltr"><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;color:#000099">Yeah I looked at that and ran my code on the python3 interpreter - it worked. </div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;color:#000099">Then i put it into a class - it worked.</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;color:#000099"> And then only did I put it (the class) to use in opensips..</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;color:#000099"><br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;color:#000099">Had there been an issue, python3 would have kicked and scream which it did not.</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;color:#000099"><br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;color:#000099"> No issue with the regexp. </div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;color:#000099"> Thank you</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;color:#000099"><br></div><div><div dir="ltr" data-smartmail="gmail_signature"><div dir="ltr"><div dir="ltr"><div dir="ltr"></div></div></div></div></div><font color="#000000" face="verdana, sans-serif">G<span class="gmail_default" style="font-family:arial,helvetica,sans-serif;color:rgb(0,0,153)">ordon</span></font></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sat, 16 May 2020 at 18:03, Tomi Hakkarainen <<a href="mailto:tpaivaa@gmail.com" target="_blank">tpaivaa@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div 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="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="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 style="background-color:rgb(236,240,243);padding:0px 1px;font-size:15.44px;font-family:monospace,sans-serif;border-radius:3px;color:rgb(34,34,34);text-align:justify"><span>'\'</span></code><span style="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 style="background-color:rgb(236,240,243);padding:0px 1px;font-size:15.44px;font-family:monospace,sans-serif;border-radius:3px;color:rgb(34,34,34);text-align:justify"><span>'\\\\'</span></code><span style="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 style="background-color:rgb(236,240,243);padding:0px 1px;font-size:15.44px;font-family:monospace,sans-serif;border-radius:3px;color:rgb(34,34,34);text-align:justify"><span>\\</span></code><span style="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 style="background-color:rgb(236,240,243);padding:0px 1px;font-size:15.44px;font-family:monospace,sans-serif;border-radius:3px;color:rgb(34,34,34);text-align:justify"><span>\\</span></code><span style="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 href="https://docs.python.org/3/library/exceptions.html#DeprecationWarning" title="DeprecationWarning" style="color:rgb(99,99,187);text-decoration:none;font-family:"Lucida Grande",Arial,sans-serif;text-align:justify" target="_blank"><code style="padding:0px 1px;font-size:15.44px;font-family:monospace,sans-serif;border-radius:3px"><span>DeprecationWarning</span></code></a><span style="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 href="https://docs.python.org/3/library/exceptions.html#SyntaxError" title="SyntaxError" style="color:rgb(99,99,187);text-decoration:none;font-family:"Lucida Grande",Arial,sans-serif;text-align:justify" target="_blank"><code style="padding:0px 1px;font-size:15.44px;font-family:monospace,sans-serif;border-radius:3px"><span>SyntaxError</span></code></a><span style="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="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="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 style="background-color:rgb(236,240,243);padding:0px 1px;font-size:15.44px;font-family:monospace,sans-serif;border-radius:3px;color:rgb(34,34,34);text-align:justify"><span>'r'</span></code><span style="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="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 style="background-color:rgb(236,240,243);padding:0px 1px;font-size:15.44px;font-family:monospace,sans-serif;border-radius:3px;color:rgb(34,34,34);text-align:justify"><span>r"\n"</span></code><span style="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 style="background-color:rgb(236,240,243);padding:0px 1px;font-size:15.44px;font-family:monospace,sans-serif;border-radius:3px;color:rgb(34,34,34);text-align:justify"><span>'\'</span></code><span style="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 style="background-color:rgb(236,240,243);padding:0px 1px;font-size:15.44px;font-family:monospace,sans-serif;border-radius:3px;color:rgb(34,34,34);text-align:justify"><span>'n'</span></code><span style="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 style="background-color:rgb(236,240,243);padding:0px 1px;font-size:15.44px;font-family:monospace,sans-serif;border-radius:3px;color:rgb(34,34,34);text-align:justify"><span>"\n"</span></code><span style="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 <<a href="mailto:anexiole@gmail.com" target="_blank">anexiole@gmail.com</a>> 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" target="_blank">Ben.Newlin@genesys.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<div lang="EN-US">
<div>
<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-right:none;border-bottom:none;border-left:none;border-top:1pt solid rgb(181,196,223);padding:3pt 0in 0in">
<p class="MsoNormal"><b><span style="font-size:12pt;color:black">From: </span></b><span style="font-size:12pt;color:black">Users <<a href="mailto:users-bounces@lists.opensips.org" rel="noreferrer" target="_blank">users-bounces@lists.opensips.org</a>> on behalf of Gordon Yeong <<a href="mailto:anexiole@gmail.com" rel="noreferrer" target="_blank">anexiole@gmail.com</a>><br>
<b>Reply-To: </b>OpenSIPS users mailling list <<a href="mailto:users@lists.opensips.org" rel="noreferrer" target="_blank">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" rel="noreferrer" target="_blank">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:rgb(0,0,153)">hi guys,<u></u><u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:Arial,sans-serif;color:rgb(0,0,153)"> 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:rgb(0,0,153)"><u></u> <u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:Arial,sans-serif;color:rgb(0,0,153)"> 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:rgb(0,0,153)"><u></u> <u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:Arial,sans-serif;color:rgb(0,0,153)">Script is as follows:<u></u><u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:Arial,sans-serif;color:rgb(0,0,153)">---------- start ---------------<u></u><u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:Arial,sans-serif;color:rgb(0,0,153)"><u></u> <u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:Arial,sans-serif;color:rgb(0,0,153)"><u></u> <u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:Arial,sans-serif;color:rgb(0,0,153)">#!/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" rel="noreferrer" target="_blank">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:rgb(0,0,153)"><u></u> <u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:Arial,sans-serif;color:rgb(0,0,153)">----------- end --------------<u></u><u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:Arial,sans-serif;color:rgb(0,0,153)"><u></u> <u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:Arial,sans-serif;color:rgb(0,0,153)">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:rgb(0,0,153)"><u></u> <u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:Arial,sans-serif;color:rgb(0,0,153)">---- start--------<u></u><u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:Arial,sans-serif;color:rgb(0,0,153)"><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:rgb(0,0,153)"><u></u> <u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:Arial,sans-serif;color:rgb(0,0,153)">----- end ------<u></u><u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:Arial,sans-serif;color:rgb(0,0,153)"><u></u> <u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:Arial,sans-serif;color:rgb(0,0,153)"><u></u> <u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:Arial,sans-serif;color:rgb(0,0,153)"><u></u> <u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:Arial,sans-serif;color:rgb(0,0,153)">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:rgb(0,0,153)"><u></u> <u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:Arial,sans-serif;color:rgb(0,0,153)">$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:rgb(0,0,153)"><u></u> <u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:Arial,sans-serif;color:rgb(0,0,153)">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:rgb(0,0,153)"> 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:rgb(0,0,153)"> 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:rgb(0,0,153)"><u></u> <u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:Arial,sans-serif;color:rgb(0,0,153)">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:rgb(0,0,153)"> <u></u><u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:Arial,sans-serif;color:rgb(0,0,153)">Thank you<u></u><u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:Arial,sans-serif;color:rgb(0,0,153)"><u></u> <u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:Arial,sans-serif;color:rgb(0,0,153)"><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" rel="noreferrer" target="_blank">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><a href="mailto:Users@lists.opensips.org" target="_blank">Users@lists.opensips.org</a></span><br><span><a href="http://lists.opensips.org/cgi-bin/mailman/listinfo/users" target="_blank">http://lists.opensips.org/cgi-bin/mailman/listinfo/users</a></span><br></div></div>_______________________________________________<br>
Users mailing list<br>
<a href="mailto:Users@lists.opensips.org" target="_blank">Users@lists.opensips.org</a><br>
<a href="http://lists.opensips.org/cgi-bin/mailman/listinfo/users" rel="noreferrer" target="_blank">http://lists.opensips.org/cgi-bin/mailman/listinfo/users</a><br>
</blockquote></div>