-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Conversation
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) |
There was a problem hiding this comment.
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 ...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, sounds reasonable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great, thanks @lixxu !
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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. :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ohh, that makes sense 😄
There was a problem hiding this comment.
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 Report
@@ 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
Continue to review full report at Codecov.
|
There was a problem hiding this 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?
22 is used for |
Yes, Regardless, is that even something that we want to be supporting? Why have any ftp at all? Am I missing something? |
I agree with @ahopkins . We should stick to |
ftp and ftps are from testing.py, they are there maybe in case that someone use sanic to write ftp/ftps server? |
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 ... |
so, should remove ftp stuff from |
IMO it makes more sense (inside Sanic) to remove |
sanic/app.py
Outdated
|
||
port = kwargs.pop("_port", None) | ||
if port is None: | ||
port = self.config.get("SERVER_PORT", "") |
There was a problem hiding this comment.
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', ''))
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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. 😛
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
@lixxu, can you also squash these commits? |
sorry, I made the commits ugly. Is there anyway to fix it? :( |
@lixxu When rebase is done it will prompt you to edit the messages |
tks @harshanarayana, seems worked (with force pushed :( ). |
@lixxu That's expected. Since you |
I'm tagging this for 19.03 since it is a change, not a fix. |
the issue #1380 was tagged for 18.12 lts. |
That is because #1380 is pointing out a bug. This fixes a bug, yes, but it also adds functionality. |
sanic/app.py
Outdated
netloc = netloc.split("://", 1)[-1] | ||
|
||
if not netloc: | ||
scheme = "" | ||
external = False |
There was a problem hiding this comment.
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
This PR looks like out of date, could you update it ? |
There was a problem hiding this 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 ?
Sorry, how to update it? |
@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. |
@lixxu can you update your branch? |
updated, please review. |
@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.
oops, I lost the code lines (sorry, not familiar with squash, rebase), how to find them back? |
No description provided.