Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactot Route.all_plugins() #1306

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions bottle.py
Original file line number Diff line number Diff line change
Expand Up @@ -550,14 +550,14 @@ def prepare(self):

def all_plugins(self):
""" Yield all Plugins affecting this route. """
unique = set()
for p in reversed(self.app.plugins + self.plugins):
if True in self.skiplist: break
name = getattr(p, 'name', False)
if name and (name in self.skiplist or name in unique): continue
if p in self.skiplist or type(p) in self.skiplist: continue
if name: unique.add(name)
yield p
if True in self.skiplist: return
skips = set(self.skiplist)
for plugins in self.plugins, self.app.plugins:
for p in reversed(plugins):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Compared to the original code I find this much harder to understand.
normally a for loop in python is in the form:

for single in plural
    do something ... 

You wrote for plugins in <other list of plugins>. I didn't dive into analyzing in depth to see what is the other set of plugins. But this form of for will immediately cause a headache for a lot of people.
Further, an embedded for loop is harder to understand compared to a simple for loop.

if p not in skips and type(p) not in skips:
name = getattr(p, 'name', False)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you really need False here ? getattr will return None by default.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@oz123 getattr() raises AttibuteError by default! So there needs to be a third argument, but IMHO it should be None and not False.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@oz123, you wrong, if len(args) == 2 and obj has no attr, error rises:

getattr(None, 'name')
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/IPython/core/interactiveshell.py", line 3331, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-2-41470b2f07a7>", line 1, in <module>
    getattr(None, 'name')
AttributeError: 'NoneType' object has no attribute 'name'

Copy link
Contributor

@oz123 oz123 Jan 5, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, my bad one. Mixed it with dict.get!

if name and name not in skips: skips.add(name)
yield p

def _make_callback(self):
callback = self.callback
Expand Down