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
Boxiang Sun
cython
Commits
6ffbfc5e
Commit
6ffbfc5e
authored
May 17, 2018
by
scoder
Committed by
GitHub
May 17, 2018
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #2260 from gabrieldemarmiesse/transfered_type_casting
Transfered type casting
parents
b74716a2
bebb9b54
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
46 additions
and
0 deletions
+46
-0
docs/src/userguide/language_basics.rst
docs/src/userguide/language_basics.rst
+46
-0
No files found.
docs/src/userguide/language_basics.rst
View file @
6ffbfc5e
...
...
@@ -596,6 +596,52 @@ detect a problem that exists. Ultimately, you need to understand the issue and
be careful what you do.
Type Casting
------------
Where C uses ``"("`` and ``")"``, Cython uses ``"<"`` and ``">"``. For example::
cdef char *p
cdef float *q
p = <char*>q
When casting a C value to a Python object type or vice versa,
Cython will attempt a coercion. Simple examples are casts like ``<int>pyobj``,
which converts a Python number to a plain C ``int`` value, or ``<bytes>charptr``,
which copies a C ``char*`` string into a new Python bytes object.
.. note:: Cython will not prevent a redundant cast, but emits a warning for it.
To get the address of some Python object, use a cast to a pointer type
like ``<void*>`` or ``<PyObject*>``.
You can also cast a C pointer back to a Python object reference
with ``<object>``, or a more specific builtin or extension type
(e.g. ``<MyExtType>ptr``). This will increase the reference count of
the object by one, i.e. the cast returns an owned reference.
Here is an example::
from __future__ import print_function
from cpython.ref cimport PyObject
from libc.stdint cimport uintptr_t
python_string = "foo"
cdef void* ptr = <void*>python_string
cdef uintptr_t adress_in_c = <uintptr_t>ptr
address_from_void = adress_in_c # address_from_void is a python int
cdef PyObject* ptr2 = <PyObject*>python_string
cdef uintptr_t address_in_c2 = <uintptr_t>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)``.
Checked Type Casts
------------------
...
...
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