Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Add simple implementation of Python 3 style open()

  • Loading branch information...
commit d60f657e2b75e72bd3ddd477c44fb7e1d6ca0895 1 parent cf41e14
Thomas Kluyver authored August 01, 2011
9  IPython/core/magic.py
@@ -52,6 +52,7 @@
52 52
 from IPython.core.prefilter import ESC_MAGIC
53 53
 from IPython.lib.pylabtools import mpl_runner
54 54
 from IPython.testing.skipdoctest import skip_doctest
  55
+from IPython.utils import py3compat
55 56
 from IPython.utils.io import file_read, nlprint
56 57
 from IPython.utils.path import get_py_filename, unquote_filename
57 58
 from IPython.utils.process import arg_split, abbrev_cwd
@@ -2085,11 +2086,9 @@ def magic_save(self,parameter_s = ''):
2085 2086
         except (TypeError, ValueError) as e:
2086 2087
             print e.args[0]
2087 2088
             return
2088  
-        if isinstance(cmds, unicode):
2089  
-            cmds = cmds.encode("utf-8")
2090  
-        with open(fname,'w') as f:
2091  
-            f.write("# coding: utf-8\n")
2092  
-            f.write(cmds)
  2089
+        with py3compat.open(fname,'w', encoding="utf-8") as f:
  2090
+            f.write(u"# coding: utf-8\n")
  2091
+            f.write(py3compat.cast_unicode(cmds))
2093 2092
         print 'The following commands were written to file `%s`:' % fname
2094 2093
         print cmds
2095 2094
     
25  IPython/utils/py3compat.py
@@ -2,6 +2,8 @@
2 2
 """Compatibility tricks for Python 3. Mainly to do with unicode."""
3 3
 import sys
4 4
 
  5
+orig_open = open
  6
+
5 7
 def no_code(x, encoding=None):
6 8
     return x
7 9
 
@@ -38,6 +40,8 @@ def isidentifier(s, dotted=False):
38 40
         if dotted:
39 41
             return all(isidentifier(a) for a in s.split("."))
40 42
         return s.isidentifier()
  43
+    
  44
+    open = orig_open
41 45
 
42 46
 else:
43 47
     PY3 = False
@@ -56,6 +60,27 @@ def isidentifier(s, dotted=False):
56 60
         if dotted:
57 61
             return all(isidentifier(a) for a in s.split("."))
58 62
         return bool(_name_re.match(s))
  63
+    
  64
+    class open(object):
  65
+        """Wrapper providing key part of Python 3 open() interface."""
  66
+        def __init__(self, fname, mode="r", encoding="utf-8"):
  67
+            self.f = orig_open(fname, mode)
  68
+            self.enc = encoding
  69
+        
  70
+        def write(self, s):
  71
+            return self.f.write(s.encode(self.enc))
  72
+        
  73
+        def read(self, size=-1):
  74
+            return self.f.read(size).decode(self.enc)
  75
+        
  76
+        def close(self):
  77
+            return self.f.close()
  78
+        
  79
+        def __enter__(self):
  80
+            return self
  81
+        
  82
+        def __exit__(self, etype, value, traceback):
  83
+            self.f.close()
59 84
 
60 85
 def execfile(fname, glob, loc=None):
61 86
     loc = loc if (loc is not None) else glob

0 notes on commit d60f657

Please sign in to comment.
Something went wrong with that request. Please try again.