Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Open sidebar
Kirill Smelkov
cython
Commits
2c8e7b22
Unverified
Commit
2c8e7b22
authored
4 years ago
by
William Ayd
Committed by
GitHub
4 years ago
Browse files
Options
Download
Email Patches
Plain Diff
Optimize builtin str() calls (GH-3478)
parent
e9d7fd45
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
83 additions
and
0 deletions
+83
-0
Cython/Compiler/Optimize.py
Cython/Compiler/Optimize.py
+32
-0
Cython/Utility/StringTools.c
Cython/Utility/StringTools.c
+19
-0
tests/run/strfunction.pyx
tests/run/strfunction.pyx
+32
-0
No files found.
Cython/Compiler/Optimize.py
View file @
2c8e7b22
...
...
@@ -2312,6 +2312,38 @@ class OptimizeBuiltinCalls(Visitor.NodeRefCleanupMixin,
return
ExprNodes
.
CachedBuiltinMethodCallNode
(
node
,
function
.
obj
,
attr_name
,
arg_list
)
PyObject_String_func_type
=
PyrexTypes
.
CFuncType
(
Builtin
.
str_type
,
[
PyrexTypes
.
CFuncTypeArg
(
"obj"
,
PyrexTypes
.
py_object_type
,
None
)
])
def
_handle_simple_function_str
(
self
,
node
,
function
,
pos_args
):
"""Optimize single argument calls to str().
"""
if
len
(
pos_args
)
!=
1
:
if
len
(
pos_args
)
==
0
:
return
ExprNodes
.
StringNode
(
node
.
pos
,
value
=
EncodedString
(),
constant_result
=
''
)
return
node
arg
=
pos_args
[
0
]
if
arg
.
type
is
Builtin
.
str_type
:
if
not
arg
.
may_be_none
():
return
arg
cname
=
"__Pyx_PyStr_Str"
utility_code
=
UtilityCode
.
load_cached
(
'PyStr_Str'
,
'StringTools.c'
)
else
:
cname
=
'__Pyx_PyObject_Str'
utility_code
=
UtilityCode
.
load_cached
(
'PyObject_Str'
,
'StringTools.c'
)
return
ExprNodes
.
PythonCapiCallNode
(
node
.
pos
,
cname
,
self
.
PyObject_String_func_type
,
args
=
pos_args
,
is_temp
=
node
.
is_temp
,
utility_code
=
utility_code
,
py_name
=
"str"
)
PyObject_Unicode_func_type
=
PyrexTypes
.
CFuncType
(
Builtin
.
unicode_type
,
[
PyrexTypes
.
CFuncTypeArg
(
"obj"
,
PyrexTypes
.
py_object_type
,
None
)
...
...
This diff is collapsed.
Click to expand it.
Cython/Utility/StringTools.c
View file @
2c8e7b22
...
...
@@ -1219,3 +1219,22 @@ static CYTHON_INLINE PyObject* __Pyx_PyUnicode_Unicode(PyObject *obj) {
#define __Pyx_PyObject_Unicode(obj) \
(likely(PyUnicode_CheckExact(obj)) ? __Pyx_NewRef(obj) : PyObject_Unicode(obj))
#endif
//////////////////// PyStr_Str.proto ////////////////////
static
CYTHON_INLINE
PyObject
*
__Pyx_PyStr_Str
(
PyObject
*
obj
);
/*proto*/
//////////////////// PyStr_Str ////////////////////
static
CYTHON_INLINE
PyObject
*
__Pyx_PyStr_Str
(
PyObject
*
obj
)
{
if
(
unlikely
(
obj
==
Py_None
))
obj
=
PYIDENT
(
"None"
);
return
__Pyx_NewRef
(
obj
);
}
//////////////////// PyObject_Str.proto ////////////////////
#define __Pyx_PyObject_Str(obj) \
(likely(PyString_CheckExact(obj)) ? __Pyx_NewRef(obj) : PyObject_Str(obj))
This diff is collapsed.
Click to expand it.
tests/run/strfunction.pyx
View file @
2c8e7b22
...
...
@@ -5,6 +5,8 @@ __doc__ = u"""
'test'
"""
cimport
cython
s
=
str
z
=
str
(
'test'
)
...
...
@@ -39,3 +41,33 @@ def sub(string):
#def csub(string):
# return csubs(string)
@
cython
.
test_fail_if_path_exists
(
"//SimpleCallNode"
)
@
cython
.
test_assert_path_exists
(
"//PythonCapiCallNode"
)
def
typed
(
str
s
):
"""
>>> print(typed(None))
None
>>> type(typed(None)) is type(typed(None))
True
>>> print(typed('abc'))
abc
>>> type(typed('abc')) is type(typed('abc'))
True
"""
return
str
(
s
)
@
cython
.
test_fail_if_path_exists
(
"//SimpleCallNode"
,
"//PythonCapiCallNode"
,
)
def
typed_not_none
(
str
s
not
None
):
"""
>>> print(typed('abc'))
abc
>>> type(typed('abc')) is type(typed('abc'))
True
"""
return
str
(
s
)
This diff is collapsed.
Click to expand it.
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