[Rpm-maint] [PATCH 10/15] Port tagNumFromPyObject on Python 3 to use unicode objects

Panu Matilainen pmatilai at redhat.com
Tue Oct 27 09:27:10 UTC 2009


On Thu, 15 Oct 2009, James Antill wrote:

> On Thu, 2009-10-15 at 15:15 -0400, David Malcolm wrote:
>> Preserve the string-based API to headers:
>>    h['name']
>> by expecting a PyUnicode on Python 3, and a PyString on Python 2.
>> ---
>>  python/header-py.c |    7 +++++++
>>  1 files changed, 7 insertions(+), 0 deletions(-)
>>
>> diff --git a/python/header-py.c b/python/header-py.c
>> index 98cd753..f3454df 100644
>> --- a/python/header-py.c
>> +++ b/python/header-py.c
>> @@ -421,8 +421,15 @@ int tagNumFromPyObject (PyObject *item, rpmTag *tagp)
>>      if (PyInt_Check(item)) {
>>  	/* XXX we should probably validate tag numbers too */
>>  	tag = PyInt_AsLong(item);
>> +#if PY_MAJOR_VERSION >= 3
>> +    } else if (PyUnicode_Check(item)) {
>> +	PyObject *utf8_bytes = PyUnicode_AsUTF8String(item);
>> +	tag = rpmTagGetValue(PyBytes_AsString(utf8_bytes));
>> +	Py_XDECREF(utf8_bytes);
>> +#else
>
> There's no reason for ifdef here, as it isn't a bug if:
>
> ipkg.hdr[u'name']
>
> ...works in 2.* ... dito. hdr[b'name'] working in 3.*.

Ok, this and ts.dbMatch() key argument now accept unicode and 
automatically convert to utf8 on both Python 2.x and 3.x using a central 
converter function.

 	- Panu -


More information about the Rpm-maint mailing list