Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
C
cpython
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
cpython
Commits
b69a3c2b
Kaydet (Commit)
b69a3c2b
authored
Tem 14, 2006
tarafından
Thomas Heller
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Updates for the ctypes documentation.
üst
ce049a0a
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
27 additions
and
37 deletions
+27
-37
libctypes.tex
Doc/lib/libctypes.tex
+27
-37
No files found.
Doc/lib/libctypes.tex
Dosyayı görüntüle @
b69a3c2b
...
@@ -790,10 +790,6 @@ Initializers of the correct type can also be specified:
...
@@ -790,10 +790,6 @@ Initializers of the correct type can also be specified:
\subsubsection
{
Pointers
\label
{
ctypes-pointers
}}
\subsubsection
{
Pointers
\label
{
ctypes-pointers
}}
XXX Rewrite this section. Normally one only uses indexing, not the .contents
attribute!
List some recipes with pointers. bool(ptr), POINTER(tp)(), ...?
Pointer instances are created by calling the
\code
{
pointer
}
function on a
Pointer instances are created by calling the
\code
{
pointer
}
function on a
\code
{
ctypes
}
type:
\code
{
ctypes
}
type:
\begin{verbatim}
\begin{verbatim}
...
@@ -826,7 +822,8 @@ Assigning another \class{c{\_}int} instance to the pointer's contents
...
@@ -826,7 +822,8 @@ Assigning another \class{c{\_}int} instance to the pointer's contents
attribute would cause the pointer to point to the memory location
attribute would cause the pointer to point to the memory location
where this is stored:
where this is stored:
\begin{verbatim}
\begin{verbatim}
>>> pi.contents = c
_
int(99)
>>> i = c
_
int(99)
>>> pi.contents = i
>>> pi.contents
>>> pi.contents
c
_
long(99)
c
_
long(99)
>>>
>>>
...
@@ -855,9 +852,6 @@ memory locations. Generally you only use this feature if you receive a
...
@@ -855,9 +852,6 @@ memory locations. Generally you only use this feature if you receive a
pointer from a C function, and you
\emph
{
know
}
that the pointer actually
pointer from a C function, and you
\emph
{
know
}
that the pointer actually
points to an array instead of a single item.
points to an array instead of a single item.
\subsubsection
{
Pointer classes/types
\label
{
ctypes-pointer-classestypes
}}
Behind the scenes, the
\code
{
pointer
}
function does more than simply
Behind the scenes, the
\code
{
pointer
}
function does more than simply
create pointer instances, it has to create pointer
\emph
{
types
}
first.
create pointer instances, it has to create pointer
\emph
{
types
}
first.
This is done with the
\code
{
POINTER
}
function, which accepts any
This is done with the
\code
{
POINTER
}
function, which accepts any
...
@@ -875,6 +869,31 @@ TypeError: expected c_long instead of int
...
@@ -875,6 +869,31 @@ TypeError: expected c_long instead of int
>>>
>>>
\end{verbatim}
\end{verbatim}
Calling the pointer type without an argument creates a
\code
{
NULL
}
pointer.
\code
{
NULL
}
pointers have a
\code
{
False
}
boolean value:
\begin{verbatim}
>>> null
_
ptr = POINTER(c
_
int)()
>>> print bool(null
_
ptr)
False
>>>
\end{verbatim}
\code
{
ctypes
}
checks for
\code
{
NULL
}
when dereferencing pointers (but
dereferencing non-
\code
{
NULL
}
pointers would crash Python):
\begin{verbatim}
>>> null
_
ptr[0]
Traceback (most recent call last):
....
ValueError: NULL pointer access
>>>
>>> null
_
ptr[0] = 1234
Traceback (most recent call last):
....
ValueError: NULL pointer access
>>>
\end{verbatim}
\subsubsection
{
Type conversions
\label
{
ctypes-type-conversions
}}
\subsubsection
{
Type conversions
\label
{
ctypes-type-conversions
}}
...
@@ -1357,35 +1376,6 @@ IndexError: invalid index
...
@@ -1357,35 +1376,6 @@ IndexError: invalid index
>>>
>>>
\end{verbatim}
\end{verbatim}
The solution is to use 1-element arrays; as a special case ctypes does
no bounds checking on them:
\begin{verbatim}
>>> short
_
array = (c
_
short * 1)()
>>> print sizeof(short
_
array)
2
>>> resize(short
_
array, 32)
>>> sizeof(short
_
array)
32
>>> sizeof(type(short
_
array))
2
>>> short
_
array[0:8]
[0, 0, 0, 0, 0, 0, 0, 0]
>>> short
_
array[7] = 42
>>> short
_
array[0:8]
[0, 0, 0, 0, 0, 0, 0, 42]
>>>
\end{verbatim}
Using 1-element arrays as variable sized fields in structures works as
well, but they should be used as the last field in the structure
definition. This example shows a definition from the Windows header
files:
\begin{verbatim}
class SP
_
DEVICE
_
INTERFACE
_
DETAIL
_
DATA(Structure):
_
fields
_
= [("cbSize", c
_
int),
("DevicePath", c
_
char * 1)]
\end{verbatim}
Another way to use variable-sized data types with
\code
{
ctypes
}
is to use
Another way to use variable-sized data types with
\code
{
ctypes
}
is to use
the dynamic nature of Python, and (re-)define the data type after the
the dynamic nature of Python, and (re-)define the data type after the
required size is already known, on a case by case basis.
required size is already known, on a case by case basis.
...
...
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