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
Kirill Smelkov
cython
Commits
059eac4e
Commit
059eac4e
authored
Dec 31, 2013
by
Stefan Behnel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
optimise str % formatting
parent
48975c91
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
63 additions
and
2 deletions
+63
-2
Cython/Compiler/ExprNodes.py
Cython/Compiler/ExprNodes.py
+6
-1
Cython/Utility/ModuleSetupCode.c
Cython/Utility/ModuleSetupCode.c
+8
-1
tests/run/strmethods.pyx
tests/run/strmethods.pyx
+49
-0
No files found.
Cython/Compiler/ExprNodes.py
View file @
059eac4e
...
...
@@ -9463,9 +9463,14 @@ class ModNode(DivNode):
def
py_operation_function
(
self
):
if
self
.
operand1
.
type
is
unicode_type
:
if
self
.
operand1
.
may_be_none
():
return
'__Pyx_PyUnicode_Format'
return
'__Pyx_PyUnicode_Format
Safe
'
else
:
return
'PyUnicode_Format'
elif
self
.
operand1
.
type
is
str_type
:
if
self
.
operand1
.
may_be_none
():
return
'__Pyx_PyString_FormatSafe'
else
:
return
'__Pyx_PyString_Format'
return
super
(
ModNode
,
self
).
py_operation_function
()
...
...
Cython/Utility/ModuleSetupCode.c
View file @
059eac4e
...
...
@@ -157,10 +157,17 @@
#define __Pyx_PyUnicode_READ(k, d, i) ((k=k), (Py_UCS4)(((Py_UNICODE*)d)[i]))
#endif
#define __Pyx_PyUnicode_Format(a, b) ((unlikely((a) == Py_None)) ? PyNumber_Remainder(a, b) : PyUnicode_Format(a, b))
#define __Pyx_PyString_FormatSafe(a, b) ((unlikely((a) == Py_None)) ? PyNumber_Remainder(a, b) : __Pyx_PyString_Format(a, b))
#define __Pyx_PyUnicode_FormatSafe(a, b) ((unlikely((a) == Py_None)) ? PyNumber_Remainder(a, b) : PyUnicode_Format(a, b))
#define __Pyx_PyUnicode_Concat(a, b) ((unlikely((a) == Py_None) || unlikely((b) == Py_None)) ? \
PyNumber_Add(a, b) : PyUnicode_Concat(a, b))
#if PY_MAJOR_VERSION >= 3
#define __Pyx_PyString_Format(a, b) PyUnicode_Format(a, b)
#else
#define __Pyx_PyString_Format(a, b) PyString_Format(a, b)
#endif
#if PY_MAJOR_VERSION >= 3
#define PyBaseString_Type PyUnicode_Type
#define PyStringObject PyUnicodeObject
...
...
tests/run/strmethods.pyx
View file @
059eac4e
...
...
@@ -83,3 +83,52 @@ def literal_join(args):
result
=
'|'
.
join
(
args
)
assert
cython
.
typeof
(
result
)
==
'basestring object'
,
cython
.
typeof
(
result
)
return
result
# unicode.__mod__(format, values)
format1
=
'abc%sdef'
format2
=
'abc%sdef%sghi'
def
mod_format
(
str
s
,
values
):
"""
>>> mod_format(format1, 'sa') == 'abcsadef' or mod_format(format1, 'sa')
True
>>> mod_format(format2, ('XYZ', 'ABC')) == 'abcXYZdefABCghi' or mod_format(format2, ('XYZ', 'ABC'))
True
>>> mod_format(None, 'sa')
Traceback (most recent call last):
TypeError: unsupported operand type(s) for %: 'NoneType' and 'str'
>>> class RMod(object):
... def __rmod__(self, other):
... return 123
>>> mod_format(None, RMod())
123
"""
assert
cython
.
typeof
(
s
%
values
)
==
'basestring object'
,
cython
.
typeof
(
s
%
values
)
return
s
%
values
def
mod_format_literal
(
values
):
"""
>>> mod_format_literal('sa') == 'abcsadef' or mod_format(format1, 'sa')
True
>>> mod_format_literal(('sa',)) == 'abcsadef' or mod_format(format1, ('sa',))
True
>>> mod_format_literal(['sa']) == "abc['sa']def" or mod_format(format1, ['sa'])
True
"""
assert
cython
.
typeof
(
'abc%sdef'
%
values
)
==
'basestring object'
,
cython
.
typeof
(
'abc%sdef'
%
values
)
return
'abc%sdef'
%
values
def
mod_format_tuple
(
*
values
):
"""
>>> mod_format_tuple('sa') == 'abcsadef' or mod_format(format1, 'sa')
True
>>> mod_format_tuple()
Traceback (most recent call last):
TypeError: not enough arguments for format string
"""
assert
cython
.
typeof
(
'abc%sdef'
%
values
)
==
'basestring object'
,
cython
.
typeof
(
'abc%sdef'
%
values
)
return
'abc%sdef'
%
values
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