~ubuntu-branches/ubuntu/saucy/lxml/saucy-updates

« back to all changes in this revision

Viewing changes to src/lxml/nsclasses.pxi

  • Committer: Package Import Robot
  • Author(s): Matthias Klose
  • Date: 2012-10-19 19:02:57 UTC
  • mfrom: (1.3.9)
  • Revision ID: package-import@ubuntu.com-20121019190257-ryczr7c7lbgrvi9h
Tags: 3.0.1-0ubuntu1
New upstream version.

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
    pass
12
12
 
13
13
 
 
14
@cython.internal
14
15
cdef class _NamespaceRegistry:
15
16
    u"Dictionary-like namespace registry"
16
17
    cdef object _ns_uri
17
 
    cdef object _ns_uri_utf
 
18
    cdef bytes _ns_uri_utf
18
19
    cdef dict _entries
19
20
    cdef char* _c_ns_uri_utf
20
21
    def __cinit__(self, ns_uri):
32
33
 
33
34
        Forgivingly update the registry.
34
35
 
35
 
        If registered values do not match the required type for this
36
 
        registry, or if their name starts with '_', they will be
37
 
        silently discarded. This allows registrations at the module or
38
 
        class level using vars(), globals() etc."""
 
36
        ``class_dict_iterable`` may be a dict or some other iterable
 
37
        that yields (name, value) pairs.
 
38
 
 
39
        If a value does not match the required type for this registry,
 
40
        or if the name starts with '_', it will be silently discarded.
 
41
        This allows registrations at the module or class level using
 
42
        vars(), globals() etc."""
39
43
        if hasattr(class_dict_iterable, u'items'):
40
44
            class_dict_iterable = class_dict_iterable.items()
41
45
        for name, item in class_dict_iterable:
42
 
            if (name is None or name[:1] != u'_') and callable(item):
 
46
            if (name is None or name[:1] != '_') and callable(item):
43
47
                self[name] = item
44
48
 
45
49
    def __getitem__(self, name):
76
80
        return iter(self._entries.items())
77
81
 
78
82
    def clear(self):
79
 
        python.PyDict_Clear(self._entries)
 
83
        self._entries.clear()
80
84
 
 
85
@cython.final
 
86
@cython.internal
81
87
cdef class _ClassNamespaceRegistry(_NamespaceRegistry):
82
88
    u"Dictionary-like registry for namespace implementation classes"
83
89
    def __setitem__(self, name, item):
128
134
    cdef python.PyObject* dict_result
129
135
    cdef ElementNamespaceClassLookup lookup
130
136
    cdef _NamespaceRegistry registry
131
 
    cdef char* c_namespace_utf
132
137
    if state is None:
133
138
        return _lookupDefaultElementClass(None, doc, c_node)
134
139
 
139
144
    c_namespace_utf = _getNs(c_node)
140
145
    if c_namespace_utf is not NULL:
141
146
        dict_result = python.PyDict_GetItem(
142
 
            lookup._namespace_registries, c_namespace_utf)
 
147
            lookup._namespace_registries, <unsigned char*>c_namespace_utf)
143
148
    else:
144
149
        dict_result = python.PyDict_GetItem(
145
150
            lookup._namespace_registries, None)
149
154
 
150
155
        if c_node.name is not NULL:
151
156
            dict_result = python.PyDict_GetItem(
152
 
                classes, c_node.name)
 
157
                classes, <unsigned char*>c_node.name)
153
158
        else:
154
159
            dict_result = NULL
155
160
 
175
180
 
176
181
    Creates a new one if it does not yet exist. A function namespace
177
182
    can only be used to register extension functions."""
178
 
    if ns_uri:
179
 
        ns_utf = _utf8(ns_uri)
180
 
    else:
181
 
        ns_utf = None
 
183
    ns_utf = _utf8(ns_uri) if ns_uri else None
182
184
    try:
183
185
        return __FUNCTION_NAMESPACE_REGISTRIES[ns_utf]
184
186
    except KeyError:
186
188
                   _XPathFunctionNamespaceRegistry(ns_uri)
187
189
        return registry
188
190
 
 
191
@cython.internal
189
192
cdef class _FunctionNamespaceRegistry(_NamespaceRegistry):
190
193
    def __setitem__(self, name, item):
191
194
        if not callable(item):
199
202
    def __repr__(self):
200
203
        return u"FunctionNamespace(%r)" % self._ns_uri
201
204
 
 
205
@cython.final
 
206
@cython.internal
202
207
cdef class _XPathFunctionNamespaceRegistry(_FunctionNamespaceRegistry):
203
208
    cdef object _prefix
204
209
    cdef object _prefix_utf
232
237
                ns_prefixes.append(
233
238
                    (registry._prefix_utf, registry._ns_uri_utf))
234
239
    return ns_prefixes
235
 
 
236
 
cdef object _find_extension(ns_uri_utf, name_utf):
237
 
    cdef python.PyObject* dict_result
238
 
    dict_result = python.PyDict_GetItem(
239
 
        __FUNCTION_NAMESPACE_REGISTRIES, ns_uri_utf)
240
 
    if dict_result is NULL:
241
 
        return None
242
 
    extensions = (<_NamespaceRegistry>dict_result)._entries
243
 
    dict_result = python.PyDict_GetItem(extensions, name_utf)
244
 
    if dict_result is NULL:
245
 
        return None
246
 
    else:
247
 
        return <object>dict_result