mediagoblin-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [GMG-Devel] cyclic imports


From: Nathan Yergler
Subject: Re: [GMG-Devel] cyclic imports
Date: Sat, 15 Dec 2012 21:56:29 -0800
User-agent: mu4e 0.9.9; emacs 24.1.1

The performance cost to doing embedded imports (within a function
definition) isn't that great: it just means you do the import every time
you call the function. There's nothing technically wrong with
in-function imports, but import cycles are a "bad smell" to me.

If the CSRF meddleware is the cause of the cycle in this case (which is
what it sounds like from the original email), it should probably be
modified to use an in-function import.

NRY

Christopher Allan Webber <address@hidden> writes:

> Well, the solution we often use is in the case of some awful cycle,
> import inside the function:
>
> def foo():
>   import mediagoblin.would_cycle
>
> I actually have no idea what the cost of doing imports like this is, but
> we definitely love them it seems. ;)
>
> We could refactor... but I'm hesitant to jump in and refactor like
> crazy... that could be causing messes to save messes.
>
> Here's an idea I had this morning... warning, it's crazy ("crazy" is the
> word of the morning it seems):
>
> #+BEGIN_SRC python
> some_module = None
>
> def blah_blah():
>     _import_stragglers()
>     some_module.method_call("yeehaw")
>
>
> STRAGGLERS_IMPORTED = False
> def _import_stragglers():
>     """
>     Do any imports that would cause import cycles in this module, if we 
> haven't
>     already
>     """
>     if STRAGGLERS_IMPORTED is True:
>         return
>
>     import mediagoblin.foo.some_module as some_module_tmp
>     global some_module
>     some_module_tmp = some_module
>
>     import mediagoblin.foo.another_module as another_module_tmp
>     global another_module
>     another_module_tmp = another_module
>
>     global STRAGGLERS_IMPORTED
>     STRAGGLERS_IMPORTED = True
> #+END_SRC
>
> However that looks messy, and I can't think of any way to make it less
> messy.  If only we had lisp macros! ;)  I don't know if it's necessary
> either... I haven't profiled how long redundant inline imports are.
>
> Otherwise, what's wrong with in-function imports?  They work :)
>
> Sebastian Spaeth writes:
>
>> Whenever I modify things, I have to fight cyclic imports in MediaGoblin.
>> This is an issue that we really need to think about. Consider this:
>>
>> I want to give better error message in the case of csrf failures. To do
>> so I modify
>> GMG.meddlewares.csrf to return a GMG.tools.response:render_error().
>>
>>
>> 1) tools.response imports tools.template to get access to
>>     render_template()
>> 2) tools.template imports meddlewares.csrf to get access to
>>     render_csrf_token().
>> 3) meddlewares.csrf imports tools.response to get access to
>>     render_error()
>> 4) BANG BOOM CRASH. ImportError!
>>
>> How can we solve this issues in this specific and the general case? Here
>> I could imagine to solve/fix this by splitting CSRF into a
>> meddlewares.csrf (which does the checking) and a tools.csrf (which does
>> the actual rendering of the token). But that would split CSRF
>> functionality over 2 files. (we could also put it into tools.template
>> rather than tools.csrf).
>>
>> Sebastian
>> _______________________________________________
>> devel mailing list
>> address@hidden
>> http://lists.mediagoblin.org/listinfo/devel
>
> _______________________________________________
> devel mailing list
> address@hidden
> http://lists.mediagoblin.org/listinfo/devel


reply via email to

[Prev in Thread] Current Thread [Next in Thread]