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
Labels
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
nexedi
cython
Commits
fcee9e0e
Commit
fcee9e0e
authored
Dec 09, 2012
by
Stefan Behnel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
moved Py_UCS4/Py_UNICODE type conversion helper functions to utility code file
parent
d48da4fd
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
97 additions
and
99 deletions
+97
-99
Cython/Compiler/PyrexTypes.py
Cython/Compiler/PyrexTypes.py
+2
-99
Cython/Utility/TypeConversion.c
Cython/Utility/TypeConversion.c
+95
-0
No files found.
Cython/Compiler/PyrexTypes.py
View file @
fcee9e0e
...
...
@@ -1661,65 +1661,13 @@ class CPyUCS4IntType(CIntType):
from_py_function
=
"__Pyx_PyObject_AsPy_UCS4"
def
create_from_py_utility_code
(
self
,
env
):
env
.
use_utility_code
(
pyobject_as_py_ucs4_utility_code
)
env
.
use_utility_code
(
UtilityCode
.
load_cached
(
"ObjectAsUCS4"
,
"TypeConversion.c"
)
)
return
True
def
sign_and_name
(
self
):
return
"Py_UCS4"
pyobject_as_py_ucs4_utility_code
=
UtilityCode
(
proto
=
'''
static CYTHON_INLINE Py_UCS4 __Pyx_PyObject_AsPy_UCS4(PyObject*);
'''
,
impl
=
'''
static CYTHON_INLINE Py_UCS4 __Pyx_PyObject_AsPy_UCS4(PyObject* x) {
long ival;
if (PyUnicode_Check(x)) {
Py_ssize_t length;
#if CYTHON_PEP393_ENABLED
length = PyUnicode_GET_LENGTH(x);
if (likely(length == 1)) {
return PyUnicode_READ_CHAR(x, 0);
}
#else
length = PyUnicode_GET_SIZE(x);
if (likely(length == 1)) {
return PyUnicode_AS_UNICODE(x)[0];
}
#if Py_UNICODE_SIZE == 2
else if (PyUnicode_GET_SIZE(x) == 2) {
Py_UCS4 high_val = PyUnicode_AS_UNICODE(x)[0];
if (high_val >= 0xD800 && high_val <= 0xDBFF) {
Py_UCS4 low_val = PyUnicode_AS_UNICODE(x)[1];
if (low_val >= 0xDC00 && low_val <= 0xDFFF) {
return 0x10000 + (((high_val & ((1<<10)-1)) << 10) | (low_val & ((1<<10)-1)));
}
}
}
#endif
#endif
PyErr_Format(PyExc_ValueError,
"only single character unicode strings can be converted to Py_UCS4, "
"got length %" CYTHON_FORMAT_SSIZE_T "d", length);
return (Py_UCS4)-1;
}
ival = __Pyx_PyInt_AsLong(x);
if (unlikely(ival < 0)) {
if (!PyErr_Occurred())
PyErr_SetString(PyExc_OverflowError,
"cannot convert negative value to Py_UCS4");
return (Py_UCS4)-1;
} else if (unlikely(ival > 1114111)) {
PyErr_SetString(PyExc_OverflowError,
"value too large to convert to Py_UCS4");
return (Py_UCS4)-1;
}
return (Py_UCS4)ival;
}
'''
)
class
CPyUnicodeIntType
(
CIntType
):
# Py_UNICODE
...
...
@@ -1734,57 +1682,12 @@ class CPyUnicodeIntType(CIntType):
from_py_function
=
"__Pyx_PyObject_AsPy_UNICODE"
def
create_from_py_utility_code
(
self
,
env
):
env
.
use_utility_code
(
pyobject_as_py_unicode_utility_code
)
env
.
use_utility_code
(
UtilityCode
.
load_cached
(
"ObjectAsPyUnicode"
,
"TypeConversion.c"
)
)
return
True
def
sign_and_name
(
self
):
return
"Py_UNICODE"
pyobject_as_py_unicode_utility_code
=
UtilityCode
(
proto
=
'''
static CYTHON_INLINE Py_UNICODE __Pyx_PyObject_AsPy_UNICODE(PyObject*);
'''
,
impl
=
'''
static CYTHON_INLINE Py_UNICODE __Pyx_PyObject_AsPy_UNICODE(PyObject* x) {
long ival;
#if CYTHON_PEP393_ENABLED
const long maxval = 1114111;
#else
static long maxval = 0;
#endif
if (PyUnicode_Check(x)) {
if (unlikely(__Pyx_PyUnicode_GET_LENGTH(x) != 1)) {
PyErr_Format(PyExc_ValueError,
"only single character unicode strings can be converted to Py_UNICODE, "
"got length %" CYTHON_FORMAT_SSIZE_T "d", __Pyx_PyUnicode_GET_LENGTH(x));
return (Py_UNICODE)-1;
}
#if CYTHON_PEP393_ENABLED
ival = PyUnicode_READ_CHAR(x, 0);
#else
return PyUnicode_AS_UNICODE(x)[0];
#endif
} else {
#if !CYTHON_PEP393_ENABLED
if (unlikely(!maxval))
maxval = (long)PyUnicode_GetMax();
#endif
ival = __Pyx_PyInt_AsLong(x);
}
if (unlikely(ival < 0)) {
if (!PyErr_Occurred())
PyErr_SetString(PyExc_OverflowError,
"cannot convert negative value to Py_UNICODE");
return (Py_UNICODE)-1;
} else if (unlikely(ival > maxval)) {
PyErr_SetString(PyExc_OverflowError,
"value too large to convert to Py_UNICODE");
return (Py_UNICODE)-1;
}
return (Py_UNICODE)ival;
}
'''
)
class
CPyHashTType
(
CIntType
):
...
...
Cython/Utility/TypeConversion.c
View file @
fcee9e0e
...
...
@@ -34,3 +34,98 @@ bad:
return
result
;
}
/////////////// ObjectAsUCS4.proto ///////////////
static
CYTHON_INLINE
Py_UCS4
__Pyx_PyObject_AsPy_UCS4
(
PyObject
*
);
/////////////// ObjectAsUCS4 ///////////////
static
CYTHON_INLINE
Py_UCS4
__Pyx_PyObject_AsPy_UCS4
(
PyObject
*
x
)
{
long
ival
;
if
(
PyUnicode_Check
(
x
))
{
Py_ssize_t
length
;
#if CYTHON_PEP393_ENABLED
length
=
PyUnicode_GET_LENGTH
(
x
);
if
(
likely
(
length
==
1
))
{
return
PyUnicode_READ_CHAR
(
x
,
0
);
}
#else
length
=
PyUnicode_GET_SIZE
(
x
);
if
(
likely
(
length
==
1
))
{
return
PyUnicode_AS_UNICODE
(
x
)[
0
];
}
#if Py_UNICODE_SIZE == 2
else
if
(
PyUnicode_GET_SIZE
(
x
)
==
2
)
{
Py_UCS4
high_val
=
PyUnicode_AS_UNICODE
(
x
)[
0
];
if
(
high_val
>=
0xD800
&&
high_val
<=
0xDBFF
)
{
Py_UCS4
low_val
=
PyUnicode_AS_UNICODE
(
x
)[
1
];
if
(
low_val
>=
0xDC00
&&
low_val
<=
0xDFFF
)
{
return
0x10000
+
(((
high_val
&
((
1
<<
10
)
-
1
))
<<
10
)
|
(
low_val
&
((
1
<<
10
)
-
1
)));
}
}
}
#endif
#endif
PyErr_Format
(
PyExc_ValueError
,
"only single character unicode strings can be converted to Py_UCS4, "
"got length %"
CYTHON_FORMAT_SSIZE_T
"d"
,
length
);
return
(
Py_UCS4
)
-
1
;
}
ival
=
__Pyx_PyInt_AsLong
(
x
);
if
(
unlikely
(
ival
<
0
))
{
if
(
!
PyErr_Occurred
())
PyErr_SetString
(
PyExc_OverflowError
,
"cannot convert negative value to Py_UCS4"
);
return
(
Py_UCS4
)
-
1
;
}
else
if
(
unlikely
(
ival
>
1114111
))
{
PyErr_SetString
(
PyExc_OverflowError
,
"value too large to convert to Py_UCS4"
);
return
(
Py_UCS4
)
-
1
;
}
return
(
Py_UCS4
)
ival
;
}
/////////////// ObjectAsPyUnicode.proto ///////////////
static
CYTHON_INLINE
Py_UNICODE
__Pyx_PyObject_AsPy_UNICODE
(
PyObject
*
);
/////////////// ObjectAsPyUnicode ///////////////
static
CYTHON_INLINE
Py_UNICODE
__Pyx_PyObject_AsPy_UNICODE
(
PyObject
*
x
)
{
long
ival
;
#if CYTHON_PEP393_ENABLED
const
long
maxval
=
1114111
;
#else
static
long
maxval
=
0
;
#endif
if
(
PyUnicode_Check
(
x
))
{
if
(
unlikely
(
__Pyx_PyUnicode_GET_LENGTH
(
x
)
!=
1
))
{
PyErr_Format
(
PyExc_ValueError
,
"only single character unicode strings can be converted to Py_UNICODE, "
"got length %"
CYTHON_FORMAT_SSIZE_T
"d"
,
__Pyx_PyUnicode_GET_LENGTH
(
x
));
return
(
Py_UNICODE
)
-
1
;
}
#if CYTHON_PEP393_ENABLED
ival
=
PyUnicode_READ_CHAR
(
x
,
0
);
#else
return
PyUnicode_AS_UNICODE
(
x
)[
0
];
#endif
}
else
{
#if !CYTHON_PEP393_ENABLED
if
(
unlikely
(
!
maxval
))
maxval
=
(
long
)
PyUnicode_GetMax
();
#endif
ival
=
__Pyx_PyInt_AsLong
(
x
);
}
if
(
unlikely
(
ival
<
0
))
{
if
(
!
PyErr_Occurred
())
PyErr_SetString
(
PyExc_OverflowError
,
"cannot convert negative value to Py_UNICODE"
);
return
(
Py_UNICODE
)
-
1
;
}
else
if
(
unlikely
(
ival
>
maxval
))
{
PyErr_SetString
(
PyExc_OverflowError
,
"value too large to convert to Py_UNICODE"
);
return
(
Py_UNICODE
)
-
1
;
}
return
(
Py_UNICODE
)
ival
;
}
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