##// END OF EJS Templates
Python 3 compatibility for identifiers.
Thomas Kluyver -
Show More
@@ -1258,13 +1258,9 b' class InteractiveShell(SingletonConfigurable, Magic):'
1258 """
1258 """
1259 oname = oname.strip()
1259 oname = oname.strip()
1260 #print '1- oname: <%r>' % oname # dbg
1260 #print '1- oname: <%r>' % oname # dbg
1261 if not py3compat.PY3:
1261 if not py3compat.isidentifier(oname.lstrip(ESC_MAGIC), dotted=True):
1262 try:
1262 print 'Python identifiers can only contain ascii characters.'
1263 oname = oname.encode('ascii')
1263 return dict(found=False)
1264 #print '2- oname: <%r>' % oname # dbg
1265 except UnicodeError:
1266 print 'Python identifiers can only contain ascii characters.'
1267 return dict(found=False)
1268
1264
1269 alias_ns = None
1265 alias_ns = None
1270 if namespaces is None:
1266 if namespaces is None:
@@ -1,3 +1,5 b''
1 # coding: utf-8
2 """Compatibility tricks for Python 3. Mainly to do with unicode."""
1 import sys
3 import sys
2
4
3 def no_code(x, encoding=None):
5 def no_code(x, encoding=None):
@@ -32,6 +34,11 b' if sys.version_info[0] >= 3:'
32 str_to_bytes = encode
34 str_to_bytes = encode
33 bytes_to_str = decode
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 else:
42 else:
36 PY3 = False
43 PY3 = False
37
44
@@ -42,6 +49,13 b' else:'
42 unicode_to_str = encode
49 unicode_to_str = encode
43 str_to_bytes = no_code
50 str_to_bytes = no_code
44 bytes_to_str = no_code
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 def execfile(fname, glob, loc=None):
60 def execfile(fname, glob, loc=None):
47 loc = loc if (loc is not None) else glob
61 loc = loc if (loc is not None) else glob
@@ -1008,12 +1008,12 b' class ObjectName(TraitType):'
1008 This does not check that the name exists in any scope."""
1008 This does not check that the name exists in any scope."""
1009 info_text = "a valid object identifier in Python"
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 # Python 2:
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 def coerce_str(self, obj, value):
1017 def coerce_str(self, obj, value):
1018 "In Python 2, coerce ascii-only unicode to str"
1018 "In Python 2, coerce ascii-only unicode to str"
1019 if isinstance(value, unicode):
1019 if isinstance(value, unicode):
@@ -1023,15 +1023,10 b' class ObjectName(TraitType):'
1023 self.error(obj, value)
1023 self.error(obj, value)
1024 return value
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 def validate(self, obj, value):
1026 def validate(self, obj, value):
1032 value = self.coerce_str(obj, value)
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 return value
1030 return value
1036 self.error(obj, value)
1031 self.error(obj, value)
1037
1032
@@ -1040,8 +1035,7 b' class DottedObjectName(ObjectName):'
1040 def validate(self, obj, value):
1035 def validate(self, obj, value):
1041 value = self.coerce_str(obj, value)
1036 value = self.coerce_str(obj, value)
1042
1037
1043 if isinstance(value, str) and all(self.isidentifier(x) \
1038 if isinstance(value, str) and py3compat.isidentifier(value, dotted=True):
1044 for x in value.split('.')):
1045 return value
1039 return value
1046 self.error(obj, value)
1040 self.error(obj, value)
1047
1041
General Comments 0
You need to be logged in to leave comments. Login now