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
01fd4fe4
Commit
01fd4fe4
authored
May 10, 2018
by
gabrieldemarmiesse
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Transfered type casting
parent
eb18ac4c
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
42 additions
and
0 deletions
+42
-0
docs/src/userguide/language_basics.rst
docs/src/userguide/language_basics.rst
+42
-0
No files found.
docs/src/userguide/language_basics.rst
View file @
01fd4fe4
...
...
@@ -381,6 +381,48 @@ Sometimes Cython will complain unnecessarily, and sometimes it will fail to
detect a problem that exists. Ultimately, you need to understand the issue and
be careful what you do.
Type Casting
------------
As C uses the ``"("`` and ``")"`` for casting types, Cython uses ``"<"`` and ``">"``
to signify a casting. For example::
cdef char *p
cdef float *q
p = <char*>q
If one of the types is a Python object for ``<type>x``, Cython will try to do a coercion.
.. note:: Cython will not stop a casting where there is no conversion, but it will emit a warning.
To get the address of some Python object, use a cast to a pointer type
like ``<void*>`` or ``<PyObject*>``.
You can also get the python object from a pointer to this object by doing ``<object>``.
Here is an example::
from __future__ import print_function
from cpython.ref cimport PyObject
python_string = "foo"
cdef void* ptr = <void*>python_string
cdef long adress_in_c = <long>ptr
address_from_void = adress_in_c # address_from_void is a python int
cdef PyObject* ptr2 = <PyObject*>python_string
cdef long address_in_c2 = <long>ptr2
address_from_PyObject = address_in_c2 # address_from_PyObject is a python int
assert address_from_void == address_from_PyObject == id(python_string)
print(<object>ptr) # Prints "foo"
print(<object>ptr2) # prints "foo"
The precedence of ``<...>`` is such that ``<type>a.b.c`` is interpreted as ``<type>(a.b.c)``.
Statements and expressions
==========================
...
...
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