Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
TASTE
uPython-mirror
Commits
809eaa26
Commit
809eaa26
authored
Jan 29, 2014
by
Paul Sokolovsky
Browse files
Add FFI module example.
parent
60a9fac8
Changes
1
Hide whitespace changes
Inline
Side-by-side
examples/unix/ffi_example.py
0 → 100644
View file @
809eaa26
import
ffi
libc
=
ffi
.
open
(
"libc.so.6"
)
print
(
"libc:"
,
libc
)
print
()
# Declare few functions
perror
=
libc
.
func
(
"v"
,
"perror"
,
[
"s"
])
time
=
libc
.
func
(
"i"
,
"time"
,
"p"
)
open
=
libc
.
func
(
"i"
,
"open"
,
[
"s"
,
"i"
])
qsort
=
libc
.
func
(
"v"
,
"qsort"
,
"piip"
)
# And one variable
errno
=
libc
.
var
(
"i"
,
"errno"
)
print
(
"time:"
,
time
)
print
(
"UNIX time is:"
,
time
(
None
))
print
()
perror
(
"ffi before error"
)
open
(
"somethingnonexistent__"
,
0
)
print
(
errno
)
perror
(
"ffi after error"
)
print
()
def
cmp
(
pa
,
pb
):
a
=
ffi
.
as_bytearray
(
pa
,
1
)
b
=
ffi
.
as_bytearray
(
pb
,
1
)
print
(
"cmp:"
,
a
,
b
)
return
a
[
0
]
-
b
[
0
]
cmp_c
=
ffi
.
callback
(
"i"
,
cmp
,
"pp"
)
print
(
"callback:"
,
cmp_c
)
# TODO: violates Py semantics, pass bytearray
s
=
"foobar"
print
(
"org string:"
,
s
)
qsort
(
s
,
len
(
s
),
1
,
cmp_c
)
print
(
"qsort'ed:"
,
s
)
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