rgalanakis / pynocle

Code metrics for Python code.
10 stars 4 forks source link

string.replace(os.altsep, os.sep) crashes on Mac #13

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
On Mac, os.altsep is None. This leads to TypeErrors when calling 
string.replace(os.altsep, os.sep). The patch attached below fixes the issue

beaumont@beaumont:~/pynocle$ hg log -r tip -p
changeset:   90:afaab9ad9d22
tag:         tip
user:        Chris Beaumont <beaumont@hawaii.edu>
date:        Wed Jun 13 16:32:31 2012 -0400
summary:     Fix bug when handling os.altsep on Macs

diff -r 9dbf4784fcad -r afaab9ad9d22 pynocle/depgraph/rendering.py
--- a/pynocle/depgraph/rendering.py Sun Jun 10 23:15:29 2012 +0000
+++ b/pynocle/depgraph/rendering.py Wed Jun 13 16:32:31 2012 -0400
@@ -68,7 +68,7 @@
             #Skip the period on the extension
             result = os.path.splitext(outputfilename)[1][1:]
         return result
-    
+
     def render(self, outputfilename,
                dotpath=None, overrideformat=None, wait=True, moreargs=()):
         """Renders the dot file at dotpath to outputfilename.
@@ -160,7 +160,7 @@
             f.write('digraph G {\n')
             for kvp in self.styler.graphsettings().items():
                 f.write('    %s=%s;\n' % kvp)
-            
+
             pkgs, modules = self._write_edges(f)
             failed = dict((self.styler.nodetext(fname), fname)
                           for fname in self.failedfiles)
@@ -279,10 +279,10 @@
         if not s2:
             # We've removed the whole path,
             # grab the last dir from the leadingpath that removed it
-            s2 = self.leading_path.replace(os.altsep, os.sep).split(os.sep)[-1]
+            s2 = utils.sanitize_altsep(self.leading_path).split(os.sep)[-1]
         else:
             s2 = os.path.splitdrive(s2)[1]
-            s2 = s2.replace(os.sep, '.').replace(os.altsep, '.')
+            s2 = utils.sanitize_altsep(s2).replace(os.sep, '.')
         return s2.strip('.')

     def weight(self, a, b):
@@ -292,7 +292,7 @@
         self.weight_heaviest.

         If module b starts with an underscore, assume a high weight.
-        If module a starts with module b or vice versa, assume a 
+        If module a starts with module b or vice versa, assume a
         """
         if b.split('.')[-1].startswith('_'):
             # A module that starts with an underscore.
diff -r 9dbf4784fcad -r afaab9ad9d22 pynocle/utils.py
--- a/pynocle/utils.py  Sun Jun 10 23:15:29 2012 +0000
+++ b/pynocle/utils.py  Wed Jun 13 16:32:31 2012 -0400
@@ -137,12 +137,17 @@

     :param leading: If None, cwd.
     """
-    leading = (leading or os.getcwd()).replace(os.altsep, os.sep)
-    s = os.path.splitext(path.replace(os.altsep, os.sep))[0]
+    leading = sanitize_altsep(leading or os.getcwd())
+    s = os.path.splitext(sanitize_altsep(path))[0]
     if s.startswith(leading):
         s = s.replace(leading, '')
     return s.strip(os.sep)

+def sanitize_altsep(string):
+    if os.altsep:
+        return string.replace(os.altsep, os.sep)
+    else:
+        return string

 def rst_to_html(rststr):
     from docutils.core import publish_string

Original issue reported on code.google.com by cnb4s...@gmail.com on 13 Jun 2012 at 8:38

Attachments:

GoogleCodeExporter commented 9 years ago
this also fails in linux distributions. another workaround is to run `os.altsep 
= '/'` before running pynocle

Original comment by c...@dimagi.com on 18 Sep 2013 at 9:22