Show More
@@ -1258,13 +1258,9 b' class InteractiveShell(SingletonConfigurable, Magic):' | |||
|
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: |
@@ -1,3 +1,5 b'' | |||
|
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 b' if sys.version_info[0] >= 3:' | |||
|
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 b' else:' | |||
|
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 |
@@ -1008,12 +1008,12 b' 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 b' class ObjectName(TraitType):' | |||
|
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 |
|
|
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 b' 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 |
|
|
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 |
General Comments 0
You need to be logged in to leave comments.
Login now