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

add _port to url_for, update routing doc #1406

Closed
wants to merge 1 commit into from
Closed

Conversation

lixxu
Copy link
Contributor

@lixxu lixxu commented Nov 8, 2018

No description provided.

sanic/app.py Outdated
@@ -690,6 +698,16 @@ def url_for(self, view_name: str, **kwargs):

# parse the remainder of the keyword arguments into a querystring
query_string = urlencode(kwargs, doseq=True) if kwargs else ""

if external and port:
port = int(port)
Copy link
Member

Choose a reason for hiding this comment

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

I know this may sound silly, but shouldn't we wrap this call with a try/except in case int raises a ValueError or TypeError? Just in case someone tries to pass some random string or some other weird object to the _port argument or SERVER_PORT configuration key? At least we will prevent any issues in the future with this ...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, sounds reasonable.

Copy link
Member

Choose a reason for hiding this comment

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

Great, thanks @lixxu !

Copy link
Contributor

Choose a reason for hiding this comment

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

I actually like this more as it was, makes it impossible to use it wrong. the try/except logic lets you pass a nonsense port and doesn't even complain

sanic/app.py Outdated
try:
port = int(port)
except (ValueError, TypeError):
pass # just ignore it
Copy link
Member

Choose a reason for hiding this comment

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

Yeah, I think we can just ignore this (for now). If someone complains in the future, we can perhaps just add a logger.warn call here and that should suffice.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Don't know why lint check failed.

Copy link
Member

Choose a reason for hiding this comment

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

@lixxu try to run black to format the code (in the root of the repo):

$ black ./sanic

Copy link
Contributor Author

@lixxu lixxu Nov 8, 2018

Choose a reason for hiding this comment

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

oops, 69 files reformatted, 19 files left unchanged. (almost all are single quote replaced by double quotes). Should I commit these changes? a lot files, may conflict with other PRs.

Copy link
Member

Choose a reason for hiding this comment

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

Have you rebased with the last master?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sorry, I ran the cmd outside of sanic folder, now only the 2 files updated. :)

Copy link
Member

Choose a reason for hiding this comment

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

Ohh, that makes sense 😄

Choose a reason for hiding this comment

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

TLDR; Fail fast and fail loud.
I saw that @lixxu had already implemented it first without try:except then they were asked to put it in a try:except block. In this case, I believe failing loudly would have better point that failing silently here. A user might wrongly pass some object that can't be converted into integer and this function will fail silently.

@codecov-io
Copy link

codecov-io commented Nov 8, 2018

Codecov Report

Merging #1406 into master will decrease coverage by 0.36%.
The diff coverage is 58.82%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1406      +/-   ##
==========================================
- Coverage   91.35%   90.99%   -0.37%     
==========================================
  Files          18       18              
  Lines        1781     1787       +6     
  Branches      337      340       +3     
==========================================
- Hits         1627     1626       -1     
- Misses        130      135       +5     
- Partials       24       26       +2
Impacted Files Coverage Δ
sanic/testing.py 92.5% <50%> (-2.8%) ⬇️
sanic/app.py 91.07% <61.53%> (-1.18%) ⬇️
sanic/router.py 95.89% <0%> (+0.45%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update d581315...e6dd8cb. Read the comment docs.

Copy link
Member

@ahopkins ahopkins left a comment

Choose a reason for hiding this comment

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

If port 21 is included, why not 22?

@lixxu
Copy link
Contributor Author

lixxu commented Nov 8, 2018

22 is used for ssh, and I think (maybe wrong) it may have no chance to generate urls for ssh. Or you means for sftp?

@ahopkins
Copy link
Member

ahopkins commented Nov 8, 2018

Yes, sftp, which ... as far as I know is just a cover for ssh on port 22.

Regardless, is that even something that we want to be supporting? Why have any ftp at all? Am I missing something?

@vltr
Copy link
Member

vltr commented Nov 8, 2018

I agree with @ahopkins . We should stick to http and https since Sanic is a web framework [...]

@lixxu
Copy link
Contributor Author

lixxu commented Nov 8, 2018

ftp and ftps are from testing.py, they are there maybe in case that someone use sanic to write ftp/ftps server?

@vltr
Copy link
Member

vltr commented Nov 8, 2018

That would have the need of a completely different protocol implementation ... As well as a lot of FTP specific features like mapping all the commands, perhaps build a proper FS integration and etc, etc ... I think it'll be too much work to make it run on top of Sanic - unless someone already had the trouble to do something like that (and I would be really impressed).

If I needed (today) to implement my own custom FTP server, I would go with Twisted or aioftp. No need to reinvent the wheel ...

@lixxu
Copy link
Contributor Author

lixxu commented Nov 8, 2018

so, should remove ftp stuff from url_for?

@vltr
Copy link
Member

vltr commented Nov 8, 2018

IMO it makes more sense (inside Sanic) to remove ftp from the "port normalization" part of the function.

sanic/app.py Outdated

port = kwargs.pop("_port", None)
if port is None:
port = self.config.get("SERVER_PORT", "")
Copy link
Contributor

Choose a reason for hiding this comment

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

Wouldn't it be much simpler to merge all of these into a single line and let python handle it rather than the conditional branching?

port = kwargs.pop('_port', self.config.get('SERVER_PORT', ''))

Copy link
Contributor Author

Choose a reason for hiding this comment

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

single line format will not get the value from configuration if someone passes _port=None as parameter.

Copy link
Contributor

@harshanarayana harshanarayana Nov 8, 2018

Choose a reason for hiding this comment

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

@lixxu
The following should give you the same output.

port = kwargs.pop('_port', self.config.get('SERVER_PORT', '')) or ''

I mentioned this since I've seen a great performance improvement with the removal of each if-else construct. I know this doesn't add too much overhead but still felt like a good idea.

PS: Yes, I see it now. The grave i've been digging for myself. 😛

Copy link
Contributor

Choose a reason for hiding this comment

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

Just FYI,

>>> timeit.timeit(stmt=lookup, number=1000000)
0.472626347996993
>>> timeit.timeit(stmt=cond, number=1000000)
0.5024505830078851

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok, updated for it. but I made the commits history ugly. :( wonder how to squash it again.

@ahopkins
Copy link
Member

ahopkins commented Nov 8, 2018

@lixxu, can you also squash these commits?

@lixxu
Copy link
Contributor Author

lixxu commented Nov 8, 2018

sorry, I made the commits ugly. Is there anyway to fix it? :(

@harshanarayana
Copy link
Contributor

@lixxu git rebase -i commit-id-to-start

When rebase is done it will prompt you to edit the messages

@lixxu
Copy link
Contributor Author

lixxu commented Nov 9, 2018

tks @harshanarayana, seems worked (with force pushed :( ).

@harshanarayana
Copy link
Contributor

@lixxu That's expected. Since you rebased it, now the commit IDs in github and the one you are trying to push are all wibly wobly. So you have to use -f more or less always when you use git rebase commands.

@sjsadowski
Copy link
Contributor

I'm tagging this for 19.03 since it is a change, not a fix.

@sjsadowski sjsadowski added this to the 19.03 milestone Nov 12, 2018
@lixxu
Copy link
Contributor Author

lixxu commented Nov 12, 2018

the issue #1380 was tagged for 18.12 lts.

@sjsadowski
Copy link
Contributor

That is because #1380 is pointing out a bug. This fixes a bug, yes, but it also adds functionality.

abuckenheimer
abuckenheimer previously approved these changes Nov 17, 2018
sanic/app.py Outdated
netloc = netloc.split("://", 1)[-1]

if not netloc:
scheme = ""
external = False
Copy link
Contributor

Choose a reason for hiding this comment

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

the branching here is getting a bit complicated, I'd maybe suggest something like

scheme = re.match(
    r"(?P<scheme>https?)?(?(scheme)://)(?P<netloc>\w+)",
    kwargs.pop("_scheme", "")
).groupdict()
if scheme:
    scheme, netloc = scheme['scheme'], scheme['netloc']
else:
    scheme, netloc = ""

but we can always address this seperately

@yunstanford
Copy link
Member

This PR looks like out of date, could you update it ?

Copy link
Member

@yunstanford yunstanford left a comment

Choose a reason for hiding this comment

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

Could you update this outdated branch ?

@lixxu
Copy link
Contributor Author

lixxu commented Dec 5, 2018

Sorry, how to update it?

@MuhistheHolvi
Copy link

@lixxu You can either merge master to your local branch and then merge or you can rebase your branch onto master branch. You need to resolve the conflicts as shown in github.

@sjsadowski sjsadowski closed this Mar 3, 2019
@sjsadowski sjsadowski reopened this Mar 3, 2019
@sjsadowski
Copy link
Contributor

@lixxu can you update your branch?

@lixxu
Copy link
Contributor Author

lixxu commented Mar 4, 2019

@lixxu can you update your branch?

updated, please review.

@sjsadowski
Copy link
Contributor

@lixxu can you back that out, do a squash and merge or rebase instead? This is gonna be a huge amount of clutter in the commits to merge back in.

* enable blueprint group middleware support

This commit will enable the users to implement a middleware at the
blueprint group level whereby enforcing the middleware automatically to
each of the available Blueprints that are part of the group.

This will eanble a simple way in which a certain set of common features
and criteria can be enforced on a Blueprint group. i.e. authentication
and authorization

This commit will address the feature request raised as part of Issue sanic-org#1386

Signed-off-by: Harsha Narayana <[email protected]>

* enable indexing of BlueprintGroup object

Signed-off-by: Harsha Narayana <[email protected]>

* rename blueprint group file to fix spelling error

Signed-off-by: Harsha Narayana <[email protected]>

* add documentation and additional unit tests

Signed-off-by: Harsha Narayana <[email protected]>

* cleanup and optimize headers in unit test file

Signed-off-by: Harsha Narayana <[email protected]>

* fix Bluprint Group iteratable method

Signed-off-by: Harsha Narayana <[email protected]>

* add additional unit test to check StopIteration condition

Signed-off-by: Harsha Narayana <[email protected]>

* cleanup iter protocol implemenation for blueprint group and add slots

Signed-off-by: Harsha Narayana <[email protected]>

* fix blueprint group middleware invocation identification

Signed-off-by: Harsha Narayana <[email protected]>

* feat: enable list behavior on blueprint group object and use append instead of properly to add blueprint to group

Signed-off-by: Harsha Narayana <[email protected]>

add port to url_for, and update doc for it, reset scheme and external if server not specified in url_for

add port to url_for, and update doc for it, reset scheme and external if server not specified in url_for

Revert "add port to url_for, and update doc for it, reset scheme and external if server not specified in url_for"

This reverts commit b073dfa.
@lixxu
Copy link
Contributor Author

lixxu commented Mar 5, 2019

oops, I lost the code lines (sorry, not familiar with squash, rebase), how to find them back?

@lixxu lixxu closed this Mar 17, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants