[Bug 1818807] [NEW] Overriding functions in the default namespace
Victoria Dlugopolskaya
1818807 at bugs.launchpad.net
Wed Mar 6 10:10:51 UTC 2019
Public bug reported:
If I specify a namespace with a prefix, a custom function works fine:
from lxml import etree
_function_ns = etree.FunctionNamespace('http://example.com')
_function_ns.prefix = 'a'
def _dummy_count(arg):
return 10
_function_ns['count'] = _dummy_count
_xml = etree.Element('structure')
print _xml.xpath('a:count()')
Output is '10.0'.
But if I add the function to the default namespace, I've got an error:
from lxml import etree
_function_ns = etree.FunctionNamespace(None)
def _dummy_count(arg):
return 10
_function_ns['count'] = _dummy_count
_xml = etree.Element('structure')
print _xml.xpath('count()')
Output:
Traceback (most recent call last):
File "/Users/user/Desktop/function-namespace.py", line 7, in <module>
print _xml.xpath('count()')
File "src/lxml/etree.pyx", line 1575, in lxml.etree._Element.xpath
File "src/lxml/xpath.pxi", line 307, in lxml.etree.XPathElementEvaluator.__call__
File "src/lxml/xpath.pxi", line 227, in lxml.etree._XPathEvaluatorBase._handle_result
lxml.etree.XPathEvalError: Invalid number of arguments
Seems it doesn't recognise my 'count(arg)' function and use standard
'count(...)' instead.
It works fine with custom functions that are not conflicting with
standard functions, but how can I specify a custom function with one of
the standard functions name?
---
Python : sys.version_info(major=2, minor=7, micro=15, releaselevel='final', serial=0)
lxml.etree : (4, 3, 0, 0)
libxml used : (2, 9, 9)
libxml compiled : (2, 9, 9)
libxslt used : (1, 1, 32)
libxslt compiled : (1, 1, 32)
** Affects: lxml (Ubuntu)
Importance: Undecided
Status: New
** Description changed:
If I specify a namespace with a prefix, a custom function works fine:
from lxml import etree
_function_ns = etree.FunctionNamespace('http://example.com')
_function_ns.prefix = 'a'
def _dummy_count(arg):
- return 10
+ return 10
_function_ns['count'] = _dummy_count
_xml = etree.Element('structure')
print _xml.xpath('a:count()')
Output is '10.0'.
But if I add the function to the default namespace, I've got an error:
from lxml import etree
_function_ns = etree.FunctionNamespace(None)
def _dummy_count(arg):
- return 10
+ return 10
_function_ns['count'] = _dummy_count
_xml = etree.Element('structure')
print _xml.xpath('count()')
Output:
Traceback (most recent call last):
- File "/Users/user/Desktop/function-namespace.py", line 7, in <module>
- print _xml.xpath('count()')
- File "src/lxml/etree.pyx", line 1575, in lxml.etree._Element.xpath
- File "src/lxml/xpath.pxi", line 307, in lxml.etree.XPathElementEvaluator.__call__
- File "src/lxml/xpath.pxi", line 227, in lxml.etree._XPathEvaluatorBase._handle_result
+ File "/Users/user/Desktop/function-namespace.py", line 7, in <module>
+ print _xml.xpath('count()')
+ File "src/lxml/etree.pyx", line 1575, in lxml.etree._Element.xpath
+ File "src/lxml/xpath.pxi", line 307, in lxml.etree.XPathElementEvaluator.__call__
+ File "src/lxml/xpath.pxi", line 227, in lxml.etree._XPathEvaluatorBase._handle_result
lxml.etree.XPathEvalError: Invalid number of arguments
Seems it doesn't recognise my 'count(arg)' function and use standard
'count(...)' instead.
It works fine with custom functions that are not conflicting with
standard functions, but how can I specify a custom function with one of
the standard functions name?
+
+ ---
+
+ Python : sys.version_info(major=2, minor=7, micro=15, releaselevel='final', serial=0)
+ lxml.etree : (4, 3, 0, 0)
+ libxml used : (2, 9, 9)
+ libxml compiled : (2, 9, 9)
+ libxslt used : (1, 1, 32)
+ libxslt compiled : (1, 1, 32)
--
You received this bug notification because you are a member of Ubuntu
Foundations Bugs, which is subscribed to lxml in Ubuntu.
https://bugs.launchpad.net/bugs/1818807
Title:
Overriding functions in the default namespace
Status in lxml package in Ubuntu:
New
Bug description:
If I specify a namespace with a prefix, a custom function works fine:
from lxml import etree
_function_ns = etree.FunctionNamespace('http://example.com')
_function_ns.prefix = 'a'
def _dummy_count(arg):
return 10
_function_ns['count'] = _dummy_count
_xml = etree.Element('structure')
print _xml.xpath('a:count()')
Output is '10.0'.
But if I add the function to the default namespace, I've got an error:
from lxml import etree
_function_ns = etree.FunctionNamespace(None)
def _dummy_count(arg):
return 10
_function_ns['count'] = _dummy_count
_xml = etree.Element('structure')
print _xml.xpath('count()')
Output:
Traceback (most recent call last):
File "/Users/user/Desktop/function-namespace.py", line 7, in <module>
print _xml.xpath('count()')
File "src/lxml/etree.pyx", line 1575, in lxml.etree._Element.xpath
File "src/lxml/xpath.pxi", line 307, in lxml.etree.XPathElementEvaluator.__call__
File "src/lxml/xpath.pxi", line 227, in lxml.etree._XPathEvaluatorBase._handle_result
lxml.etree.XPathEvalError: Invalid number of arguments
Seems it doesn't recognise my 'count(arg)' function and use standard
'count(...)' instead.
It works fine with custom functions that are not conflicting with
standard functions, but how can I specify a custom function with one
of the standard functions name?
---
Python : sys.version_info(major=2, minor=7, micro=15, releaselevel='final', serial=0)
lxml.etree : (4, 3, 0, 0)
libxml used : (2, 9, 9)
libxml compiled : (2, 9, 9)
libxslt used : (1, 1, 32)
libxslt compiled : (1, 1, 32)
To manage notifications about this bug go to:
https://bugs.launchpad.net/ubuntu/+source/lxml/+bug/1818807/+subscriptions
More information about the foundations-bugs
mailing list