Skip to content
This repository

The constructor of Client() checks for AssertionError in validate_url to open a file instead of connection to a URL if it fails. #872

Closed
wants to merge 1 commit into from

2 participants

Hannes Schulz Min RK
Hannes Schulz

The constructor of Client() checks for AssertionError in validate_url to open a file instead of connection to a URL if it fails.

When running python -O, this failed, since the assertions were optimized away and other (random) errors were thrown.
These random errors are not catched. IMHO one should not depend on the side effects of assertions, which has been done here.

Min RK
Owner

Fair point.

I overloaded the validate_url, which is really just supposed to check that a url is valid, but I used it in the client to distinguish between a url and a file, which is inappropriate, since assertions get optimized away.

Do you want to change the try/except in Client from:

try:
   util.validate_url(url_or_file)
except AssertionError:

to:

if not util.is_url(url_or_file):

adding to parallel.util:

def is_url(url):
    """boolean check for whether a string is a zmq url"""
    if '://' not in url:
        return False
    proto, addr = url.split('://', 1)
    if proto.lower() not in ['tcp','pgm','epgm','ipc','inproc']:
        return False
    return True

That way the assertions in validate_url are never used for actual logic, and only used for actual validation, where assertions are appropriate, and appropriate to optimize away when you trust that you don't have certain errors.

Hannes Schulz

This definitely sounds cleaner than my (minimum-impact-solution-) patch, I'd go with your solution!

Min RK
Owner

Did you want to make those changes to your PR, or shall I just make the change?

Hannes Schulz

I changed my commit as you suggested.

Min RK

This needs to be util.is_url, otherwise it gets a NameError. Also, it's indented by a tab instead of 8 spaces for some reason.

Min RK
Owner

Since it's a tiny typo, I'll go ahead and fix it in the merge. Thanks!

Min RK minrk closed this pull request from a commit October 17, 2011
Min RK fix is_url typo in parallel client
closes #872
4332951
Min RK minrk closed this in 4332951 October 17, 2011
Brian E. Granger ellisonbg referenced this pull request from a commit January 10, 2012
Commit has since been removed from the repository and is no longer available.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Oct 17, 2011
Hannes Schulz introduce is_url() replacing validate_url() in cases where it is used…
… in a control structure (as opposed to an assert)
80a97ea
This page is out of date. Refresh to see the latest.
4  IPython/parallel/client/client.py
@@ -306,9 +306,7 @@ def __init__(self, url_or_file=None, profile=None, profile_dir=None, ipython_dir
306 306
         assert url_or_file is not None, "I can't find enough information to connect to a hub!"\
307 307
             " Please specify at least one of url_or_file or profile."
308 308
 
309  
-        try:
310  
-            util.validate_url(url_or_file)
311  
-        except AssertionError:
  309
+	if not is_url(url_or_file):
312 310
             if not os.path.exists(url_or_file):
313 311
                 if self._cd:
314 312
                     url_or_file = os.path.join(self._cd.security_dir, url_or_file)
9  IPython/parallel/util.py
@@ -108,6 +108,15 @@ def asbytes(s):
108 108
         s = s.encode('ascii')
109 109
     return s
110 110
 
  111
+def is_url(url):
  112
+    """boolean check for whether a string is a zmq url"""
  113
+    if '://' not in url:
  114
+        return False
  115
+    proto, addr = url.split('://', 1)
  116
+    if proto.lower() not in ['tcp','pgm','epgm','ipc','inproc']:
  117
+        return False
  118
+    return True
  119
+
111 120
 def validate_url(url):
112 121
     """validate a url for zeromq"""
113 122
     if not isinstance(url, basestring):
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.