[OpenSIPS-Users] python module - python_exec() behaviour in opensips

Tomi Hakkarainen tpaivaa at gmail.com
Sat May 16 08:01:19 EST 2020


Hi,

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...

from python re docs:

Regular expressions use the backslash character ('\') 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 '\\\\' as the pattern string, because the regular expression must be \\, and each backslash must be expressed as \\ 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 DeprecationWarning and in the future this will become a SyntaxError. This behaviour will happen even if it is a valid escape sequence for a regular expression.

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 'r'. So r"\n" is a two-character string containing '\' and 'n', while "\n"is a one-character string containing a newline.


Tomi

On 16. May 2020, at 1.47, Gordon Yeong <anexiole at gmail.com> wrote:


It's passed the init. If i did a print (LM_ERR) In the first line of init(), it doesn't show that print. 

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.

That's why i suspect something is wrong bit can't tell exactly what....


> On Fri, 15 May 2020, 11:21 pm Ben Newlin, <Ben.Newlin at genesys.com> wrote:
> Gordon,
> 
>  
> 
> 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.
> 
>  
> 
> Ben Newlin
> 
>  
> 
> From: Users <users-bounces at lists.opensips.org> on behalf of Gordon Yeong <anexiole at gmail.com>
> Reply-To: OpenSIPS users mailling list <users at lists.opensips.org>
> Date: Friday, May 15, 2020 at 1:54 AM
> To: OpenSIPS users mailling list <users at lists.opensips.org>
> Subject: [OpenSIPS-Users] python module - python_exec() behaviour in opensips
> 
>  
> 
> hi guys,
> 
>  Using opensips 3.0 and python3 here.
> 
>  
> 
>  I have a script which parses a number in a string (r'.*rn=\+6114(\d{2})').
> 
>  
> 
> Script is as follows:
> 
> ---------- start ---------------
> 
>  
> 
>  
> 
> #!/usr/bin/python3
> import re;
> import pprint;
> 
> 
> class test:
> 
>     def get_rn():
>         rnRegExp = rnRegExp = re.compile(r'.*rn=\+6114(\d{2})');
>         doit = rnRegExp.match( str("sip:+61386947785;npdi;rn=+611411 at tipt.ns.zippi.vocus.com;user=phone"));
> 
>         if doit:
>             pp = pprint.PrettyPrinter(indent=4);
>             return doit.group(1);
>         else:
>             return None;
> 
> result = test.get_rn();
> 
> if result is None:
>     print("Got no match\n");
> else:
>     print("Result is " + result);
> 
>  
> 
> ----------- end --------------
> 
>  
> 
> In my opensips setup, I have the function above looking like this:
> 
>  
> 
> ---- start--------
> 
> 
> from OpenSIPS import LM_ERR
> import re;
> import pprint;
> 
> def mod_init():
>     print("In mod_init, SIP MSG is : ");
>     pp = pprint.PrettyPrinter(indent=4)
>     pp.pprint(SIPMsg);
>     return SIPMsg();
> 
> def __init__():
>     return 1;
> 
> import re;
> import pprint;
> 
> class SIPMsg:
> 
>     def child_init(self, rank):
>         return 0
> 
>     def get_rn(self, msg, raw_enum_result):
>         try:
>              get_rn = re.compile(r'.*rn=\+6114(\d{2})');
> 
>              rn = get_rn.match(str(raw_enum_result));
>              return rn.group(1);
>         except AttributeError:
>              # Invalid input string
>              LM_ERR(" attribute ERRor" );
>              return '';
>         except TypeError:
>              # Invalid input string
>              LM_ERR(" Type Error" );
>              return '';
>         else:
>              LM_ERR(" general ERRor" );
>              return '';
> 
>  
> 
> ----- end ------
> 
>  
> 
>  
> 
>  
> 
> In my opensips config, I call the get_rn() function via python_exec() as below.
> 
>  
> 
> $var(rn_code)=python_exec("get_rn", $var(raw_string));
> 
>  
> 
> I'm facing an issue here because when I run the script by itself in a command line, it works.
> 
>  No "Error: Text Type" but when my opensips run, it keeps hitting a TypeError.
> 
>  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.
> 
>  
> 
> Does processing control belong directly to python3 when we call python_exec() or is there something funny going on here?
> 
>  
> 
> Thank you
> 
>  
> 
>  
> 
> Regards,
> Gordon 
> 
> _______________________________________________
> Users mailing list
> Users at lists.opensips.org
> http://lists.opensips.org/cgi-bin/mailman/listinfo/users
_______________________________________________
Users mailing list
Users at lists.opensips.org
http://lists.opensips.org/cgi-bin/mailman/listinfo/users
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.opensips.org/pipermail/users/attachments/20200516/d9f7d923/attachment-0001.html>


More information about the Users mailing list