<commit_before># -*- Mode: Python -*-
# vi:si:et:sw=4:sts=4:ts=4
#
# gst-python
# Copyright (C) 2002 <NAME>
#               2004 <NAME>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Library General Public License for more details.
#
# You should have received a copy of the GNU Library General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
#
# Author: <NAME> <<EMAIL>>

from argtypes import UInt64Arg, Int64Arg, PointerArg, ArgMatcher, ArgType, matcher
from reversewrapper import Parameter, ReturnType, GBoxedParam, GBoxedReturn, IntParam, IntReturn

class XmlNodeArg(ArgType):
	"""libxml2 node generator"""

	names = {"xobj":"xmlNode",
			"xptr":"xmlNodePtr",
			"xwrap":"libxml_xmlNodePtrWrap"}

	parm = ('    if(xml == NULL) return NULL;\n'
			'    xobj = PyObject_GetAttrString(xml, "%(xobj)s");\n'
			'    if(!PyObject_IsInstance(py%(name)s, xobj)) {\n'
			'        PyErr_Clear();\n'
			'        PyErr_SetString(PyExc_RuntimeError,"%(name)s is not a %(xobj)s instance");\n'
			'        Py_DECREF(xobj);Py_DECREF(xml);\n'
			'        return NULL;\n'
			'    }\n'
			'    o = PyObject_GetAttrString(py%(name)s, "_o");\n'
			'    %(name)s = PyCObject_AsVoidPtr(o);\n')
	parmp = ('    Py_DECREF(o); Py_DECREF(xobj);Py_DECREF(xml);\n')

	ret =  ('    if(xml == NULL) return NULL;\n')
	retp = ('    xargs = PyTuple_New(1);\n'
			'    xobj = PyObject_GetAttrString(xml, "%(xobj)s");\n'
			'    o = %(xwrap)s(ret);\n'
			'    PyTuple_SetItem(xargs, 0, o);\n'
			'    return PyInstance_New(xobj, xargs, PyDict_New());\n')

	def write_param(self, ptype, pname, pdflt, pnull, keeprefcount, info):
		info.varlist.add('PyObject', '*xml = _gst_get_libxml2_module()')
		info.varlist.add('PyObject', '*o')
		info.varlist.add('PyObject', '*xobj')
		info.varlist.add('PyObject', '*py' + pname)
		info.varlist.add(self.names["xptr"], pname)
		#if pnull:
		info.add_parselist('O', ['&py'+pname], [pname])
		info.arglist.append(pname)
		self.names["name"] = pname
		info.codebefore.append(self.parm % self.names)
		info.codeafter.append(self.parmp % self.names);
	def write_return(self, ptype, ownsreturn, info):
		info.varlist.add('PyObject', '*xml = _gst_get_libxml2_module()')
		info.varlist.add('PyObject', '*xargs')
		info.varlist.add('PyObject', '*xobj')
		info.varlist.add('PyObject', '*o')
		info.varlist.add(self.names["xptr"], 'ret')
		info.codebefore.append(self.ret % self.names)
		info.codeafter.append(self.retp % self.names)

class XmlDocArg(XmlNodeArg):
	"""libxml2 doc generator"""
	names = {"xobj":"xmlDoc",
			"xptr":"xmlDocPtr",
			"xwrap":"libxml_xmlDocPtrWrap"}

class GstCapsArg(ArgType):
	"""GstCaps node generator"""

	before = ('    %(name)s = pygst_caps_from_pyobject (py_%(name)s, %(namecopy)s);\n'
		  '    if (PyErr_Occurred())\n'
		  '      return NULL;\n')
	beforenull = ('    if (py_%(name)s == Py_None || py_%(name)s == NULL)\n'
		      '        %(name)s = NULL;\n'
		      '    else\n'
		      '  ' + before)
	after = ('    if (%(name)s && %(name)s_is_copy)\n'
		 '        gst_caps_unref (%(name)s);\n')

	def write_param(self, ptype, pname, pdflt, pnull, keeprefcount, info):
		if ptype == 'const-GstCaps*':
			self.write_const_param(pname, pdflt, pnull, info)
		elif ptype == 'GstCaps*':
			self.write_normal_param(pname, pdflt, pnull, info)
		else:
			raise RuntimeError, "write_param not implemented for %s" % ptype

	def write_const_param(self, pname, pdflt, pnull, info):
		if pdflt:
			assert pdflt == 'NULL'
			info.varlist.add('PyObject', '*py_' + pname + ' = NULL')
		else:
			info.varlist.add('PyObject', '*py_' + pname)
		info.varlist.add('GstCaps', '*'+pname)
		info.varlist.add('gboolean', pname+'_is_copy')
		info.add_parselist('O', ['&py_'+pname], [pname])
		info.arglist.append(pname)
		if pnull:
			info.codebefore.append (self.beforenull % { 'name' : pname, 'namecopy' : '&'+pname+'_is_copy' })
		else:
			info.codebefore.append (self.before % { 'name' : pname, 'namecopy' : '&'+pname+'_is_copy' })
		info.codeafter.append (self.after % { 'name' : pname, 'namecopy' : '&'+pname+'_is_copy' })

	def write_normal_param(self, pname, pdflt, pnull, info):
		if pdflt:
			assert pdflt == 'NULL'
			info.varlist.add('PyObject', '*py_' + pname + ' = NULL')
		else:
			info.varlist.add('PyObject', '*py_' + pname)
		info.varlist.add('GstCaps', '*'+pname)
		info.add_parselist('O', ['&py_'+pname], [pname])
		info.arglist.append(pname)
		if pnull:
			info.codebefore.append (self.beforenull % { 'name' : pname, 'namecopy' : 'NULL' })
		else:
			info.codebefore.append (self.before % { 'name' : pname, 'namecopy' : 'NULL' })

	def write_return(self, ptype, ownsreturn, info):
		if ptype == 'GstCaps*':
			info.varlist.add('GstCaps', '*ret')
			copyval = 'FALSE'
		elif ptype == 'const-GstCaps*':
			info.varlist.add('const GstCaps', '*ret')
			copyval = 'TRUE'
		else:
			raise RuntimeError, "write_return not implemented for %s" % ptype
		info.codeafter.append('    return pyg_boxed_new (GST_TYPE_CAPS, ret, '+copyval+', TRUE);')

class GstIteratorArg(ArgType):
	def write_return(self, ptype, ownsreturn, info):
		info.varlist.add('GstIterator', '*ret')
		info.codeafter.append('    return pygst_iterator_new(ret);')

class GstRTSPUrlArg(ArgType):
	"""GstRTSPUrl node generator"""

	before = ('    parse_result = gst_rtsp_url_parse (py_%(name)s, &%(name)s);\n'
		  '    if (parse_result != GST_RTSP_OK) {\n'
          '      PyErr_SetString(PyExc_TypeError, "invalid url");\n'
		  '      return NULL;\n'
          '    }')
	beforenull = ('    if (py_%(name)s == NULL)\n'
		      '        %(name)s = NULL;\n'
		      '    else\n'
		      '  ' + before)
	after = ('    if (%(name)s)\n'
		 '        gst_rtsp_url_free (%(name)s);\n')

	def write_param(self, ptype, pname, pdflt, pnull, keeprefcount, info):
		if ptype in ('const-GstRTSPUrl*', 'GstRTSPUrl*'):
			self.write_normal_param(pname, pdflt, pnull, info)
		else:
			raise RuntimeError, "write_param not implemented for %s" % ptype

	def write_normal_param(self, pname, pdflt, pnull, info):
		info.varlist.add('GstRTSPResult', 'parse_result')
		if pdflt:
			assert pdflt == 'NULL'
			info.varlist.add('const char', '*py_' + pname + ' = NULL')
		else:
			info.varlist.add('const char', '*py_' + pname)
		info.varlist.add('GstRTSPUrl', '*'+pname)
		info.add_parselist('s', ['&py_'+pname], [pname])
		info.arglist.append(pname)
		if pnull:
			info.codebefore.append (self.beforenull % { 'name' : pname  })
		else:
			info.codebefore.append (self.before % { 'name' : pname })
		info.codeafter.append (self.after % { 'name' : pname })

	def write_return(self, ptype, ownsreturn, info):
		if ptype == 'GstRTSPUrl*':
			info.varlist.add('GstRTSPUrl', '*ret')
			copyval = 'FALSE'
		elif ptype == 'const-GstRTSPUrl*':
			info.varlist.add('const GstRTSPUrl', '*ret')
			copyval = 'TRUE'
		else:
			raise RuntimeError, "write_return not implemented for %s" % ptype
		info.codeafter.append('    return pyg_boxed_new (GST_TYPE_CAPS, ret, '+copyval+', TRUE);')

class GstMiniObjectParam(Parameter):

	def get_c_type(self):
		return self.props.get('c_type', 'GstMiniObject *')

	def convert_c2py(self):
		self.wrapper.add_declaration("PyObject *py_%s = NULL;" % self.name)<commit_msg>python bindings: fix returning GstRTSPUrl types<commit_after># -*- Mode: Python -*-
# vi:si:et:sw=4:sts=4:ts=4
#
# gst-python
# Copyright (C) 2002 <NAME>
#               2004 <NAME>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Library General Public License for more details.
#
# You should have received a copy of the GNU Library General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
#
# Author: <NAME> <<EMAIL>>

from argtypes import UInt64Arg, Int64Arg, PointerArg, ArgMatcher, ArgType, matcher
from reversewrapper import Parameter, ReturnType, GBoxedParam, GBoxedReturn, IntParam, IntReturn

class XmlNodeArg(ArgType):
	"""libxml2 node generator"""

	names = {"xobj":"xmlNode",
			"xptr":"xmlNodePtr",
			"xwrap":"libxml_xmlNodePtrWrap"}

	parm = ('    if(xml == NULL) return NULL;\n'
			'    xobj = PyObject_GetAttrString(xml, "%(xobj)s");\n'
			'    if(!PyObject_IsInstance(py%(name)s, xobj)) {\n'
			'        PyErr_Clear();\n'
			'        PyErr_SetString(PyExc_RuntimeError,"%(name)s is not a %(xobj)s instance");\n'
			'        Py_DECREF(xobj);Py_DECREF(xml);\n'
			'        return NULL;\n'
			'    }\n'
			'    o = PyObject_GetAttrString(py%(name)s, "_o");\n'
			'    %(name)s = PyCObject_AsVoidPtr(o);\n')
	parmp = ('    Py_DECREF(o); Py_DECREF(xobj);Py_DECREF(xml);\n')

	ret =  ('    if(xml == NULL) return NULL;\n')
	retp = ('    xargs = PyTuple_New(1);\n'
			'    xobj = PyObject_GetAttrString(xml, "%(xobj)s");\n'
			'    o = %(xwrap)s(ret);\n'
			'    PyTuple_SetItem(xargs, 0, o);\n'
			'    return PyInstance_New(xobj, xargs, PyDict_New());\n')

	def write_param(self, ptype, pname, pdflt, pnull, keeprefcount, info):
		info.varlist.add('PyObject', '*xml = _gst_get_libxml2_module()')
		info.varlist.add('PyObject', '*o')
		info.varlist.add('PyObject', '*xobj')
		info.varlist.add('PyObject', '*py' + pname)
		info.varlist.add(self.names["xptr"], pname)
		#if pnull:
		info.add_parselist('O', ['&py'+pname], [pname])
		info.arglist.append(pname)
		self.names["name"] = pname
		info.codebefore.append(self.parm % self.names)
		info.codeafter.append(self.parmp % self.names);
	def write_return(self, ptype, ownsreturn, info):
		info.varlist.add('PyObject', '*xml = _gst_get_libxml2_module()')
		info.varlist.add('PyObject', '*xargs')
		info.varlist.add('PyObject', '*xobj')
		info.varlist.add('PyObject', '*o')
		info.varlist.add(self.names["xptr"], 'ret')
		info.codebefore.append(self.ret % self.names)
		info.codeafter.append(self.retp % self.names)

class XmlDocArg(XmlNodeArg):
	"""libxml2 doc generator"""
	names = {"xobj":"xmlDoc",
			"xptr":"xmlDocPtr",
			"xwrap":"libxml_xmlDocPtrWrap"}

class GstCapsArg(ArgType):
	"""GstCaps node generator"""

	before = ('    %(name)s = pygst_caps_from_pyobject (py_%(name)s, %(namecopy)s);\n'
		  '    if (PyErr_Occurred())\n'
		  '      return NULL;\n')
	beforenull = ('    if (py_%(name)s == Py_None || py_%(name)s == NULL)\n'
		      '        %(name)s = NULL;\n'
		      '    else\n'
		      '  ' + before)
	after = ('    if (%(name)s && %(name)s_is_copy)\n'
		 '        gst_caps_unref (%(name)s);\n')

	def write_param(self, ptype, pname, pdflt, pnull, keeprefcount, info):
		if ptype == 'const-GstCaps*':
			self.write_const_param(pname, pdflt, pnull, info)
		elif ptype == 'GstCaps*':
			self.write_normal_param(pname, pdflt, pnull, info)
		else:
			raise RuntimeError, "write_param not implemented for %s" % ptype

	def write_const_param(self, pname, pdflt, pnull, info):
		if pdflt:
			assert pdflt == 'NULL'
			info.varlist.add('PyObject', '*py_' + pname + ' = NULL')
		else:
			info.varlist.add('PyObject', '*py_' + pname)
		info.varlist.add('GstCaps', '*'+pname)
		info.varlist.add('gboolean', pname+'_is_copy')
		info.add_parselist('O', ['&py_'+pname], [pname])
		info.arglist.append(pname)
		if pnull:
			info.codebefore.append (self.beforenull % { 'name' : pname, 'namecopy' : '&'+pname+'_is_copy' })
		else:
			info.codebefore.append (self.before % { 'name' : pname, 'namecopy' : '&'+pname+'_is_copy' })
		info.codeafter.append (self.after % { 'name' : pname, 'namecopy' : '&'+pname+'_is_copy' })

	def write_normal_param(self, pname, pdflt, pnull, info):
		if pdflt:
			assert pdflt == 'NULL'
			info.varlist.add('PyObject', '*py_' + pname + ' = NULL')
		else:
			info.varlist.add('PyObject', '*py_' + pname)
		info.varlist.add('GstCaps', '*'+pname)
		info.add_parselist('O', ['&py_'+pname], [pname])
		info.arglist.append(pname)
		if pnull:
			info.codebefore.append (self.beforenull % { 'name' : pname, 'namecopy' : 'NULL' })
		else:
			info.codebefore.append (self.before % { 'name' : pname, 'namecopy' : 'NULL' })

	def write_return(self, ptype, ownsreturn, info):
		if ptype == 'GstCaps*':
			info.varlist.add('GstCaps', '*ret')
			copyval = 'FALSE'
		elif ptype == 'const-GstCaps*':
			info.varlist.add('const GstCaps', '*ret')
			copyval = 'TRUE'
		else:
			raise RuntimeError, "write_return not implemented for %s" % ptype
		info.codeafter.append('    return pyg_boxed_new (GST_TYPE_CAPS, ret, '+copyval+', TRUE);')

class GstIteratorArg(ArgType):
	def write_return(self, ptype, ownsreturn, info):
		info.varlist.add('GstIterator', '*ret')
		info.codeafter.append('    return pygst_iterator_new(ret);')

class GstRTSPUrlArg(ArgType):
	"""GstRTSPUrl node generator"""

	before = ('    parse_result = gst_rtsp_url_parse (py_%(name)s, &%(name)s);\n'
		  '    if (parse_result != GST_RTSP_OK) {\n'
          '      PyErr_SetString(PyExc_TypeError, "invalid url");\n'
		  '      return NULL;\n'
          '    }')
	beforenull = ('    if (py_%(name)s == NULL)\n'
		      '        %(name)s = NULL;\n'
		      '    else\n'
		      '  ' + before)
	after = ('    if (%(name)s)\n'
		 '        gst_rtsp_url_free (%(name)s);\n')

	def write_param(self, ptype, pname, pdflt, pnull, keeprefcount, info):
		if ptype in ('const-GstRTSPUrl*', 'GstRTSPUrl*'):
			self.write_normal_param(pname, pdflt, pnull, info)
		else:
			raise RuntimeError, "write_param not implemented for %s" % ptype

	def write_normal_param(self, pname, pdflt, pnull, info):
		info.varlist.add('GstRTSPResult', 'parse_result')
		if pdflt:
			assert pdflt == 'NULL'
			info.varlist.add('const char', '*py_' + pname + ' = NULL')
		else:
			info.varlist.add('const char', '*py_' + pname)
		info.varlist.add('GstRTSPUrl', '*'+pname)
		info.add_parselist('s', ['&py_'+pname], [pname])
		info.arglist.append(pname)
		if pnull:
			info.codebefore.append (self.beforenull % { 'name' : pname  })
		else:
			info.codebefore.append (self.before % { 'name' : pname })
		info.codeafter.append (self.after % { 'name' : pname })

	def write_return(self, ptype, ownsreturn, info):
		if ptype == 'GstRTSPUrl*':
			info.varlist.add('GstRTSPUrl', '*ret')
			copyval = 'FALSE'
		elif ptype == 'const-GstRTSPUrl*':
			info.varlist.add('const GstRTSPUrl', '*ret')
			copyval = 'TRUE'
		else:
			raise RuntimeError, "write_return not implemented for %s" % ptype
		info.codeafter.append('    return pyg_boxed_new (GST_TYPE_RTSP_URL, ret, '+copyval+', TRUE);')

class GstMiniObjectParam(Parameter):

	def get_c_type(self):
		return self.props.get('c_type', 'GstMiniObject *')

	def convert_c2py(self):
		self.wrapper.add_declaration("PyObject *py_%s = NULL;" % self.name)
{'fraction_non_alphanumeric': 0.17251461988304093, 'fraction_numerical': 0.004808317089018844, 'mean_word_length': 4.000324886289799, 'pattern_counts': {'":': 12, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 40}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4642563', 'n_tokens_mistral': 6370, 'n_tokens_neox': 5871, 'n_words': 1236}