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
Kirill Smelkov
cython
Commits
ed6a7e2e
Commit
ed6a7e2e
authored
Jul 06, 2018
by
scoder
Committed by
GitHub
Jul 06, 2018
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #2419 from gabrieldemarmiesse/test_language_basics_7
Added tests for "language basics" part 7
parents
96b2d936
b0f153e3
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
29 additions
and
26 deletions
+29
-26
docs/examples/userguide/language_basics/casting_python.pyx
docs/examples/userguide/language_basics/casting_python.pyx
+17
-0
docs/examples/userguide/language_basics/compile_time.pyx
docs/examples/userguide/language_basics/compile_time.pyx
+9
-0
docs/src/userguide/language_basics.rst
docs/src/userguide/language_basics.rst
+3
-26
No files found.
docs/examples/userguide/language_basics/casting_python.pyx
0 → 100644
View file @
ed6a7e2e
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"
docs/examples/userguide/language_basics/compile_time.pyx
0 → 100644
View file @
ed6a7e2e
from
__future__
import
print_function
DEF
FavouriteFood
=
u"spam"
DEF
ArraySize
=
42
DEF
OtherArraySize
=
2
*
ArraySize
+
17
cdef
int
a1
[
ArraySize
]
cdef
int
a2
[
OtherArraySize
]
print
(
"I like"
,
FavouriteFood
)
\ No newline at end of file
docs/src/userguide/language_basics.rst
View file @
ed6a7e2e
...
@@ -626,26 +626,9 @@ You can also cast a C pointer back to a Python object reference
...
@@ -626,26 +626,9 @@ You can also cast a C pointer back to a Python object reference
with ``<object>``, or a more specific builtin or extension type
with ``<object>``, or a more specific builtin or extension type
(e.g. ``<MyExtType>ptr``). This will increase the reference count of
(e.g. ``<MyExtType>ptr``). This will increase the reference count of
the object by one, i.e. the cast returns an owned reference.
the object by one, i.e. the cast returns an owned reference.
Here is an example::
Here is an example:
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"
.. literalinclude:: ../../examples/userguide/language_basics/casting_python.pyx
The precedence of ``<...>`` is such that ``<type>a.b.c`` is interpreted as ``<type>(a.b.c)``.
The precedence of ``<...>`` is such that ``<type>a.b.c`` is interpreted as ``<type>(a.b.c)``.
...
@@ -965,13 +948,7 @@ the source at that point as a literal. For this to work, the compile-time
...
@@ -965,13 +948,7 @@ the source at that point as a literal. For this to work, the compile-time
expression must evaluate to a Python value of type ``int``, ``long``,
expression must evaluate to a Python value of type ``int``, ``long``,
``float``, ``bytes`` or ``unicode`` (``str`` in Py3).
``float``, ``bytes`` or ``unicode`` (``str`` in Py3).
::
.. literalinclude:: ../../examples/userguide/language_basics/compile_time.pyx
from __future__ import print_function
cdef int a1[ArraySize]
cdef int a2[OtherArraySize]
print("I like", FavouriteFood)
Conditional Statements
Conditional Statements
----------------------
----------------------
...
...
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