The sun never sets on the Python empire

Making the Move

In what is not the first, but is hopefully the last attempt at getting the Asterisk testsuite to run on Python3 we’ve gone ahead and created development branches for Asterisk, Starpy and the Testsuite itself.

For Starpy the branch is 1.1, for Asterisk and the Testsuite we are using development/16/python3.  Starpy had previously used the now reserved word ‘async’ in its AMI manager interface.  This has been renamed to ‘nowait’ but the change causes a break in compatibility with the Testsuite so if working on the development branch, please make sure that you are using the 1.1 branch of Starpy.

If you ever want to take a look at the current development branches on Gerrit, you can do a search for branches starting with dev:

https://gerrit.asterisk.org/admin/repos/testsuite,branches/q/filter:dev
https://gerrit.asterisk.org/admin/repos/asterisk,branches/q/filter:dev

To keep the actual running tests happy, we want the test suite and asterisk to have matching development branch names.  This search will show all development branches and we plan to use this naming mechanism on larger features going forward.

To review any open submissions to the Testsuite on this branch, you can use the following Gerrit search:

https://gerrit.asterisk.org/q/project:testsuite+branch:development%252F16%252Fpython3+status:open

While most of the changes for Python3 compatibility revolves around the switch from print as a statement to a function and the replacement of deprecated libraries, even more pervasive are conflicts between bytes and strings due to Python twisted’s extensive use of the former.  In the good news category, the latest version of twisted (at time of writing 22.1) includes support for python 3.8, so luckily we can use it off-the-shelf.

Our issues start to arrive with our use of the twisted library as Starpy and the Testsuite previously had treated strings and byes interchangeably.  Converting between the two is relatively easy, but we must make sure that we are doing it consistently both in method and coverage.  This means we both want to use an agreed upon method to do the conversion and want to make sure that we are converting exclusively in the appropriate places.

For instance, knowing that we want to build a bytes object to pass to twisted’s basic sendLine is straightforward.  However, realizing that overloading the delimiter to a string can cause an error within twisted itself is less so.  If we make sure that our variables are initialized to the correct type for their use it makes for a lot less work.  Conversely, when we are building a url out of existing strings we only need to do the byte conversion once after we have put all the pieces together.

More work will continue to be done in this area, but it’s not all that’s left.  We also need to move to PJSUA 2 to avoid having to patch indefinitely.

PJSUA 2

The testsuite uses the PJSUA python extensions via the PJSUA interface and phone modules.  These modules allow the Testsuite to create user accounts and simulate a corresponding SIP user agent.  This overlaps with the use of SIPp, but doesn’t require the creation of scenario files and allows for a simpler event-based test case.  The current PJSUA Python libraries aren’t compatible with Python3 and there is no plan for it to.

Running with Python 3

Testing with Python 3 while still having 2 installed isn’t too complicated, but we want to make sure we aren’t mixing the two.  You need to have the latest Python 3 compatible version of all the required libraries.  If you have Python 2 also installed, make sure that you are specifying Python3 when building/installing and using pip3 instead of pip2.

The Current State

…is in flux.  Please, we encourage taking a peak at the development branch and giving it a go on your test system.  Running the following will let you know which tests your environment is set up to run:

python3 ./runtests.py -l

Thanks and we look forward to hearing from you!

About the Author

What can we help you find?