Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Python 3 compatibility for identifiers.

  • Loading branch information...
commit 8a385920e7218b7a6c7e4d461c02a952b6f5f305 1 parent c7f7b48
Thomas Kluyver authored August 01, 2011
10  IPython/core/interactiveshell.py
@@ -1258,13 +1258,9 @@ def _ofind(self, oname, namespaces=None):
1258 1258
         """
1259 1259
         oname = oname.strip()
1260 1260
         #print '1- oname: <%r>' % oname  # dbg
1261  
-        if not py3compat.PY3:
1262  
-            try:
1263  
-                oname = oname.encode('ascii')
1264  
-                #print '2- oname: <%r>' % oname  # dbg
1265  
-            except UnicodeError:
1266  
-                print 'Python identifiers can only contain ascii characters.'
1267  
-                return dict(found=False)
  1261
+        if not py3compat.isidentifier(oname.lstrip(ESC_MAGIC), dotted=True):
  1262
+            print 'Python identifiers can only contain ascii characters.'
  1263
+            return dict(found=False)
1268 1264
 
1269 1265
         alias_ns = None
1270 1266
         if namespaces is None:
14  IPython/utils/py3compat.py
... ...
@@ -1,3 +1,5 @@
  1
+# coding: utf-8
  2
+"""Compatibility tricks for Python 3. Mainly to do with unicode."""
1 3
 import sys
2 4
 
3 5
 def no_code(x, encoding=None):
@@ -32,6 +34,11 @@ def cast_bytes(s, encoding=None):
32 34
     str_to_bytes = encode
33 35
     bytes_to_str = decode
34 36
     
  37
+    def isidentifier(s, dotted=False):
  38
+        if dotted:
  39
+            return all(isidentifier(a) for a in s.split("."))
  40
+        return s.isidentifier()
  41
+
35 42
 else:
36 43
     PY3 = False
37 44
     
@@ -42,6 +49,13 @@ def cast_bytes(s, encoding=None):
42 49
     unicode_to_str = encode
43 50
     str_to_bytes = no_code
44 51
     bytes_to_str = no_code
  52
+    
  53
+    import re
  54
+    _name_re = re.compile(r"[a-zA-Z_][a-zA-Z0-9_]*$")
  55
+    def isidentifier(s, dotted=False):
  56
+        if dotted:
  57
+            return all(isidentifier(a) for a in s.split("."))
  58
+        return bool(_name_re.match(s))
45 59
 
46 60
 def execfile(fname, glob, loc=None):
47 61
     loc = loc if (loc is not None) else glob
20  IPython/utils/traitlets.py
@@ -1008,12 +1008,12 @@ class ObjectName(TraitType):
1008 1008
     This does not check that the name exists in any scope."""
1009 1009
     info_text = "a valid object identifier in Python"
1010 1010
 
1011  
-    if sys.version_info[0] < 3:
  1011
+    if py3compat.PY3:
  1012
+        # Python 3:
  1013
+        coerce_str = staticmethod(lambda _,s: s)
  1014
+    
  1015
+    else:
1012 1016
         # Python 2:
1013  
-        _name_re = re.compile(r"[a-zA-Z_][a-zA-Z0-9_]*$")
1014  
-        def isidentifier(self, s):
1015  
-            return bool(self._name_re.match(s))
1016  
-        
1017 1017
         def coerce_str(self, obj, value):
1018 1018
             "In Python 2, coerce ascii-only unicode to str"
1019 1019
             if isinstance(value, unicode):
@@ -1023,15 +1023,10 @@ def coerce_str(self, obj, value):
1023 1023
                     self.error(obj, value)
1024 1024
             return value
1025 1025
     
1026  
-    else:
1027  
-        # Python 3:
1028  
-        isidentifier = staticmethod(lambda s: s.isidentifier())
1029  
-        coerce_str = staticmethod(lambda _,s: s)
1030  
-    
1031 1026
     def validate(self, obj, value):
1032 1027
         value = self.coerce_str(obj, value)
1033 1028
         
1034  
-        if isinstance(value, str) and self.isidentifier(value):
  1029
+        if isinstance(value, str) and py3compat.isidentifier(value):
1035 1030
             return value
1036 1031
         self.error(obj, value)
1037 1032
 
@@ -1040,8 +1035,7 @@ class DottedObjectName(ObjectName):
1040 1035
     def validate(self, obj, value):
1041 1036
         value = self.coerce_str(obj, value)
1042 1037
         
1043  
-        if isinstance(value, str) and all(self.isidentifier(x) \
1044  
-                                                    for x in value.split('.')):
  1038
+        if isinstance(value, str) and py3compat.isidentifier(value, dotted=True):
1045 1039
             return value
1046 1040
         self.error(obj, value)
1047 1041
 

0 notes on commit 8a38592

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