aboutsummaryrefslogtreecommitdiff
path: root/Tools
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2020-06-30 09:03:15 +0300
committerGitHub <noreply@github.com>2020-06-30 09:03:15 +0300
commit349f76c6aace5a4a2b57f6b442a532faf0027d6b (patch)
treec9d8fe1e3062e51e65cd7b2a763822908cb7a1cb /Tools
parentbpo-41123: Remove PyUnicode_AsUnicodeCopy (GH-21209) (diff)
downloadcpython-349f76c6aace5a4a2b57f6b442a532faf0027d6b.tar.gz
cpython-349f76c6aace5a4a2b57f6b442a532faf0027d6b.tar.bz2
cpython-349f76c6aace5a4a2b57f6b442a532faf0027d6b.zip
bpo-36346: Prepare for removing the legacy Unicode C API (AC only). (GH-21223)
Diffstat (limited to 'Tools')
-rwxr-xr-xTools/clinic/clinic.py65
1 files changed, 63 insertions, 2 deletions
diff --git a/Tools/clinic/clinic.py b/Tools/clinic/clinic.py
index b1bf7826ebf..3a9f4c228c2 100755
--- a/Tools/clinic/clinic.py
+++ b/Tools/clinic/clinic.py
@@ -3374,20 +3374,81 @@ class unicode_converter(CConverter):
displayname=displayname)
return super().parse_arg(argname, displayname)
+@add_legacy_c_converter('u')
@add_legacy_c_converter('u#', zeroes=True)
@add_legacy_c_converter('Z', accept={str, NoneType})
@add_legacy_c_converter('Z#', accept={str, NoneType}, zeroes=True)
class Py_UNICODE_converter(CConverter):
type = 'const Py_UNICODE *'
default_type = (str, Null, NoneType)
- format_unit = 'u'
def converter_init(self, *, accept={str}, zeroes=False):
format_unit = 'Z' if accept=={str, NoneType} else 'u'
if zeroes:
format_unit += '#'
self.length = True
- self.format_unit = format_unit
+ self.format_unit = format_unit
+ else:
+ self.accept = accept
+ if accept == {str}:
+ self.converter = '_PyUnicode_WideCharString_Converter'
+ elif accept == {str, NoneType}:
+ self.converter = '_PyUnicode_WideCharString_Opt_Converter'
+ else:
+ fail("Py_UNICODE_converter: illegal 'accept' argument " + repr(accept))
+
+ def cleanup(self):
+ if not self.length:
+ return """\
+#if !USE_UNICODE_WCHAR_CACHE
+PyMem_Free((void *){name});
+#endif /* USE_UNICODE_WCHAR_CACHE */
+""".format(name=self.name)
+
+ def parse_arg(self, argname, argnum):
+ if not self.length:
+ if self.accept == {str}:
+ return """
+ if (!PyUnicode_Check({argname})) {{{{
+ _PyArg_BadArgument("{{name}}", {argnum}, "str", {argname});
+ goto exit;
+ }}}}
+ #if USE_UNICODE_WCHAR_CACHE
+ _Py_COMP_DIAG_PUSH
+ _Py_COMP_DIAG_IGNORE_DEPR_DECLS
+ {paramname} = _PyUnicode_AsUnicode({argname});
+ _Py_COMP_DIAG_POP
+ #else /* USE_UNICODE_WCHAR_CACHE */
+ {paramname} = PyUnicode_AsWideCharString({argname}, NULL);
+ #endif /* USE_UNICODE_WCHAR_CACHE */
+ if ({paramname} == NULL) {{{{
+ goto exit;
+ }}}}
+ """.format(argname=argname, paramname=self.name, argnum=argnum)
+ elif self.accept == {str, NoneType}:
+ return """
+ if ({argname} == Py_None) {{{{
+ {paramname} = NULL;
+ }}}}
+ else if (PyUnicode_Check({argname})) {{{{
+ #if USE_UNICODE_WCHAR_CACHE
+ _Py_COMP_DIAG_PUSH
+ _Py_COMP_DIAG_IGNORE_DEPR_DECLS
+ {paramname} = _PyUnicode_AsUnicode({argname});
+ _Py_COMP_DIAG_POP
+ #else /* USE_UNICODE_WCHAR_CACHE */
+ {paramname} = PyUnicode_AsWideCharString({argname}, NULL);
+ #endif /* USE_UNICODE_WCHAR_CACHE */
+ if ({paramname} == NULL) {{{{
+ goto exit;
+ }}}}
+ }}}}
+ else {{{{
+ _PyArg_BadArgument("{{name}}", {argnum}, "str or None", {argname});
+ goto exit;
+ }}}}
+ """.format(argname=argname, paramname=self.name, argnum=argnum)
+ return super().parse_arg(argname, argnum)
@add_legacy_c_converter('s*', accept={str, buffer})
@add_legacy_c_converter('z*', accept={str, buffer, NoneType})