Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Gwenaël Samain
cython
Commits
9309fe02
Commit
9309fe02
authored
Oct 14, 2009
by
Stefan Behnel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
safer way to initialise string constants: keep bytes/str/unicode separated also in corner cases
parent
5a8e4501
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
16 additions
and
5 deletions
+16
-5
Cython/Compiler/Code.py
Cython/Compiler/Code.py
+16
-5
No files found.
Cython/Compiler/Code.py
View file @
9309fe02
...
@@ -290,6 +290,7 @@ class PyObjectConst(object):
...
@@ -290,6 +290,7 @@ class PyObjectConst(object):
possible_unicode_identifier
=
re
.
compile
(
ur"(?![0-9])\
w+$
", re.U).match
possible_unicode_identifier
=
re
.
compile
(
ur"(?![0-9])\
w+$
", re.U).match
possible_bytes_identifier = re.compile(r"
(
?!
[
0
-
9
])
\
w
+
$
".encode('ASCII')).match
possible_bytes_identifier = re.compile(r"
(
?!
[
0
-
9
])
\
w
+
$
".encode('ASCII')).match
nice_identifier = re.compile('^[a-zA-Z0-9_]+$').match
nice_identifier = re.compile('^[a-zA-Z0-9_]+$').match
find_alphanums = re.compile('([a-zA-Z0-9]+)').findall
class StringConst(object):
class StringConst(object):
"""Global info about a C string constant held by GlobalState.
"""Global info about a C string constant held by GlobalState.
...
@@ -307,18 +308,28 @@ class StringConst(object):
...
@@ -307,18 +308,28 @@ class StringConst(object):
def get_py_string_const(self, encoding, identifier=None, is_str=False):
def get_py_string_const(self, encoding, identifier=None, is_str=False):
py_strings = self.py_strings
py_strings = self.py_strings
text = self.text
text = self.text
if encoding is not None:
encoding = encoding.upper()
is_str = bool(identifier or is_str)
is_str = bool(identifier or is_str)
is_unicode = encoding is None and not is_str
key = (is_str, encoding)
if encoding is None:
# unicode string
encoding_key = None
else:
# bytes or str
encoding = encoding.lower()
if encoding in ('utf8', 'utf-8', 'ascii', 'usascii', 'us-ascii'):
encoding = None
encoding_key = None
else:
encoding_key = ''.join(find_alphanums(encoding))
key = (is_str, is_unicode, encoding_key)
if py_strings is not None and key in py_strings:
if py_strings is not None and key in py_strings:
py_string = py_strings[key]
py_string = py_strings[key]
else:
else:
if py_strings is None:
if py_strings is None:
self.py_strings = {}
self.py_strings = {}
is_unicode = encoding is None and not is_str
if identifier:
if identifier:
intern = True
intern = True
elif identifier is None:
elif identifier is None:
...
@@ -588,7 +599,7 @@ class GlobalState(object):
...
@@ -588,7 +599,7 @@ class GlobalState(object):
return self.new_const_cname()
return self.new_const_cname()
if len(value) < 20 and nice_identifier(value):
if len(value) < 20 and nice_identifier(value):
return "
%
s
%
s
" % (Naming.const_prefix, value)
return "
%
s
_
%
s
" % (Naming.const_prefix, value)
else:
else:
return self.new_const_cname()
return self.new_const_cname()
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment