Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add compile time option for Py2 that makes the API always return Unic…

…ode strings for names and text instead of byte strings for pure ASCII
  • Loading branch information...
commit c37f36f2843f0f9c1ef938920b47978d6e28ed4e 1 parent 4c6a7f4
@scoder scoder authored
View
4 CHANGES.txt
@@ -8,6 +8,10 @@ Under development
Features added
--------------
+* New build-time option ``--with-unicode-strings`` for Python 2 that
+ makes the API always return Unicode strings for names and text
+ instead of byte strings for plain ASCII content.
+
* New incremental XML file writing API ``etree.xmlfile()``.
* E factory in lxml.objectify is callable to simplify the creation of
View
3  setupinfo.py
@@ -250,6 +250,8 @@ def define_macros():
macros.append(('WITHOUT_THREADING', None))
if OPTION_WITH_REFNANNY:
macros.append(('CYTHON_REFNANNY', None))
+ if OPTION_WITH_UNICODE_STRINGS:
+ macros.append(('LXML_UNICODE_STRINGS', '1'))
return macros
_ERROR_PRINTED = False
@@ -359,6 +361,7 @@ def option_value(name):
staticbuild = bool(os.environ.get('STATICBUILD', ''))
# pick up any commandline options and/or env variables
OPTION_WITHOUT_OBJECTIFY = has_option('without-objectify')
+OPTION_WITH_UNICODE_STRINGS = has_option('with-unicode-strings')
OPTION_WITHOUT_ASSERT = has_option('without-assert')
OPTION_WITHOUT_THREADING = has_option('without-threading')
OPTION_WITHOUT_CYTHON = has_option('without-cython')
View
6 src/lxml/apihelpers.pxi
@@ -1301,7 +1301,7 @@ cdef object funicode(const_xmlChar* s):
cdef Py_ssize_t slen
cdef const_xmlChar* spos
cdef bint is_non_ascii
- if python.IS_PYTHON3:
+ if python.LXML_UNICODE_STRINGS:
return s.decode('UTF-8')
spos = s
is_non_ascii = 0
@@ -1551,11 +1551,11 @@ cdef inline object _namespacedName(xmlNode* c_node):
cdef object _namespacedNameFromNsName(const_xmlChar* href, const_xmlChar* name):
if href is NULL:
return funicode(name)
- elif python.IS_PYTHON3:
+ elif python.LXML_UNICODE_STRINGS and python.PY_VERSION_HEX >= 0x02060000:
return python.PyUnicode_FromFormat("{%s}%s", href, name)
else:
s = python.PyBytes_FromFormat("{%s}%s", href, name)
- if isutf8(_xcstr(s)):
+ if python.LXML_UNICODE_STRINGS or isutf8(_xcstr(s)):
return python.PyUnicode_FromEncodedObject(s, 'UTF-8', NULL)
else:
return s
View
9 src/lxml/includes/etree_defs.h
@@ -32,6 +32,15 @@
# define IS_PYTHON3 0
#endif
+#if IS_PYTHON3
+#undef LXML_UNICODE_STRINGS
+#define LXML_UNICODE_STRINGS 1
+#else
+#ifndef LXML_UNICODE_STRINGS
+#define LXML_UNICODE_STRINGS 0
+#endif
+#endif
+
#if !IS_PYPY
# define PyWeakref_LockObject(obj) (NULL)
#endif
View
1  src/lxml/python.pxd
@@ -129,5 +129,6 @@ cdef extern from "etree_defs.h": # redefines some functions as macros
cdef bint _isString(object obj)
cdef const_char* _fqtypename(object t)
cdef object PY_NEW(object t)
+ cdef bint LXML_UNICODE_STRINGS
cdef bint IS_PYTHON3
cdef bint IS_PYPY
Please sign in to comment.
Something went wrong with that request. Please try again.