[svn.haxx.se] · SVN Dev · SVN Users · SVN Org · TSVN Dev · TSVN Users · Subclipse Dev · Subclipse Users · this month's index

Re: (swig-py3) how to handle "invalid" property value?

From: Daniel Shahaf <d.s_at_daniel.shahaf.name>
Date: Tue, 11 Dec 2018 10:58:13 +0000

Yasuhito FUTATSUKI wrote on Tue, 11 Dec 2018 19:22 +0900:
> On 12/11/18 5:12 PM, Daniel Shahaf wrote:
> > Generic APIs that work on any property should return bytes. More
> > specific APIs that work on properties that have further restrictions
> > (svn:needs-lock, svn:date), or even a structured value (svn:externals,
> > svn:mergeinfo), may use appropriately more specific data types.
>
> I see. Then I'll try to fix those API wrappers returns propety values
> to return as bytes in py3. I think precise restriction check for
> properties is not the matter of wrappers, so I don't touch it.
>

Agreed: in general, the wrappers should follow the behaviour of the C
API's as closely as possible. If a random C API doesn't validate
properties as it returns them, neither should its Python wrapper.

> > In general, if an API has an input of type bytes and needs to return
> > str, I would have it throw an exception if the conversion fails, so the
> > caller is forced to deal with the failure mode explicitly. It'd be fine
> > to use surrogateescape or return bytes *if the caller has explicitly
> > requested that*, but the default postcondition should be as simple as
> > possible: "This function either returns str or raises an exception".
>
> I'm sorry, I meant "Those APIs" as not generic APIs, but "those APIs
> return propety values", and I don't want to hide exception if there
> are ways not to do so, too. (I've searched the way to add keywords
> arguments with default values like charset, errors, or return_type
> with make relation to argment for props, but I couldn't)

Interesting idea. (I don't know swig well enough to comment on it.)

> However, I also think if C API returns value successfuly and the wrapper
> fails to return its value "unexpectedly", the wrapper is incomplete.

Sure.

> My interest is "what should care as bytes object and what should
> care as str object Python 3 API wrapper". Current typemaps seems
> almost all char *, svn_string_t * are mapped to str object in py3.

Returning str would have been correct under py2. It might be just that
the typemaps haven't been updated for the str/bytes changes in py3.
The log of the swig-py3 branch might have some clues.

There _are_ some cases of char* / svn_string_t* that semantically
correspond to py3 str, for example, svn_cmdline_printf() --- although
that's not a good example, as it's variadic --- but even so, if it's be
easier to implement the rule "_all_ char* and svn_string_t* in C
signatures are expected to be 'bytes' in Python", I'll support that.
It'll be easier for API consumers to learn the rule "Always .encode()
your str's" than to have to learn which functions are exceptions to the rule.

Cheers,

Daniel
Received on 2018-12-11 11:58:26 CET

This is an archived mail posted to the Subversion Dev mailing list.