Python 2 and Python 3 differences

You must use Python 3 to write new scripts, or to edit existing scripts. For more information about the differences between Python 2 and Python 3, see What's New in Python 3.0 in the Python documentation.

If you are familiar with writing scripts in Python 2, there are some changes in Python 3 that you should know about before you convert your SOAR scripts.

Importing modules

You can import the following Python modules when you write a script.
  • array
  • base64
  • bs4
  • calendar
  • collections
  • datetime
  • email
  • enum
  • hashlib
  • html
  • html2text
  • json
  • random
  • re
  • regex
  • string
  • time
  • xml
In addition, the following Python built-ins are available for Python 3 scripts only.
  • all()
  • any()
  • bytearray()
  • bytes()
  • classmethod
  • staticmethod
  • type()

Types of errors

The Python 3 implementation introduces two new error types.
  • ResilientSecurityException occurs if a script attempts to breach the security restrictions.
  • ResilientMemoryLimitException occurs if the script tries to allocate more than the maximum amount of RAM allowed (64 MB).

'from' keyword

The from keyword in Python 3 clashes with the from attribute in the email message context object. For this reason, the use of emailmessage.from is not supported in Python 3 scripts and needs to be replaced by emailmessage.sender.

Nonexistent fields

In Python 2, attempting to access a context object attribute that does not exist succeeds and returns None. However, in Python 3, the same operation produces an attribute error, which states that the field name is invalid. This behavior is in line with standard Python 3. The hasattr() method can be used to check whether an attribute exists. For example,
if hasattr(incident, 'nonExistentField'):
  log.info(incident.nonExistentField)
else:
  log.info('Tried to access a field that does not exist')
The script returns the following log message.
INFO: Cannot access a field that does not exist.

Unicode support

Python 3 supports Unicode out of the box, which means that you no longer need to explicitly use the u prefix or the unicode() function to store a string as Unicode. If you convert existing scripts from Python 2 to Python 3, you must check and remove these elements to avoid unicode-related errors in Python 3. For more information, see the Python 3 documentation https://docs.python.org/3.6/howto/unicode.html.

Text object data types

Text object data types behave differently in Python 2 and Python 3. These differences do not necessarily break scripts that are converted from Python 2 to Python 3 but might be of interest.
  • In Python 2, if you use a simple string to set the value of a text-area field, the value is stored as a Unicode object. If you set it using a helper function, the value is stored as a SimpleTextContentDTO.

    In Python 3, whether you use a simple string or a helper function to set the value of a text-area field, the value is stored as a TextObject.

  • In Python 2, the format for objects of type SimpleTextContentDTO can be TEXT or HTML, depending on the helper that is used to set the value.

    In Python 3, the default format for objects of type TextObject is ‘html’. The format is set to 'text' only if the field value is set by running helper.createPlainText().

You can explore the differences in Python versions by running the following script and checking the log messages.
incident.description = 'set direct'
log.info(type(incident.description)) 

incident.description = helper.createPlainText('set using createPlainText helper') 
log.info(type(incident.description)) 

incident.description = helper.createRichText('set using createRichText helper') 
log.info(type(incident.description))