Access To Bindings In Python

Last modified by Ecaterina Moraru (Valica) on 2026/08/05 17:15

script_errorFixes access to script bindings in python macro for XE < 2.4
TypeSnippet
Category
Developed by

xwiki:XWiki.cjdelisle

Rating
0 Votes
LicenseGNU Lesser General Public License 2.1

Table of contents

Description

Information

Since XWiki 2.4 the bug is fixed and this is no longer needed.

This snippet is a workaround for http://bugs.jython.org/issue1426 Which is discussed in the mailing list here: http://xwiki.markmail.org/message/nuctq6fmjhbgogig

The problem is that the objects which are normally available in scripting:

  • doc - The current document
  • xcontext - The current Context
  • xwiki - The XWiki object
  • request - The current Request
  • response - The current Response

Are not available in Python.

{{python}}
# This python snippet loads the script engine bindings which are unavailable in jython 
# because of http://bugs.jython.org/issue1426
# The only bindings which are loaded are: xcontext, doc, xwiki, request and response
import com.xpn.xwiki.web.Utils as Utils
import org.xwiki.context.Execution as Execution
import com.xpn.xwiki.api.Context as Context
import com.xpn.xwiki.api.XWiki as XWiki
import com.xpn.xwiki.api.Document as Document
xcontext = Context(Utils.getComponent(Execution).getContext().getProperty("xwikicontext"))
doc = Document(xcontext.getDoc(), xcontext.getContext())
xwiki = XWiki(xcontext.getXWiki(), xcontext.getContext())
request = xcontext.getRequest()
response = xcontext.getResponse()

#insert your python code here
{{/python}}

Get Connected