On Fri, Jul 17, 2015 at 11:15 PM, Stefan Behnel <
stef...@behnel.de> wrote:
> Matthew Honnibal schrieb am 18.07.2015 um 03:25:
>> Currently my main code-base contains 4,820 lines in .pyx files, and 1,270
>> lines in .pxd files. Most of the information in the .pxd files is
>> redundant, and maintaining them is frequently painful.
>>
>> It's very common for me to change a small type declaration, e.g. to add or
>> remove an except value or a nogil, and have to make the change in two
>> places.
>
> At least for some changes, making them in the .pxd file should be enough,
> as long as both files don't contain *contradicting* declarations.
>
>> Adding or removing an argument from a function signature is also
>> very common. If I move a helper class to its own file, suddenly I have to
>> create a new file, and delete the attribute type declarations from the
>> original. And inlining things is really bad — then the implementation lives
>> in the .pxd files, which will surely surprise me later. I really dislike
>> using inline for this reason. Maybe after trying to inline the function,
>> I'll see it made no difference to performance, so I'll remove it. Then I
>> have to move the whole function back to the .pyx file --- except the
>> declaration in the .pxd file.
>
> For trial and error, you can rename the function, e.g. prefix it with an
> underscore. Saves typing and copying. And going back to a previous version
> should just mean backing out a single change from VCS or so, rather than
> manually undoing changes.
Note also that inlining inherently must be in the pxd file, as (other
than the case below) inly the pxd files are inspected during
compilation.
>> Is there any reason we couldn't be generating the .pxd files, given some
>> annotations in the .pyx declarations?
>
> IIRC, there is support for cimporting also directly from .pyx files. See
> the "cimport_from_pyx.srctree" test. Don't know if there are limitations to
> what it can do.
This redundancy of pyx and pxd files, and having to have two files
around, is exactly why cimport-from-pyx was added. The only limitation
is not distinguishing between implementation and public interface
(though one may argue that such distinctions aren't very Pythonic
anyways... using conventions like underscore names is more standard).
This is essentially the same restriction auto-generated pxd files
would have as well.
> There is certainly the limitation that you cannot limit the
> public parts of an implementation, so you'd have to take care of not
> writing spaghetti code yourself. And .pyx files also tend to be
> substantially larger than .pxd files, so compilation would take longer.
> (Not that it matters, given that there's still a slowish C compiler coming
> after us anyway...)
Even for pxd files, parsing is expensive, so I've toyed with the idea
of caching a parsed version of the source anyways...
>> Even if it meant always exporting
>> everything, I would much prefer that to the current system. If a class is
>> in the .pxd at all, every C attribute must be exported anyway
>
> Yes, because Cython has to know about the object struct layout in order to
> handle it correctly. Especially subclassing would otherwise be impossible.
>
>
>>, so I don't
>> get much value out of choosing what's in the .pxd at the moment. Probably a
>> better solution, though, would be to export names that don't start with an
>> underscore. This is what Python does. Decorators could also be used, e.g.
>> to specify public/readonly.
>
> Sounds like a reasonable restriction.
>
>
>> I've been tempted at various points to hack something together along these
>> lines, since I'm pretty sure something like this could at least meet my own
>> needs. But I thought I'd check whether there was a dimension to the problem
>> that I was missing; some reason why this is harder than it seems.
>
> Shouldn't be. I'm not a big fan of this myself, but given that there's
> support for this already, I wouldn't object to extending it a bit more if
> there is a need for improvement.
>
> Stefan
>
> --
>
> ---
> You received this message because you are subscribed to the Google Groups "cython-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
cython-users...@googlegroups.com.
> For more options, visit
https://groups.google.com/d/optout.