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
29c1030e
Commit
29c1030e
authored
Mar 22, 2016
by
Stefan Behnel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
simplify code generated for f-string formatting using simple utility functions
parent
0d7af347
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
33 additions
and
26 deletions
+33
-26
Cython/Compiler/ExprNodes.py
Cython/Compiler/ExprNodes.py
+13
-25
Cython/Utility/ModuleSetupCode.c
Cython/Utility/ModuleSetupCode.c
+0
-1
Cython/Utility/StringTools.c
Cython/Utility/StringTools.c
+20
-0
No files found.
Cython/Compiler/ExprNodes.py
View file @
29c1030e
...
...
@@ -3029,40 +3029,28 @@ class FormattedValueNode(ExprNode):
return
self
def
generate_result_code
(
self
,
code
):
conversion_result
=
value_result
=
self
.
value
.
py_result
()
value_result
=
self
.
value
.
py_result
()
# common case: expect Unicode pass-through if no format spec
no_format_spec
=
isinstance
(
self
.
format_spec
,
UnicodeNode
)
and
not
self
.
format_spec
.
value
.
strip
()
if
self
.
conversion_char
:
fn
=
self
.
find_conversion_func
(
self
.
conversion_char
)
if
fn
is
None
:
assert
False
,
"invalid conversion character found: '%s'"
%
self
.
conversion_char
code
.
putln
(
'{'
)
conversion_result
=
Naming
.
quick_temp_cname
code
.
putln
(
'PyObject *%s = %s(%s); %s'
%
(
conversion_result
,
fn
,
value_result
,
code
.
error_goto_if_null
(
conversion_result
,
self
.
pos
)
))
code
.
put_gotref
(
conversion_result
)
if
isinstance
(
self
.
format_spec
,
UnicodeNode
)
and
not
self
.
format_spec
.
value
:
# common case: no format spec => expect Unicode pass-through
assert
fn
is
not
None
,
"invalid conversion character found: '%s'"
%
self
.
conversion_char
value_result
=
'%s(%s)'
%
(
fn
,
value_result
)
code
.
globalstate
.
use_utility_code
(
UtilityCode
.
load_cached
(
"PyObjectFormatAndDecref"
,
"StringTools.c"
))
format_func
=
'__Pyx_PyObject_FormatSimpleAndDecref'
if
no_format_spec
else
'__Pyx_PyObject_FormatAndDecref'
elif
no_format_spec
:
code
.
globalstate
.
use_utility_code
(
UtilityCode
.
load_cached
(
"PyObjectFormatSimple"
,
"StringTools.c"
))
format_func
=
'__Pyx_PyObject_FormatSimple'
else
:
format_func
=
'PyObject_Format'
code
.
put
(
"%s = %s(%s, %s);
"
%
(
code
.
put
ln
(
"%s = %s(%s, %s); %s
"
%
(
self
.
result
(),
format_func
,
conversion_result
,
self
.
format_spec
.
py_result
()))
if
self
.
conversion_char
:
code
.
put_decref
(
conversion_result
,
py_object_type
)
code
.
putln
(
'}'
)
code
.
putln
(
code
.
error_goto_if_null
(
self
.
result
(),
self
.
pos
))
value_result
,
self
.
format_spec
.
py_result
(),
code
.
error_goto_if_null
(
self
.
result
(),
self
.
pos
)))
code
.
put_gotref
(
self
.
py_result
())
...
...
Cython/Utility/ModuleSetupCode.c
View file @
29c1030e
...
...
@@ -123,7 +123,6 @@
#define PyObject_Realloc(p) PyMem_Realloc(p)
#endif
#define __Pyx_PyObject_FormatSimple(s, f) (likely(PyUnicode_CheckExact(s)) ? (Py_INCREF(s), s) : PyObject_Format(s, f))
#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))
...
...
Cython/Utility/StringTools.c
View file @
29c1030e
...
...
@@ -811,3 +811,23 @@ static CYTHON_INLINE int __Pyx_PyByteArray_Append(PyObject* bytearray, int value
Py_DECREF
(
retval
);
return
0
;
}
//////////////////// PyObjectFormatSimple.proto ////////////////////
#define __Pyx_PyObject_FormatSimple(s, f) (likely(PyUnicode_CheckExact(s)) ? (Py_INCREF(s), s) : PyObject_Format(s, f))
//////////////////// PyObjectFormatAndDecref.proto ////////////////////
#define __Pyx_PyObject_FormatSimpleAndDecref(s, f) \
((unlikely(!s) || likely(PyUnicode_CheckExact(s))) ? s : __Pyx_PyObject_FormatAndDecref(s, f))
static
CYTHON_INLINE
PyObject
*
__Pyx_PyObject_FormatAndDecref
(
PyObject
*
s
,
PyObject
*
f
);
//////////////////// PyObjectFormatAndDecref ////////////////////
static
CYTHON_INLINE
PyObject
*
__Pyx_PyObject_FormatAndDecref
(
PyObject
*
s
,
PyObject
*
f
)
{
PyObject
*
result
=
PyObject_Format
(
s
,
f
);
Py_DECREF
(
s
);
return
result
;
}
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