%!PS-Adobe-2.0
%%Creator: dvips(k) 5.96.1 Copyright 2007 Radical Eye Software
%%Title: paper-letter/ext.dvi
%%CreationDate: Fri Dec 19 16:18:57 2008
%%Pages: 98
%%PageOrder: Ascend
%%BoundingBox: 0 0 612 792
%%DocumentFonts: Helvetica Helvetica-Oblique Times-Roman Times-Bold
%%+ CMSY10 Times-Italic Helvetica-Bold Courier CMSY9 CMMI10
%%DocumentPaperSizes: Letter
%%EndComments
%DVIPSWebPage: (www.radicaleye.com)
%DVIPSCommandLine: dvips -N0 -t letter -o paper-letter/ext.ps
%+ paper-letter/ext.dvi
%DVIPSParameters: dpi=600
%DVIPSSource: TeX output 2008.12.19:1618
%%BeginProcSet: tex.pro 0 0
%!
/TeXDict 300 dict def TeXDict begin/N{def}def/B{bind def}N/S{exch}N/X{S
N}B/A{dup}B/TR{translate}N/isls false N/vsize 11 72 mul N/hsize 8.5 72
mul N/landplus90{false}def/@rigin{isls{[0 landplus90{1 -1}{-1 1}ifelse 0
0 0]concat}if 72 Resolution div 72 VResolution div neg scale isls{
landplus90{VResolution 72 div vsize mul 0 exch}{Resolution -72 div hsize
mul 0}ifelse TR}if Resolution VResolution vsize -72 div 1 add mul TR[
matrix currentmatrix{A A round sub abs 0.00001 lt{round}if}forall round
exch round exch]setmatrix}N/@landscape{/isls true N}B/@manualfeed{
statusdict/manualfeed true put}B/@copies{/#copies X}B/FMat[1 0 0 -1 0 0]
N/FBB[0 0 0 0]N/nn 0 N/IEn 0 N/ctr 0 N/df-tail{/nn 8 dict N nn begin
/FontType 3 N/FontMatrix fntrx N/FontBBox FBB N string/base X array
/BitMaps X/BuildChar{CharBuilder}N/Encoding IEn N end A{/foo setfont}2
array copy cvx N load 0 nn put/ctr 0 N[}B/sf 0 N/df{/sf 1 N/fntrx FMat N
df-tail}B/dfs{div/sf X/fntrx[sf 0 0 sf neg 0 0]N df-tail}B/E{pop nn A
definefont setfont}B/Cw{Cd A length 5 sub get}B/Ch{Cd A length 4 sub get
}B/Cx{128 Cd A length 3 sub get sub}B/Cy{Cd A length 2 sub get 127 sub}
B/Cdx{Cd A length 1 sub get}B/Ci{Cd A type/stringtype ne{ctr get/ctr ctr
1 add N}if}B/CharBuilder{save 3 1 roll S A/base get 2 index get S
/BitMaps get S get/Cd X pop/ctr 0 N Cdx 0 Cx Cy Ch sub Cx Cw add Cy
setcachedevice Cw Ch true[1 0 0 -1 -.1 Cx sub Cy .1 sub]{Ci}imagemask
restore}B/D{/cc X A type/stringtype ne{]}if nn/base get cc ctr put nn
/BitMaps get S ctr S sf 1 ne{A A length 1 sub A 2 index S get sf div put
}if put/ctr ctr 1 add N}B/I{cc 1 add D}B/bop{userdict/bop-hook known{
bop-hook}if/SI save N @rigin 0 0 moveto/V matrix currentmatrix A 1 get A
mul exch 0 get A mul add .99 lt{/QV}{/RV}ifelse load def pop pop}N/eop{
SI restore userdict/eop-hook known{eop-hook}if showpage}N/@start{
userdict/start-hook known{start-hook}if pop/VResolution X/Resolution X
1000 div/DVImag X/IEn 256 array N 2 string 0 1 255{IEn S A 360 add 36 4
index cvrs cvn put}for pop 65781.76 div/vsize X 65781.76 div/hsize X}N
/p{show}N/RMat[1 0 0 -1 0 0]N/BDot 260 string N/Rx 0 N/Ry 0 N/V{}B/RV/v{
/Ry X/Rx X V}B statusdict begin/product where{pop false[(Display)(NeXT)
(LaserWriter 16/600)]{A length product length le{A length product exch 0
exch getinterval eq{pop true exit}if}{pop}ifelse}forall}{false}ifelse
end{{gsave TR -.1 .1 TR 1 1 scale Rx Ry false RMat{BDot}imagemask
grestore}}{{gsave TR -.1 .1 TR Rx Ry scale 1 1 false RMat{BDot}
imagemask grestore}}ifelse B/QV{gsave newpath transform round exch round
exch itransform moveto Rx 0 rlineto 0 Ry neg rlineto Rx neg 0 rlineto
fill grestore}B/a{moveto}B/delta 0 N/tail{A/delta X 0 rmoveto}B/M{S p
delta add tail}B/b{S p tail}B/c{-4 M}B/d{-3 M}B/e{-2 M}B/f{-1 M}B/g{0 M}
B/h{1 M}B/i{2 M}B/j{3 M}B/k{4 M}B/w{0 rmoveto}B/l{p -4 w}B/m{p -3 w}B/n{
p -2 w}B/o{p -1 w}B/q{p 1 w}B/r{p 2 w}B/s{p 3 w}B/t{p 4 w}B/x{0 S
rmoveto}B/y{3 2 roll p a}B/bos{/SS save N}B/eos{SS restore}B end
%%EndProcSet
%%BeginProcSet: 8r.enc 0 0
% File 8r.enc TeX Base 1 Encoding Revision 2.0 2002-10-30
%
% @@psencodingfile@{
% author = "S. Rahtz, P. MacKay, Alan Jeffrey, B. Horn, K. Berry,
% W. Schmidt, P. Lehman",
% version = "2.0",
% date = "27nov06",
% filename = "8r.enc",
% email = "tex-fonts@@tug.org",
% docstring = "This is the encoding vector for Type1 and TrueType
% fonts to be used with TeX. This file is part of the
% PSNFSS bundle, version 9"
% @}
%
% The idea is to have all the characters normally included in Type 1 fonts
% available for typesetting. This is effectively the characters in Adobe
% Standard encoding, ISO Latin 1, Windows ANSI including the euro symbol,
% MacRoman, and some extra characters from Lucida.
%
% Character code assignments were made as follows:
%
% (1) the Windows ANSI characters are almost all in their Windows ANSI
% positions, because some Windows users cannot easily reencode the
% fonts, and it makes no difference on other systems. The only Windows
% ANSI characters not available are those that make no sense for
% typesetting -- rubout (127 decimal), nobreakspace (160), softhyphen
% (173). quotesingle and grave are moved just because it's such an
% irritation not having them in TeX positions.
%
% (2) Remaining characters are assigned arbitrarily to the lower part
% of the range, avoiding 0, 10 and 13 in case we meet dumb software.
%
% (3) Y&Y Lucida Bright includes some extra text characters; in the
% hopes that other PostScript fonts, perhaps created for public
% consumption, will include them, they are included starting at 0x12.
% These are /dotlessj /ff /ffi /ffl.
%
% (4) hyphen appears twice for compatibility with both ASCII and Windows.
%
% (5) /Euro was assigned to 128, as in Windows ANSI
%
% (6) Missing characters from MacRoman encoding incorporated as follows:
%
% PostScript MacRoman TeXBase1
% -------------- -------------- --------------
% /notequal 173 0x16
% /infinity 176 0x17
% /lessequal 178 0x18
% /greaterequal 179 0x19
% /partialdiff 182 0x1A
% /summation 183 0x1B
% /product 184 0x1C
% /pi 185 0x1D
% /integral 186 0x81
% /Omega 189 0x8D
% /radical 195 0x8E
% /approxequal 197 0x8F
% /Delta 198 0x9D
% /lozenge 215 0x9E
%
/TeXBase1Encoding [
% 0x00
/.notdef /dotaccent /fi /fl
/fraction /hungarumlaut /Lslash /lslash
/ogonek /ring /.notdef /breve
/minus /.notdef /Zcaron /zcaron
% 0x10
/caron /dotlessi /dotlessj /ff
/ffi /ffl /notequal /infinity
/lessequal /greaterequal /partialdiff /summation
/product /pi /grave /quotesingle
% 0x20
/space /exclam /quotedbl /numbersign
/dollar /percent /ampersand /quoteright
/parenleft /parenright /asterisk /plus
/comma /hyphen /period /slash
% 0x30
/zero /one /two /three
/four /five /six /seven
/eight /nine /colon /semicolon
/less /equal /greater /question
% 0x40
/at /A /B /C
/D /E /F /G
/H /I /J /K
/L /M /N /O
% 0x50
/P /Q /R /S
/T /U /V /W
/X /Y /Z /bracketleft
/backslash /bracketright /asciicircum /underscore
% 0x60
/quoteleft /a /b /c
/d /e /f /g
/h /i /j /k
/l /m /n /o
% 0x70
/p /q /r /s
/t /u /v /w
/x /y /z /braceleft
/bar /braceright /asciitilde /.notdef
% 0x80
/Euro /integral /quotesinglbase /florin
/quotedblbase /ellipsis /dagger /daggerdbl
/circumflex /perthousand /Scaron /guilsinglleft
/OE /Omega /radical /approxequal
% 0x90
/.notdef /.notdef /.notdef /quotedblleft
/quotedblright /bullet /endash /emdash
/tilde /trademark /scaron /guilsinglright
/oe /Delta /lozenge /Ydieresis
% 0xA0
/.notdef /exclamdown /cent /sterling
/currency /yen /brokenbar /section
/dieresis /copyright /ordfeminine /guillemotleft
/logicalnot /hyphen /registered /macron
% 0xB0
/degree /plusminus /twosuperior /threesuperior
/acute /mu /paragraph /periodcentered
/cedilla /onesuperior /ordmasculine /guillemotright
/onequarter /onehalf /threequarters /questiondown
% 0xC0
/Agrave /Aacute /Acircumflex /Atilde
/Adieresis /Aring /AE /Ccedilla
/Egrave /Eacute /Ecircumflex /Edieresis
/Igrave /Iacute /Icircumflex /Idieresis
% 0xD0
/Eth /Ntilde /Ograve /Oacute
/Ocircumflex /Otilde /Odieresis /multiply
/Oslash /Ugrave /Uacute /Ucircumflex
/Udieresis /Yacute /Thorn /germandbls
% 0xE0
/agrave /aacute /acircumflex /atilde
/adieresis /aring /ae /ccedilla
/egrave /eacute /ecircumflex /edieresis
/igrave /iacute /icircumflex /idieresis
% 0xF0
/eth /ntilde /ograve /oacute
/ocircumflex /otilde /odieresis /divide
/oslash /ugrave /uacute /ucircumflex
/udieresis /yacute /thorn /ydieresis
] def
%%EndProcSet
%%BeginProcSet: texps.pro 0 0
%!
TeXDict begin/rf{findfont dup length 1 add dict begin{1 index/FID ne 2
index/UniqueID ne and{def}{pop pop}ifelse}forall[1 index 0 6 -1 roll
exec 0 exch 5 -1 roll VResolution Resolution div mul neg 0 0]FontType 0
ne{/Metrics exch def dict begin Encoding{exch dup type/integertype ne{
pop pop 1 sub dup 0 le{pop}{[}ifelse}{FontMatrix 0 get div Metrics 0 get
div def}ifelse}forall Metrics/Metrics currentdict end def}{{1 index type
/nametype eq{exit}if exch pop}loop}ifelse[2 index currentdict end
definefont 3 -1 roll makefont/setfont cvx]cvx def}def/ObliqueSlant{dup
sin S cos div neg}B/SlantFont{4 index mul add}def/ExtendFont{3 -1 roll
mul exch}def/ReEncodeFont{CharStrings rcheck{/Encoding false def dup[
exch{dup CharStrings exch known not{pop/.notdef/Encoding true def}if}
forall Encoding{]exch pop}{cleartomark}ifelse}if/Encoding exch def}def
end
%%EndProcSet
%%BeginFont: CMMI10
%!PS-AdobeFont-1.1: CMMI10 1.100
%%CreationDate: 1996 Jul 23 07:53:57
% Copyright (C) 1997 American Mathematical Society. All Rights Reserved.
11 dict begin
/FontInfo 7 dict dup begin
/version (1.100) readonly def
/Notice (Copyright (C) 1997 American Mathematical Society. All Rights Reserved) readonly def
/FullName (CMMI10) readonly def
/FamilyName (Computer Modern) readonly def
/Weight (Medium) readonly def
/ItalicAngle -14.04 def
/isFixedPitch false def
end readonly def
/FontName /CMMI10 def
/PaintType 0 def
/FontType 1 def
/FontMatrix [0.001 0 0 0.001 0 0] readonly def
/Encoding 256 array
0 1 255 {1 index exch /.notdef put} for
dup 62 /greater put
readonly def
/FontBBox{-32 -250 1048 750}readonly def
currentdict end
currentfile eexec
D9D66F633B846A97B686A97E45A3D0AA0529731C99A784CCBE85B4993B2EEBDE
3B12D472B7CF54651EF21185116A69AB1096ED4BAD2F646635E019B6417CC77B
532F85D811C70D1429A19A5307EF63EB5C5E02C89FC6C20F6D9D89E7D91FE470
B72BEFDA23F5DF76BE05AF4CE93137A219ED8A04A9D7D6FDF37E6B7FCDE0D90B
986423E5960A5D9FBB4C956556E8DF90CBFAEC476FA36FD9A5C8175C9AF513FE
D919C2DDD26BDC0D99398B9F4D03D5993DFC0930297866E1CD0A319B6B1FD958
9E394A533A081C36D456A09920001A3D2199583EB9B84B4DEE08E3D12939E321
990CD249827D9648574955F61BAAA11263A91B6C3D47A5190165B0C25ABF6D3E
6EC187E4B05182126BB0D0323D943170B795255260F9FD25F2248D04F45DFBFB
DEF7FF8B19BFEF637B210018AE02572B389B3F76282BEB29CC301905D388C721
59616893E774413F48DE0B408BC66DCE3FE17CB9F84D205839D58014D6A88823
D9320AE93AF96D97A02C4D5A2BB2B8C7925C4578003959C46E3CE1A2F0EAC4BF
8B9B325E46435BDE60BC54D72BC8ACB5C0A34413AC87045DC7B84646A324B808
6FD8E34217213E131C3B1510415CE45420688ED9C1D27890EC68BD7C1235FAF9
1DAB3A369DD2FC3BE5CF9655C7B7EDA7361D7E05E5831B6B8E2EEC542A7B38EE
03BE4BAC6079D038ACB3C7C916279764547C2D51976BABA94BA9866D79F13909
95AA39B0F03103A07CBDF441B8C5669F729020AF284B7FF52A29C6255FCAACF1
74109050FBA2602E72593FBCBFC26E726EE4AEF97B7632BC4F5F353B5C67FED2
3EA752A4A57B8F7FEFF1D7341D895F0A3A0BE1D8E3391970457A967EFF84F6D8
47750B1145B8CC5BD96EE7AA99DDC9E06939E383BDA41175233D58AD263EBF19
AFC0E2F840512D321166547B306C592B8A01E1FA2564B9A26DAC14256414E4C8
42616728D918C74D13C349F4186EC7B9708B86467425A6FDB3A396562F7EE4D8
40B43621744CF8A23A6E532649B66C2A0002DD04F8F39618E4F572819DD34837
B5A08E643FDCA1505AF6A1FA3DDFD1FA758013CAED8ACDDBBB334D664DFF5B53
9560176676ABB71BBD0EE56B4CC492C0652750227CEC7B86E4740EB7B8775564
332769DD30794E501BBB0E4E5CB665F3628E10B1137CC8BC5C0A64A310B5E27E
5FD6E3B04DA3914C15987E638A72790AF4073CE9CDBF6E3C749CB4DFF9C54951
A58C386C54BC4E98B102B5E91E8567D2EEEF048F2CBD5D243701D20909290B4B
A3083F632D8552D42DEE0C69A4B14D8B15AA082DECC12B2ECAE6F663E6D09F81
EE2979EF41FBF12C9D8BF23B77E0A20088EBD107C5BF9DD6F03FFC3AB65B69A7
54953327E1D4AEF5A146273392BBDB321D4CC9A8FFFCFE5C515B466E21546CC7
C6209E5A76F916B03DB98BC6CED334F33E7B373D42761696F5A876CA6F93F16E
15A07E2E102148CA4F62A99C
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
cleartomark
%%EndFont
%%BeginFont: CMSY9
%!PS-AdobeFont-1.1: CMSY9 1.0
%%CreationDate: 1991 Aug 15 07:22:27
% Copyright (C) 1997 American Mathematical Society. All Rights Reserved.
11 dict begin
/FontInfo 7 dict dup begin
/version (1.0) readonly def
/Notice (Copyright (C) 1997 American Mathematical Society. All Rights Reserved) readonly def
/FullName (CMSY9) readonly def
/FamilyName (Computer Modern) readonly def
/Weight (Medium) readonly def
/ItalicAngle -14.035 def
/isFixedPitch false def
end readonly def
/FontName /CMSY9 def
/PaintType 0 def
/FontType 1 def
/FontMatrix [0.001 0 0 0.001 0 0] readonly def
/Encoding 256 array
0 1 255 {1 index exch /.notdef put} for
dup 110 /backslash put
readonly def
/FontBBox{-30 -958 1146 777}readonly def
currentdict end
currentfile eexec
D9D66F633B846A97B686A97E45A3D0AA052F09F9C8ADE9D907C058B87E9B6964
7D53359E51216774A4EAA1E2B58EC3176BD1184A633B951372B4198D4E8C5EF4
A213ACB58AA0A658908035BF2ED8531779838A960DFE2B27EA49C37156989C85
E21B3ABF72E39A89232CD9F4237FC80C9E64E8425AA3BEF7DED60B122A52922A
221A37D9A807DD01161779DDE7D31FF2B87F97C73D63EECDDA4C49501773468A
27D1663E0B62F461F6E40A5D6676D0037D33F24E2FAC2B0009AD3C8350CDF8CC
65BCA87979C36D14CB552E9A985E48BE4E88ECA16DF418749AF04FDD2B0E1380
D281BB2476BB45FF30946B247DFD7F57305FA87E50CA338121C71CDFDF927A9C
77FF14CB4A1D6D80356FB1171ED38C37702350497B44E42CE31DB2F493807DAA
15B887C671199A54C4C1294BC520F5538C15556BC43C9F62342B121C6DCD6C5F
491DA47FF360201EE21C08A781ED0589A6DF91B99FE118B9B29E4F068672E52F
1A06C514D91C4C937D4E642503392B1CD1BE8C2F499A1AD3753691F4D1DAB520
BDD769BBCDAF968237D9A46AD1899410D3594B3AF945E894F1DE5176A6E251CA
0F18B516FC05626A7304DB1E7B5BCF5A9E5EC8BE59C6BBC21B87F6789768C630
5758F0A7B847BBFDB156DDCE8089E499002240DFA847142E7361BBB5301D9021
E17CF2305E7BAA2F556CDF147EACBFCF444772DC14B954EC09BD666280B159BC
8E5C78FE11B8145EA7D6E7799B3F0E7486EA5808A32B6A4068041C23D9D0D8AD
AB1CC078FA184EF3892F5BD9952F9B93ADA2CBC45985503FC7D967A94F374476
53F27028B241C5C7600951D452047B680F935C5A3EEBB8B2515F9C3675ADF0
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
cleartomark
%%EndFont
%%BeginFont: CMSY10
%!PS-AdobeFont-1.1: CMSY10 1.0
%%CreationDate: 1991 Aug 15 07:20:57
% Copyright (C) 1997 American Mathematical Society. All Rights Reserved.
11 dict begin
/FontInfo 7 dict dup begin
/version (1.0) readonly def
/Notice (Copyright (C) 1997 American Mathematical Society. All Rights Reserved) readonly def
/FullName (CMSY10) readonly def
/FamilyName (Computer Modern) readonly def
/Weight (Medium) readonly def
/ItalicAngle -14.035 def
/isFixedPitch false def
end readonly def
/FontName /CMSY10 def
/PaintType 0 def
/FontType 1 def
/FontMatrix [0.001 0 0 0.001 0 0] readonly def
/Encoding 256 array
0 1 255 {1 index exch /.notdef put} for
dup 13 /circlecopyrt put
dup 15 /bullet put
readonly def
/FontBBox{-29 -960 1116 775}readonly def
currentdict end
currentfile eexec
D9D66F633B846A97B686A97E45A3D0AA052F09F9C8ADE9D907C058B87E9B6964
7D53359E51216774A4EAA1E2B58EC3176BD1184A633B951372B4198D4E8C5EF4
A213ACB58AA0A658908035BF2ED8531779838A960DFE2B27EA49C37156989C85
E21B3ABF72E39A89232CD9F4237FC80C9E64E8425AA3BEF7DED60B122A52922A
221A37D9A807DD01161779DDE7D31FF2B87F97C73D63EECDDA4C49501773468A
27D1663E0B62F461F6E40A5D6676D1D12B51E641C1D4E8E2771864FC104F8CBF
5B78EC1D88228725F1C453A678F58A7E1B7BD7CA700717D288EB8DA1F57C4F09
0ABF1D42C5DDD0C384C7E22F8F8047BE1D4C1CC8E33368FB1AC82B4E96146730
DE3302B2E6B819CB6AE455B1AF3187FFE8071AA57EF8A6616B9CB7941D44EC7A
71A7BB3DF755178D7D2E4BB69859EFA4BBC30BD6BB1531133FD4D9438FF99F09
4ECC068A324D75B5F696B8688EEB2F17E5ED34CCD6D047A4E3806D000C199D7C
515DB70A8D4F6146FE068DC1E5DE8BC57036431151EC603C8BCFE359BBD953AD
5F3D998D8365AF76D4CF6A06FB66E1CBCFDB3A67D8880377CD677B9705EE853A
D849F88A828E244FA5816B7D081D4D1975A56D771790C21AE05809BEE6613452
953F26C39CE61F0E134BC35404FE2F0B7D244D9A6F66E4D817D1C65024013AD3
5F242EDE94633B8E20EA42B07F83BA649F12B784B2AAFA542BC597C6C2C9AE9C
39D60FBA6CD16AADB5DFDABF22F37E244DF3987FBFA55031CFCD46ADF03650CE
D6CDFFD87BF7407FFB63F3FE4C29FE753896101B2CEAB9007B66E29D6A5B85D3
FF91F888E445677157A5996FC7A3BD19BAF3A3BCF33F0719B992A0F013B1F0A1
82E42DF6F651071FB407F448DFF37653E2A0569BDD8EABC3280B3582745DB98B
644E5B341F531B0D81ED85F815B993C2B8B34CDDC6AE97606F6B3B1C5427F174
362FAB82B935087B568E6F526347058C406E878F1CC2DD05C26D35565347C5A5
7575891BB4
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
cleartomark
%%EndFont
TeXDict begin 40258431 52099146 1000 600 600 (paper-letter/ext.dvi)
@start /Fa 214[25 25 40[{TeXBase1Encoding ReEncodeFont}2
74.7198 /Times-Roman rf /Fb 170[86 85[{TeXBase1Encoding ReEncodeFont}1
119.552 /Times-Roman rf /Fc 167[69 9[69 4[32 73[{
TeXBase1Encoding ReEncodeFont}3 95.6413 /Times-Roman
rf /Fd 193[65 62[{}1 83.022 /CMMI10 rf /Fe 145[38 110[{}1
74.7198 /CMSY9 rf /Ff 136[48 2[22 26 29 2[33 37 2[37
5[29 37 100[{TeXBase1Encoding ReEncodeFont}9 66.4176
/Times-Bold rf /Fg 133[50 50 50 72 50 55 28 50 33 1[55
55 55 83 22 50 22 22 55 55 28 55 55 50 55 55 8[66 94
1[72 61 66 72 1[66 78 72 83 55 2[28 2[61 66 72 72 66
66 7[55 55 55 55 55 55 55 55 55 55 1[28 33 42[50 2[{
TeXBase1Encoding ReEncodeFont}56 99.6264 /Helvetica rf
/Fh 139[40 40 2[40 40 2[40 6[40 40 40 1[40 1[40 95[{
TeXBase1Encoding ReEncodeFont}10 66.4176 /Courier rf
/Fi 203[25 25 25 25 49[{TeXBase1Encoding ReEncodeFont}4
49.8132 /Times-Roman rf /Fj 130[45 45 45 45 45 45 45
45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45
45 45 45 45 45 45 1[45 45 45 45 45 45 45 45 45 45 45
45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45
45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45
45 45 45 45 45 45 45 45 45 45 45 45 45 45 33[{
TeXBase1Encoding ReEncodeFont}92 74.7198 /Courier rf
/Fk 203[29 29 29 29 49[{TeXBase1Encoding ReEncodeFont}4
58.1154 /Times-Roman rf /Fl 133[60 60 60 86 60 66 33
60 40 66 66 66 66 100 27 60 1[27 66 66 33 66 66 60 66
66 9[113 80 86 73 80 86 1[80 2[100 66 80 1[33 86 1[73
80 86 86 80 80 6[33 66 66 66 66 66 66 66 66 66 66 1[33
2[70 3[27 39[{TeXBase1Encoding ReEncodeFont}57 119.552
/Helvetica rf /Fm 130[50 50 50 50 50 50 50 50 50 50 50
50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50
1[50 1[50 1[50 50 50 50 50 50 50 50 50 50 1[50 50 50
50 50 50 1[50 50 50 50 50 50 50 50 50 2[50 1[50 6[50
1[50 50 50 50 1[50 50 2[50 50 50 50 3[50 50 34[{
TeXBase1Encoding ReEncodeFont}71 83.022 /Courier rf /Fn
168[113 80 86 73 1[86 2[93 86 4[33 86 1[73 80 2[86 86
65[{TeXBase1Encoding ReEncodeFont}13 119.552 /Helvetica-Bold
rf /Fo 133[42 42 42 60 42 46 23 42 28 1[46 46 46 69 18
42 18 18 46 46 23 46 46 42 46 46 8[55 78 55 1[51 55 60
1[55 65 60 69 46 2[23 60 65 51 55 60 60 55 55 6[23 46
46 46 46 46 46 46 46 46 46 23 23 28 1[48 3[18 36[42 2[{
TeXBase1Encoding ReEncodeFont}62 83.022 /Helvetica rf
/Fp 104[66 2[29 29 25[33 33 48 33 33 18 26 22 1[33 33
33 52 18 33 18 18 33 33 22 29 33 29 33 29 8[48 63 2[41
5[48 4[22 3[41 1[44 1[48 6[18 8[33 2[17 1[17 4[22 5[22
29[37 37 2[{TeXBase1Encoding ReEncodeFont}43 66.4176
/Times-Roman rf /Fq 134[37 37 55 37 42 23 32 32 1[42
42 42 60 23 37 23 23 42 42 23 37 42 37 42 42 9[69 2[46
42 51 60 51 60 1[69 46 2[28 60 60 51 51 60 55 51 51 13[42
42 42 1[23 4[42 39[42 2[{TeXBase1Encoding ReEncodeFont}48
83.022 /Times-Italic rf /Fr 134[42 42 60 42 46 28 32
37 1[46 42 46 69 23 46 28 23 46 42 28 37 46 37 46 42
7[60 1[83 60 60 55 46 60 1[51 65 60 78 55 2[32 65 65
51 55 60 60 55 60 1[42 4[28 42 1[42 42 42 42 42 42 42
42 23 21 28 1[47 40[46 2[{TeXBase1Encoding ReEncodeFont}61
83.022 /Times-Bold rf /Fs 240[42 1[83 13[{}2 83.022 /CMSY10
rf /Ft 104[83 42 1[37 37 24[37 42 42 60 42 42 23 32 28
42 42 42 42 65 23 42 23 23 42 42 28 37 42 37 42 37 28
5[51 60 60 78 60 60 51 46 55 60 46 60 60 74 51 60 32
28 60 60 46 51 60 55 55 60 1[37 3[23 23 42 42 42 42 42
42 42 42 42 42 23 21 28 21 47 1[28 28 28 5[28 29[46 46
2[{TeXBase1Encoding ReEncodeFont}81 83.022 /Times-Roman
rf /Fu 133[37 37 37 54 37 42 21 37 25 42 42 42 42 62
17 37 17 17 42 42 21 42 42 37 42 42 7[50 50 3[46 50 54
1[50 58 54 62 3[21 2[46 50 54 54 50 50 76 5[21 42 42
42 1[42 1[42 42 42 42 21 21 25 2[29 39[37 2[{
TeXBase1Encoding ReEncodeFont}57 74.7198 /Helvetica rf
/Fv 134[50 1[72 1[55 33 1[44 2[50 55 4[28 55 1[33 44
55 2[50 13[55 2[61 9[61 70[{TeXBase1Encoding ReEncodeFont}16
99.6264 /Times-Bold rf /Fw 139[28 1[33 4[78 28 2[28 50
2[44 1[44 50 44 27[61 72 9[28 50 50 5[50 50 50 3[25 44[{
TeXBase1Encoding ReEncodeFont}19 99.6264 /Times-Roman
rf /Fx 137[72 80 40 72 48 2[80 80 120 1[72 1[32 3[80
80 2[80 14[104 5[80 1[72 2[112 88 1[104 21[40 1[40 44[{
TeXBase1Encoding ReEncodeFont}21 143.462 /Helvetica rf
/Fy 140[72 6[32 6[80 3[80 14[104 28[80 1[80 80 3[40 46[{
TeXBase1Encoding ReEncodeFont}9 143.462 /Helvetica-Oblique
rf /Fz 134[103 103 149 1[115 57 103 69 1[115 115 115
172 46 2[46 115 115 1[115 115 103 115 115 9[195 2[126
138 149 1[138 161 149 1[115 2[57 149 2[138 149 149 138
138 21[121 40[103 2[{TeXBase1Encoding ReEncodeFont}37
206.559 /Helvetica rf end
%%EndProlog
%%BeginSetup
%%Feature: *Resolution 600dpi
TeXDict begin
%%BeginPaperSize: Letter
/setpagedevice where
{ pop << /PageSize [612 792] >> setpagedevice }
{ /letter where { pop letter } if }
ifelse
%%EndPaperSize
end
%%EndSetup
%%Page: 1 1
TeXDict begin 1 0 bop 0 83 3901 9 v 432 446 a Fz(Extending)57
b(and)g(Embedding)f(the)i(Python)2941 695 y(Inter)6 b(preter)3015
925 y Fy(Release)38 b(2.5.3)2684 2039 y Fx(Guido)g(v)l(an)i(Rossum)2365
2221 y(F)-6 b(red)39 b(L.)g(Dr)o(ak)m(e)n(,)f(Jr)-7 b(.,)39
b(editor)3044 4153 y Fw(19th)24 b(December)l(,)h(2008)2676
5201 y Fv(Python)g(Softwar)n(e)h(F)n(oundation)3019 5317
y Fw(Email:)k Fu(docs@p)n(ython.org)p eop end
%%Page: 2 2
TeXDict begin 2 1 bop 0 83 a Ft(Cop)o(yright)380 80 y(c)357
83 y Fs(\015)20 b Ft(2001-2008)d(Python)i(Softw)o(are)h(F)o(oundation.)
i(All)f(rights)f(reserv)o(ed.)0 230 y(Cop)o(yright)380
227 y(c)357 230 y Fs(\015)g Ft(2000)f(BeOpen.com.)24
b(All)c(rights)g(reserv)o(ed.)0 377 y(Cop)o(yright)380
374 y(c)357 377 y Fs(\015)g Ft(1995-2000)d(Corporation)h(for)h
(National)h(Research)g(Initiati)n(v)o(es.)k(All)d(rights)f(reserv)o
(ed.)0 524 y(Cop)o(yright)380 521 y(c)357 524 y Fs(\015)g
Ft(1991-1995)d(Stichting)j(Mathematisch)f(Centrum.)24
b(All)d(rights)f(reserv)o(ed.)0 671 y(See)h(the)f(end)f(of)h(this)h
(document)d(for)i(complete)f(license)h(and)g(permissions)f
(information.)p eop end
%%Page: 1 3
TeXDict begin 1 2 bop 1796 2237 a Fr(Abstract)0 2467
y Ft(Python)24 b(is)h(an)g(interpreted,)f(object-oriented)e
(programming)f(language.)37 b(This)25 b(document)e(describes)h(ho)n(w)g
(to)h(write)g(modules)e(in)0 2566 y(C)32 b(or)e(C)242
2559 y(++)367 2566 y(to)h(e)o(xtend)e(the)i(Python)e(interpreter)g
(with)i(ne)n(w)g(modules.)55 b(Those)30 b(modules)f(can)i(de\002ne)f
(ne)n(w)g(functions)g(b)n(ut)g(also)0 2666 y(ne)n(w)d(object)f(types)h
(and)g(their)g(methods.)44 b(The)27 b(document)e(also)j(describes)e(ho)
n(w)h(to)g(embed)f(the)h(Python)f(interpreter)g(in)h(another)0
2766 y(application,)16 b(for)i(use)g(as)g(an)g(e)o(xtension)e
(language.)22 b(Finally)-5 b(,)18 b(it)g(sho)n(ws)g(ho)n(w)f(to)h
(compile)f(and)g(link)g(e)o(xtension)g(modules)f(so)i(that)g(the)o(y)0
2865 y(can)i(be)g(loaded)f(dynamically)f(\(at)j(run)e(time\))h(into)g
(the)g(interpreter)m(,)e(if)j(the)f(underlying)d(operating)i(system)h
(supports)f(this)i(feature.)0 3012 y(This)h(document)d(assumes)j(basic)
g(kno)n(wledge)d(about)i(Python.)27 b(F)o(or)22 b(an)f(informal)f
(introduction)f(to)j(the)f(language,)f(see)i(the)g Fq(Python)0
3112 y(T)-5 b(utorial)p Ft(.)24 b(The)18 b Fq(Python)e(Refer)m(ence)h
(Manual)f Ft(gi)n(v)o(es)i(a)g(more)e(formal)h(de\002nition)f(of)i(the)
f(language.)23 b(The)17 b Fq(Python)f(Libr)o(ary)i(Refer)m(ence)0
3211 y Ft(documents)d(the)j(e)o(xisting)e(object)h(types,)g(functions)f
(and)g(modules)g(\(both)g(b)n(uilt-in)h(and)g(written)g(in)g(Python\))e
(that)j(gi)n(v)o(e)e(the)h(language)0 3311 y(its)k(wide)f(application)f
(range.)0 3458 y(F)o(or)h(a)g(detailed)g(description)f(of)h(the)g
(whole)g(Python/C)f(API,)h(see)h(the)f(separate)g Fq(Python/C)f(API)h
(Refer)m(ence)g(Manual)p Ft(.)p eop end
%%Page: 2 4
TeXDict begin 2 3 bop eop end
%%Page: 1 5
TeXDict begin 1 4 bop 2764 747 a Fz(CONTENTS)0 1594 y
Fr(1)83 b(Extending)20 b(Python)f(with)i(C)f(or)h(C)1202
1587 y(++)3858 1594 y(1)125 1694 y Ft(1.1)110 b(A)21
b(Simple)f(Example)43 b(.)e(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g
(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)
f(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)143
b(1)125 1793 y(1.2)110 b(Intermezzo:)24 b(Errors)19 b(and)g(Exceptions)
75 b(.)42 b(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h
(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)
g(.)143 b(2)125 1893 y(1.3)110 b(Back)21 b(to)f(the)g(Example)43
b(.)f(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f
(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)
h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)143 b(4)125 1993 y(1.4)110
b(The)20 b(Module')-5 b(s)20 b(Method)f(T)-7 b(able)20
b(and)f(Initialization)h(Function)72 b(.)42 b(.)f(.)g(.)g(.)h(.)f(.)g
(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)143
b(5)125 2092 y(1.5)110 b(Compilation)19 b(and)h(Linkage)75
b(.)41 b(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)
h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.)g
(.)g(.)h(.)f(.)g(.)g(.)143 b(6)125 2192 y(1.6)110 b(Calling)21
b(Python)e(Functions)g(from)g(C)68 b(.)41 b(.)h(.)f(.)g(.)g(.)h(.)f(.)g
(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)
f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)143 b(7)125 2291
y(1.7)110 b(Extracting)19 b(P)o(arameters)g(in)i(Extension)e(Functions)
88 b(.)41 b(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g
(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)143
b(9)125 2391 y(1.8)110 b(K)n(e)o(yw)o(ord)19 b(P)o(arameters)g(for)h
(Extension)f(Functions)97 b(.)41 b(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h
(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)
g(.)102 b(10)125 2491 y(1.9)110 b(Building)20 b(Arbitrary)e(V)-9
b(alues)63 b(.)41 b(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f
(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)
h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)102 b(11)125 2590 y(1.10)68
b(Reference)20 b(Counts)83 b(.)41 b(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h
(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)
g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)102
b(12)125 2690 y(1.11)68 b(Writing)20 b(Extensions)g(in)g(C)1145
2683 y(++)1266 2690 y(.)41 b(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)
g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h
(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)102 b(16)125
2790 y(1.12)68 b(Pro)o(viding)18 b(a)j(C)g(API)g(for)e(an)h(Extension)f
(Module)84 b(.)41 b(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g
(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)102
b(16)0 2972 y Fr(2)83 b(De\002ning)20 b(New)h(T)-6 b(ypes)2988
b(21)125 3072 y Ft(2.1)110 b(The)20 b(Basics)62 b(.)41
b(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h
(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)
g(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)102 b(21)125
3171 y(2.2)110 b(T)-7 b(ype)20 b(Methods)75 b(.)42 b(.)f(.)g(.)h(.)f(.)
g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)h
(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.)
g(.)h(.)f(.)g(.)g(.)102 b(50)0 3354 y Fr(3)83 b(Building)21
b(C)g(and)f(C)745 3347 y(++)861 3354 y(Extensions)g(with)h(distutils)
2094 b(63)125 3454 y Ft(3.1)110 b(Distrib)n(uting)20
b(your)f(e)o(xtension)f(modules)86 b(.)41 b(.)g(.)g(.)h(.)f(.)g(.)h(.)f
(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)
h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)102 b(64)0 3636 y Fr(4)83
b(Building)21 b(C)g(and)f(C)745 3629 y(++)861 3636 y(Extensions)g(on)h
(W)o(indo)o(ws)2114 b(67)125 3736 y Ft(4.1)110 b(A)21
b(Cookbook)d(Approach)86 b(.)41 b(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g
(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)
g(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)102
b(67)125 3836 y(4.2)110 b(Dif)n(ferences)19 b(Between)k(U)t
Fp(N)t(I)t(X)g Ft(and)c(W)m(indo)n(ws)99 b(.)42 b(.)f(.)g(.)h(.)f(.)g
(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)
f(.)g(.)g(.)h(.)f(.)g(.)g(.)102 b(69)125 3935 y(4.3)110
b(Using)20 b(DLLs)h(in)f(Practice)77 b(.)41 b(.)g(.)h(.)f(.)g(.)g(.)h
(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)
g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)102
b(70)0 4118 y Fr(5)83 b(Embedding)21 b(Python)e(in)i(Another)f(A)n
(pplication)2167 b(71)125 4218 y Ft(5.1)110 b(V)-9 b(ery)20
b(High)g(Le)n(v)o(el)f(Embedding)94 b(.)41 b(.)g(.)g(.)h(.)f(.)g(.)g(.)
h(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g
(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)102
b(71)125 4317 y(5.2)110 b(Be)o(yond)19 b(V)-9 b(ery)20
b(High)f(Le)n(v)o(el)h(Embedding:)j(An)d(o)o(v)o(ervie)n(w)88
b(.)41 b(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)
g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)102 b(72)125 4417
y(5.3)110 b(Pure)20 b(Embedding)56 b(.)41 b(.)g(.)h(.)f(.)g(.)g(.)h(.)f
(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)
h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g
(.)g(.)102 b(73)125 4516 y(5.4)110 b(Extending)18 b(Embedded)g(Python)
76 b(.)41 b(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f
(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)
h(.)f(.)g(.)g(.)102 b(75)125 4616 y(5.5)110 b(Embedding)18
b(Python)h(in)h(C)1140 4609 y(++)1266 4616 y(.)41 b(.)h(.)f(.)g(.)g(.)h
(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)
g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)102
b(76)125 4716 y(5.6)110 b(Linking)19 b(Requirements)55
b(.)41 b(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.)
g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h
(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)102 b(76)0 4898 y Fr(A)65
b(Reporting)19 b(Bugs)3137 b(77)0 5081 y(B)70 b(History)19
b(and)h(License)2983 b(79)125 5181 y Ft(B.1)97 b(History)20
b(of)g(the)g(softw)o(are)34 b(.)41 b(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)
g(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h
(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)102
b(79)125 5280 y(B.2)97 b(T)-6 b(erms)20 b(and)g(conditions)f(for)g
(accessing)h(or)g(otherwise)g(using)f(Python)47 b(.)41
b(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)h(.)f
(.)g(.)g(.)102 b(80)125 5380 y(B.3)97 b(Licenses)21 b(and)e(Ackno)n
(wledgements)e(for)j(Incorporated)d(Softw)o(are)54 b(.)41
b(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)g(.)h(.)f(.)g(.)h(.)f(.)g(.)g(.)h
(.)f(.)g(.)g(.)102 b(83)p 0 5549 3901 4 v 3882 5649 a
Fo(i)p eop end
%%Page: 2 6
TeXDict begin 2 5 bop 0 5549 3901 4 v 0 5649 a Fo(ii)p
eop end
%%Page: 1 7
TeXDict begin 1 6 bop 0 83 3901 9 v 3503 230 a Fo(CHAPTER)3641
427 y Fn(ONE)p 0 515 V 903 978 a Fz(Extending)57 b(Python)h(with)g(C)f
(or)g(C)3601 958 y(++)0 1468 y Ft(It)21 b(is)g(quite)f(easy)g(to)h(add)
f(ne)n(w)g(b)n(uilt-in)f(modules)h(to)g(Python,)f(if)i(you)e(kno)n(w)g
(ho)n(w)h(to)h(program)d(in)i(C.)h(Such)f Fq(e)n(xtension)g(modules)g
Ft(can)0 1568 y(do)g(tw)o(o)g(things)g(that)h(can')o(t)e(be)h(done)f
(directly)h(in)g(Python:)25 b(the)o(y)19 b(can)h(implement)f(ne)n(w)h
(b)n(uilt-in)g(object)g(types,)g(and)f(the)o(y)h(can)g(call)h(C)0
1667 y(library)e(functions)g(and)g(system)i(calls.)0
1814 y(T)-7 b(o)27 b(support)e(e)o(xtensions,)i(the)g(Python)f(API)h
(\(Application)e(Programmers)f(Interf)o(ace\))i(de\002nes)g(a)h(set)h
(of)e(functions,)h(macros)f(and)0 1914 y(v)n(ariables)d(that)h(pro)o
(vide)e(access)i(to)g(most)g(aspects)g(of)f(the)h(Python)f(run-time)f
(system.)36 b(The)23 b(Python)g(API)h(is)h(incorporated)c(in)i(a)i(C)0
2013 y(source)19 b(\002le)i(by)f(including)e(the)j(header)e
Fm("Python.h")p Ft(.)0 2160 y(The)f(compilation)f(of)i(an)f(e)o
(xtension)g(module)f(depends)g(on)i(its)g(intended)e(use)i(as)h(well)f
(as)g(on)g(your)e(system)i(setup;)g(details)g(are)g(gi)n(v)o(en)0
2260 y(in)h(later)h(chapters.)0 2587 y Fl(1.1)121 b(A)33
b(Simple)f(Example)0 2820 y Ft(Let')-5 b(s)24 b(create)e(an)h(e)o
(xtension)f(module)g(called)g(`)p Fm(spam)p Ft(')h(\(the)f(f)o(a)n(v)n
(orite)h(food)f(of)h(Monty)f(Python)f(f)o(ans...\))33
b(and)23 b(let')-5 b(s)24 b(say)f(we)g(w)o(ant)g(to)0
2920 y(create)d(a)g(Python)f(interf)o(ace)g(to)h(the)f(C)i(library)e
(function)f Fm(system\(\))p Ft(.)2084 2890 y Fk(1)2141
2920 y Ft(This)i(function)e(tak)o(es)j(a)f(null-terminated)d(character)
i(string)0 3019 y(as)i(ar)o(gument)d(and)h(returns)h(an)g(inte)o(ger)-5
b(.)24 b(W)-7 b(e)21 b(w)o(ant)g(this)f(function)f(to)h(be)g(callable)g
(from)f(Python)h(as)g(follo)n(ws:)236 3166 y Fj(>>>)45
b(import)e(spam)236 3258 y(>>>)i(status)e(=)i(spam.system\("ls)d(-l"\))
0 3544 y Ft(Be)o(gin)23 b(by)g(creating)f(a)h(\002le)h(`)p
Fu(spammodule)o(.c)p Ft('.)32 b(\(Historically)-5 b(,)22
b(if)h(a)h(module)e(is)i(called)f(`)p Fm(spam)p Ft(',)g(the)g(C)h
(\002le)g(containing)d(its)j(imple-)0 3644 y(mentation)18
b(is)i(called)f(`)p Fu(spammodule)o(.c)p Ft(';)e(if)j(the)f(module)f
(name)h(is)h(v)o(ery)e(long,)h(lik)o(e)g(`)p Fm(spammify)p
Ft(',)f(the)h(module)f(name)h(can)g(be)g(just)0 3743
y(`)p Fu(spammify)-7 b(.c)p Ft('.\))0 3890 y(The)20 b(\002rst)h(line)f
(of)g(our)f(\002le)i(can)f(be:)236 4128 y Fj(#include)44
b(<Python.h>)0 4415 y Ft(which)22 b(pulls)g(in)h(the)g(Python)e(API)i
(\(you)e(can)h(add)g(a)h(comment)e(describing)g(the)h(purpose)f(of)h
(the)h(module)e(and)h(a)h(cop)o(yright)d(notice)0 4515
y(if)g(you)g(lik)o(e\).)p 0 4605 3901 17 v 0 4896 17
292 v 75 4720 a Fr(W)-5 b(ar)o(ning:)64 b Ft(Since)24
b(Python)f(may)g(de\002ne)g(some)h(pre-processor)d(de\002nitions)i
(which)h(af)n(fect)f(the)h(standard)f(headers)g(on)g(some)75
4820 y(systems,)d(you)f Fq(must)i Ft(include)e(`)p Fu(Python.h)p
Ft(')f(before)h(an)o(y)g(standard)g(headers)h(are)g(included.)p
3883 4896 V 0 4912 3901 17 v 0 5068 a(All)i(user)n(-visible)f(symbols)g
(de\002ned)g(by)g(`)p Fu(Python.h)p Ft(')e(ha)n(v)o(e)i(a)h(pre\002x)f
(of)g(`)p Fm(Py)p Ft(')g(or)g(`)p Fm(PY)p Ft(',)g(e)o(xcept)g(those)g
(de\002ned)g(in)g(standard)g(header)0 5167 y(\002les.)39
b(F)o(or)24 b(con)m(v)o(enience,)f(and)h(since)h(the)o(y)f(are)g(used)h
(e)o(xtensi)n(v)o(ely)d(by)j(the)f(Python)g(interpreter)m(,)f
Fm("Python.h")h Ft(includes)g(a)h(fe)n(w)p 0 5238 1560
4 v 90 5294 a Fi(1)120 5317 y Fp(An)16 b(interf)o(ace)21
b(for)c(this)g(function)i(already)g(e)o(xists)f(in)f(the)h(standard)h
(module)f Fh(os)e Fp(\227)h(it)h(w)o(as)f(chosen)h(as)f(a)g(simple)h
(and)f(straightforw)o(ard)j(e)o(xample.)p 0 5549 3901
4 v 3854 5649 a Fo(1)p eop end
%%Page: 2 8
TeXDict begin 2 7 bop 0 83 a Ft(standard)22 b(header)g(\002les:)33
b Fm(<stdio.h>)p Ft(,)23 b Fm(<string.h>)p Ft(,)f Fm(<errno.h>)p
Ft(,)h(and)g Fm(<stdlib.h>)p Ft(.)33 b(If)24 b(the)f(latter)h(header)e
(\002le)i(does)0 183 y(not)c(e)o(xist)g(on)g(your)f(system,)h(it)h
(declares)f(the)g(functions)f Fm(malloc\(\))p Ft(,)g
Fm(free\(\))g Ft(and)h Fm(realloc\(\))f Ft(directly)-5
b(.)0 330 y(The)40 b(ne)o(xt)g(thing)g(we)h(add)f(to)g(our)g(module)f
(\002le)i(is)h(the)e(C)i(function)d(that)h(will)i(be)e(called)g(when)g
(the)h(Python)e(e)o(xpression)0 429 y(`)p Fm(spam.system\()p
Fq(string)p Fm(\))p Ft(')17 b(is)22 b(e)n(v)n(aluated)c(\(we')o(ll)i
(see)h(shortly)f(ho)n(w)f(it)i(ends)f(up)g(being)f(called\):)236
667 y Fj(static)44 b(PyObject)954 680 y(*)236 759 y
(spam_system\(PyObject)1178 772 y(*)1223 759 y(self,)g(PyObject)1895
772 y(*)1940 759 y(args\))236 850 y({)416 941 y(const)g(char)909
954 y(*)954 941 y(command;)416 1033 y(int)g(sts;)416
1215 y(if)g(\(!PyArg_ParseTuple\(args,)d("s",)j(&command\)\))595
1307 y(return)g(NULL;)416 1398 y(sts)g(=)h(system\(command\);)416
1489 y(return)e(Py_BuildValue\("i",)f(sts\);)236 1581
y(})0 1867 y Ft(There)19 b(is)h(a)g(straightforw)o(ard)d(translation)i
(from)f(the)i(ar)o(gument)d(list)j(in)g(Python)e(\(for)h(e)o(xample,)f
(the)h(single)h(e)o(xpression)e Fm("ls)49 b(-l")p Ft(\))0
1967 y(to)22 b(the)f(ar)o(guments)e(passed)i(to)h(the)f(C)i(function.)j
(The)21 b(C)i(function)c(al)o(w)o(ays)j(has)g(tw)o(o)f(ar)o(guments,)f
(con)m(v)o(entionally)d(named)k Fq(self)34 b Ft(and)0
2066 y Fq(ar)m(gs)p Ft(.)0 2213 y(The)19 b Fq(self)32
b Ft(ar)o(gument)16 b(is)k(only)f(used)g(when)f(the)h(C)h(function)d
(implements)i(a)g(b)n(uilt-in)f(method,)g(not)h(a)g(function.)k(In)c
(the)g(e)o(xample,)f Fq(self)0 2313 y Ft(will)26 b(al)o(w)o(ays)f(be)g
(a)g Fm(NULL)f Ft(pointer)m(,)h(since)g(we)g(are)f(de\002ning)g(a)h
(function,)f(not)h(a)g(method.)37 b(\(This)25 b(is)h(done)d(so)j(that)e
(the)h(interpreter)0 2412 y(doesn')o(t)19 b(ha)n(v)o(e)g(to)i
(understand)d(tw)o(o)i(dif)n(ferent)f(types)h(of)g(C)h(functions.\))0
2559 y(The)32 b Fq(ar)m(gs)h Ft(ar)o(gument)d(will)k(be)f(a)g(pointer)e
(to)i(a)g(Python)f(tuple)g(object)g(containing)f(the)h(ar)o(guments.)61
b(Each)32 b(item)h(of)f(the)h(tuple)0 2659 y(corresponds)21
b(to)i(an)g(ar)o(gument)e(in)j(the)f(call')-5 b(s)24
b(ar)o(gument)d(list.)35 b(The)23 b(ar)o(guments)e(are)i(Python)f
(objects)h(\227)h(in)g(order)d(to)j(do)f(an)o(ything)0
2759 y(with)28 b(them)g(in)g(our)f(C)i(function)d(we)j(ha)n(v)o(e)e(to)
h(con)m(v)o(ert)e(them)i(to)g(C)h(v)n(alues.)47 b(The)28
b(function)e Fm(PyArg_ParseTuple\(\))f Ft(in)k(the)0
2858 y(Python)g(API)h(checks)f(the)g(ar)o(gument)f(types)h(and)h(con)m
(v)o(erts)e(them)h(to)h(C)g(v)n(alues.)53 b(It)30 b(uses)h(a)f
(template)f(string)g(to)h(determine)f(the)0 2958 y(required)19
b(types)i(of)g(the)g(ar)o(guments)e(as)i(well)h(as)f(the)g(types)g(of)g
(the)g(C)h(v)n(ariables)e(into)h(which)f(to)h(store)g(the)g(con)m(v)o
(erted)d(v)n(alues.)27 b(More)0 3057 y(about)19 b(this)i(later)-5
b(.)0 3204 y Fm(PyArg_ParseTuple\(\))22 b Ft(returns)i(true)g
(\(nonzero\))e(if)j(all)g(ar)o(guments)d(ha)n(v)o(e)i(the)h(right)f
(type)g(and)g(its)i(components)c(ha)n(v)o(e)i(been)0
3304 y(stored)19 b(in)h(the)g(v)n(ariables)f(whose)g(addresses)g(are)h
(passed.)25 b(It)19 b(returns)g(f)o(alse)h(\(zero\))f(if)h(an)f(in)m(v)
n(alid)g(ar)o(gument)e(list)k(w)o(as)f(passed.)25 b(In)20
b(the)0 3404 y(latter)j(case)h(it)f(also)h(raises)f(an)g(appropriate)e
(e)o(xception)g(so)i(the)g(calling)g(function)e(can)i(return)f
Fm(NULL)h Ft(immediately)e(\(as)j(we)f(sa)o(w)g(in)0
3503 y(the)d(e)o(xample\).)0 3830 y Fl(1.2)121 b(Inter)s(mezz)n(o:)43
b(Errors)32 b(and)j(Exceptions)0 4063 y Ft(An)26 b(important)f(con)m(v)
o(ention)e(throughout)h(the)i(Python)f(interpreter)g(is)i(the)f(follo)n
(wing:)36 b(when)26 b(a)g(function)f(f)o(ails,)j(it)f(should)f(set)h
(an)0 4162 y(e)o(xception)j(condition)h(and)g(return)g(an)h(error)f(v)n
(alue)h(\(usually)f(a)h Fm(NULL)g Ft(pointer\).)59 b(Exceptions)31
b(are)h(stored)g(in)g(a)h(static)f(global)0 4262 y(v)n(ariable)20
b(inside)g(the)h(interpreter;)f(if)h(this)g(v)n(ariable)f(is)h
Fm(NULL)g Ft(no)f(e)o(xception)f(has)i(occurred.)k(A)c(second)f(global)
g(v)n(ariable)g(stores)h(the)0 4362 y(\223associated)k(v)n(alue\224)g
(of)g(the)h(e)o(xception)d(\(the)i(second)g(ar)o(gument)e(to)j
Fm(raise)p Ft(\).)40 b(A)26 b(third)f(v)n(ariable)f(contains)h(the)g
(stack)h(traceback)0 4461 y(in)32 b(case)h(the)f(error)f(originated)g
(in)h(Python)f(code.)60 b(These)32 b(three)g(v)n(ariables)f(are)h(the)g
(C)i(equi)n(v)n(alents)c(of)i(the)g(Python)f(v)n(ariables)0
4561 y Fm(sys.exc_type)p Ft(,)25 b Fm(sys.exc_value)f
Ft(and)g Fm(sys.exc_traceback)f Ft(\(see)j(the)g(section)f(on)g(module)
f Fm(sys)i Ft(in)f(the)h Fq(Python)0 4661 y(Libr)o(ary)20
b(Refer)m(ence)p Ft(\).)k(It)d(is)g(important)d(to)j(kno)n(w)e(about)g
(them)h(to)g(understand)e(ho)n(w)i(errors)f(are)h(passed)h(around.)0
4807 y(The)f(Python)f(API)h(de\002nes)g(a)h(number)d(of)i(functions)f
(to)h(set)h(v)n(arious)e(types)h(of)g(e)o(xceptions.)0
4954 y(The)g(most)h(common)e(one)h(is)i Fm(PyErr_SetString\(\))p
Ft(.)i(Its)d(ar)o(guments)d(are)j(an)g(e)o(xception)d(object)j(and)f(a)
h(C)h(string.)k(The)20 b(e)o(xcep-)0 5054 y(tion)j(object)g(is)h
(usually)f(a)g(prede\002ned)f(object)g(lik)o(e)i Fm
(PyExc_ZeroDivisionError)p Ft(.)30 b(The)23 b(C)h(string)f(indicates)g
(the)h(cause)f(of)0 5154 y(the)d(error)f(and)h(is)h(con)m(v)o(erted)d
(to)i(a)h(Python)e(string)g(object)h(and)g(stored)f(as)i(the)g
(\223associated)e(v)n(alue\224)h(of)g(the)g(e)o(xception.)0
5300 y(Another)g(useful)h(function)f(is)i Fm(PyErr_SetFromErrno\(\))p
Ft(,)c(which)j(only)g(tak)o(es)g(an)h(e)o(xception)d(ar)o(gument)g(and)
i(constructs)g(the)0 5400 y(associated)g(v)n(alue)f(by)h(inspection)f
(of)g(the)h(global)g(v)n(ariable)e Fm(errno)p Ft(.)28
b(The)20 b(most)h(general)f(function)f(is)j Fm(PyErr_SetObject\(\))p
Ft(,)p 0 5549 3901 4 v 0 5649 a Fo(2)2210 b(Chapter)24
b(1.)52 b(Extending)24 b(Python)f(with)h(C)f(or)g(C)3780
5641 y(++)p eop end
%%Page: 3 9
TeXDict begin 3 8 bop 0 83 a Ft(which)28 b(tak)o(es)i(tw)o(o)f(object)f
(ar)o(guments,)h(the)g(e)o(xception)e(and)h(its)i(associated)f(v)n
(alue.)50 b(Y)-9 b(ou)29 b(don')o(t)e(need)h(to)h Fm(Py_INCREF\(\))f
Ft(the)0 183 y(objects)20 b(passed)g(to)g(an)o(y)g(of)g(these)g
(functions.)0 330 y(Y)-9 b(ou)15 b(can)g(test)i(non-destructi)n(v)o
(ely)12 b(whether)i(an)i(e)o(xception)e(has)i(been)f(set)h(with)g
Fm(PyErr_Occurred\(\))p Ft(.)21 b(This)16 b(returns)e(the)i(current)0
429 y(e)o(xception)22 b(object,)h(or)h Fm(NULL)f Ft(if)h(no)f(e)o
(xception)f(has)i(occurred.)33 b(Y)-9 b(ou)23 b(normally)f(don')o(t)g
(need)g(to)i(call)g Fm(PyErr_Occurred\(\))d Ft(to)0 529
y(see)g(whether)e(an)h(error)f(occurred)f(in)j(a)f(function)f(call,)h
(since)g(you)g(should)f(be)h(able)g(to)g(tell)h(from)e(the)i(return)e
(v)n(alue.)0 676 y(When)g(a)g(function)f Fq(f)32 b Ft(that)19
b(calls)h(another)d(function)h Fq(g)h Ft(detects)g(that)g(the)g(latter)
h(f)o(ails,)g Fq(f)31 b Ft(should)18 b(itself)i(return)e(an)h(error)f
(v)n(alue)h(\(usually)0 775 y Fm(NULL)k Ft(or)g Fm(-1)p
Ft(\).)35 b(It)23 b(should)g Fq(not)g Ft(call)g(one)g(of)g(the)h
Fm(PyErr_)1756 790 y(*)1806 775 y(\(\))e Ft(functions)g(\227)i(one)f
(has)g(already)f(been)h(called)g(by)g Fq(g)p Ft(.)35
b Fq(f)12 b Ft(')-5 b(s)24 b(caller)g(is)0 875 y(then)18
b(supposed)g(to)h(also)g(return)f(an)g(error)g(indication)g(to)h
Fq(its)g Ft(caller)m(,)g(again)f Fq(without)g Ft(calling)h
Fm(PyErr_)2992 890 y(*)3042 875 y(\(\))p Ft(,)e(and)i(so)g(on)f(\227)i
(the)f(most)0 975 y(detailed)f(cause)h(of)f(the)h(error)e(w)o(as)i
(already)f(reported)f(by)h(the)h(function)e(that)h(\002rst)i(detected)d
(it.)26 b(Once)18 b(the)h(error)e(reaches)h(the)h(Python)0
1074 y(interpreter')-5 b(s)17 b(main)i(loop,)f(this)h(aborts)f(the)h
(currently)e(e)o(x)o(ecuting)f(Python)i(code)g(and)g(tries)h(to)g
(\002nd)f(an)h(e)o(xception)e(handler)g(speci\002ed)0
1174 y(by)j(the)g(Python)f(programmer)-5 b(.)0 1321 y(\(There)24
b(are)h(situations)f(where)h(a)g(module)f(can)g(actually)h(gi)n(v)o(e)f
(a)h(more)f(detailed)g(error)g(message)h(by)f(calling)h(another)e
Fm(PyErr_-)0 1435 y(*)50 1420 y(\(\))i Ft(function,)g(and)f(in)i(such)f
(cases)h(it)g(is)g(\002ne)f(to)g(do)g(so.)41 b(As)26
b(a)f(general)f(rule,)i(ho)n(we)n(v)o(er)m(,)e(this)i(is)g(not)f
(necessary)-5 b(,)25 b(and)g(can)g(cause)0 1520 y(information)18
b(about)h(the)h(cause)g(of)g(the)g(error)g(to)g(be)g(lost:)26
b(most)20 b(operations)f(can)h(f)o(ail)g(for)g(a)g(v)n(ariety)g(of)g
(reasons.\))0 1667 y(T)-7 b(o)22 b(ignore)e(an)i(e)o(xception)e(set)i
(by)f(a)h(function)e(call)i(that)g(f)o(ailed,)g(the)f(e)o(xception)f
(condition)g(must)i(be)f(cleared)g(e)o(xplicitly)g(by)g(calling)0
1766 y Fm(PyErr_Clear\(\))p Ft(.)28 b(The)21 b(only)g(time)h(C)h(code)e
(should)g(call)h Fm(PyErr_Clear\(\))e Ft(is)j(if)f(it)g(doesn')o(t)e(w)
o(ant)i(to)g(pass)h(the)e(error)g(on)g(to)0 1866 y(the)26
b(interpreter)e(b)n(ut)h(w)o(ants)h(to)g(handle)e(it)j(completely)d(by)
h(itself)h(\(possibly)f(by)g(trying)f(something)g(else,)k(or)d
(pretending)e(nothing)0 1966 y(went)d(wrong\).)0 2113
y(Ev)o(ery)h(f)o(ailing)h Fm(malloc\(\))f Ft(call)i(must)f(be)h(turned)
e(into)h(an)g(e)o(xception)e(\227)j(the)g(direct)f(caller)g(of)g
Fm(malloc\(\))f Ft(\(or)h Fm(realloc\(\))p Ft(\))0 2212
y(must)g(call)h Fm(PyErr_NoMemory\(\))d Ft(and)i(return)f(a)h(f)o
(ailure)g(indicator)f(itself.)32 b(All)23 b(the)f(object-creating)e
(functions)h(\(for)g(e)o(xample,)0 2312 y Fm(PyInt_FromLong\(\))p
Ft(\))c(already)i(do)h(this,)h(so)f(this)h(note)f(is)h(only)e(rele)n(v)
n(ant)g(to)i(those)f(who)f(call)i Fm(malloc\(\))e Ft(directly)-5
b(.)0 2459 y(Also)19 b(note)f(that,)g(with)h(the)f(important)f(e)o
(xception)g(of)h Fm(PyArg_ParseTuple\(\))d Ft(and)j(friends,)g
(functions)f(that)h(return)f(an)i(inte)o(ger)0 2558 y(status)i(usually)
e(return)g(a)i(positi)n(v)o(e)e(v)n(alue)h(or)g(zero)g(for)f(success)i
(and)e Fm(-1)i Ft(for)e(f)o(ailure,)h(lik)o(e)i(U)t Fp(N)t(I)t(X)h
Ft(system)e(calls.)0 2705 y(Finally)-5 b(,)26 b(be)f(careful)g(to)h
(clean)f(up)g(garbage)e(\(by)i(making)f Fm(Py_XDECREF\(\))g
Ft(or)h Fm(Py_DECREF\(\))f Ft(calls)i(for)f(objects)h(you)e(ha)n(v)o(e)
0 2805 y(already)19 b(created\))g(when)h(you)f(return)g(an)h(error)f
(indicator!)0 2952 y(The)h(choice)g(of)g(which)h(e)o(xception)d(to)j
(raise)g(is)g(entirely)f(yours.)25 b(There)20 b(are)h(predeclared)d(C)k
(objects)e(corresponding)d(to)k(all)g(b)n(uilt-in)0 3051
y(Python)28 b(e)o(xceptions,)h(such)f(as)h Fm(PyExc_ZeroDivisionError)p
Ft(,)e(which)h(you)g(can)h(use)g(directly)-5 b(.)49 b(Of)29
b(course,)h(you)e(should)0 3151 y(choose)i(e)o(xceptions)g(wisely)h
(\227)h(don')o(t)d(use)i Fm(PyExc_TypeError)e Ft(to)i(mean)f(that)i(a)f
(\002le)h(couldn')o(t)d(be)h(opened)g(\(that)h(should)0
3251 y(probably)13 b(be)j Fm(PyExc_IOError)p Ft(\).)21
b(If)16 b(something')-5 b(s)14 b(wrong)h(with)g(the)h(ar)o(gument)d
(list,)18 b(the)d Fm(PyArg_ParseTuple\(\))e Ft(function)0
3350 y(usually)i(raises)h Fm(PyExc_TypeError)p Ft(.)21
b(If)16 b(you)e(ha)n(v)o(e)h(an)h(ar)o(gument)d(whose)i(v)n(alue)g
(must)h(be)f(in)h(a)g(particular)e(range)g(or)i(must)f(satisfy)0
3450 y(other)k(conditions,)g Fm(PyExc_ValueError)f Ft(is)j
(appropriate.)0 3597 y(Y)-9 b(ou)17 b(can)g(also)h(de\002ne)f(a)h(ne)n
(w)g(e)o(xception)d(that)j(is)g(unique)f(to)g(your)g(module.)22
b(F)o(or)17 b(this,)i(you)d(usually)h(declare)g(a)h(static)h(object)e
(v)n(ariable)0 3696 y(at)k(the)f(be)o(ginning)d(of)j(your)f(\002le:)236
3934 y Fj(static)44 b(PyObject)954 3947 y(*)999 3934
y(SpamError;)0 4221 y Ft(and)17 b(initialize)g(it)h(in)f(your)f
(module')-5 b(s)16 b(initialization)g(function)g(\()p
Fm(initspam\(\))p Ft(\))f(with)i(an)g(e)o(xception)e(object)i(\(lea)n
(ving)f(out)h(the)g(error)0 4321 y(checking)h(for)i(no)n(w\):)p
0 5549 3901 4 v 0 5649 a Fo(1.2.)52 b(Inter)r(mezz)o(o:)28
b(Errors)22 b(and)i(Exceptions)2384 b(3)p eop end
%%Page: 4 10
TeXDict begin 4 9 bop 236 174 a Fj(PyMODINIT_FUNC)236
266 y(initspam\(void\))236 357 y({)416 448 y(PyObject)819
461 y(*)864 448 y(m;)416 631 y(m)44 b(=)h(Py_InitModule\("spam",)c
(SpamMethods\);)416 722 y(if)j(\(m)h(==)f(NULL\))595
814 y(return;)416 996 y(SpamError)f(=)i
(PyErr_NewException\("spam.error",)39 b(NULL,)44 b(NULL\);)416
1088 y(Py_INCREF\(SpamError\);)416 1179 y(PyModule_AddObject\(m,)d
("error",)i(SpamError\);)236 1270 y(})0 1557 y Ft(Note)17
b(that)g(the)h(Python)e(name)g(for)h(the)g(e)o(xception)e(object)i(is)h
Fm(spam.error)p Ft(.)23 b(The)17 b Fm(PyErr_NewException\(\))d
Ft(function)h(may)0 1656 y(create)20 b(a)g(class)h(with)f(the)g(base)h
(class)g(being)e Fm(Exception)g Ft(\(unless)g(another)g(class)i(is)g
(passed)f(in)g(instead)g(of)f Fm(NULL)p Ft(\),)h(described)e(in)0
1756 y(the)i Fq(Python)f(Libr)o(ary)h(Refer)m(ence)g
Ft(under)f(\223Built-in)h(Exceptions.)-6 b(\224)0 1903
y(Note)24 b(also)h(that)g(the)f Fm(SpamError)g Ft(v)n(ariable)f
(retains)h(a)h(reference)e(to)i(the)f(ne)n(wly)g(created)g(e)o
(xception)e(class;)28 b(this)d(is)g(intentional!)0 2002
y(Since)20 b(the)g(e)o(xception)f(could)g(be)h(remo)o(v)o(ed)e(from)h
(the)h(module)f(by)g(e)o(xternal)g(code,)g(an)i(o)n(wned)d(reference)h
(to)h(the)g(class)i(is)f(needed)d(to)0 2102 y(ensure)i(that)h(it)g
(will)h(not)e(be)h(discarded,)e(causing)h Fm(SpamError)g
Ft(to)h(become)e(a)i(dangling)e(pointer)-5 b(.)26 b(Should)19
b(it)j(become)d(a)i(dangling)0 2202 y(pointer)m(,)d(C)k(code)d(which)h
(raises)g(the)h(e)o(xception)d(could)h(cause)h(a)h(core)f(dump)e(or)i
(other)g(unintended)d(side)k(ef)n(fects.)0 2349 y(W)-7
b(e)21 b(discuss)g(the)f(use)h(of)e(PyMODINIT)p 1187
2349 25 4 v 29 w(FUNC)i(as)g(a)f(function)f(return)g(type)h(later)g(in)
g(this)h(sample.)0 2676 y Fl(1.3)121 b(Bac)n(k)32 b(to)i(the)h(Example)
0 2909 y Ft(Going)19 b(back)h(to)g(our)g(e)o(xample)e(function,)h(you)g
(should)g(no)n(w)h(be)g(able)g(to)g(understand)e(this)j(statement:)416
3147 y Fj(if)44 b(\(!PyArg_ParseTuple\(args,)d("s",)j(&command\)\))595
3238 y(return)g(NULL;)0 3525 y Ft(It)21 b(returns)f Fm(NULL)g
Ft(\(the)g(error)g(indicator)f(for)h(functions)g(returning)e(object)i
(pointers\))g(if)g(an)h(error)f(is)h(detected)f(in)h(the)f(ar)o(gument)
f(list,)0 3625 y(relying)d(on)g(the)h(e)o(xception)e(set)i(by)g
Fm(PyArg_ParseTuple\(\))p Ft(.)k(Otherwise)16 b(the)h(string)g(v)n
(alue)f(of)g(the)h(ar)o(gument)e(has)i(been)f(copied)0
3724 y(to)22 b(the)h(local)f(v)n(ariable)f Fm(command)p
Ft(.)31 b(This)22 b(is)h(a)g(pointer)e(assignment)g(and)h(you)f(are)i
(not)f(supposed)e(to)j(modify)d(the)j(string)f(to)g(which)0
3824 y(it)f(points)f(\(so)g(in)g(Standard)f(C,)i(the)f(v)n(ariable)f
Fm(command)h Ft(should)f(properly)f(be)i(declared)f(as)i(`)p
Fm(const)48 b(char)3300 3839 y(*)3350 3824 y(command)p
Ft('\).)0 3971 y(The)32 b(ne)o(xt)f(statement)h(is)h(a)g(call)f(to)g
(the)j(U)t Fp(N)t(I)t(X)g Ft(function)30 b Fm(system\(\))p
Ft(,)k(passing)e(it)g(the)h(string)e(we)i(just)f(got)g(from)f
Fm(PyArg_-)0 4070 y(ParseTuple\(\))p Ft(:)416 4308 y
Fj(sts)44 b(=)h(system\(command\);)0 4595 y Ft(Our)26
b Fm(spam.system\(\))e Ft(function)h(must)h(return)g(the)g(v)n(alue)g
(of)g Fm(sts)g Ft(as)h(a)g(Python)e(object.)43 b(This)27
b(is)g(done)e(using)h(the)g(function)0 4695 y Fm(Py_BuildValue\(\))p
Ft(,)18 b(which)h(is)i(something)e(lik)o(e)i(the)f(in)m(v)o(erse)f(of)h
Fm(PyArg_ParseTuple\(\))p Ft(:)i(it)f(tak)o(es)g(a)f(format)f(string)h
(and)0 4794 y(an)g(arbitrary)f(number)f(of)i(C)h(v)n(alues,)f(and)f
(returns)h(a)g(ne)n(w)g(Python)f(object.)25 b(More)19
b(info)h(on)g Fm(Py_BuildValue\(\))e Ft(is)j(gi)n(v)o(en)e(later)-5
b(.)416 5032 y Fj(return)43 b(Py_BuildValue\("i",)f(sts\);)0
5319 y Ft(In)20 b(this)h(case,)f(it)h(will)g(return)e(an)h(inte)o(ger)f
(object.)24 b(\(Y)-8 b(es,)20 b(e)n(v)o(en)g(inte)o(gers)f(are)h
(objects)g(on)g(the)g(heap)g(in)g(Python!\))p 0 5549
3901 4 v 0 5649 a Fo(4)2210 b(Chapter)24 b(1.)52 b(Extending)24
b(Python)f(with)h(C)f(or)g(C)3780 5641 y(++)p eop end
%%Page: 5 11
TeXDict begin 5 10 bop 0 83 a Ft(If)31 b(you)f(ha)n(v)o(e)g(a)i(C)g
(function)d(that)i(returns)f(no)h(useful)f(ar)o(gument)f(\(a)i
(function)e(returning)g Fm(void)p Ft(\),)k(the)e(corresponding)d
(Python)0 183 y(function)18 b(must)j(return)e Fm(None)p
Ft(.)24 b(Y)-9 b(ou)20 b(need)g(this)g(idiom)g(to)g(do)g(so)g(\(which)g
(is)h(implemented)d(by)i(the)g Fm(Py_RETURN_NONE)e Ft(macro\):)416
421 y Fj(Py_INCREF\(Py_None\);)416 512 y(return)43 b(Py_None;)0
799 y Fm(Py_None)18 b Ft(is)h(the)f(C)h(name)f(for)g(the)g(special)h
(Python)e(object)h Fm(None)p Ft(.)24 b(It)18 b(is)i(a)e(genuine)f
(Python)g(object)h(rather)g(than)f(a)i Fm(NULL)f Ft(pointer)m(,)0
898 y(which)i(means)g(\223error\224)e(in)j(most)f(conte)o(xts,)f(as)i
(we)f(ha)n(v)o(e)g(seen.)0 1222 y Fl(1.4)121 b(The)34
b(Module')-6 b(s)34 b(Method)g(T)-14 b(ab)n(le)34 b(and)h
(Initialization)e(Function)0 1455 y Ft(I)17 b(promised)e(to)h(sho)n(w)h
(ho)n(w)f Fm(spam_system\(\))e Ft(is)k(called)e(from)g(Python)f
(programs.)22 b(First,)c(we)f(need)e(to)i(list)h(its)f(name)f(and)g
(address)0 1555 y(in)k(a)h(\223method)e(table\224:)236
1793 y Fj(static)44 b(PyMethodDef)f(SpamMethods[])g(=)h({)416
1884 y(...)416 1976 y({"system",)88 b(spam_system,)42
b(METH_VARARGS,)460 2067 y("Execute)i(a)h(shell)f(command."},)416
2158 y(...)416 2250 y({NULL,)f(NULL,)h(0,)h(NULL})358
b(/)1761 2263 y(*)1850 2250 y(Sentinel)2254 2263 y(*)2299
2250 y(/)236 2341 y(};)0 2627 y Ft(Note)19 b(the)g(third)f(entry)h(\(`)
p Fm(METH_VARARGS)p Ft('\).)c(This)k(is)h(a)g(\003ag)f(telling)g(the)g
(interpreter)e(the)i(calling)f(con)m(v)o(ention)e(to)k(be)e(used)h(for)
g(the)0 2727 y(C)k(function.)29 b(It)22 b(should)f(normally)g(al)o(w)o
(ays)h(be)g(`)p Fm(METH_VARARGS)p Ft(')e(or)i(`)p Fm(METH_VARARGS)47
b(|)j(METH_KEYWORDS)p Ft(';)20 b(a)i(v)n(alue)0 2827
y(of)e Fm(0)g Ft(means)g(that)h(an)f(obsolete)f(v)n(ariant)g(of)h
Fm(PyArg_ParseTuple\(\))e Ft(is)j(used.)0 2973 y(When)c(using)f(only)g
(`)p Fm(METH_VARARGS)p Ft(',)f(the)i(function)e(should)h(e)o(xpect)g
(the)h(Python-le)n(v)o(el)d(parameters)i(to)h(be)g(passed)g(in)g(as)h
(a)f(tuple)0 3073 y(acceptable)i(for)h(parsing)f(via)h
Fm(PyArg_ParseTuple\(\))p Ft(;)e(more)h(information)f(on)h(this)i
(function)e(is)i(pro)o(vided)c(belo)n(w)-5 b(.)0 3220
y(The)19 b Fm(METH_KEYWORDS)e Ft(bit)i(may)f(be)h(set)h(in)f(the)g
(third)f(\002eld)h(if)g(k)o(e)o(yw)o(ord)e(ar)o(guments)g(should)h(be)h
(passed)g(to)g(the)g(function.)j(In)d(this)0 3320 y(case,)28
b(the)d(C)i(function)d(should)h(accept)h(a)g(third)f(`)p
Fm(PyObject)1913 3335 y(*)1963 3320 y Ft(')h(parameter)f(which)g(will)i
(be)e(a)i(dictionary)d(of)h(k)o(e)o(yw)o(ords.)41 b(Use)0
3419 y Fm(PyArg_ParseTupleAndKeywords\(\))15 b Ft(to)21
b(parse)f(the)g(ar)o(guments)e(to)i(such)g(a)h(function.)0
3566 y(The)h(method)f(table)h(must)g(be)g(passed)h(to)f(the)g
(interpreter)f(in)h(the)h(module')-5 b(s)21 b(initialization)g
(function.)30 b(The)22 b(initialization)f(function)0
3666 y(must)28 b(be)f(named)g Fm(init)p Fq(name)p Fm(\(\))p
Ft(,)h(where)f Fq(name)g Ft(is)h(the)g(name)f(of)g(the)h(module,)g(and)
f(should)g(be)h(the)f(only)g(non-)p Fm(static)e Ft(item)0
3765 y(de\002ned)19 b(in)i(the)f(module)f(\002le:)236
4004 y Fj(PyMODINIT_FUNC)236 4095 y(initspam\(void\))236
4186 y({)416 4278 y(\(void\))43 b(Py_InitModule\("spam",)f
(SpamMethods\);)236 4369 y(})0 4655 y Ft(Note)26 b(that)f(PyMODINIT)p
792 4655 25 4 v 29 w(FUNC)h(declares)f(the)h(function)e(as)i
Fm(void)g Ft(return)e(type,)i(declares)g(an)o(y)e(special)i(linkage)f
(declarations)0 4755 y(required)18 b(by)i(the)g(platform,)f(and)g(for)h
(C)1164 4748 y(++)1279 4755 y(declares)g(the)g(function)e(as)j
Fm(extern)49 b("C")p Ft(.)0 4902 y(When)18 b(the)g(Python)g(program)e
(imports)h(module)g Fm(spam)h Ft(for)g(the)g(\002rst)h(time,)g
Fm(initspam\(\))e Ft(is)i(called.)24 b(\(See)18 b(belo)n(w)g(for)g
(comments)0 5001 y(about)31 b(embedding)e(Python.\))58
b(It)32 b(calls)g Fm(Py_InitModule\(\))p Ft(,)h(which)e(creates)g(a)i
(\223module)d(object\224)h(\(which)g(is)h(inserted)f(in)0
5101 y(the)26 b(dictionary)e Fm(sys.modules)g Ft(under)g(the)i(k)o(e)o
(y)f Fm("spam")p Ft(\),)h(and)f(inserts)h(b)n(uilt-in)f(function)f
(objects)i(into)f(the)h(ne)n(wly)f(created)0 5201 y(module)c(based)i
(upon)e(the)h(table)h(\(an)f(array)g(of)g Fm(PyMethodDef)g
Ft(structures\))f(that)i(w)o(as)g(passed)g(as)g(its)h(second)e(ar)o
(gument.)30 b Fm(Py_-)0 5300 y(InitModule\(\))24 b Ft(returns)h(a)i
(pointer)d(to)i(the)g(module)f(object)g(that)h(it)h(creates)f(\(which)f
(is)i(unused)d(here\).)41 b(It)27 b(may)e(abort)g(with)h(a)0
5400 y(f)o(atal)20 b(error)g(for)f(certain)h(errors,)f(or)h(return)f
Fm(NULL)h Ft(if)g(the)g(module)f(could)h(not)f(be)h(initialized)g
(satisf)o(actorily)-5 b(.)p 0 5549 3901 4 v 0 5649 a
Fo(1.4.)52 b(The)23 b(Module')l(s)h(Method)g(T)-10 b(ab)n(le)24
b(and)g(Initialization)i(Function)1691 b(5)p eop end
%%Page: 6 12
TeXDict begin 6 11 bop 0 83 a Ft(When)27 b(embedding)e(Python,)i(the)g
Fm(initspam\(\))f Ft(function)g(is)i(not)e(called)h(automatically)f
(unless)h(there')-5 b(s)27 b(an)h(entry)e(in)h(the)g
Fm(_-)0 183 y(PyImport_Inittab)21 b Ft(table.)35 b(The)23
b(easiest)i(w)o(ay)e(to)h(handle)e(this)i(is)h(to)f(statically)f
(initialize)h(your)e(statically-link)o(ed)h(modules)0
282 y(by)d(directly)f(calling)h Fm(initspam\(\))f Ft(after)h(the)g
(call)g(to)h Fm(Py_Initialize\(\))p Ft(:)236 520 y Fj(int)236
612 y(main\(int)44 b(argc,)g(char)1133 625 y(*)1178 612
y(argv[]\))236 703 y({)416 794 y(/)461 807 y(*)550 794
y(Pass)g(argv[0])g(to)g(the)h(Python)f(interpreter)2298
807 y(*)2343 794 y(/)416 886 y(Py_SetProgramName\(argv[0]\);)416
1068 y(/)461 1081 y(*)550 1068 y(Initialize)f(the)i(Python)e
(interpreter.)88 b(Required.)2612 1081 y(*)2657 1068
y(/)416 1160 y(Py_Initialize\(\);)416 1342 y(/)461 1355
y(*)550 1342 y(Add)44 b(a)h(static)f(module)1447 1355
y(*)1492 1342 y(/)416 1434 y(initspam\(\);)0 1720 y Ft(An)20
b(e)o(xample)f(may)h(be)g(found)e(in)j(the)f(\002le)h(`)p
Fu(Demo/embed/demo)m(.c)p Ft(')16 b(in)k(the)h(Python)e(source)g
(distrib)n(ution.)0 1867 y Fr(Note:)44 b Ft(Remo)o(ving)28
b(entries)i(from)f Fm(sys.modules)f Ft(or)i(importing)e(compiled)g
(modules)h(into)h(multiple)f(interpreters)f(within)i(a)0
1967 y(process)h(\(or)f(follo)n(wing)g(a)h Fm(fork\(\))g
Ft(without)f(an)h(interv)o(ening)e Fm(exec\(\))p Ft(\))h(can)h(create)g
(problems)f(for)g(some)h(e)o(xtension)f(mod-)0 2066 y(ules.)57
b(Extension)29 b(module)g(authors)h(should)g(e)o(x)o(ercise)g(caution)f
(when)h(initializing)g(internal)g(data)h(structures.)56
b(Note)30 b(also)h(that)0 2166 y(the)39 b Fm(reload\(\))f
Ft(function)f(can)h(be)h(used)g(with)g(e)o(xtension)e(modules,)42
b(and)c(will)i(call)f(the)g(module)e(initialization)h(function)0
2266 y(\()p Fm(initspam\(\))27 b Ft(in)i(the)g(e)o(xample\),)g(b)n(ut)g
(will)g(not)g(load)f(the)h(module)e(again)h(if)h(it)h(w)o(as)f(loaded)f
(from)g(a)h(dynamically)e(loadable)0 2365 y(object)20
b(\002le)h(\(`)p Fu(.so)p Ft(')e(on)j(U)t Fp(N)t(I)t(X)r
Ft(,)f(`)p Fu(.dll)p Ft(')d(on)i(W)m(indo)n(ws\).)0 2512
y(A)k(more)e(substantial)h(e)o(xample)e(module)h(is)i(included)e(in)h
(the)g(Python)f(source)g(distrib)n(ution)g(as)i(`)p Fu
(Modules/xxmodule)o(.c)p Ft('.)31 b(This)23 b(\002le)0
2612 y(may)d(be)h(used)f(as)i(a)f(template)f(or)h(simply)f(read)g(as)i
(an)e(e)o(xample.)26 b(The)20 b Fr(modulator)-8 b(.py)19
b Ft(script)i(included)e(in)i(the)g(source)f(distrib)n(ution)0
2711 y(or)h(W)m(indo)n(ws)g(install)h(pro)o(vides)e(a)i(simple)f
(graphical)f(user)i(interf)o(ace)e(for)h(declaring)f(the)i(functions)e
(and)h(objects)g(which)g(a)h(module)0 2811 y(should)30
b(implement,)j(and)e(can)g(generate)g(a)h(template)f(which)g(can)g(be)g
(\002lled)h(in.)59 b(The)31 b(script)h(li)n(v)o(es)f(in)h(the)f(`)p
Fu(T)-9 b(ools/modulator/)p Ft(')0 2911 y(directory;)19
b(see)h(the)h(`)p Fu(README)p Ft(')e(\002le)i(there)f(for)f(more)h
(information.)0 3238 y Fl(1.5)121 b(Compilation)34 b(and)h(Linkage)0
3471 y Ft(There)25 b(are)i(tw)o(o)f(more)f(things)h(to)h(do)e(before)g
(you)h(can)g(use)g(your)f(ne)n(w)h(e)o(xtension:)36 b(compiling)24
b(and)i(linking)f(it)i(with)f(the)h(Python)0 3570 y(system.)39
b(If)25 b(you)f(use)h(dynamic)e(loading,)i(the)g(details)g(may)f
(depend)f(on)i(the)g(style)g(of)g(dynamic)e(loading)g(your)h(system)h
(uses;)j(see)0 3670 y(the)20 b(chapters)e(about)h(b)n(uilding)f(e)o
(xtension)g(modules)g(\(chapter)g(3\))i(and)f(additional)f(information)
f(that)j(pertains)e(only)h(to)h(b)n(uilding)e(on)0 3770
y(W)m(indo)n(ws)i(\(chapter)e(4\))i(for)g(more)f(information)f(about)h
(this.)0 3917 y(If)h(you)f(can')o(t)g(use)h(dynamic)e(loading,)g(or)i
(if)g(you)f(w)o(ant)h(to)g(mak)o(e)g(your)e(module)h(a)h(permanent)e
(part)i(of)f(the)h(Python)f(interpreter)m(,)e(you)0 4016
y(will)i(ha)n(v)o(e)e(to)i(change)e(the)h(con\002guration)d(setup)j
(and)g(reb)n(uild)f(the)h(interpreter)-5 b(.)23 b(Luckily)-5
b(,)17 b(this)i(is)g(v)o(ery)e(simple)h(on)i(U)t Fp(N)t(I)t(X)r
Ft(:)25 b(just)19 b(place)0 4116 y(your)k(\002le)h(\(`)p
Fu(spammodule)o(.c)p Ft(')d(for)i(e)o(xample\))f(in)j(the)e(`)p
Fu(Modules/)p Ft(')f(directory)g(of)i(an)g(unpack)o(ed)d(source)j
(distrib)n(ution,)f(add)g(a)h(line)g(to)0 4215 y(the)c(\002le)h(`)p
Fu(Modules/Setup)m(.local)p Ft(')15 b(describing)k(your)g(\002le:)236
4454 y Fj(spam)44 b(spammodule.o)0 4740 y Ft(and)31 b(reb)n(uild)g(the)
h(interpreter)e(by)h(running)f Fr(mak)o(e)i Ft(in)g(the)f(tople)n(v)o
(el)g(directory)-5 b(.)57 b(Y)-9 b(ou)31 b(can)h(also)g(run)f
Fr(mak)o(e)h Ft(in)g(the)g(`)p Fu(Modules/)p Ft(')0 4840
y(subdirectory)-5 b(,)15 b(b)n(ut)j(then)f(you)g(must)h(\002rst)g(reb)n
(uild)f(`)p Fu(Mak)o(e\002le)p Ft(')f(there)i(by)f(running)e(`)p
Fr(mak)o(e)j Ft(Mak)o(e\002le'.)24 b(\(This)17 b(is)i(necessary)e(each)
g(time)0 4939 y(you)i(change)g(the)h(`)p Fu(Setup)p Ft(')f(\002le.\))0
5086 y(If)g(your)e(module)h(requires)g(additional)f(libraries)i(to)g
(link)g(with,)g(these)g(can)f(be)h(listed)h(on)e(the)h(line)g(in)g(the)
g(con\002guration)d(\002le)k(as)f(well,)0 5186 y(for)h(instance:)p
0 5549 3901 4 v 0 5649 a Fo(6)2210 b(Chapter)24 b(1.)52
b(Extending)24 b(Python)f(with)h(C)f(or)g(C)3780 5641
y(++)p eop end
%%Page: 7 13
TeXDict begin 7 12 bop 236 174 a Fj(spam)44 b(spammodule.o)f(-lX11)0
623 y Fl(1.6)121 b(Calling)33 b(Python)i(Functions)f(from)g(C)0
856 y Ft(So)23 b(f)o(ar)f(we)h(ha)n(v)o(e)f(concentrated)e(on)i(making)
f(C)j(functions)d(callable)h(from)g(Python.)30 b(The)23
b(re)n(v)o(erse)e(is)i(also)g(useful:)30 b(calling)22
b(Python)0 956 y(functions)f(from)g(C.)h(This)g(is)h(especially)f(the)g
(case)g(for)g(libraries)f(that)h(support)f(so-called)g
(\223callback\224)g(functions.)29 b(If)22 b(a)g(C)h(interf)o(ace)0
1055 y(mak)o(es)h(use)h(of)e(callbacks,)i(the)f(equi)n(v)n(alent)f
(Python)g(often)g(needs)h(to)g(pro)o(vide)e(a)j(callback)e(mechanism)g
(to)i(the)f(Python)f(program-)0 1155 y(mer;)30 b(the)e(implementation)d
(will)j(require)d(calling)i(the)g(Python)f(callback)h(functions)f(from)
g(a)h(C)h(callback.)46 b(Other)26 b(uses)i(are)f(also)0
1254 y(imaginable.)0 1401 y(F)o(ortunately)-5 b(,)14
b(the)h(Python)g(interpreter)e(is)k(easily)e(called)g(recursi)n(v)o
(ely)-5 b(,)14 b(and)h(there)g(is)h(a)g(standard)f(interf)o(ace)f(to)i
(call)f(a)h(Python)f(function.)0 1501 y(\(I)21 b(w)o(on')o(t)f(dwell)h
(on)g(ho)n(w)g(to)g(call)h(the)f(Python)f(parser)h(with)g(a)h
(particular)e(string)h(as)h(input)e(\227)i(if)f(you')l(re)f
(interested,)g(ha)n(v)o(e)h(a)h(look)e(at)0 1601 y(the)g
(implementation)e(of)i(the)g Fr(-c)h Ft(command)d(line)i(option)f(in)h
(`)p Fu(Python/p)n(ythonmain.c)p Ft(')c(from)j(the)h(Python)f(source)h
(code.\))0 1747 y(Calling)g(a)h(Python)d(function)h(is)i(easy)-5
b(.)24 b(First,)d(the)f(Python)f(program)f(must)i(someho)n(w)f(pass)h
(you)g(the)g(Python)e(function)h(object.)24 b(Y)-9 b(ou)0
1847 y(should)16 b(pro)o(vide)f(a)i(function)f(\(or)g(some)h(other)f
(interf)o(ace\))g(to)h(do)g(this.)24 b(When)17 b(this)h(function)d(is)j
(called,)f(sa)n(v)o(e)g(a)h(pointer)d(to)j(the)f(Python)0
1947 y(function)23 b(object)g(\(be)h(careful)g(to)g Fm(Py_INCREF\(\))f
Ft(it!\))38 b(in)24 b(a)h(global)f(v)n(ariable)f(\227)i(or)f(where)n(v)
o(er)e(you)i(see)h(\002t.)38 b(F)o(or)24 b(e)o(xample,)g(the)0
2046 y(follo)n(wing)19 b(function)f(might)i(be)g(part)g(of)f(a)i
(module)e(de\002nition:)236 2284 y Fj(static)44 b(PyObject)954
2297 y(*)999 2284 y(my_callback)e(=)j(NULL;)236 2467
y(static)f(PyObject)954 2480 y(*)236 2558 y(my_set_callback\(PyObject)
1357 2571 y(*)1402 2558 y(dummy,)g(PyObject)2119 2571
y(*)2164 2558 y(args\))236 2650 y({)416 2741 y(PyObject)819
2754 y(*)864 2741 y(result)g(=)g(NULL;)416 2832 y(PyObject)819
2845 y(*)864 2832 y(temp;)416 3015 y(if)g(\(PyArg_ParseTuple\(args,)d
("O:set_callback",)h(&temp\)\))i({)595 3106 y(if)g
(\(!PyCallable_Check\(temp\)\))d({)774 3198 y
(PyErr_SetString\(PyExc_TypeError,)f("parameter)j(must)h(be)g
(callable"\);)774 3289 y(return)g(NULL;)595 3380 y(})595
3472 y(Py_XINCREF\(temp\);)401 b(/)1806 3485 y(*)1895
3472 y(Add)44 b(a)h(reference)e(to)i(new)f(callback)3330
3485 y(*)3375 3472 y(/)595 3563 y(Py_XDECREF\(my_callback\);)86
b(/)1806 3576 y(*)1895 3563 y(Dispose)44 b(of)g(previous)g(callback)
3195 3576 y(*)3240 3563 y(/)595 3654 y(my_callback)f(=)i(temp;)313
b(/)1806 3667 y(*)1895 3654 y(Remember)43 b(new)i(callback)2881
3667 y(*)2926 3654 y(/)595 3746 y(/)640 3759 y(*)729
3746 y(Boilerplate)e(to)i(return)f("None")2029 3759 y(*)2074
3746 y(/)595 3837 y(Py_INCREF\(Py_None\);)595 3928 y(result)g(=)g
(Py_None;)416 4020 y(})416 4111 y(return)f(result;)236
4202 y(})0 4489 y Ft(This)21 b(function)f(must)h(be)g(re)o(gistered)f
(with)h(the)h(interpreter)d(using)i(the)g Fm(METH_VARARGS)f
Ft(\003ag;)h(this)h(is)g(described)e(in)i(section)e(1.4,)0
4588 y(\223The)f(Module')-5 b(s)18 b(Method)g(T)-7 b(able)20
b(and)e(Initialization)g(Function.)-6 b(\224)24 b(The)19
b Fm(PyArg_ParseTuple\(\))d Ft(function)i(and)g(its)j(ar)o(guments)0
4688 y(are)f(documented)e(in)i(section)g(1.7,)f(\223Extracting)g(P)o
(arameters)g(in)h(Extension)f(Functions.)-6 b(\224)0
4835 y(The)26 b(macros)f Fm(Py_XINCREF\(\))g Ft(and)h
Fm(Py_XDECREF\(\))e Ft(increment/decrement)f(the)j(reference)f(count)g
(of)h(an)g(object)g(and)g(are)0 4935 y(safe)g(in)g(the)g(presence)f(of)
g Fm(NULL)h Ft(pointers)f(\(b)n(ut)g(note)h(that)f Fq(temp)h
Ft(will)h(not)e(be)h Fm(NULL)g Ft(in)g(this)g(conte)o(xt\).)40
b(More)25 b(info)h(on)f(them)g(in)0 5034 y(section)20
b(1.10,)f(\223Reference)g(Counts.)-6 b(\224)0 5181 y(Later)m(,)29
b(when)f(it)h(is)g(time)g(to)f(call)h(the)f(function,)g(you)g(call)g
(the)h(C)g(function)d Fm(PyEval_CallObject\(\))p Ft(.)46
b(This)29 b(function)d(has)0 5281 y(tw)o(o)f(ar)o(guments,)d(both)i
(pointers)f(to)i(arbitrary)d(Python)h(objects:)33 b(the)25
b(Python)e(function,)g(and)h(the)g(ar)o(gument)e(list.)38
b(The)24 b(ar)o(gument)0 5380 y(list)29 b(must)f(al)o(w)o(ays)g(be)g(a)
g(tuple)f(object,)i(whose)f(length)f(is)h(the)g(number)e(of)i(ar)o
(guments.)45 b(T)-7 b(o)28 b(call)g(the)g(Python)f(function)f(with)i
(no)p 0 5549 3901 4 v 0 5649 a Fo(1.6.)52 b(Calling)25
b(Python)e(Functions)g(from)h(C)2473 b(7)p eop end
%%Page: 8 14
TeXDict begin 8 13 bop 0 83 a Ft(ar)o(guments,)21 b(pass)h(an)h(empty)e
(tuple;)i(to)g(call)f(it)h(with)g(one)f(ar)o(gument,)e(pass)j(a)f
(singleton)g(tuple.)31 b Fm(Py_BuildValue\(\))20 b Ft(returns)h(a)0
183 y(tuple)f(when)f(its)j(format)d(string)h(consists)g(of)g(zero)g(or)
g(more)f(format)g(codes)h(between)f(parentheses.)24 b(F)o(or)c(e)o
(xample:)416 421 y Fj(int)44 b(arg;)416 512 y(PyObject)819
525 y(*)864 512 y(arglist;)416 603 y(PyObject)819 616
y(*)864 603 y(result;)416 695 y(...)416 786 y(arg)g(=)h(123;)416
877 y(...)416 969 y(/)461 982 y(*)550 969 y(Time)f(to)h(call)f(the)g
(callback)1716 982 y(*)1761 969 y(/)416 1060 y(arglist)f(=)i
(Py_BuildValue\("\(i\)",)c(arg\);)416 1151 y(result)i(=)i
(PyEval_CallObject\(my_callback,)40 b(arglist\);)416
1243 y(Py_DECREF\(arglist\);)0 1529 y Fm(PyEval_CallObject\(\))g
Ft(returns)i(a)h(Python)e(object)h(pointer:)70 b(this)43
b(is)g(the)g(return)f(v)n(alue)g(of)g(the)h(Python)f(function.)0
1629 y Fm(PyEval_CallObject\(\))23 b Ft(is)k
(\223reference-count-neutral\224)21 b(with)26 b(respect)g(to)g(its)i
(ar)o(guments.)40 b(In)26 b(the)h(e)o(xample)d(a)j(ne)n(w)f(tuple)0
1729 y(w)o(as)21 b(created)e(to)i(serv)o(e)e(as)i(the)g(ar)o(gument)c
(list,)k(which)f(is)h Fm(Py_DECREF\(\))p Ft(-ed)d(immediately)h(after)h
(the)g(call.)0 1875 y(The)25 b(return)f(v)n(alue)g(of)h
Fm(PyEval_CallObject\(\))e Ft(is)j(\223ne)n(w\224:)35
b(either)24 b(it)i(is)g(a)g(brand)e(ne)n(w)h(object,)h(or)e(it)i(is)h
(an)e(e)o(xisting)f(object)0 1975 y(whose)19 b(reference)f(count)g(has)
h(been)g(incremented.)j(So,)e(unless)f(you)g(w)o(ant)g(to)g(sa)n(v)o(e)
h(it)g(in)f(a)h(global)e(v)n(ariable,)g(you)h(should)f(someho)n(w)0
2075 y Fm(Py_DECREF\(\))h Ft(the)h(result,)g(e)n(v)o(en)f
(\(especially!\))24 b(if)c(you)g(are)g(not)f(interested)h(in)g(its)i(v)
n(alue.)0 2222 y(Before)27 b(you)f(do)g(this,)k(ho)n(we)n(v)o(er)m(,)c
(it)i(is)g(important)d(to)j(check)e(that)h(the)g(return)f(v)n(alue)g
(isn')o(t)h Fm(NULL)p Ft(.)g(If)g(it)h(is,)h(the)e(Python)f(function)0
2321 y(terminated)g(by)g(raising)h(an)f(e)o(xception.)44
b(If)26 b(the)h(C)h(code)e(that)h(called)g Fm(PyEval_CallObject\(\))d
Ft(is)k(called)f(from)f(Python,)h(it)0 2421 y(should)i(no)n(w)g(return)
f(an)i(error)e(indication)g(to)i(its)h(Python)d(caller)m(,)k(so)e(the)f
(interpreter)f(can)i(print)f(a)h(stack)f(trace,)j(or)d(the)h(calling)0
2520 y(Python)22 b(code)g(can)h(handle)f(the)h(e)o(xception.)31
b(If)23 b(this)g(is)h(not)f(possible)f(or)h(desirable,)g(the)f(e)o
(xception)g(should)f(be)i(cleared)f(by)h(calling)0 2620
y Fm(PyErr_Clear\(\))p Ft(.)g(F)o(or)d(e)o(xample:)416
2767 y Fj(if)44 b(\(result)g(==)g(NULL\))595 2858 y(return)g(NULL;)g(/)
1223 2871 y(*)1312 2858 y(Pass)g(error)g(back)2029 2871
y(*)2074 2858 y(/)416 2950 y(...use)f(result...)416 3041
y(Py_DECREF\(result\);)0 3327 y Ft(Depending)24 b(on)i(the)g(desired)f
(interf)o(ace)h(to)g(the)g(Python)f(callback)h(function,)f(you)g(may)h
(also)h(ha)n(v)o(e)e(to)h(pro)o(vide)f(an)h(ar)o(gument)d(list)0
3427 y(to)f Fm(PyEval_CallObject\(\))p Ft(.)27 b(In)22
b(some)g(cases)g(the)g(ar)o(gument)e(list)j(is)g(also)f(pro)o(vided)e
(by)h(the)h(Python)f(program,)f(through)g(the)0 3527
y(same)i(interf)o(ace)g(that)g(speci\002ed)g(the)h(callback)e
(function.)30 b(It)22 b(can)g(then)g(be)g(sa)n(v)o(ed)g(and)g(used)g
(in)g(the)h(same)f(manner)f(as)i(the)f(function)0 3626
y(object.)i(In)c(other)g(cases,)g(you)g(may)f(ha)n(v)o(e)h(to)g
(construct)g(a)g(ne)n(w)g(tuple)g(to)g(pass)h(as)g(the)f(ar)o(gument)e
(list.)26 b(The)20 b(simplest)h(w)o(ay)f(to)g(do)g(this)0
3726 y(is)h(to)f(call)h Fm(Py_BuildValue\(\))p Ft(.)i(F)o(or)c(e)o
(xample,)g(if)h(you)f(w)o(ant)i(to)f(pass)h(an)f(inte)o(gral)f(e)n(v)o
(ent)g(code,)g(you)g(might)h(use)g(the)g(follo)n(wing)0
3826 y(code:)416 3972 y Fj(PyObject)819 3985 y(*)864
3972 y(arglist;)416 4064 y(...)416 4155 y(arglist)43
b(=)i(Py_BuildValue\("\(l\)",)c(eventcode\);)416 4246
y(result)i(=)i(PyEval_CallObject\(my_callback,)40 b(arglist\);)416
4338 y(Py_DECREF\(arglist\);)416 4429 y(if)k(\(result)g(==)g(NULL\))595
4520 y(return)g(NULL;)g(/)1223 4533 y(*)1312 4520 y(Pass)g(error)g
(back)2029 4533 y(*)2074 4520 y(/)416 4612 y(/)461 4625
y(*)550 4612 y(Here)g(maybe)g(use)h(the)f(result)1716
4625 y(*)1761 4612 y(/)416 4703 y(Py_DECREF\(result\);)0
4989 y Ft(Note)22 b(the)g(placement)e(of)i(`)p Fm(Py_DECREF\(arglist\))
p Ft(')c(immediately)j(after)g(the)h(call,)h(before)d(the)i(error)f
(check!)29 b(Also)22 b(note)f(that)0 5089 y(strictly)f(spok)o(en)g
(this)g(code)g(is)h(not)f(complete:)k Fm(Py_BuildValue\(\))18
b Ft(may)i(run)f(out)h(of)g(memory)-5 b(,)18 b(and)h(this)i(should)e
(be)h(check)o(ed.)p 0 5549 3901 4 v 0 5649 a Fo(8)2210
b(Chapter)24 b(1.)52 b(Extending)24 b(Python)f(with)h(C)f(or)g(C)3780
5641 y(++)p eop end
%%Page: 9 15
TeXDict begin 9 14 bop 0 86 a Fl(1.7)121 b(Extr)o(acting)33
b(P)-5 b(ar)o(ameters)34 b(in)g(Extension)g(Functions)0
319 y Ft(The)20 b Fm(PyArg_ParseTuple\(\))d Ft(function)i(is)i
(declared)e(as)i(follo)n(ws:)236 557 y Fj(int)45 b
(PyArg_ParseTuple\(PyObject)1581 570 y(*)1626 557 y(arg,)f(char)2074
570 y(*)2119 557 y(format,)g(...\);)0 844 y Ft(The)32
b Fq(ar)m(g)g Ft(ar)o(gument)f(must)h(be)g(a)h(tuple)f(object)g
(containing)f(an)h(ar)o(gument)e(list)k(passed)e(from)g(Python)f(to)i
(a)f(C)i(function.)60 b(The)0 944 y Fq(format)20 b Ft(ar)o(gument)d
(must)i(be)f(a)i(format)e(string,)g(whose)h(syntax)f(is)i(e)o(xplained)
d(in)i(\223P)o(arsing)f(ar)o(guments)e(and)j(b)n(uilding)e(v)n
(alues\224)i(in)g(the)0 1043 y Fq(Python/C)h(API)h(Refer)m(ence)f
(Manual)p Ft(.)27 b(The)20 b(remaining)g(ar)o(guments)f(must)i(be)g
(addresses)f(of)h(v)n(ariables)f(whose)h(type)g(is)g(determined)0
1143 y(by)f(the)g(format)f(string.)0 1290 y(Note)h(that)h(while)f
Fm(PyArg_ParseTuple\(\))d Ft(checks)j(that)g(the)h(Python)e(ar)o
(guments)f(ha)n(v)o(e)i(the)g(required)e(types,)i(it)h(cannot)e(check)0
1389 y(the)26 b(v)n(alidity)f(of)h(the)g(addresses)f(of)h(C)h(v)n
(ariables)e(passed)h(to)g(the)g(call:)37 b(if)26 b(you)f(mak)o(e)g
(mistak)o(es)i(there,)f(your)f(code)g(will)i(probably)0
1489 y(crash)20 b(or)g(at)h(least)f(o)o(v)o(erwrite)f(random)f(bits)j
(in)f(memory)-5 b(.)23 b(So)e(be)f(careful!)0 1636 y(Note)26
b(that)f(an)o(y)g(Python)g(object)g(references)f(which)h(are)h(pro)o
(vided)d(to)j(the)f(caller)h(are)f Fq(borr)l(owed)h Ft(references;)h
(do)e(not)g(decrement)0 1735 y(their)20 b(reference)e(count!)0
1882 y(Some)i(e)o(xample)f(calls:)416 2120 y Fj(int)44
b(ok;)416 2212 y(int)g(i,)g(j;)416 2303 y(long)g(k,)g(l;)416
2394 y(const)g(char)909 2407 y(*)954 2394 y(s;)416 2486
y(int)g(size;)416 2668 y(ok)g(=)h(PyArg_ParseTuple\(args,)c(""\);)j(/)
1940 2681 y(*)2029 2668 y(No)h(arguments)2612 2681 y(*)2657
2668 y(/)595 2760 y(/)640 2773 y(*)729 2760 y(Python)f(call:)g(f\(\))
1492 2773 y(*)1537 2760 y(/)416 3081 y(ok)g(=)h
(PyArg_ParseTuple\(args,)c("s",)j(&s\);)g(/)2164 3094
y(*)2254 3081 y(A)g(string)2657 3094 y(*)2702 3081 y(/)595
3172 y(/)640 3185 y(*)729 3172 y(Possible)g(Python)g(call:)g
(f\('whoops!'\))2298 3185 y(*)2343 3172 y(/)416 3493
y(ok)g(=)h(PyArg_ParseTuple\(args,)c("lls",)j(&k,)g(&l,)g(&s\);)g(/)
2612 3506 y(*)2702 3493 y(Two)g(longs)g(and)h(a)f(string)3733
3506 y(*)3778 3493 y(/)595 3585 y(/)640 3598 y(*)729
3585 y(Possible)g(Python)g(call:)g(f\(1,)g(2,)g('three'\))2478
3598 y(*)2523 3585 y(/)416 3906 y(ok)g(=)h(PyArg_ParseTuple\(args,)c
("\(ii\)s#",)i(&i,)h(&j,)h(&s,)f(&size\);)595 3997 y(/)640
4010 y(*)729 3997 y(A)h(pair)f(of)h(ints)f(and)g(a)h(string,)e(whose)h
(size)h(is)f(also)g(returned)3285 4010 y(*)3330 3997
y(/)595 4089 y(/)640 4102 y(*)729 4089 y(Possible)g(Python)g(call:)g
(f\(\(1,)g(2\),)g('three'\))2567 4102 y(*)2612 4089 y(/)p
0 5549 3901 4 v 0 5649 a Fo(1.7.)52 b(Extr)o(acting)23
b(P)m(ar)o(ameters)g(in)h(Extension)g(Functions)1989
b(9)p eop end
%%Page: 10 16
TeXDict begin 10 15 bop 416 174 a Fj({)595 266 y(const)44
b(char)1088 279 y(*)1133 266 y(file;)595 357 y(const)g(char)1088
370 y(*)1133 357 y(mode)g(=)h("r";)595 448 y(int)f(bufsize)g(=)h(0;)595
540 y(ok)f(=)h(PyArg_ParseTuple\(args,)c("s|si",)j(&file,)g(&mode,)f
(&bufsize\);)595 631 y(/)640 644 y(*)729 631 y(A)i(string,)f(and)g
(optionally)f(another)h(string)g(and)g(an)g(integer)3195
644 y(*)3240 631 y(/)595 722 y(/)640 735 y(*)729 722
y(Possible)g(Python)g(calls:)729 814 y(f\('spam'\))729
905 y(f\('spam',)g('w'\))729 996 y(f\('spam',)g('wb',)g(100000\))1805
1009 y(*)1850 996 y(/)416 1088 y(})416 1409 y({)595 1500
y(int)g(left,)g(top,)g(right,)g(bottom,)g(h,)g(v;)595
1591 y(ok)g(=)h(PyArg_ParseTuple\(args,)c("\(\(ii\)\(ii\)\)\(ii\)",)998
1683 y(&left,)j(&top,)g(&right,)g(&bottom,)f(&h,)i(&v\);)595
1774 y(/)640 1787 y(*)729 1774 y(A)g(rectangle)e(and)i(a)f(point)1805
1787 y(*)1850 1774 y(/)595 1865 y(/)640 1878 y(*)729
1865 y(Possible)g(Python)g(call:)729 1957 y(f\(\(\(0,)g(0\),)h(\(400,)f
(300\)\),)f(\(10,)h(10\)\))2254 1970 y(*)2299 1957 y(/)416
2048 y(})416 2369 y({)595 2461 y(Py_complex)f(c;)595
2552 y(ok)h(=)h(PyArg_ParseTuple\(args,)c("D:myfunction",)h(&c\);)595
2643 y(/)640 2656 y(*)729 2643 y(a)j(complex,)f(also)g(providing)f(a)i
(function)e(name)h(for)h(errors)3105 2656 y(*)3150 2643
y(/)595 2735 y(/)640 2748 y(*)729 2735 y(Possible)f(Python)g(call:)g
(myfunction\(1+2j\))2478 2748 y(*)2523 2735 y(/)416 2826
y(})0 3274 y Fl(1.8)121 b(K)-5 b(e)n(yw)o(ord)34 b(P)-5
b(ar)o(ameters)34 b(f)l(or)g(Extension)g(Functions)0
3507 y Ft(The)20 b Fm(PyArg_ParseTupleAndKeywords\(\))15
b Ft(function)k(is)i(declared)e(as)i(follo)n(ws:)236
3745 y Fj(int)45 b(PyArg_ParseTupleAndKeywords\(PyObject)2074
3758 y(*)2119 3745 y(arg,)f(PyObject)2747 3758 y(*)2792
3745 y(kwdict,)1671 3836 y(char)1895 3849 y(*)1940 3836
y(format,)f(char)2523 3849 y(*)2568 3836 y(kwlist[],)g(...\);)0
4123 y Ft(The)17 b Fq(ar)m(g)h Ft(and)f Fq(format)i Ft(parameters)e
(are)g(identical)g(to)h(those)g(of)f(the)h Fm(PyArg_ParseTuple\(\))d
Ft(function.)22 b(The)c Fq(kwdict)h Ft(parameter)0 4223
y(is)30 b(the)f(dictionary)e(of)i(k)o(e)o(yw)o(ords)e(recei)n(v)o(ed)h
(as)h(the)g(third)g(parameter)e(from)h(the)h(Python)f(runtime.)50
b(The)29 b Fq(kwlist)j Ft(parameter)27 b(is)j(a)0 4322
y Fm(NULL)p Ft(-terminated)18 b(list)j(of)f(strings)h(which)e(identify)
h(the)g(parameters;)f(the)i(names)f(are)g(matched)f(with)h(the)h(type)f
(information)e(from)0 4422 y Fq(format)29 b Ft(from)e(left)i(to)f
(right.)48 b(On)28 b(success,)i Fm(PyArg_ParseTupleAndKeywords\(\))23
b Ft(returns)k(true,)i(otherwise)f(it)g(returns)0 4521
y(f)o(alse)21 b(and)e(raises)i(an)f(appropriate)e(e)o(xception.)0
4668 y Fr(Note:)28 b Ft(Nested)22 b(tuples)g(cannot)f(be)h(parsed)f
(when)g(using)h(k)o(e)o(yw)o(ord)e(ar)o(guments!)28 b(K)n(e)o(yw)o(ord)
21 b(parameters)f(passed)i(in)g(which)g(are)g(not)0 4768
y(present)e(in)g(the)g Fq(kwlist)j Ft(will)e(cause)f
Fm(TypeError)f Ft(to)i(be)f(raised.)0 4915 y(Here)g(is)h(an)f(e)o
(xample)f(module)g(which)h(uses)g(k)o(e)o(yw)o(ords,)f(based)g(on)h(an)
g(e)o(xample)f(by)h(Geof)n(f)f(Philbrick)g(\()p Fu(philbr)q(ic)o(k@hks)
o(.com)p Ft(\):)p 0 5549 3901 4 v 0 5649 a Fo(10)2164
b(Chapter)24 b(1.)52 b(Extending)24 b(Python)f(with)h(C)f(or)g(C)3780
5641 y(++)p eop end
%%Page: 11 17
TeXDict begin 11 16 bop 236 174 a Fj(#include)44 b("Python.h")236
357 y(static)g(PyObject)954 370 y(*)236 448 y
(keywdarg_parrot\(PyObject)1357 461 y(*)1402 448 y(self,)g(PyObject)
2074 461 y(*)2119 448 y(args,)g(PyObject)2792 461 y(*)2837
448 y(keywds\))236 540 y({)416 631 y(int)g(voltage;)416
722 y(char)640 735 y(*)685 722 y(state)g(=)g("a)h(stiff";)416
814 y(char)640 827 y(*)685 814 y(action)e(=)i("voom";)416
905 y(char)640 918 y(*)685 905 y(type)f(=)g("Norwegian)g(Blue";)416
1088 y(static)f(char)954 1101 y(*)999 1088 y(kwlist[])g(=)i
({"voltage",)d("state",)i("action",)f("type",)h(NULL};)416
1270 y(if)g(\(!PyArg_ParseTupleAndKeywords\(args,)39
b(keywds,)44 b("i|sss",)f(kwlist,)1895 1362 y(&voltage,)g(&state,)h
(&action,)f(&type\)\))595 1453 y(return)h(NULL;)416 1636
y(printf\("--)f(This)h(parrot)g(wouldn't)f(\045s)i(if)f(you)g(put)h
(\045i)f(Volts)g(through)g(it.\\n",)729 1727 y(action,)g(voltage\);)416
1818 y(printf\("--)f(Lovely)h(plumage,)f(the)h(\045s)h(--)f(It's)g
(\045s!\\n",)g(type,)g(state\);)416 2001 y(Py_INCREF\(Py_None\);)416
2183 y(return)f(Py_None;)236 2275 y(})236 2457 y(static)h(PyMethodDef)f
(keywdarg_methods[])f(=)i({)416 2549 y(/)461 2562 y(*)550
2549 y(The)g(cast)h(of)f(the)g(function)g(is)g(necessary)g(since)g
(PyCFunction)f(values)460 2653 y(*)550 2640 y(only)h(take)g(two)h
(PyObject)1538 2653 y(*)1626 2640 y(parameters,)e(and)h
(keywdarg_parrot\(\))e(takes)460 2744 y(*)550 2731 y(three.)460
2836 y(*)505 2823 y(/)416 2914 y({"parrot",)h
(\(PyCFunction\)keywdarg_parrot,)d(METH_VARARGS)i(|)j(METH_KEYWORDS,)
460 3005 y("Print)f(a)h(lovely)f(skit)g(to)g(standard)g(output."},)416
3097 y({NULL,)f(NULL,)h(0,)h(NULL})134 b(/)1537 3110
y(*)1626 3097 y(sentinel)2029 3110 y(*)2074 3097 y(/)236
3188 y(};)236 3509 y(void)236 3601 y(initkeywdarg\(void\))236
3692 y({)326 3783 y(/)371 3796 y(*)460 3783 y(Create)44
b(the)h(module)e(and)i(add)f(the)g(functions)2254 3796
y(*)2299 3783 y(/)326 3875 y(Py_InitModule\("keywdarg",)d
(keywdarg_methods\);)236 3966 y(})0 4413 y Fl(1.9)121
b(Building)33 b(Arbitr)o(ar)t(y)f(V)-8 b(alues)0 4646
y Ft(This)20 b(function)f(is)i(the)f(counterpart)e(to)i
Fm(PyArg_ParseTuple\(\))p Ft(.)i(It)f(is)g(declared)e(as)i(follo)n(ws:)
236 4793 y Fj(PyObject)640 4806 y(*)685 4793 y(Py_BuildValue\(char)1536
4806 y(*)1581 4793 y(format,)44 b(...\);)0 5079 y Ft(It)28
b(recognizes)e(a)i(set)g(of)f(format)f(units)i(similar)f(to)h(the)f
(ones)g(recognized)f(by)h Fm(PyArg_ParseTuple\(\))p Ft(,)f(b)n(ut)h
(the)h(ar)o(guments)0 5179 y(\(which)20 b(are)g(input)g(to)h(the)g
(function,)e(not)h(output\))f(must)i(not)f(be)h(pointers,)f(just)h(v)n
(alues.)26 b(It)21 b(returns)f(a)h(ne)n(w)f(Python)g(object,)g
(suitable)0 5278 y(for)g(returning)e(from)h(a)h(C)i(function)c(called)i
(from)f(Python.)p 0 5549 3901 4 v 0 5649 a Fo(1.9.)52
b(Building)26 b(Arbitr)o(ar)r(y)d(V)-6 b(alues)2714 b(11)p
eop end
%%Page: 12 18
TeXDict begin 12 17 bop 0 83 a Ft(One)20 b(dif)n(ference)e(with)j
Fm(PyArg_ParseTuple\(\))p Ft(:)h(while)e(the)g(latter)h(requires)e(its)
i(\002rst)g(ar)o(gument)d(to)i(be)g(a)h(tuple)f(\(since)g(Python)0
183 y(ar)o(gument)k(lists)29 b(are)d(al)o(w)o(ays)h(represented)e(as)j
(tuples)e(internally\),)h Fm(Py_BuildValue\(\))e Ft(does)h(not)g(al)o
(w)o(ays)i(b)n(uild)e(a)h(tuple.)44 b(It)0 282 y(b)n(uilds)18
b(a)h(tuple)f(only)f(if)i(its)g(format)e(string)h(contains)f(tw)o(o)i
(or)f(more)f(format)g(units.)25 b(If)18 b(the)g(format)f(string)h(is)h
(empty)-5 b(,)17 b(it)i(returns)f Fm(None)p Ft(;)0 382
y(if)i(it)h(contains)f(e)o(xactly)f(one)h(format)f(unit,)h(it)h
(returns)e(whate)n(v)o(er)g(object)g(is)i(described)e(by)h(that)g
(format)f(unit.)25 b(T)-7 b(o)21 b(force)e(it)i(to)f(return)f(a)0
482 y(tuple)h(of)g(size)h(0)f(or)g(one,)f(parenthesize)g(the)h(format)f
(string.)0 628 y(Examples)g(\(to)h(the)g(left)h(the)f(call,)g(to)h(the)
f(right)g(the)g(resulting)f(Python)g(v)n(alue\):)416
867 y Fj(Py_BuildValue\(""\))1073 b(None)416 958 y(Py_BuildValue\("i",)
41 b(123\))807 b(123)416 1049 y(Py_BuildValue\("iii",)41
b(123,)j(456,)g(789\))269 b(\(123,)44 b(456,)g(789\))416
1141 y(Py_BuildValue\("s",)d("hello"\))627 b('hello')416
1232 y(Py_BuildValue\("ss",)41 b("hello",)j("world"\))178
b(\('hello',)43 b('world'\))416 1323 y(Py_BuildValue\("s#",)e("hello",)
j(4\))448 b('hell')416 1415 y(Py_BuildValue\("\(\)"\))983
b(\(\))416 1506 y(Py_BuildValue\("\(i\)",)41 b(123\))717
b(\(123,\))416 1597 y(Py_BuildValue\("\(ii\)",)41 b(123,)j(456\))448
b(\(123,)44 b(456\))416 1689 y(Py_BuildValue\("\(i,i\)",)d(123,)j
(456\))403 b(\(123,)44 b(456\))416 1780 y(Py_BuildValue\("[i,i]",)d
(123,)j(456\))403 b([123,)44 b(456])416 1871 y
(Py_BuildValue\("{s:i,s:i}",)1043 1962 y("abc",)g(123,)g("def",)g
(456\))179 b({'abc':)43 b(123,)h('def':)g(456})416 2054
y(Py_BuildValue\("\(\(ii\)\(ii\)\))c(\(ii\)",)1043 2145
y(1,)45 b(2,)f(3,)h(4,)f(5,)h(6\))448 b(\(\(\(1,)44 b(2\),)g(\(3,)g
(4\)\),)g(\(5,)h(6\)\))0 2587 y Fl(1.10)121 b(Ref)l(erence)36
b(Counts)0 2820 y Ft(In)20 b(languages)e(lik)o(e)i(C)h(or)f(C)807
2813 y(++)901 2820 y(,)g(the)g(programmer)d(is)k(responsible)e(for)g
(dynamic)f(allocation)h(and)h(deallocation)e(of)i(memory)e(on)i(the)0
2920 y(heap.)36 b(In)23 b(C,)i(this)g(is)g(done)e(using)g(the)h
(functions)f Fm(malloc\(\))g Ft(and)g Fm(free\(\))p Ft(.)36
b(In)24 b(C)2557 2913 y(++)2651 2920 y(,)h(the)f(operators)f
Fm(new)h Ft(and)f Fm(delete)h Ft(are)0 3019 y(used)c(with)g
(essentially)h(the)f(same)g(meaning)f(and)g(we')o(ll)i(restrict)f(the)g
(follo)n(wing)f(discussion)h(to)g(the)g(C)h(case.)0 3166
y(Ev)o(ery)f(block)g(of)h(memory)e(allocated)i(with)g
Fm(malloc\(\))f Ft(should)g(e)n(v)o(entually)g(be)h(returned)e(to)i
(the)h(pool)e(of)h(a)n(v)n(ailable)g(memory)e(by)0 3266
y(e)o(xactly)25 b(one)h(call)g(to)h Fm(free\(\))p Ft(.)42
b(It)26 b(is)i(important)c(to)i(call)h Fm(free\(\))f
Ft(at)g(the)g(right)g(time.)43 b(If)26 b(a)h(block')-5
b(s)25 b(address)h(is)h(for)o(gotten)d(b)n(ut)0 3365
y Fm(free\(\))g Ft(is)h(not)e(called)h(for)g(it,)h(the)f(memory)f(it)h
(occupies)g(cannot)f(be)h(reused)f(until)h(the)g(program)e(terminates.)
36 b(This)24 b(is)h(called)f(a)0 3465 y Fq(memory)19
b(leak)p Ft(.)25 b(On)19 b(the)g(other)f(hand,)g(if)i(a)f(program)e
(calls)j Fm(free\(\))f Ft(for)g(a)g(block)f(and)h(then)g(continues)f
(to)h(use)g(the)g(block,)g(it)h(creates)0 3565 y(a)f(con\003ict)f(with)
g(re-use)g(of)g(the)h(block)e(through)g(another)f Fm(malloc\(\))i
Ft(call.)25 b(This)18 b(is)i(called)e Fq(using)g(fr)m(eed)g(memory)p
Ft(.)25 b(It)18 b(has)h(the)f(same)0 3664 y(bad)i(consequences)e(as)j
(referencing)d(uninitialized)g(data)i(\227)h(core)f(dumps,)f(wrong)g
(results,)h(mysterious)f(crashes.)0 3811 y(Common)e(causes)i(of)g
(memory)e(leaks)i(are)g(unusual)e(paths)i(through)d(the)j(code.)24
b(F)o(or)18 b(instance,)h(a)g(function)e(may)h(allocate)h(a)g(block)f
(of)0 3911 y(memory)-5 b(,)20 b(do)i(some)f(calculation,)g(and)h(then)f
(free)h(the)g(block)f(again.)29 b(No)n(w)22 b(a)h(change)d(in)i(the)g
(requirements)e(for)i(the)g(function)e(may)0 4010 y(add)i(a)h(test)h
(to)f(the)g(calculation)e(that)i(detects)g(an)g(error)e(condition)g
(and)h(can)h(return)e(prematurely)g(from)h(the)g(function.)31
b(It')-5 b(s)24 b(easy)e(to)0 4110 y(for)o(get)16 b(to)h(free)g(the)g
(allocated)g(memory)e(block)h(when)h(taking)f(this)i(premature)d(e)o
(xit,)j(especially)f(when)f(it)i(is)g(added)f(later)g(to)g(the)g(code.)
0 4210 y(Such)k(leaks,)g(once)g(introduced,)d(often)j(go)g(undetected)e
(for)i(a)g(long)g(time:)27 b(the)21 b(error)f(e)o(xit)h(is)h(tak)o(en)f
(only)g(in)g(a)g(small)h(fraction)e(of)h(all)0 4309 y(calls,)f(and)g
(most)f(modern)f(machines)h(ha)n(v)o(e)h(plenty)e(of)i(virtual)f
(memory)-5 b(,)18 b(so)i(the)g(leak)f(only)g(becomes)g(apparent)f(in)i
(a)g(long-running)0 4409 y(process)g(that)g(uses)h(the)f(leaking)f
(function)f(frequently)-5 b(.)23 b(Therefore,)17 b(it')-5
b(s)22 b(important)c(to)j(pre)n(v)o(ent)d(leaks)i(from)f(happening)f
(by)i(ha)n(ving)0 4509 y(a)h(coding)d(con)m(v)o(ention)f(or)j(strate)o
(gy)g(that)g(minimizes)g(this)g(kind)g(of)g(errors.)0
4655 y(Since)i(Python)e(mak)o(es)i(hea)n(vy)f(use)g(of)h
Fm(malloc\(\))f Ft(and)g Fm(free\(\))p Ft(,)g(it)h(needs)g(a)g(strate)o
(gy)f(to)g(a)n(v)n(oid)h(memory)e(leaks)i(as)g(well)g(as)h(the)0
4755 y(use)f(of)f(freed)f(memory)-5 b(.)26 b(The)21 b(chosen)f(method)g
(is)i(called)g Fq(r)m(efer)m(ence)f(counting)p Ft(.)26
b(The)21 b(principle)f(is)i(simple:)28 b(e)n(v)o(ery)20
b(object)g(contains)0 4855 y(a)i(counter)m(,)e(which)g(is)j
(incremented)c(when)i(a)h(reference)e(to)h(the)h(object)f(is)h(stored)f
(some)n(where,)f(and)h(which)g(is)h(decremented)d(when)0
4954 y(a)25 b(reference)e(to)i(it)g(is)h(deleted.)37
b(When)25 b(the)f(counter)f(reaches)h(zero,)h(the)g(last)g(reference)e
(to)i(the)g(object)f(has)h(been)f(deleted)f(and)i(the)0
5054 y(object)20 b(is)h(freed.)0 5201 y(An)j(alternati)n(v)o(e)e
(strate)o(gy)h(is)i(called)f Fq(automatic)e(garba)o(g)o(e)g(collection)
p Ft(.)35 b(\(Sometimes,)24 b(reference)e(counting)g(is)j(also)f
(referred)e(to)i(as)0 5300 y(a)h(garbage)e(collection)h(strate)o(gy)-5
b(,)24 b(hence)g(my)h(use)g(of)f(\223automatic\224)g(to)h(distinguish)f
(the)g(tw)o(o.\))39 b(The)25 b(big)f(adv)n(antage)f(of)i(automatic)0
5400 y(garbage)17 b(collection)i(is)h(that)g(the)g(user)f(doesn')o(t)f
(need)h(to)h(call)g Fm(free\(\))f Ft(e)o(xplicitly)-5
b(.)23 b(\(Another)18 b(claimed)h(adv)n(antage)e(is)k(an)e(impro)o(v)o
(e-)p 0 5549 3901 4 v 0 5649 a Fo(12)2164 b(Chapter)24
b(1.)52 b(Extending)24 b(Python)f(with)h(C)f(or)g(C)3780
5641 y(++)p eop end
%%Page: 13 19
TeXDict begin 13 18 bop 0 83 a Ft(ment)27 b(in)h(speed)f(or)g(memory)e
(usage)i(\227)h(this)g(is)g(no)f(hard)g(f)o(act)g(ho)n(we)n(v)o(er)-5
b(.\))45 b(The)27 b(disadv)n(antage)e(is)k(that)e(for)g(C,)h(there)f
(is)h(no)f(truly)0 183 y(portable)19 b(automatic)h(garbage)f(collector)
m(,)g(while)i(reference)d(counting)h(can)i(be)f(implemented)f(portably)
g(\(as)i(long)f(as)h(the)g(functions)0 282 y Fm(malloc\(\))e
Ft(and)h Fm(free\(\))g Ft(are)g(a)n(v)n(ailable)g(\227)g(which)g(the)g
(C)i(Standard)d(guarantees\).)k(Maybe)c(some)h(day)g(a)h(suf)n
(\002ciently)e(portable)0 382 y(automatic)g(garbage)f(collector)i(will)
g(be)h(a)n(v)n(ailable)e(for)h(C.)h(Until)f(then,)g(we')o(ll)g(ha)n(v)o
(e)g(to)g(li)n(v)o(e)g(with)g(reference)f(counts.)0 529
y(While)e(Python)f(uses)h(the)g(traditional)f(reference)f(counting)g
(implementation,)g(it)i(also)g(of)n(fers)f(a)h(c)o(ycle)g(detector)e
(that)i(w)o(orks)g(to)g(detect)0 628 y(reference)k(c)o(ycles.)31
b(This)23 b(allo)n(ws)g(applications)e(to)i(not)f(w)o(orry)f(about)h
(creating)f(direct)h(or)g(indirect)g(circular)g(references;)g(these)h
(are)0 728 y(the)h(weakness)g(of)h(garbage)d(collection)h(implemented)g
(using)h(only)f(reference)g(counting.)36 b(Reference)23
b(c)o(ycles)h(consist)h(of)f(objects)0 828 y(which)18
b(contain)f(\(possibly)g(indirect\))g(references)f(to)i(themselv)o(es,)
g(so)g(that)h(each)e(object)h(in)g(the)g(c)o(ycle)g(has)g(a)h
(reference)d(count)h(which)0 927 y(is)k(non-zero.)j(T)-7
b(ypical)20 b(reference)e(counting)h(implementations)f(are)j(not)f
(able)g(to)h(reclaim)e(the)i(memory)e(belonging)f(to)i(an)o(y)g
(objects)0 1027 y(in)27 b(a)g(reference)e(c)o(ycle,)j(or)e(referenced)f
(from)h(the)g(objects)h(in)g(the)f(c)o(ycle,)i(e)n(v)o(en)e(though)f
(there)h(are)h(no)f(further)f(references)h(to)h(the)0
1127 y(c)o(ycle)20 b(itself.)0 1273 y(The)e(c)o(ycle)g(detector)f(is)i
(able)f(to)g(detect)g(garbage)f(c)o(ycles)h(and)f(can)h(reclaim)g(them)
g(so)g(long)g(as)h(there)e(are)i(no)e(\002nalizers)h(implemented)0
1373 y(in)27 b(Python)e(\()p Fm(__del__\(\))g Ft(methods\).)43
b(When)26 b(there)h(are)f(such)g(\002nalizers,)i(the)f(detector)f(e)o
(xposes)f(the)i(c)o(ycles)f(through)f(the)h Fm(gc)0 1473
y Ft(module)c(\(speci\002cally)-5 b(,)22 b(the)h Fm(garbage)f
Ft(v)n(ariable)g(in)i(that)f(module\).)31 b(The)23 b
Fm(gc)g Ft(module)f(also)h(e)o(xposes)g(a)g(w)o(ay)g(to)g(run)g(the)g
(detector)0 1572 y(\(the)e Fm(collect\(\))f Ft(function\),)g(as)i(well)
g(as)g(con\002guration)d(interf)o(aces)i(and)f(the)i(ability)f(to)h
(disable)f(the)g(detector)g(at)g(runtime.)28 b(The)0
1672 y(c)o(ycle)23 b(detector)f(is)i(considered)e(an)h(optional)f
(component;)h(though)e(it)j(is)h(included)c(by)i(def)o(ault,)h(it)g
(can)f(be)g(disabled)g(at)g(b)n(uild)g(time)0 1772 y(using)c(the)h
Fr(--without-cycle-gc)c Ft(option)i(to)i(the)f Fr(con\002gur)o(e)g
Ft(script)h(on)h(U)t Fp(N)t(I)t(X)h Ft(platforms)d(\(including)e(Mac)j
(OS)g(X\))g(or)f(by)g(remo)o(ving)0 1871 y(the)j(de\002nition)f(of)h
Fm(WITH_CYCLE_GC)e Ft(in)j(the)f(`)p Fu(p)n(ycon\002g.h)p
Ft(')e(header)h(on)h(other)f(platforms.)30 b(If)22 b(the)g(c)o(ycle)g
(detector)f(is)i(disabled)e(in)0 1971 y(this)g(w)o(ay)-5
b(,)19 b(the)i Fm(gc)f Ft(module)f(will)i(not)f(be)g(a)n(v)n(ailable.)0
2256 y Fg(1.10.1)101 b(Ref)m(erence)29 b(Counting)g(in)f(Python)0
2459 y Ft(There)20 b(are)h(tw)o(o)g(macros,)f Fm(Py_INCREF\(x\))g
Ft(and)g Fm(Py_DECREF\(x\))p Ft(,)f(which)i(handle)f(the)g
(incrementing)f(and)h(decrementing)f(of)0 2558 y(the)j(reference)e
(count.)29 b Fm(Py_DECREF\(\))21 b Ft(also)h(frees)g(the)f(object)h
(when)f(the)h(count)f(reaches)g(zero.)30 b(F)o(or)21
b(\003e)o(xibility)-5 b(,)21 b(it)i(doesn')o(t)d(call)0
2658 y Fm(free\(\))f Ft(directly)f(\227)i(rather)m(,)e(it)i(mak)o(es)f
(a)h(call)f(through)e(a)j(function)d(pointer)h(in)i(the)f(object')-5
b(s)19 b Fq(type)g(object)p Ft(.)24 b(F)o(or)19 b(this)h(purpose)d
(\(and)0 2757 y(others\),)i(e)n(v)o(ery)g(object)h(also)g(contains)g(a)
g(pointer)f(to)h(its)i(type)d(object.)0 2904 y(The)30
b(big)f(question)g(no)n(w)g(remains:)44 b(when)30 b(to)g(use)g
Fm(Py_INCREF\(x\))e Ft(and)h Fm(Py_DECREF\(x\))p Ft(?)53
b(Let')-5 b(s)30 b(\002rst)h(introduce)d(some)0 3004
y(terms.)h(Nobody)19 b(\223o)n(wns\224)i(an)h(object;)g(ho)n(we)n(v)o
(er)m(,)d(you)h(can)i Fq(own)f(a)h(r)m(efer)m(ence)f
Ft(to)h(an)f(object.)28 b(An)22 b(object')-5 b(s)21 b(reference)f
(count)h(is)h(no)n(w)0 3104 y(de\002ned)f(as)i(the)f(number)e(of)i(o)n
(wned)f(references)g(to)h(it.)31 b(The)22 b(o)n(wner)f(of)h(a)h
(reference)d(is)j(responsible)e(for)g(calling)h Fm(Py_DECREF\(\))0
3203 y Ft(when)e(the)g(reference)f(is)i(no)f(longer)f(needed.)k
(Ownership)d(of)g(a)g(reference)f(can)h(be)g(transferred.)j(There)d
(are)g(three)g(w)o(ays)h(to)f(dispose)0 3303 y(of)d(an)h(o)n(wned)e
(reference:)23 b(pass)18 b(it)g(on,)g(store)f(it,)i(or)e(call)h
Fm(Py_DECREF\(\))p Ft(.)23 b(F)o(or)o(getting)15 b(to)j(dispose)f(of)h
(an)f(o)n(wned)g(reference)f(creates)0 3402 y(a)21 b(memory)d(leak.)0
3549 y(It)24 b(is)h(also)f(possible)f(to)h Fq(borr)l(ow)939
3519 y Fk(2)997 3549 y Ft(a)g(reference)e(to)i(an)g(object.)35
b(The)23 b(borro)n(wer)f(of)h(a)i(reference)d(should)h(not)g(call)h
Fm(Py_DECREF\(\))p Ft(.)0 3649 y(The)g(borro)n(wer)e(must)j(not)f(hold)
f(on)h(to)h(the)f(object)g(longer)f(than)h(the)h(o)n(wner)e(from)g
(which)h(it)h(w)o(as)h(borro)n(wed.)35 b(Using)24 b(a)h(borro)n(wed)0
3749 y(reference)19 b(after)g(the)i(o)n(wner)e(has)h(disposed)f(of)h
(it)h(risks)g(using)f(freed)f(memory)f(and)i(should)f(be)h(a)n(v)n
(oided)g(completely)-5 b(.)3467 3718 y Fk(3)0 3895 y
Ft(The)21 b(adv)n(antage)f(of)h(borro)n(wing)e(o)o(v)o(er)i(o)n(wning)f
(a)i(reference)e(is)i(that)g(you)f(don')o(t)f(need)h(to)g(tak)o(e)h
(care)f(of)h(disposing)e(of)i(the)f(reference)0 3995
y(on)g(all)i(possible)e(paths)h(through)e(the)i(code)f(\227)h(in)g
(other)f(w)o(ords,)h(with)g(a)g(borro)n(wed)e(reference)g(you)h(don')o
(t)f(run)h(the)h(risk)g(of)g(leaking)0 4095 y(when)i(a)h(premature)e(e)
o(xit)h(is)i(tak)o(en.)37 b(The)25 b(disadv)n(antage)d(of)i(borro)n
(wing)e(o)o(v)o(er)h(leaking)h(is)i(that)e(there)g(are)h(some)f(subtle)
h(situations)0 4194 y(where)19 b(in)h(seemingly)f(correct)g(code)g(a)h
(borro)n(wed)e(reference)g(can)h(be)h(used)g(after)f(the)h(o)n(wner)f
(from)f(which)i(it)g(w)o(as)h(borro)n(wed)c(has)j(in)0
4294 y(f)o(act)g(disposed)g(of)g(it.)0 4441 y(A)25 b(borro)n(wed)d
(reference)h(can)h(be)g(changed)f(into)h(an)g(o)n(wned)f(reference)g
(by)h(calling)g Fm(Py_INCREF\(\))p Ft(.)36 b(This)25
b(does)f(not)g(af)n(fect)g(the)0 4540 y(status)d(of)f(the)h(o)n(wner)f
(from)f(which)h(the)h(reference)d(w)o(as)k(borro)n(wed)c(\227)j(it)g
(creates)g(a)g(ne)n(w)f(o)n(wned)f(reference,)g(and)h(gi)n(v)o(es)g
(full)h(o)n(wner)0 4640 y(responsibilities)f(\(the)f(ne)n(w)h(o)n(wner)
g(must)g(dispose)g(of)f(the)i(reference)d(properly)-5
b(,)18 b(as)j(well)f(as)h(the)f(pre)n(vious)f(o)n(wner\).)p
0 4711 1560 4 v 90 4767 a Fi(2)120 4790 y Fp(The)d(metaphor)j(of)e
(\223borro)n(wing\224)i(a)f(reference)h(is)e(not)h(completely)h
(correct:)24 b(the)17 b(o)n(wner)h(still)h(has)e(a)g(cop)o(y)h(of)f
(the)h(reference.)90 4848 y Fi(3)120 4871 y Fp(Checking)j(that)f(the)g
(reference)i(count)e(is)f(at)h(least)g(1)f Ff(does)g(not)g(w)o(ork)h
Fp(\227)f(the)h(reference)i(count)e(itself)h(could)f(be)f(in)h(freed)g
(memory)f(and)h(may)f(thus)g(be)h(reused)0 4950 y(for)d(another)i
(object!)p 0 5549 3901 4 v 0 5649 a Fo(1.10.)52 b(Ref)n(erence)23
b(Counts)2905 b(13)p eop end
%%Page: 14 20
TeXDict begin 14 19 bop 0 83 a Fg(1.10.2)101 b(Ownership)28
b(Rules)0 286 y Ft(Whene)n(v)o(er)e(an)i(object)g(reference)e(is)j
(passed)f(into)g(or)g(out)f(of)h(a)g(function,)g(it)h(is)g(part)f(of)f
(the)i(function')-5 b(s)26 b(interf)o(ace)h(speci\002cation)0
386 y(whether)19 b(o)n(wnership)g(is)i(transferred)d(with)j(the)f
(reference)e(or)i(not.)0 532 y(Most)25 b(functions)d(that)j(return)e(a)
h(reference)f(to)h(an)h(object)e(pass)i(on)f(o)n(wnership)f(with)h(the)
g(reference.)36 b(In)24 b(particular)m(,)f(all)i(functions)0
632 y(whose)16 b(function)f(it)i(is)h(to)e(create)g(a)h(ne)n(w)g
(object,)f(such)g(as)h Fm(PyInt_FromLong\(\))e Ft(and)g
Fm(Py_BuildValue\(\))p Ft(,)g(pass)i(o)n(wnership)0 732
y(to)i(the)h(recei)n(v)o(er)-5 b(.)23 b(Ev)o(en)18 b(if)i(the)f(object)
g(is)h(not)f(actually)f(ne)n(w)-5 b(,)19 b(you)f(still)i(recei)n(v)o(e)
f(o)n(wnership)e(of)i(a)h(ne)n(w)f(reference)e(to)j(that)f(object.)24
b(F)o(or)0 831 y(instance,)c Fm(PyInt_FromLong\(\))d
Ft(maintains)j(a)h(cache)e(of)h(popular)f(v)n(alues)g(and)h(can)g
(return)f(a)i(reference)d(to)i(a)h(cached)e(item.)0 978
y(Man)o(y)48 b(functions)f(that)i(e)o(xtract)f(objects)h(from)f(other)g
(objects)g(also)h(transfer)f(o)n(wnership)g(with)h(the)f(reference,)55
b(for)48 b(in-)0 1078 y(stance)37 b Fm(PyObject_GetAttrString\(\))p
Ft(.)71 b(The)37 b(picture)f(is)h(less)h(clear)m(,)j(here,)f(ho)n(we)n
(v)o(er)m(,)e(since)g(a)f(fe)n(w)g(common)e(rou-)0 1177
y(tines)c(are)g(e)o(xceptions:)45 b Fm(PyTuple_GetItem\(\))p
Ft(,)31 b Fm(PyList_GetItem\(\))p Ft(,)f Fm(PyDict_GetItem\(\))p
Ft(,)h(and)f Fm(PyDict_-)0 1277 y(GetItemString\(\))18
b Ft(all)j(return)e(references)g(that)h(you)f(borro)n(w)g(from)g(the)h
(tuple,)f(list)j(or)e(dictionary)-5 b(.)0 1424 y(The)22
b(function)e Fm(PyImport_AddModule\(\))f Ft(also)j(returns)f(a)h(borro)
n(wed)e(reference,)g(e)n(v)o(en)h(though)f(it)j(may)f(actually)f
(create)h(the)0 1524 y(object)e(it)h(returns:)j(this)d(is)g(possible)f
(because)f(an)h(o)n(wned)f(reference)g(to)h(the)g(object)g(is)h(stored)
f(in)g Fm(sys.modules)p Ft(.)0 1670 y(When)i(you)f(pass)i(an)f(object)g
(reference)f(into)h(another)e(function,)h(in)i(general,)e(the)h
(function)f(borro)n(ws)g(the)h(reference)e(from)i(you)f(\227)0
1770 y(if)j(it)h(needs)e(to)h(store)g(it,)i(it)e(will)h(use)f
Fm(Py_INCREF\(\))f Ft(to)h(become)e(an)i(independent)d(o)n(wner)-5
b(.)36 b(There)23 b(are)h(e)o(xactly)f(tw)o(o)h(important)0
1870 y(e)o(xceptions)15 b(to)i(this)g(rule:)24 b Fm
(PyTuple_SetItem\(\))14 b Ft(and)i Fm(PyList_SetItem\(\))p
Ft(.)21 b(These)c(functions)e(tak)o(e)i(o)o(v)o(er)f(o)n(wnership)f(of)
0 1969 y(the)20 b(item)g(passed)f(to)h(them)g(\227)g(e)n(v)o(en)f(if)h
(the)o(y)f(f)o(ail!)25 b(\(Note)20 b(that)f Fm(PyDict_SetItem\(\))f
Ft(and)h(friends)g(don')o(t)f(tak)o(e)i(o)o(v)o(er)e(o)n(wnership)0
2069 y(\227)j(the)o(y)e(are)h(\223normal.)-6 b(\224\))0
2216 y(When)25 b(a)h(C)g(function)d(is)j(called)f(from)f(Python,)h(it)h
(borro)n(ws)e(references)f(to)j(its)g(ar)o(guments)d(from)h(the)h
(caller)-5 b(.)40 b(The)25 b(caller)g(o)n(wns)g(a)0 2315
y(reference)17 b(to)i(the)f(object,)g(so)h(the)g(borro)n(wed)d
(reference')-5 b(s)18 b(lifetime)g(is)i(guaranteed)c(until)j(the)f
(function)f(returns.)24 b(Only)18 b(when)g(such)g(a)0
2415 y(borro)n(wed)d(reference)h(must)i(be)f(stored)g(or)g(passed)h
(on,)f(it)i(must)e(be)h(turned)e(into)h(an)h(o)n(wned)e(reference)g(by)
h(calling)g Fm(Py_INCREF\(\))p Ft(.)0 2562 y(The)j(object)h(reference)e
(returned)g(from)g(a)i(C)h(function)d(that)i(is)g(called)g(from)f
(Python)f(must)i(be)g(an)f(o)n(wned)g(reference)f(\227)i(o)n(wnership)0
2662 y(is)g(transferred)d(from)i(the)g(function)e(to)j(its)g(caller)-5
b(.)0 2946 y Fg(1.10.3)101 b(Thin)28 b(Ice)0 3149 y Ft(There)17
b(are)i(a)f(fe)n(w)g(situations)h(where)e(seemingly)g(harmless)h(use)h
(of)f(a)g(borro)n(wed)e(reference)h(can)h(lead)g(to)g(problems.)23
b(These)18 b(all)h(ha)n(v)o(e)0 3249 y(to)h(do)g(with)g(implicit)h(in)m
(v)n(ocations)d(of)i(the)g(interpreter)m(,)e(which)i(can)g(cause)g(the)
g(o)n(wner)f(of)h(a)h(reference)d(to)j(dispose)f(of)g(it.)0
3396 y(The)k(\002rst)g(and)g(most)g(important)e(case)j(to)f(kno)n(w)f
(about)g(is)i(using)e Fm(Py_DECREF\(\))f Ft(on)i(an)g(unrelated)e
(object)i(while)g(borro)n(wing)d(a)0 3495 y(reference)e(to)h(a)g(list)i
(item.)j(F)o(or)20 b(instance:)236 3734 y Fj(void)236
3825 y(bug\(PyObject)819 3838 y(*)864 3825 y(list\))236
3916 y({)416 4008 y(PyObject)819 4021 y(*)864 4008 y(item)44
b(=)h(PyList_GetItem\(list,)c(0\);)416 4190 y(PyList_SetItem\(list,)g
(1,)k(PyInt_FromLong\(0L\)\);)416 4282 y(PyObject_Print\(item,)c
(stdout,)j(0\);)g(/)1940 4295 y(*)2029 4282 y(BUG!)2254
4295 y(*)2299 4282 y(/)236 4373 y(})0 4659 y Ft(This)23
b(function)d(\002rst)j(borro)n(ws)e(a)i(reference)d(to)j
Fm(list[0])p Ft(,)f(then)g(replaces)g Fm(list[1])f Ft(with)i(the)f(v)n
(alue)g Fm(0)p Ft(,)h(and)e(\002nally)i(prints)f(the)0
4759 y(borro)n(wed)c(reference.)23 b(Looks)c(harmless,)h(right?)k(But)d
(it')-5 b(s)21 b(not!)0 4906 y(Let')-5 b(s)29 b(follo)n(w)f(the)g
(control)g(\003o)n(w)g(into)g Fm(PyList_SetItem\(\))p
Ft(.)48 b(The)28 b(list)h(o)n(wns)g(references)e(to)h(all)h(its)h
(items,)h(so)e(when)e(item)0 5006 y(1)i(is)g(replaced,)g(it)g(has)g(to)
g(dispose)f(of)g(the)h(original)e(item)i(1.)50 b(No)n(w)29
b(let')-5 b(s)29 b(suppose)f(the)g(original)g(item)g(1)h(w)o(as)g(an)g
(instance)f(of)g(a)0 5105 y(user)n(-de\002ned)20 b(class,)j(and)f(let')
-5 b(s)23 b(further)d(suppose)h(that)h(the)g(class)h(de\002ned)e(a)h
Fm(__del__\(\))f Ft(method.)29 b(If)22 b(this)g(class)h(instance)f(has)
g(a)0 5205 y(reference)d(count)g(of)h(1,)g(disposing)f(of)h(it)h(will)f
(call)h(its)g Fm(__del__\(\))e Ft(method.)0 5352 y(Since)i(it)h(is)h
(written)e(in)g(Python,)f(the)i Fm(__del__\(\))e Ft(method)g(can)h(e)o
(x)o(ecute)f(arbitrary)g(Python)g(code.)27 b(Could)21
b(it)h(perhaps)e(do)h(some-)p 0 5549 3901 4 v 0 5649
a Fo(14)2164 b(Chapter)24 b(1.)52 b(Extending)24 b(Python)f(with)h(C)f
(or)g(C)3780 5641 y(++)p eop end
%%Page: 15 21
TeXDict begin 15 20 bop 0 83 a Ft(thing)19 b(to)h(in)m(v)n(alidate)f
(the)h(reference)e(to)i Fm(item)g Ft(in)g Fm(bug\(\))p
Ft(?)k(Y)-9 b(ou)20 b(bet!)k(Assuming)c(that)g(the)g(list)h(passed)e
(into)h Fm(bug\(\))g Ft(is)g(accessible)0 183 y(to)f(the)g
Fm(__del__\(\))f Ft(method,)f(it)j(could)d(e)o(x)o(ecute)h(a)h
(statement)g(to)g(the)g(ef)n(fect)f(of)h(`)p Fm(del)48
b(list[0])p Ft(',)18 b(and)g(assuming)h(this)g(w)o(as)h(the)0
282 y(last)h(reference)e(to)h(that)g(object,)g(it)h(w)o(ould)e(free)h
(the)g(memory)e(associated)i(with)h(it,)f(thereby)f(in)m(v)n(alidating)
f Fm(item)p Ft(.)0 429 y(The)29 b(solution,)h(once)e(you)g(kno)n(w)g
(the)h(source)f(of)h(the)g(problem,)g(is)h(easy:)43 b(temporarily)27
b(increment)g(the)i(reference)f(count.)50 b(The)0 529
y(correct)19 b(v)o(ersion)g(of)h(the)g(function)f(reads:)236
676 y Fj(void)236 767 y(no_bug\(PyObject)954 780 y(*)999
767 y(list\))236 858 y({)416 950 y(PyObject)819 963 y(*)864
950 y(item)44 b(=)h(PyList_GetItem\(list,)c(0\);)416
1132 y(Py_INCREF\(item\);)416 1224 y(PyList_SetItem\(list,)g(1,)k
(PyInt_FromLong\(0L\)\);)416 1315 y(PyObject_Print\(item,)c(stdout,)j
(0\);)416 1406 y(Py_DECREF\(item\);)236 1498 y(})0 1784
y Ft(This)29 b(is)h(a)g(true)f(story)-5 b(.)51 b(An)29
b(older)f(v)o(ersion)g(of)h(Python)f(contained)g(v)n(ariants)g(of)h
(this)h(b)n(ug)e(and)h(someone)f(spent)h(a)g(considerable)0
1884 y(amount)19 b(of)h(time)g(in)g(a)h(C)g(deb)n(ugger)d(to)j
(\002gure)e(out)h(why)f(his)i Fm(__del__\(\))e Ft(methods)g(w)o(ould)g
(f)o(ail...)0 2031 y(The)j(second)g(case)g(of)h(problems)e(with)h(a)h
(borro)n(wed)d(reference)h(is)i(a)g(v)n(ariant)f(in)m(v)n(olving)e
(threads.)31 b(Normally)-5 b(,)21 b(multiple)h(threads)f(in)0
2130 y(the)k(Python)f(interpreter)g(can')o(t)g(get)h(in)h(each)f
(other')-5 b(s)24 b(w)o(ay)-5 b(,)26 b(because)f(there)g(is)h(a)f
(global)g(lock)f(protecting)g(Python')-5 b(s)24 b(entire)h(object)0
2230 y(space.)32 b(Ho)n(we)n(v)o(er)m(,)21 b(it)i(is)h(possible)e(to)h
(temporarily)d(release)j(this)g(lock)f(using)g(the)h(macro)e
Fm(Py_BEGIN_ALLOW_THREADS)p Ft(,)e(and)0 2329 y(to)26
b(re-acquire)e(it)j(using)e Fm(Py_END_ALLOW_THREADS)p
Ft(.)e(This)j(is)h(common)d(around)g(blocking)g(I/O)i(calls,)i(to)f
(let)f(other)f(threads)0 2429 y(use)f(the)g(processor)e(while)i(w)o
(aiting)g(for)f(the)g(I/O)h(to)g(complete.)35 b(Ob)o(viously)-5
b(,)22 b(the)i(follo)n(wing)e(function)g(has)i(the)g(same)g(problem)e
(as)0 2529 y(the)e(pre)n(vious)f(one:)236 2676 y Fj(void)236
2767 y(bug\(PyObject)819 2780 y(*)864 2767 y(list\))236
2858 y({)416 2950 y(PyObject)819 2963 y(*)864 2950 y(item)44
b(=)h(PyList_GetItem\(list,)c(0\);)416 3041 y(Py_BEGIN_ALLOW_THREADS)
416 3132 y(...some)i(blocking)h(I/O)g(call...)416 3224
y(Py_END_ALLOW_THREADS)416 3315 y(PyObject_Print\(item,)d(stdout,)j
(0\);)g(/)1940 3328 y(*)2029 3315 y(BUG!)2254 3328 y(*)2299
3315 y(/)236 3406 y(})0 3829 y Fg(1.10.4)101 b(NULL)28
b(P)-5 b(ointers)0 4032 y Ft(In)20 b(general,)f(functions)g(that)i(tak)
o(e)f(object)g(references)f(as)i(ar)o(guments)d(do)i(not)g(e)o(xpect)g
(you)f(to)i(pass)g(them)f Fm(NULL)g Ft(pointers,)f(and)h(will)0
4132 y(dump)c(core)h(\(or)f(cause)i(later)f(core)g(dumps\))f(if)h(you)g
(do)g(so.)24 b(Functions)16 b(that)i(return)e(object)g(references)g
(generally)g(return)g Fm(NULL)h Ft(only)0 4232 y(to)k(indicate)g(that)h
(an)f(e)o(xception)e(occurred.)26 b(The)21 b(reason)g(for)g(not)g
(testing)g(for)f Fm(NULL)i Ft(ar)o(guments)d(is)j(that)f(functions)f
(often)h(pass)h(the)0 4331 y(objects)g(the)o(y)f(recei)n(v)o(e)g(on)g
(to)h(other)f(function)g(\227)h(if)g(each)g(function)e(were)i(to)g
(test)h(for)e Fm(NULL)p Ft(,)g(there)h(w)o(ould)f(be)h(a)g(lot)g(of)g
(redundant)0 4431 y(tests)f(and)f(the)g(code)g(w)o(ould)f(run)h(more)f
(slo)n(wly)-5 b(.)0 4578 y(It)26 b(is)h(better)f(to)g(test)h(for)e
Fm(NULL)h Ft(only)f(at)h(the)g(\223source:\224)36 b(when)25
b(a)h(pointer)f(that)h(may)g(be)f Fm(NULL)h Ft(is)h(recei)n(v)o(ed,)f
(for)f(e)o(xample,)h(from)0 4677 y Fm(malloc\(\))19 b
Ft(or)h(from)f(a)i(function)d(that)j(may)e(raise)i(an)f(e)o(xception.)0
4824 y(The)k(macros)g Fm(Py_INCREF\(\))f Ft(and)h Fm(Py_DECREF\(\))f
Ft(do)h(not)g(check)f(for)h Fm(NULL)g Ft(pointers)g(\227)h(ho)n(we)n(v)
o(er)m(,)e(their)h(v)n(ariants)g Fm(Py_-)0 4924 y(XINCREF\(\))19
b Ft(and)h Fm(Py_XDECREF\(\))e Ft(do.)0 5071 y(The)26
b(macros)g(for)g(checking)e(for)i(a)h(particular)e(object)h(type)g(\()p
Fm(Py)p Fq(type)p Fm(_Check\(\))p Ft(\))d(don')o(t)i(check)g(for)h
Fm(NULL)g Ft(pointers)g(\227)h(again,)0 5170 y(there)d(is)i(much)d
(code)h(that)h(calls)g(se)n(v)o(eral)f(of)g(these)h(in)g(a)g(ro)n(w)f
(to)h(test)g(an)g(object)f(against)g(v)n(arious)f(dif)n(ferent)g(e)o
(xpected)g(types,)i(and)0 5270 y(this)c(w)o(ould)e(generate)g
(redundant)f(tests.)26 b(There)19 b(are)h(no)g(v)n(ariants)g(with)g
Fm(NULL)g Ft(checking.)p 0 5549 3901 4 v 0 5649 a Fo(1.10.)52
b(Ref)n(erence)23 b(Counts)2905 b(15)p eop end
%%Page: 16 22
TeXDict begin 16 21 bop 0 83 a Ft(The)21 b(C)i(function)d(calling)h
(mechanism)g(guarantees)f(that)i(the)g(ar)o(gument)d(list)k(passed)f
(to)g(C)g(functions)e(\()p Fm(args)i Ft(in)g(the)f(e)o(xamples\))g(is)0
183 y(ne)n(v)o(er)e Fm(NULL)h Ft(\227)h(in)f(f)o(act)g(it)h(guarantees)
e(that)h(it)h(is)g(al)o(w)o(ays)g(a)g(tuple.)1982 153
y Fk(4)0 330 y Ft(It)f(is)i(a)e(se)n(v)o(ere)g(error)f(to)h(e)n(v)o(er)
f(let)i(a)g Fm(NULL)f Ft(pointer)f(\223escape\224)h(to)g(the)g(Python)f
(user)-5 b(.)0 657 y Fl(1.11)121 b(Wr)r(iting)33 b(Extensions)h(in)f(C)
1589 646 y(++)0 890 y Ft(It)19 b(is)g(possible)f(to)h(write)f(e)o
(xtension)f(modules)h(in)g(C)1483 883 y(++)1577 890 y(.)25
b(Some)18 b(restrictions)g(apply)-5 b(.)23 b(If)18 b(the)h(main)f
(program)e(\(the)i(Python)g(interpreter\))0 989 y(is)29
b(compiled)e(and)g(link)o(ed)h(by)g(the)g(C)h(compiler)m(,)f(global)f
(or)h(static)h(objects)f(with)h(constructors)d(cannot)h(be)h(used.)49
b(This)28 b(is)h(not)f(a)0 1089 y(problem)23 b(if)i(the)g(main)f
(program)e(is)k(link)o(ed)e(by)g(the)h(C)1605 1082 y(++)1724
1089 y(compiler)-5 b(.)38 b(Functions)23 b(that)i(will)h(be)e(called)h
(by)f(the)h(Python)e(interpreter)0 1189 y(\(in)g(particular)m(,)f
(module)f(initialization)i(functions\))e(ha)n(v)o(e)h(to)h(be)g
(declared)f(using)g Fm(extern)49 b("C")p Ft(.)33 b(It)24
b(is)g(unnecessary)d(to)i(enclose)0 1288 y(the)j(Python)f(header)g
(\002les)i(in)f Fm(extern)49 b("C")g({...})26 b Ft(\227)g(the)o(y)g
(use)g(this)g(form)f(already)g(if)i(the)f(symbol)f(`)p
Fm(__cplusplus)p Ft(')f(is)0 1388 y(de\002ned)19 b(\(all)i(recent)e(C)
675 1381 y(++)790 1388 y(compilers)g(de\002ne)h(this)h(symbol\).)0
1715 y Fl(1.12)121 b(Pro)n(viding)34 b(a)g(C)f(API)g(f)l(or)h(an)g
(Extension)h(Module)0 1948 y Ft(Man)o(y)26 b(e)o(xtension)g(modules)g
(just)i(pro)o(vide)d(ne)n(w)i(functions)f(and)g(types)h(to)h(be)f(used)
g(from)f(Python,)i(b)n(ut)f(sometimes)g(the)g(code)f(in)0
2048 y(an)c(e)o(xtension)f(module)g(can)h(be)g(useful)f(for)h(other)f
(e)o(xtension)g(modules.)30 b(F)o(or)22 b(e)o(xample,)f(an)h(e)o
(xtension)f(module)g(could)g(implement)0 2147 y(a)27
b(type)f(\223collection\224)f(which)h(w)o(orks)g(lik)o(e)g(lists)i
(without)e(order)-5 b(.)43 b(Just)27 b(lik)o(e)f(the)h(standard)e
(Python)g(list)j(type)e(has)g(a)h(C)g(API)g(which)0 2247
y(permits)19 b(e)o(xtension)g(modules)f(to)i(create)f(and)h(manipulate)
e(lists,)j(this)f(ne)n(w)g(collection)e(type)h(should)g(ha)n(v)o(e)g(a)
h(set)h(of)e(C)i(functions)d(for)0 2347 y(direct)i(manipulation)e(from)
h(other)g(e)o(xtension)g(modules.)0 2494 y(At)i(\002rst)f(sight)g(this)
h(seems)f(easy:)25 b(just)c(write)f(the)g(functions)e(\(without)h
(declaring)g(them)g Fm(static)p Ft(,)g(of)h(course\),)f(pro)o(vide)e
(an)j(appro-)0 2593 y(priate)h(header)g(\002le,)i(and)e(document)f(the)
h(C)i(API.)f(And)f(in)h(f)o(act)g(this)h(w)o(ould)e(w)o(ork)g(if)h(all)
g(e)o(xtension)f(modules)f(were)i(al)o(w)o(ays)g(link)o(ed)0
2693 y(statically)d(with)g(the)g(Python)f(interpreter)-5
b(.)23 b(When)c(modules)f(are)h(used)f(as)i(shared)e(libraries,)g(ho)n
(we)n(v)o(er)m(,)f(the)i(symbols)f(de\002ned)g(in)h(one)0
2792 y(module)g(may)h(not)f(be)i(visible)f(to)g(another)f(module.)k
(The)d(details)h(of)f(visibility)g(depend)e(on)i(the)g(operating)f
(system;)h(some)g(systems)0 2892 y(use)g(one)f(global)g(namespace)g
(for)g(the)g(Python)g(interpreter)f(and)h(all)i(e)o(xtension)d(modules)
g(\(W)m(indo)n(ws,)h(for)g(e)o(xample\),)f(whereas)h(oth-)0
2992 y(ers)g(require)e(an)h(e)o(xplicit)f(list)j(of)e(imported)e
(symbols)i(at)g(module)f(link)h(time)h(\(AIX)f(is)h(one)f(e)o
(xample\),)e(or)i(of)n(fer)f(a)i(choice)e(of)h(dif)n(ferent)0
3091 y(strate)o(gies)26 b(\(most)g(Unices\).)44 b(And)26
b(e)n(v)o(en)f(if)i(symbols)f(are)g(globally)f(visible,)j(the)e(module)
g(whose)g(functions)f(one)h(wishes)h(to)f(call)0 3191
y(might)20 b(not)f(ha)n(v)o(e)h(been)f(loaded)h(yet!)0
3338 y(Portability)26 b(therefore)f(requires)h(not)g(to)h(mak)o(e)f(an)
o(y)g(assumptions)g(about)g(symbol)g(visibility)-5 b(.)44
b(This)27 b(means)f(that)h(all)g(symbols)f(in)0 3437
y(e)o(xtension)17 b(modules)g(should)g(be)h(declared)f
Fm(static)p Ft(,)h(e)o(xcept)f(for)g(the)i(module')-5
b(s)17 b(initialization)g(function,)g(in)h(order)f(to)h(a)n(v)n(oid)g
(name)0 3537 y(clashes)f(with)f(other)g(e)o(xtension)f(modules)g(\(as)i
(discussed)f(in)h(section)f(1.4\).)23 b(And)16 b(it)h(means)f(that)g
(symbols)g(that)h Fq(should)e Ft(be)h(accessible)0 3637
y(from)j(other)h(e)o(xtension)e(modules)h(must)i(be)f(e)o(xported)e(in)
i(a)h(dif)n(ferent)d(w)o(ay)-5 b(.)0 3784 y(Python)26
b(pro)o(vides)f(a)i(special)g(mechanism)e(to)i(pass)g(C-le)n(v)o(el)g
(information)d(\(pointers\))h(from)h(one)g(e)o(xtension)g(module)f(to)i
(another)0 3883 y(one:)39 b(CObjects.)46 b(A)28 b(CObject)f(is)h(a)g
(Python)e(data)h(type)f(which)h(stores)g(a)h(pointer)e(\()p
Fm(void)2754 3898 y(*)2804 3883 y Ft(\).)46 b(CObjects)28
b(can)f(only)f(be)h(created)0 3983 y(and)h(accessed)g(via)g(their)g(C)i
(API,)e(b)n(ut)g(the)o(y)g(can)g(be)g(passed)h(around)d(lik)o(e)j(an)o
(y)e(other)h(Python)f(object.)49 b(In)28 b(particular)m(,)g(the)o(y)g
(can)0 4082 y(be)d(assigned)f(to)h(a)h(name)e(in)h(an)g(e)o(xtension)f
(module')-5 b(s)24 b(namespace.)38 b(Other)24 b(e)o(xtension)g(modules)
g(can)g(then)h(import)f(this)h(module,)0 4182 y(retrie)n(v)o(e)19
b(the)h(v)n(alue)g(of)g(this)g(name,)g(and)f(then)h(retrie)n(v)o(e)f
(the)h(pointer)f(from)g(the)i(CObject.)0 4329 y(There)16
b(are)h(man)o(y)f(w)o(ays)i(in)f(which)g(CObjects)h(can)f(be)g(used)g
(to)g(e)o(xport)f(the)h(C)h(API)g(of)f(an)g(e)o(xtension)e(module.)23
b(Each)17 b(name)f(could)h(get)0 4429 y(its)23 b(o)n(wn)f(CObject,)h
(or)f(all)h(C)g(API)g(pointers)e(could)g(be)i(stored)e(in)i(an)f(array)
f(whose)h(address)g(is)h(published)e(in)i(a)f(CObject.)32
b(And)22 b(the)0 4528 y(v)n(arious)e(tasks)i(of)f(storing)f(and)g
(retrie)n(ving)g(the)h(pointers)f(can)h(be)g(distrib)n(uted)f(in)h(dif)
n(ferent)f(w)o(ays)h(between)g(the)g(module)e(pro)o(viding)0
4628 y(the)h(code)g(and)f(the)i(client)f(modules.)0 4775
y(The)h(follo)n(wing)e(e)o(xample)g(demonstrates)h(an)g(approach)f
(that)i(puts)g(most)g(of)f(the)h(b)n(urden)e(on)i(the)g(writer)f(of)h
(the)g(e)o(xporting)d(module,)0 4874 y(which)i(is)i(appropriate)d(for)h
(commonly)e(used)j(library)f(modules.)25 b(It)c(stores)g(all)h(C)g(API)
f(pointers)f(\(just)h(one)f(in)h(the)g(e)o(xample!\))k(in)c(an)0
4974 y(array)f(of)g Fm(void)g Ft(pointers)g(which)g(becomes)g(the)g(v)n
(alue)g(of)g(a)h(CObject.)27 b(The)20 b(header)f(\002le)j
(corresponding)17 b(to)k(the)f(module)f(pro)o(vides)0
5074 y(a)g(macro)f(that)h(tak)o(es)g(care)f(of)h(importing)e(the)h
(module)g(and)g(retrie)n(ving)f(its)j(C)f(API)g(pointers;)g(client)f
(modules)g(only)g(ha)n(v)o(e)g(to)h(call)g(this)0 5173
y(macro)g(before)g(accessing)h(the)g(C)h(API.)p 0 5244
1560 4 v 90 5300 a Fi(4)120 5323 y Fp(These)c(guarantees)i(don')o(t)f
(hold)g(when)f(you)g(use)h(the)f(\223old\224)i(style)f(calling)h(con)m
(v)o(ention)h(\227)d(this)h(is)f(still)h(found)g(in)f(much)g(e)o
(xisting)i(code.)p 0 5549 3901 4 v 0 5649 a Fo(16)2164
b(Chapter)24 b(1.)52 b(Extending)24 b(Python)f(with)h(C)f(or)g(C)3780
5641 y(++)p eop end
%%Page: 17 23
TeXDict begin 17 22 bop 0 83 a Ft(The)20 b(e)o(xporting)f(module)g(is)j
(a)f(modi\002cation)e(of)i(the)f Fm(spam)h Ft(module)e(from)h(section)h
(1.1.)k(The)c(function)e Fm(spam.system\(\))g Ft(does)0
183 y(not)30 b(call)h(the)f(C)i(library)d(function)g
Fm(system\(\))g Ft(directly)-5 b(,)32 b(b)n(ut)e(a)h(function)e
Fm(PySpam_System\(\))p Ft(,)i(which)e(w)o(ould)h(of)g(course)0
282 y(do)e(something)e(more)h(complicated)g(in)h(reality)g(\(such)f(as)
i(adding)d(\223spam\224)i(to)g(e)n(v)o(ery)f(command\).)46
b(This)28 b(function)e Fm(PySpam_-)0 382 y(System\(\))19
b Ft(is)i(also)g(e)o(xported)d(to)i(other)g(e)o(xtension)e(modules.)0
529 y(The)i(function)e Fm(PySpam_System\(\))g Ft(is)j(a)g(plain)f(C)h
(function,)d(declared)h Fm(static)h Ft(lik)o(e)g(e)n(v)o(erything)e
(else:)236 767 y Fj(static)44 b(int)236 858 y(PySpam_System\(const)e
(char)1357 871 y(*)1402 858 y(command\))236 950 y({)416
1041 y(return)h(system\(command\);)236 1132 y(})0 1419
y Ft(The)20 b(function)e Fm(spam_system\(\))h Ft(is)i(modi\002ed)e(in)h
(a)h(tri)n(vial)f(w)o(ay:)236 1657 y Fj(static)44 b(PyObject)954
1670 y(*)236 1748 y(spam_system\(PyObject)1178 1761 y(*)1223
1748 y(self,)g(PyObject)1895 1761 y(*)1940 1748 y(args\))236
1840 y({)416 1931 y(const)g(char)909 1944 y(*)954 1931
y(command;)416 2022 y(int)g(sts;)416 2205 y(if)g
(\(!PyArg_ParseTuple\(args,)d("s",)j(&command\)\))595
2296 y(return)g(NULL;)416 2388 y(sts)g(=)h(PySpam_System\(command\);)
416 2479 y(return)e(Py_BuildValue\("i",)f(sts\);)236
2570 y(})0 2857 y Ft(In)20 b(the)g(be)o(ginning)e(of)i(the)g(module,)e
(right)i(after)g(the)g(line)236 3095 y Fj(#include)44
b("Python.h")0 3381 y Ft(tw)o(o)20 b(more)g(lines)g(must)h(be)f(added:)
236 3620 y Fj(#define)44 b(SPAM_MODULE)236 3711 y(#include)g
("spammodule.h")0 3997 y Ft(The)26 b Fm(#define)g Ft(is)h(used)f(to)h
(tell)g(the)g(header)e(\002le)i(that)f(it)i(is)f(being)e(included)g(in)
i(the)f(e)o(xporting)e(module,)j(not)f(a)h(client)f(module.)0
4097 y(Finally)-5 b(,)19 b(the)i(module')-5 b(s)19 b(initialization)g
(function)g(must)h(tak)o(e)g(care)g(of)g(initializing)g(the)g(C)h(API)g
(pointer)d(array:)p 0 5549 3901 4 v 0 5649 a Fo(1.12.)52
b(Pro)o(viding)24 b(a)f(C)g(API)h(f)n(or)f(an)g(Extension)h(Module)2003
b(17)p eop end
%%Page: 18 24
TeXDict begin 18 23 bop 236 174 a Fj(PyMODINIT_FUNC)236
266 y(initspam\(void\))236 357 y({)416 448 y(PyObject)819
461 y(*)864 448 y(m;)416 540 y(static)43 b(void)954 553
y(*)999 540 y(PySpam_API[PySpam_API_pointers];)416 631
y(PyObject)819 644 y(*)864 631 y(c_api_object;)416 814
y(m)h(=)h(Py_InitModule\("spam",)c(SpamMethods\);)416
905 y(if)j(\(m)h(==)f(NULL\))595 996 y(return;)416 1179
y(/)461 1192 y(*)550 1179 y(Initialize)f(the)i(C)f(API)h(pointer)e
(array)2119 1192 y(*)2164 1179 y(/)416 1270 y
(PySpam_API[PySpam_System_NUM])d(=)k(\(void)2119 1283
y(*)2164 1270 y(\)PySpam_System;)416 1453 y(/)461 1466
y(*)550 1453 y(Create)g(a)h(CObject)e(containing)g(the)i(API)f(pointer)
g(array's)f(address)3240 1466 y(*)3285 1453 y(/)416 1544
y(c_api_object)f(=)j(PyCObject_FromVoidPtr\(\(void)2343
1557 y(*)2388 1544 y(\)PySpam_API,)e(NULL\);)416 1727
y(if)h(\(c_api_object)f(!=)h(NULL\))595 1818 y(PyModule_AddObject\(m,)d
("_C_API",)i(c_api_object\);)236 1910 y(})0 2196 y Ft(Note)32
b(that)f Fm(PySpam_API)g Ft(is)h(declared)f Fm(static)p
Ft(;)37 b(otherwise)31 b(the)g(pointer)g(array)g(w)o(ould)f(disappear)h
(when)g Fm(initspam\(\))0 2296 y Ft(terminates!)0 2443
y(The)20 b(b)n(ulk)g(of)f(the)i(w)o(ork)e(is)i(in)g(the)f(header)f
(\002le)i(`)p Fu(spammodule)o(.h)p Ft(',)16 b(which)k(looks)g(lik)o(e)g
(this:)p 0 5549 3901 4 v 0 5649 a Fo(18)2164 b(Chapter)24
b(1.)52 b(Extending)24 b(Python)f(with)h(C)f(or)g(C)3780
5641 y(++)p eop end
%%Page: 19 25
TeXDict begin 19 24 bop 236 174 a Fj(#ifndef)44 b(Py_SPAMMODULE_H)236
266 y(#define)g(Py_SPAMMODULE_H)236 357 y(#ifdef)g(__cplusplus)236
448 y(extern)g("C")g({)236 540 y(#endif)236 722 y(/)281
735 y(*)371 722 y(Header)g(file)g(for)g(spammodule)1581
735 y(*)1626 722 y(/)236 905 y(/)281 918 y(*)371 905
y(C)g(API)h(functions)1088 918 y(*)1133 905 y(/)236 996
y(#define)f(PySpam_System_NUM)e(0)236 1088 y(#define)i
(PySpam_System_RETURN)d(int)236 1179 y(#define)j(PySpam_System_PROTO)e
(\(const)h(char)2029 1192 y(*)2074 1179 y(command\))236
1362 y(/)281 1375 y(*)371 1362 y(Total)h(number)g(of)g(C)h(API)f
(pointers)1761 1375 y(*)1806 1362 y(/)236 1453 y(#define)g
(PySpam_API_pointers)e(1)236 1727 y(#ifdef)i(SPAM_MODULE)236
1818 y(/)281 1831 y(*)371 1818 y(This)g(section)g(is)g(used)g(when)g
(compiling)g(spammodule.c)2567 1831 y(*)2612 1818 y(/)236
2001 y(static)g(PySpam_System_RETURN)e(PySpam_System)g
(PySpam_System_PROTO;)236 2183 y(#else)236 2275 y(/)281
2288 y(*)371 2275 y(This)i(section)g(is)g(used)g(in)h(modules)e(that)h
(use)h(spammodule's)e(API)2971 2288 y(*)3016 2275 y(/)236
2457 y(static)h(void)774 2470 y(**)864 2457 y(PySpam_API;)236
2640 y(#define)g(PySpam_System)f(\\)281 2731 y(\()326
2744 y(*)371 2731 y(\(PySpam_System_RETURN)e(\()1402
2744 y(*)1447 2731 y(\)PySpam_System_PROTO\))g
(PySpam_API[PySpam_System_NUM]\))236 2914 y(/)281 2927
y(*)371 2914 y(Return)j(-1)g(and)g(set)h(exception)e(on)i(error,)e(0)i
(on)f(success.)2702 2927 y(*)2747 2914 y(/)236 3005 y(static)g(int)236
3097 y(import_spam\(void\))236 3188 y({)416 3279 y(PyObject)819
3292 y(*)864 3279 y(module)g(=)g(PyImport_ImportModule\("spam"\);)416
3462 y(if)g(\(module)g(!=)g(NULL\))g({)595 3553 y(PyObject)998
3566 y(*)1043 3553 y(c_api_object)f(=)i
(PyObject_GetAttrString\(module,)39 b("_C_API"\);)595
3645 y(if)44 b(\(c_api_object)f(==)i(NULL\))774 3736
y(return)f(-1;)595 3827 y(if)g(\(PyCObject_Check\(c_api_object\)\))774
3919 y(PySpam_API)f(=)i(\(void)1626 3932 y(**)1716 3919
y(\)PyCObject_AsVoidPtr\(c_api_object\);)595 4010 y
(Py_DECREF\(c_api_object\);)416 4101 y(})416 4193 y(return)e(0;)236
4284 y(})236 4467 y(#endif)236 4649 y(#ifdef)h(__cplusplus)236
4741 y(})236 4832 y(#endif)236 5015 y(#endif)g(/)595
5028 y(*)685 5015 y(!defined\(Py_SPAMMODULE_H\))1850
5028 y(*)1895 5015 y(/)0 5301 y Ft(All)24 b(that)f(a)h(client)f(module)
f(must)h(do)g(in)h(order)e(to)h(ha)n(v)o(e)g(access)h(to)f(the)g
(function)f Fm(PySpam_System\(\))f Ft(is)j(to)f(call)h(the)f(function)p
0 5549 3901 4 v 0 5649 a Fo(1.12.)52 b(Pro)o(viding)24
b(a)f(C)g(API)h(f)n(or)f(an)g(Extension)h(Module)2003
b(19)p eop end
%%Page: 20 26
TeXDict begin 20 25 bop 0 83 a Ft(\(or)20 b(rather)f(macro\))g
Fm(import_spam\(\))f Ft(in)i(its)i(initialization)d(function:)236
321 y Fj(PyMODINIT_FUNC)236 413 y(initclient\(void\))236
504 y({)416 595 y(PyObject)819 608 y(*)864 595 y(m;)416
778 y(m)44 b(=)h(Py_InitModule\("client",)c(ClientMethods\);)416
869 y(if)j(\(m)h(==)f(NULL\))595 960 y(return;)416 1052
y(if)g(\(import_spam\(\))f(<)h(0\))595 1143 y(return;)416
1234 y(/)461 1247 y(*)550 1234 y(additional)f(initialization)g(can)h
(happen)g(here)2433 1247 y(*)2478 1234 y(/)236 1326 y(})0
1612 y Ft(The)28 b(main)g(disadv)n(antage)e(of)i(this)h(approach)d(is)j
(that)g(the)f(\002le)h(`)p Fu(spammodule)o(.h)p Ft(')c(is)k(rather)e
(complicated.)48 b(Ho)n(we)n(v)o(er)m(,)28 b(the)g(basic)0
1712 y(structure)19 b(is)i(the)g(same)f(for)g(each)f(function)g(that)h
(is)h(e)o(xported,)d(so)j(it)f(has)h(to)f(be)g(learned)f(only)h(once.)0
1859 y(Finally)34 b(it)g(should)f(be)h(mentioned)e(that)i(CObjects)g
(of)n(fer)f(additional)g(functionality)-5 b(,)34 b(which)f(is)i
(especially)f(useful)f(for)g(mem-)0 1958 y(ory)28 b(allocation)g(and)h
(deallocation)e(of)i(the)g(pointer)e(stored)i(in)g(a)g(CObject.)52
b(The)29 b(details)g(are)g(described)f(in)h(the)g Fq(Python/C)f(API)0
2058 y(Refer)m(ence)i(Manual)g Ft(in)h(the)g(section)g
(\223CObjects\224)g(and)g(in)g(the)g(implementation)e(of)h(CObjects)i
(\(\002les)g(`)p Fu(Include/cobject.h)p Ft(')26 b(and)0
2158 y(`)p Fu(Objects/cobject.c)p Ft(')18 b(in)i(the)h(Python)e(source)
g(code)g(distrib)n(ution\).)p 0 5549 3901 4 v 0 5649
a Fo(20)2164 b(Chapter)24 b(1.)52 b(Extending)24 b(Python)f(with)h(C)f
(or)g(C)3780 5641 y(++)p eop end
%%Page: 21 27
TeXDict begin 21 26 bop 0 83 3901 9 v 3503 230 a Fo(CHAPTER)3623
427 y Fn(TW)n(O)p 0 515 V 2023 978 a Fz(De\002ning)58
b(Ne)l(w)f(T)-25 b(ypes)0 1468 y Ft(As)28 b(mentioned)d(in)i(the)g
(last)h(chapter)m(,)f(Python)f(allo)n(ws)h(the)g(writer)g(of)g(an)g(e)o
(xtension)f(module)f(to)i(de\002ne)g(ne)n(w)g(types)g(that)g(can)g(be)0
1568 y(manipulated)18 b(from)h(Python)g(code,)h(much)f(lik)o(e)h
(strings)h(and)e(lists)j(in)e(core)g(Python.)0 1714 y(This)c(is)g(not)f
(hard;)i(the)e(code)g(for)g(all)h(e)o(xtension)e(types)h(follo)n(ws)g
(a)h(pattern,)g(b)n(ut)f(there)g(are)h(some)f(details)h(that)g(you)e
(need)h(to)h(understand)0 1814 y(before)j(you)g(can)h(get)g(started.)0
1961 y Fr(Note:)30 b Ft(The)22 b(w)o(ay)h(ne)n(w)g(types)g(are)f
(de\002ned)g(changed)f(dramatically)h(\(and)g(for)g(the)h(better\))f
(in)h(Python)f(2.2.)32 b(This)23 b(document)e(docu-)0
2061 y(ments)e(ho)n(w)f(to)h(de\002ne)f(ne)n(w)g(types)h(for)f(Python)g
(2.2)g(and)g(later)-5 b(.)25 b(If)18 b(you)g(need)g(to)h(support)e
(older)h(v)o(ersions)g(of)g(Python,)g(you)g(will)h(need)0
2160 y(to)h(refer)g(to)g(older)f(v)o(ersions)h(of)g(this)g
(documentation.)0 2488 y Fl(2.1)121 b(The)34 b(Basics)0
2720 y Ft(The)18 b(Python)f(runtime)g(sees)i(all)f(Python)f(objects)h
(as)h(v)n(ariables)e(of)h(type)g Fm(PyObject)2499 2735
y(*)2549 2720 y Ft(.)23 b(A)18 b Fm(PyObject)g Ft(is)h(not)e(a)i(v)o
(ery)e(magni\002cent)0 2820 y(object)24 b(-)g(it)h(just)g(contains)e
(the)h(refcount)f(and)g(a)i(pointer)e(to)h(the)g(object')-5
b(s)24 b(\223type)g(object\224.)36 b(This)24 b(is)h(where)f(the)g
(action)g(is;)j(the)d(type)0 2920 y(object)j(determines)g(which)h
(\(C\))g(functions)e(get)i(called)g(when,)h(for)e(instance,)j(an)d
(attrib)n(ute)h(gets)g(look)o(ed)f(up)g(on)h(an)g(object)f(or)h(it)0
3019 y(is)h(multiplied)e(by)g(another)g(object.)47 b(These)28
b(C)h(functions)d(are)i(called)g(\223type)f(methods\224)g(to)h
(distinguish)f(them)g(from)g(things)g(lik)o(e)0 3119
y Fm([].append)19 b Ft(\(which)g(we)i(call)g(\223object)e
(methods\224\).)0 3266 y(So,)h(if)h(you)e(w)o(ant)h(to)h(de\002ne)e(a)i
(ne)n(w)f(object)g(type,)f(you)g(need)h(to)g(create)g(a)h(ne)n(w)f
(type)f(object.)0 3413 y(This)26 b(sort)f(of)g(thing)g(can)g(only)g(be)
g(e)o(xplained)f(by)h(e)o(xample,)g(so)g(here')-5 b(s)26
b(a)g(minimal,)f(b)n(ut)h(complete,)f(module)f(that)i(de\002nes)f(a)h
(ne)n(w)0 3512 y(type:)236 3701 y Fj(#include)44 b(<Python.h>)236
3883 y(typedef)g(struct)g({)416 3975 y(PyObject_HEAD)416
4066 y(/)461 4079 y(*)550 4066 y(Type-specific)f(fields)h(go)g(here.)
1895 4079 y(*)1940 4066 y(/)236 4157 y(})h(noddy_NoddyObject;)236
4340 y(static)f(PyTypeObject)f(noddy_NoddyType)f(=)j({)416
4431 y(PyObject_HEAD_INIT\(NULL\))416 4523 y(0,)1120
b(/)1671 4536 y(*)1716 4523 y(ob_size)2031 4536 y(*)2076
4523 y(/)416 4614 y("noddy.Noddy",)580 b(/)1671 4627
y(*)1716 4614 y(tp_name)2031 4627 y(*)2076 4614 y(/)416
4705 y(sizeof\(noddy_NoddyObject\),)40 b(/)1671 4718
y(*)1716 4705 y(tp_basicsize)2256 4718 y(*)2301 4705
y(/)416 4797 y(0,)1120 b(/)1671 4810 y(*)1716 4797 y(tp_itemsize)2211
4810 y(*)2256 4797 y(/)416 4888 y(0,)g(/)1671 4901 y(*)1716
4888 y(tp_dealloc)2166 4901 y(*)2211 4888 y(/)416 4979
y(0,)g(/)1671 4992 y(*)1716 4979 y(tp_print)2076 4992
y(*)2121 4979 y(/)416 5071 y(0,)g(/)1671 5084 y(*)1716
5071 y(tp_getattr)2166 5084 y(*)2211 5071 y(/)416 5162
y(0,)g(/)1671 5175 y(*)1716 5162 y(tp_setattr)2166 5175
y(*)2211 5162 y(/)416 5253 y(0,)g(/)1671 5266 y(*)1716
5253 y(tp_compare)2166 5266 y(*)2211 5253 y(/)416 5344
y(0,)g(/)1671 5357 y(*)1716 5344 y(tp_repr)2031 5357
y(*)2076 5344 y(/)p 0 5549 3901 4 v 3808 5649 a Fo(21)p
eop end
%%Page: 22 28
TeXDict begin 22 27 bop 416 83 a Fj(0,)1120 b(/)1671
96 y(*)1716 83 y(tp_as_number)2256 96 y(*)2301 83 y(/)416
174 y(0,)g(/)1671 187 y(*)1716 174 y(tp_as_sequence)2346
187 y(*)2391 174 y(/)416 266 y(0,)g(/)1671 279 y(*)1716
266 y(tp_as_mapping)2301 279 y(*)2346 266 y(/)416 357
y(0,)g(/)1671 370 y(*)1716 357 y(tp_hash)2074 370 y(*)2119
357 y(/)416 448 y(0,)g(/)1671 461 y(*)1716 448 y(tp_call)2031
461 y(*)2076 448 y(/)416 540 y(0,)g(/)1671 553 y(*)1716
540 y(tp_str)1986 553 y(*)2031 540 y(/)416 631 y(0,)g(/)1671
644 y(*)1716 631 y(tp_getattro)2211 644 y(*)2256 631
y(/)416 722 y(0,)g(/)1671 735 y(*)1716 722 y(tp_setattro)2211
735 y(*)2256 722 y(/)416 814 y(0,)g(/)1671 827 y(*)1716
814 y(tp_as_buffer)2256 827 y(*)2301 814 y(/)416 905
y(Py_TPFLAGS_DEFAULT,)355 b(/)1671 918 y(*)1716 905 y(tp_flags)2076
918 y(*)2121 905 y(/)416 996 y("Noddy)43 b(objects",)492
b(/)1671 1009 y(*)1761 996 y(tp_doc)2074 1009 y(*)2119
996 y(/)236 1088 y(};)236 1270 y(static)44 b(PyMethodDef)f
(noddy_methods[])f(=)j({)416 1362 y({NULL})88 b(/)819
1375 y(*)909 1362 y(Sentinel)1312 1375 y(*)1357 1362
y(/)236 1453 y(};)236 1636 y(#ifndef)44 b(PyMODINIT_FUNC)e(/)1312
1649 y(*)1402 1636 y(declarations)h(for)h(DLL)g(import/export)2971
1649 y(*)3016 1636 y(/)236 1727 y(#define)g(PyMODINIT_FUNC)e(void)236
1818 y(#endif)236 1910 y(PyMODINIT_FUNC)236 2001 y(initnoddy\(void\))
236 2092 y({)416 2183 y(PyObject)776 2196 y(*)864 2183
y(m;)416 2366 y(noddy_NoddyType.tp_new)f(=)j(PyType_GenericNew;)416
2457 y(if)g(\(PyType_Ready\(&noddy_NoddyType\))c(<)k(0\))595
2549 y(return;)416 2731 y(m)g(=)h(Py_InitModule3\("noddy",)c
(noddy_methods,)1267 2823 y("Example)j(module)g(that)g(creates)f(an)i
(extension)e(type."\);)416 3005 y(Py_INCREF\(&noddy_NoddyType\);)416
3097 y(PyModule_AddObject\(m,)e("Noddy",)i(\(PyObject)2254
3110 y(*)2299 3097 y(\)&noddy_NoddyType\);)236 3188 y(})0
3385 y Ft(No)n(w)20 b(that')-5 b(s)21 b(quite)f(a)g(bit)h(to)f(tak)o(e)
g(in)h(at)f(once,)f(b)n(ut)i(hopefully)d(bits)i(will)h(seem)g(f)o
(amiliar)e(from)h(the)g(last)h(chapter)-5 b(.)0 3532
y(The)20 b(\002rst)h(bit)f(that)h(will)f(be)h(ne)n(w)f(is:)236
3770 y Fj(typedef)44 b(struct)g({)416 3861 y(PyObject_HEAD)236
3952 y(})h(noddy_NoddyObject;)0 4239 y Ft(This)27 b(is)h(what)f(a)h
(Noddy)d(object)i(will)g(contain\227in)f(this)h(case,)i(nothing)d(more)
g(than)g(e)n(v)o(ery)g(Python)g(object)g(contains,)i(namely)e(a)0
4339 y(refcount)c(and)i(a)g(pointer)f(to)h(a)g(type)f(object.)36
b(These)24 b(are)g(the)g(\002elds)g(the)g Fm(PyObject_HEAD)e
Ft(macro)h(brings)g(in.)36 b(The)24 b(reason)f(for)0
4438 y(the)j(macro)f(is)i(to)f(standardize)e(the)i(layout)f(and)g(to)h
(enable)g(special)g(deb)n(ugging)d(\002elds)j(in)g(deb)n(ug)f(b)n
(uilds.)42 b(Note)26 b(that)g(there)f(is)i(no)0 4538
y(semicolon)f(after)h(the)h Fm(PyObject_HEAD)d Ft(macro;)30
b(one)d(is)h(included)e(in)i(the)f(macro)g(de\002nition.)45
b(Be)28 b(w)o(ary)f(of)g(adding)f(one)h(by)0 4637 y(accident;)h(it')-5
b(s)26 b(easy)g(to)g(do)f(from)g(habit,)h(and)f(your)g(compiler)f
(might)h(not)g(complain,)h(b)n(ut)f(someone)g(else')-5
b(s)27 b(probably)c(will!)42 b(\(On)0 4737 y(W)m(indo)n(ws,)19
b(MSVC)i(is)h(kno)n(wn)c(to)j(call)f(this)h(an)f(error)f(and)h(refuse)f
(to)i(compile)e(the)h(code.\))0 4884 y(F)o(or)g(contrast,)f(let')-5
b(s)21 b(tak)o(e)g(a)f(look)g(at)g(the)g(corresponding)d(de\002nition)i
(for)h(standard)f(Python)g(inte)o(gers:)p 0 5549 3901
4 v 0 5649 a Fo(22)2614 b(Chapter)24 b(2.)52 b(De\002ning)23
b(Ne)n(w)h(T)-10 b(ypes)p eop end
%%Page: 23 29
TeXDict begin 23 28 bop 236 174 a Fj(typedef)44 b(struct)g({)416
266 y(PyObject_HEAD)416 357 y(long)g(ob_ival;)236 448
y(})h(PyIntObject;)0 735 y Ft(Mo)o(ving)18 b(on,)i(we)h(come)e(to)h
(the)h(crunch)d(\227)j(the)f(type)g(object.)236 973 y
Fj(static)44 b(PyTypeObject)f(noddy_NoddyType)f(=)j({)416
1064 y(PyObject_HEAD_INIT\(NULL\))416 1156 y(0,)1120
b(/)1671 1169 y(*)1716 1156 y(ob_size)2031 1169 y(*)2076
1156 y(/)416 1247 y("noddy.Noddy",)580 b(/)1671 1260
y(*)1716 1247 y(tp_name)2031 1260 y(*)2076 1247 y(/)416
1338 y(sizeof\(noddy_NoddyObject\),)40 b(/)1671 1351
y(*)1716 1338 y(tp_basicsize)2256 1351 y(*)2301 1338
y(/)416 1430 y(0,)1120 b(/)1671 1443 y(*)1716 1430 y(tp_itemsize)2211
1443 y(*)2256 1430 y(/)416 1521 y(0,)g(/)1671 1534 y(*)1716
1521 y(tp_dealloc)2166 1534 y(*)2211 1521 y(/)416 1612
y(0,)g(/)1671 1625 y(*)1716 1612 y(tp_print)2076 1625
y(*)2121 1612 y(/)416 1704 y(0,)g(/)1671 1717 y(*)1716
1704 y(tp_getattr)2166 1717 y(*)2211 1704 y(/)416 1795
y(0,)g(/)1671 1808 y(*)1716 1795 y(tp_setattr)2166 1808
y(*)2211 1795 y(/)416 1886 y(0,)g(/)1671 1899 y(*)1716
1886 y(tp_compare)2166 1899 y(*)2211 1886 y(/)416 1978
y(0,)g(/)1671 1991 y(*)1716 1978 y(tp_repr)2031 1991
y(*)2076 1978 y(/)416 2069 y(0,)g(/)1671 2082 y(*)1716
2069 y(tp_as_number)2256 2082 y(*)2301 2069 y(/)416 2160
y(0,)g(/)1671 2173 y(*)1716 2160 y(tp_as_sequence)2346
2173 y(*)2391 2160 y(/)416 2252 y(0,)g(/)1671 2265 y(*)1716
2252 y(tp_as_mapping)2301 2265 y(*)2346 2252 y(/)416
2343 y(0,)g(/)1671 2356 y(*)1716 2343 y(tp_hash)2074
2356 y(*)2119 2343 y(/)416 2434 y(0,)g(/)1671 2447 y(*)1716
2434 y(tp_call)2031 2447 y(*)2076 2434 y(/)416 2526 y(0,)g(/)1671
2539 y(*)1716 2526 y(tp_str)1986 2539 y(*)2031 2526 y(/)416
2617 y(0,)g(/)1671 2630 y(*)1716 2617 y(tp_getattro)2211
2630 y(*)2256 2617 y(/)416 2708 y(0,)g(/)1671 2721 y(*)1716
2708 y(tp_setattro)2211 2721 y(*)2256 2708 y(/)416 2800
y(0,)g(/)1671 2813 y(*)1716 2800 y(tp_as_buffer)2256
2813 y(*)2301 2800 y(/)416 2891 y(Py_TPFLAGS_DEFAULT,)355
b(/)1671 2904 y(*)1716 2891 y(tp_flags)2076 2904 y(*)2121
2891 y(/)416 2982 y("Noddy)43 b(objects",)492 b(/)1671
2995 y(*)1761 2982 y(tp_doc)2074 2995 y(*)2119 2982 y(/)236
3073 y(};)0 3360 y Ft(No)n(w)19 b(if)h(you)f(go)g(and)g(look)f(up)h
(the)h(de\002nition)e(of)h Fm(PyTypeObject)f Ft(in)i(`)p
Fu(object.h)p Ft(')d(you')o(ll)h(see)i(that)g(it)g(has)g(man)o(y)e
(more)h(\002elds)g(that)0 3460 y(the)h(de\002nition)f(abo)o(v)o(e.)k
(The)d(remaining)f(\002elds)h(will)h(be)f(\002lled)h(with)f(zeros)g(by)
f(the)i(C)g(compiler)m(,)d(and)i(it')-5 b(s)21 b(common)d(practice)i
(to)g(not)0 3559 y(specify)g(them)f(e)o(xplicitly)g(unless)i(you)e
(need)h(them.)0 3706 y(This)g(is)h(so)g(important)e(that)h(we')l(re)g
(going)e(to)j(pick)e(the)i(top)f(of)f(it)i(apart)f(still)h(further:)416
3944 y Fj(PyObject_HEAD_INIT\(NULL\))0 4231 y Ft(This)f(line)h(is)g(a)f
(bit)h(of)f(a)g(w)o(art;)h(what)f(we')l(d)g(lik)o(e)g(to)g(write)h(is:)
416 4469 y Fj(PyObject_HEAD_INIT\(&PyType_Type\))0 4756
y Ft(as)i(the)g(type)f(of)g(a)h(type)f(object)g(is)h(\223type\224,)f(b)
n(ut)h(this)g(isn')o(t)f(strictly)h(conforming)c(C)24
b(and)e(some)g(compilers)g(complain.)30 b(F)o(ortunately)-5
b(,)0 4855 y(this)21 b(member)e(will)h(be)h(\002lled)f(in)g(for)g(us)h
(by)e Fm(PyType_Ready\(\))p Ft(.)416 5002 y Fj(0,)1165
b(/)1716 5015 y(*)1805 5002 y(ob_size)2164 5015 y(*)2209
5002 y(/)0 5289 y Ft(The)18 b Fm(ob_size)f Ft(\002eld)h(of)g(the)g
(header)f(is)j(not)d(used;)i(its)g(presence)e(in)h(the)h(type)e
(structure)g(is)j(a)e(historical)g(artif)o(act)g(that)g(is)h
(maintained)0 5388 y(for)h(binary)e(compatibility)h(with)h(e)o
(xtension)f(modules)g(compiled)g(for)h(older)f(v)o(ersions)g(of)h
(Python.)k(Al)o(w)o(ays)d(set)g(this)f(\002eld)h(to)f(zero.)p
0 5549 3901 4 v 0 5649 a Fo(2.1.)52 b(The)23 b(Basics)3207
b(23)p eop end
%%Page: 24 30
TeXDict begin 24 29 bop 416 174 a Fj("noddy.Noddy",)625
b(/)1716 187 y(*)1805 174 y(tp_name)2164 187 y(*)2209
174 y(/)0 461 y Ft(The)18 b(name)g(of)g(our)g(type.)24
b(This)19 b(will)g(appear)e(in)i(the)g(def)o(ault)f(te)o(xtual)f
(representation)g(of)h(our)g(objects)g(and)g(in)h(some)f(error)g
(messages,)0 560 y(for)i(e)o(xample:)236 799 y Fj(>>>)45
b("")f(+)h(noddy.new_noddy\(\))236 890 y(Traceback)f(\(most)g(recent)f
(call)i(last\):)326 981 y(File)f("<stdin>",)f(line)h(1,)h(in)f(?)236
1073 y(TypeError:)f(cannot)h(add)h(type)f("noddy.Noddy")e(to)j(string)0
1359 y Ft(Note)21 b(that)g(the)g(name)g(is)h(a)f(dotted)f(name)h(that)g
(includes)f(both)h(the)g(module)e(name)i(and)f(the)h(name)g(of)g(the)g
(type)f(within)h(the)g(module.)0 1459 y(The)f(module)f(in)h(this)h
(case)f(is)i Fm(noddy)d Ft(and)h(the)g(type)g(is)h Fm(Noddy)p
Ft(,)f(so)g(we)h(set)g(the)f(type)g(name)f(to)i Fm(noddy.Noddy)p
Ft(.)416 1606 y Fj(sizeof\(noddy_NoddyObject\),)85 b(/)1716
1619 y(*)1805 1606 y(tp_basicsize)2388 1619 y(*)2433
1606 y(/)0 1892 y Ft(This)20 b(is)h(so)g(that)f(Python)f(kno)n(ws)h(ho)
n(w)f(much)h(memory)e(to)j(allocate)e(when)h(you)f(call)i
Fm(PyObject_New\(\))p Ft(.)0 2039 y Fr(Note:)j Ft(If)19
b(you)g(w)o(ant)g(your)f(type)h(to)h(be)f(subclassable)g(from)g
(Python,)f(and)h(your)f(type)h(has)g(the)h(same)f Fm(tp_basicsize)f
Ft(as)i(its)g(base)0 2139 y(type,)e(you)f(may)h(ha)n(v)o(e)g(problems)e
(with)j(multiple)e(inheritance.)23 b(A)c(Python)e(subclass)h(of)g(your)
f(type)h(will)h(ha)n(v)o(e)e(to)i(list)g(your)e(type)h(\002rst)0
2238 y(in)k(its)g Fm(__bases__)p Ft(,)e(or)i(else)g(it)g(will)g(not)f
(be)g(able)h(to)f(call)h(your)e(type')-5 b(s)22 b Fm(__new__)e
Ft(method)g(without)h(getting)g(an)g(error)-5 b(.)28
b(Y)-9 b(ou)21 b(can)0 2338 y(a)n(v)n(oid)i(this)h(problem)e(by)h
(ensuring)e(that)j(your)e(type)h(has)h(a)f(lar)o(ger)f(v)n(alue)h(for)g
Fm(tp_basicsize)e Ft(than)i(its)i(base)e(type)g(does.)34
b(Most)0 2438 y(of)23 b(the)f(time,)i(this)f(will)h(be)f(true)f(an)o
(yw)o(ay)-5 b(,)22 b(because)g(either)g(your)g(base)h(type)f(will)i(be)
e Fm(object)p Ft(,)h(or)g(else)g(you)f(will)i(be)e(adding)g(data)0
2537 y(members)d(to)h(your)f(base)i(type,)e(and)h(therefore)e
(increasing)h(its)i(size.)416 2684 y Fj(0,)1165 b(/)1716
2697 y(*)1805 2684 y(tp_itemsize)2343 2697 y(*)2388 2684
y(/)0 2971 y Ft(This)20 b(has)h(to)f(do)g(with)g(v)n(ariable)f(length)h
(objects)g(lik)o(e)g(lists)i(and)d(strings.)25 b(Ignore)19
b(this)h(for)g(no)n(w)-5 b(.)0 3117 y(Skipping)19 b(a)h(number)f(of)h
(type)f(methods)g(that)i(we)f(don')o(t)f(pro)o(vide,)f(we)i(set)h(the)f
(class)i(\003ags)e(to)g Fm(Py_TPFLAGS_DEFAULT)p Ft(.)416
3264 y Fj(Py_TPFLAGS_DEFAULT,)355 b(/)1671 3277 y(*)1716
3264 y(tp_flags)2076 3277 y(*)2121 3264 y(/)0 3551 y
Ft(All)26 b(types)f(should)f(include)g(this)h(constant)g(in)g(their)g
(\003ags.)40 b(It)25 b(enables)g(all)h(of)e(the)h(members)f(de\002ned)g
(by)h(the)g(current)f(v)o(ersion)g(of)0 3650 y(Python.)0
3797 y(W)-7 b(e)21 b(pro)o(vide)d(a)j(doc)f(string)g(for)f(the)h(type)g
(in)g Fm(tp_doc)p Ft(.)416 3944 y Fj("Noddy)43 b(objects",)492
b(/)1671 3957 y(*)1761 3944 y(tp_doc)2074 3957 y(*)2119
3944 y(/)0 4231 y Ft(No)n(w)30 b(we)h(get)f(into)g(the)g(type)g
(methods,)h(the)f(things)g(that)g(mak)o(e)g(your)f(objects)h(dif)n
(ferent)e(from)h(the)h(others.)55 b(W)-7 b(e)31 b(aren')o(t)e(going)0
4330 y(to)e(implement)f(an)o(y)h(of)g(these)g(in)g(this)h(v)o(ersion)e
(of)h(the)g(module.)45 b(W)-7 b(e')o(ll)28 b(e)o(xpand)d(this)j(e)o
(xample)e(later)h(to)h(ha)n(v)o(e)e(more)g(interesting)0
4430 y(beha)n(vior)-5 b(.)0 4577 y(F)o(or)22 b(no)n(w)-5
b(,)22 b(all)h(we)g(w)o(ant)f(to)h(be)f(able)h(to)f(do)g(is)i(to)e
(create)h(ne)n(w)f Fm(Noddy)g Ft(objects.)31 b(T)-7 b(o)23
b(enable)f(object)g(creation,)f(we)i(ha)n(v)o(e)f(to)h(pro)o(vide)0
4676 y(a)k Fm(tp_new)g Ft(implementation.)42 b(In)27
b(this)g(case,)i(we)e(can)g(just)g(use)g(the)g(def)o(ault)f
(implementation)f(pro)o(vided)f(by)i(the)h(API)g(function)0
4776 y Fm(PyType_GenericNew\(\))p Ft(.)k(W)-7 b(e')l(d)24
b(lik)o(e)f(to)h(just)g(assign)f(this)h(to)g(the)f Fm(tp_new)g
Ft(slot,)h(b)n(ut)f(we)h(can')o(t,)f(for)g(portability)e(sak)o(e,)k(On)
0 4876 y(some)h(platforms)g(or)g(compilers,)h(we)g(can')o(t)f
(statically)h(initialize)f(a)h(structure)f(member)f(with)i(a)g
(function)e(de\002ned)h(in)g(another)g(C)0 4975 y(module,)18
b(so,)h(instead,)g(we')o(ll)g(assign)g(the)g Fm(tp_new)g
Ft(slot)g(in)g(the)h(module)d(initialization)h(function)g(just)h
(before)f(calling)g Fm(PyType_-)0 5075 y(Ready\(\))p
Ft(:)p 0 5549 3901 4 v 0 5649 a Fo(24)2614 b(Chapter)24
b(2.)52 b(De\002ning)23 b(Ne)n(w)h(T)-10 b(ypes)p eop
end
%%Page: 25 31
TeXDict begin 25 30 bop 416 174 a Fj(noddy_NoddyType.tp_new)41
b(=)j(PyType_GenericNew;)416 266 y(if)g
(\(PyType_Ready\(&noddy_NoddyType\))c(<)k(0\))595 357
y(return;)0 644 y Ft(All)21 b(the)f(other)f(type)h(methods)f(are)h
Fm(NULL)p Ft(,)g(so)h(we')o(ll)f(go)g(o)o(v)o(er)f(them)h(later)g(\227)
h(that')-5 b(s)20 b(for)g(a)h(later)f(section!)0 790
y(Ev)o(erything)d(else)k(in)g(the)f(\002le)h(should)e(be)h(f)o(amiliar)
m(,)f(e)o(xcept)g(for)h(some)g(code)f(in)i Fm(initnoddy\(\))p
Ft(:)416 1029 y Fj(if)44 b(\(PyType_Ready\(&noddy_NoddyType\))c(<)k
(0\))595 1120 y(return;)0 1406 y Ft(This)20 b(initializes)h(the)f
Fm(Noddy)g Ft(type,)f(\002ling)i(in)f(a)h(number)d(of)i(members,)f
(including)f Fm(ob_type)i Ft(that)g(we)g(initially)h(set)g(to)f
Fm(NULL)p Ft(.)416 1553 y Fj(PyModule_AddObject\(m,)41
b("Noddy",)i(\(PyObject)2254 1566 y(*)2299 1553 y
(\)&noddy_NoddyType\);)0 1840 y Ft(This)20 b(adds)g(the)h(type)e(to)i
(the)f(module)f(dictionary)-5 b(.)22 b(This)f(allo)n(ws)f(us)h(to)f
(create)g Fm(Noddy)g Ft(instances)g(by)g(calling)f(the)i
Fm(Noddy)e Ft(class:)236 2078 y Fj(>>>)45 b(import)e(noddy)236
2169 y(>>>)i(mynoddy)e(=)i(noddy.Noddy\(\))0 2456 y Ft(That')-5
b(s)20 b(it!)26 b(All)21 b(that)f(remains)g(is)h(to)f(b)n(uild)g(it;)h
(put)f(the)g(abo)o(v)o(e)e(code)i(in)g(a)h(\002le)g(called)f(`)p
Fu(noddy)-7 b(.c)p Ft(')18 b(and)236 2694 y Fj(from)44
b(distutils.core)f(import)h(setup,)g(Extension)236 2785
y(setup\(name="noddy",)e(version="1.0",)505 2877 y
(ext_modules=[Extension\("noddy",)e(["noddy.c"]\)]\))0
3163 y Ft(in)20 b(a)h(\002le)g(called)f(`)p Fu(setup)m(.p)n(y)p
Ft(';)f(then)g(typing)236 3401 y Fj($)45 b(python)f(setup.py)f(build)0
3688 y Ft(at)23 b(a)f(shell)g(should)f(produce)f(a)j(\002le)g(`)p
Fu(noddy)-7 b(.so)p Ft(')19 b(in)j(a)h(subdirectory;)d(mo)o(v)o(e)h(to)
h(that)g(directory)f(and)g(\002re)h(up)g(Python)f(\227)h(you)g(should)0
3788 y(be)e(able)g(to)h Fm(import)48 b(noddy)20 b Ft(and)g(play)f
(around)g(with)h(Noddy)f(objects.)0 3934 y(That)h(w)o(asn')o(t)g(so)g
(hard,)f(w)o(as)i(it?)0 4081 y(Of)27 b(course,)g(the)g(current)e(Noddy)
g(type)h(is)i(pretty)e(uninteresting.)42 b(It)27 b(has)f(no)h(data)f
(and)g(doesn')o(t)f(do)h(an)o(ything.)43 b(It)26 b(can')o(t)g(e)n(v)o
(en)g(be)0 4181 y(subclassed.)0 4463 y Fg(2.1.1)100 b(Adding)30
b(data)f(and)f(methods)h(to)f(the)g(Basic)g(e)m(xample)0
4666 y Ft(Let')-5 b(s)22 b(e)o(xpend)d(the)i(basic)h(e)o(xample)d(to)j
(add)e(some)h(data)g(and)g(methods.)27 b(Let')-5 b(s)22
b(also)f(mak)o(e)g(the)g(type)g(usable)g(as)h(a)f(base)h(class.)28
b(W)-7 b(e')o(ll)0 4765 y(create)20 b(a)h(ne)n(w)f(module,)e
Fm(noddy2)i Ft(that)g(adds)g(these)g(capabilities:)236
4943 y Fj(#include)44 b(<Python.h>)236 5035 y(#include)g
("structmember.h")236 5217 y(typedef)g(struct)g({)416
5309 y(PyObject_HEAD)416 5400 y(PyObject)819 5413 y(*)864
5400 y(first;)g(/)1223 5413 y(*)1312 5400 y(first)g(name)1805
5413 y(*)1850 5400 y(/)p 0 5549 3901 4 v 0 5649 a Fo(2.1.)52
b(The)23 b(Basics)3207 b(25)p eop end
%%Page: 26 32
TeXDict begin 26 31 bop 416 83 a Fj(PyObject)819 96 y(*)864
83 y(last;)89 b(/)1223 96 y(*)1312 83 y(last)44 b(name)1761
96 y(*)1806 83 y(/)416 174 y(int)g(number;)236 266 y(})h(Noddy;)236
448 y(static)f(void)236 540 y(Noddy_dealloc\(Noddy)1091
553 y(*)1178 540 y(self\))236 631 y({)416 722 y
(Py_XDECREF\(self->first\);)416 814 y(Py_XDECREF\(self->last\);)416
905 y(self->ob_type->tp_free\(\(PyObject)1856 918 y(*)1901
905 y(\)se)o(lf\);)236 996 y(})236 1179 y(static)g(PyObject)954
1192 y(*)236 1270 y(Noddy_new\(PyTypeObject)1267 1283
y(*)1312 1270 y(type,)g(PyObject)1985 1283 y(*)2030 1270
y(args,)f(PyObject)2702 1283 y(*)2747 1270 y(kwds\))236
1362 y({)416 1453 y(Noddy)685 1466 y(*)730 1453 y(self;)416
1636 y(self)h(=)g(\(Noddy)1043 1649 y(*)1088 1636 y
(\)type->tp_alloc\(type,)d(0\);)416 1727 y(if)j(\(self)g(!=)h(NULL\))f
({)595 1818 y(self->first)f(=)i(PyString_FromString\(""\);)595
1910 y(if)f(\(self->first)f(==)i(NULL\))685 2001 y({)774
2092 y(Py_DECREF\(self\);)774 2183 y(return)f(NULL;)685
2275 y(})595 2457 y(self->last)f(=)i(PyString_FromString\(""\);)595
2549 y(if)f(\(self->last)f(==)i(NULL\))685 2640 y({)774
2731 y(Py_DECREF\(self\);)774 2823 y(return)f(NULL;)685
2914 y(})595 3097 y(self->number)f(=)h(0;)416 3188 y(})416
3371 y(return)f(\(PyObject)1178 3384 y(*)1223 3371 y(\)self;)236
3462 y(})236 3645 y(static)h(int)236 3736 y(Noddy_init\(Noddy)998
3749 y(*)1043 3736 y(self,)g(PyObject)1716 3749 y(*)1761
3736 y(args,)f(PyObject)2433 3749 y(*)2478 3736 y(kwds\))236
3827 y({)416 3919 y(PyObject)819 3932 y(*)864 3919 y(first=NULL,)1402
3932 y(*)1447 3919 y(last=NULL,)1940 3932 y(*)1985 3919
y(tmp;)416 4101 y(static)g(char)954 4114 y(*)999 4101
y(kwlist[])g(=)i({"first",)e("last",)g("number",)h(NULL};)416
4284 y(if)g(\(!)h(PyArg_ParseTupleAndKeywords\(args,)39
b(kwds,)44 b("|OOi",)f(kwlist,)1940 4375 y(&first,)g(&last,)1940
4467 y(&self->number\)\))595 4558 y(return)h(-1;)416
4741 y(if)g(\(first\))g({)595 4832 y(tmp)g(=)h(self->first;)595
4923 y(Py_INCREF\(first\);)595 5015 y(self->first)e(=)i(first;)595
5106 y(Py_XDECREF\(tmp\);)416 5197 y(})416 5380 y(if)f(\(last\))g({)p
0 5549 3901 4 v 0 5649 a Fo(26)2614 b(Chapter)24 b(2.)52
b(De\002ning)23 b(Ne)n(w)h(T)-10 b(ypes)p eop end
%%Page: 27 33
TeXDict begin 27 32 bop 595 83 a Fj(tmp)44 b(=)h(self->last;)595
174 y(Py_INCREF\(last\);)595 266 y(self->last)e(=)i(last;)595
357 y(Py_XDECREF\(tmp\);)416 448 y(})416 631 y(return)e(0;)236
722 y(})236 996 y(static)h(PyMemberDef)f(Noddy_members[])f(=)j({)416
1088 y({"first",)e(T_OBJECT_EX,)g(offsetof\(Noddy,)f(first\),)i(0,)460
1179 y("first)g(name"},)416 1270 y({"last",)f(T_OBJECT_EX,)g
(offsetof\(Noddy,)f(last\),)i(0,)460 1362 y("last)g(name"},)416
1453 y({"number",)f(T_INT,)h(offsetof\(Noddy,)e(number\),)h(0,)460
1544 y("noddy)h(number"},)416 1636 y({NULL})88 b(/)819
1649 y(*)909 1636 y(Sentinel)1312 1649 y(*)1357 1636
y(/)236 1727 y(};)236 1910 y(static)44 b(PyObject)954
1923 y(*)236 2001 y(Noddy_name\(Noddy)956 2014 y(*)1043
2001 y(self\))236 2092 y({)416 2183 y(static)f(PyObject)1133
2196 y(*)1178 2183 y(format)h(=)g(NULL;)416 2275 y(PyObject)819
2288 y(*)864 2275 y(args,)1133 2288 y(*)1178 2275 y(result;)416
2457 y(if)g(\(format)g(==)g(NULL\))g({)595 2549 y(format)g(=)g
(PyString_FromString\("\045s)d(\045s"\);)595 2640 y(if)j(\(format)g(==)
h(NULL\))774 2731 y(return)f(NULL;)416 2823 y(})416 3005
y(if)g(\(self->first)f(==)h(NULL\))g({)595 3097 y
(PyErr_SetString\(PyExc_AttributeError,)38 b("first"\);)595
3188 y(return)44 b(NULL;)416 3279 y(})416 3462 y(if)g(\(self->last)f
(==)i(NULL\))f({)595 3553 y(PyErr_SetString\(PyExc_AttributeError,)38
b("last"\);)595 3645 y(return)44 b(NULL;)416 3736 y(})416
3919 y(args)g(=)g(Py_BuildValue\("OO",)e(self->first,)h(self->last\);)
416 4010 y(if)h(\(args)g(==)h(NULL\))595 4101 y(return)f(NULL;)416
4284 y(result)f(=)i(PyString_Format\(format,)c(args\);)416
4375 y(Py_DECREF\(args\);)416 4558 y(return)i(result;)236
4649 y(})236 4832 y(static)h(PyMethodDef)f(Noddy_methods[])f(=)j({)416
4923 y({"name",)e(\(PyCFunction\)Noddy_name,)e(METH_NOARGS,)460
5015 y("Return)j(the)g(name,)g(combining)g(the)g(first)g(and)g(last)g
(name")416 5106 y(},)416 5197 y({NULL})88 b(/)819 5210
y(*)909 5197 y(Sentinel)1312 5210 y(*)1357 5197 y(/)236
5289 y(};)p 0 5549 3901 4 v 0 5649 a Fo(2.1.)52 b(The)23
b(Basics)3207 b(27)p eop end
%%Page: 28 34
TeXDict begin 28 33 bop 236 83 a Fj(static)44 b(PyTypeObject)f
(NoddyType)g(=)i({)416 174 y(PyObject_HEAD_INIT\(NULL\))416
266 y(0,)1120 b(/)1671 279 y(*)1716 266 y(ob_size)2031
279 y(*)2076 266 y(/)416 357 y("noddy.Noddy",)580 b(/)1671
370 y(*)1716 357 y(tp_name)2031 370 y(*)2076 357 y(/)416
448 y(sizeof\(Noddy\),)g(/)1671 461 y(*)1716 448 y(tp_basicsize)2256
461 y(*)2301 448 y(/)416 540 y(0,)1120 b(/)1671 553 y(*)1716
540 y(tp_itemsize)2211 553 y(*)2256 540 y(/)416 631 y
(\(destructor\)Noddy_dealloc,)40 b(/)1671 644 y(*)1716
631 y(tp_dealloc)2166 644 y(*)2211 631 y(/)416 722 y(0,)1120
b(/)1671 735 y(*)1716 722 y(tp_print)2076 735 y(*)2121
722 y(/)416 814 y(0,)g(/)1671 827 y(*)1716 814 y(tp_getattr)2166
827 y(*)2211 814 y(/)416 905 y(0,)g(/)1671 918 y(*)1716
905 y(tp_setattr)2166 918 y(*)2211 905 y(/)416 996 y(0,)g(/)1671
1009 y(*)1716 996 y(tp_compare)2166 1009 y(*)2211 996
y(/)416 1088 y(0,)g(/)1671 1101 y(*)1716 1088 y(tp_repr)2031
1101 y(*)2076 1088 y(/)416 1179 y(0,)g(/)1671 1192 y(*)1716
1179 y(tp_as_number)2256 1192 y(*)2301 1179 y(/)416 1270
y(0,)g(/)1671 1283 y(*)1716 1270 y(tp_as_sequence)2346
1283 y(*)2391 1270 y(/)416 1362 y(0,)g(/)1671 1375 y(*)1716
1362 y(tp_as_mapping)2301 1375 y(*)2346 1362 y(/)416
1453 y(0,)g(/)1671 1466 y(*)1716 1453 y(tp_hash)2074
1466 y(*)2119 1453 y(/)416 1544 y(0,)g(/)1671 1557 y(*)1716
1544 y(tp_call)2031 1557 y(*)2076 1544 y(/)416 1636 y(0,)g(/)1671
1649 y(*)1716 1636 y(tp_str)1986 1649 y(*)2031 1636 y(/)416
1727 y(0,)g(/)1671 1740 y(*)1716 1727 y(tp_getattro)2211
1740 y(*)2256 1727 y(/)416 1818 y(0,)g(/)1671 1831 y(*)1716
1818 y(tp_setattro)2211 1831 y(*)2256 1818 y(/)416 1910
y(0,)g(/)1671 1923 y(*)1716 1910 y(tp_as_buffer)2256
1923 y(*)2301 1910 y(/)416 2001 y(Py_TPFLAGS_DEFAULT)41
b(|)k(Py_TPFLAGS_BASETYPE,)c(/)2343 2014 y(*)2388 2001
y(tp_flags)2748 2014 y(*)2793 2001 y(/)416 2092 y("Noddy)i(objects",)
492 b(/)1671 2105 y(*)1761 2092 y(tp_doc)2074 2105 y(*)2119
2092 y(/)416 2183 y(0,)672 b(/)1223 2196 y(*)1312 2183
y(tp_traverse)1850 2196 y(*)1895 2183 y(/)416 2275 y(0,)g(/)1223
2288 y(*)1312 2275 y(tp_clear)1716 2288 y(*)1761 2275
y(/)416 2366 y(0,)g(/)1223 2379 y(*)1312 2366 y(tp_richcompare)1985
2379 y(*)2030 2366 y(/)416 2457 y(0,)g(/)1223 2470 y(*)1312
2457 y(tp_weaklistoffset)2119 2470 y(*)2164 2457 y(/)416
2549 y(0,)g(/)1223 2562 y(*)1312 2549 y(tp_iter)1671
2562 y(*)1716 2549 y(/)416 2640 y(0,)g(/)1223 2653 y(*)1312
2640 y(tp_iternext)1850 2653 y(*)1895 2640 y(/)416 2731
y(Noddy_methods,)580 b(/)1671 2744 y(*)1761 2731 y(tp_methods)2254
2744 y(*)2299 2731 y(/)416 2823 y(Noddy_members,)g(/)1671
2836 y(*)1761 2823 y(tp_members)2254 2836 y(*)2299 2823
y(/)416 2914 y(0,)1120 b(/)1671 2927 y(*)1761 2914 y(tp_getset)2209
2927 y(*)2254 2914 y(/)416 3005 y(0,)g(/)1671 3018 y(*)1761
3005 y(tp_base)2119 3018 y(*)2164 3005 y(/)416 3097 y(0,)g(/)1671
3110 y(*)1761 3097 y(tp_dict)2119 3110 y(*)2164 3097
y(/)416 3188 y(0,)g(/)1671 3201 y(*)1761 3188 y(tp_descr_get)2343
3201 y(*)2388 3188 y(/)416 3279 y(0,)g(/)1671 3292 y(*)1761
3279 y(tp_descr_set)2343 3292 y(*)2388 3279 y(/)416 3371
y(0,)g(/)1671 3384 y(*)1761 3371 y(tp_dictoffset)2388
3384 y(*)2433 3371 y(/)416 3462 y(\(initproc\)Noddy_init,)265
b(/)1671 3475 y(*)1761 3462 y(tp_init)2119 3475 y(*)2164
3462 y(/)416 3553 y(0,)1120 b(/)1671 3566 y(*)1761 3553
y(tp_alloc)2164 3566 y(*)2209 3553 y(/)416 3645 y(Noddy_new,)760
b(/)1671 3658 y(*)1761 3645 y(tp_new)2074 3658 y(*)2119
3645 y(/)236 3736 y(};)236 3919 y(static)44 b(PyMethodDef)f
(module_methods[])f(=)j({)416 4010 y({NULL})88 b(/)819
4023 y(*)909 4010 y(Sentinel)1312 4023 y(*)1357 4010
y(/)236 4101 y(};)236 4284 y(#ifndef)44 b(PyMODINIT_FUNC)e(/)1312
4297 y(*)1402 4284 y(declarations)h(for)h(DLL)g(import/export)2971
4297 y(*)3016 4284 y(/)236 4375 y(#define)g(PyMODINIT_FUNC)e(void)236
4467 y(#endif)236 4558 y(PyMODINIT_FUNC)236 4649 y(initnoddy2\(void\))
236 4741 y({)416 4832 y(PyObject)776 4845 y(*)864 4832
y(m;)416 5015 y(if)i(\(PyType_Ready\(&NoddyType\))d(<)j(0\))595
5106 y(return;)416 5289 y(m)g(=)h(Py_InitModule3\("noddy2",)c
(module_methods,)1267 5380 y("Example)j(module)g(that)g(creates)f(an)i
(extension)e(type."\);)p 0 5549 3901 4 v 0 5649 a Fo(28)2614
b(Chapter)24 b(2.)52 b(De\002ning)23 b(Ne)n(w)h(T)-10
b(ypes)p eop end
%%Page: 29 35
TeXDict begin 29 34 bop 416 174 a Fj(if)44 b(\(m)h(==)f(NULL\))505
266 y(return;)416 448 y(Py_INCREF\(&NoddyType\);)416
540 y(PyModule_AddObject\(m,)d("Noddy",)i(\(PyObject)2254
553 y(*)2299 540 y(\)&NoddyType\);)236 631 y(})0 828
y Ft(This)20 b(v)o(ersion)f(of)h(the)g(module)f(has)i(a)f(number)f(of)h
(changes.)0 975 y(W)-7 b(e')l(v)o(e)20 b(added)f(an)h(e)o(xtra)g
(include:)236 1213 y Fj(#include)44 b("structmember.h")0
1499 y Ft(This)20 b(include)g(pro)o(vides)e(declarations)h(that)h(we)h
(use)f(to)g(handle)g(attrib)n(utes,)f(as)i(described)e(a)i(bit)f(later)
-5 b(.)0 1646 y(The)22 b(name)g(of)g(the)g Fm(Noddy)g
Ft(object)g(structure)f(has)i(been)e(shortened)g(to)h
Fm(Noddy)p Ft(.)31 b(The)22 b(type)g(object)g(name)g(has)g(been)g
(shortened)e(to)0 1746 y Fm(NoddyType)p Ft(.)0 1893 y(The)i
Fm(Noddy)g Ft(type)g(no)n(w)g(has)h(three)f(data)h(attrib)n(utes,)f
Fq(\002r)o(st)q Ft(,)j Fq(last)q Ft(,)e(and)f Fq(number)r
Ft(.)31 b(The)23 b Fq(\002r)o(st)i Ft(and)d Fq(last)i
Ft(v)n(ariables)e(are)g(Python)g(strings)0 1992 y(containing)c(\002rst)
j(and)f(last)h(names.)k(The)19 b Fq(number)j Ft(attrib)n(ute)d(is)j(an)
e(inte)o(ger)-5 b(.)0 2139 y(The)20 b(object)g(structure)f(is)i
(updated)d(accordingly:)236 2377 y Fj(typedef)44 b(struct)g({)416
2469 y(PyObject_HEAD)416 2560 y(PyObject)819 2573 y(*)864
2560 y(first;)416 2651 y(PyObject)819 2664 y(*)864 2651
y(last;)416 2743 y(int)g(number;)236 2834 y(})h(Noddy;)0
3120 y Ft(Because)29 b(we)f(no)n(w)g(ha)n(v)o(e)g(data)g(to)h(manage,)g
(we)g(ha)n(v)o(e)f(to)g(be)g(more)g(careful)g(about)f(object)h
(allocation)f(and)h(deallocation.)48 b(At)29 b(a)0 3220
y(minimum,)18 b(we)j(need)f(a)g(deallocation)f(method:)236
3458 y Fj(static)44 b(void)236 3550 y(Noddy_dealloc\(Noddy)1091
3563 y(*)1178 3550 y(self\))236 3641 y({)416 3732 y
(Py_XDECREF\(self->first\);)416 3824 y(Py_XDECREF\(self->last\);)416
3915 y(self->ob_type->tp_free\(\(PyObject)1856 3928 y(*)1901
3915 y(\)se)o(lf\);)236 4006 y(})0 4293 y Ft(which)20
b(is)h(assigned)e(to)i(the)f Fm(tp_dealloc)f Ft(member:)416
4531 y Fj(\(destructor\)Noddy_dealloc,)40 b(/)1671 4544
y(*)1716 4531 y(tp_dealloc)2166 4544 y(*)2211 4531 y(/)0
4817 y Ft(This)26 b(method)f(decrements)g(the)h(reference)f(counts)g
(of)h(the)g(tw)o(o)h(Python)e(attrib)n(utes.)42 b(W)-7
b(e)28 b(use)e Fm(Py_XDECREF\(\))f Ft(here)g(because)0
4917 y(the)g Fm(first)f Ft(and)h Fm(last)f Ft(members)g(could)g(be)h
Fm(NULL)p Ft(.)f(It)h(then)g(calls)g(the)g Fm(tp_free)f
Ft(member)g(of)g(the)h(object')-5 b(s)25 b(type)f(to)h(free)g(the)0
5017 y(object')-5 b(s)23 b(memory)-5 b(.)33 b(Note)23
b(that)h(the)f(object')-5 b(s)24 b(type)f(might)f(not)i(be)f
Fm(NoddyType)p Ft(,)g(because)g(the)g(object)g(may)g(be)g(an)h
(instance)f(of)g(a)0 5116 y(subclass.)0 5263 y(W)-7 b(e)21
b(w)o(ant)g(to)f(mak)o(e)g(sure)g(that)g(the)g(\002rst)h(and)f(last)h
(names)f(are)g(initialized)g(to)g(empty)f(strings,)h(so)h(we)g(pro)o
(vide)d(a)i(ne)n(w)g(method:)p 0 5549 3901 4 v 0 5649
a Fo(2.1.)52 b(The)23 b(Basics)3207 b(29)p eop end
%%Page: 30 36
TeXDict begin 30 35 bop 236 83 a Fj(static)44 b(PyObject)954
96 y(*)236 174 y(Noddy_new\(PyTypeObject)1267 187 y(*)1312
174 y(type,)g(PyObject)1985 187 y(*)2030 174 y(args,)f(PyObject)2702
187 y(*)2747 174 y(kwds\))236 266 y({)416 357 y(Noddy)685
370 y(*)730 357 y(self;)416 540 y(self)h(=)g(\(Noddy)1043
553 y(*)1088 540 y(\)type->tp_alloc\(type,)d(0\);)416
631 y(if)j(\(self)g(!=)h(NULL\))f({)595 722 y(self->first)f(=)i
(PyString_FromString\(""\);)595 814 y(if)f(\(self->first)f(==)i(NULL\))
685 905 y({)774 996 y(Py_DECREF\(self\);)774 1088 y(return)f(NULL;)685
1179 y(})595 1362 y(self->last)f(=)i(PyString_FromString\(""\);)595
1453 y(if)f(\(self->last)f(==)i(NULL\))685 1544 y({)774
1636 y(Py_DECREF\(self\);)774 1727 y(return)f(NULL;)685
1818 y(})595 2001 y(self->number)f(=)h(0;)416 2092 y(})416
2275 y(return)f(\(PyObject)1178 2288 y(*)1223 2275 y(\)self;)236
2366 y(})0 2653 y Ft(and)20 b(install)g(it)h(in)g(the)f
Fm(tp_new)f Ft(member:)416 2891 y Fj(Noddy_new,)760 b(/)1671
2904 y(*)1761 2891 y(tp_new)2074 2904 y(*)2119 2891 y(/)0
3177 y Ft(The)20 b(ne)n(w)g(member)f(is)i(responsible)f(for)f(creating)
h(\(as)g(opposed)f(to)i(initializing\))e(objects)h(of)g(the)g(type.)25
b(It)c(is)g(e)o(xposed)e(in)i(Python)e(as)0 3277 y(the)i
Fm(__new__\(\))f Ft(method.)25 b(See)c(the)g(paper)f(titled)h
(\223Unifying)e(types)i(and)f(classes)i(in)f(Python\224)f(for)g(a)h
(detailed)g(discussion)f(of)h(the)0 3377 y Fm(__new__\(\))g
Ft(method.)29 b(One)22 b(reason)f(to)h(implement)f(a)h(ne)n(w)g(method)
f(is)i(to)f(assure)g(the)g(initial)g(v)n(alues)g(of)g(instance)f(v)n
(ariables.)30 b(In)0 3476 y(this)20 b(case,)g(we)g(use)f(the)h(ne)n(w)f
(method)f(to)h(mak)o(e)g(sure)h(that)f(the)h(initial)f(v)n(alues)g(of)h
(the)f(members)f Fm(first)h Ft(and)g Fm(last)h Ft(are)f(not)g
Fm(NULL)p Ft(.)0 3576 y(If)k(we)h(didn')o(t)e(care)h(whether)g(the)g
(initial)h(v)n(alues)f(were)h Fm(NULL)p Ft(,)f(we)h(could)e(ha)n(v)o(e)
h(used)g Fm(PyType_GenericNew\(\))e Ft(as)j(our)f(ne)n(w)0
3675 y(method,)c(as)h(we)h(did)f(before.)j Fm(PyType_GenericNew\(\))18
b Ft(initializes)i(all)h(of)f(the)g(instance)g(v)n(ariable)f(members)g
(to)i Fm(NULL)p Ft(.)0 3822 y(The)f(ne)n(w)h(method)f(is)h(a)h(static)f
(method)f(that)h(is)g(passed)g(the)g(type)f(being)g(instantiated)g(and)
h(an)o(y)f(ar)o(guments)e(passed)j(when)f(the)h(type)0
3922 y(w)o(as)c(called,)g(and)e(that)i(returns)e(the)h(ne)n(w)h(object)
e(created.)23 b(Ne)n(w)17 b(methods)e(al)o(w)o(ays)h(accept)g
(positional)g(and)f(k)o(e)o(yw)o(ord)g(ar)o(guments,)g(b)n(ut)0
4022 y(the)o(y)22 b(often)h(ignore)f(the)h(ar)o(guments,)e(lea)n(ving)i
(the)g(ar)o(gument)e(handling)g(to)i(initializer)g(methods.)33
b(Note)23 b(that)g(if)h(the)f(type)g(supports)0 4121
y(subclassing,)28 b(the)f(type)f(passed)h(may)f(not)h(be)g(the)g(type)f
(being)g(de\002ned.)44 b(The)27 b(ne)n(w)g(method)e(calls)j(the)f(tp)p
3189 4121 25 4 v 29 w(alloc)g(slot)h(to)f(allocate)0
4221 y(memory)-5 b(.)29 b(W)-7 b(e)24 b(don')o(t)c(\002ll)j(the)g
Fm(tp_alloc)e Ft(slot)i(ourselv)o(es.)30 b(Rather)22
b Fm(PyType_Ready\(\))e Ft(\002lls)k(it)f(for)e(us)i(by)f(inheriting)f
(it)i(from)0 4320 y(our)c(base)i(class,)g(which)e(is)i
Fm(object)f Ft(by)g(def)o(ault.)k(Most)c(types)g(use)h(the)f(def)o
(ault)g(allocation.)0 4467 y Fr(Note:)j Ft(If)16 b(you)g(are)h
(creating)f(a)h(co-operati)n(v)o(e)d Fm(tp_new)i Ft(\(one)g(that)h
(calls)g(a)g(base)g(type')-5 b(s)17 b Fm(tp_new)f Ft(or)h
Fm(__new__)p Ft(\),)f(you)g(must)g Fq(not)h Ft(try)0
4567 y(to)h(determine)f(what)g(method)g(to)h(call)g(using)g(method)e
(resolution)h(order)g(at)h(runtime.)23 b(Al)o(w)o(ays)18
b(statically)h(determine)d(what)i(type)f(you)0 4667 y(are)24
b(going)e(to)i(call,)h(and)e(call)h(its)h Fm(tp_new)e
Ft(directly)-5 b(,)24 b(or)f(via)h Fm(type->tp_base->tp_new)p
Ft(.)32 b(If)24 b(you)f(do)g(not)g(do)h(this,)h(Python)0
4766 y(subclasses)d(of)f(your)f(type)g(that)i(also)f(inherit)g(from)f
(other)g(Python-de\002ned)f(classes)j(may)f(not)g(w)o(ork)f(correctly)
-5 b(.)26 b(\(Speci\002cally)-5 b(,)20 b(you)0 4866 y(may)g(not)g(be)g
(able)g(to)g(create)g(instances)g(of)g(such)g(subclasses)h(without)e
(getting)g(a)i Fm(TypeError)p Ft(.\))0 5013 y(W)-7 b(e)21
b(pro)o(vide)d(an)j(initialization)e(function:)p 0 5549
3901 4 v 0 5649 a Fo(30)2614 b(Chapter)24 b(2.)52 b(De\002ning)23
b(Ne)n(w)h(T)-10 b(ypes)p eop end
%%Page: 31 37
TeXDict begin 31 36 bop 236 83 a Fj(static)44 b(int)236
174 y(Noddy_init\(Noddy)998 187 y(*)1043 174 y(self,)g(PyObject)1716
187 y(*)1761 174 y(args,)f(PyObject)2433 187 y(*)2478
174 y(kwds\))236 266 y({)416 357 y(PyObject)819 370 y(*)864
357 y(first=NULL,)1402 370 y(*)1447 357 y(last=NULL,)1940
370 y(*)1985 357 y(tmp;)416 540 y(static)g(char)954 553
y(*)999 540 y(kwlist[])g(=)i({"first",)e("last",)g("number",)h(NULL};)
416 722 y(if)g(\(!)h(PyArg_ParseTupleAndKeywords\(args,)39
b(kwds,)44 b("|OOi",)f(kwlist,)1940 814 y(&first,)g(&last,)1940
905 y(&self->number\)\))595 996 y(return)h(-1;)416 1179
y(if)g(\(first\))g({)595 1270 y(tmp)g(=)h(self->first;)595
1362 y(Py_INCREF\(first\);)595 1453 y(self->first)e(=)i(first;)595
1544 y(Py_XDECREF\(tmp\);)416 1636 y(})416 1818 y(if)f(\(last\))g({)595
1910 y(tmp)g(=)h(self->last;)595 2001 y(Py_INCREF\(last\);)595
2092 y(self->last)e(=)i(last;)595 2183 y(Py_XDECREF\(tmp\);)416
2275 y(})416 2457 y(return)e(0;)236 2549 y(})0 2835 y
Ft(by)20 b(\002lling)g(the)g Fm(tp_init)g Ft(slot.)416
3073 y Fj(\(initproc\)Noddy_init,)400 b(/)1806 3086 y(*)1895
3073 y(tp_init)2254 3086 y(*)2299 3073 y(/)0 3360 y Ft(The)15
b Fm(tp_init)g Ft(slot)h(is)g(e)o(xposed)e(in)i(Python)e(as)i(the)g
Fm(__init__\(\))e Ft(method.)22 b(It)16 b(is)g(used)f(to)h(initialize)g
(an)f(object)g(after)g(it')-5 b(s)17 b(created.)0 3460
y(Unlik)o(e)24 b(the)g(ne)n(w)h(method,)e(we)i(can')o(t)f(guarantee)e
(that)j(the)f(initializer)g(is)h(called.)38 b(The)24
b(initializer)g(isn')o(t)g(called)g(when)g(unpickling)0
3559 y(objects)17 b(and)f(it)i(can)f(be)g(o)o(v)o(erridden.)k(Our)c
(initializer)g(accepts)g(ar)o(guments)e(to)i(pro)o(vide)e(initial)j(v)n
(alues)e(for)h(our)f(instance.)24 b(Initializers)0 3659
y(al)o(w)o(ays)d(accept)e(positional)h(and)f(k)o(e)o(yw)o(ord)g(ar)o
(guments.)0 3806 y(Initializers)j(can)g(be)h(called)f(multiple)g
(times.)32 b(An)o(yone)20 b(can)i(call)h(the)g Fm(__init__\(\))e
Ft(method)g(on)h(our)f(objects.)32 b(F)o(or)22 b(this)h(reason,)0
3905 y(we)g(ha)n(v)o(e)g(to)g(be)g(e)o(xtra)f(careful)g(when)g
(assigning)g(the)h(ne)n(w)g(v)n(alues.)32 b(W)-7 b(e)25
b(might)d(be)h(tempted,)f(for)g(e)o(xample)g(to)h(assign)g(the)g
Fm(first)0 4005 y Ft(member)c(lik)o(e)h(this:)416 4243
y Fj(if)44 b(\(first\))g({)595 4335 y(Py_XDECREF\(self->first\);)595
4426 y(Py_INCREF\(first\);)595 4517 y(self->first)f(=)i(first;)416
4608 y(})0 4895 y Ft(But)25 b(this)f(w)o(ould)f(be)h(risk)o(y)-5
b(.)36 b(Our)23 b(type)h(doesn')o(t)e(restrict)i(the)g(type)g(of)g(the)
g Fm(first)f Ft(member)m(,)g(so)i(it)f(could)f(be)h(an)o(y)f(kind)h(of)
f(object.)0 4995 y(It)k(could)f(ha)n(v)o(e)h(a)g(destructor)f(that)h
(causes)h(code)e(to)h(be)g(e)o(x)o(ecuted)e(that)i(tries)h(to)f(access)
h(the)f Fm(first)g Ft(member)-5 b(.)44 b(T)-7 b(o)28
b(be)f(paranoid)0 5094 y(and)18 b(protect)g(ourselv)o(es)g(against)g
(this)i(possibility)-5 b(,)18 b(we)h(almost)g(al)o(w)o(ays)g(reassign)g
(members)f(before)f(decrementing)g(their)h(reference)0
5194 y(counts.)24 b(When)c(don')o(t)f(we)h(ha)n(v)o(e)g(to)g(do)g
(this?)125 5400 y Fs(\017)41 b Ft(when)19 b(we)i(absolutely)e(kno)n(w)g
(that)h(the)g(reference)f(count)g(is)i(greater)e(than)h(1)p
0 5549 3901 4 v 0 5649 a Fo(2.1.)52 b(The)23 b(Basics)3207
b(31)p eop end
%%Page: 32 38
TeXDict begin 32 37 bop 125 83 a Fs(\017)41 b Ft(when)19
b(we)i(kno)n(w)e(that)h(deallocation)f(of)h(the)g(object)1717
53 y Fk(1)1770 83 y Ft(will)h(not)e(cause)i(an)o(y)e(calls)i(back)e
(into)h(our)g(type')-5 b(s)20 b(code)125 249 y Fs(\017)41
b Ft(when)19 b(decrementing)f(a)i(reference)f(count)g(in)h(a)h
Fm(tp_dealloc)e Ft(handler)g(when)g(garbage-collections)e(is)k(not)f
(supported)3809 219 y Fk(2)0 462 y Ft(W)-7 b(e)24 b(w)o(ant)f(to)g(w)o
(ant)g(to)g(e)o(xpose)e(our)h(instance)h(v)n(ariables)f(as)h(attrib)n
(utes.)33 b(There)22 b(are)h(a)g(number)e(of)h(w)o(ays)i(to)f(do)f
(that.)33 b(The)22 b(simplest)0 562 y(w)o(ay)e(is)h(to)g(de\002ne)e
(member)g(de\002nitions:)236 800 y Fj(static)44 b(PyMemberDef)f
(Noddy_members[])f(=)j({)416 891 y({"first",)e(T_OBJECT_EX,)g
(offsetof\(Noddy,)f(first\),)i(0,)460 983 y("first)g(name"},)416
1074 y({"last",)f(T_OBJECT_EX,)g(offsetof\(Noddy,)f(last\),)i(0,)460
1165 y("last)g(name"},)416 1257 y({"number",)f(T_INT,)h
(offsetof\(Noddy,)e(number\),)h(0,)460 1348 y("noddy)h(number"},)416
1439 y({NULL})88 b(/)819 1452 y(*)909 1439 y(Sentinel)1312
1452 y(*)1357 1439 y(/)236 1531 y(};)0 1817 y Ft(and)20
b(put)f(the)i(de\002nitions)e(in)h(the)h Fm(tp_members)d
Ft(slot:)416 2055 y Fj(Noddy_members,)580 b(/)1671 2068
y(*)1761 2055 y(tp_members)2254 2068 y(*)2299 2055 y(/)0
2342 y Ft(Each)25 b(member)f(de\002nition)h(has)h(a)g(member)e(name,)i
(type,)g(of)n(fset,)h(access)f(\003ags)g(and)f(documentation)e(string.)
41 b(See)26 b(the)f(\223Generic)0 2442 y(Attrib)n(ute)20
b(Management\224)e(section)i(belo)n(w)f(for)h(details.)0
2588 y(A)26 b(disadv)n(antage)d(of)i(this)g(approach)e(is)j(that)f(it)h
(doesn')o(t)e(pro)o(vide)f(a)i(w)o(ay)h(to)f(restrict)g(the)g(types)g
(of)g(objects)g(that)g(can)g(be)g(assigned)0 2688 y(to)j(the)g(Python)e
(attrib)n(utes.)48 b(W)-7 b(e)29 b(e)o(xpect)e(the)h(\002rst)g(and)g
(last)g(names)g(to)g(be)f(strings,)j(b)n(ut)e(an)o(y)f(Python)f
(objects)i(can)g(be)f(assigned.)0 2788 y(Further)m(,)18
b(the)i(attrib)n(utes)g(can)g(be)f(deleted,)g(setting)h(the)g(C)h
(pointers)e(to)h Fm(NULL)p Ft(.)f(Ev)o(en)g(though)f(we)i(can)g(mak)o
(e)f(sure)h(the)g(members)f(are)0 2887 y(initialized)h(to)g(non-)p
Fm(NULL)e Ft(v)n(alues,)i(the)g(members)f(can)h(be)g(set)h(to)g
Fm(NULL)f Ft(if)g(the)g(attrib)n(utes)g(are)h(deleted.)0
3034 y(W)-7 b(e)21 b(de\002ne)f(a)h(single)f(method,)e
Fm(name)p Ft(,)i(that)g(outputs)g(the)g(objects)g(name)f(as)i(the)f
(concatenation)e(of)i(the)g(\002rst)h(and)f(last)h(names.)p
0 3105 1560 4 v 90 3161 a Fi(1)120 3184 y Fp(This)16
b(is)h(true)h(when)f(we)h(kno)n(w)f(that)h(the)g(object)h(is)e(a)g
(basic)h(type,)g(lik)o(e)g(a)f(string)h(or)f(a)g(\003oat.)90
3242 y Fi(2)120 3265 y Fp(W)-5 b(e)18 b(relied)j(on)e(this)h(in)f(the)h
Fh(tp_dealloc)e Fp(handler)i(in)g(this)g(e)o(xample,)g(because)h(our)f
(type)g(doesn')o(t)g(support)g(garbage)g(collection.)31
b(Ev)o(en)19 b(if)h(a)f(type)h(supports)0 3344 y(garbage)k(collection,)
k(there)c(are)g(calls)g(that)g(can)g(be)f(made)g(to)g(\223untrack\224)j
(the)e(object)h(from)d(garbage)j(collection,)i(ho)n(we)n(v)o(er)m(,)f
(these)e(calls)h(are)e(adv)n(anced)j(and)d(not)0 3423
y(co)o(v)o(ered)c(here.)p 0 5549 3901 4 v 0 5649 a Fo(32)2614
b(Chapter)24 b(2.)52 b(De\002ning)23 b(Ne)n(w)h(T)-10
b(ypes)p eop end
%%Page: 33 39
TeXDict begin 33 38 bop 236 83 a Fj(static)44 b(PyObject)954
96 y(*)236 174 y(Noddy_name\(Noddy)956 187 y(*)1043 174
y(self\))236 266 y({)416 357 y(static)f(PyObject)1133
370 y(*)1178 357 y(format)h(=)g(NULL;)416 448 y(PyObject)819
461 y(*)864 448 y(args,)1133 461 y(*)1178 448 y(result;)416
631 y(if)g(\(format)g(==)g(NULL\))g({)595 722 y(format)g(=)g
(PyString_FromString\("\045s)d(\045s"\);)595 814 y(if)j(\(format)g(==)h
(NULL\))774 905 y(return)f(NULL;)416 996 y(})416 1179
y(if)g(\(self->first)f(==)h(NULL\))g({)595 1270 y
(PyErr_SetString\(PyExc_AttributeError,)38 b("first"\);)595
1362 y(return)44 b(NULL;)416 1453 y(})416 1636 y(if)g(\(self->last)f
(==)i(NULL\))f({)595 1727 y(PyErr_SetString\(PyExc_AttributeError,)38
b("last"\);)595 1818 y(return)44 b(NULL;)416 1910 y(})416
2092 y(args)g(=)g(Py_BuildValue\("OO",)e(self->first,)h(self->last\);)
416 2183 y(if)h(\(args)g(==)h(NULL\))595 2275 y(return)f(NULL;)416
2457 y(result)f(=)i(PyString_Format\(format,)c(args\);)416
2549 y(Py_DECREF\(args\);)416 2731 y(return)i(result;)236
2823 y(})0 3109 y Ft(The)23 b(method)g(is)h(implemented)e(as)i(a)g(C)h
(function)d(that)h(tak)o(es)h(a)g Fm(Noddy)g Ft(\(or)f
Fm(Noddy)g Ft(subclass\))h(instance)f(as)h(the)g(\002rst)g(ar)o
(gument.)0 3209 y(Methods)29 b(al)o(w)o(ays)i(tak)o(e)f(an)f(instance)h
(as)h(the)f(\002rst)g(ar)o(gument.)52 b(Methods)29 b(often)g(tak)o(e)h
(positional)f(and)h(k)o(e)o(yw)o(ord)e(ar)o(guments)g(as)0
3309 y(well,)e(b)n(ut)e(in)g(this)h(cased)f(we)h(don')o(t)e(tak)o(e)h
(an)o(y)f(and)h(don')o(t)f(need)g(to)i(accept)f(a)g(positional)g(ar)o
(gument)e(tuple)h(or)h(k)o(e)o(yw)o(ord)f(ar)o(gument)0
3408 y(dictionary)-5 b(.)23 b(This)d(method)f(is)i(equi)n(v)n(alent)e
(to)h(the)g(Python)f(method:)416 3646 y Fj(def)44 b(name\(self\):)550
3738 y(return)g("\045s)g(\045s")h(\045)f(\(self.first,)f(self.last\))0
4024 y Ft(Note)19 b(that)g(we)g(ha)n(v)o(e)f(to)i(check)e(for)g(the)h
(possibility)f(that)h(our)g Fm(first)f Ft(and)g Fm(last)h
Ft(members)f(are)h Fm(NULL)p Ft(.)f(This)h(is)h(because)f(the)o(y)f
(can)0 4124 y(be)j(deleted,)e(in)i(which)f(case)h(the)o(y)f(are)h(set)g
(to)g Fm(NULL)p Ft(.)f(It)h(w)o(ould)f(be)g(better)h(to)f(pre)n(v)o
(ent)f(deletion)h(of)g(these)h(attrib)n(utes)g(and)f(to)g(restrict)0
4223 y(the)g(attrib)n(ute)g(v)n(alues)g(to)g(be)g(strings.)25
b(W)-7 b(e')o(ll)22 b(see)e(ho)n(w)g(to)g(do)g(that)g(in)h(the)f(ne)o
(xt)f(section.)0 4370 y(No)n(w)h(that)g(we')l(v)o(e)g(de\002ned)f(the)h
(method,)e(we)j(need)f(to)g(create)g(an)g(array)f(of)h(method)f
(de\002nitions:)236 4517 y Fj(static)44 b(PyMethodDef)f
(Noddy_methods[])f(=)j({)416 4608 y({"name",)e
(\(PyCFunction\)Noddy_name,)e(METH_NOARGS,)460 4700 y("Return)j(the)g
(name,)g(combining)g(the)g(first)g(and)g(last)g(name")416
4791 y(},)416 4882 y({NULL})88 b(/)819 4895 y(*)909 4882
y(Sentinel)1312 4895 y(*)1357 4882 y(/)236 4974 y(};)0
5260 y Ft(and)20 b(assign)g(them)g(to)g(the)g Fm(tp_methods)f
Ft(slot:)p 0 5549 3901 4 v 0 5649 a Fo(2.1.)52 b(The)23
b(Basics)3207 b(33)p eop end
%%Page: 34 40
TeXDict begin 34 39 bop 416 174 a Fj(Noddy_methods,)580
b(/)1671 187 y(*)1761 174 y(tp_methods)2254 187 y(*)2299
174 y(/)0 461 y Ft(Note)20 b(that)g(we)h(used)f(the)g
Fm(METH_NOARGS)f Ft(\003ag)h(to)g(indicate)g(that)g(the)h(method)d(is)k
(passed)e(no)f(ar)o(guments.)0 608 y(Finally)-5 b(,)18
b(we')o(ll)h(mak)o(e)f(our)g(type)g(usable)h(as)g(a)g(base)g(class.)25
b(W)-7 b(e')l(v)o(e)19 b(written)f(our)g(methods)g(carefully)f(so)i(f)o
(ar)f(so)h(that)g(the)o(y)f(don')o(t)f(mak)o(e)0 707
y(an)o(y)g(assumptions)g(about)g(the)h(type)f(of)h(the)f(object)h
(being)f(created)g(or)g(used,)h(so)g(all)h(we)f(need)f(to)h(do)f(is)i
(to)f(add)f(the)h Fm(Py_TPFLAGS_-)0 807 y(BASETYPE)h
Ft(to)i(our)e(class)i(\003ag)f(de\002nition:)416 1045
y Fj(Py_TPFLAGS_DEFAULT)41 b(|)k(Py_TPFLAGS_BASETYPE,)c(/)2343
1058 y(*)2388 1045 y(tp_flags)2748 1058 y(*)2793 1045
y(/)0 1332 y Ft(W)-7 b(e)21 b(rename)e Fm(initnoddy\(\))g
Ft(to)h Fm(initnoddy2\(\))f Ft(and)h(update)f(the)h(module)f(name)g
(passed)h(to)h Fm(Py_InitModule3\(\))p Ft(.)0 1479 y(Finally)-5
b(,)19 b(we)i(update)e(our)g(`)p Fu(setup)m(.p)n(y)p
Ft(')g(\002le)i(to)f(b)n(uild)g(the)g(ne)n(w)g(module:)236
1625 y Fj(from)44 b(distutils.core)f(import)h(setup,)g(Extension)236
1717 y(setup\(name="noddy",)e(version="1.0",)505 1808
y(ext_modules=[)640 1899 y(Extension\("noddy",)g(["noddy.c"]\),)640
1991 y(Extension\("noddy2",)f(["noddy2.c"]\),)640 2082
y(]\))0 2506 y Fg(2.1.2)100 b(Pro)o(viding)30 b(\002ner)e(control)h(o)o
(v)n(er)e(data)i(attr)q(ib)n(utes)0 2709 y Ft(In)19 b(this)h(section,)f
(we')o(ll)h(pro)o(vide)d(\002ner)i(control)f(o)o(v)o(er)g(ho)n(w)h(the)
g Fm(first)g Ft(and)g Fm(last)g Ft(attrib)n(utes)g(are)h(set)g(in)f
(the)h Fm(Noddy)f Ft(e)o(xample.)k(In)0 2809 y(the)d(pre)n(vious)e(v)o
(ersion)h(of)g(our)h(module,)e(the)i(instance)f(v)n(ariables)h
Fm(first)f Ft(and)g Fm(last)h Ft(could)f(be)h(set)h(to)f(non-string)d
(v)n(alues)j(or)g(e)n(v)o(en)0 2908 y(deleted.)k(W)-7
b(e)22 b(w)o(ant)e(to)g(mak)o(e)g(sure)g(that)g(these)h(attrib)n(utes)f
(al)o(w)o(ays)g(contain)g(strings.)236 3097 y Fj(#include)44
b(<Python.h>)236 3188 y(#include)g("structmember.h")236
3371 y(typedef)g(struct)g({)416 3462 y(PyObject_HEAD)416
3553 y(PyObject)819 3566 y(*)864 3553 y(first;)416 3645
y(PyObject)819 3658 y(*)864 3645 y(last;)416 3736 y(int)g(number;)236
3827 y(})h(Noddy;)236 4010 y(static)f(void)236 4101 y
(Noddy_dealloc\(Noddy)1091 4114 y(*)1178 4101 y(self\))236
4193 y({)416 4284 y(Py_XDECREF\(self->first\);)416 4375
y(Py_XDECREF\(self->last\);)416 4467 y
(self->ob_type->tp_free\(\(PyObject)1856 4480 y(*)1901
4467 y(\)se)o(lf\);)236 4558 y(})236 4741 y(static)g(PyObject)954
4754 y(*)236 4832 y(Noddy_new\(PyTypeObject)1267 4845
y(*)1312 4832 y(type,)g(PyObject)1985 4845 y(*)2030 4832
y(args,)f(PyObject)2702 4845 y(*)2747 4832 y(kwds\))236
4923 y({)416 5014 y(Noddy)685 5027 y(*)730 5014 y(self;)416
5197 y(self)h(=)g(\(Noddy)1043 5210 y(*)1088 5197 y
(\)type->tp_alloc\(type,)d(0\);)416 5288 y(if)j(\(self)g(!=)h(NULL\))f
({)595 5380 y(self->first)f(=)i(PyString_FromString\(""\);)p
0 5549 3901 4 v 0 5649 a Fo(34)2614 b(Chapter)24 b(2.)52
b(De\002ning)23 b(Ne)n(w)h(T)-10 b(ypes)p eop end
%%Page: 35 41
TeXDict begin 35 40 bop 595 83 a Fj(if)44 b(\(self->first)f(==)i
(NULL\))685 174 y({)774 266 y(Py_DECREF\(self\);)774
357 y(return)f(NULL;)685 448 y(})595 631 y(self->last)f(=)i
(PyString_FromString\(""\);)595 722 y(if)f(\(self->last)f(==)i(NULL\))
685 814 y({)774 905 y(Py_DECREF\(self\);)774 996 y(return)f(NULL;)685
1088 y(})595 1270 y(self->number)f(=)h(0;)416 1362 y(})416
1544 y(return)f(\(PyObject)1178 1557 y(*)1223 1544 y(\)self;)236
1636 y(})236 1818 y(static)h(int)236 1910 y(Noddy_init\(Noddy)998
1923 y(*)1043 1910 y(self,)g(PyObject)1716 1923 y(*)1761
1910 y(args,)f(PyObject)2433 1923 y(*)2478 1910 y(kwds\))236
2001 y({)416 2092 y(PyObject)819 2105 y(*)864 2092 y(first=NULL,)1402
2105 y(*)1447 2092 y(last=NULL,)1940 2105 y(*)1985 2092
y(tmp;)416 2275 y(static)g(char)954 2288 y(*)999 2275
y(kwlist[])g(=)i({"first",)e("last",)g("number",)h(NULL};)416
2457 y(if)g(\(!)h(PyArg_ParseTupleAndKeywords\(args,)39
b(kwds,)44 b("|SSi",)f(kwlist,)1940 2549 y(&first,)g(&last,)1940
2640 y(&self->number\)\))595 2731 y(return)h(-1;)416
2914 y(if)g(\(first\))g({)595 3005 y(tmp)g(=)h(self->first;)595
3097 y(Py_INCREF\(first\);)595 3188 y(self->first)e(=)i(first;)595
3279 y(Py_DECREF\(tmp\);)416 3371 y(})416 3553 y(if)f(\(last\))g({)595
3645 y(tmp)g(=)h(self->last;)595 3736 y(Py_INCREF\(last\);)595
3827 y(self->last)e(=)i(last;)595 3919 y(Py_DECREF\(tmp\);)416
4010 y(})416 4193 y(return)e(0;)236 4284 y(})236 4467
y(static)h(PyMemberDef)f(Noddy_members[])f(=)j({)416
4558 y({"number",)e(T_INT,)h(offsetof\(Noddy,)e(number\),)h(0,)460
4649 y("noddy)h(number"},)416 4741 y({NULL})88 b(/)819
4754 y(*)909 4741 y(Sentinel)1312 4754 y(*)1357 4741
y(/)236 4832 y(};)236 5015 y(static)44 b(PyObject)954
5028 y(*)236 5106 y(Noddy_getfirst\(Noddy)1178 5119 y(*)1223
5106 y(self,)g(void)1716 5119 y(*)1761 5106 y(closure\))236
5197 y({)416 5289 y(Py_INCREF\(self->first\);)416 5380
y(return)f(self->first;)p 0 5549 3901 4 v 0 5649 a Fo(2.1.)52
b(The)23 b(Basics)3207 b(35)p eop end
%%Page: 36 42
TeXDict begin 36 41 bop 236 83 a Fj(})236 266 y(static)44
b(int)236 357 y(Noddy_setfirst\(Noddy)1178 370 y(*)1223
357 y(self,)g(PyObject)1895 370 y(*)1940 357 y(value,)g(void)2478
370 y(*)2523 357 y(closure\))236 448 y({)326 540 y(if)g(\(value)g(==)h
(NULL\))f({)416 631 y(PyErr_SetString\(PyExc_TypeError,)39
b("Cannot)44 b(delete)f(the)i(first)f(attribute"\);)416
722 y(return)f(-1;)326 814 y(})326 996 y(if)h(\(!)h
(PyString_Check\(value\)\))c({)416 1088 y
(PyErr_SetString\(PyExc_TypeError,)1133 1179 y("The)j(first)g
(attribute)f(value)h(must)g(be)h(a)g(string"\);)416 1270
y(return)e(-1;)326 1362 y(})326 1544 y(Py_DECREF\(self->first\);)326
1636 y(Py_INCREF\(value\);)326 1727 y(self->first)g(=)i(value;)326
1910 y(return)f(0;)236 2001 y(})236 2183 y(static)g(PyObject)954
2196 y(*)236 2275 y(Noddy_getlast\(Noddy)1133 2288 y(*)1178
2275 y(self,)g(void)1671 2288 y(*)1716 2275 y(closure\))236
2366 y({)416 2457 y(Py_INCREF\(self->last\);)416 2549
y(return)f(self->last;)236 2640 y(})236 2823 y(static)h(int)236
2914 y(Noddy_setlast\(Noddy)1133 2927 y(*)1178 2914 y(self,)g(PyObject)
1850 2927 y(*)1895 2914 y(value,)g(void)2433 2927 y(*)2478
2914 y(closure\))236 3005 y({)326 3097 y(if)g(\(value)g(==)h(NULL\))f
({)416 3188 y(PyErr_SetString\(PyExc_TypeError,)39 b("Cannot)44
b(delete)f(the)i(last)f(attribute"\);)416 3279 y(return)f(-1;)326
3371 y(})326 3553 y(if)h(\(!)h(PyString_Check\(value\)\))c({)416
3645 y(PyErr_SetString\(PyExc_TypeError,)1133 3736 y("The)j(last)g
(attribute)f(value)h(must)h(be)f(a)h(string"\);)416 3827
y(return)e(-1;)326 3919 y(})326 4101 y(Py_DECREF\(self->last\);)326
4193 y(Py_INCREF\(value\);)326 4284 y(self->last)g(=)i(value;)326
4467 y(return)f(0;)236 4558 y(})236 4741 y(static)g(PyGetSetDef)f
(Noddy_getseters[])f(=)j({)416 4832 y({"first",)460 4923
y(\(getter\)Noddy_getfirst,)c(\(setter\)Noddy_setfirst,)460
5015 y("first)j(name",)460 5106 y(NULL},)416 5197 y({"last",)460
5289 y(\(getter\)Noddy_getlast,)e(\(setter\)Noddy_setlast,)460
5380 y("last)i(name",)p 0 5549 3901 4 v 0 5649 a Fo(36)2614
b(Chapter)24 b(2.)52 b(De\002ning)23 b(Ne)n(w)h(T)-10
b(ypes)p eop end
%%Page: 37 43
TeXDict begin 37 42 bop 460 83 a Fj(NULL},)416 174 y({NULL})88
b(/)819 187 y(*)909 174 y(Sentinel)1312 187 y(*)1357
174 y(/)236 266 y(};)236 448 y(static)44 b(PyObject)954
461 y(*)236 540 y(Noddy_name\(Noddy)956 553 y(*)1043
540 y(self\))236 631 y({)416 722 y(static)f(PyObject)1133
735 y(*)1178 722 y(format)h(=)g(NULL;)416 814 y(PyObject)819
827 y(*)864 814 y(args,)1133 827 y(*)1178 814 y(result;)416
996 y(if)g(\(format)g(==)g(NULL\))g({)595 1088 y(format)g(=)g
(PyString_FromString\("\045s)d(\045s"\);)595 1179 y(if)j(\(format)g(==)
h(NULL\))774 1270 y(return)f(NULL;)416 1362 y(})416 1544
y(args)g(=)g(Py_BuildValue\("OO",)e(self->first,)h(self->last\);)416
1636 y(if)h(\(args)g(==)h(NULL\))595 1727 y(return)f(NULL;)416
1910 y(result)f(=)i(PyString_Format\(format,)c(args\);)416
2001 y(Py_DECREF\(args\);)416 2183 y(return)i(result;)236
2275 y(})236 2457 y(static)h(PyMethodDef)f(Noddy_methods[])f(=)j({)416
2549 y({"name",)e(\(PyCFunction\)Noddy_name,)e(METH_NOARGS,)460
2640 y("Return)j(the)g(name,)g(combining)g(the)g(first)g(and)g(last)g
(name")416 2731 y(},)416 2823 y({NULL})88 b(/)819 2836
y(*)909 2823 y(Sentinel)1312 2836 y(*)1357 2823 y(/)236
2914 y(};)236 3097 y(static)44 b(PyTypeObject)f(NoddyType)g(=)i({)416
3188 y(PyObject_HEAD_INIT\(NULL\))416 3279 y(0,)1120
b(/)1671 3292 y(*)1716 3279 y(ob_size)2031 3292 y(*)2076
3279 y(/)416 3371 y("noddy.Noddy",)580 b(/)1671 3384
y(*)1716 3371 y(tp_name)2031 3384 y(*)2076 3371 y(/)416
3462 y(sizeof\(Noddy\),)g(/)1671 3475 y(*)1716 3462 y(tp_basicsize)2256
3475 y(*)2301 3462 y(/)416 3553 y(0,)1120 b(/)1671 3566
y(*)1716 3553 y(tp_itemsize)2211 3566 y(*)2256 3553 y(/)416
3645 y(\(destructor\)Noddy_dealloc,)40 b(/)1671 3658
y(*)1716 3645 y(tp_dealloc)2166 3658 y(*)2211 3645 y(/)416
3736 y(0,)1120 b(/)1671 3749 y(*)1716 3736 y(tp_print)2076
3749 y(*)2121 3736 y(/)416 3827 y(0,)g(/)1671 3840 y(*)1716
3827 y(tp_getattr)2166 3840 y(*)2211 3827 y(/)416 3919
y(0,)g(/)1671 3932 y(*)1716 3919 y(tp_setattr)2166 3932
y(*)2211 3919 y(/)416 4010 y(0,)g(/)1671 4023 y(*)1716
4010 y(tp_compare)2166 4023 y(*)2211 4010 y(/)416 4101
y(0,)g(/)1671 4114 y(*)1716 4101 y(tp_repr)2031 4114
y(*)2076 4101 y(/)416 4193 y(0,)g(/)1671 4206 y(*)1716
4193 y(tp_as_number)2256 4206 y(*)2301 4193 y(/)416 4284
y(0,)g(/)1671 4297 y(*)1716 4284 y(tp_as_sequence)2346
4297 y(*)2391 4284 y(/)416 4375 y(0,)g(/)1671 4388 y(*)1716
4375 y(tp_as_mapping)2301 4388 y(*)2346 4375 y(/)416
4467 y(0,)g(/)1671 4480 y(*)1716 4467 y(tp_hash)2074
4480 y(*)2119 4467 y(/)416 4558 y(0,)g(/)1671 4571 y(*)1716
4558 y(tp_call)2031 4571 y(*)2076 4558 y(/)416 4649 y(0,)g(/)1671
4662 y(*)1716 4649 y(tp_str)1986 4662 y(*)2031 4649 y(/)416
4741 y(0,)g(/)1671 4754 y(*)1716 4741 y(tp_getattro)2211
4754 y(*)2256 4741 y(/)416 4832 y(0,)g(/)1671 4845 y(*)1716
4832 y(tp_setattro)2211 4845 y(*)2256 4832 y(/)416 4923
y(0,)g(/)1671 4936 y(*)1716 4923 y(tp_as_buffer)2256
4936 y(*)2301 4923 y(/)416 5015 y(Py_TPFLAGS_DEFAULT)41
b(|)k(Py_TPFLAGS_BASETYPE,)c(/)2343 5028 y(*)2388 5015
y(tp_flags)2748 5028 y(*)2793 5015 y(/)416 5106 y("Noddy)i(objects",)
492 b(/)1671 5119 y(*)1761 5106 y(tp_doc)2074 5119 y(*)2119
5106 y(/)416 5197 y(0,)672 b(/)1223 5210 y(*)1312 5197
y(tp_traverse)1850 5210 y(*)1895 5197 y(/)416 5289 y(0,)g(/)1223
5302 y(*)1312 5289 y(tp_clear)1716 5302 y(*)1761 5289
y(/)416 5380 y(0,)g(/)1223 5393 y(*)1312 5380 y(tp_richcompare)1985
5393 y(*)2030 5380 y(/)p 0 5549 3901 4 v 0 5649 a Fo(2.1.)52
b(The)23 b(Basics)3207 b(37)p eop end
%%Page: 38 44
TeXDict begin 38 43 bop 416 83 a Fj(0,)672 b(/)1223 96
y(*)1312 83 y(tp_weaklistoffset)2119 96 y(*)2164 83 y(/)416
174 y(0,)g(/)1223 187 y(*)1312 174 y(tp_iter)1671 187
y(*)1716 174 y(/)416 266 y(0,)g(/)1223 279 y(*)1312 266
y(tp_iternext)1850 279 y(*)1895 266 y(/)416 357 y(Noddy_methods,)580
b(/)1671 370 y(*)1761 357 y(tp_methods)2254 370 y(*)2299
357 y(/)416 448 y(Noddy_members,)g(/)1671 461 y(*)1761
448 y(tp_members)2254 461 y(*)2299 448 y(/)416 540 y(Noddy_getseters,)
490 b(/)1671 553 y(*)1761 540 y(tp_getset)2209 553 y(*)2254
540 y(/)416 631 y(0,)1120 b(/)1671 644 y(*)1761 631 y(tp_base)2119
644 y(*)2164 631 y(/)416 722 y(0,)g(/)1671 735 y(*)1761
722 y(tp_dict)2119 735 y(*)2164 722 y(/)416 814 y(0,)g(/)1671
827 y(*)1761 814 y(tp_descr_get)2343 827 y(*)2388 814
y(/)416 905 y(0,)g(/)1671 918 y(*)1761 905 y(tp_descr_set)2343
918 y(*)2388 905 y(/)416 996 y(0,)g(/)1671 1009 y(*)1761
996 y(tp_dictoffset)2388 1009 y(*)2433 996 y(/)416 1088
y(\(initproc\)Noddy_init,)265 b(/)1671 1101 y(*)1761
1088 y(tp_init)2119 1101 y(*)2164 1088 y(/)416 1179 y(0,)1120
b(/)1671 1192 y(*)1761 1179 y(tp_alloc)2164 1192 y(*)2209
1179 y(/)416 1270 y(Noddy_new,)760 b(/)1671 1283 y(*)1761
1270 y(tp_new)2074 1283 y(*)2119 1270 y(/)236 1362 y(};)236
1544 y(static)44 b(PyMethodDef)f(module_methods[])f(=)j({)416
1636 y({NULL})88 b(/)819 1649 y(*)909 1636 y(Sentinel)1312
1649 y(*)1357 1636 y(/)236 1727 y(};)236 1910 y(#ifndef)44
b(PyMODINIT_FUNC)e(/)1312 1923 y(*)1402 1910 y(declarations)h(for)h
(DLL)g(import/export)2971 1923 y(*)3016 1910 y(/)236
2001 y(#define)g(PyMODINIT_FUNC)e(void)236 2092 y(#endif)236
2183 y(PyMODINIT_FUNC)236 2275 y(initnoddy3\(void\))236
2366 y({)416 2457 y(PyObject)776 2470 y(*)864 2457 y(m;)416
2640 y(if)i(\(PyType_Ready\(&NoddyType\))d(<)j(0\))595
2731 y(return;)416 2914 y(m)g(=)h(Py_InitModule3\("noddy3",)c
(module_methods,)1267 3005 y("Example)j(module)g(that)g(creates)f(an)i
(extension)e(type."\);)416 3188 y(if)h(\(m)h(==)f(NULL\))505
3279 y(return;)416 3462 y(Py_INCREF\(&NoddyType\);)416
3553 y(PyModule_AddObject\(m,)d("Noddy",)i(\(PyObject)2254
3566 y(*)2299 3553 y(\)&NoddyType\);)236 3645 y(})0 3841
y Ft(T)-7 b(o)24 b(pro)o(vide)e(greater)h(control,)h(o)o(v)o(er)e(the)i
Fm(first)g Ft(and)g Fm(last)f Ft(attrib)n(utes,)i(we')o(ll)f(use)h
(custom)e(getter)h(and)f(setter)i(functions.)35 b(Here)0
3941 y(are)20 b(the)g(functions)f(for)h(getting)f(and)h(setting)g(the)g
Fm(first)g Ft(attrib)n(ute:)p 0 5549 3901 4 v 0 5649
a Fo(38)2614 b(Chapter)24 b(2.)52 b(De\002ning)23 b(Ne)n(w)h(T)-10
b(ypes)p eop end
%%Page: 39 45
TeXDict begin 39 44 bop 236 174 a Fj(Noddy_getfirst\(Noddy)1178
187 y(*)1223 174 y(self,)44 b(void)1716 187 y(*)1761
174 y(closure\))236 266 y({)416 357 y(Py_INCREF\(self->first\);)416
448 y(return)f(self->first;)236 540 y(})236 722 y(static)h(int)236
814 y(Noddy_setfirst\(Noddy)1178 827 y(*)1223 814 y(self,)g(PyObject)
1895 827 y(*)1940 814 y(value,)g(void)2478 827 y(*)2523
814 y(closure\))236 905 y({)326 996 y(if)g(\(value)g(==)h(NULL\))f({)
416 1088 y(PyErr_SetString\(PyExc_TypeError,)39 b("Cannot)44
b(delete)f(the)i(first)f(attribute"\);)416 1179 y(return)f(-1;)326
1270 y(})326 1453 y(if)h(\(!)h(PyString_Check\(value\)\))c({)416
1544 y(PyErr_SetString\(PyExc_TypeError,)1133 1636 y("The)j(first)g
(attribute)f(value)h(must)g(be)h(a)g(string"\);)416 1727
y(return)e(-1;)326 1818 y(})326 2001 y(Py_DECREF\(self->first\);)326
2092 y(Py_INCREF\(value\);)326 2183 y(self->first)g(=)i(value;)326
2366 y(return)f(0;)236 2457 y(})0 2744 y Ft(The)16 b(getter)g(function)
f(is)j(passed)e(a)h Fm(Noddy)g Ft(object)f(and)g(a)h(\223closure\224,)f
(which)g(is)i(v)n(oid)e(pointer)-5 b(.)23 b(In)16 b(this)h(case,)h(the)
e(closure)g(is)i(ignored.)0 2844 y(\(The)25 b(closure)h(supports)e(an)i
(adv)n(anced)e(usage)i(in)g(which)f(de\002nition)g(data)h(is)h(passed)f
(to)g(the)g(getter)f(and)h(setter)-5 b(.)43 b(This)26
b(could,)g(for)0 2943 y(e)o(xample,)18 b(be)i(used)g(to)g(allo)n(w)g(a)
g(single)g(set)h(of)f(getter)f(and)h(setter)g(functions)e(that)i
(decide)g(the)g(attrib)n(ute)f(to)h(get)g(or)g(set)h(based)e(on)h(data)
0 3043 y(in)g(the)h(closure.\))0 3190 y(The)c(setter)i(function)d(is)j
(passed)e(the)h Fm(Noddy)g Ft(object,)f(the)h(ne)n(w)f(v)n(alue,)h(and)
f(the)h(closure.)23 b(The)18 b(ne)n(w)g(v)n(alue)f(may)g(be)h
Fm(NULL)p Ft(,)f(in)h(which)0 3289 y(case)24 b(the)f(attrib)n(ute)g(is)
h(being)e(deleted.)33 b(In)23 b(our)g(setter)m(,)g(we)h(raise)f(an)h
(error)e(if)h(the)g(attrib)n(ute)g(is)h(deleted)f(or)g(if)g(the)g
(attrib)n(ute)g(v)n(alue)g(is)0 3389 y(not)d(a)g(string.)0
3536 y(W)-7 b(e)21 b(create)f(an)g(array)g(of)g Fm(PyGetSetDef)e
Ft(structures:)236 3774 y Fj(static)44 b(PyGetSetDef)f
(Noddy_getseters[])f(=)j({)416 3865 y({"first",)460 3957
y(\(getter\)Noddy_getfirst,)c(\(setter\)Noddy_setfirst,)460
4048 y("first)j(name",)460 4139 y(NULL},)416 4231 y({"last",)460
4322 y(\(getter\)Noddy_getlast,)e(\(setter\)Noddy_setlast,)460
4413 y("last)i(name",)460 4505 y(NULL},)416 4596 y({NULL})88
b(/)819 4609 y(*)909 4596 y(Sentinel)1312 4609 y(*)1357
4596 y(/)236 4687 y(};)0 4974 y Ft(and)20 b(re)o(gister)f(it)i(in)f
(the)h Fm(tp_getset)e Ft(slot:)416 5212 y Fj(Noddy_getseters,)490
b(/)1671 5225 y(*)1761 5212 y(tp_getset)2209 5225 y(*)2254
5212 y(/)p 0 5549 3901 4 v 0 5649 a Fo(2.1.)52 b(The)23
b(Basics)3207 b(39)p eop end
%%Page: 40 46
TeXDict begin 40 45 bop 0 83 a Ft(to)20 b(re)o(gister)g(out)g(attrib)n
(ute)f(getters)i(and)e(setters.)0 230 y(The)i(last)h(item)f(in)h(a)f
Fm(PyGetSetDef)f Ft(structure)g(is)i(the)g(closure)e(mentioned)f(abo)o
(v)o(e.)27 b(In)21 b(this)g(case,)h(we)g(aren')o(t)e(using)g(the)h
(closure,)0 330 y(so)g(we)f(just)h(pass)g Fm(NULL)p Ft(.)0
476 y(W)-7 b(e)21 b(also)g(remo)o(v)o(e)d(the)i(member)f(de\002nitions)
g(for)h(these)g(attrib)n(utes:)236 623 y Fj(static)44
b(PyMemberDef)f(Noddy_members[])f(=)j({)416 715 y({"number",)e(T_INT,)h
(offsetof\(Noddy,)e(number\),)h(0,)460 806 y("noddy)h(number"},)416
897 y({NULL})88 b(/)819 910 y(*)909 897 y(Sentinel)1312
910 y(*)1357 897 y(/)236 989 y(};)0 1288 y Ft(W)-7 b(e)21
b(also)g(need)e(to)i(update)e(the)h Fm(tp_init)f Ft(handler)g(to)i
(only)e(allo)n(w)h(strings)2229 1258 y Fk(3)2282 1288
y Ft(to)h(be)f(passed:)236 1526 y Fj(static)44 b(int)236
1618 y(Noddy_init\(Noddy)998 1631 y(*)1043 1618 y(self,)g(PyObject)1716
1631 y(*)1761 1618 y(args,)f(PyObject)2433 1631 y(*)2478
1618 y(kwds\))236 1709 y({)416 1800 y(PyObject)819 1813
y(*)864 1800 y(first=NULL,)1402 1813 y(*)1447 1800 y(last=NULL,)1940
1813 y(*)1985 1800 y(tmp;)416 1983 y(static)g(char)954
1996 y(*)999 1983 y(kwlist[])g(=)i({"first",)e("last",)g("number",)h
(NULL};)416 2166 y(if)g(\(!)h(PyArg_ParseTupleAndKeywords\(args,)39
b(kwds,)44 b("|SSi",)f(kwlist,)1940 2257 y(&first,)g(&last,)1940
2348 y(&self->number\)\))595 2440 y(return)h(-1;)416
2622 y(if)g(\(first\))g({)595 2714 y(tmp)g(=)h(self->first;)595
2805 y(Py_INCREF\(first\);)595 2896 y(self->first)e(=)i(first;)595
2988 y(Py_DECREF\(tmp\);)416 3079 y(})416 3262 y(if)f(\(last\))g({)595
3353 y(tmp)g(=)h(self->last;)595 3444 y(Py_INCREF\(last\);)595
3536 y(self->last)e(=)i(last;)595 3627 y(Py_DECREF\(tmp\);)416
3718 y(})416 3901 y(return)e(0;)236 3992 y(})0 4279 y
Ft(W)m(ith)27 b(these)g(changes,)g(we)g(can)g(assure)g(that)f(the)h
Fm(first)f Ft(and)h Fm(last)f Ft(members)g(are)h(ne)n(v)o(er)e
Fm(NULL)i Ft(so)g(we)g(can)f(remo)o(v)o(e)f(checks)0
4378 y(for)f Fm(NULL)f Ft(v)n(alues)h(in)h(almost)f(all)g(cases.)38
b(This)24 b(means)g(that)g(most)h(of)e(the)i Fm(Py_XDECREF\(\))d
Ft(calls)j(can)f(be)g(con)m(v)o(erted)d(to)k Fm(Py_-)0
4478 y(DECREF\(\))d Ft(calls.)33 b(The)23 b(only)e(place)i(we)g(can')o
(t)f(change)f(these)i(calls)h(is)f(in)g(the)g(deallocator)m(,)e(where)h
(there)h(is)g(the)g(possibility)f(that)0 4578 y(the)e(initialization)g
(of)g(these)g(members)f(f)o(ailed)h(in)g(the)h(constructor)-5
b(.)0 4724 y(W)e(e)26 b(also)f(rename)e(the)i(module)e(initialization)h
(function)f(and)h(module)f(name)h(in)h(the)g(initialization)f
(function,)g(as)h(we)g(did)f(before,)0 4824 y(and)c(we)g(add)g(an)g(e)o
(xtra)f(de\002nition)g(to)i(the)f(`)p Fu(setup)m(.p)n(y)p
Ft(')f(\002le.)p 0 4893 1560 4 v 90 4949 a Fi(3)120 4972
y Fp(W)-5 b(e)24 b(no)n(w)h(kno)n(w)g(that)h(the)g(\002rst)f(and)g
(last)h(members)f(are)g(strings,)i(so)e(perhaps)h(we)f(could)h(be)f
(less)g(careful)h(about)g(decrementing)i(their)f(reference)g(counts,)0
5051 y(ho)n(we)n(v)o(er)m(,)18 b(we)d(accept)j(instances)g(of)d(string)
i(subclasses.)k(Ev)o(en)16 b(though)g(deallocating)k(normal)c(strings)h
(w)o(on')o(t)f(call)h(back)f(into)h(our)f(objects,)h(we)e(can')o(t)i
(guarantee)h(that)0 5130 y(deallocating)j(an)d(instance)h(of)e(a)g
(string)h(subclass)g(w)o(on')o(t.)j(call)d(back)g(into)g(out)g
(objects.)p 0 5549 3901 4 v 0 5649 a Fo(40)2614 b(Chapter)24
b(2.)52 b(De\002ning)23 b(Ne)n(w)h(T)-10 b(ypes)p eop
end
%%Page: 41 47
TeXDict begin 41 46 bop 0 83 a Fg(2.1.3)100 b(Suppor)t(ting)31
b(cyclic)c(garbage)j(collection)0 286 y Ft(Python)16
b(has)i(a)g(c)o(yclic-garbage)c(collector)i(that)i(can)f(identify)f
(unneeded)g(objects)h(e)n(v)o(en)f(when)h(their)g(reference)f(counts)h
(are)g(not)g(zero.)0 386 y(This)j(can)g(happen)f(when)g(objects)h(are)h
(in)m(v)n(olv)o(ed)d(in)i(c)o(ycles.)25 b(F)o(or)20 b(e)o(xample,)e
(consider:)236 624 y Fj(>>>)45 b(l)f(=)h([])236 715 y(>>>)g
(l.append\(l\))236 806 y(>>>)g(del)f(l)0 1093 y Ft(In)20
b(this)g(e)o(xample,)e(we)j(create)e(a)i(list)g(that)f(contains)f
(itself.)26 b(When)19 b(we)i(delete)e(it,)i(it)f(still)i(has)e(a)g
(reference)e(from)h(itself.)26 b(Its)20 b(reference)0
1193 y(count)27 b(doesn')o(t)f(drop)g(to)i(zero.)47 b(F)o(ortunately)-5
b(,)27 b(Python')-5 b(s)26 b(c)o(yclic-garbage)f(collector)h(will)j(e)n
(v)o(entually)c(\002gure)i(out)h(that)f(the)h(list)h(is)0
1292 y(garbage)18 b(and)i(free)g(it.)0 1439 y(In)28 b(the)h(second)e(v)
o(ersion)g(of)h(the)h Fm(Noddy)f Ft(e)o(xample,)g(we)h(allo)n(wed)f(an)
o(y)f(kind)h(of)g(object)g(to)h(be)f(stored)g(in)g(the)h
Fm(first)f Ft(or)g Fm(last)0 1539 y Ft(attrib)n(utes.)329
1509 y Fk(4)387 1539 y Ft(This)20 b(means)g(that)h Fm(Noddy)e
Ft(objects)h(can)g(participate)g(in)g(c)o(ycles:)236
1777 y Fj(>>>)45 b(import)e(noddy2)236 1868 y(>>>)i(n)f(=)h
(noddy2.Noddy\(\))236 1960 y(>>>)g(l)f(=)h([n])236 2051
y(>>>)g(n.first)e(=)i(l)0 2337 y Ft(This)20 b(is)h(pretty)e(silly)-5
b(,)20 b(b)n(ut)g(it)g(gi)n(v)o(es)g(us)g(an)g(e)o(xcuse)f(to)h(add)f
(support)g(for)g(the)h(c)o(yclic-garbage)d(collector)i(to)h(the)f
Fm(Noddy)h Ft(e)o(xample.)j(T)-7 b(o)0 2437 y(support)19
b(c)o(yclic)h(garbage)e(collection,)h(types)h(need)f(to)h(\002ll)i(tw)o
(o)e(slots)h(and)f(set)h(a)f(class)h(\003ag)g(that)f(enables)g(these)g
(slots:)236 2625 y Fj(#include)44 b(<Python.h>)236 2717
y(#include)g("structmember.h")236 2899 y(typedef)g(struct)g({)416
2991 y(PyObject_HEAD)416 3082 y(PyObject)819 3095 y(*)864
3082 y(first;)416 3173 y(PyObject)819 3186 y(*)864 3173
y(last;)416 3265 y(int)g(number;)236 3356 y(})h(Noddy;)236
3539 y(static)f(int)236 3630 y(Noddy_traverse\(Noddy)1178
3643 y(*)1223 3630 y(self,)g(visitproc)f(visit,)h(void)2478
3643 y(*)2523 3630 y(arg\))236 3721 y({)416 3813 y(int)g(vret;)416
3995 y(if)g(\(self->first\))f({)595 4087 y(vret)h(=)h
(visit\(self->first,)d(arg\);)595 4178 y(if)i(\(vret)g(!=)h(0\))774
4269 y(return)f(vret;)416 4361 y(})416 4452 y(if)g(\(self->last\))f({)
595 4543 y(vret)h(=)h(visit\(self->last,)d(arg\);)595
4634 y(if)i(\(vret)g(!=)h(0\))774 4726 y(return)f(vret;)416
4817 y(})416 5000 y(return)f(0;)236 5091 y(})p 0 5236
1560 4 v 90 5291 a Fi(4)120 5315 y Fp(Ev)o(en)20 b(in)h(the)h(third)f
(v)o(ersion,)i(we)d(aren')o(t)i(guaranteed)i(to)d(a)o(v)o(oid)g(c)o
(ycles.)33 b(Instances)22 b(of)f(string)h(subclasses)f(are)h(allo)n
(wed)h(and)e(string)g(subclasses)h(could)g(allo)n(w)0
5393 y(c)o(ycles)d(e)n(v)o(en)f(if)f(normal)h(strings)g(don')o(t.)p
0 5549 3901 4 v 0 5649 a Fo(2.1.)52 b(The)23 b(Basics)3207
b(41)p eop end
%%Page: 42 48
TeXDict begin 42 47 bop 236 83 a Fj(static)44 b(int)236
174 y(Noddy_clear\(Noddy)1043 187 y(*)1088 174 y(self\))236
266 y({)416 357 y(PyObject)819 370 y(*)864 357 y(tmp;)416
540 y(tmp)g(=)h(self->first;)416 631 y(self->first)e(=)h(NULL;)416
722 y(Py_XDECREF\(tmp\);)416 905 y(tmp)g(=)h(self->last;)416
996 y(self->last)e(=)h(NULL;)416 1088 y(Py_XDECREF\(tmp\);)416
1270 y(return)f(0;)236 1362 y(})236 1544 y(static)h(void)236
1636 y(Noddy_dealloc\(Noddy)1091 1649 y(*)1178 1636 y(self\))236
1727 y({)416 1818 y(Noddy_clear\(self\);)416 1910 y
(self->ob_type->tp_free\(\(PyObject)1856 1923 y(*)1901
1910 y(\)se)o(lf\);)236 2001 y(})236 2183 y(static)g(PyObject)954
2196 y(*)236 2275 y(Noddy_new\(PyTypeObject)1267 2288
y(*)1312 2275 y(type,)g(PyObject)1985 2288 y(*)2030 2275
y(args,)f(PyObject)2702 2288 y(*)2747 2275 y(kwds\))236
2366 y({)416 2457 y(Noddy)685 2470 y(*)730 2457 y(self;)416
2640 y(self)h(=)g(\(Noddy)1043 2653 y(*)1088 2640 y
(\)type->tp_alloc\(type,)d(0\);)416 2731 y(if)j(\(self)g(!=)h(NULL\))f
({)595 2823 y(self->first)f(=)i(PyString_FromString\(""\);)595
2914 y(if)f(\(self->first)f(==)i(NULL\))685 3005 y({)774
3097 y(Py_DECREF\(self\);)774 3188 y(return)f(NULL;)685
3279 y(})595 3462 y(self->last)f(=)i(PyString_FromString\(""\);)595
3553 y(if)f(\(self->last)f(==)i(NULL\))685 3645 y({)774
3736 y(Py_DECREF\(self\);)774 3827 y(return)f(NULL;)685
3919 y(})595 4101 y(self->number)f(=)h(0;)416 4193 y(})416
4375 y(return)f(\(PyObject)1178 4388 y(*)1223 4375 y(\)self;)236
4467 y(})236 4649 y(static)h(int)236 4741 y(Noddy_init\(Noddy)998
4754 y(*)1043 4741 y(self,)g(PyObject)1716 4754 y(*)1761
4741 y(args,)f(PyObject)2433 4754 y(*)2478 4741 y(kwds\))236
4832 y({)416 4923 y(PyObject)819 4936 y(*)864 4923 y(first=NULL,)1402
4936 y(*)1447 4923 y(last=NULL,)1940 4936 y(*)1985 4923
y(tmp;)416 5106 y(static)g(char)954 5119 y(*)999 5106
y(kwlist[])g(=)i({"first",)e("last",)g("number",)h(NULL};)416
5289 y(if)g(\(!)h(PyArg_ParseTupleAndKeywords\(args,)39
b(kwds,)44 b("|OOi",)f(kwlist,)1940 5380 y(&first,)g(&last,)p
0 5549 3901 4 v 0 5649 a Fo(42)2614 b(Chapter)24 b(2.)52
b(De\002ning)23 b(Ne)n(w)h(T)-10 b(ypes)p eop end
%%Page: 43 49
TeXDict begin 43 48 bop 1940 83 a Fj(&self->number\)\))595
174 y(return)44 b(-1;)416 357 y(if)g(\(first\))g({)595
448 y(tmp)g(=)h(self->first;)595 540 y(Py_INCREF\(first\);)595
631 y(self->first)e(=)i(first;)595 722 y(Py_XDECREF\(tmp\);)416
814 y(})416 996 y(if)f(\(last\))g({)595 1088 y(tmp)g(=)h(self->last;)
595 1179 y(Py_INCREF\(last\);)595 1270 y(self->last)e(=)i(last;)595
1362 y(Py_XDECREF\(tmp\);)416 1453 y(})416 1636 y(return)e(0;)236
1727 y(})236 2001 y(static)h(PyMemberDef)f(Noddy_members[])f(=)j({)416
2092 y({"first",)e(T_OBJECT_EX,)g(offsetof\(Noddy,)f(first\),)i(0,)460
2183 y("first)g(name"},)416 2275 y({"last",)f(T_OBJECT_EX,)g
(offsetof\(Noddy,)f(last\),)i(0,)460 2366 y("last)g(name"},)416
2457 y({"number",)f(T_INT,)h(offsetof\(Noddy,)e(number\),)h(0,)460
2549 y("noddy)h(number"},)416 2640 y({NULL})88 b(/)819
2653 y(*)909 2640 y(Sentinel)1312 2653 y(*)1357 2640
y(/)236 2731 y(};)236 2914 y(static)44 b(PyObject)954
2927 y(*)236 3005 y(Noddy_name\(Noddy)956 3018 y(*)1043
3005 y(self\))236 3097 y({)416 3188 y(static)f(PyObject)1133
3201 y(*)1178 3188 y(format)h(=)g(NULL;)416 3279 y(PyObject)819
3292 y(*)864 3279 y(args,)1133 3292 y(*)1178 3279 y(result;)416
3462 y(if)g(\(format)g(==)g(NULL\))g({)595 3553 y(format)g(=)g
(PyString_FromString\("\045s)d(\045s"\);)595 3645 y(if)j(\(format)g(==)
h(NULL\))774 3736 y(return)f(NULL;)416 3827 y(})416 4010
y(if)g(\(self->first)f(==)h(NULL\))g({)595 4101 y
(PyErr_SetString\(PyExc_AttributeError,)38 b("first"\);)595
4193 y(return)44 b(NULL;)416 4284 y(})416 4467 y(if)g(\(self->last)f
(==)i(NULL\))f({)595 4558 y(PyErr_SetString\(PyExc_AttributeError,)38
b("last"\);)595 4649 y(return)44 b(NULL;)416 4741 y(})416
4923 y(args)g(=)g(Py_BuildValue\("OO",)e(self->first,)h(self->last\);)
416 5015 y(if)h(\(args)g(==)h(NULL\))595 5106 y(return)f(NULL;)416
5289 y(result)f(=)i(PyString_Format\(format,)c(args\);)416
5380 y(Py_DECREF\(args\);)p 0 5549 3901 4 v 0 5649 a
Fo(2.1.)52 b(The)23 b(Basics)3207 b(43)p eop end
%%Page: 44 50
TeXDict begin 44 49 bop 416 174 a Fj(return)43 b(result;)236
266 y(})236 448 y(static)h(PyMethodDef)f(Noddy_methods[])f(=)j({)416
540 y({"name",)e(\(PyCFunction\)Noddy_name,)e(METH_NOARGS,)460
631 y("Return)j(the)g(name,)g(combining)g(the)g(first)g(and)g(last)g
(name")416 722 y(},)416 814 y({NULL})88 b(/)819 827 y(*)909
814 y(Sentinel)1312 827 y(*)1357 814 y(/)236 905 y(};)236
1088 y(static)44 b(PyTypeObject)f(NoddyType)g(=)i({)416
1179 y(PyObject_HEAD_INIT\(NULL\))416 1270 y(0,)1120
b(/)1671 1283 y(*)1716 1270 y(ob_size)2031 1283 y(*)2076
1270 y(/)416 1362 y("noddy.Noddy",)580 b(/)1671 1375
y(*)1716 1362 y(tp_name)2031 1375 y(*)2076 1362 y(/)416
1453 y(sizeof\(Noddy\),)g(/)1671 1466 y(*)1716 1453 y(tp_basicsize)2256
1466 y(*)2301 1453 y(/)416 1544 y(0,)1120 b(/)1671 1557
y(*)1716 1544 y(tp_itemsize)2211 1557 y(*)2256 1544 y(/)416
1636 y(\(destructor\)Noddy_dealloc,)40 b(/)1671 1649
y(*)1716 1636 y(tp_dealloc)2166 1649 y(*)2211 1636 y(/)416
1727 y(0,)1120 b(/)1671 1740 y(*)1716 1727 y(tp_print)2076
1740 y(*)2121 1727 y(/)416 1818 y(0,)g(/)1671 1831 y(*)1716
1818 y(tp_getattr)2166 1831 y(*)2211 1818 y(/)416 1910
y(0,)g(/)1671 1923 y(*)1716 1910 y(tp_setattr)2166 1923
y(*)2211 1910 y(/)416 2001 y(0,)g(/)1671 2014 y(*)1716
2001 y(tp_compare)2166 2014 y(*)2211 2001 y(/)416 2092
y(0,)g(/)1671 2105 y(*)1716 2092 y(tp_repr)2031 2105
y(*)2076 2092 y(/)416 2183 y(0,)g(/)1671 2196 y(*)1716
2183 y(tp_as_number)2256 2196 y(*)2301 2183 y(/)416 2275
y(0,)g(/)1671 2288 y(*)1716 2275 y(tp_as_sequence)2346
2288 y(*)2391 2275 y(/)416 2366 y(0,)g(/)1671 2379 y(*)1716
2366 y(tp_as_mapping)2301 2379 y(*)2346 2366 y(/)416
2457 y(0,)g(/)1671 2470 y(*)1716 2457 y(tp_hash)2074
2470 y(*)2119 2457 y(/)416 2549 y(0,)g(/)1671 2562 y(*)1716
2549 y(tp_call)2031 2562 y(*)2076 2549 y(/)416 2640 y(0,)g(/)1671
2653 y(*)1716 2640 y(tp_str)1986 2653 y(*)2031 2640 y(/)416
2731 y(0,)g(/)1671 2744 y(*)1716 2731 y(tp_getattro)2211
2744 y(*)2256 2731 y(/)416 2823 y(0,)g(/)1671 2836 y(*)1716
2823 y(tp_setattro)2211 2836 y(*)2256 2823 y(/)416 2914
y(0,)g(/)1671 2927 y(*)1716 2914 y(tp_as_buffer)2256
2927 y(*)2301 2914 y(/)416 3005 y(Py_TPFLAGS_DEFAULT)41
b(|)k(Py_TPFLAGS_BASETYPE)d(|)i(Py_TPFLAGS_HAVE_GC,)e(/)3285
3018 y(*)3330 3005 y(tp_flags)3690 3018 y(*)3735 3005
y(/)416 3097 y("Noddy)h(objects",)492 b(/)1671 3110 y(*)1761
3097 y(tp_doc)2074 3110 y(*)2119 3097 y(/)416 3188 y
(\(traverseproc\)Noddy_traverse,)129 b(/)1895 3201 y(*)1985
3188 y(tp_traverse)2523 3201 y(*)2568 3188 y(/)416 3279
y(\(inquiry\)Noddy_clear,)489 b(/)1895 3292 y(*)1985
3279 y(tp_clear)2388 3292 y(*)2433 3279 y(/)416 3371
y(0,)672 b(/)1223 3384 y(*)1312 3371 y(tp_richcompare)1985
3384 y(*)2030 3371 y(/)416 3462 y(0,)g(/)1223 3475 y(*)1312
3462 y(tp_weaklistoffset)2119 3475 y(*)2164 3462 y(/)416
3553 y(0,)g(/)1223 3566 y(*)1312 3553 y(tp_iter)1671
3566 y(*)1716 3553 y(/)416 3645 y(0,)g(/)1223 3658 y(*)1312
3645 y(tp_iternext)1850 3658 y(*)1895 3645 y(/)416 3736
y(Noddy_methods,)580 b(/)1671 3749 y(*)1761 3736 y(tp_methods)2254
3749 y(*)2299 3736 y(/)416 3827 y(Noddy_members,)g(/)1671
3840 y(*)1761 3827 y(tp_members)2254 3840 y(*)2299 3827
y(/)416 3919 y(0,)1120 b(/)1671 3932 y(*)1761 3919 y(tp_getset)2209
3932 y(*)2254 3919 y(/)416 4010 y(0,)g(/)1671 4023 y(*)1761
4010 y(tp_base)2119 4023 y(*)2164 4010 y(/)416 4101 y(0,)g(/)1671
4114 y(*)1761 4101 y(tp_dict)2119 4114 y(*)2164 4101
y(/)416 4193 y(0,)g(/)1671 4206 y(*)1761 4193 y(tp_descr_get)2343
4206 y(*)2388 4193 y(/)416 4284 y(0,)g(/)1671 4297 y(*)1761
4284 y(tp_descr_set)2343 4297 y(*)2388 4284 y(/)416 4375
y(0,)g(/)1671 4388 y(*)1761 4375 y(tp_dictoffset)2388
4388 y(*)2433 4375 y(/)416 4467 y(\(initproc\)Noddy_init,)265
b(/)1671 4480 y(*)1761 4467 y(tp_init)2119 4480 y(*)2164
4467 y(/)416 4558 y(0,)1120 b(/)1671 4571 y(*)1761 4558
y(tp_alloc)2164 4571 y(*)2209 4558 y(/)416 4649 y(Noddy_new,)760
b(/)1671 4662 y(*)1761 4649 y(tp_new)2074 4662 y(*)2119
4649 y(/)236 4741 y(};)236 4923 y(static)44 b(PyMethodDef)f
(module_methods[])f(=)j({)416 5015 y({NULL})88 b(/)819
5028 y(*)909 5015 y(Sentinel)1312 5028 y(*)1357 5015
y(/)236 5106 y(};)236 5289 y(#ifndef)44 b(PyMODINIT_FUNC)e(/)1312
5302 y(*)1402 5289 y(declarations)h(for)h(DLL)g(import/export)2971
5302 y(*)3016 5289 y(/)236 5380 y(#define)g(PyMODINIT_FUNC)e(void)p
0 5549 3901 4 v 0 5649 a Fo(44)2614 b(Chapter)24 b(2.)52
b(De\002ning)23 b(Ne)n(w)h(T)-10 b(ypes)p eop end
%%Page: 45 51
TeXDict begin 45 50 bop 236 83 a Fj(#endif)236 174 y(PyMODINIT_FUNC)236
266 y(initnoddy4\(void\))236 357 y({)416 448 y(PyObject)776
461 y(*)864 448 y(m;)416 631 y(if)44 b(\(PyType_Ready\(&NoddyType\))d
(<)j(0\))595 722 y(return;)416 905 y(m)g(=)h(Py_InitModule3\("noddy4",)
c(module_methods,)1267 996 y("Example)j(module)g(that)g(creates)f(an)i
(extension)e(type."\);)416 1179 y(if)h(\(m)h(==)f(NULL\))505
1270 y(return;)416 1453 y(Py_INCREF\(&NoddyType\);)416
1544 y(PyModule_AddObject\(m,)d("Noddy",)i(\(PyObject)2254
1557 y(*)2299 1544 y(\)&NoddyType\);)236 1636 y(})0 1832
y Ft(The)20 b(tra)n(v)o(ersal)g(method)e(pro)o(vides)h(access)i(to)f
(subobjects)f(that)h(could)g(participate)f(in)h(c)o(ycles:)236
2070 y Fj(static)44 b(int)236 2162 y(Noddy_traverse\(Noddy)1178
2175 y(*)1223 2162 y(self,)g(visitproc)f(visit,)h(void)2478
2175 y(*)2523 2162 y(arg\))236 2253 y({)416 2344 y(int)g(vret;)416
2527 y(if)g(\(self->first\))f({)595 2618 y(vret)h(=)h
(visit\(self->first,)d(arg\);)595 2710 y(if)i(\(vret)g(!=)h(0\))774
2801 y(return)f(vret;)416 2892 y(})416 2984 y(if)g(\(self->last\))f({)
595 3075 y(vret)h(=)h(visit\(self->last,)d(arg\);)595
3166 y(if)i(\(vret)g(!=)h(0\))774 3258 y(return)f(vret;)416
3349 y(})416 3532 y(return)f(0;)236 3623 y(})0 3909 y
Ft(F)o(or)31 b(each)f(subobject)g(that)h(can)f(participate)g(in)h(c)o
(ycles,)i(we)f(need)e(to)h(call)g(the)g Fm(visit\(\))f
Ft(function,)i(which)e(is)i(passed)f(to)g(the)0 4009
y(tra)n(v)o(ersal)21 b(method.)29 b(The)22 b Fm(visit\(\))f
Ft(function)g(tak)o(es)h(as)h(ar)o(guments)d(the)i(subobject)e(and)i
(the)g(e)o(xtra)f(ar)o(gument)f Fq(ar)m(g)i Ft(passed)f(to)i(the)0
4109 y(tra)n(v)o(ersal)d(method.)j(It)e(returns)e(an)h(inte)o(ger)f(v)n
(alue)h(that)g(must)g(be)g(returned)f(if)h(it)h(is)g(non-zero.)0
4256 y(Python)k(2.4)h(and)f(higher)g(pro)o(vide)g(a)h
Fm(Py_VISIT\(\))f Ft(macro)g(that)i(automates)e(calling)h(visit)h
(functions.)42 b(W)m(ith)26 b Fm(Py_VISIT\(\))p Ft(,)0
4355 y Fm(Noddy_traverse\(\))18 b Ft(can)i(be)g(simpli\002ed:)236
4593 y Fj(static)44 b(int)236 4685 y(Noddy_traverse\(Noddy)1178
4698 y(*)1223 4685 y(self,)g(visitproc)f(visit,)h(void)2478
4698 y(*)2523 4685 y(arg\))236 4776 y({)416 4867 y
(Py_VISIT\(self->first\);)416 4959 y(Py_VISIT\(self->last\);)416
5050 y(return)f(0;)236 5141 y(})p 0 5549 3901 4 v 0 5649
a Fo(2.1.)52 b(The)23 b(Basics)3207 b(45)p eop end
%%Page: 46 52
TeXDict begin 46 51 bop 0 83 a Fr(Note:)36 b Ft(Note)26
b(that)g(the)f Fm(tp_traverse)g Ft(implementation)e(must)j(name)g(its)h
(ar)o(guments)c(e)o(xactly)i Fq(visit)k Ft(and)c Fq(ar)m(g)h
Ft(in)g(order)e(to)i(use)0 183 y Fm(Py_VISIT\(\))p Ft(.)e(This)c(is)h
(to)f(encourage)e(uniformity)g(across)j(these)f(boring)e
(implementations.)0 330 y(W)-7 b(e)20 b(also)e(need)g(to)h(pro)o(vide)d
(a)j(method)f(for)f(clearing)h(an)o(y)g(subobjects)f(that)i(can)f
(participate)g(in)g(c)o(ycles.)24 b(W)-7 b(e)20 b(implement)d(the)i
(method)0 429 y(and)h(reimplement)e(the)i(deallocator)f(to)h(use)h(it:)
236 576 y Fj(static)44 b(int)236 667 y(Noddy_clear\(Noddy)1043
680 y(*)1088 667 y(self\))236 759 y({)416 850 y(PyObject)819
863 y(*)864 850 y(tmp;)416 1033 y(tmp)g(=)h(self->first;)416
1124 y(self->first)e(=)h(NULL;)416 1215 y(Py_XDECREF\(tmp\);)416
1398 y(tmp)g(=)h(self->last;)416 1489 y(self->last)e(=)h(NULL;)416
1581 y(Py_XDECREF\(tmp\);)416 1763 y(return)f(0;)236
1855 y(})236 2037 y(static)h(void)236 2129 y(Noddy_dealloc\(Noddy)1091
2142 y(*)1178 2129 y(self\))236 2220 y({)416 2311 y
(Noddy_clear\(self\);)416 2402 y(self->ob_type->tp_free\(\(PyObject)
1856 2415 y(*)1901 2402 y(\)se)o(lf\);)236 2494 y(})0
2780 y Ft(Notice)22 b(the)g(use)g(of)g(a)h(temporary)d(v)n(ariable)h
(in)h Fm(Noddy_clear\(\))p Ft(.)29 b(W)-7 b(e)23 b(use)f(the)g
(temporary)e(v)n(ariable)h(so)i(that)f(we)g(can)g(set)h(each)0
2880 y(member)15 b(to)h Fm(NULL)f Ft(before)g(decrementing)e(its)k
(reference)e(count.)22 b(W)-7 b(e)17 b(do)f(this)g(because,)g(as)h(w)o
(as)g(discussed)e(earlier)m(,)h(if)g(the)g(reference)0
2980 y(count)22 b(drops)f(to)i(zero,)f(we)h(might)f(cause)h(code)e(to)i
(run)f(that)h(calls)g(back)f(into)g(the)h(object.)31
b(In)22 b(addition,)g(because)g(we)h(no)n(w)f(support)0
3079 y(garbage)d(collection,)h(we)h(also)g(ha)n(v)o(e)g(to)g(w)o(orry)f
(about)g(code)g(being)g(run)g(that)i(triggers)e(garbage)f(collection.)
26 b(If)21 b(garbage)e(collection)0 3179 y(is)i(run,)e(our)g
Fm(tp_traverse)g Ft(handler)f(could)h(get)h(called.)25
b(W)-7 b(e)21 b(can')o(t)e(tak)o(e)h(a)h(chance)e(of)g(ha)n(ving)g
Fm(Noddy_traverse\(\))f Ft(called)0 3278 y(when)i(a)g(member')-5
b(s)19 b(reference)g(count)g(has)i(dropped)c(to)k(zero)e(and)h(its)h(v)
n(alue)f(hasn')o(t)f(been)g(set)i(to)g Fm(NULL)p Ft(.)0
3425 y(Python)k(2.4)h(and)f(higher)g(pro)o(vide)f(a)j
Fm(Py_CLEAR\(\))e Ft(that)h(automates)f(the)i(careful)e(decrementing)f
(of)h(reference)g(counts.)42 b(W)m(ith)0 3525 y Fm(Py_CLEAR\(\))p
Ft(,)19 b(the)h Fm(Noddy_clear\(\))e Ft(function)h(can)h(be)g
(simpli\002ed:)236 3763 y Fj(static)44 b(int)236 3854
y(Noddy_clear\(Noddy)1043 3867 y(*)1088 3854 y(self\))236
3946 y({)416 4037 y(Py_CLEAR\(self->first\);)416 4128
y(Py_CLEAR\(self->last\);)416 4220 y(return)f(0;)236
4311 y(})0 4598 y Ft(Finally)-5 b(,)19 b(we)i(add)f(the)g
Fm(Py_TPFLAGS_HAVE_GC)d Ft(\003ag)j(to)h(the)f(class)h(\003ags:)416
4836 y Fj(Py_TPFLAGS_DEFAULT)41 b(|)k(Py_TPFLAGS_BASETYPE)d(|)i
(Py_TPFLAGS_HAVE_GC,)e(/)3285 4849 y(*)3330 4836 y(tp_flags)3690
4849 y(*)3735 4836 y(/)0 5122 y Ft(That')-5 b(s)20 b(pretty)g(much)f
(it.)26 b(If)20 b(we)h(had)e(written)h(custom)g Fm(tp_alloc)f
Ft(or)h Fm(tp_free)g Ft(slots,)h(we')l(d)e(need)h(to)g(modify)f(them)h
(for)f(c)o(yclic-)0 5222 y(garbage)f(collection.)24 b(Most)c(e)o
(xtensions)g(will)g(use)h(the)f(v)o(ersions)f(automatically)g(pro)o
(vided.)p 0 5549 3901 4 v 0 5649 a Fo(46)2614 b(Chapter)24
b(2.)52 b(De\002ning)23 b(Ne)n(w)h(T)-10 b(ypes)p eop
end
%%Page: 47 53
TeXDict begin 47 52 bop 0 83 a Fg(2.1.4)100 b(Subclassing)30
b(other)f(types)0 286 y Ft(It)j(is)g(possible)f(to)h(create)f(ne)n(w)g
(e)o(xtension)f(types)i(that)f(are)h(deri)n(v)o(ed)d(from)i(e)o
(xisting)g(types.)58 b(It)32 b(is)g(easiest)h(to)e(inherit)g(from)g
(the)0 386 y(b)n(uilt)g(in)h(types,)h(since)e(an)h(e)o(xtension)d(can)i
(easily)h(use)f(the)g Fm(PyTypeObject)f Ft(it)i(needs.)57
b(It)32 b(can)e(be)i(dif)n(\002cult)e(to)h(share)g(these)0
485 y Fm(PyTypeObject)19 b Ft(structures)g(between)g(e)o(xtension)g
(modules.)0 632 y(In)24 b(this)g(e)o(xample)f(we)h(will)h(create)e(a)i
Fm(Shoddy)e Ft(type)g(that)i(inherits)e(from)g(the)h(b)n(uiltin)g
Fm(list)f Ft(type.)36 b(The)24 b(ne)n(w)f(type)h(will)h(be)e(com-)0
732 y(pletely)j(compatible)g(with)h(re)o(gular)e(lists,)30
b(b)n(ut)d(will)g(ha)n(v)o(e)g(an)f(additional)g Fm(increment\(\))f
Ft(method)h(that)h(increases)g(an)f(internal)0 831 y(counter)-5
b(.)236 1070 y Fj(>>>)45 b(import)e(shoddy)236 1161 y(>>>)i(s)f(=)h
(shoddy.Shoddy\(range\(3\)\))236 1252 y(>>>)g(s.extend\(s\))236
1343 y(>>>)g(print)f(len\(s\))236 1435 y(6)236 1526 y(>>>)h(print)f
(s.increment\(\))236 1617 y(1)236 1709 y(>>>)h(print)f(s.increment\(\))
236 1800 y(2)236 2112 y(#include)g(<Python.h>)236 2295
y(typedef)g(struct)g({)416 2386 y(PyListObject)e(list;)416
2478 y(int)i(state;)236 2569 y(})h(Shoddy;)236 2843 y(static)f
(PyObject)954 2856 y(*)236 2934 y(Shoddy_increment\(Shoddy)1312
2947 y(*)1357 2934 y(self,)g(PyObject)2029 2947 y(*)2074
2934 y(unused\))236 3026 y({)416 3117 y(self->state++;)416
3208 y(return)f(PyInt_FromLong\(self->state\);)236 3300
y(})236 3574 y(static)h(PyMethodDef)f(Shoddy_methods[])f(=)j({)416
3665 y({"increment",)d(\(PyCFunction\)Shoddy_increment,)e(METH_NOARGS,)
460 3756 y(PyDoc_STR\("increment)i(state)i(counter"\)},)416
3847 y({NULL,NULL},)236 3939 y(};)236 4121 y(static)g(int)236
4213 y(Shoddy_init\(Shoddy)1088 4226 y(*)1133 4213 y(self,)g(PyObject)
1805 4226 y(*)1850 4213 y(args,)g(PyObject)2523 4226
y(*)2568 4213 y(kwds\))236 4304 y({)416 4395 y(if)g
(\(PyList_Type.tp_init\(\(PyObject)1940 4408 y(*)1985
4395 y(\)self,)f(args,)h(kwds\))g(<)h(0\))595 4487 y(return)f(-1;)416
4578 y(self->state)f(=)h(0;)416 4669 y(return)f(0;)236
4761 y(})236 5035 y(static)h(PyTypeObject)f(ShoddyType)g(=)i({)416
5126 y(PyObject_HEAD_INIT\(NULL\))416 5217 y(0,)1030
b(/)1581 5230 y(*)1671 5217 y(ob_size)2029 5230 y(*)2074
5217 y(/)416 5309 y("shoddy.Shoddy",)400 b(/)1581 5322
y(*)1671 5309 y(tp_name)2029 5322 y(*)2074 5309 y(/)416
5400 y(sizeof\(Shoddy\),)445 b(/)1581 5413 y(*)1671 5400
y(tp_basicsize)2254 5413 y(*)2299 5400 y(/)p 0 5549 3901
4 v 0 5649 a Fo(2.1.)52 b(The)23 b(Basics)3207 b(47)p
eop end
%%Page: 48 54
TeXDict begin 48 53 bop 416 83 a Fj(0,)1030 b(/)1581
96 y(*)1671 83 y(tp_itemsize)2209 96 y(*)2254 83 y(/)416
174 y(0,)g(/)1581 187 y(*)1671 174 y(tp_dealloc)2164
187 y(*)2209 174 y(/)416 266 y(0,)g(/)1581 279 y(*)1671
266 y(tp_print)2074 279 y(*)2119 266 y(/)416 357 y(0,)g(/)1581
370 y(*)1671 357 y(tp_getattr)2164 370 y(*)2209 357 y(/)416
448 y(0,)g(/)1581 461 y(*)1671 448 y(tp_setattr)2164
461 y(*)2209 448 y(/)416 540 y(0,)g(/)1581 553 y(*)1671
540 y(tp_compare)2164 553 y(*)2209 540 y(/)416 631 y(0,)g(/)1581
644 y(*)1671 631 y(tp_repr)2029 644 y(*)2074 631 y(/)416
722 y(0,)g(/)1581 735 y(*)1671 722 y(tp_as_number)2254
735 y(*)2299 722 y(/)416 814 y(0,)g(/)1581 827 y(*)1671
814 y(tp_as_sequence)2343 827 y(*)2388 814 y(/)416 905
y(0,)g(/)1581 918 y(*)1671 905 y(tp_as_mapping)2298 918
y(*)2343 905 y(/)416 996 y(0,)g(/)1581 1009 y(*)1671
996 y(tp_hash)2029 1009 y(*)2074 996 y(/)416 1088 y(0,)g(/)1581
1101 y(*)1671 1088 y(tp_call)2029 1101 y(*)2074 1088
y(/)416 1179 y(0,)g(/)1581 1192 y(*)1671 1179 y(tp_str)1985
1192 y(*)2030 1179 y(/)416 1270 y(0,)g(/)1581 1283 y(*)1671
1270 y(tp_getattro)2209 1283 y(*)2254 1270 y(/)416 1362
y(0,)g(/)1581 1375 y(*)1671 1362 y(tp_setattro)2209 1375
y(*)2254 1362 y(/)416 1453 y(0,)g(/)1581 1466 y(*)1671
1453 y(tp_as_buffer)2254 1466 y(*)2299 1453 y(/)416 1544
y(Py_TPFLAGS_DEFAULT)41 b(|)505 1636 y(Py_TPFLAGS_BASETYPE,)131
b(/)1581 1649 y(*)1671 1636 y(tp_flags)2074 1649 y(*)2119
1636 y(/)416 1727 y(0,)1030 b(/)1581 1740 y(*)1671 1727
y(tp_doc)1985 1740 y(*)2030 1727 y(/)416 1818 y(0,)g(/)1581
1831 y(*)1671 1818 y(tp_traverse)2209 1831 y(*)2254 1818
y(/)416 1910 y(0,)g(/)1581 1923 y(*)1671 1910 y(tp_clear)2074
1923 y(*)2119 1910 y(/)416 2001 y(0,)g(/)1581 2014 y(*)1671
2001 y(tp_richcompare)2343 2014 y(*)2388 2001 y(/)416
2092 y(0,)g(/)1581 2105 y(*)1671 2092 y(tp_weaklistoffset)2478
2105 y(*)2523 2092 y(/)416 2183 y(0,)g(/)1581 2196 y(*)1671
2183 y(tp_iter)2029 2196 y(*)2074 2183 y(/)416 2275 y(0,)g(/)1581
2288 y(*)1671 2275 y(tp_iternext)2209 2288 y(*)2254 2275
y(/)416 2366 y(Shoddy_methods,)445 b(/)1581 2379 y(*)1671
2366 y(tp_methods)2164 2379 y(*)2209 2366 y(/)416 2457
y(0,)1030 b(/)1581 2470 y(*)1671 2457 y(tp_members)2164
2470 y(*)2209 2457 y(/)416 2549 y(0,)g(/)1581 2562 y(*)1671
2549 y(tp_getset)2119 2562 y(*)2164 2549 y(/)416 2640
y(0,)g(/)1581 2653 y(*)1671 2640 y(tp_base)2029 2653
y(*)2074 2640 y(/)416 2731 y(0,)g(/)1581 2744 y(*)1671
2731 y(tp_dict)2029 2744 y(*)2074 2731 y(/)416 2823 y(0,)g(/)1581
2836 y(*)1671 2823 y(tp_descr_get)2254 2836 y(*)2299
2823 y(/)416 2914 y(0,)g(/)1581 2927 y(*)1671 2914 y(tp_descr_set)2254
2927 y(*)2299 2914 y(/)416 3005 y(0,)g(/)1581 3018 y(*)1671
3005 y(tp_dictoffset)2298 3018 y(*)2343 3005 y(/)416
3097 y(\(initproc\)Shoddy_init,)130 b(/)1581 3110 y(*)1671
3097 y(tp_init)2029 3110 y(*)2074 3097 y(/)416 3188 y(0,)1030
b(/)1581 3201 y(*)1671 3188 y(tp_alloc)2074 3201 y(*)2119
3188 y(/)416 3279 y(0,)g(/)1581 3292 y(*)1671 3279 y(tp_new)1985
3292 y(*)2030 3279 y(/)236 3371 y(};)236 3553 y(PyMODINIT_FUNC)236
3645 y(initshoddy\(void\))236 3736 y({)416 3827 y(PyObject)819
3840 y(*)864 3827 y(m;)416 4010 y(ShoddyType.tp_base)41
b(=)k(&PyList_Type;)416 4101 y(if)f(\(PyType_Ready\(&ShoddyType\))d(<)j
(0\))595 4193 y(return;)416 4375 y(m)g(=)h(Py_InitModule3\("shoddy",)c
(NULL,)j("Shoddy)f(module"\);)416 4467 y(if)h(\(m)h(==)f(NULL\))595
4558 y(return;)416 4741 y(Py_INCREF\(&ShoddyType\);)416
4832 y(PyModule_AddObject\(m,)d("Shoddy",)i(\(PyObject)2298
4845 y(*)2343 4832 y(\))i(&ShoddyType\);)236 4923 y(})0
5120 y Ft(As)21 b(you)e(can)g(see,)i(the)e(source)h(code)f(closely)g
(resembles)h(the)g Fm(Noddy)f Ft(e)o(xamples)g(in)h(pre)n(vious)e
(sections.)25 b(W)-7 b(e)21 b(will)g(break)e(do)n(wn)g(the)0
5220 y(main)h(dif)n(ferences)e(between)i(them.)p 0 5549
3901 4 v 0 5649 a Fo(48)2614 b(Chapter)24 b(2.)52 b(De\002ning)23
b(Ne)n(w)h(T)-10 b(ypes)p eop end
%%Page: 49 55
TeXDict begin 49 54 bop 236 174 a Fj(typedef)44 b(struct)g({)236
266 y(PyListObject)f(list;)236 357 y(int)i(state;)236
448 y(})g(Shoddy;)0 735 y Ft(The)18 b(primary)f(dif)n(ference)f(for)i
(deri)n(v)o(ed)f(type)h(objects)g(is)i(that)e(the)h(base)f(type')-5
b(s)19 b(object)f(structure)f(must)i(be)f(the)g(\002rst)i(v)n(alue.)j
(The)18 b(base)0 834 y(type)i(will)h(already)e(include)g(the)h
Fm(PyObject_HEAD)e Ft(at)j(the)f(be)o(ginning)e(of)i(its)h(structure.)0
981 y(When)30 b(a)g(Python)f(object)g(is)i(a)f Fm(Shoddy)g
Ft(instance,)h(its)g Fq(PyObject*)e Ft(pointer)g(can)g(be)h(safely)g
(cast)h(to)f(both)f Fq(PyListObject*)g Ft(and)0 1081
y Fq(Shoddy*)p Ft(.)236 1319 y Fj(static)44 b(int)236
1410 y(Shoddy_init\(Shoddy)1088 1423 y(*)1133 1410 y(self,)g(PyObject)
1805 1423 y(*)1850 1410 y(args,)g(PyObject)2523 1423
y(*)2568 1410 y(kwds\))236 1502 y({)236 1593 y(if)h
(\(PyList_Type.tp_init\(\(PyObject)1761 1606 y(*)1806
1593 y(\)self,)e(args,)h(kwds\))g(<)h(0\))236 1684 y(return)f(-1;)236
1776 y(self->state)f(=)i(0;)236 1867 y(return)f(0;)236
1958 y(})0 2245 y Ft(In)20 b(the)g Fm(__init__)f Ft(method)g(for)h(our)
f(type,)h(we)g(can)g(see)h(ho)n(w)f(to)g(call)h(through)d(to)i(the)g
Fm(__init__)f Ft(method)g(of)h(the)g(base)h(type.)0 2392
y(This)g(pattern)e(is)i(important)e(when)h(writing)f(a)i(type)f(with)h
(custom)e Fm(new)i Ft(and)e Fm(dealloc)h Ft(methods.)k(The)c
Fm(new)h Ft(method)e(should)g(not)0 2491 y(actually)26
b(create)h(the)f(memory)f(for)h(the)h(object)f(with)h
Fm(tp_alloc)p Ft(,)g(that)g(will)h(be)e(handled)g(by)g(the)h(base)f
(class)i(when)e(calling)h(its)0 2591 y Fm(tp_new)p Ft(.)0
2738 y(When)g(\002lling)h(out)f(the)g Fm(PyTypeObject)f
Ft(for)g(the)i Fm(Shoddy)f Ft(type,)h(you)f(see)h(a)f(slot)h(for)f
Fm(tp_base)p Ft(.)46 b(Due)27 b(to)h(cross)f(platform)0
2838 y(compiler)21 b(issues,)j(you)e(can')o(t)f(\002ll)j(that)e
(\002eld)h(directly)f(with)g(the)h Fm(PyList_Type)p Ft(;)f(it)h(can)g
(be)f(done)f(later)i(in)g(the)f(module')-5 b(s)22 b Fm(init)0
2937 y Ft(function.)236 3175 y Fj(PyMODINIT_FUNC)236
3267 y(initshoddy\(void\))236 3358 y({)236 3449 y(PyObject)640
3462 y(*)685 3449 y(m;)236 3632 y(ShoddyType.tp_base)42
b(=)j(&PyList_Type;)236 3723 y(if)g(\(PyType_Ready\(&ShoddyType\))40
b(<)45 b(0\))236 3815 y(return;)236 3997 y(m)g(=)g
(Py_InitModule3\("shoddy",)40 b(NULL,)k("Shoddy)g(module"\);)236
4089 y(if)h(\(m)f(==)h(NULL\))236 4180 y(return;)236
4363 y(Py_INCREF\(&ShoddyType\);)236 4454 y(PyModule_AddObject\(m,)d
("Shoddy",)h(\(PyObject)2119 4467 y(*)2164 4454 y(\))i(&ShoddyType\);)
236 4545 y(})0 4832 y Ft(Before)24 b(calling)g Fm(PyType_Ready)p
Ft(,)g(the)h(type)f(structure)g(must)h(ha)n(v)o(e)f(the)h
Fm(tp_base)f Ft(slot)h(\002lled)g(in.)39 b(When)24 b(we)i(are)e(deri)n
(ving)f(a)0 4931 y(ne)n(w)c(type,)f(it)i(is)g(not)f(necessary)f(to)h
(\002ll)h(out)f(the)g Fm(tp_alloc)f Ft(slot)i(with)f
Fm(PyType_GenericNew)d Ft(\226)j(the)g(allocate)g(function)f(from)0
5031 y(the)i(base)g(type)g(will)h(be)f(inherited.)0 5178
y(After)k(that,)i(calling)f Fm(PyType_Ready)e Ft(and)h(adding)f(the)i
(type)f(object)g(to)h(the)g(module)e(is)j(the)f(same)g(as)g(with)g(the)
g(basic)g Fm(Noddy)0 5277 y Ft(e)o(xamples.)p 0 5549
3901 4 v 0 5649 a Fo(2.1.)52 b(The)23 b(Basics)3207 b(49)p
eop end
%%Page: 50 56
TeXDict begin 50 55 bop 0 86 a Fl(2.2)121 b(T)-14 b(ype)33
b(Methods)0 319 y Ft(This)20 b(section)g(aims)h(to)f(gi)n(v)o(e)g(a)g
(quick)f(\003y-by)h(on)f(the)i(v)n(arious)e(type)g(methods)g(you)h(can)
g(implement)f(and)g(what)h(the)o(y)g(do.)0 466 y(Here)g(is)h(the)f
(de\002nition)f(of)h Fm(PyTypeObject)p Ft(,)f(with)h(some)g(\002elds)h
(only)e(used)h(in)g(deb)n(ug)f(b)n(uilds)h(omitted:)236
651 y Fj(typedef)44 b(struct)g(_typeobject)f({)416 742
y(PyObject_VAR_HEAD)416 834 y(char)640 847 y(*)685 834
y(tp_name;)g(/)1133 847 y(*)1223 834 y(For)h(printing,)f(in)i(format)e
("<module>.<name>")3105 847 y(*)3150 834 y(/)416 925
y(int)h(tp_basicsize,)f(tp_itemsize;)f(/)1850 938 y(*)1940
925 y(For)i(allocation)2612 938 y(*)2657 925 y(/)416
1108 y(/)461 1121 y(*)550 1108 y(Methods)g(to)g(implement)g(standard)f
(operations)2388 1121 y(*)2433 1108 y(/)416 1290 y(destructor)g
(tp_dealloc;)416 1382 y(printfunc)g(tp_print;)416 1473
y(getattrfunc)g(tp_getattr;)416 1564 y(setattrfunc)g(tp_setattr;)416
1656 y(cmpfunc)g(tp_compare;)416 1747 y(reprfunc)g(tp_repr;)416
1930 y(/)461 1943 y(*)550 1930 y(Method)h(suites)g(for)g(standard)g
(classes)2119 1943 y(*)2164 1930 y(/)416 2112 y(PyNumberMethods)1133
2125 y(*)1178 2112 y(tp_as_number;)416 2204 y(PySequenceMethods)1223
2217 y(*)1268 2204 y(tp_as_sequence;)416 2295 y(PyMappingMethods)1178
2308 y(*)1223 2295 y(tp_as_mapping;)416 2478 y(/)461
2491 y(*)550 2478 y(More)g(standard)g(operations)f(\(here)h(for)g
(binary)g(compatibility\))3105 2491 y(*)3150 2478 y(/)416
2660 y(hashfunc)f(tp_hash;)416 2752 y(ternaryfunc)g(tp_call;)416
2843 y(reprfunc)g(tp_str;)416 2934 y(getattrofunc)f(tp_getattro;)416
3026 y(setattrofunc)g(tp_setattro;)416 3208 y(/)461 3221
y(*)550 3208 y(Functions)h(to)i(access)f(object)g(as)g(input/output)f
(buffer)2792 3221 y(*)2837 3208 y(/)416 3300 y(PyBufferProcs)1043
3313 y(*)1088 3300 y(tp_as_buffer;)416 3482 y(/)461 3495
y(*)550 3482 y(Flags)h(to)h(define)e(presence)h(of)g(optional/expanded)
e(features)3016 3495 y(*)3061 3482 y(/)416 3574 y(long)i(tp_flags;)416
3756 y(char)640 3769 y(*)685 3756 y(tp_doc;)f(/)1088
3769 y(*)1178 3756 y(Documentation)f(string)2119 3769
y(*)2164 3756 y(/)416 3939 y(/)461 3952 y(*)550 3939
y(Assigned)i(meaning)f(in)i(release)e(2.0)1985 3952 y(*)2030
3939 y(/)416 4030 y(/)461 4043 y(*)550 4030 y(call)h(function)g(for)g
(all)g(accessible)f(objects)2388 4043 y(*)2433 4030 y(/)416
4121 y(traverseproc)f(tp_traverse;)416 4304 y(/)461 4317
y(*)550 4304 y(delete)i(references)f(to)i(contained)e(objects)2298
4317 y(*)2343 4304 y(/)416 4395 y(inquiry)g(tp_clear;)416
4578 y(/)461 4591 y(*)550 4578 y(Assigned)h(meaning)f(in)i(release)e
(2.1)1985 4591 y(*)2030 4578 y(/)416 4669 y(/)461 4682
y(*)550 4669 y(rich)h(comparisons)1312 4682 y(*)1357
4669 y(/)416 4761 y(richcmpfunc)f(tp_richcompare;)416
4943 y(/)461 4956 y(*)550 4943 y(weak)h(reference)g(enabler)1581
4956 y(*)1626 4943 y(/)416 5035 y(long)g(tp_weaklistoffset;)416
5217 y(/)461 5230 y(*)550 5217 y(Added)g(in)h(release)e(2.2)1492
5230 y(*)1537 5217 y(/)416 5309 y(/)461 5322 y(*)550
5309 y(Iterators)998 5322 y(*)1043 5309 y(/)416 5400
y(getiterfunc)g(tp_iter;)p 0 5549 3901 4 v 0 5649 a Fo(50)2614
b(Chapter)24 b(2.)52 b(De\002ning)23 b(Ne)n(w)h(T)-10
b(ypes)p eop end
%%Page: 51 57
TeXDict begin 51 56 bop 416 83 a Fj(iternextfunc)42 b(tp_iternext;)416
266 y(/)461 279 y(*)550 266 y(Attribute)h(descriptor)h(and)g
(subclassing)f(stuff)2478 279 y(*)2523 266 y(/)416 357
y(struct)g(PyMethodDef)1267 370 y(*)1312 357 y(tp_methods;)416
448 y(struct)g(PyMemberDef)1267 461 y(*)1312 448 y(tp_members;)416
540 y(struct)g(PyGetSetDef)1267 553 y(*)1312 540 y(tp_getset;)416
631 y(struct)g(_typeobject)1267 644 y(*)1312 631 y(tp_base;)416
722 y(PyObject)819 735 y(*)864 722 y(tp_dict;)416 814
y(descrgetfunc)f(tp_descr_get;)416 905 y(descrsetfunc)g(tp_descr_set;)
416 996 y(long)i(tp_dictoffset;)416 1088 y(initproc)f(tp_init;)416
1179 y(allocfunc)g(tp_alloc;)416 1270 y(newfunc)g(tp_new;)416
1362 y(freefunc)g(tp_free;)h(/)1268 1375 y(*)1357 1362
y(Low-level)f(free-memory)g(routine)2702 1375 y(*)2747
1362 y(/)416 1453 y(inquiry)g(tp_is_gc;)h(/)1268 1466
y(*)1357 1453 y(For)g(PyObject_IS_GC)2209 1466 y(*)2254
1453 y(/)416 1544 y(PyObject)819 1557 y(*)864 1544 y(tp_bases;)416
1636 y(PyObject)819 1649 y(*)864 1636 y(tp_mro;)g(/)1268
1649 y(*)1357 1636 y(method)g(resolution)f(order)2433
1649 y(*)2478 1636 y(/)416 1727 y(PyObject)819 1740 y(*)864
1727 y(tp_cache;)416 1818 y(PyObject)819 1831 y(*)864
1818 y(tp_subclasses;)416 1910 y(PyObject)819 1923 y(*)864
1910 y(tp_weaklist;)236 2092 y(})i(PyTypeObject;)0 2289
y Ft(No)n(w)24 b(that')-5 b(s)24 b(a)h Fq(lot)f Ft(of)g(methods.)35
b(Don')o(t)23 b(w)o(orry)g(too)g(much)h(though)e(-)i(if)g(you)f(ha)n(v)
o(e)h(a)g(type)g(you)f(w)o(ant)h(to)g(de\002ne,)g(the)g(chances)g(are)0
2388 y(v)o(ery)19 b(good)g(that)h(you)f(will)i(only)f(implement)f(a)h
(handful)f(of)h(these.)0 2535 y(As)26 b(you)f(probably)e(e)o(xpect)h
(by)h(no)n(w)-5 b(,)26 b(we')l(re)e(going)g(to)i(go)f(o)o(v)o(er)f
(this)i(and)f(gi)n(v)o(e)f(more)h(information)e(about)h(the)i(v)n
(arious)e(handlers.)0 2635 y(W)-7 b(e)22 b(w)o(on')o(t)d(go)h(in)h(the)
f(order)g(the)o(y)f(are)i(de\002ned)e(in)i(the)g(structure,)e(because)h
(there)g(is)h(a)g(lot)g(of)f(historical)g(baggage)f(that)h(impacts)h
(the)0 2735 y(ordering)i(of)h(the)h(\002elds;)i(be)e(sure)g(your)e
(type)i(initialization)f(k)o(eeps)g(the)h(\002elds)g(in)g(the)g(right)f
(order!)37 b(It')-5 b(s)26 b(often)e(easiest)h(to)g(\002nd)g(an)0
2834 y(e)o(xample)c(that)i(includes)f(all)i(the)f(\002elds)g(you)f
(need)g(\(e)n(v)o(en)g(if)h(the)o(y')l(re)e(initialized)h(to)h
Fm(0)p Ft(\))g(and)f(then)h(change)e(the)i(v)n(alues)f(to)h(suit)h
(your)0 2934 y(ne)n(w)c(type.)416 3172 y Fj(char)640
3185 y(*)685 3172 y(tp_name;)43 b(/)1133 3185 y(*)1223
3172 y(For)h(printing)1805 3185 y(*)1850 3172 y(/)0 3459
y Ft(The)17 b(name)f(of)h(the)h(type)e(-)i(as)g(mentioned)d(in)i(the)g
(last)i(section,)e(this)h(will)g(appear)e(in)h(v)n(arious)f(places,)i
(almost)f(entirely)f(for)h(diagnostic)0 3558 y(purposes.)24
b(T)m(ry)19 b(to)i(choose)e(something)g(that)h(will)h(be)f(helpful)f
(in)h(such)g(a)h(situation!)416 3796 y Fj(int)44 b(tp_basicsize,)f
(tp_itemsize;)f(/)1850 3809 y(*)1940 3796 y(For)i(allocation)2612
3809 y(*)2657 3796 y(/)0 4083 y Ft(These)17 b(\002elds)h(tell)g(the)f
(runtime)f(ho)n(w)h(much)g(memory)e(to)j(allocate)f(when)g(ne)n(w)g
(objects)g(of)g(this)h(type)f(are)g(created.)23 b(Python)16
b(has)i(some)0 4183 y(b)n(uilt-in)h(support)g(for)h(v)n(ariable)f
(length)g(structures)g(\(think:)24 b(strings,)c(lists\))h(which)f(is)h
(where)e(the)h Fm(tp_itemsize)f Ft(\002eld)h(comes)g(in.)0
4282 y(This)g(will)h(be)f(dealt)h(with)f(later)-5 b(.)416
4520 y Fj(char)640 4533 y(*)685 4520 y(tp_doc;)0 4807
y Ft(Here)19 b(you)f(can)h(put)g(a)g(string)g(\(or)g(its)h(address\))e
(that)h(you)g(w)o(ant)g(returned)e(when)i(the)g(Python)f(script)h
(references)f Fm(obj.__doc__)f Ft(to)0 4906 y(retrie)n(v)o(e)i(the)h
(doc)g(string.)0 5053 y(No)n(w)g(we)h(come)e(to)i(the)f(basic)g(type)g
(methods\227the)e(ones)i(most)h(e)o(xtension)d(types)i(will)h
(implement.)p 0 5549 3901 4 v 0 5649 a Fo(2.2.)52 b(T)-10
b(ype)23 b(Methods)3102 b(51)p eop end
%%Page: 52 58
TeXDict begin 52 57 bop 0 83 a Fg(2.2.1)100 b(Finalization)30
b(and)f(De-allocation)179 377 y Fj(destructor)43 b(tp_dealloc;)0
664 y Ft(This)33 b(function)d(is)j(called)g(when)e(the)i(reference)d
(count)i(of)g(the)g(instance)g(of)g(your)f(type)h(is)i(reduced)c(to)j
(zero)f(and)f(the)i(Python)0 763 y(interpreter)22 b(w)o(ants)h(to)h
(reclaim)e(it.)35 b(If)23 b(your)f(type)h(has)h(memory)d(to)j(free)e
(or)h(other)g(clean-up)f(to)h(perform,)f(put)h(it)h(here.)33
b(The)23 b(object)0 863 y(itself)e(needs)f(to)g(be)g(freed)f(here)h(as)
h(well.)k(Here)20 b(is)i(an)e(e)o(xample)e(of)i(this)h(function:)236
1101 y Fj(static)44 b(void)236 1193 y
(newdatatype_dealloc\(newdatatypeobject)1940 1206 y(*)2029
1193 y(obj\))236 1284 y({)416 1375 y
(free\(obj->obj_UnderlyingDatatypePtr\))o(;)416 1467
y(obj->ob_type->tp_free\(obj\);)236 1558 y(})0 1844 y
Ft(One)18 b(important)f(requirement)e(of)j(the)h(deallocator)d
(function)h(is)i(that)f(it)h(lea)n(v)o(es)f(an)o(y)f(pending)g(e)o
(xceptions)f(alone.)24 b(This)18 b(is)h(important)0 1944
y(since)h(deallocators)e(are)h(frequently)e(called)i(as)h(the)g
(interpreter)e(unwinds)g(the)h(Python)g(stack;)g(when)g(the)h(stack)f
(is)h(unw)o(ound)e(due)g(to)0 2044 y(an)h(e)o(xception)d(\(rather)i
(than)g(normal)f(returns\),)g(nothing)g(is)j(done)d(to)i(protect)f(the)
g(deallocators)g(from)f(seeing)h(that)h(an)g(e)o(xception)d(has)0
2143 y(already)22 b(been)g(set.)34 b(An)o(y)23 b(actions)f(which)h(a)g
(deallocator)f(performs)f(which)h(may)h(cause)g(additional)e(Python)h
(code)g(to)i(be)e(e)o(x)o(ecuted)0 2243 y(may)j(detect)g(that)g(an)h(e)
o(xception)d(has)j(been)e(set.)41 b(This)26 b(can)f(lead)g(to)h
(misleading)e(errors)g(from)h(the)g(interpreter)-5 b(.)39
b(The)25 b(proper)f(w)o(ay)0 2342 y(to)d(protect)f(against)g(this)i(is)
g(to)f(sa)n(v)o(e)g(a)h(pending)d(e)o(xception)g(before)g(performing)f
(the)j(unsafe)f(action,)h(and)f(restoring)g(it)i(when)e(done.)0
2442 y(This)g(can)g(be)g(done)g(using)f(the)i Fm(PyErr_Fetch\(\))d
Ft(and)h Fm(PyErr_Restore\(\))f Ft(functions:)236 2680
y Fj(static)44 b(void)236 2772 y(my_dealloc\(PyObject)1133
2785 y(*)1178 2772 y(obj\))236 2863 y({)416 2954 y(MyObject)819
2967 y(*)864 2954 y(self)g(=)h(\(MyObject)1626 2967 y(*)1671
2954 y(\))g(obj;)416 3046 y(PyObject)819 3059 y(*)864
3046 y(cbresult;)416 3228 y(if)f(\(self->my_callback)e(!=)i(NULL\))g({)
595 3320 y(PyObject)998 3333 y(*)1043 3320 y(err_type,)1492
3333 y(*)1537 3320 y(err_value,)2029 3333 y(*)2074 3320
y(err_traceback;)595 3411 y(int)g(have_error)f(=)i(PyErr_Occurred\(\))d
(?)j(1)f(:)h(0;)595 3594 y(if)f(\(have_error\))774 3685
y(PyErr_Fetch\(&err_type,)d(&err_value,)i(&err_traceback\);)595
3868 y(cbresult)g(=)i(PyObject_CallObject\(self->my_callback,)38
b(NULL\);)595 3959 y(if)44 b(\(cbresult)g(==)g(NULL\))774
4050 y(PyErr_WriteUnraisable\(self->my_callback\))o(;)595
4141 y(else)774 4233 y(Py_DECREF\(cbresult\);)595 4415
y(if)g(\(have_error\))774 4507 y(PyErr_Restore\(err_type,)d(err_value,)
i(err_traceback\);)595 4689 y(Py_DECREF\(self->my_callback\);)416
4781 y(})416 4872 y(obj->ob_type->tp_free\(\(PyObject)1811
4885 y(*)1856 4872 y(\)sel)o(f\);)236 4963 y(})p 0 5549
3901 4 v 0 5649 a Fo(52)2614 b(Chapter)24 b(2.)52 b(De\002ning)23
b(Ne)n(w)h(T)-10 b(ypes)p eop end
%%Page: 53 59
TeXDict begin 53 58 bop 0 83 a Fg(2.2.2)100 b(Object)28
b(Presentation)0 286 y Ft(In)21 b(Python,)g(there)g(are)g(three)g(w)o
(ays)h(to)g(generate)e(a)i(te)o(xtual)f(representation)f(of)h(an)g
(object:)28 b(the)22 b Fm(repr\(\))f Ft(function)e(\(or)i(equi)n(v)n
(alent)0 386 y(back-tick)14 b(syntax\),)i(the)f Fm(str\(\))h
Ft(function,)e(and)h(the)h Fm(print)g Ft(statement.)23
b(F)o(or)15 b(most)h(objects,)g(the)g Fm(print)f Ft(statement)h(is)g
(equi)n(v)n(alent)0 485 y(to)21 b(the)g Fm(str\(\))g
Ft(function,)e(b)n(ut)i(it)h(is)g(possible)f(to)g(special-case)g
(printing)e(to)i(a)h Fm(FILE)2502 500 y(*)2572 485 y
Ft(if)g(necessary;)f(this)g(should)f(only)g(be)h(done)f(if)0
585 y(ef)n(\002cienc)o(y)d(is)j(identi\002ed)e(as)i(a)f(problem)f(and)g
(pro\002ling)f(suggests)i(that)g(creating)f(a)h(temporary)e(string)i
(object)f(to)h(be)g(written)g(to)g(a)g(\002le)0 684 y(is)i(too)f(e)o
(xpensi)n(v)o(e.)0 831 y(These)g(handlers)f(are)h(all)h(optional,)e
(and)g(most)h(types)g(at)h(most)f(need)g(to)g(implement)f(the)h
Fm(tp_str)g Ft(and)g Fm(tp_repr)f Ft(handlers.)416 1070
y Fj(reprfunc)43 b(tp_repr;)416 1161 y(reprfunc)g(tp_str;)416
1252 y(printfunc)g(tp_print;)0 1539 y Ft(The)22 b Fm(tp_repr)f
Ft(handler)f(should)h(return)g(a)h(string)g(object)f(containing)f(a)i
(representation)e(of)i(the)f(instance)h(for)f(which)h(it)g(is)h
(called.)0 1638 y(Here)d(is)h(a)g(simple)f(e)o(xample:)236
1876 y Fj(static)44 b(PyObject)954 1889 y(*)236 1968
y(newdatatype_repr\(newdatatypeobject)1805 1981 y(*)1895
1968 y(obj\))236 2059 y({)416 2150 y(return)f
(PyString_FromFormat\("Repr-ified_newdataty)o(pe{{si)o(ze:\\\045d)o
(}}",)1626 2242 y(obj->obj_UnderlyingDatatypePtr->size\);)236
2333 y(})0 2620 y Ft(If)22 b(no)g Fm(tp_repr)g Ft(handler)f(is)i
(speci\002ed,)g(the)f(interpreter)f(will)i(supply)f(a)h(representation)
d(that)j(uses)g(the)f(type')-5 b(s)23 b Fm(tp_name)e
Ft(and)h(a)0 2719 y(uniquely-identifying)15 b(v)n(alue)20
b(for)f(the)i(object.)0 2866 y(The)c Fm(tp_str)g Ft(handler)e(is)j(to)g
Fm(str\(\))f Ft(what)g(the)g Fm(tp_repr)g Ft(handler)e(described)h(abo)
o(v)o(e)g(is)i(to)f Fm(repr\(\))p Ft(;)h(that)g(is,)g(it)g(is)g(called)
f(when)0 2966 y(Python)i(code)h(calls)h Fm(str\(\))e
Ft(on)h(an)g(instance)g(of)g(your)f(object.)25 b(Its)c(implementation)d
(is)j(v)o(ery)e(similar)h(to)h(the)f Fm(tp_repr)g Ft(function,)0
3065 y(b)n(ut)f(the)h(resulting)e(string)h(is)h(intended)e(for)g(human)
g(consumption.)k(If)d Fm(tp_str)g Ft(is)h(not)f(speci\002ed,)g(the)g
Fm(tp_repr)g Ft(handler)f(is)i(used)0 3165 y(instead.)0
3312 y(Here)g(is)h(a)g(simple)f(e)o(xample:)236 3550
y Fj(static)44 b(PyObject)954 3563 y(*)236 3641 y
(newdatatype_str\(newdatatypeobject)1761 3654 y(*)1850
3641 y(obj\))236 3733 y({)416 3824 y(return)f
(PyString_FromFormat\("Stringified_newdatat)o(ype{{s)o(ize:\\\045)o
(d}}",)1626 3915 y(obj->obj_UnderlyingDatatypePtr->size\);)236
4007 y(})0 4293 y Ft(The)22 b(print)g(function)f(will)i(be)f(called)h
(whene)n(v)o(er)d(Python)h(needs)h(to)h(\224print\224)e(an)i(instance)f
(of)g(the)h(type.)31 b(F)o(or)22 b(e)o(xample,)f(if)i('node')e(is)0
4393 y(an)f(instance)g(of)g(type)g(T)m(reeNode,)e(then)i(the)g(print)g
(function)e(is)j(called)f(when)g(Python)f(code)g(calls:)236
4631 y Fj(print)44 b(node)0 4918 y Ft(There)27 b(is)h(a)g(\003ags)g(ar)
o(gument)e(and)h(one)g(\003ag,)i Fm(Py_PRINT_RAW)p Ft(,)d(and)h(it)h
(suggests)g(that)f(you)g(print)g(without)g(string)g(quotes)g(and)0
5017 y(possibly)20 b(without)f(interpreting)f(escape)i(sequences.)0
5164 y(The)g(print)f(function)g(recei)n(v)o(es)g(a)i(\002le)g(object)e
(as)i(an)f(ar)o(gument.)j(Y)-9 b(ou)20 b(will)h(lik)o(ely)f(w)o(ant)g
(to)g(write)h(to)f(that)g(\002le)h(object.)0 5311 y(Here)f(is)h(a)g
(sample)f(print)f(function:)p 0 5549 3901 4 v 0 5649
a Fo(2.2.)52 b(T)-10 b(ype)23 b(Methods)3102 b(53)p eop
end
%%Page: 54 60
TeXDict begin 54 59 bop 236 83 a Fj(static)44 b(int)236
174 y(newdatatype_print\(newdatatypeobject)1850 187 y(*)1895
174 y(obj,)g(FILE)2343 187 y(*)2388 174 y(fp,)g(int)h(flags\))236
266 y({)416 357 y(if)f(\(flags)g(&)h(Py_PRINT_RAW\))d({)595
448 y(fprintf\(fp,)h("<{newdatatype)f(object--size:)h(\045d}>",)954
540 y(obj->obj_UnderlyingDatatypePtr->size)o(\);)416
631 y(})416 722 y(else)h({)595 814 y(fprintf\(fp,)f("\\"<{newdatatype)f
(object--size:)h(\045d}>\\"",)954 905 y
(obj->obj_UnderlyingDatatypePtr->size)o(\);)416 996 y(})416
1088 y(return)g(0;)236 1179 y(})0 1600 y Fg(2.2.3)100
b(Attr)q(ib)n(ute)29 b(Management)0 1803 y Ft(F)o(or)20
b(e)n(v)o(ery)g(object)g(which)g(can)g(support)g(attrib)n(utes,)g(the)h
(corresponding)c(type)j(must)h(pro)o(vide)d(the)j(functions)e(that)i
(control)e(ho)n(w)i(the)0 1902 y(attrib)n(utes)i(are)f(resolv)o(ed.)31
b(There)22 b(needs)g(to)h(be)f(a)h(function)e(which)h(can)h(retrie)n(v)
o(e)e(attrib)n(utes)i(\(if)f(an)o(y)g(are)h(de\002ned\),)e(and)h
(another)f(to)0 2002 y(set)g(attrib)n(utes)g(\(if)f(setting)h(attrib)n
(utes)f(is)i(allo)n(wed\).)j(Remo)o(ving)19 b(an)h(attrib)n(ute)h(is)g
(a)g(special)g(case,)g(for)f(which)g(the)g(ne)n(w)h(v)n(alue)f(passed)0
2102 y(to)g(the)h(handler)d(is)j Fm(NULL)p Ft(.)0 2248
y(Python)g(supports)g(tw)o(o)i(pairs)f(of)g(attrib)n(ute)g(handlers;)g
(a)h(type)f(that)g(supports)g(attrib)n(utes)g(only)f(needs)h(to)h
(implement)e(the)h(functions)0 2348 y(for)27 b(one)g(pair)-5
b(.)46 b(The)27 b(dif)n(ference)e(is)k(that)e(one)g(pair)g(tak)o(es)h
(the)f(name)g(of)g(the)g(attrib)n(ute)g(as)h(a)g Fm(char)2951
2363 y(*)3001 2348 y Ft(,)h(while)e(the)g(other)g(accepts)g(a)0
2448 y Fm(PyObject)400 2463 y(*)450 2448 y Ft(.)d(Each)c(type)f(can)h
(use)h(whiche)n(v)o(er)d(pair)i(mak)o(es)g(more)f(sense)i(for)e(the)h
(implementation')-5 b(s)19 b(con)m(v)o(enience.)416 2595
y Fj(getattrfunc)87 b(tp_getattr;)357 b(/)1895 2608 y(*)1985
2595 y(char)2209 2608 y(*)2298 2595 y(version)2657 2608
y(*)2702 2595 y(/)416 2686 y(setattrfunc)87 b(tp_setattr;)416
2777 y(/)461 2790 y(*)550 2777 y(...)729 2790 y(*)774
2777 y(/)416 2869 y(getattrofunc)42 b(tp_getattrofunc;)132
b(/)1895 2882 y(*)1985 2869 y(PyObject)2388 2882 y(*)2478
2869 y(version)2836 2882 y(*)2881 2869 y(/)416 2960 y(setattrofunc)42
b(tp_setattrofunc;)0 3246 y Ft(If)29 b(accessing)h(attrib)n(utes)f(of)h
(an)f(object)g(is)i(al)o(w)o(ays)f(a)g(simple)f(operation)f(\(this)i
(will)g(be)g(e)o(xplained)d(shortly\),)k(there)e(are)h(generic)0
3346 y(implementations)21 b(which)h(can)g(be)h(used)g(to)f(pro)o(vide)f
(the)i Fm(PyObject)2107 3361 y(*)2178 3346 y Ft(v)o(ersion)f(of)g(the)h
(attrib)n(ute)f(management)f(functions.)31 b(The)0 3446
y(actual)23 b(need)f(for)g(type-speci\002c)f(attrib)n(ute)i(handlers)f
(almost)g(completely)g(disappeared)e(starting)j(with)g(Python)f(2.2,)g
(though)f(there)0 3545 y(are)f(man)o(y)f(e)o(xamples)g(which)h(ha)n(v)o
(e)f(not)h(been)g(updated)e(to)j(use)f(some)g(of)g(the)g(ne)n(w)g
(generic)f(mechanism)g(that)i(is)g(a)n(v)n(ailable.)0
3811 y Fo(Gener)q(ic)i(Attr)q(ib)n(ute)i(Management)0
4014 y Ft(Ne)n(w)20 b(in)h(v)o(ersion)e(2.2.)0 4161 y(Most)26
b(e)o(xtension)f(types)h(only)f(use)i Fq(simple)f Ft(attrib)n(utes.)43
b(So,)27 b(what)g(mak)o(es)f(the)g(attrib)n(utes)g(simple?)43
b(There)25 b(are)h(only)f(a)i(couple)e(of)0 4261 y(conditions)19
b(that)h(must)g(be)g(met:)104 4477 y(1.)41 b(The)19 b(name)h(of)g(the)g
(attrib)n(utes)g(must)g(be)h(kno)n(wn)d(when)i Fm(PyType_Ready\(\))e
Ft(is)j(called.)104 4638 y(2.)41 b(No)20 b(special)g(processing)f(is)i
(needed)d(to)j(record)d(that)j(an)f(attrib)n(ute)f(w)o(as)i(look)o(ed)e
(up)h(or)g(set,)g(nor)g(do)f(actions)h(need)g(to)g(be)g(tak)o(en)208
4738 y(based)f(on)h(the)g(v)n(alue.)0 4954 y(Note)g(that)g(this)g(list)
h(does)e(not)h(place)f(an)o(y)g(restrictions)h(on)f(the)h(v)n(alues)f
(of)h(the)f(attrib)n(utes,)h(when)f(the)h(v)n(alues)f(are)h(computed,)e
(or)h(ho)n(w)0 5054 y(rele)n(v)n(ant)g(data)h(is)h(stored.)0
5201 y(When)f Fm(PyType_Ready\(\))f Ft(is)j(called,)e(it)h(uses)h
(three)e(tables)h(referenced)d(by)i(the)h(type)f(object)g(to)h(create)g
Fq(descriptor)o(s)g Ft(which)f(are)0 5300 y(placed)e(in)g(the)h
(dictionary)e(of)h(the)g(type)g(object.)24 b(Each)18
b(descriptor)f(controls)h(access)h(to)f(one)g(attrib)n(ute)g(of)h(the)f
(instance)g(object.)24 b(Each)0 5400 y(of)g(the)g(tables)h(is)g
(optional;)h(if)e(all)h(three)f(are)g Fm(NULL)p Ft(,)g(instances)h(of)f
(the)g(type)g(will)h(only)f(ha)n(v)o(e)f(attrib)n(utes)i(that)f(are)h
(inherited)e(from)p 0 5549 3901 4 v 0 5649 a Fo(54)2614
b(Chapter)24 b(2.)52 b(De\002ning)23 b(Ne)n(w)h(T)-10
b(ypes)p eop end
%%Page: 55 61
TeXDict begin 55 60 bop 0 83 a Ft(their)18 b(base)f(type,)h(and)f
(should)g(lea)n(v)o(e)h(the)g Fm(tp_getattro)e Ft(and)h
Fm(tp_setattro)f Ft(\002elds)j Fm(NULL)e Ft(as)i(well,)f(allo)n(wing)f
(the)h(base)g(type)0 183 y(to)i(handle)f(attrib)n(utes.)0
330 y(The)h(tables)g(are)g(declared)f(as)i(three)f(\002elds)h(of)f(the)
g(type)f(object:)416 568 y Fj(struct)43 b(PyMethodDef)1267
581 y(*)1312 568 y(tp_methods;)416 659 y(struct)g(PyMemberDef)1267
672 y(*)1312 659 y(tp_members;)416 750 y(struct)g(PyGetSetDef)1267
763 y(*)1312 750 y(tp_getset;)0 1037 y Ft(If)25 b Fm(tp_methods)f
Ft(is)i(not)f Fm(NULL)p Ft(,)f(it)i(must)f(refer)g(to)g(an)g(array)f
(of)h Fm(PyMethodDef)f Ft(structures.)39 b(Each)25 b(entry)f(in)h(the)g
(table)h(is)g(an)0 1137 y(instance)20 b(of)g(this)g(structure:)236
1375 y Fj(typedef)44 b(struct)g(PyMethodDef)f({)416 1466
y(char)954 1479 y(*)999 1466 y(ml_name;)312 b(/)1716
1479 y(*)1805 1466 y(method)44 b(name)2343 1479 y(*)2388
1466 y(/)416 1557 y(PyCFunction)87 b(ml_meth;)313 b(/)1716
1570 y(*)1805 1557 y(implementation)43 b(function)2881
1570 y(*)2926 1557 y(/)416 1649 y(int)447 b(ml_flags;)268
b(/)1716 1662 y(*)1805 1649 y(flags)2074 1662 y(*)2119
1649 y(/)416 1740 y(char)954 1753 y(*)999 1740 y(ml_doc;)357
b(/)1716 1753 y(*)1805 1740 y(docstring)2254 1753 y(*)2299
1740 y(/)236 1831 y(})45 b(PyMethodDef;)0 2118 y Ft(One)22
b(entry)f(should)g(be)h(de\002ned)f(for)g(each)h(method)e(pro)o(vided)g
(by)h(the)h(type;)h(no)e(entries)h(are)g(needed)f(for)g(methods)g
(inherited)g(from)0 2217 y(a)h(base)g(type.)30 b(One)22
b(additional)f(entry)g(is)i(needed)d(at)j(the)f(end;)g(it)h(is)g(a)f
(sentinel)g(that)g(marks)g(the)g(end)f(of)h(the)g(array)-5
b(.)29 b(The)21 b Fm(ml_name)0 2317 y Ft(\002eld)f(of)g(the)g(sentinel)
h(must)f(be)g Fm(NULL)p Ft(.)0 2464 y(XXX)h(Need)f(to)g(refer)f(to)i
(some)f(uni\002ed)f(discussion)h(of)g(the)g(structure)f(\002elds,)i
(shared)e(with)h(the)h(ne)o(xt)e(section.)0 2611 y(The)g(second)f
(table)h(is)h(used)f(to)g(de\002ne)g(attrib)n(utes)g(which)f(map)h
(directly)f(to)h(data)g(stored)g(in)g(the)g(instance.)24
b(A)c(v)n(ariety)e(of)h(primiti)n(v)o(e)f(C)0 2710 y(types)i(are)g
(supported,)e(and)i(access)h(may)e(be)h(read-only)e(or)i(read-write.)k
(The)c(structures)f(in)i(the)f(table)g(are)g(de\002ned)f(as:)236
2857 y Fj(typedef)44 b(struct)g(PyMemberDef)f({)416 2949
y(char)640 2962 y(*)685 2949 y(name;)416 3040 y(int)134
b(type;)416 3131 y(int)g(offset;)416 3223 y(int)g(flags;)416
3314 y(char)640 3327 y(*)685 3314 y(doc;)236 3405 y(})45
b(PyMemberDef;)0 3692 y Ft(F)o(or)20 b(each)g(entry)f(in)h(the)g
(table,)g(a)h(descriptor)d(will)j(be)f(constructed)f(and)g(added)g(to)i
(the)f(type)f(which)h(will)h(be)f(able)g(to)g(e)o(xtract)g(a)g(v)n
(alue)0 3791 y(from)27 b(the)h(instance)g(structure.)47
b(The)28 b Fm(type)g Ft(\002eld)g(should)f(contain)g(one)h(of)g(the)g
(type)f(codes)h(de\002ned)f(in)h(the)g(`)p Fu(str)q(uctmember)l(.h)p
Ft(')0 3891 y(header;)c(the)f(v)n(alue)f(will)i(be)f(used)g(to)h
(determine)e(ho)n(w)g(to)i(con)m(v)o(ert)d(Python)h(v)n(alues)h(to)g
(and)g(from)f(C)i(v)n(alues.)34 b(The)22 b Fm(flags)h
Ft(\002eld)h(is)0 3991 y(used)c(to)g(store)g(\003ags)h(which)f(control)
f(ho)n(w)g(the)h(attrib)n(ute)g(can)g(be)g(accessed.)0
4138 y(XXX)h(Need)f(to)g(mo)o(v)o(e)f(some)h(of)g(this)g(to)h(a)f
(shared)g(section!)0 4284 y(The)g(follo)n(wing)e(\003ag)j(constants)e
(are)i(de\002ned)e(in)h(`)p Fu(str)q(uctmember)l(.h)p
Ft(';)f(the)o(y)g(may)h(be)g(combined)e(using)i(bitwise-OR.)752
4511 y Fr(Constant)p 1647 4541 4 100 v 622 w(Meaning)p
702 4544 2497 4 v 752 4614 a Fm(READONLY)p 1647 4644
4 100 v 546 w Ft(Ne)n(v)o(er)f(writable.)752 4713 y Fm(RO)p
1647 4743 V 846 w Ft(Shorthand)f(for)i Fm(READONLY)p
Ft(.)752 4813 y Fm(READ_RESTRICTED)p 1647 4843 V 196
w Ft(Not)g(readable)f(in)i(restricted)e(mode.)752 4913
y Fm(WRITE_RESTRICTED)p 1647 4942 V 146 w Ft(Not)h(writable)g(in)h
(restricted)e(mode.)752 5012 y Fm(RESTRICTED)p 1647 5042
V 446 w Ft(Not)h(readable)f(or)h(writable)g(in)g(restricted)g(mode.)0
5237 y(An)28 b(interesting)f(adv)n(antage)f(of)h(using)g(the)h
Fm(tp_members)f Ft(table)g(to)h(b)n(uild)g(descriptors)f(that)g(are)h
(used)g(at)g(runtime)f(is)h(that)g(an)o(y)0 5337 y(attrib)n(ute)22
b(de\002ned)g(this)h(w)o(ay)g(can)f(ha)n(v)o(e)g(an)h(associated)f(doc)
h(string)f(simply)g(by)g(pro)o(viding)e(the)j(te)o(xt)g(in)f(the)h
(table.)32 b(An)23 b(application)p 0 5549 3901 4 v 0
5649 a Fo(2.2.)52 b(T)-10 b(ype)23 b(Methods)3102 b(55)p
eop end
%%Page: 56 62
TeXDict begin 56 61 bop 0 83 a Ft(can)18 b(use)g(the)g(introspection)e
(API)i(to)g(retrie)n(v)o(e)f(the)h(descriptor)e(from)h(the)h(class)h
(object,)f(and)f(get)h(the)g(doc)f(string)h(using)f(its)i
Fm(__doc_-)0 183 y(_)i Ft(attrib)n(ute.)0 330 y(As)g(with)f(the)h
Fm(tp_methods)d Ft(table,)i(a)h(sentinel)f(entry)f(with)i(a)f
Fm(name)g Ft(v)n(alue)g(of)g Fm(NULL)g Ft(is)h(required.)0
598 y Fo(T)-10 b(ype-speci\002c)21 b(Attr)q(ib)n(ute)k(Management)0
801 y Ft(F)o(or)15 b(simplicity)-5 b(,)16 b(only)f(the)g
Fm(char)976 816 y(*)1042 801 y Ft(v)o(ersion)f(will)j(be)e
(demonstrated)f(here;)i(the)g(type)f(of)h(the)f(name)g(parameter)f(is)j
(the)f(only)f(dif)n(ference)0 900 y(between)25 b(the)g
Fm(char)630 915 y(*)705 900 y Ft(and)g Fm(PyObject)1251
915 y(*)1326 900 y Ft(\003a)n(v)n(ors)g(of)g(the)h(interf)o(ace.)40
b(This)26 b(e)o(xample)e(ef)n(fecti)n(v)o(ely)f(does)i(the)h(same)g
(thing)e(as)j(the)0 1000 y(generic)22 b(e)o(xample)g(abo)o(v)o(e,)g(b)n
(ut)i(does)f(not)g(use)h(the)f(generic)f(support)g(added)g(in)i(Python)
e(2.2.)34 b(The)23 b(v)n(alue)g(in)g(sho)n(wing)f(this)i(is)h(tw)o(o-)0
1100 y(fold:)e(it)18 b(demonstrates)e(ho)n(w)h(basic)h(attrib)n(ute)f
(management)e(can)i(be)h(done)e(in)i(a)g(w)o(ay)f(that)h(is)g(portable)
e(to)i(older)e(v)o(ersions)h(of)g(Python,)0 1199 y(and)f(e)o(xplains)g
(ho)n(w)g(the)h(handler)f(functions)f(are)i(called,)g(so)g(that)g(if)g
(you)f(do)h(need)f(to)h(e)o(xtend)e(their)i(functionality)-5
b(,)15 b(you')o(ll)h(understand)0 1299 y(what)k(needs)g(to)g(be)g
(done.)0 1446 y(The)i Fm(tp_getattr)g Ft(handler)f(is)i(called)g(when)f
(the)g(object)g(requires)g(an)g(attrib)n(ute)h(look-up.)30
b(It)23 b(is)g(called)g(in)f(the)h(same)g(situations)0
1545 y(where)d(the)g Fm(__getattr__\(\))e Ft(method)h(of)h(a)g(class)i
(w)o(ould)d(be)h(called.)0 1692 y(A)36 b(lik)o(ely)f(w)o(ay)h(to)g
(handle)e(this)i(is)h(\(1\))e(to)h(implement)e(a)i(set)g(of)g
(functions)e(\(such)h(as)h Fm(newdatatype_getSize\(\))c
Ft(and)0 1792 y Fm(newdatatype_setSize\(\))27 b Ft(in)j(the)h(e)o
(xample)e(belo)n(w\),)i(\(2\))f(pro)o(vide)e(a)j(method)e(table)h
(listing)h(these)f(functions,)h(and)f(\(3\))0 1891 y(pro)o(vide)19
b(a)i(getattr)g(function)e(that)i(returns)f(the)h(result)g(of)g(a)g
(lookup)f(in)h(that)g(table.)27 b(The)21 b(method)e(table)i(uses)h(the)
f(same)g(structure)f(as)0 1991 y(the)g Fm(tp_methods)f
Ft(\002eld)h(of)g(the)h(type)e(object.)0 2138 y(Here)h(is)h(an)f(e)o
(xample:)236 2376 y Fj(static)44 b(PyMethodDef)f(newdatatype_methods[])
e(=)k({)416 2467 y({"getSize",)e(\(PyCFunction\)newdatatype_getSize,)c
(METH_VARARGS,)460 2559 y("Return)44 b(the)g(current)g(size."},)416
2650 y({"setSize",)f(\(PyCFunction\)newdatatype_setSize,)c
(METH_VARARGS,)460 2741 y("Set)45 b(the)f(size."},)416
2833 y({NULL,)f(NULL,)h(0,)h(NULL})492 b(/)1895 2846
y(*)1985 2833 y(sentinel)2388 2846 y(*)2433 2833 y(/)236
2924 y(};)236 3107 y(static)44 b(PyObject)954 3120 y(*)236
3198 y(newdatatype_getattr\(newdatatypeobject)1940 3211
y(*)1985 3198 y(obj,)g(char)2433 3211 y(*)2478 3198 y(name\))236
3289 y({)416 3381 y(return)f(Py_FindMethod\(newdatatype_methods,)c
(\(PyObject)2747 3394 y(*)2792 3381 y(\)obj,)44 b(name\);)236
3472 y(})0 3759 y Ft(The)23 b Fm(tp_setattr)f Ft(handler)g(is)j(called)
e(when)g(the)g Fm(__setattr__\(\))f Ft(or)h Fm(__delattr__\(\))e
Ft(method)i(of)g(a)h(class)g(instance)0 3858 y(w)o(ould)j(be)h(called.)
49 b(When)28 b(an)g(attrib)n(ute)f(should)g(be)h(deleted,)h(the)f
(third)g(parameter)e(will)j(be)f Fm(NULL)p Ft(.)g(Here)g(is)h(an)f(e)o
(xample)e(that)0 3958 y(simply)20 b(raises)h(an)f(e)o(xception;)e(if)j
(this)f(were)g(really)g(all)h(you)e(w)o(anted,)h(the)g
Fm(tp_setattr)f Ft(handler)g(should)g(be)h(set)h(to)f
Fm(NULL)p Ft(.)236 4105 y Fj(static)44 b(int)236 4196
y(newdatatype_setattr\(newdatatypeobject)1940 4209 y(*)1985
4196 y(obj,)g(char)2433 4209 y(*)2478 4196 y(name,)g(PyObject)3150
4209 y(*)3195 4196 y(v\))236 4287 y({)416 4379 y
(\(void\)PyErr_Format\(PyExc_RuntimeErro)o(r,)39 b("Read-only)k
(attribute:)g(\\\045s",)h(name\);)416 4470 y(return)f(-1;)236
4561 y(})0 4986 y Fg(2.2.4)100 b(Object)28 b(Compar)q(ison)179
5189 y Fj(cmpfunc)44 b(tp_compare;)p 0 5549 3901 4 v
0 5649 a Fo(56)2614 b(Chapter)24 b(2.)52 b(De\002ning)23
b(Ne)n(w)h(T)-10 b(ypes)p eop end
%%Page: 57 63
TeXDict begin 57 62 bop 0 83 a Ft(The)27 b Fm(tp_compare)e
Ft(handler)h(is)i(called)f(when)f(comparisons)f(are)i(needed)f(and)h
(the)g(object)f(does)h(not)g(implement)e(the)i(speci\002c)0
183 y(rich)18 b(comparison)d(method)i(which)g(matches)h(the)g
(requested)f(comparison.)22 b(\(It)c(is)g(al)o(w)o(ays)h(used)e(if)i
(de\002ned)e(and)g(the)h Fm(PyObject_-)0 282 y(Compare\(\))25
b Ft(or)h Fm(PyObject_Cmp\(\))e Ft(functions)h(are)h(used,)h(or)e(if)i
Fm(cmp\(\))f Ft(is)h(used)e(from)g(Python.\))42 b(It)26
b(is)h(analogous)d(to)j(the)0 382 y Fm(__cmp__\(\))d
Ft(method.)38 b(This)25 b(function)f(should)g(return)g
Fm(-1)h Ft(if)g Fq(obj1)g Ft(is)h(less)g(than)e Fq(obj2)p
Ft(,)h Fm(0)h Ft(if)f(the)o(y)g(are)g(equal,)g(and)g
Fm(1)g Ft(if)g Fq(obj1)g Ft(is)0 482 y(greater)c(than)g
Fq(obj2)p Ft(.)29 b(\(It)22 b(w)o(as)h(pre)n(viously)d(allo)n(wed)h(to)
h(return)f(arbitrary)f(ne)o(gati)n(v)o(e)g(or)h(positi)n(v)o(e)h(inte)o
(gers)f(for)g(less)i(than)e(and)h(greater)0 581 y(than,)27
b(respecti)n(v)o(ely;)g(as)f(of)g(Python)f(2.2,)h(this)h(is)f(no)g
(longer)e(allo)n(wed.)42 b(In)25 b(the)h(future,)g(other)f(return)g(v)n
(alues)h(may)f(be)h(assigned)f(a)0 681 y(dif)n(ferent)19
b(meaning.\))0 828 y(A)j Fm(tp_compare)e Ft(handler)h(may)g(raise)h(an)
f(e)o(xception.)27 b(In)22 b(this)g(case)g(it)g(should)f(return)f(a)i
(ne)o(gati)n(v)o(e)e(v)n(alue.)28 b(The)22 b(caller)f(has)h(to)g(test)0
927 y(for)e(the)g(e)o(xception)e(using)i Fm(PyErr_Occurred\(\))p
Ft(.)0 1074 y(Here)g(is)h(a)g(sample)f(implementation:)236
1221 y Fj(static)44 b(int)236 1312 y
(newdatatype_compare\(newdatatypeobject)1940 1325 y(*)2029
1312 y(obj1,)g(newdatatypeobject)3105 1325 y(*)3195 1312
y(obj2\))236 1404 y({)416 1495 y(long)g(result;)416 1678
y(if)g(\(obj1->obj_UnderlyingDatatypePtr->size)38 b(<)595
1769 y(obj2->obj_UnderlyingDatatypePtr->size\))g({)595
1860 y(result)44 b(=)g(-1;)416 1952 y(})416 2043 y(else)g(if)g
(\(obj1->obj_UnderlyingDatatypePtr->size)39 b(>)819 2134
y(obj2->obj_UnderlyingDatatypePtr->size\))f({)595 2226
y(result)44 b(=)g(1;)416 2317 y(})416 2408 y(else)g({)595
2500 y(result)g(=)g(0;)416 2591 y(})416 2682 y(return)f(result;)236
2774 y(})0 3198 y Fg(2.2.5)100 b(Abstr)o(act)28 b(Protocol)h(Suppor)t
(t)0 3401 y Ft(Python)e(supports)g(a)h(v)n(ariety)f(of)h
Fq(abstr)o(act)g Ft(`protocols;')i(the)e(speci\002c)g(interf)o(aces)f
(pro)o(vided)f(to)i(use)g(these)h(interf)o(aces)e(are)h(docu-)0
3501 y(mented)19 b(in)i(the)f Fq(Python/C)f(API)h(Refer)m(ence)g
(Manual)f Ft(in)h(the)g(chapter)f(\223)-7 b(Abstract)21
b(Objects)f(Layer)-5 b(.)f(\224)0 3648 y(A)19 b(number)d(of)j(these)f
(abstract)g(interf)o(aces)g(were)g(de\002ned)g(early)g(in)g(the)g(de)n
(v)o(elopment)e(of)i(the)g(Python)g(implementation.)k(In)c(particu-)0
3747 y(lar)m(,)i(the)g(number)m(,)e(mapping,)g(and)i(sequence)f
(protocols)g(ha)n(v)o(e)g(been)h(part)g(of)f(Python)h(since)g(the)g(be)
o(ginning.)j(Other)c(protocols)g(ha)n(v)o(e)0 3847 y(been)e(added)f(o)o
(v)o(er)g(time.)25 b(F)o(or)17 b(protocols)f(which)h(depend)f(on)h(se)n
(v)o(eral)g(handler)f(routines)g(from)h(the)g(type)g(implementation,)f
(the)h(older)0 3947 y(protocols)23 b(ha)n(v)o(e)h(been)f(de\002ned)h
(as)h(optional)e(blocks)g(of)h(handlers)g(referenced)e(by)i(the)g(type)
g(object.)37 b(F)o(or)24 b(ne)n(wer)f(protocols)g(there)0
4046 y(are)d(additional)f(slots)i(in)f(the)g(main)g(type)g(object,)f
(with)h(a)h(\003ag)f(bit)g(being)f(set)i(to)g(indicate)e(that)h(the)h
(slots)f(are)h(present)e(and)h(should)f(be)0 4146 y(check)o(ed)k(by)h
(the)g(interpreter)-5 b(.)35 b(\(The)24 b(\003ag)g(bit)g(does)g(not)g
(indicate)f(that)h(the)h(slot)f(v)n(alues)g(are)g(non-)p
Fm(NULL)p Ft(.)e(The)i(\003ag)g(may)f(be)h(set)h(to)0
4245 y(indicate)20 b(the)g(presence)f(of)h(a)g(slot,)h(b)n(ut)f(a)h
(slot)g(may)e(still)j(be)e(un\002lled.\))416 4392 y Fj(PyNumberMethods)
132 b(tp_as_number;)416 4484 y(PySequenceMethods)42 b(tp_as_sequence;)
416 4575 y(PyMappingMethods)87 b(tp_as_mapping;)0 4861
y Ft(If)20 b(you)f(wish)h(your)e(object)i(to)g(be)f(able)h(to)g(act)g
(lik)o(e)g(a)h(number)m(,)c(a)j(sequence,)f(or)g(a)i(mapping)d(object,)
h(then)g(you)g(place)h(the)f(address)h(of)0 4961 y(a)k(structure)e
(that)h(implements)f(the)h(C)h(type)f Fm(PyNumberMethods)p
Ft(,)e Fm(PySequenceMethods)p Ft(,)g(or)i Fm(PyMappingMethods)p
Ft(,)0 5061 y(respecti)n(v)o(ely)-5 b(.)30 b(It)23 b(is)g(up)f(to)h
(you)f(to)g(\002ll)i(in)e(this)h(structure)f(with)h(appropriate)d(v)n
(alues.)31 b(Y)-9 b(ou)22 b(can)g(\002nd)h(e)o(xamples)e(of)h(the)h
(use)g(of)f(each)0 5160 y(of)e(these)g(in)h(the)f(`)p
Fu(Objects)p Ft(')f(directory)g(of)h(the)g(Python)f(source)g(distrib)n
(ution.)p 0 5549 3901 4 v 0 5649 a Fo(2.2.)52 b(T)-10
b(ype)23 b(Methods)3102 b(57)p eop end
%%Page: 58 64
TeXDict begin 58 63 bop 416 83 a Fj(hashfunc)43 b(tp_hash;)0
370 y Ft(This)25 b(function,)f(if)i(you)e(choose)g(to)h(pro)o(vide)e
(it,)j(should)e(return)g(a)i(hash)e(number)f(for)i(an)g(instance)f(of)h
(your)f(data)h(type.)38 b(Here)25 b(is)h(a)0 469 y(moderately)18
b(pointless)i(e)o(xample:)236 616 y Fj(static)44 b(long)236
707 y(newdatatype_hash\(newdatatypeobject)1805 720 y(*)1850
707 y(obj\))236 799 y({)416 890 y(long)g(result;)416
981 y(result)f(=)i(obj->obj_UnderlyingDatatypePtr->size;)416
1073 y(result)e(=)i(result)1133 1086 y(*)1223 1073 y(3;)416
1164 y(return)e(result;)236 1255 y(})416 1532 y(ternaryfunc)g(tp_call;)
0 1819 y Ft(This)19 b(function)e(is)j(called)f(when)f(an)h(instance)f
(of)h(your)e(data)i(type)g(is)g(\224called\224,)g(for)f(e)o(xample,)g
(if)h Fm(obj1)f Ft(is)i(an)f(instance)f(of)h(your)f(data)0
1919 y(type)i(and)f(the)i(Python)e(script)h(contains)f
Fm(obj1\('hello'\))p Ft(,)f(the)j Fm(tp_call)e Ft(handler)g(is)i(in)m
(v)n(ok)o(ed.)0 2065 y(This)f(function)f(tak)o(es)h(three)g(ar)o
(guments:)104 2295 y(1.)41 b Fq(ar)m(g1)20 b Ft(is)h(the)g(instance)g
(of)f(the)h(data)g(type)f(which)g(is)i(the)f(subject)f(of)h(the)g
(call.)27 b(If)20 b(the)h(call)g(is)h Fm(obj1\('hello'\))p
Ft(,)d(then)h Fq(ar)m(g1)208 2395 y Ft(is)h Fm(obj1)p
Ft(.)104 2561 y(2.)41 b Fq(ar)m(g2)30 b Ft(is)i(a)f(tuple)g(containing)
e(the)i(ar)o(guments)d(to)j(the)g(call.)58 b(Y)-9 b(ou)30
b(can)h(use)g Fm(PyArg_ParseTuple\(\))d Ft(to)j(e)o(xtract)g(the)208
2661 y(ar)o(guments.)104 2827 y(3.)41 b Fq(ar)m(g3)27
b Ft(is)j(a)f(dictionary)e(of)h(k)o(e)o(yw)o(ord)f(ar)o(guments)f(that)
j(were)g(passed.)50 b(If)28 b(this)h(is)h(non-)p Fm(NULL)c
Ft(and)i(you)g(support)f(k)o(e)o(yw)o(ord)208 2926 y(ar)o(guments,)32
b(use)g Fm(PyArg_ParseTupleAndKeywords\(\))c Ft(to)k(e)o(xtract)f(the)h
(ar)o(guments.)58 b(If)32 b(you)f(do)h(not)f(w)o(ant)h(to)208
3026 y(support)25 b(k)o(e)o(yw)o(ord)g(ar)o(guments)f(and)i(this)h(is)h
(non-)p Fm(NULL)p Ft(,)c(raise)j(a)g Fm(TypeError)f Ft(with)g(a)h
(message)g(saying)f(that)g(k)o(e)o(yw)o(ord)208 3126
y(ar)o(guments)18 b(are)i(not)g(supported.)0 3356 y(Here)g(is)h(a)g
(desultory)e(e)o(xample)f(of)i(the)g(implementation)e(of)i(the)h(call)f
(function.)p 0 5549 3901 4 v 0 5649 a Fo(58)2614 b(Chapter)24
b(2.)52 b(De\002ning)23 b(Ne)n(w)h(T)-10 b(ypes)p eop
end
%%Page: 59 65
TeXDict begin 59 64 bop 236 174 a Fj(/)281 187 y(*)371
174 y(Implement)43 b(the)h(call)h(function.)281 279 y(*)505
266 y(obj1)f(is)h(the)f(instance)g(receiving)f(the)h(call.)281
370 y(*)505 357 y(obj2)g(is)h(a)g(tuple)f(containing)f(the)h(arguments)
f(to)i(the)f(call,)g(in)h(this)281 461 y(*)729 448 y(case)g(3)f
(strings.)281 553 y(*)326 540 y(/)236 631 y(static)g(PyObject)954
644 y(*)236 722 y(newdatatype_call\(newdatatypeobject)1805
735 y(*)1850 722 y(obj,)g(PyObject)2478 735 y(*)2523
722 y(args,)g(PyObject)3195 735 y(*)3240 722 y(other\))236
814 y({)416 905 y(PyObject)819 918 y(*)864 905 y(result;)416
996 y(char)640 1009 y(*)685 996 y(arg1;)416 1088 y(char)640
1101 y(*)685 1088 y(arg2;)416 1179 y(char)640 1192 y(*)685
1179 y(arg3;)416 1362 y(if)g(\(!PyArg_ParseTuple\(args,)d("sss:call",)i
(&arg1,)h(&arg2,)f(&arg3\)\))h({)595 1453 y(return)g(NULL;)416
1544 y(})416 1636 y(result)f(=)i(PyString_FromFormat\()595
1727 y("Returning)e(--)i(value:)e([\\\045d])h(arg1:)g([\\\045s])g
(arg2:)g([\\\045s])g(arg3:)g([\\\045s]\\n",)595 1818
y(obj->obj_UnderlyingDatatypePtr->size,)595 1910 y(arg1,)g(arg2,)g
(arg3\);)416 2001 y(printf\("\\\045s",)e
(PyString_AS_STRING\(result\)\);)416 2092 y(return)h(result;)236
2183 y(})0 2470 y Ft(XXX)21 b(some)f(\002elds)g(need)g(to)g(be)g(added)
f(here...)416 2708 y Fj(/)461 2721 y(*)550 2708 y(Added)44
b(in)h(release)e(2.2)1492 2721 y(*)1537 2708 y(/)416
2800 y(/)461 2813 y(*)550 2800 y(Iterators)998 2813 y(*)1043
2800 y(/)416 2891 y(getiterfunc)g(tp_iter;)416 2982 y(iternextfunc)f
(tp_iternext;)0 3269 y Ft(These)16 b(functions)f(pro)o(vide)f(support)h
(for)g(the)h(iterator)g(protocol.)22 b(An)o(y)15 b(object)h(which)f
(wishes)i(to)f(support)f(iteration)g(o)o(v)o(er)g(its)i(contents)0
3368 y(\(which)24 b(may)g(be)h(generated)e(during)g(iteration\))g(must)
i(implement)e(the)i Fm(tp_iter)f Ft(handler)-5 b(.)37
b(Objects)25 b(which)f(are)h(returned)e(by)h(a)0 3468
y Fm(tp_iter)18 b Ft(handler)g(must)h(implement)e(both)i(the)g
Fm(tp_iter)f Ft(and)g Fm(tp_iternext)g Ft(handlers.)23
b(Both)c(handlers)f(tak)o(e)h(e)o(xactly)f(one)0 3568
y(parameter)m(,)f(the)h(instance)g(for)g(which)g(the)o(y)g(are)g(being)
g(called,)g(and)g(return)g(a)g(ne)n(w)h(reference.)j(In)d(the)f(case)h
(of)f(an)h(error)m(,)e(the)o(y)h(should)0 3667 y(set)j(an)f(e)o
(xception)e(and)i(return)f Fm(NULL)p Ft(.)0 3814 y(F)o(or)32
b(an)g(object)g(which)g(represents)g(an)g(iterable)g(collection,)i(the)
f Fm(tp_iter)e Ft(handler)g(must)i(return)e(an)h(iterator)g(object.)61
b(The)0 3914 y(iterator)28 b(object)g(is)i(responsible)d(for)h
(maintaining)f(the)i(state)h(of)e(the)h(iteration.)50
b(F)o(or)28 b(collections)g(which)g(can)h(support)e(multiple)0
4013 y(iterators)21 b(which)f(do)h(not)g(interfere)f(with)h(each)g
(other)f(\(as)i(lists)g(and)f(tuples)g(do\),)f(a)i(ne)n(w)f(iterator)f
(should)h(be)g(created)f(and)h(returned.)0 4113 y(Objects)i(which)g
(can)g(only)f(be)i(iterated)e(o)o(v)o(er)g(once)h(\(usually)f(due)g(to)
i(side)f(ef)n(fects)g(of)g(iteration\))f(should)g(implement)g(this)i
(handler)0 4213 y(by)d(returning)f(a)i(ne)n(w)g(reference)e(to)i
(themselv)o(es,)g(and)f(should)g(also)h(implement)e(the)i
Fm(tp_iternext)f Ft(handler)-5 b(.)28 b(File)23 b(objects)f(are)0
4312 y(an)e(e)o(xample)f(of)h(such)g(an)g(iterator)-5
b(.)0 4459 y(Iterator)20 b(objects)i(should)e(implement)g(both)h
(handlers.)28 b(The)21 b Fm(tp_iter)f Ft(handler)g(should)h(return)f(a)
i(ne)n(w)f(reference)f(to)i(the)f(iterator)0 4559 y(\(this)31
b(is)g(the)g(same)f(as)h(the)g Fm(tp_iter)f Ft(handler)f(for)g(objects)
i(which)f(can)g(only)g(be)g(iterated)g(o)o(v)o(er)f(destructi)n(v)o
(ely\).)54 b(The)30 b Fm(tp_-)0 4658 y(iternext)25 b
Ft(handler)g(should)g(return)g(a)i(ne)n(w)f(reference)e(to)i(the)h(ne)o
(xt)e(object)h(in)g(the)g(iteration)g(if)g(there)g(is)h(one.)42
b(If)26 b(the)g(iteration)0 4758 y(has)d(reached)e(the)i(end,)f(it)h
(may)f(return)g Fm(NULL)g Ft(without)g(setting)g(an)h(e)o(xception)d
(or)j(it)g(may)f(set)h Fm(StopIteration)p Ft(;)f(a)n(v)n(oiding)g(the)0
4858 y(e)o(xception)c(can)i(yield)g(slightly)g(better)g(performance.)i
(If)e(an)g(actual)g(error)f(occurs,)g(it)i(should)e(set)i(an)f(e)o
(xception)f(and)g(return)g Fm(NULL)p Ft(.)p 0 5549 3901
4 v 0 5649 a Fo(2.2.)52 b(T)-10 b(ype)23 b(Methods)3102
b(59)p eop end
%%Page: 60 66
TeXDict begin 60 65 bop 0 83 a Fg(2.2.6)100 b(W)m(eak)29
b(Ref)m(erence)g(Suppor)t(t)0 286 y Ft(One)f(of)g(the)h(goals)f(of)g
(Python')-5 b(s)27 b(weak-reference)f(implementation)g(is)j(to)g(allo)n
(w)f(an)o(y)g(type)g(to)g(participate)f(in)i(the)f(weak)g(refer)n(-)0
386 y(ence)23 b(mechanism)g(without)g(incurring)e(the)j(o)o(v)o(erhead)
d(on)i(those)h(objects)f(which)g(do)g(not)h(bene\002t)f(by)g(weak)h
(referencing)d(\(such)i(as)0 485 y(numbers\).)0 632 y(F)o(or)f(an)g
(object)f(to)i(be)f(weakly)f(referencable,)f(the)i(e)o(xtension)f(must)
h(include)f(a)i Fm(PyObject)2743 647 y(*)2814 632 y Ft(\002eld)f(in)g
(the)g(instance)g(structure)f(for)0 732 y(the)i(use)g(of)f(the)h(weak)g
(reference)e(mechanism;)i(it)h(must)e(be)h(initialized)g(to)g
Fm(NULL)f Ft(by)h(the)g(object')-5 b(s)22 b(constructor)-5
b(.)32 b(It)23 b(must)g(also)g(set)0 831 y(the)c Fm(tp_weaklistoffset)d
Ft(\002eld)i(of)h(the)f(corresponding)e(type)i(object)g(to)g(the)h(of)n
(fset)f(of)h(the)f(\002eld.)25 b(F)o(or)18 b(e)o(xample,)f(the)i
(instance)0 931 y(type)h(is)h(de\002ned)e(with)h(the)h(follo)n(wing)d
(structure:)236 1169 y Fj(typedef)44 b(struct)g({)416
1260 y(PyObject_HEAD)416 1352 y(PyClassObject)1043 1365
y(*)1088 1352 y(in_class;)312 b(/)1850 1365 y(*)1940
1352 y(The)44 b(class)g(object)2702 1365 y(*)2747 1352
y(/)416 1443 y(PyObject)1043 1456 y(*)1088 1443 y(in_dict;)357
b(/)1850 1456 y(*)1940 1443 y(A)44 b(dictionary)2523
1456 y(*)2568 1443 y(/)416 1534 y(PyObject)1043 1547
y(*)1088 1534 y(in_weakreflist;)e(/)1850 1547 y(*)1940
1534 y(List)i(of)g(weak)h(references)3016 1547 y(*)3061
1534 y(/)236 1626 y(})g(PyInstanceObject;)0 1912 y Ft(The)20
b(statically-declared)e(type)i(object)g(for)f(instances)h(is)h
(de\002ned)f(this)g(w)o(ay:)236 2150 y Fj(PyTypeObject)43
b(PyInstance_Type)f(=)j({)416 2242 y
(PyObject_HEAD_INIT\(&PyType_Type\))416 2333 y(0,)416
2424 y("module.instance",)416 2607 y(/)461 2620 y(*)550
2607 y(Lots)f(of)h(stuff)f(omitted)f(for)i(brevity...)2209
2620 y(*)2254 2607 y(/)416 2790 y(Py_TPFLAGS_DEFAULT,)1117
b(/)2433 2803 y(*)2523 2790 y(tp_flags)2926 2803 y(*)2971
2790 y(/)416 2881 y(0,)1882 b(/)2433 2894 y(*)2523 2881
y(tp_doc)2836 2894 y(*)2881 2881 y(/)416 2972 y(0,)g(/)2433
2985 y(*)2523 2972 y(tp_traverse)3061 2985 y(*)3106 2972
y(/)416 3064 y(0,)g(/)2433 3077 y(*)2523 3064 y(tp_clear)2926
3077 y(*)2971 3064 y(/)416 3155 y(0,)g(/)2433 3168 y(*)2523
3155 y(tp_richcompare)3195 3168 y(*)3240 3155 y(/)416
3246 y(offsetof\(PyInstanceObject,)40 b(in_weakreflist\),)i(/)2433
3259 y(*)2523 3246 y(tp_weaklistoffset)3330 3259 y(*)3375
3246 y(/)236 3338 y(};)0 3624 y Ft(The)20 b(type)g(constructor)e(is)j
(responsible)e(for)g(initializing)h(the)g(weak)g(reference)f(list)i(to)
f Fm(NULL)p Ft(:)236 3862 y Fj(static)44 b(PyObject)954
3875 y(*)236 3954 y(instance_new\(\))f({)416 4045 y(/)461
4058 y(*)550 4045 y(Other)h(initialization)f(stuff)h(omitted)f(for)h
(brevity)2657 4058 y(*)2702 4045 y(/)416 4228 y(self->in_weakreflist)d
(=)k(NULL;)416 4410 y(return)e(\(PyObject)1178 4423 y(*)1223
4410 y(\))h(self;)236 4502 y(})0 4788 y Ft(The)19 b(only)g(further)g
(addition)f(is)j(that)e(the)h(destructor)e(needs)i(to)g(call)g(the)g
(weak)f(reference)f(manager)g(to)i(clear)g(an)o(y)f(weak)g(references.)
0 4888 y(This)h(should)e(be)i(done)f(before)f(an)o(y)h(other)f(parts)i
(of)f(the)h(destruction)e(ha)n(v)o(e)h(occurred,)f(b)n(ut)h(is)i(only)e
(required)e(if)j(the)g(weak)f(reference)0 4987 y(list)i(is)g(non-)p
Fm(NULL)p Ft(:)p 0 5549 3901 4 v 0 5649 a Fo(60)2614
b(Chapter)24 b(2.)52 b(De\002ning)23 b(Ne)n(w)h(T)-10
b(ypes)p eop end
%%Page: 61 67
TeXDict begin 61 66 bop 236 174 a Fj(static)44 b(void)236
266 y(instance_dealloc\(PyInstanceObject)1761 279 y(*)1806
266 y(inst\))236 357 y({)416 448 y(/)461 461 y(*)550
448 y(Allocate)g(temporaries)f(if)h(needed,)g(but)g(do)g(not)h(begin)
550 540 y(destruction)e(just)h(yet.)460 644 y(*)505 631
y(/)416 814 y(if)g(\(inst->in_weakreflist)d(!=)k(NULL\))595
905 y(PyObject_ClearWeakRefs\(\(PyObject)2074 918 y(*)2119
905 y(\))g(inst\);)416 1088 y(/)461 1101 y(*)550 1088
y(Proceed)f(with)g(object)g(destruction)f(normally.)2433
1101 y(*)2478 1088 y(/)236 1179 y(})0 1604 y Fg(2.2.7)100
b(More)29 b(Suggestions)0 1807 y Ft(Remember)15 b(that)h(you)f(can)h
(omit)g(most)g(of)g(these)g(functions,)f(in)i(which)e(case)i(you)e(pro)
o(vide)f Fm(0)i Ft(as)h(a)g(v)n(alue.)23 b(There)15 b(are)h(type)f
(de\002nitions)0 1906 y(for)20 b(each)g(of)h(the)f(functions)f(you)h
(must)h(pro)o(vide.)j(The)o(y)19 b(are)i(in)f(`)p Fu(object.h)p
Ft(')f(in)h(the)h(Python)e(include)h(directory)f(that)i(comes)f(with)h
(the)0 2006 y(source)e(distrib)n(ution)g(of)h(Python.)0
2153 y(In)28 b(order)f(to)i(learn)e(ho)n(w)h(to)h(implement)e(an)o(y)g
(speci\002c)i(method)e(for)g(your)g(ne)n(w)i(data)f(type,)h(do)f(the)h
(follo)n(wing:)39 b(Do)n(wnload)27 b(and)0 2252 y(unpack)f(the)h
(Python)g(source)f(distrib)n(ution.)46 b(Go)27 b(the)h(`)p
Fu(Objects)p Ft(')e(directory)-5 b(,)27 b(then)g(search)g(the)g(C)i
(source)e(\002les)h(for)f Fm(tp_)g Ft(plus)h(the)0 2352
y(function)21 b(you)g(w)o(ant)h(\(for)f(e)o(xample,)h
Fm(tp_print)f Ft(or)h Fm(tp_compare)p Ft(\).)29 b(Y)-9
b(ou)22 b(will)h(\002nd)f(e)o(xamples)f(of)h(the)g(function)f(you)g(w)o
(ant)h(to)0 2452 y(implement.)0 2598 y(When)31 b(you)g(need)g(to)h(v)o
(erify)e(that)i(an)g(object)f(is)i(an)e(instance)g(of)h(the)g(type)f
(you)g(are)g(implementing,)h(use)g(the)g Fm(PyObject_-)0
2698 y(TypeCheck)19 b Ft(function.)k(A)e(sample)f(of)g(its)h(use)f
(might)g(be)g(something)f(lik)o(e)h(the)g(follo)n(wing:)416
2936 y Fj(if)44 b(\(!)h(PyObject_TypeCheck\(some_object,)39
b(&MyType\)\))k({)595 3028 y(PyErr_SetString\(PyExc_TypeError,)c("arg)
44 b(#1)h(not)f(a)h(mything"\);)595 3119 y(return)f(NULL;)416
3210 y(})p 0 5549 3901 4 v 0 5649 a Fo(2.2.)52 b(T)-10
b(ype)23 b(Methods)3102 b(61)p eop end
%%Page: 62 68
TeXDict begin 62 67 bop 0 5549 3901 4 v 0 5649 a Fo(62)p
eop end
%%Page: 63 69
TeXDict begin 63 68 bop 0 83 3901 9 v 3503 230 a Fo(CHAPTER)3495
427 y Fn(THREE)p 0 515 V 617 978 a Fz(Building)56 b(C)h(and)g(C)2166
958 y(++)2465 978 y(Extensions)h(with)3154 1227 y(distutils)0
1706 y Ft(Starting)17 b(in)g(Python)f(1.4,)h(Python)f(pro)o(vides,)g
(on)j(U)t Fp(N)t(I)t(X)r Ft(,)f(a)g(special)f(mak)o(e)g(\002le)h(for)f
(b)n(uilding)e(mak)o(e)i(\002les)h(for)f(b)n(uilding)f(dynamically-)0
1805 y(link)o(ed)30 b(e)o(xtensions)f(and)g(custom)h(interpreters.)54
b(Starting)30 b(with)g(Python)f(2.0,)j(this)f(mechanism)e(\(kno)n(wn)f
(as)j(related)f(to)g(Mak)o(e-)0 1905 y(\002le.pre.in,)21
b(and)h(Setup)f(\002les\))i(is)g(no)f(longer)e(supported.)29
b(Building)21 b(custom)h(interpreters)e(w)o(as)j(rarely)f(used,)g(and)f
(e)o(xtension)g(mod-)0 2005 y(ules)g(can)e(be)i(b)n(uilt)f(using)g
(distutils.)0 2151 y(Building)e(an)h(e)o(xtension)e(module)h(using)g
(distutils)i(requires)d(that)i(distutils)h(is)g(installed)e(on)h(the)g
(b)n(uild)f(machine,)g(which)g(is)i(included)0 2251 y(in)i(Python)e
(2.x)g(and)h(a)n(v)n(ailable)g(separately)g(for)f(Python)h(1.5.)27
b(Since)22 b(distutils)g(also)f(supports)g(creation)f(of)h(binary)f
(packages,)g(users)0 2351 y(don')o(t)f(necessarily)g(need)h(a)g
(compiler)f(and)h(distutils)h(to)f(install)h(the)f(e)o(xtension.)0
2498 y(A)27 b(distutils)g(package)e(contains)g(a)i(dri)n(v)o(er)e
(script,)j(`)p Fu(setup)m(.p)n(y)p Ft('.)42 b(This)26
b(is)h(a)g(plain)f(Python)f(\002le,)k(which,)e(in)f(the)h(most)f
(simple)g(case,)0 2597 y(could)19 b(look)h(lik)o(e)g(this:)236
2835 y Fj(from)44 b(distutils.core)f(import)h(setup,)g(Extension)236
3018 y(module1)g(=)h(Extension\('demo',)1133 3109 y(sources)f(=)g
(['demo.c']\))236 3292 y(setup)g(\(name)g(=)h('PackageName',)550
3383 y(version)f(=)g('1.0',)550 3475 y(description)f(=)i('This)f(is)g
(a)h(demo)f(package',)550 3566 y(ext_modules)f(=)i([module1]\))0
3944 y Ft(W)m(ith)20 b(this)h(`)p Fu(setup)m(.p)n(y)p
Ft(',)e(and)g(a)i(\002le)g(`)p Fu(demo)m(.c)p Ft(',)e(running)236
4182 y Fj(python)44 b(setup.py)g(build)0 4469 y Ft(will)26
b(compile)f(`)p Fu(demo)m(.c)p Ft(',)h(and)f(produce)e(an)j(e)o
(xtension)e(module)g(named)h(`)p Fm(demo)p Ft(')g(in)g(the)h(`)p
Fu(b)o(uild)p Ft(')d(directory)-5 b(.)39 b(Depending)23
b(on)j(the)0 4568 y(system,)35 b(the)d(module)f(\002le)i(will)g(end)f
(up)g(in)g(a)h(subdirectory)c(`)p Fu(b)o(uild/lib)m(.system)p
Ft(',)i(and)h(may)g(ha)n(v)o(e)f(a)i(name)f(lik)o(e)g(`)p
Fu(demo)m(.so)p Ft(')f(or)0 4668 y(`)p Fu(demo)m(.p)n(yd)p
Ft('.)0 4815 y(In)17 b(the)g(`)p Fu(setup)m(.p)n(y)p
Ft(',)f(all)i(e)o(x)o(ecution)d(is)k(performed)14 b(by)j(calling)g(the)
g(`)p Fm(setup)p Ft(')g(function.)22 b(This)17 b(tak)o(es)h(a)g(v)n
(ariable)e(number)f(of)i(k)o(e)o(yw)o(ord)0 4914 y(ar)o(guments,)25
b(of)g(which)g(the)g(e)o(xample)f(abo)o(v)o(e)g(uses)i(only)f(a)h
(subset.)41 b(Speci\002cally)-5 b(,)26 b(the)f(e)o(xample)f
(speci\002es)i(meta-information)c(to)0 5014 y(b)n(uild)17
b(packages,)f(and)h(it)h(speci\002es)g(the)f(contents)f(of)h(the)h
(package.)k(Normally)-5 b(,)16 b(a)i(package)e(will)i(contain)e(of)h
(addition)f(modules,)g(lik)o(e)0 5114 y(Python)22 b(source)f(modules,)h
(documentation,)e(subpackages,)i(etc.)32 b(Please)24
b(refer)d(to)i(the)g(distutils)g(documentation)d(in)j
Fq(Distrib)n(uting)0 5213 y(Python)c(Modules)h Ft(to)g(learn)g(more)f
(about)g(the)h(features)g(of)g(distutils;)h(this)f(section)g(e)o
(xplains)g(b)n(uilding)f(e)o(xtension)f(modules)h(only)-5
b(.)0 5360 y(It)34 b(is)i(common)c(to)i(pre-compute)e(ar)o(guments)g
(to)i Fm(setup)p Ft(,)j(to)e(better)e(structure)h(the)g(dri)n(v)o(er)f
(script.)67 b(In)33 b(the)i(e)o(xample)d(abo)o(v)o(e,)p
0 5549 3901 4 v 3808 5649 a Fo(63)p eop end
%%Page: 64 70
TeXDict begin 64 69 bop 0 83 a Ft(the`)p Fm(ext_modules)p
Ft(')37 b(ar)o(gument)g(to)j Fm(setup)f Ft(is)h(a)g(list)h(of)e(e)o
(xtension)f(modules,)44 b(each)39 b(of)g(which)g(is)i(an)e(instance)g
(of)h(the)0 183 y Fm(Extension)p Ft(.)30 b(In)22 b(the)g(e)o(xample,)f
(the)i(instance)e(de\002nes)h(an)h(e)o(xtension)d(named)i(`)p
Fm(demo)p Ft(')f(which)h(is)h(b)n(uild)f(by)f(compiling)g(a)i(single)0
282 y(source)c(\002le,)i(`)p Fu(demo)m(.c)p Ft('.)0 429
y(In)29 b(man)o(y)e(cases,)32 b(b)n(uilding)27 b(an)i(e)o(xtension)e
(is)j(more)e(comple)o(x,)h(since)g(additional)e(preprocessor)g
(de\002nes)i(and)f(libraries)g(may)h(be)0 529 y(needed.)24
b(This)c(is)h(demonstrated)d(in)j(the)f(e)o(xample)f(belo)n(w)-5
b(.)236 767 y Fj(from)44 b(distutils.core)f(import)h(setup,)g
(Extension)236 950 y(module1)g(=)h(Extension\('demo',)1133
1041 y(define_macros)e(=)h([\('MAJOR_VERSION',)e('1'\),)1895
1132 y(\('MINOR_VERSION',)g('0'\)],)1133 1224 y(include_dirs)h(=)h
(['/usr/local/include'],)1133 1315 y(libraries)f(=)i(['tcl83'],)1133
1406 y(library_dirs)e(=)h(['/usr/local/lib'],)1133 1498
y(sources)g(=)g(['demo.c']\))236 1680 y(setup)g(\(name)g(=)h
('PackageName',)550 1772 y(version)f(=)g('1.0',)550 1863
y(description)f(=)i('This)f(is)g(a)h(demo)f(package',)550
1954 y(author)g(=)h('Martin)e(v.)i(Loewis',)550 2046
y(author_email)e(=)i('
[email protected]',)550 2137 y(url)f(=)h
('
http://www.python.org/doc/current/ext/)o(buildi)o(ng.htm)o(l',)550
2228 y(long_description)d(=)j(''')236 2319 y(This)f(is)h(really)f(just)
g(a)h(demo)f(package.)236 2411 y(''',)550 2502 y(ext_modules)f(=)i
([module1]\))0 2880 y Ft(In)16 b(this)g(e)o(xample,)f
Fm(setup)h Ft(is)g(called)g(with)g(additional)f(meta-information,)e
(which)i(is)i(recommended)c(when)i(distrib)n(ution)g(packages)0
2980 y(ha)n(v)o(e)21 b(to)i(be)f(b)n(uilt.)30 b(F)o(or)22
b(the)g(e)o(xtension)f(itself,)i(it)f(speci\002es)h(preprocessor)d
(de\002nes,)i(include)f(directories,)g(library)g(directories,)g(and)0
3079 y(libraries.)k(Depending)19 b(on)h(the)g(compiler)m(,)f(distutils)
i(passes)g(this)g(information)e(in)h(dif)n(ferent)f(w)o(ays)i(to)g(the)
f(compiler)-5 b(.)25 b(F)o(or)20 b(e)o(xample,)0 3179
y(on)i(U)t Fp(N)t(I)t(X)r Ft(,)f(this)f(may)g(result)g(in)h(the)f
(compilation)e(commands)236 3417 y Fj(gcc)45 b(-DNDEBUG)e(-g)i(-O3)f
(-Wall)g(-Wstrict-prototypes)d(-fPIC)j(-DMAJOR_VERSION=1)e
(-DMINOR_VERSION=0)g(-I/usr/local/include)g
(-I/usr/local/include/python2.2)e(-c)k(demo.c)g(-o)g
(build/temp.linux-i686-2.2/demo.o)236 3600 y(gcc)h(-shared)e
(build/temp.linux-i686-2.2/demo.o)d(-L/usr/local/lib)i(-ltcl83)h(-o)i
(build/lib.linux-i686-2.2/demo.so)0 3886 y Ft(These)20
b(lines)h(are)f(for)f(demonstration)f(purposes)h(only;)g(distutils)i
(users)g(should)e(trust)h(that)g(distutils)h(gets)g(the)f(in)m(v)n
(ocations)e(right.)0 4214 y Fl(3.1)121 b(Distr)r(ib)n(uting)33
b(y)n(our)h(e)l(xtension)h(modules)0 4446 y Ft(When)20
b(an)g(e)o(xtension)f(has)h(been)g(successfully)f(b)n(uild,)h(there)g
(are)g(three)f(w)o(ays)i(to)f(use)h(it.)0 4593 y(End-users)e(will)i
(typically)e(w)o(ant)h(to)h(install)f(the)h(module,)d(the)o(y)i(do)f
(so)i(by)f(running)236 4832 y Fj(python)44 b(setup.py)g(install)0
5118 y Ft(Module)19 b(maintainers)g(should)g(produce)g(source)g
(packages;)g(to)h(do)g(so,)g(the)o(y)g(run)p 0 5549 3901
4 v 0 5649 a Fo(64)1726 b(Chapter)23 b(3.)52 b(Building)26
b(C)d(and)g(C)2880 5641 y(++)3000 5649 y(Extensions)g(with)h(distutils)
p eop end
%%Page: 65 71
TeXDict begin 65 70 bop 236 174 a Fj(python)44 b(setup.py)g(sdist)0
461 y Ft(In)21 b(some)g(cases,)h(additional)e(\002les)j(need)d(to)i(be)
f(included)f(in)h(a)h(source)f(distrib)n(ution;)g(this)g(is)i(done)d
(through)f(a)j(`)p Fu(MANIFEST)-9 b(.in)p Ft(')19 b(\002le;)0
560 y(see)i(the)f(distutils)h(documentation)c(for)j(details.)0
707 y(If)j(the)f(source)g(distrib)n(ution)g(has)h(been)f(b)n(uild)g
(successfully)-5 b(,)22 b(maintainers)g(can)g(also)h(create)g(binary)e
(distrib)n(utions.)31 b(Depending)21 b(on)0 807 y(the)f(platform,)f
(one)g(of)h(the)g(follo)n(wing)f(commands)f(can)i(be)h(used)e(to)i(do)f
(so.)236 1045 y Fj(python)44 b(setup.py)g(bdist_wininst)236
1137 y(python)g(setup.py)g(bdist_rpm)236 1228 y(python)g(setup.py)g
(bdist_dumb)p 0 5549 3901 4 v 0 5649 a Fo(3.1.)52 b(Distr)q(ib)n(uting)
25 b(y)n(our)e(e)n(xtension)g(modules)2308 b(65)p eop
end
%%Page: 66 72
TeXDict begin 66 71 bop 0 5549 3901 4 v 0 5649 a Fo(66)p
eop end
%%Page: 67 73
TeXDict begin 67 72 bop 0 83 3901 9 v 3503 230 a Fo(CHAPTER)3561
427 y Fn(FOUR)p 0 515 V 754 978 a Fz(Building)56 b(C)i(and)f(C)2304
958 y(++)2603 978 y(Extensions)g(on)3065 1227 y(Windo)m(ws)0
1706 y Ft(This)21 b(chapter)e(brie\003y)g(e)o(xplains)h(ho)n(w)g(to)g
(create)g(a)h(W)m(indo)n(ws)f(e)o(xtension)f(module)g(for)g(Python)h
(using)f(Microsoft)h(V)-5 b(isual)20 b(C)3644 1699 y(++)3738
1706 y(,)h(and)0 1805 y(follo)n(ws)h(with)g(more)g(detailed)g
(background)d(information)h(on)i(ho)n(w)f(it)i(w)o(orks.)32
b(The)22 b(e)o(xplanatory)d(material)j(is)h(useful)f(for)g(both)g(the)0
1905 y(W)m(indo)n(ws)17 b(programmer)d(learning)i(to)i(b)n(uild)f
(Python)f(e)o(xtensions)g(and)h(the)i(U)t Fp(N)t(I)t(X)i
Ft(programmer)14 b(interested)j(in)g(producing)e(softw)o(are)0
2005 y(which)20 b(can)g(be)g(successfully)f(b)n(uilt)i(on)e(both)j(U)t
Fp(N)t(I)t(X)h Ft(and)d(W)m(indo)n(ws.)0 2151 y(Module)k(authors)h(are)
h(encouraged)c(to)k(use)g(the)g(distutils)g(approach)d(for)i(b)n
(uilding)g(e)o(xtension)f(modules,)i(instead)f(of)g(the)h(one)f(de-)0
2251 y(scribed)f(in)h(this)g(section.)38 b(Y)-9 b(ou)25
b(will)g(still)h(need)e(the)h(C)g(compiler)f(that)h(w)o(as)g(used)g(to)
f(b)n(uild)h(Python;)h(typically)d(Microsoft)h(V)-5 b(isual)0
2351 y(C)55 2344 y(++)149 2351 y(.)0 2498 y Fr(Note:)24
b Ft(This)d(chapter)e(mentions)g(a)i(number)e(of)g(\002lenames)i(that)f
(include)f(an)h(encoded)f(Python)g(v)o(ersion)g(number)-5
b(.)24 b(These)c(\002lenames)0 2597 y(are)f(represented)f(with)i(the)f
(v)o(ersion)f(number)g(sho)n(wn)h(as)h(`)p Fm(XY)p Ft(';)f(in)g(practi)
n(v)o(e,)f(`)p Fm(X)p Ft(')h(will)i(be)e(the)g(major)g(v)o(ersion)f
(number)g(and)h(`)p Fm(Y)p Ft(')g(will)0 2697 y(be)j(the)g(minor)f(v)o
(ersion)g(number)f(of)i(the)g(Python)f(release)h(you')l(re)e(w)o
(orking)g(with.)31 b(F)o(or)22 b(e)o(xample,)e(if)j(you)e(are)h(using)f
(Python)g(2.2.1,)0 2796 y(`)p Fm(XY)p Ft(')f(will)h(actually)e(be)h(`)p
Fm(22)p Ft('.)0 3118 y Fl(4.1)121 b(A)33 b(Cookbook)i(Approach)0
3351 y Ft(There)17 b(are)h(tw)o(o)g(approaches)d(to)j(b)n(uilding)f(e)o
(xtension)f(modules)g(on)i(W)m(indo)n(ws,)f(just)h(as)h(there)e(are)h
(on)h(U)t Fp(N)t(I)t(X)r Ft(:)25 b(use)18 b(the)g Fm(distutils)0
3451 y Ft(package)23 b(to)h(control)f(the)i(b)n(uild)e(process,)i(or)f
(do)g(things)g(manually)-5 b(.)35 b(The)24 b(distutils)h(approach)d(w)o
(orks)i(well)h(for)e(most)i(e)o(xtensions;)0 3550 y(documentation)h(on)
j(using)g Fm(distutils)f Ft(to)h(b)n(uild)g(and)f(package)g(e)o
(xtension)g(modules)g(is)i(a)n(v)n(ailable)e(in)i Fq(Distrib)n(uting)f
(Python)0 3650 y(Modules)p Ft(.)24 b(This)d(section)f(describes)f(the)i
(manual)e(approach)f(to)i(b)n(uilding)f(Python)g(e)o(xtensions)g
(written)h(in)g(C)i(or)d(C)3368 3643 y(++)3462 3650 y(.)0
3797 y(T)-7 b(o)18 b(b)n(uild)f(e)o(xtensions)f(using)h(these)h
(instructions,)e(you)h(need)g(to)h(ha)n(v)o(e)e(a)i(cop)o(y)f(of)g(the)
h(Python)e(sources)h(of)g(the)h(same)f(v)o(ersion)g(as)h(your)0
3897 y(installed)f(Python.)23 b(Y)-9 b(ou)17 b(will)i(need)e(Microsoft)
f(V)-5 b(isual)18 b(C)1685 3890 y(++)1797 3897 y(\223De)n(v)o(eloper)e
(Studio\224;)i(project)e(\002les)j(are)e(supplied)g(for)g(VC)3545
3890 y(++)3657 3897 y(v)o(ersion)0 3996 y(7.1,)23 b(b)n(ut)g(you)f(can)
h(use)h(older)e(v)o(ersions)g(of)h(VC)1400 3989 y(++)1494
3996 y(.)35 b(Notice)23 b(that)g(you)f(should)g(use)i(the)f(same)g(v)o
(ersion)f(of)h(VC)3265 3989 y(++)3359 3996 y(that)g(w)o(as)h(used)f(to)
0 4096 y(b)n(uild)d(Python)f(itself.)26 b(The)20 b(e)o(xample)f
(\002les)j(described)d(here)h(are)g(distrib)n(uted)g(with)g(the)h
(Python)e(sources)h(in)g(the)h(`)p Fu(PC)p Fe(n)p Fu(e)n(xample)p
3751 4096 23 4 v 25 w(nt)p Fe(n)p Ft(')0 4195 y(directory)-5
b(.)104 4395 y(1.)41 b Fr(Copy)19 b(the)i(example)f(\002les)208
4495 y Ft(The)d(`)p Fu(e)n(xample)p 669 4495 V 25 w(nt)p
Ft(')g(directory)f(is)j(a)f(subdirectory)e(of)h(the)h(`)p
Fu(PC)p Ft(')g(directory)-5 b(,)16 b(in)i(order)e(to)i(k)o(eep)g(all)g
(the)g(PC-speci\002c)g(\002les)h(under)208 4594 y(the)k(same)h
(directory)e(in)h(the)h(source)f(distrib)n(ution.)34
b(Ho)n(we)n(v)o(er)m(,)22 b(the)i(`)p Fu(e)n(xample)p
2526 4594 V 24 w(nt)p Ft(')f(directory)f(can')o(t)h(actually)g(be)g
(used)h(from)208 4694 y(this)h(location.)37 b(Y)-9 b(ou)24
b(\002rst)h(need)f(to)h(cop)o(y)f(or)g(mo)o(v)o(e)g(it)h(up)f(one)g(le)
n(v)o(el,)h(so)g(that)g(`)p Fu(e)n(xample)p 2845 4694
V 25 w(nt)p Ft(')f(is)h(a)h(sibling)e(of)g(the)h(`)p
Fu(PC)p Ft(')f(and)208 4793 y(`)p Fu(Include)p Ft(')17
b(directories.)24 b(Do)d(all)f(your)f(w)o(ork)h(from)f(within)h(this)h
(ne)n(w)f(location.)104 4947 y(2.)41 b Fr(Open)20 b(the)h(pr)o(oject)
208 5047 y Ft(From)29 b(VC)533 5040 y(++)627 5047 y(,)34
b(use)c(the)h Fo(File)69 b Fd(>)34 b Fo(Open)h(Solution)e
Ft(dialog)c(\(not)h Fo(File)70 b Fd(>)34 b Fo(Open)p
Ft(!\).)55 b(Na)n(vigate)30 b(to)h(and)e(select)i(the)g(\002le)208
5147 y(`)p Fu(e)n(xample)o(.sln)p Ft(',)17 b(in)j(the)g
Fq(copy)g Ft(of)g(the)g(`)p Fu(e)n(xample)p 1611 5147
V 24 w(nt)p Ft(')g(directory)f(you)g(made)h(abo)o(v)o(e.)j(Click)d
(Open.)104 5300 y(3.)41 b Fr(Build)21 b(the)g(example)e(DLL)208
5400 y Ft(In)g(order)g(to)i(check)e(that)h(e)n(v)o(erything)e(is)j(set)
g(up)f(right,)f(try)h(b)n(uilding:)p 0 5549 3901 4 v
3808 5649 a Fo(67)p eop end
%%Page: 68 74
TeXDict begin 68 73 bop 257 83 a Ft(\(a\))40 b(Select)33
b(a)f(con\002guration.)57 b(This)33 b(step)f(is)h(optional.)59
b(Choose)31 b Fo(Build)74 b Fd(>)36 b Fo(Con\002gur)o(ation)h(Manager)
72 b Fd(>)36 b Fo(Activ)n(e)390 183 y(Solution)d(Con\002gur)o(ation)28
b Ft(and)f(select)h(either)e Fo(Release)j Ft(or)p Fo(Deb)n(ug)p
Ft(.)46 b(If)27 b(you)f(skip)h(this)h(step,)h(VC)3382
176 y(++)3503 183 y(will)f(use)g(the)390 282 y(Deb)n(ug)20
b(con\002guration)d(by)j(def)o(ault.)252 406 y(\(b\))40
b(Build)22 b(the)f(DLL.)g(Choose)g Fo(Build)50 b Fd(>)24
b Fo(Build)i(Solution)p Ft(.)k(This)22 b(creates)f(all)h(intermediate)e
(and)h(result)g(\002les)h(in)g(a)f(subdi-)390 506 y(rectory)15
b(called)h(either)g(`)p Fu(Deb)o(ug)p Ft(')d(or)j(`)p
Fu(Release)p Ft(',)f(depending)e(on)j(which)g(con\002guration)d(you)i
(selected)h(in)h(the)f(preceding)390 605 y(step.)104
774 y(4.)41 b Fr(T)-8 b(esting)21 b(the)f(deb)n(ug-mode)g(DLL)208
874 y Ft(Once)25 b(the)i(Deb)n(ug)e(b)n(uild)h(has)g(succeeded,)g
(bring)f(up)h(a)g(DOS)h(box,)g(and)e(change)g(to)h(the)h(`)p
Fu(e)n(xample)p 3191 874 23 4 v 24 w(nt)p Fe(n)p Fu(Deb)o(ug)p
Ft(')d(directory)-5 b(.)208 974 y(Y)c(ou)18 b(should)g(no)n(w)g(be)h
(able)g(to)g(repeat)g(the)g(follo)n(wing)e(session)j(\()p
Fm(C>)e Ft(is)i(the)f(DOS)h(prompt,)d Fm(>>>)i Ft(is)h(the)f(Python)f
(prompt;)g(note)208 1073 y(that)i(b)n(uild)g(information)d(and)j(v)n
(arious)f(deb)n(ug)g(output)g(from)g(Python)g(may)h(not)g(match)g(this)
g(screen)g(dump)f(e)o(xactly\):)444 1293 y Fj
(C>..\\..\\PCbuild\\python_d)444 1384 y(Adding)44 b(parser)f
(accelerators)g(...)444 1476 y(Done.)444 1567 y(Python)h(2.2)g(\(#28,)g
(Dec)g(19)h(2001,)f(23:26:37\))f([MSC)h(32)h(bit)f(\(Intel\)])g(on)g
(win32)444 1658 y(Type)g("copyright",)f("credits")g(or)i("license")e
(for)h(more)g(information.)444 1750 y(>>>)g(import)g(example)444
1841 y([4897)g(refs])444 1932 y(>>>)g(example.foo\(\))444
2024 y(Hello,)g(world)444 2115 y([4903)g(refs])444 2206
y(>>>)208 2474 y Ft(Congratulations!)22 b(Y)-9 b(ou')l(v)o(e)19
b(successfully)g(b)n(uilt)h(your)f(\002rst)i(Python)e(e)o(xtension)g
(module.)104 2631 y(5.)41 b Fr(Cr)o(eating)18 b(y)n(our)i(o)o(wn)h(pr)o
(oject)208 2731 y Ft(Choose)e(a)h(name)f(and)h(create)f(a)h(directory)e
(for)h(it.)26 b(Cop)o(y)19 b(your)g(C)i(sources)e(into)g(it.)26
b(Note)20 b(that)g(the)f(module)g(source)g(\002le)h(name)208
2831 y(does)c(not)h(necessarily)f(ha)n(v)o(e)g(to)h(match)g(the)f
(module)g(name,)h(b)n(ut)f(the)h(name)g(of)f(the)h(initialization)f
(function)f(should)h(match)h(the)208 2930 y(module)i(name)i(\227)g(you)
g(can)g(only)f(import)g(a)h(module)f Fm(spam)h Ft(if)g(its)i
(initialization)d(function)f(is)j(called)f Fm(initspam\(\))p
Ft(,)f(and)208 3030 y(it)h(should)f(call)i Fm(Py_InitModule\(\))c
Ft(with)j(the)g(string)g Fm("spam")f Ft(as)i(its)g(\002rst)g(ar)o
(gument)c(\(use)j(the)g(minimal)g(`)p Fu(e)n(xample)o(.c)p
Ft(')208 3129 y(in)26 b(this)g(directory)f(as)i(a)f(guide\).)41
b(By)27 b(con)m(v)o(ention,)d(it)j(li)n(v)o(es)f(in)g(a)h(\002le)f
(called)g(`)p Fu(spam.c)p Ft(')g(or)g(`)p Fu(spammodule)o(.c)p
Ft('.)40 b(The)26 b(output)208 3229 y(\002le)e(should)f(be)h(called)g
(`)p Fu(spam.dll)p Ft(')e(or)h(`)p Fu(spam.p)n(yd)p Ft(')g(\(the)g
(latter)i(is)f(supported)e(to)i(a)n(v)n(oid)g(confusion)e(with)i(a)h
(system)f(library)208 3329 y(`)p Fu(spam.dll)p Ft(')17
b(to)j(which)f(your)g(module)f(could)h(be)g(a)i(Python)d(interf)o
(ace\))h(in)h(Release)g(mode,)f(or)g(`)p Fu(spam)p 3158
3329 V 26 w(d.dll)p Ft(')f(or)i(`)p Fu(spam)p 3670 3329
V 26 w(d.p)n(yd)p Ft(')208 3428 y(in)g(Deb)n(ug)f(mode.)208
3557 y(No)n(w)h(your)f(options)g(are:)257 3726 y(\(a\))40
b(Cop)o(y)20 b(`)p Fu(e)n(xample)o(.sln)p Ft(')d(and)j(`)p
Fu(e)n(xample)o(.vcproj)p Ft(',)d(rename)i(them)h(to)g(`)p
Fu(spam.*)p Ft(',)f(and)h(edit)g(them)g(by)g(hand,)f(or)252
3850 y(\(b\))40 b(Create)21 b(a)f(brand)f(ne)n(w)h(project;)f
(instructions)h(are)g(belo)n(w)-5 b(.)208 4019 y(In)23
b(either)h(case,)h(cop)o(y)e(`)p Fu(e)n(xample)p 1203
4019 V 25 w(nt)p Fe(n)p Fu(e)n(xample)o(.def)p Ft(')e(to)j(`)p
Fu(spam)p Fe(n)p Fu(spam.def)p Ft(',)f(and)h(edit)g(the)g(ne)n(w)g(`)p
Fu(spam.def)p Ft(')e(so)i(its)h(second)208 4118 y(line)17
b(contains)f(the)h(string)g(`)p Fm(initspam)p Ft('.)22
b(If)17 b(you)f(created)g(a)h(ne)n(w)g(project)f(yourself,)g(add)h(the)
g(\002le)g(`)p Fu(spam.def)p Ft(')e(to)i(the)g(project)208
4218 y(no)n(w)-5 b(.)23 b(\(This)c(is)h(an)f(anno)o(ying)d(little)k
(\002le)g(with)f(only)f(tw)o(o)h(lines.)25 b(An)19 b(alternati)n(v)o(e)
f(approach)e(is)k(to)f(for)o(get)f(about)g(the)h(`)p
Fu(.def)p Ft(')e(\002le,)208 4318 y(and)i(add)h(the)g(option)e
Fr(/export:initspam)h Ft(some)n(where)g(to)h(the)g(Link)g(settings,)g
(by)g(manually)e(editing)h(the)i(setting)f(in)g(Project)208
4417 y(Properties)f(dialog\).)104 4574 y(6.)41 b Fr(Cr)o(eating)18
b(a)j(brand)f(new)h(pr)o(oject)208 4674 y Ft(Use)16 b(the)h
Fo(File)38 b Fd(>)18 b Fo(Ne)n(w)38 b Fd(>)19 b Fo(Project)e
Ft(dialog)e(to)i(create)f(a)g(ne)n(w)g(Project)g(W)-7
b(orkspace.)23 b(Select)17 b Fo(Visual)j(C++)g(Projects/Win32/)208
4774 y(Win32)28 b(Project)p Ft(,)d(enter)e(the)g(name)g(\(`)p
Fm(spam)p Ft('\),)g(and)g(mak)o(e)g(sure)g(the)h(Location)e(is)j(set)f
(to)g(parent)e(of)i(the)f(`)p Fu(spam)p Ft(')g(directory)208
4873 y(you)15 b(ha)n(v)o(e)h(created)g(\(which)g(should)g(be)g(a)h
(direct)f(subdirectory)f(of)h(the)h(Python)e(b)n(uild)h(tree,)h(a)g
(sibling)g(of)f(`)p Fu(Include)p Ft(')e(and)j(`)p Fu(PC)p
Ft('\).)208 4973 y(Select)h(W)m(in32)f(as)h(the)g(platform)e(\(in)h(my)
g(v)o(ersion,)g(this)h(is)h(the)e(only)g(choice\).)23
b(Mak)o(e)18 b(sure)f(the)h(Create)g(ne)n(w)f(w)o(orkspace)f(radio)208
5072 y(b)n(utton)j(is)i(selected.)k(Click)c(OK.)208 5201
y(Y)-9 b(ou)28 b(should)g(no)n(w)h(create)g(the)g(\002le)h(`)p
Fu(spam.def)p Ft(')e(as)i(instructed)e(in)h(the)h(pre)n(vious)d
(section.)52 b(Add)29 b(the)g(source)g(\002les)h(to)g(the)208
5300 y(project,)21 b(using)h Fo(Project)50 b Fd(>)25
b Fo(Add)h(Existing)g(Item)p Ft(.)32 b(Set)23 b(the)f(pattern)f(to)2379
5315 y Fm(*)2429 5300 y(.)2479 5315 y(*)2551 5300 y Ft(and)h(select)h
(both)e(`)p Fu(spam.c)p Ft(')h(and)f(`)p Fu(spam.def)p
Ft(')208 5400 y(and)e(click)h(OK.)h(\(Inserting)d(them)i(one)g(by)f
(one)h(is)h(\002ne)g(too.\))p 0 5549 3901 4 v 0 5649
a Fo(68)1746 b(Chapter)23 b(4.)52 b(Building)25 b(C)e(and)h(C)2900
5641 y(++)3020 5649 y(Extensions)f(on)h(Windo)o(ws)p
eop end
%%Page: 69 75
TeXDict begin 69 74 bop 208 83 a Ft(No)n(w)26 b(open)f(the)h
Fo(Project)60 b Fd(>)29 b Fo(spam)g(proper)s(ties)f Ft(dialog.)42
b(Y)-9 b(ou)25 b(only)g(need)h(to)g(change)f(a)i(fe)n(w)f(settings.)43
b(Mak)o(e)26 b(sure)g Fo(All)208 183 y(Con\002gur)o(ations)g
Ft(is)g(selected)f(from)g(the)g Fo(Settings)30 b(f)n(or)r(:)36
b Ft(dropdo)n(wn)22 b(list.)41 b(Select)26 b(the)g(C/C)2955
176 y(++)3075 183 y(tab)m(.)40 b(Choose)25 b(the)g(General)208
282 y(cate)o(gory)g(in)i(the)g(popup)e(menu)h(at)i(the)f(top.)45
b(T)-7 b(ype)27 b(the)g(follo)n(wing)e(te)o(xt)i(in)g(the)g(entry)g
(box)f(labeled)g Fo(Additional)33 b(Include)208 382 y(Director)q(ies)p
Ft(:)444 601 y Fj(..\\Include,..\\PC)208 869 y Ft(Then,)19
b(choose)g(the)h(General)g(cate)o(gory)e(in)i(the)h(Link)o(er)e(tab,)h
(and)f(enter)444 1089 y Fj(..\\PCbuild)208 1360 y Ft(in)h(the)g(te)o
(xt)g(box)f(labelled)h Fo(Additional)26 b(libr)o(ar)r(y)d(Director)q
(ies)p Ft(.)208 1488 y(No)n(w)d(you)f(need)g(to)i(add)e(some)h
(mode-speci\002c)f(settings:)208 1616 y(Select)31 b Fo(Release)h
Ft(in)g(the)f Fo(Con\002gur)o(ation)h Ft(dropdo)n(wn)d(list.)59
b(Choose)30 b(the)h Fo(Link)h Ft(tab,)i(choose)c(the)h
Fo(Input)i Ft(cate)o(gory)-5 b(,)31 b(and)208 1716 y(append)18
b Fm(pythonXY.lib)h Ft(to)h(the)g(list)h(in)g(the)f Fo(Additional)26
b(Dependencies)21 b Ft(box.)208 1844 y(Select)d Fo(Deb)n(ug)h
Ft(in)f(the)g Fo(Con\002gur)o(ation)h Ft(dropdo)n(wn)c(list,)k(and)e
(append)f Fm(pythonXY_d.lib)g Ft(to)i(the)g(list)h(in)f(the)f
Fo(Additional)208 1944 y(Dependencies)24 b Ft(box.)35
b(Then)22 b(click)i(the)g(C/C)1583 1937 y(++)1701 1944
y(tab,)g(select)h Fo(Code)i(Gener)o(ation)p Ft(,)e(and)e(select)i
Fo(Multi-threaded)j(Deb)n(ug)208 2043 y(DLL)21 b Ft(from)e(the)h
Fo(Runtime)k(libr)o(ar)r(y)d Ft(dropdo)n(wn)d(list.)208
2172 y(Select)24 b Fo(Release)h Ft(again)e(from)g(the)h
Fo(Con\002gur)o(ation)i Ft(dropdo)n(wn)21 b(list.)38
b(Select)24 b Fo(Multi-threaded)29 b(DLL)c Ft(from)e(the)h
Fo(Runtime)208 2271 y(libr)o(ar)r(y)d Ft(dropdo)n(wn)c(list.)0
2478 y(If)j(your)f(module)g(creates)h(a)h(ne)n(w)f(type,)f(you)g(may)h
(ha)n(v)o(e)g(trouble)f(with)h(this)h(line:)416 2716
y Fj(PyObject_HEAD_INIT\(&PyType_Type\))0 3002 y Ft(Change)e(it)i(to:)
416 3241 y Fj(PyObject_HEAD_INIT\(NULL\))0 3527 y Ft(and)f(add)f(the)h
(follo)n(wing)f(to)h(the)h(module)d(initialization)i(function:)416
3765 y Fj(MyObject_Type.ob_type)41 b(=)k(&PyType_Type;)0
4052 y Ft(Refer)20 b(to)g(section)g(3)h(of)f(the)g Fq(Python)f(F)-10
b(A)m(Q)20 b Ft(for)g(details)g(on)g(why)f(you)h(must)g(do)g(this.)0
4375 y Fl(4.2)121 b(Diff)l(erences)35 b(Betw)o(een)j
Fb(U)6 b Fc(N)g(I)g(X)36 b Fl(and)f(Windo)n(ws)2 4608
y Ft(U)t Fp(N)t(I)t(X)20 b Ft(and)c(W)m(indo)n(ws)h(use)g(completely)e
(dif)n(ferent)h(paradigms)f(for)i(run-time)e(loading)h(of)g(code.)24
b(Before)16 b(you)g(try)h(to)g(b)n(uild)g(a)g(module)0
4708 y(that)j(can)g(be)g(dynamically)f(loaded,)f(be)j(a)o(w)o(are)e(of)
h(ho)n(w)g(your)f(system)h(w)o(orks.)0 4855 y(In)g(U)t
Fp(N)t(I)t(X)r Ft(,)g(a)f(shared)f(object)g(\(`)p Fu(.so)p
Ft('\))g(\002le)h(contains)f(code)g(to)h(be)g(used)f(by)g(the)h
(program,)e(and)h(also)h(the)g(names)f(of)g(functions)g(and)g(data)0
4954 y(that)i(it)g(e)o(xpects)f(to)h(\002nd)g(in)g(the)f(program.)k
(When)c(the)h(\002le)h(is)f(joined)f(to)h(the)g(program,)d(all)j
(references)f(to)h(those)f(functions)g(and)g(data)0 5054
y(in)g(the)h(\002le')-5 b(s)20 b(code)f(are)g(changed)e(to)j(point)e
(to)i(the)f(actual)g(locations)g(in)g(the)h(program)d(where)i(the)g
(functions)f(and)g(data)i(are)f(placed)f(in)0 5154 y(memory)-5
b(.)23 b(This)d(is)h(basically)f(a)h(link)f(operation.)0
5300 y(In)e(W)m(indo)n(ws,)h(a)g(dynamic-link)d(library)h(\(`)p
Fu(.dll)p Ft('\))g(\002le)i(has)g(no)g(dangling)d(references.)23
b(Instead,)c(an)f(access)i(to)e(functions)g(or)g(data)h(goes)0
5400 y(through)i(a)j(lookup)e(table.)35 b(So)23 b(the)h(DLL)f(code)g
(does)g(not)g(ha)n(v)o(e)g(to)h(be)f(\002x)o(ed)g(up)g(at)h(runtime)e
(to)i(refer)e(to)i(the)g(program')-5 b(s)21 b(memory;)p
0 5549 3901 4 v 0 5649 a Fo(4.2.)52 b(Diff)n(erences)23
b(Betw)o(een)j Ft(U)t Fp(N)t(I)t(X)f Fo(and)f(Windo)o(ws)2118
b(69)p eop end
%%Page: 70 76
TeXDict begin 70 75 bop 0 83 a Ft(instead,)30 b(the)f(code)f(already)g
(uses)h(the)g(DLL)-8 b(')j(s)30 b(lookup)d(table,)j(and)f(the)g(lookup)
e(table)h(is)i(modi\002ed)e(at)h(runtime)f(to)h(point)f(to)h(the)0
183 y(functions)19 b(and)g(data.)0 330 y(In)26 b(U)t
Fp(N)t(I)t(X)r Ft(,)g(there)e(is)h(only)f(one)g(type)g(of)g(library)f
(\002le)i(\(`)p Fu(.a)p Ft('\))e(which)h(contains)g(code)f(from)h(se)n
(v)o(eral)f(object)h(\002les)h(\(`)p Fu(.o)p Ft('\).)36
b(During)24 b(the)0 429 y(link)g(step)g(to)g(create)g(a)g(shared)g
(object)f(\002le)i(\(`)p Fu(.so)p Ft('\),)e(the)h(link)o(er)g(may)f
(\002nd)h(that)g(it)h(doesn')o(t)d(kno)n(w)h(where)h(an)g(identi\002er)
f(is)i(de\002ned.)0 529 y(The)f(link)o(er)f(will)i(look)e(for)g(it)i
(in)f(the)g(object)f(\002les)i(in)f(the)g(libraries;)h(if)f(it)h
(\002nds)f(it,)h(it)g(will)g(include)e(all)h(the)g(code)f(from)g(that)h
(object)0 628 y(\002le.)0 775 y(In)d(W)m(indo)n(ws,)g(there)f(are)i(tw)
o(o)f(types)g(of)g(library)-5 b(,)20 b(a)i(static)g(library)e(and)h(an)
g(import)f(library)g(\(both)g(called)h(`)p Fu(.lib)p
Ft('\).)27 b(A)21 b(static)h(library)f(is)0 875 y(lik)o(e)g(a)i(U)t
Fp(N)t(I)t(X)h Ft(`)p Fu(.a)p Ft(')19 b(\002le;)j(it)f(contains)f(code)
g(to)h(be)g(included)e(as)i(necessary)-5 b(.)25 b(An)c(import)f
(library)f(is)j(basically)e(used)g(only)g(to)h(reassure)0
975 y(the)j(link)o(er)f(that)h(a)g(certain)f(identi\002er)g(is)h(le)o
(gal,)g(and)f(will)i(be)e(present)g(in)h(the)g(program)d(when)i(the)h
(DLL)g(is)g(loaded.)35 b(So)23 b(the)h(link)o(er)0 1074
y(uses)e(the)f(information)e(from)h(the)i(import)e(library)g(to)i(b)n
(uild)f(the)g(lookup)f(table)h(for)g(using)g(identi\002ers)g(that)g
(are)g(not)g(included)f(in)i(the)0 1174 y(DLL.)f(When)f(an)h
(application)f(or)g(a)h(DLL)g(is)h(link)o(ed,)e(an)h(import)f(library)g
(may)g(be)h(generated,)e(which)h(will)i(need)e(to)h(be)g(used)g(for)f
(all)0 1273 y(future)f(DLLs)h(that)h(depend)d(on)i(the)g(symbols)g(in)g
(the)g(application)f(or)h(DLL.)0 1420 y(Suppose)c(you)f(are)i(b)n
(uilding)e(tw)o(o)i(dynamic-load)d(modules,)i(B)h(and)g(C,)g(which)f
(should)g(share)g(another)f(block)h(of)g(code)g(A.)h(On)i(U)t
Fp(N)t(I)t(X)r Ft(,)0 1520 y(you)h(w)o(ould)h Fq(not)g
Ft(pass)g(`)p Fu(A.a)p Ft(')g(to)g(the)g(link)o(er)g(for)f(`)p
Fu(B)o(.so)p Ft(')h(and)f(`)p Fu(C)n(.so)p Ft(';)h(that)h(w)o(ould)e
(cause)h(it)h(to)f(be)h(included)d(twice,)j(so)g(that)f(B)h(and)f(C)0
1620 y(w)o(ould)g(each)h(ha)n(v)o(e)f(their)h(o)n(wn)f(cop)o(y)-5
b(.)29 b(In)22 b(W)m(indo)n(ws,)f(b)n(uilding)g(`)p Fu(A.dll)p
Ft(')f(will)j(also)f(b)n(uild)f(`)p Fu(A.lib)p Ft('.)29
b(Y)-9 b(ou)21 b Fq(do)h Ft(pass)g(`)p Fu(A.lib)p Ft(')e(to)i(the)g
(link)o(er)0 1719 y(for)k(B)h(and)f(C.)h(`)p Fu(A.lib)p
Ft(')e(does)h(not)g(contain)g(code;)i(it)g(just)f(contains)e
(information)f(which)i(will)h(be)g(used)f(at)h(runtime)e(to)i(access)g
(A)-9 b(')k(s)0 1819 y(code.)0 1966 y(In)22 b(W)m(indo)n(ws,)f(using)h
(an)g(import)f(library)f(is)j(sort)f(of)g(lik)o(e)g(using)g(`)p
Fm(import)48 b(spam)p Ft(';)23 b(it)f(gi)n(v)o(es)g(you)f(access)h(to)h
(spam')-5 b(s)22 b(names,)g(b)n(ut)0 2065 y(does)k(not)g(create)g(a)h
(separate)e(cop)o(y)-5 b(.)42 b(On)29 b(U)t Fp(N)t(I)t(X)r
Ft(,)f(linking)d(with)i(a)g(library)e(is)i(more)e(lik)o(e)i(`)p
Fm(from)49 b(spam)g(import)3544 2080 y(*)3594 2065 y
Ft(';)30 b(it)d(does)0 2165 y(create)20 b(a)h(separate)e(cop)o(y)-5
b(.)0 2492 y Fl(4.3)121 b(Using)34 b(DLLs)g(in)f(Pr)o(actice)0
2725 y Ft(W)m(indo)n(ws)17 b(Python)g(is)i(b)n(uilt)f(in)g(Microsoft)f
(V)-5 b(isual)18 b(C)1542 2718 y(++)1636 2725 y(;)h(using)e(other)h
(compilers)e(may)i(or)f(may)h(not)f(w)o(ork)g(\(though)f(Borland)h
(seems)0 2825 y(to\).)25 b(The)20 b(rest)g(of)g(this)h(section)f(is)h
(MSVC)1227 2818 y(++)1342 2825 y(speci\002c.)0 2972 y(When)f(creating)f
(DLLs)h(in)g(W)m(indo)n(ws,)f(you)g(must)h(pass)g(`)p
Fu(p)n(ythonXY)-10 b(.lib)p Ft(')15 b(to)20 b(the)g(link)o(er)-5
b(.)25 b(T)-7 b(o)20 b(b)n(uild)f(tw)o(o)i(DLLs,)e(spam)h(and)f(ni)h
(\(which)0 3071 y(uses)h(C)g(functions)e(found)f(in)i(spam\),)g(you)f
(could)g(use)i(these)f(commands:)236 3309 y Fj(cl)45
b(/LD)f(/I/python/include)e(spam.c)i(../libs/pythonXY.lib)236
3401 y(cl)h(/LD)f(/I/python/include)e(ni.c)i(spam.lib)g
(../libs/pythonXY.lib)0 3687 y Ft(The)22 b(\002rst)h(command)d(created)
h(three)h(\002les:)30 b(`)p Fu(spam.obj)p Ft(',)21 b(`)p
Fu(spam.dll)p Ft(')f(and)i(`)p Fu(spam.lib)p Ft('.)29
b(`)p Fu(Spam.dll)p Ft(')19 b(does)j(not)g(contain)f(an)o(y)h(Python)0
3787 y(functions)c(\(such)h(as)h Fm(PyArg_ParseTuple\(\))p
Ft(\),)c(b)n(ut)j(it)i(does)e(kno)n(w)f(ho)n(w)h(to)g(\002nd)h(the)f
(Python)f(code)h(thanks)g(to)g(`)p Fu(p)n(ythonXY)-10
b(.lib)p Ft('.)0 3934 y(The)27 b(second)f(command)g(created)g(`)p
Fu(ni.dll)p Ft(')f(\(and)h(`)p Fu(.obj)p Ft(')g(and)h(`)p
Fu(.lib)p Ft('\),)f(which)h(kno)n(ws)g(ho)n(w)f(to)i(\002nd)f(the)g
(necessary)g(functions)f(from)0 4033 y(spam,)20 b(and)f(also)i(from)e
(the)h(Python)f(e)o(x)o(ecutable.)0 4180 y(Not)29 b(e)n(v)o(ery)e
(identi\002er)h(is)h(e)o(xported)e(to)h(the)h(lookup)e(table.)49
b(If)29 b(you)e(w)o(ant)i(an)o(y)f(other)g(modules)f(\(including)f
(Python\))i(to)g(be)h(able)0 4280 y(to)d(see)g(your)f(identi\002ers,)h
(you)f(ha)n(v)o(e)g(to)h(say)g(`)p Fm(_declspec\(dllexport\))p
Ft(',)d(as)j(in)g(`)p Fm(void)49 b(_declspec\(dllexport\))0
4380 y(initspam\(void\))p Ft(')18 b(or)i(`)p Fm(PyObject)48
b(_declspec\(dllexport\))2358 4395 y(*)2408 4380 y
(NiGetSpamData\(void\))p Ft('.)0 4526 y(De)n(v)o(eloper)30
b(Studio)h(will)i(thro)n(w)e(in)h(a)g(lot)g(of)g(import)f(libraries)g
(that)h(you)f(do)h(not)f(really)h(need,)i(adding)c(about)h(100K)g(to)h
(your)0 4626 y(e)o(x)o(ecutable.)f(T)-7 b(o)24 b(get)f(rid)f(of)h
(them,)g(use)g(the)g(Project)g(Settings)g(dialog,)g(Link)f(tab,)i(to)f
(specify)f Fq(ignor)m(e)g(default)g(libr)o(aries)p Ft(.)34
b(Add)23 b(the)0 4726 y(correct)c(`)p Fu(msvcr)s(t)p
Fq(xx)p Fo(.lib)p Ft(')j(to)f(the)f(list)h(of)f(libraries.)p
0 5549 3901 4 v 0 5649 a Fo(70)1746 b(Chapter)23 b(4.)52
b(Building)25 b(C)e(and)h(C)2900 5641 y(++)3020 5649
y(Extensions)f(on)h(Windo)o(ws)p eop end
%%Page: 71 77
TeXDict begin 71 76 bop 0 83 3901 9 v 3503 230 a Fo(CHAPTER)3634
427 y Fn(FIVE)p 0 515 V 31 978 a Fz(Embedding)56 b(Python)h(in)h
(Another)e(Application)0 1468 y Ft(The)19 b(pre)n(vious)g(chapters)g
(discussed)g(ho)n(w)h(to)f(e)o(xtend)g(Python,)f(that)i(is,)h(ho)n(w)e
(to)h(e)o(xtend)f(the)g(functionality)f(of)i(Python)e(by)i(attaching)0
1568 y(a)29 b(library)e(of)h(C)h(functions)e(to)h(it.)50
b(It)29 b(is)g(also)g(possible)f(to)g(do)g(it)h(the)f(other)g(w)o(ay)g
(around:)40 b(enrich)27 b(your)g(C/C)3296 1561 y(++)3419
1568 y(application)g(by)0 1667 y(embedding)17 b(Python)h(in)i(it.)25
b(Embedding)17 b(pro)o(vides)g(your)h(application)g(with)i(the)f
(ability)g(to)h(implement)e(some)h(of)g(the)h(functionality)0
1767 y(of)k(your)f(application)g(in)h(Python)g(rather)f(than)h(C)h(or)f
(C)1630 1760 y(++)1724 1767 y(.)38 b(This)25 b(can)f(be)g(used)g(for)g
(man)o(y)f(purposes;)i(one)f(e)o(xample)f(w)o(ould)g(be)h(to)0
1866 y(allo)n(w)g(users)h(to)f(tailor)g(the)g(application)f(to)h(their)
g(needs)g(by)g(writing)f(some)h(scripts)h(in)f(Python.)36
b(Y)-9 b(ou)24 b(can)g(also)g(use)h(it)f(yourself)f(if)0
1966 y(some)d(of)g(the)g(functionality)e(can)i(be)g(written)g(in)h
(Python)e(more)g(easily)-5 b(.)0 2113 y(Embedding)20
b(Python)h(is)j(similar)e(to)h(e)o(xtending)d(it,)k(b)n(ut)f(not)f
(quite.)31 b(The)22 b(dif)n(ference)f(is)i(that)g(when)f(you)g(e)o
(xtend)f(Python,)g(the)i(main)0 2213 y(program)j(of)h(the)h
(application)f(is)i(still)g(the)f(Python)e(interpreter)m(,)i(while)g
(if)g(you)f(embed)g(Python,)h(the)g(main)g(program)e(may)h(ha)n(v)o(e)0
2312 y(nothing)f(to)i(do)f(with)g(Python)g(\227)h(instead,)h(some)e
(parts)h(of)f(the)h(application)e(occasionally)g(call)i(the)f(Python)g
(interpreter)f(to)h(run)0 2412 y(some)20 b(Python)f(code.)0
2559 y(So)26 b(if)h(you)e(are)h(embedding)d(Python,)j(you)f(are)h(pro)o
(viding)d(your)i(o)n(wn)h(main)f(program.)40 b(One)26
b(of)g(the)g(things)g(this)g(main)g(program)0 2658 y(has)20
b(to)g(do)g(is)h(initialize)f(the)g(Python)e(interpreter)-5
b(.)24 b(At)d(the)e(v)o(ery)g(least,)i(you)e(ha)n(v)o(e)g(to)h(call)h
(the)e(function)g Fm(Py_Initialize\(\))e Ft(\(on)0 2758
y(Mac)h(OS,)g(call)g Fm(PyMac_Initialize\(\))d Ft(instead\).)24
b(There)17 b(are)g(optional)g(calls)h(to)g(pass)h(command)c(line)j(ar)o
(guments)e(to)i(Python.)0 2858 y(Then)h(later)i(you)e(can)h(call)h(the)
f(interpreter)e(from)h(an)o(y)h(part)g(of)g(the)g(application.)0
3004 y(There)15 b(are)h(se)n(v)o(eral)g(dif)n(ferent)e(w)o(ays)j(to)f
(call)g(the)g(interpreter:)22 b(you)15 b(can)h(pass)h(a)f(string)g
(containing)e(Python)h(statements)h(to)g Fm(PyRun_-)0
3104 y(SimpleString\(\))p Ft(,)j(or)h(you)g(can)h(pass)g(a)g(stdio)g
(\002le)g(pointer)f(and)g(a)h(\002le)h(name)e(\(for)g(identi\002cation)
f(in)i(error)f(messages)h(only\))e(to)0 3204 y Fm(PyRun_SimpleFile\(\))
p Ft(.)i(Y)-9 b(ou)16 b(can)g(also)h(call)g(the)g(lo)n(wer)n(-le)n(v)o
(el)e(operations)g(described)h(in)g(the)h(pre)n(vious)e(chapters)h(to)h
(construct)0 3303 y(and)j(use)g(Python)f(objects.)0 3450
y(A)i(simple)f(demo)f(of)h(embedding)e(Python)h(can)h(be)g(found)e(in)j
(the)f(directory)e(`)p Fu(Demo/embed/)p Ft(')g(of)i(the)g(source)f
(distrib)n(ution.)0 3597 y Fr(See)i(Also:)0 3744 y Fq(Python/C)e(API)h
(Refer)m(ence)g(Manual)0 3844 y Fa(\()p Fu(../api/api.html)-5
b Fa(\))208 3943 y Ft(The)18 b(details)h(of)g(Python')-5
b(s)18 b(C)i(interf)o(ace)d(are)i(gi)n(v)o(en)f(in)h(this)g(manual.)24
b(A)19 b(great)f(deal)h(of)f(necessary)h(information)d(can)j(be)f
(found)208 4043 y(here.)0 4370 y Fl(5.1)121 b(V)-10 b(er)t(y)33
b(High)h(Le)l(v)m(el)g(Embedding)0 4603 y Ft(The)17 b(simplest)h(form)e
(of)h(embedding)e(Python)h(is)i(the)g(use)f(of)g(the)h(v)o(ery)e(high)g
(le)n(v)o(el)h(interf)o(ace.)23 b(This)18 b(interf)o(ace)e(is)j
(intended)c(to)j(e)o(x)o(ecute)0 4703 y(a)g(Python)e(script)i(without)e
(needing)g(to)i(interact)f(with)g(the)h(application)e(directly)-5
b(.)23 b(This)17 b(can)g(for)g(e)o(xample)f(be)i(used)f(to)g(perform)f
(some)0 4802 y(operation)i(on)i(a)h(\002le.)p 0 5549
3901 4 v 3808 5649 a Fo(71)p eop end
%%Page: 72 78
TeXDict begin 72 77 bop 236 83 a Fj(#include)44 b(<Python.h>)236
266 y(int)236 357 y(main\(int)g(argc,)g(char)1133 370
y(*)1178 357 y(argv[]\))236 448 y({)326 540 y(Py_Initialize\(\);)326
631 y(PyRun_SimpleString\("from)d(time)j(import)g(time,ctime\\n")1178
722 y("print)g('Today)f(is',ctime\(time\(\)\)\\n"\);)326
814 y(Py_Finalize\(\);)326 905 y(return)h(0;)236 996
y(})0 1283 y Ft(The)17 b(abo)o(v)o(e)f(code)h(\002rst)h(initializes)g
(the)f(Python)g(interpreter)f(with)h Fm(Py_Initialize\(\))p
Ft(,)f(follo)n(wed)g(by)h(the)g(e)o(x)o(ecution)f(of)h(a)h(hard-)0
1382 y(coded)k(Python)g(script)h(that)g(print)f(the)i(date)e(and)h
(time.)34 b(Afterw)o(ards,)22 b(the)h Fm(Py_Finalize\(\))f
Ft(call)h(shuts)g(the)g(interpreter)f(do)n(wn,)0 1482
y(follo)n(wed)h(by)h(the)g(end)g(of)f(the)i(program.)35
b(In)23 b(a)i(real)f(program,)f(you)g(may)h(w)o(ant)h(to)f(get)g(the)g
(Python)f(script)i(from)e(another)g(source,)0 1582 y(perhaps)i(a)h(te)o
(xt-editor)e(routine,)i(a)g(\002le,)i(or)d(a)i(database.)41
b(Getting)25 b(the)h(Python)f(code)g(from)g(a)h(\002le)h(can)f(better)f
(be)h(done)f(by)g(using)0 1681 y(the)c Fm(PyRun_SimpleFile\(\))e
Ft(function,)h(which)g(sa)n(v)o(es)i(you)f(the)g(trouble)f(of)h
(allocating)g(memory)e(space)j(and)f(loading)f(the)h(\002le)0
1781 y(contents.)0 2108 y Fl(5.2)121 b(Be)n(y)n(ond)34
b(V)-10 b(er)t(y)33 b(High)h(Le)l(v)m(el)g(Embedding:)43
b(An)34 b(o)n(v)m(er)t(vie)n(w)0 2341 y Ft(The)26 b(high)g(le)n(v)o(el)
g(interf)o(ace)f(gi)n(v)o(es)h(you)g(the)g(ability)h(to)f(e)o(x)o
(ecute)f(arbitrary)g(pieces)i(of)f(Python)f(code)h(from)f(your)g
(application,)i(b)n(ut)0 2441 y(e)o(xchanging)16 b(data)k(v)n(alues)f
(is)h(quite)f(cumbersome)e(to)j(say)f(the)h(least.)25
b(If)19 b(you)g(w)o(ant)g(that,)h(you)e(should)h(use)g(lo)n(wer)g(le)n
(v)o(el)g(calls.)26 b(At)20 b(the)0 2540 y(cost)h(of)e(ha)n(ving)g(to)i
(write)f(more)g(C)h(code,)e(you)g(can)h(achie)n(v)o(e)f(almost)h(an)o
(ything.)0 2687 y(It)g(should)f(be)h(noted)f(that)h(e)o(xtending)e
(Python)g(and)i(embedding)d(Python)i(is)i(quite)e(the)h(same)g(acti)n
(vity)-5 b(,)19 b(despite)h(the)g(dif)n(ferent)e(intent.)0
2787 y(Most)26 b(topics)g(discussed)g(in)g(the)g(pre)n(vious)f
(chapters)g(are)h(still)h(v)n(alid.)42 b(T)-7 b(o)26
b(sho)n(w)g(this,)i(consider)d(what)h(the)g(e)o(xtension)e(code)i(from)
0 2887 y(Python)19 b(to)h(C)i(really)d(does:)104 3116
y(1.)41 b(Con)m(v)o(ert)18 b(data)i(v)n(alues)g(from)f(Python)g(to)i
(C,)104 3282 y(2.)41 b(Perform)18 b(a)j(function)d(call)j(to)f(a)h(C)g
(routine)e(using)h(the)g(con)m(v)o(erted)e(v)n(alues,)h(and)104
3449 y(3.)41 b(Con)m(v)o(ert)18 b(the)i(data)h(v)n(alues)e(from)g(the)i
(call)f(from)f(C)j(to)e(Python.)0 3678 y(When)g(embedding)e(Python,)h
(the)h(interf)o(ace)f(code)h(does:)104 3908 y(1.)41 b(Con)m(v)o(ert)18
b(data)i(v)n(alues)g(from)f(C)i(to)g(Python,)104 4074
y(2.)41 b(Perform)18 b(a)j(function)d(call)j(to)f(a)h(Python)e(interf)o
(ace)g(routine)g(using)h(the)g(con)m(v)o(erted)e(v)n(alues,)h(and)104
4240 y(3.)41 b(Con)m(v)o(ert)18 b(the)i(data)h(v)n(alues)e(from)g(the)i
(call)f(from)f(Python)g(to)i(C.)0 4470 y(As)27 b(you)e(can)h(see,)i
(the)f(data)f(con)m(v)o(ersion)d(steps)k(are)f(simply)g(sw)o(apped)g
(to)g(accommodate)e(the)i(dif)n(ferent)f(direction)g(of)h(the)g(cross-)
0 4570 y(language)d(transfer)-5 b(.)39 b(The)25 b(only)f(dif)n(ference)
f(is)j(the)f(routine)f(that)h(you)f(call)h(between)f(both)g(data)h(con)
m(v)o(ersions.)37 b(When)25 b(e)o(xtending,)0 4670 y(you)19
b(call)i(a)g(C)g(routine,)d(when)i(embedding,)d(you)j(call)g(a)h
(Python)e(routine.)0 4816 y(This)h(chapter)f(will)i(not)f(discuss)h(ho)
n(w)e(to)i(con)m(v)o(ert)d(data)i(from)f(Python)g(to)h(C)h(and)f(vice)g
(v)o(ersa.)k(Also,)d(proper)d(use)i(of)g(references)f(and)0
4916 y(dealing)h(with)h(errors)g(is)h(assumed)e(to)i(be)f(understood.)k
(Since)c(these)g(aspects)h(do)e(not)h(dif)n(fer)f(from)g(e)o(xtending)f
(the)i(interpreter)m(,)e(you)0 5016 y(can)h(refer)f(to)i(earlier)f
(chapters)f(for)g(the)i(required)d(information.)p 0 5549
3901 4 v 0 5649 a Fo(72)1814 b(Chapter)23 b(5.)52 b(Embedding)25
b(Python)e(in)h(Another)g(Application)p eop end
%%Page: 73 79
TeXDict begin 73 78 bop 0 86 a Fl(5.3)121 b(Pure)33 b(Embedding)0
319 y Ft(The)19 b(\002rst)i(program)c(aims)k(to)e(e)o(x)o(ecute)g(a)h
(function)e(in)i(a)g(Python)f(script.)24 b(Lik)o(e)c(in)g(the)g
(section)f(about)g(the)h(v)o(ery)e(high)h(le)n(v)o(el)h(interf)o(ace,)0
419 y(the)g(Python)f(interpreter)g(does)h(not)g(directly)f(interact)h
(with)g(the)g(application)f(\(b)n(ut)h(that)g(will)h(change)e(in)h(the)
g(ne)o(xt)g(section\).)0 566 y(The)g(code)f(to)i(run)e(a)i(function)d
(de\002ned)h(in)i(a)f(Python)f(script)i(is:)236 742 y
Fj(#include)44 b(<Python.h>)236 925 y(int)236 1016 y(main\(int)g(argc,)
g(char)1133 1029 y(*)1178 1016 y(argv[]\))236 1108 y({)416
1199 y(PyObject)819 1212 y(*)864 1199 y(pName,)1178 1212
y(*)1223 1199 y(pModule,)1626 1212 y(*)1671 1199 y(pDict,)1985
1212 y(*)2030 1199 y(pFunc;)416 1290 y(PyObject)819 1303
y(*)864 1290 y(pArgs,)1178 1303 y(*)1223 1290 y(pValue;)416
1382 y(int)g(i;)416 1564 y(if)g(\(argc)g(<)h(3\))f({)595
1656 y(fprintf\(stderr,"Usage:)d(call)j(pythonfile)f(funcname)h
([args]\\n"\);)595 1747 y(return)g(1;)416 1838 y(})416
2021 y(Py_Initialize\(\);)416 2112 y(pName)g(=)g
(PyString_FromString\(argv[1]\);)416 2204 y(/)461 2217
y(*)550 2204 y(Error)g(checking)g(of)g(pName)g(left)g(out)2029
2217 y(*)2074 2204 y(/)416 2386 y(pModule)f(=)i
(PyImport_Import\(pName\);)416 2478 y(Py_DECREF\(pName\);)416
2660 y(if)f(\(pModule)g(!=)g(NULL\))g({)595 2752 y(pFunc)g(=)h
(PyObject_GetAttrString\(pModule,)39 b(argv[2]\);)595
2843 y(/)640 2856 y(*)729 2843 y(pFunc)44 b(is)h(a)g(new)f(reference)
1850 2856 y(*)1895 2843 y(/)595 3026 y(if)g(\(pFunc)g(&&)h
(PyCallable_Check\(pFunc\)\))40 b({)774 3117 y(pArgs)k(=)h
(PyTuple_New\(argc)d(-)j(3\);)774 3208 y(for)g(\(i)f(=)h(0;)f(i)h(<)g
(argc)f(-)g(3;)h(++i\))f({)954 3300 y(pValue)f(=)i
(PyInt_FromLong\(atoi\(argv[i)40 b(+)45 b(3]\)\);)954
3391 y(if)f(\(!pValue\))f({)1133 3482 y(Py_DECREF\(pArgs\);)1133
3574 y(Py_DECREF\(pModule\);)1133 3665 y(fprintf\(stderr,)f("Cannot)i
(convert)f(argument\\n"\);)1133 3756 y(return)h(1;)954
3847 y(})954 3939 y(/)999 3952 y(*)1088 3939 y(pValue)g(reference)f
(stolen)h(here:)2433 3952 y(*)2478 3939 y(/)954 4030
y(PyTuple_SetItem\(pArgs,)d(i,)j(pValue\);)774 4121 y(})774
4213 y(pValue)g(=)h(PyObject_CallObject\(pFunc,)40 b(pArgs\);)774
4304 y(Py_DECREF\(pArgs\);)774 4395 y(if)45 b(\(pValue)e(!=)i(NULL\))f
({)954 4487 y(printf\("Result)e(of)j(call:)e(\045ld\\n",)h
(PyInt_AsLong\(pValue\)\);)954 4578 y(Py_DECREF\(pValue\);)774
4669 y(})774 4761 y(else)g({)954 4852 y(Py_DECREF\(pFunc\);)954
4943 y(Py_DECREF\(pModule\);)954 5035 y(PyErr_Print\(\);)954
5126 y(fprintf\(stderr,"Call)d(failed\\n"\);)954 5217
y(return)i(1;)774 5309 y(})595 5400 y(})p 0 5549 3901
4 v 0 5649 a Fo(5.3.)52 b(Pure)24 b(Embedding)3001 b(73)p
eop end
%%Page: 74 80
TeXDict begin 74 79 bop 595 83 a Fj(else)44 b({)774 174
y(if)h(\(PyErr_Occurred\(\)\))954 266 y(PyErr_Print\(\);)774
357 y(fprintf\(stderr,)e("Cannot)g(find)h(function)g(\\"\045s\\"\\n",)f
(argv[2]\);)595 448 y(})595 540 y(Py_XDECREF\(pFunc\);)595
631 y(Py_DECREF\(pModule\);)416 722 y(})416 814 y(else)h({)595
905 y(PyErr_Print\(\);)595 996 y(fprintf\(stderr,)e("Failed)i(to)g
(load)g(\\"\045s\\"\\n",)g(argv[1]\);)595 1088 y(return)g(1;)416
1179 y(})416 1270 y(Py_Finalize\(\);)416 1362 y(return)f(0;)236
1453 y(})0 1650 y Ft(This)19 b(code)f(loads)g(a)h(Python)e(script)i
(using)f Fm(argv[1])p Ft(,)g(and)g(calls)h(the)g(function)e(named)g(in)
i Fm(argv[2])p Ft(.)24 b(Its)19 b(inte)o(ger)e(ar)o(guments)g(are)0
1749 y(the)22 b(other)g(v)n(alues)g(of)g(the)g Fm(argv)h
Ft(array)-5 b(.)30 b(If)22 b(you)g(compile)f(and)h(link)g(this)h
(program)d(\(let')-5 b(s)23 b(call)g(the)f(\002nished)g(e)o(x)o
(ecutable)f Fr(call)p Ft(\),)h(and)0 1849 y(use)e(it)h(to)g(e)o(x)o
(ecute)d(a)j(Python)e(script,)h(such)g(as:)236 2087 y
Fj(def)45 b(multiply\(a,b\):)416 2178 y(print)f("Will)g(compute",)f(a,)
h("times",)g(b)416 2270 y(c)g(=)h(0)416 2361 y(for)f(i)h(in)f
(range\(0,)g(a\):)595 2452 y(c)h(=)f(c)h(+)g(b)416 2544
y(return)e(c)0 2830 y Ft(then)20 b(the)g(result)g(should)f(be:)236
3068 y Fj($)45 b(call)f(multiply)g(multiply)f(3)i(2)236
3160 y(Will)f(compute)g(3)h(times)f(2)236 3251 y(Result)g(of)h(call:)f
(6)0 3538 y Ft(Although)20 b(the)j(program)d(is)j(quite)e(lar)o(ge)h
(for)f(its)i(functionality)-5 b(,)20 b(most)i(of)g(the)g(code)g(is)h
(for)e(data)h(con)m(v)o(ersion)e(between)h(Python)g(and)0
3637 y(C,)g(and)f(for)f(error)g(reporting.)k(The)d(interesting)f(part)h
(with)g(respect)g(to)h(embedding)c(Python)i(starts)i(with)416
3875 y Fj(Py_Initialize\(\);)416 3967 y(pName)44 b(=)g
(PyString_FromString\(argv[1]\);)416 4058 y(/)461 4071
y(*)550 4058 y(Error)g(checking)g(of)g(pName)g(left)g(out)2029
4071 y(*)2074 4058 y(/)416 4149 y(pModule)f(=)i
(PyImport_Import\(pName\);)0 4436 y Ft(After)29 b(initializing)g(the)h
(interpreter)m(,)f(the)h(script)f(is)h(loaded)f(using)g
Fm(PyImport_Import\(\))p Ft(.)50 b(This)30 b(routine)e(needs)h(a)h
(Python)0 4535 y(string)20 b(as)h(its)g(ar)o(gument,)d(which)h(is)i
(constructed)e(using)h(the)g Fm(PyString_FromString\(\))d
Ft(data)j(con)m(v)o(ersion)d(routine.)p 0 5549 3901 4
v 0 5649 a Fo(74)1814 b(Chapter)23 b(5.)52 b(Embedding)25
b(Python)e(in)h(Another)g(Application)p eop end
%%Page: 75 81
TeXDict begin 75 80 bop 416 174 a Fj(pFunc)44 b(=)g
(PyObject_GetAttrString\(pModule,)c(argv[2]\);)416 266
y(/)461 279 y(*)550 266 y(pFunc)k(is)h(a)f(new)h(reference)1671
279 y(*)1716 266 y(/)416 448 y(if)f(\(pFunc)g(&&)g
(PyCallable_Check\(pFunc\)\))d({)595 540 y(...)416 631
y(})416 722 y(Py_XDECREF\(pFunc\);)0 1009 y Ft(Once)26
b(the)h(script)f(is)i(loaded,)e(the)h(name)f(we')l(re)g(looking)e(for)i
(is)i(retrie)n(v)o(ed)c(using)i Fm(PyObject_GetAttrString\(\))p
Ft(.)40 b(If)27 b(the)0 1108 y(name)18 b(e)o(xists,)h(and)e(the)i
(object)f(returned)e(is)j(callable,)g(you)e(can)h(safely)g(assume)h
(that)f(it)h(is)h(a)e(function.)23 b(The)18 b(program)e(then)i
(proceeds)0 1208 y(by)i(constructing)e(a)j(tuple)e(of)h(ar)o(guments)e
(as)j(normal.)j(The)c(call)h(to)f(the)g(Python)f(function)g(is)i(then)e
(made)h(with:)416 1355 y Fj(pValue)43 b(=)i
(PyObject_CallObject\(pFunc,)40 b(pArgs\);)0 1641 y Ft(Upon)20
b(return)g(of)h(the)h(function,)d Fm(pValue)i Ft(is)h(either)f
Fm(NULL)g Ft(or)g(it)h(contains)f(a)h(reference)d(to)j(the)f(return)f
(v)n(alue)h(of)g(the)g(function.)27 b(Be)0 1741 y(sure)20
b(to)g(release)h(the)f(reference)e(after)i(e)o(xamining)e(the)i(v)n
(alue.)0 2069 y Fl(5.4)121 b(Extending)35 b(Embedded)g(Python)0
2301 y Ft(Until)27 b(no)n(w)-5 b(,)28 b(the)f(embedded)e(Python)h
(interpreter)f(had)i(no)f(access)i(to)f(functionality)e(from)h(the)h
(application)f(itself.)46 b(The)27 b(Python)0 2401 y(API)21
b(allo)n(ws)h(this)f(by)g(e)o(xtending)e(the)i(embedded)e(interpreter)
-5 b(.)26 b(That)21 b(is,)h(the)f(embedded)e(interpreter)h(gets)h(e)o
(xtended)e(with)j(routines)0 2501 y(pro)o(vided)e(by)i(the)g
(application.)29 b(While)23 b(it)g(sounds)f(comple)o(x,)e(it)j(is)g
(not)f(so)h(bad.)31 b(Simply)21 b(for)o(get)g(for)g(a)i(while)f(that)h
(the)f(application)0 2600 y(starts)e(the)f(Python)f(interpreter)-5
b(.)23 b(Instead,)c(consider)e(the)j(application)d(to)i(be)g(a)h(set)g
(of)e(subroutines,)g(and)g(write)i(some)f(glue)f(code)g(that)0
2700 y(gi)n(v)o(es)i(Python)f(access)i(to)f(those)g(routines,)f(just)i
(lik)o(e)f(you)f(w)o(ould)h(write)g(a)h(normal)e(Python)g(e)o
(xtension.)k(F)o(or)d(e)o(xample:)236 2938 y Fj(static)44
b(int)g(numargs=0;)236 3121 y(/)281 3134 y(*)371 3121
y(Return)g(the)g(number)g(of)g(arguments)g(of)g(the)g(application)f
(command)h(line)3195 3134 y(*)3240 3121 y(/)236 3212
y(static)g(PyObject)910 3225 y(*)236 3303 y(emb_numargs\(PyObject)1178
3316 y(*)1223 3303 y(self,)g(PyObject)1895 3316 y(*)1940
3303 y(args\))236 3395 y({)416 3486 y(if\(!PyArg_ParseTuple\(args,)c
(":numargs"\)\))595 3577 y(return)k(NULL;)416 3669 y(return)f
(Py_BuildValue\("i",)f(numargs\);)236 3760 y(})236 3943
y(static)i(PyMethodDef)f(EmbMethods[])g(=)i({)416 4034
y({"numargs",)e(emb_numargs,)f(METH_VARARGS,)460 4125
y("Return)i(the)g(number)g(of)h(arguments)e(received)g(by)i(the)f
(process."},)416 4217 y({NULL,)f(NULL,)h(0,)h(NULL})236
4308 y(};)0 4594 y Ft(Insert)21 b(the)g(abo)o(v)o(e)f(code)h(just)g
(abo)o(v)o(e)f(the)i Fm(main\(\))e Ft(function.)27 b(Also,)22
b(insert)f(the)g(follo)n(wing)f(tw)o(o)i(statements)f(directly)g(after)
g Fm(Py_-)0 4694 y(Initialize\(\))p Ft(:)416 4932 y Fj(numargs)43
b(=)i(argc;)416 5024 y(Py_InitModule\("emb",)c(EmbMethods\);)0
5310 y Ft(These)20 b(tw)o(o)h(lines)g(initialize)g(the)g
Fm(numargs)f Ft(v)n(ariable,)f(and)h(mak)o(e)g(the)h
Fm(emb.numargs\(\))e Ft(function)g(accessible)i(to)f(the)h(embed-)p
0 5549 3901 4 v 0 5649 a Fo(5.4.)52 b(Extending)24 b(Embedded)h(Python)
2543 b(75)p eop end
%%Page: 76 82
TeXDict begin 76 81 bop 0 83 a Ft(ded)20 b(Python)f(interpreter)-5
b(.)23 b(W)m(ith)e(these)f(e)o(xtensions,)f(the)h(Python)f(script)h
(can)g(do)g(things)g(lik)o(e)236 321 y Fj(import)44 b(emb)236
413 y(print)g("Number)g(of)g(arguments",)f(emb.numargs\(\))0
699 y Ft(In)20 b(a)h(real)f(application,)e(the)i(methods)g(will)g(e)o
(xpose)g(an)g(API)g(of)g(the)g(application)f(to)h(Python.)0
1026 y Fl(5.5)121 b(Embedding)35 b(Python)f(in)f(C)1541
1015 y(++)0 1259 y Ft(It)19 b(is)h(also)f(possible)f(to)h(embed)f
(Python)f(in)i(a)h(C)1364 1252 y(++)1477 1259 y(program;)d(precisely)h
(ho)n(w)g(this)i(is)f(done)f(will)h(depend)f(on)g(the)h(details)g(of)f
(the)h(C)3806 1252 y(++)0 1359 y(system)25 b(used;)h(in)f(general)e
(you)g(will)j(need)d(to)i(write)g(the)f(main)g(program)e(in)j(C)2356
1352 y(++)2450 1359 y(,)h(and)e(use)g(the)h(C)2959 1352
y(++)3078 1359 y(compiler)e(to)i(compile)e(and)0 1459
y(link)d(your)f(program.)k(There)c(is)i(no)f(need)f(to)i(recompile)e
(Python)g(itself)h(using)g(C)2359 1452 y(++)2453 1459
y(.)0 1786 y Fl(5.6)121 b(Linking)34 b(Requirements)0
2019 y Ft(While)22 b(the)f Fr(con\002gur)o(e)g Ft(script)g(shipped)f
(with)h(the)h(Python)e(sources)h(will)h(correctly)e(b)n(uild)h(Python)f
(to)h(e)o(xport)f(the)h(symbols)g(needed)0 2118 y(by)g(dynamically)f
(link)o(ed)h(e)o(xtensions,)g(this)h(is)h(not)e(automatically)f
(inherited)h(by)g(applications)g(which)g(embed)f(the)i(Python)f
(library)0 2218 y(statically)-5 b(,)21 b(at)h(least)g(on)h(U)t
Fp(N)t(I)t(X)r Ft(.)30 b(This)21 b(is)i(an)e(issue)h(when)f(the)g
(application)f(is)i(link)o(ed)f(to)g(the)h(static)g(runtime)e(library)g
(\(`)p Fu(libp)n(ython.a)p Ft('\))0 2318 y(and)g(needs)f(to)i(load)e
(dynamic)g(e)o(xtensions)g(\(implemented)f(as)j(`)p Fu(.so)p
Ft(')f(\002les\).)0 2465 y(The)j(problem)f(is)j(that)e(some)h(entry)f
(points)g(are)g(de\002ned)g(by)g(the)h(Python)e(runtime)h(solely)g(for)
g(e)o(xtension)f(modules)h(to)h(use.)35 b(If)24 b(the)0
2564 y(embedding)16 b(application)g(does)i(not)f(use)i(an)o(y)e(of)h
(these)g(entry)f(points,)h(some)g(link)o(ers)f(will)i(not)f(include)f
(those)g(entries)h(in)g(the)h(symbol)0 2664 y(table)g(of)g(the)g
(\002nished)g(e)o(x)o(ecutable.)j(Some)d(additional)f(options)g(are)h
(needed)f(to)h(inform)e(the)j(link)o(er)e(not)h(to)g(remo)o(v)o(e)e
(these)i(symbols.)0 2811 y(Determining)j(the)i(right)f(options)g(to)g
(use)h(for)f(an)o(y)g(gi)n(v)o(en)g(platform)f(can)h(be)h(quite)f(dif)n
(\002cult,)h(b)n(ut)g(fortunately)e(the)h(Python)g(con\002g-)0
2910 y(uration)e(already)f(has)j(those)e(v)n(alues.)30
b(T)-7 b(o)22 b(retrie)n(v)o(e)f(them)g(from)g(an)h(installed)g(Python)
e(interpreter)m(,)g(start)j(an)f(interacti)n(v)o(e)e(interpreter)0
3010 y(and)g(ha)n(v)o(e)f(a)i(short)f(session)g(lik)o(e)h(this:)236
3248 y Fj(>>>)45 b(import)e(distutils.sysconfig)236 3339
y(>>>)i(distutils.sysconfig.get_config_var\(')o(LINKFO)o(RSHARE)o(D'\))
236 3431 y('-Xlinker)f(-export-dynamic')0 3760 y Ft(The)26
b(contents)f(of)h(the)g(string)g(presented)f(will)i(be)f(the)g(options)
g(that)g(should)f(be)h(used.)43 b(If)26 b(the)g(string)g(is)h(empty)-5
b(,)26 b(there')-5 b(s)26 b(no)g(need)0 3860 y(to)j(add)f(an)o(y)f
(additional)g(options.)49 b(The)28 b Fm(LINKFORSHARED)f
Ft(de\002nition)g(corresponds)g(to)h(the)h(v)n(ariable)e(of)h(the)h
(same)g(name)e(in)0 3960 y(Python')-5 b(s)19 b(top-le)n(v)o(el)g(`)p
Fu(Mak)o(e\002le)p Ft('.)p 0 5549 3901 4 v 0 5649 a Fo(76)1814
b(Chapter)23 b(5.)52 b(Embedding)25 b(Python)e(in)h(Another)g
(Application)p eop end
%%Page: 77 83
TeXDict begin 77 82 bop 0 83 3901 9 v 3480 230 a Fo(APPENDIX)3814
427 y Fn(A)p 0 515 V 2468 978 a Fz(Repor)8 b(ting)57
b(Bugs)0 1468 y Ft(Python)23 b(is)i(a)g(mature)e(programming)e
(language)i(which)g(has)i(established)f(a)g(reputation)f(for)g
(stability)-5 b(.)37 b(In)24 b(order)f(to)i(maintain)e(this)0
1568 y(reputation,)18 b(the)i(de)n(v)o(elopers)e(w)o(ould)i(lik)o(e)g
(to)h(kno)n(w)e(of)h(an)o(y)f(de\002ciencies)h(you)f(\002nd)h(in)g
(Python)g(or)f(its)j(documentation.)0 1714 y(Before)e(submitting)g(a)h
(report,)f(you)g(will)i(be)f(required)e(to)i(log)f(into)h(SourceF)o(or)
o(ge;)e(this)i(will)h(mak)o(e)e(it)i(possible)e(for)h(the)g(de)n(v)o
(elopers)0 1814 y(to)f(contact)g(you)f(for)h(additional)f(information)e
(if)k(needed.)i(It)e(is)g(not)f(possible)g(to)g(submit)g(a)h(b)n(ug)e
(report)g(anon)o(ymously)-5 b(.)0 1961 y(All)24 b(b)n(ug)f(reports)f
(should)h(be)g(submitted)f(via)i(the)f(Python)f(Bug)i(T)m(rack)o(er)e
(at)i(\()p Fu(http://b)o(ugs)o(.p)n(ython.o)o(rg)-6 b
Ft(\).)34 b(The)23 b(b)n(ug)g(track)o(er)g(of)n(fers)f(a)0
2061 y(W)-7 b(eb)21 b(form)e(which)h(allo)n(ws)g(pertinent)f
(information)f(to)i(be)g(entered)f(and)h(submitted)f(to)i(the)f(de)n(v)
o(elopers.)0 2207 y(The)f(\002rst)i(step)f(in)f(\002ling)h(a)g(report)e
(is)j(to)f(determine)e(whether)g(the)i(problem)e(has)i(already)e(been)h
(reported.)k(The)c(adv)n(antage)f(in)i(doing)0 2307 y(so,)f(aside)g
(from)f(sa)n(ving)h(the)g(de)n(v)o(elopers)e(time,)i(is)h(that)f(you)f
(learn)g(what)h(has)g(been)g(done)f(to)h(\002x)g(it;)h(it)g(may)e(be)h
(that)g(the)g(problem)e(has)0 2407 y(already)h(been)h(\002x)o(ed)g(for)
f(the)i(ne)o(xt)e(release,)i(or)f(additional)f(information)f(is)j
(needed)e(\(in)h(which)g(case)h(you)e(are)h(welcome)g(to)h(pro)o(vide)0
2506 y(it)h(if)f(you)g(can!\).)k(T)-7 b(o)20 b(do)g(this,)g(search)g
(the)g(b)n(ug)g(database)g(using)f(the)h(search)g(box)f(on)h(the)g(top)
g(side)h(of)f(the)g(page.)0 2653 y(If)e(the)g(problem)f(you')l(re)f
(reporting)g(is)j(not)f(already)f(in)h(the)g(b)n(ug)g(track)o(er)m(,)f
(go)h(back)f(to)i(the)f(Python)f(Bug)h(T)m(rack)o(er)-5
b(.)24 b(Select)18 b(the)g(\223Create)0 2753 y(ne)n(w\224)i(link)g(at)g
(the)h(left)f(of)g(the)g(page)g(to)g(open)f(the)h(b)n(ug)g(reporting)e
(form.)0 2900 y(The)g(submission)f(form)g(has)h(a)g(number)e(of)i
(\002elds.)25 b(The)17 b(only)g(\002elds)i(that)f(are)g(required)e(are)
i(the)g(\223T)m(itle\224)g(and)f(\223T)-7 b(ype\224)17
b(\002elds.)25 b(F)o(or)18 b(the)0 2999 y(title,)24 b(enter)e(a)i
Fq(very)f Ft(short)f(description)g(of)g(the)h(problem;)g(less)h(than)e
(ten)h(w)o(ords)g(is)g(good.)32 b(In)22 b(the)h(\223Change)f(Note\224)h
(\002eld,)g(describe)0 3099 y(the)f(problem)e(in)h(detail,)h(including)
e(what)i(you)f(e)o(xpected)f(to)h(happen)f(and)i(what)f(did)h(happen.)
27 b(Be)c(sure)e(to)h(include)f(the)g(v)o(ersion)g(of)0
3199 y(Python)f(you)h(used,)g(whether)f(an)o(y)g(e)o(xtension)g
(modules)h(were)g(in)m(v)n(olv)o(ed,)e(and)i(what)g(hardw)o(are)f(and)h
(softw)o(are)f(platform)g(you)h(were)0 3298 y(using)f(\(including)e(v)o
(ersion)h(information)e(as)k(appropriate\).)0 3445 y(The)e(only)g
(other)g(\002eld)h(that)f(you)g(may)g(w)o(ant)h(to)f(set)i(is)f(the)g
(\223Components\224)e(\002eld,)h(which)g(allo)n(ws)h(you)f(to)g(place)h
(the)f(b)n(ug)g(report)g(into)0 3545 y(broad)g(cate)o(gories)g(\(such)g
(as)i(\223Documentation\224)d(or)i(\223Library\224\).)0
3692 y(Each)e(b)n(ug)g(report)f(will)j(be)e(assigned)g(to)h(a)g(de)n(v)
o(eloper)d(who)i(will)h(determine)e(what)i(needs)f(to)h(be)f(done)g(to)
g(correct)g(the)h(problem.)j(Y)-9 b(ou)0 3791 y(will)21
b(recei)n(v)o(e)e(an)h(update)f(each)h(time)g(action)g(is)h(tak)o(en)f
(on)g(the)g(b)n(ug.)0 3938 y Fr(See)h(Also:)0 4085 y
Fq(How)g(to)f(Report)g(Bugs)f(Ef)o(fectively)0 4185 y
Fa(\()p Fu(
http://www-mice)o(.cs)o(.ucl.ac.uk/m)o(ultimedia)o(/softw)n
(are)o(/do)o(cumen)o(tati)o(on)o(/Re)o(por)r(tin)o(gBu)o(gs)o(.h)o(tml)
-6 b Fa(\))208 4284 y Ft(Article)32 b(which)f(goes)h(into)f(some)h
(detail)g(about)f(ho)n(w)g(to)i(create)e(a)i(useful)e(b)n(ug)g(report.)
59 b(This)32 b(describes)g(what)g(kind)f(of)208 4384
y(information)17 b(is)k(useful)f(and)g(why)f(it)i(is)g(useful.)0
4531 y Fq(Bug)f(Writing)g(Guidelines)0 4630 y Fa(\()p
Fu(
http://www)l(.mozilla.org/q)o(ual)o(it)o(y/b)o(u)o(g-writin)o(g-g)o
(ui)o(de)o(li)o(nes)o(.h)o(tml)-6 b Fa(\))208 4730 y
Ft(Information)27 b(about)i(writing)h(a)g(good)f(b)n(ug)h(report.)54
b(Some)30 b(of)f(this)i(is)g(speci\002c)g(to)f(the)g(Mozilla)g
(project,)i(b)n(ut)e(describes)208 4830 y(general)19
b(good)f(practices.)p 0 5549 3901 4 v 3808 5649 a Fo(77)p
eop end
%%Page: 78 84
TeXDict begin 78 83 bop 0 5549 3901 4 v 0 5649 a Fo(78)p
eop end
%%Page: 79 85
TeXDict begin 79 84 bop 0 83 3901 9 v 3480 230 a Fo(APPENDIX)3814
427 y Fn(B)p 0 515 V 2080 978 a Fz(Histor)6 b(y)58 b(and)f(License)0
1506 y Fl(B)n(.1)120 b(Histor)t(y)32 b(of)i(the)h(softw)n(are)0
1739 y Ft(Python)k(w)o(as)i(created)f(in)g(the)h(early)e(1990s)h(by)g
(Guido)f(v)n(an)h(Rossum)g(at)h(Stichting)f(Mathematisch)f(Centrum)g
(\(CWI,)i(see)0 1839 y Fu(
http://www)l(.cwi.nl/)l Ft(\))30
b(in)g(the)g(Netherlands)e(as)j(a)f(successor)g(of)f(a)i(language)d
(called)i(ABC.)g(Guido)f(remains)h(Python')-5 b(s)29
b(principal)0 1939 y(author)m(,)18 b(although)h(it)i(includes)e(man)o
(y)g(contrib)n(utions)f(from)h(others.)0 2085 y(In)36
b(1995,)j(Guido)c(continued)f(his)j(w)o(ork)e(on)h(Python)f(at)i(the)f
(Corporation)e(for)i(National)g(Research)g(Initiati)n(v)o(es)f(\(CNRI,)
i(see)0 2185 y Fu(
http://www)l(.cnr)q(i.reston.v)n(a.us/)-6
b Ft(\))21 b(in)f(Reston,)g(V)-5 b(ir)o(ginia)19 b(where)h(he)g
(released)g(se)n(v)o(eral)f(v)o(ersions)g(of)h(the)g(softw)o(are.)0
2332 y(In)h(May)h(2000,)e(Guido)h(and)g(the)h(Python)e(core)h(de)n(v)o
(elopment)e(team)j(mo)o(v)o(ed)d(to)j(BeOpen.com)e(to)i(form)e(the)i
(BeOpen)f(PythonLabs)0 2432 y(team.)49 b(In)27 b(October)g(of)h(the)g
(same)h(year)m(,)g(the)f(PythonLabs)e(team)i(mo)o(v)o(ed)e(to)i
(Digital)h(Creations)f(\(no)n(w)f(Zope)g(Corporation;)j(see)0
2531 y Fu(
http://www)l(.z)o(ope)o(.com/)l Ft(\).)g(In)21
b(2001,)g(the)h(Python)f(Softw)o(are)g(F)o(oundation)f(\(PSF)-7
b(,)22 b(see)h Fu(
http://www)l(.p)n(ython.org/)o(psf/)-6
b Ft(\))22 b(w)o(as)h(formed,)d(a)0 2631 y(non-pro\002t)h(or)o
(ganization)f(created)i(speci\002cally)h(to)g(o)n(wn)g(Python-related)d
(Intellectual)j(Property)-5 b(.)32 b(Zope)22 b(Corporation)f(is)j(a)g
(spon-)0 2730 y(soring)19 b(member)g(of)h(the)g(PSF)-7
b(.)0 2877 y(All)26 b(Python)d(releases)j(are)e(Open)h(Source)f(\(see)h
Fu(
http://www)l(.opensource)o(.o)o(rg/)19 b Ft(for)24
b(the)h(Open)g(Source)f(De\002nition\).)38 b(Historically)-5
b(,)0 2977 y(most,)20 b(b)n(ut)g(not)g(all,)h(Python)e(releases)h(ha)n
(v)o(e)g(also)g(been)g(GPL-compatible;)e(the)i(table)g(belo)n(w)g
(summarizes)f(the)h(v)n(arious)g(releases.)p 0 5549 3901
4 v 3808 5649 a Fo(79)p eop end
%%Page: 80 86
TeXDict begin 80 85 bop 670 70 a Fr(Release)p 1074 100
4 100 v 188 w(Deri)o(v)o(ed)20 b(fr)o(om)p 1648 100 V
195 w(Y)-9 b(ear)p 2107 100 V 291 w(Owner)p 2643 100
V 195 w(GPL)21 b(compatible?)p 531 103 2838 4 v 581 173
a Ft(0.9.0)e(thru)g(1.2)p 1074 203 4 100 v 285 w(n/a)p
1648 203 V 285 w(1991-1995)p 2107 203 V 233 w(CWI)p 2643
203 V 494 w(yes)581 272 y(1.3)g(thru)h(1.5.2)p 1074 302
V 283 w(1.2)p 1648 302 V 283 w(1995-1999)p 2107 302 V
214 w(CNRI)p 2643 302 V 476 w(yes)752 372 y(1.6)p 1074
402 V 423 w(1.5.2)p 1648 402 V 348 w(2000)p 2107 402
V 313 w(CNRI)p 2643 402 V 490 w(no)752 472 y(2.0)p 1074
501 V 454 w(1.6)p 1648 501 V 380 w(2000)p 2107 501 V
195 w(BeOpen.com)p 2643 501 V 368 w(no)720 571 y(1.6.1)p
1074 601 V 423 w(1.6)p 1648 601 V 380 w(2001)p 2107 601
V 313 w(CNRI)p 2643 601 V 490 w(no)752 671 y(2.1)p 1074
701 V 347 w(2.0+1.6.1)p 1648 701 V 272 w(2001)p 2107
701 V 343 w(PSF)p 2643 701 V 520 w(no)720 770 y(2.0.1)p
1074 800 V 316 w(2.0+1.6.1)p 1648 800 V 272 w(2001)p
2107 800 V 343 w(PSF)p 2643 800 V 506 w(yes)720 870 y(2.1.1)p
1074 900 V 316 w(2.1+2.0.1)p 1648 900 V 272 w(2001)p
2107 900 V 343 w(PSF)p 2643 900 V 506 w(yes)752 970 y(2.2)p
1074 1000 V 423 w(2.1.1)p 1648 1000 V 348 w(2001)p 2107
1000 V 343 w(PSF)p 2643 1000 V 506 w(yes)720 1069 y(2.1.2)p
1074 1099 V 392 w(2.1.1)p 1648 1099 V 348 w(2002)p 2107
1099 V 343 w(PSF)p 2643 1099 V 506 w(yes)720 1169 y(2.1.3)p
1074 1199 V 392 w(2.1.2)p 1648 1199 V 348 w(2002)p 2107
1199 V 343 w(PSF)p 2643 1199 V 506 w(yes)720 1269 y(2.2.1)p
1074 1298 V 423 w(2.2)p 1648 1298 V 380 w(2002)p 2107
1298 V 343 w(PSF)p 2643 1298 V 506 w(yes)720 1368 y(2.2.2)p
1074 1398 V 392 w(2.2.1)p 1648 1398 V 348 w(2002)p 2107
1398 V 343 w(PSF)p 2643 1398 V 506 w(yes)720 1468 y(2.2.3)p
1074 1498 V 392 w(2.2.2)p 1648 1498 V 251 w(2002-2003)p
2107 1498 V 244 w(PSF)p 2643 1498 V 506 w(yes)752 1567
y(2.3)p 1074 1597 V 423 w(2.2.2)p 1648 1597 V 251 w(2002-2003)p
2107 1597 V 244 w(PSF)p 2643 1597 V 506 w(yes)720 1667
y(2.3.1)p 1074 1697 V 423 w(2.3)p 1648 1697 V 283 w(2002-2003)p
2107 1697 V 244 w(PSF)p 2643 1697 V 506 w(yes)720 1767
y(2.3.2)p 1074 1797 V 392 w(2.3.1)p 1648 1797 V 348 w(2003)p
2107 1797 V 343 w(PSF)p 2643 1797 V 506 w(yes)720 1866
y(2.3.3)p 1074 1896 V 392 w(2.3.2)p 1648 1896 V 348 w(2003)p
2107 1896 V 343 w(PSF)p 2643 1896 V 506 w(yes)720 1966
y(2.3.4)p 1074 1996 V 392 w(2.3.3)p 1648 1996 V 348 w(2004)p
2107 1996 V 343 w(PSF)p 2643 1996 V 506 w(yes)720 2066
y(2.3.5)p 1074 2095 V 392 w(2.3.4)p 1648 2095 V 348 w(2005)p
2107 2095 V 343 w(PSF)p 2643 2095 V 506 w(yes)752 2165
y(2.4)p 1074 2195 V 454 w(2.3)p 1648 2195 V 380 w(2004)p
2107 2195 V 343 w(PSF)p 2643 2195 V 506 w(yes)720 2265
y(2.4.1)p 1074 2295 V 423 w(2.4)p 1648 2295 V 380 w(2005)p
2107 2295 V 343 w(PSF)p 2643 2295 V 506 w(yes)720 2364
y(2.4.2)p 1074 2394 V 392 w(2.4.1)p 1648 2394 V 348 w(2005)p
2107 2394 V 343 w(PSF)p 2643 2394 V 506 w(yes)720 2464
y(2.4.3)p 1074 2494 V 392 w(2.4.2)p 1648 2494 V 348 w(2006)p
2107 2494 V 343 w(PSF)p 2643 2494 V 506 w(yes)720 2564
y(2.4.4)p 1074 2594 V 392 w(2.4.3)p 1648 2594 V 348 w(2006)p
2107 2594 V 343 w(PSF)p 2643 2594 V 506 w(yes)752 2663
y(2.5)p 1074 2693 V 454 w(2.4)p 1648 2693 V 380 w(2006)p
2107 2693 V 343 w(PSF)p 2643 2693 V 506 w(yes)720 2763
y(2.5.1)p 1074 2793 V 423 w(2.5)p 1648 2793 V 380 w(2007)p
2107 2793 V 343 w(PSF)p 2643 2793 V 506 w(yes)720 2863
y(2.5.2)p 1074 2892 V 392 w(2.5.1)p 1648 2892 V 348 w(2008)p
2107 2892 V 343 w(PSF)p 2643 2892 V 506 w(yes)0 3088
y Fr(Note:)35 b Ft(GPL-compatible)24 b(doesn')o(t)g(mean)g(that)i(we')l
(re)f(distrib)n(uting)f(Python)h(under)f(the)h(GPL.)h(All)g(Python)f
(licenses,)i(unlik)o(e)e(the)0 3187 y(GPL,)e(let)g(you)f(distrib)n(ute)
h(a)g(modi\002ed)f(v)o(ersion)f(without)h(making)g(your)f(changes)h
(open)g(source.)32 b(The)22 b(GPL-compatible)f(licenses)0
3287 y(mak)o(e)f(it)h(possible)f(to)g(combine)f(Python)g(with)h(other)f
(softw)o(are)h(that)g(is)i(released)d(under)g(the)h(GPL;)h(the)f
(others)g(don')o(t.)0 3434 y(Thanks)f(to)i(the)f(man)o(y)f(outside)g(v)
n(olunteers)g(who)h(ha)n(v)o(e)g(w)o(ork)o(ed)f(under)f(Guido')-5
b(s)20 b(direction)f(to)h(mak)o(e)g(these)g(releases)h(possible.)0
3761 y Fl(B)n(.2)120 b(T)-14 b(er)s(ms)32 b(and)j(conditions)f(f)l(or)g
(accessing)g(or)g(otherwise)g(using)g(Python)1024 3947
y Fr(PSF)20 b(LICENSE)j(A)-5 b(GREEMENT)22 b(FOR)e(PYTHON)g(2.5.3)104
4094 y Ft(1.)41 b(This)22 b(LICENSE)g(A)m(GREEMENT)f(is)j(between)d
(the)h(Python)f(Softw)o(are)h(F)o(oundation)e(\(\223PSF\224\),)i(and)f
(the)i(Indi)n(vidual)c(or)j(Or)n(-)208 4193 y(ganization)g
(\(\223Licensee\224\))h(accessing)h(and)g(otherwise)f(using)h(Python)f
(2.5.3)g(softw)o(are)h(in)h(source)e(or)h(binary)f(form)h(and)g(its)208
4293 y(associated)c(documentation.)104 4459 y(2.)41 b(Subject)28
b(to)g(the)h(terms)f(and)g(conditions)f(of)h(this)h(License)f
(Agreement,)h(PSF)g(hereby)e(grants)h(Licensee)g(a)h(none)o(xclusi)n(v)
o(e,)208 4559 y(ro)o(yalty-free,)e(w)o(orld-wide)g(license)h(to)g
(reproduce,)f(analyze,)i(test,)i(perform)26 b(and/or)h(display)g
(publicly)-5 b(,)28 b(prepare)f(deri)n(v)n(a-)208 4658
y(ti)n(v)o(e)g(w)o(orks,)h(distrib)n(ute,)h(and)e(otherwise)g(use)h
(Python)e(2.5.3)g(alone)h(or)g(in)h(an)o(y)e(deri)n(v)n(ati)n(v)o(e)g
(v)o(ersion,)i(pro)o(vided,)e(ho)n(we)n(v)o(er)m(,)208
4758 y(that)e(PSF')-5 b(s)26 b(License)e(Agreement)f(and)h(PSF')-5
b(s)25 b(notice)f(of)g(cop)o(yright,)f(i.e.,)j(\223Cop)o(yright)2871
4755 y(c)2848 4758 y Fs(\015)f Ft(2001-2008)c(Python)i(Softw)o(are)208
4857 y(F)o(oundation;)i(All)h(Rights)g(Reserv)o(ed\224)e(are)i
(retained)e(in)h(Python)f(2.5.3)g(alone)h(or)g(in)h(an)o(y)e(deri)n(v)n
(ati)n(v)o(e)g(v)o(ersion)g(prepared)f(by)208 4957 y(Licensee.)104
5123 y(3.)41 b(In)16 b(the)h(e)n(v)o(ent)f(Licensee)h(prepares)e(a)j
(deri)n(v)n(ati)n(v)o(e)d(w)o(ork)h(that)h(is)h(based)e(on)h(or)g
(incorporates)d(Python)i(2.5.3)g(or)g(an)o(y)h(part)f(thereof,)208
5223 y(and)25 b(w)o(ants)h(to)f(mak)o(e)g(the)h(deri)n(v)n(ati)n(v)o(e)
d(w)o(ork)i(a)n(v)n(ailable)g(to)h(others)f(as)h(pro)o(vided)d(herein,)
j(then)f(Licensee)g(hereby)f(agrees)h(to)208 5322 y(include)19
b(in)h(an)o(y)f(such)h(w)o(ork)g(a)h(brief)e(summary)g(of)h(the)g
(changes)f(made)h(to)g(Python)f(2.5.3.)p 0 5549 3901
4 v 0 5649 a Fo(80)2584 b(Appendix)25 b(B)n(.)47 b(Histor)r(y)22
b(and)i(License)p eop end
%%Page: 81 87
TeXDict begin 81 86 bop 104 83 a Ft(4.)41 b(PSF)30 b(is)g(making)e
(Python)h(2.5.3)f(a)n(v)n(ailable)h(to)g(Licensee)g(on)g(an)g(\223)-7
b(AS)31 b(IS\224)e(basis.)53 b(PSF)31 b(MAKES)e(NO)h(REPRESENT)-8
b(A-)208 183 y(TIONS)25 b(OR)h(W)-10 b(ARRANTIES,)26
b(EXPRESS)g(OR)g(IMPLIED.)f(BY)h(W)-10 b(A)h(Y)26 b(OF)g(EXAMPLE,)f(B)o
(UT)h(NO)m(T)f(LIMIT)-8 b(A)f(TION,)208 282 y(PSF)26
b(MAKES)f(NO)h(AND)g(DISCLAIMS)f(ANY)h(REPRESENT)-8 b(A)f(TION)25
b(OR)h(W)-10 b(ARRANTY)27 b(OF)f(MERCHANT)-8 b(ABIL-)208
382 y(ITY)17 b(OR)h(FITNESS)f(FOR)i(ANY)e(P)-8 b(AR)j(TICULAR)19
b(PURPOSE)f(OR)g(THA)-9 b(T)17 b(THE)g(USE)h(OF)g(PYTHON)f(2.5.3)f
(WILL)i(NO)m(T)208 482 y(INFRINGE)i(ANY)g(THIRD)h(P)-8
b(AR)j(TY)21 b(RIGHTS.)104 648 y(5.)41 b(PSF)29 b(SHALL)g(NO)m(T)g(BE)g
(LIABLE)g(T)o(O)f(LICENSEE)g(OR)i(ANY)f(O)m(THER)g(USERS)h(OF)f(PYTHON)
g(2.5.3)e(FOR)j(ANY)208 747 y(INCIDENT)-8 b(AL,)18 b(SPECIAL,)h(OR)h
(CONSEQ)o(UENTIAL)f(D)m(AMA)m(GES)f(OR)i(LOSS)g(AS)g(A)g(RESUL)-8
b(T)19 b(OF)h(MODIFYING,)208 847 y(DISTRIB)o(UTING,)j(OR)h(O)m(THER)-5
b(WISE)24 b(USING)g(PYTHON)g(2.5.3,)f(OR)h(ANY)g(DERIV)-11
b(A)i(TIVE)23 b(THEREOF)-7 b(,)24 b(EVEN)f(IF)208 946
y(AD)m(VISED)c(OF)i(THE)f(POSSIBILITY)g(THEREOF)-7 b(.)104
1112 y(6.)41 b(This)20 b(License)g(Agreement)e(will)j(automatically)e
(terminate)g(upon)g(a)i(material)f(breach)f(of)h(its)h(terms)f(and)g
(conditions.)104 1279 y(7.)41 b(Nothing)23 b(in)i(this)g(License)g
(Agreement)e(shall)i(be)g(deemed)e(to)i(create)g(an)o(y)f(relationship)
f(of)i(agenc)o(y)-5 b(,)23 b(partnership,)h(or)h(joint)208
1378 y(v)o(enture)20 b(between)h(PSF)h(and)g(Licensee.)29
b(This)22 b(License)f(Agreement)g(does)g(not)h(grant)f(permission)f(to)
i(use)g(PSF)h(trademarks)208 1478 y(or)c(trade)h(name)g(in)g(a)h
(trademark)d(sense)j(to)f(endorse)f(or)h(promote)e(products)h(or)h
(services)g(of)g(Licensee,)g(or)f(an)o(y)h(third)f(party)-5
b(.)104 1644 y(8.)41 b(By)18 b(cop)o(ying,)e(installing)h(or)h
(otherwise)f(using)g(Python)g(2.5.3,)g(Licensee)g(agrees)g(to)h(be)g
(bound)e(by)h(the)h(terms)g(and)f(conditions)208 1743
y(of)i(this)i(License)f(Agreement.)846 1926 y Fr(BEOPEN.COM)h(LICENSE)h
(A)-5 b(GREEMENT)23 b(FOR)d(PYTHON)g(2.0)579 2026 y(BEOPEN)h(PYTHON)f
(OPEN)g(SOURCE)h(LICENSE)h(A)-5 b(GREEMENT)23 b(VERSION)e(1)104
2256 y Ft(1.)41 b(This)20 b(LICENSE)g(A)m(GREEMENT)f(is)i(between)f
(BeOpen.com)e(\(\223BeOpen\224\),)h(ha)n(ving)g(an)h(of)n(\002ce)g(at)g
(160)g(Saratoga)e(A)-6 b(v)o(enue,)208 2355 y(Santa)29
b(Clara,)i(CA)f(95051,)f(and)f(the)h(Indi)n(vidual)e(or)i(Or)o
(ganization)d(\(\223Licensee\224\))h(accessing)i(and)f(otherwise)h
(using)f(this)208 2455 y(softw)o(are)19 b(in)i(source)e(or)h(binary)f
(form)g(and)h(its)h(associated)f(documentation)d(\(\223the)j(Softw)o
(are\224\).)104 2621 y(2.)41 b(Subject)18 b(to)g(the)h(terms)g(and)f
(conditions)f(of)h(this)h(BeOpen)f(Python)f(License)i(Agreement,)e
(BeOpen)h(hereby)f(grants)h(Licensee)208 2721 y(a)25
b(non-e)o(xclusi)n(v)o(e,)e(ro)o(yalty-free,)h(w)o(orld-wide)f(license)
j(to)f(reproduce,)f(analyze,)h(test,)i(perform)c(and/or)h(display)g
(publicly)-5 b(,)208 2820 y(prepare)16 b(deri)n(v)n(ati)n(v)o(e)f(w)o
(orks,)j(distrib)n(ute,)f(and)g(otherwise)g(use)h(the)g(Softw)o(are)f
(alone)g(or)g(in)h(an)o(y)f(deri)n(v)n(ati)n(v)o(e)f(v)o(ersion,)g(pro)
o(vided,)208 2920 y(ho)n(we)n(v)o(er)m(,)f(that)i(the)g(BeOpen)g
(Python)f(License)i(is)g(retained)e(in)h(the)h(Softw)o(are,)f(alone)f
(or)h(in)h(an)o(y)e(deri)n(v)n(ati)n(v)o(e)g(v)o(ersion)g(prepared)208
3019 y(by)j(Licensee.)104 3185 y(3.)41 b(BeOpen)25 b(is)j(making)c(the)
j(Softw)o(are)e(a)n(v)n(ailable)h(to)g(Licensee)g(on)g(an)g(\223)-7
b(AS)27 b(IS\224)g(basis.)43 b(BEOPEN)27 b(MAKES)f(NO)h(REPRE-)208
3285 y(SENT)-8 b(A)f(TIONS)27 b(OR)i(W)-10 b(ARRANTIES,)29
b(EXPRESS)g(OR)g(IMPLIED.)f(BY)h(W)-10 b(A)h(Y)29 b(OF)g(EXAMPLE,)e(B)o
(UT)i(NO)m(T)g(LIMI-)208 3385 y(T)-8 b(A)f(TION,)20 b(BEOPEN)h(MAKES)f
(NO)i(AND)f(DISCLAIMS)f(ANY)h(REPRESENT)-8 b(A)f(TION)21
b(OR)g(W)-10 b(ARRANTY)22 b(OF)f(MER-)208 3484 y(CHANT)-8
b(ABILITY)21 b(OR)i(FITNESS)e(FOR)i(ANY)f(P)-8 b(AR)j(TICULAR)23
b(PURPOSE)g(OR)f(THA)-9 b(T)21 b(THE)h(USE)g(OF)g(THE)g(SOFT)-8
b(-)208 3584 y(W)e(ARE)21 b(WILL)f(NO)m(T)g(INFRINGE)g(ANY)h(THIRD)f(P)
-8 b(AR)j(TY)21 b(RIGHTS.)104 3750 y(4.)41 b(BEOPEN)24
b(SHALL)f(NO)m(T)h(BE)g(LIABLE)g(T)o(O)f(LICENSEE)g(OR)i(ANY)f(O)m
(THER)f(USERS)i(OF)f(THE)g(SOFTW)-10 b(ARE)25 b(FOR)208
3850 y(ANY)c(INCIDENT)-8 b(AL,)20 b(SPECIAL,)h(OR)h(CONSEQ)o(UENTIAL)e
(D)m(AMA)m(GES)h(OR)h(LOSS)f(AS)h(A)f(RESUL)-8 b(T)22
b(OF)g(USING,)208 3949 y(MODIFYING)j(OR)i(DISTRIB)o(UTING)f(THE)h
(SOFTW)-10 b(ARE,)27 b(OR)g(ANY)f(DERIV)-11 b(A)i(TIVE)25
b(THEREOF)-7 b(,)27 b(EVEN)f(IF)g(AD-)208 4049 y(VISED)20
b(OF)h(THE)f(POSSIBILITY)g(THEREOF)-7 b(.)104 4215 y(5.)41
b(This)20 b(License)g(Agreement)e(will)j(automatically)e(terminate)g
(upon)g(a)i(material)f(breach)f(of)h(its)h(terms)f(and)g(conditions.)
104 4381 y(6.)41 b(This)27 b(License)f(Agreement)g(shall)h(be)g(go)o(v)
o(erned)d(by)j(and)f(interpreted)f(in)j(all)f(respects)g(by)g(the)g(la)
o(w)g(of)g(the)g(State)g(of)g(Cali-)208 4481 y(fornia,)22
b(e)o(xcluding)e(con\003ict)j(of)f(la)o(w)i(pro)o(visions.)31
b(Nothing)21 b(in)j(this)f(License)g(Agreement)e(shall)i(be)g(deemed)f
(to)h(create)f(an)o(y)208 4580 y(relationship)i(of)h(agenc)o(y)-5
b(,)25 b(partnership,)g(or)g(joint)h(v)o(enture)e(between)h(BeOpen)g
(and)g(Licensee.)41 b(This)26 b(License)f(Agreement)208
4680 y(does)18 b(not)h(grant)f(permission)f(to)i(use)h(BeOpen)e
(trademarks)f(or)i(trade)f(names)h(in)g(a)g(trademark)e(sense)i(to)g
(endorse)f(or)g(promote)208 4779 y(products)k(or)h(services)g(of)h
(Licensee,)f(or)h(an)o(y)e(third)h(party)-5 b(.)34 b(As)24
b(an)g(e)o(xception,)e(the)i(\223BeOpen)f(Python\224)f(logos)h(a)n(v)n
(ailable)g(at)208 4879 y(
http://www)-5 b(.p)o(ythonlabs.com/logo)o
(s.htm)o(l)15 b(may)20 b(be)g(used)g(according)e(to)i(the)h
(permissions)e(granted)g(on)h(that)g(web)g(page.)104
5045 y(7.)41 b(By)19 b(cop)o(ying,)e(installing)h(or)g(otherwise)g
(using)h(the)f(softw)o(are,)h(Licensee)f(agrees)g(to)h(be)f(bound)f(by)
i(the)f(terms)h(and)f(conditions)208 5145 y(of)h(this)i(License)f
(Agreement.)992 5327 y Fr(CNRI)h(LICENSE)h(A)-5 b(GREEMENT)22
b(FOR)e(PYTHON)g(1.6.1)p 0 5549 3901 4 v 0 5649 a Fo(B)n(.2.)53
b(T)-10 b(er)r(ms)22 b(and)i(conditions)f(f)n(or)g(accessing)f(or)h
(otherwise)g(using)h(Python)1337 b(81)p eop end
%%Page: 82 88
TeXDict begin 82 87 bop 104 83 a Ft(1.)41 b(This)25 b(LICENSE)g(A)m
(GREEMENT)f(is)i(between)e(the)h(Corporation)e(for)h(National)h
(Research)g(Initiati)n(v)o(es,)g(ha)n(ving)f(an)h(of)n(\002ce)208
183 y(at)j(1895)f(Preston)h(White)h(Dri)n(v)o(e,)g(Reston,)h(V)-11
b(A)29 b(20191)d(\(\223CNRI\224\),)i(and)f(the)i(Indi)n(vidual)d(or)i
(Or)o(ganization)d(\(\223Licensee\224\))208 282 y(accessing)18
b(and)g(otherwise)g(using)g(Python)f(1.6.1)h(softw)o(are)g(in)h(source)
e(or)i(binary)e(form)h(and)g(its)h(associated)g(documentation.)104
443 y(2.)41 b(Subject)24 b(to)g(the)h(terms)f(and)g(conditions)f(of)h
(this)h(License)g(Agreement,)e(CNRI)j(hereby)d(grants)h(Licensee)g(a)h
(none)o(xclusi)n(v)o(e,)208 542 y(ro)o(yalty-free,)18
b(w)o(orld-wide)i(license)h(to)g(reproduce,)d(analyze,)j(test,)g
(perform)e(and/or)h(display)h(publicly)-5 b(,)19 b(prepare)g(deri)n(v)n
(ati)n(v)o(e)208 642 y(w)o(orks,)28 b(distrib)n(ute,)g(and)f(otherwise)
f(use)i(Python)e(1.6.1)f(alone)i(or)g(in)g(an)o(y)f(deri)n(v)n(ati)n(v)
o(e)g(v)o(ersion,)h(pro)o(vided,)f(ho)n(we)n(v)o(er)m(,)g(that)208
741 y(CNRI')-5 b(s)30 b(License)f(Agreement)f(and)g(CNRI')-5
b(s)31 b(notice)e(of)g(cop)o(yright,)g(i.e.,)i(\223Cop)o(yright)2893
738 y(c)2870 741 y Fs(\015)f Ft(1995-2001)25 b(Corporation)i(for)208
841 y(National)k(Research)h(Initiati)n(v)o(es;)37 b(All)32
b(Rights)g(Reserv)o(ed\224)f(are)h(retained)f(in)h(Python)e(1.6.1)h
(alone)g(or)g(in)h(an)o(y)f(deri)n(v)n(ati)n(v)o(e)208
941 y(v)o(ersion)22 b(prepared)g(by)h(Licensee.)35 b(Alternately)-5
b(,)23 b(in)h(lieu)g(of)f(CNRI')-5 b(s)25 b(License)f(Agreement,)e
(Licensee)i(may)f(substitute)h(the)208 1040 y(follo)n(wing)j(te)o(xt)i
(\(omitting)e(the)i(quotes\):)42 b(\223Python)27 b(1.6.1)h(is)h(made)g
(a)n(v)n(ailable)f(subject)h(to)g(the)g(terms)g(and)f(conditions)f(in)
208 1140 y(CNRI')-5 b(s)24 b(License)f(Agreement.)33
b(This)24 b(Agreement)d(together)h(with)i(Python)e(1.6.1)g(may)h(be)g
(located)g(on)g(the)g(Internet)f(using)208 1240 y(the)27
b(follo)n(wing)f(unique,)h(persistent)g(identi\002er)g(\(kno)n(wn)f(as)
i(a)f(handle\):)39 b(1895.22/1013.)j(This)27 b(Agreement)f(may)h(also)h
(be)208 1339 y(obtained)18 b(from)h(a)i(proxy)d(serv)o(er)i(on)g(the)g
(Internet)f(using)g(the)i(follo)n(wing)d(URL:)j Fu(
http://hdl.handle)o
(.)o(ne)o(t/18)o(95)o(.2)o(2/1)o(01)o(3)-6 b Ft(.)g(\224)104
1499 y(3.)41 b(In)16 b(the)h(e)n(v)o(ent)f(Licensee)h(prepares)e(a)j
(deri)n(v)n(ati)n(v)o(e)d(w)o(ork)h(that)h(is)h(based)e(on)h(or)g
(incorporates)d(Python)i(1.6.1)g(or)g(an)o(y)h(part)f(thereof,)208
1599 y(and)25 b(w)o(ants)h(to)f(mak)o(e)g(the)h(deri)n(v)n(ati)n(v)o(e)
d(w)o(ork)i(a)n(v)n(ailable)g(to)h(others)f(as)h(pro)o(vided)d(herein,)
j(then)f(Licensee)g(hereby)f(agrees)h(to)208 1699 y(include)19
b(in)h(an)o(y)f(such)h(w)o(ork)g(a)h(brief)e(summary)g(of)h(the)g
(changes)f(made)h(to)g(Python)f(1.6.1.)104 1859 y(4.)41
b(CNRI)23 b(is)h(making)d(Python)h(1.6.1)f(a)n(v)n(ailable)h(to)h
(Licensee)g(on)f(an)h(\223)-7 b(AS)23 b(IS\224)g(basis.)34
b(CNRI)23 b(MAKES)g(NO)g(REPRESENT)-8 b(A-)208 1959 y(TIONS)25
b(OR)h(W)-10 b(ARRANTIES,)26 b(EXPRESS)g(OR)g(IMPLIED.)f(BY)h(W)-10
b(A)h(Y)26 b(OF)g(EXAMPLE,)f(B)o(UT)h(NO)m(T)f(LIMIT)-8
b(A)f(TION,)208 2058 y(CNRI)20 b(MAKES)f(NO)h(AND)g(DISCLAIMS)f(ANY)h
(REPRESENT)-8 b(A)f(TION)19 b(OR)h(W)-10 b(ARRANTY)21
b(OF)f(MERCHANT)-8 b(ABIL-)208 2158 y(ITY)17 b(OR)h(FITNESS)f(FOR)i
(ANY)e(P)-8 b(AR)j(TICULAR)19 b(PURPOSE)f(OR)g(THA)-9
b(T)17 b(THE)g(USE)h(OF)g(PYTHON)f(1.6.1)f(WILL)i(NO)m(T)208
2257 y(INFRINGE)i(ANY)g(THIRD)h(P)-8 b(AR)j(TY)21 b(RIGHTS.)104
2418 y(5.)41 b(CNRI)25 b(SHALL)g(NO)m(T)g(BE)g(LIABLE)g(T)o(O)f
(LICENSEE)g(OR)i(ANY)f(O)m(THER)g(USERS)h(OF)f(PYTHON)g(1.6.1)e(FOR)j
(ANY)208 2517 y(INCIDENT)-8 b(AL,)18 b(SPECIAL,)h(OR)h(CONSEQ)o
(UENTIAL)f(D)m(AMA)m(GES)f(OR)i(LOSS)g(AS)g(A)g(RESUL)-8
b(T)19 b(OF)h(MODIFYING,)208 2617 y(DISTRIB)o(UTING,)j(OR)h(O)m(THER)-5
b(WISE)24 b(USING)g(PYTHON)g(1.6.1,)f(OR)h(ANY)g(DERIV)-11
b(A)i(TIVE)23 b(THEREOF)-7 b(,)24 b(EVEN)f(IF)208 2717
y(AD)m(VISED)c(OF)i(THE)f(POSSIBILITY)g(THEREOF)-7 b(.)104
2877 y(6.)41 b(This)20 b(License)g(Agreement)e(will)j(automatically)e
(terminate)g(upon)g(a)i(material)f(breach)f(of)h(its)h(terms)f(and)g
(conditions.)104 3037 y(7.)41 b(This)16 b(License)f(Agreement)f(shall)j
(be)e(go)o(v)o(erned)e(by)j(the)f(federal)g(intellectual)h(property)d
(la)o(w)j(of)g(the)g(United)f(States,)i(including)208
3137 y(without)28 b(limitation)g(the)g(federal)g(cop)o(yright)f(la)o(w)
-5 b(,)30 b(and,)g(to)f(the)g(e)o(xtent)e(such)i(U.S.)g(federal)e(la)o
(w)i(does)g(not)f(apply)-5 b(,)29 b(by)g(the)208 3236
y(la)o(w)f(of)h(the)f(Commonwealth)f(of)h(V)-5 b(ir)o(ginia,)30
b(e)o(xcluding)c(V)-5 b(ir)o(ginia')g(s)28 b(con\003ict)g(of)g(la)o(w)h
(pro)o(visions.)49 b(Notwithstanding)27 b(the)208 3336
y(fore)o(going,)c(with)i(re)o(gard)f(to)h(deri)n(v)n(ati)n(v)o(e)e(w)o
(orks)i(based)g(on)g(Python)f(1.6.1)g(that)h(incorporate)e
(non-separable)g(material)h(that)208 3435 y(w)o(as)29
b(pre)n(viously)d(distrib)n(uted)i(under)f(the)h(GNU)h(General)f
(Public)g(License)h(\(GPL\),)f(the)g(la)o(w)h(of)f(the)g(Commonwealth)f
(of)208 3535 y(V)-5 b(ir)o(ginia)19 b(shall)j(go)o(v)o(ern)c(this)k
(License)e(Agreement)g(only)g(as)h(to)g(issues)h(arising)f(under)e(or)i
(with)g(respect)g(to)g(P)o(aragraphs)e(4,)i(5,)208 3635
y(and)d(7)i(of)f(this)h(License)f(Agreement.)k(Nothing)18
b(in)h(this)h(License)f(Agreement)f(shall)i(be)f(deemed)f(to)h(create)g
(an)o(y)g(relationship)208 3734 y(of)24 b(agenc)o(y)-5
b(,)24 b(partnership,)g(or)g(joint)h(v)o(enture)e(between)h(CNRI)h(and)
f(Licensee.)39 b(This)25 b(License)f(Agreement)f(does)i(not)f(grant)208
3834 y(permission)29 b(to)i(use)g(CNRI)g(trademarks)e(or)h(trade)g
(name)g(in)h(a)g(trademark)e(sense)i(to)f(endorse)g(or)g(promote)f
(products)g(or)208 3934 y(services)20 b(of)g(Licensee,)f(or)h(an)o(y)g
(third)f(party)-5 b(.)104 4094 y(8.)41 b(By)17 b(clicking)f(on)g(the)h
(\223)-7 b(A)m(CCEPT\224)18 b(b)n(utton)e(where)g(indicated,)g(or)h(by)
f(cop)o(ying,)g(installing)g(or)h(otherwise)f(using)g(Python)g(1.6.1,)
208 4193 y(Licensee)j(agrees)h(to)h(be)f(bound)e(by)i(the)g(terms)g
(and)g(conditions)e(of)i(this)h(License)f(Agreement.)1793
4362 y(A)m(CCEPT)722 4461 y Fr(CWI)h(LICENSE)h(A)-5 b(GREEMENT)23
b(FOR)d(PYTHON)g(0.9.0)e(THR)n(OUGH)h(1.2)0 4608 y Ft(Cop)o(yright)380
4605 y(c)357 4608 y Fs(\015)h Ft(1991)f(-)i(1995,)d(Stichting)i
(Mathematisch)f(Centrum)g(Amsterdam,)g(The)h(Netherlands.)k(All)d
(rights)e(reserv)o(ed.)0 4755 y(Permission)g(to)h(use,)g(cop)o(y)-5
b(,)19 b(modify)-5 b(,)17 b(and)i(distrib)n(ute)h(this)g(softw)o(are)f
(and)h(its)g(documentation)d(for)i(an)o(y)g(purpose)g(and)g(without)g
(fee)h(is)0 4855 y(hereby)i(granted,)g(pro)o(vided)f(that)i(the)h(abo)o
(v)o(e)d(cop)o(yright)h(notice)g(appear)h(in)g(all)h(copies)f(and)g
(that)g(both)g(that)g(cop)o(yright)e(notice)i(and)0 4954
y(this)f(permission)f(notice)g(appear)f(in)i(supporting)d
(documentation,)g(and)i(that)h(the)f(name)g(of)h(Stichting)f
(Mathematisch)f(Centrum)h(or)0 5054 y(CWI)26 b(not)f(be)h(used)f(in)g
(adv)o(ertising)f(or)h(publicity)f(pertaining)g(to)h(distrib)n(ution)g
(of)g(the)g(softw)o(are)g(without)g(speci\002c,)h(written)g(prior)0
5154 y(permission.)0 5300 y(STICHTING)18 b(MA)-9 b(THEMA)g(TISCH)18
b(CENTR)m(UM)g(DISCLAIMS)h(ALL)f(W)-10 b(ARRANTIES)20
b(WITH)f(REGARD)g(T)o(O)f(THIS)h(SOFT)-8 b(-)0 5400 y(W)e(ARE,)23
b(INCLUDING)e(ALL)h(IMPLIED)f(W)-10 b(ARRANTIES)23 b(OF)g(MERCHANT)-8
b(ABILITY)21 b(AND)i(FITNESS,)e(IN)i(NO)f(EVENT)p 0 5549
3901 4 v 0 5649 a Fo(82)2584 b(Appendix)25 b(B)n(.)47
b(Histor)r(y)22 b(and)i(License)p eop end
%%Page: 83 89
TeXDict begin 83 88 bop 0 83 a Ft(SHALL)30 b(STICHTING)g(MA)-9
b(THEMA)g(TISCH)29 b(CENTR)m(UM)h(BE)g(LIABLE)g(FOR)h(ANY)f(SPECIAL,)g
(INDIRECT)g(OR)h(CON-)0 183 y(SEQ)o(UENTIAL)e(D)m(AMA)m(GES)h(OR)h(ANY)
f(D)m(AMA)m(GES)f(WHA)-9 b(TSOEVER)31 b(RESUL)-8 b(TING)30
b(FR)m(OM)g(LOSS)h(OF)g(USE,)f(D)m(A)-9 b(T)h(A)0 282
y(OR)25 b(PR)m(OFITS,)f(WHETHER)h(IN)f(AN)h(A)m(CTION)f(OF)h(CONTRA)m
(CT)-6 b(,)24 b(NEGLIGENCE)g(OR)h(O)m(THER)f(T)o(OR)-5
b(TIOUS)24 b(A)m(CTION,)0 382 y(ARISING)c(OUT)h(OF)g(OR)g(IN)f
(CONNECTION)g(WITH)h(THE)f(USE)h(OR)g(PERFORMANCE)g(OF)g(THIS)f(SOFTW)
-10 b(ARE.)0 709 y Fl(B)n(.3)120 b(Licenses)34 b(and)g(Ac)n(kno)n
(wledgements)i(f)l(or)e(Incor)t(por)o(ated)i(Softw)n(are)0
942 y Ft(This)18 b(section)f(is)h(an)g(incomplete,)e(b)n(ut)h(gro)n
(wing)f(list)i(of)g(licenses)f(and)g(ackno)n(wledgements)e(for)h
(third-party)f(softw)o(are)i(incorporated)0 1042 y(in)j(the)h(Python)e
(distrib)n(ution.)0 1327 y Fg(B)n(.3.1)100 b(Mersenne)30
b(T)-12 b(wister)0 1530 y Ft(The)15 b Fm(_random)f Ft(module)g
(includes)h(code)f(based)h(on)g(a)g(do)n(wnload)f(from)g
Fu(
http://www)l(.math.k)o(eio)m(.ac.j)o(p/)g(matumoto/MT2002/emt19937)o
(ar)l(.h)o(tml)-6 b Ft(.)0 1629 y(The)20 b(follo)n(wing)e(are)j(the)f
(v)o(erbatim)e(comments)h(from)h(the)g(original)f(code:)p
0 5549 3901 4 v 0 5649 a Fo(B)n(.3.)53 b(Licenses)22
b(and)i(Ac)n(kno)o(wledgements)g(f)n(or)e(Incor)r(por)o(ated)i(Softw)o
(are)1424 b(83)p eop end
%%Page: 84 90
TeXDict begin 84 89 bop 236 174 a Fj(A)45 b(C-program)e(for)i(MT19937,)
e(with)h(initialization)f(improved)g(2002/1/26.)236 266
y(Coded)h(by)h(Takuji)f(Nishimura)f(and)h(Makoto)g(Matsumoto.)236
448 y(Before)g(using,)g(initialize)f(the)h(state)g(by)h(using)f
(init_genrand\(seed\))236 540 y(or)h(init_by_array\(init_key,)c
(key_length\).)236 722 y(Copyright)j(\(C\))g(1997)g(-)h(2002,)f(Makoto)
g(Matsumoto)f(and)h(Takuji)g(Nishimura,)236 814 y(All)h(rights)e
(reserved.)236 996 y(Redistribution)g(and)h(use)g(in)h(source)f(and)g
(binary)g(forms,)g(with)g(or)g(without)236 1088 y(modification,)f(are)h
(permitted)g(provided)f(that)h(the)g(following)g(conditions)236
1179 y(are)h(met:)281 1362 y(1.)g(Redistributions)d(of)i(source)g(code)
g(must)g(retain)g(the)h(above)f(copyright)416 1453 y(notice,)f(this)h
(list)h(of)f(conditions)f(and)h(the)h(following)e(disclaimer.)281
1636 y(2.)i(Redistributions)d(in)i(binary)g(form)g(must)g(reproduce)g
(the)g(above)g(copyright)416 1727 y(notice,)f(this)h(list)h(of)f
(conditions)f(and)h(the)h(following)e(disclaimer)g(in)i(the)416
1818 y(documentation)d(and/or)i(other)g(materials)f(provided)h(with)g
(the)g(distribution.)281 2001 y(3.)h(The)f(names)g(of)g(its)h
(contributors)e(may)h(not)g(be)h(used)f(to)g(endorse)g(or)g(promote)416
2092 y(products)f(derived)h(from)g(this)g(software)f(without)h
(specific)g(prior)g(written)416 2183 y(permission.)236
2366 y(THIS)g(SOFTWARE)g(IS)g(PROVIDED)g(BY)g(THE)h(COPYRIGHT)e
(HOLDERS)h(AND)g(CONTRIBUTORS)236 2457 y("AS)h(IS")f(AND)g(ANY)h
(EXPRESS)e(OR)i(IMPLIED)e(WARRANTIES,)g(INCLUDING,)g(BUT)i(NOT)236
2549 y(LIMITED)f(TO,)g(THE)h(IMPLIED)e(WARRANTIES)g(OF)i
(MERCHANTABILITY)d(AND)i(FITNESS)g(FOR)236 2640 y(A)h(PARTICULAR)e
(PURPOSE)h(ARE)g(DISCLAIMED.)88 b(IN)44 b(NO)h(EVENT)f(SHALL)g(THE)g
(COPYRIGHT)f(OWNER)h(OR)236 2731 y(CONTRIBUTORS)f(BE)i(LIABLE)e(FOR)i
(ANY)f(DIRECT,)g(INDIRECT,)f(INCIDENTAL,)g(SPECIAL,)236
2823 y(EXEMPLARY,)g(OR)i(CONSEQUENTIAL)e(DAMAGES)g(\(INCLUDING,)g(BUT)h
(NOT)h(LIMITED)e(TO,)236 2914 y(PROCUREMENT)g(OF)i(SUBSTITUTE)e(GOODS)h
(OR)g(SERVICES;)g(LOSS)g(OF)g(USE,)g(DATA,)g(OR)236 3005
y(PROFITS;)g(OR)g(BUSINESS)g(INTERRUPTION\))e(HOWEVER)i(CAUSED)g(AND)g
(ON)h(ANY)f(THEORY)g(OF)236 3097 y(LIABILITY,)f(WHETHER)h(IN)h
(CONTRACT,)e(STRICT)h(LIABILITY,)f(OR)h(TORT)g(\(INCLUDING)236
3188 y(NEGLIGENCE)f(OR)i(OTHERWISE\))e(ARISING)h(IN)g(ANY)g(WAY)h(OUT)f
(OF)h(THE)f(USE)g(OF)h(THIS)236 3279 y(SOFTWARE,)f(EVEN)g(IF)g(ADVISED)
g(OF)g(THE)h(POSSIBILITY)e(OF)h(SUCH)g(DAMAGE.)236 3553
y(Any)h(feedback)e(is)i(very)f(welcome.)236 3645 y
(
http://www.math.keio.ac.jp/matumoto/emt.)o(html)236
3736 y(email:)g(
[email protected])0 4161 y Fg(B)n(.3.2)100
b(Soc)n(k)n(ets)0 4364 y Ft(The)23 b Fm(socket)g Ft(module)e(uses)j
(the)g(functions,)e Fm(getaddrinfo)p Ft(,)g(and)h Fm(getnameinfo)p
Ft(,)f(which)h(are)g(coded)f(in)i(separate)e(source)0
4463 y(\002les)f(from)e(the)i(WIDE)f(Project,)g Fu(
http://www)l(.wide)o
(.ad.j)o(p/a)o(bo)o(ut/)o(ind)o(e)m(x.html)-6 b Ft(.)p
0 5549 3901 4 v 0 5649 a Fo(84)2584 b(Appendix)25 b(B)n(.)47
b(Histor)r(y)22 b(and)i(License)p eop end
%%Page: 85 91
TeXDict begin 85 90 bop 236 83 a Fj(Copyright)44 b(\(C\))g(1995,)g
(1996,)g(1997,)g(and)g(1998)g(WIDE)g(Project.)236 174
y(All)h(rights)e(reserved.)236 357 y(Redistribution)g(and)h(use)g(in)h
(source)f(and)g(binary)g(forms,)g(with)g(or)g(without)236
448 y(modification,)f(are)h(permitted)g(provided)f(that)h(the)g
(following)g(conditions)236 540 y(are)h(met:)236 631
y(1.)g(Redistributions)d(of)j(source)e(code)i(must)f(retain)f(the)i
(above)f(copyright)371 722 y(notice,)f(this)i(list)f(of)g(conditions)f
(and)i(the)f(following)f(disclaimer.)236 814 y(2.)i(Redistributions)d
(in)j(binary)e(form)i(must)f(reproduce)f(the)h(above)g(copyright)371
905 y(notice,)f(this)i(list)f(of)g(conditions)f(and)i(the)f(following)f
(disclaimer)g(in)i(the)371 996 y(documentation)d(and/or)i(other)g
(materials)f(provided)h(with)g(the)g(distribution.)236
1088 y(3.)h(Neither)e(the)i(name)f(of)g(the)h(project)e(nor)i(the)f
(names)g(of)g(its)h(contributors)371 1179 y(may)f(be)h(used)f(to)g
(endorse)g(or)g(promote)g(products)f(derived)h(from)g(this)g(software)
371 1270 y(without)f(specific)h(prior)g(written)g(permission.)236
1453 y(THIS)g(SOFTWARE)g(IS)g(PROVIDED)g(BY)g(THE)h(PROJECT)e(AND)i
(CONTRIBUTORS)d(``AS)j(IS'')f(AND)236 1544 y(GAI_ANY)g(EXPRESS)g(OR)g
(IMPLIED)g(WARRANTIES,)f(INCLUDING,)g(BUT)h(NOT)g(LIMITED)g(TO,)g(THE)
236 1636 y(IMPLIED)g(WARRANTIES)f(OF)i(MERCHANTABILITY)d(AND)i(FITNESS)
g(FOR)g(A)h(PARTICULAR)e(PURPOSE)236 1727 y(ARE)i(DISCLAIMED.)87
b(IN)45 b(NO)f(EVENT)g(SHALL)g(THE)h(PROJECT)e(OR)i(CONTRIBUTORS)e(BE)h
(LIABLE)236 1818 y(FOR)h(GAI_ANY)e(DIRECT,)h(INDIRECT,)f(INCIDENTAL,)g
(SPECIAL,)h(EXEMPLARY,)f(OR)h(CONSEQUENTIAL)236 1910
y(DAMAGES)g(\(INCLUDING,)f(BUT)h(NOT)h(LIMITED)e(TO,)h(PROCUREMENT)f
(OF)i(SUBSTITUTE)e(GOODS)236 2001 y(OR)i(SERVICES;)e(LOSS)h(OF)h(USE,)f
(DATA,)g(OR)g(PROFITS;)g(OR)g(BUSINESS)g(INTERRUPTION\))236
2092 y(HOWEVER)g(CAUSED)g(AND)g(ON)h(GAI_ANY)e(THEORY)h(OF)g
(LIABILITY,)g(WHETHER)f(IN)i(CONTRACT,)e(STRICT)236 2183
y(LIABILITY,)g(OR)i(TORT)f(\(INCLUDING)f(NEGLIGENCE)g(OR)i(OTHERWISE\))
e(ARISING)h(IN)g(GAI_ANY)g(WAY)236 2275 y(OUT)h(OF)f(THE)g(USE)h(OF)f
(THIS)g(SOFTWARE,)g(EVEN)g(IF)g(ADVISED)g(OF)g(THE)h(POSSIBILITY)e(OF)
236 2366 y(SUCH)h(DAMAGE.)0 2789 y Fg(B)n(.3.3)100 b(Floating)29
b(point)g(e)m(xception)g(control)0 2992 y Ft(The)20 b(source)f(for)h
(the)g Fm(fpectl)g Ft(module)f(includes)g(the)h(follo)n(wing)f(notice:)
p 0 5549 3901 4 v 0 5649 a Fo(B)n(.3.)53 b(Licenses)22
b(and)i(Ac)n(kno)o(wledgements)g(f)n(or)e(Incor)r(por)o(ated)i(Softw)o
(are)1424 b(85)p eop end
%%Page: 86 92
TeXDict begin 86 91 bop 460 174 a Fj
(-----------------------------------------)o(------)o(------)o(------)o
(------)o(----)416 266 y(/)1031 b(Copyright)43 b(\(c\))h(1996.)1210
b(\\)371 357 y(|)448 b(The)44 b(Regents)g(of)g(the)h(University)e(of)h
(California.)761 b(|)371 448 y(|)1076 b(All)44 b(rights)g(reserved.)
1209 b(|)371 540 y(|)3183 b(|)371 631 y(|)134 b(Permission)43
b(to)i(use,)f(copy,)g(modify,)f(and)i(distribute)e(this)h(software)g
(for)134 b(|)371 722 y(|)g(any)44 b(purpose)g(without)g(fee)g(is)h
(hereby)e(granted,)h(provided)f(that)h(this)h(en-)134
b(|)371 814 y(|)g(tire)44 b(notice)g(is)h(included)e(in)i(all)f(copies)
g(of)g(any)g(software)g(which)g(is)g(or)135 b(|)371 905
y(|)f(includes)88 b(a)i(copy)f(or)g(modification)f(of)h(this)44
b(software)g(and)g(in)h(all)134 b(|)371 996 y(|)g(copies)44
b(of)g(the)h(supporting)e(documentation)f(for)j(such)f(software.)492
b(|)371 1088 y(|)3183 b(|)371 1179 y(|)134 b(This)89
b(work)44 b(was)h(produced)e(at)i(the)f(University)f(of)h(California,)f
(Lawrence)134 b(|)371 1270 y(|)g(Livermore)43 b(National)h(Laboratory)f
(under)89 b(contract)f(no.)h(W-7405-ENG-48)133 b(|)371
1362 y(|)h(between)89 b(the)g(U.S.)g(Department)f(of)h(Energy)44
b(and)g(The)g(Regents)g(of)h(the)134 b(|)371 1453 y(|)g(University)43
b(of)i(California)e(for)h(the)g(operation)g(of)g(UC)h(LLNL.)627
b(|)371 1544 y(|)3183 b(|)371 1636 y(|)1345 b(DISCLAIMER)1388
b(|)371 1727 y(|)3183 b(|)371 1818 y(|)134 b(This)89
b(software)44 b(was)g(prepared)f(as)i(an)f(account)g(of)g(work)h
(sponsored)e(by)h(an)135 b(|)371 1910 y(|)f(agency)44
b(of)g(the)h(United)f(States)f(Government.)g(Neither)h(the)g(United)g
(States)134 b(|)371 2001 y(|)g(Government)88 b(nor)44
b(the)h(University)e(of)h(California)f(nor)i(any)f(of)h(their)f(em-)134
b(|)371 2092 y(|)g(ployees,)44 b(makes)g(any)g(warranty,)f(express)h
(or)g(implied,)g(or)89 b(assumes)g(any)134 b(|)371 2183
y(|)g(liability)88 b(or)i(responsibility)d(for)44 b(the)g(accuracy,)g
(completeness,)e(or)135 b(|)371 2275 y(|)f(usefulness)43
b(of)i(any)f(information,)88 b(apparatus,)g(product,)g(or)h(process)134
b(|)371 2366 y(|)g(disclosed,)f(or)89 b(represents)f(that)h(its)g(use)g
(would)g(not)g(infringe)134 b(|)371 2457 y(|)g(privately-owned)42
b(rights.)i(Reference)f(herein)h(to)h(any)f(specific)88
b(commer-)134 b(|)371 2549 y(|)g(cial)89 b(products,)f(process,)h(or)g
(service)f(by)45 b(trade)f(name,)g(trademark,)133 b(|)371
2640 y(|)h(manufacturer,)43 b(or)h(otherwise,)f(does)h(not)90
b(necessarily)d(constitute)h(or)135 b(|)371 2731 y(|)f(imply)89
b(its)44 b(endorsement,)f(recommendation,)f(or)j(favoring)e(by)i(the)f
(United)134 b(|)371 2823 y(|)g(States)44 b(Government)f(or)i(the)f
(University)f(of)h(California.)f(The)i(views)89 b(and)134
b(|)371 2914 y(|)g(opinions)88 b(of)45 b(authors)f(expressed)f(herein)h
(do)g(not)g(necessarily)f(state)h(or)135 b(|)371 3005
y(|)f(reflect)44 b(those)g(of)g(the)h(United)e(States)h(Government)f
(or)90 b(the)f(University)133 b(|)371 3097 y(|)h(of)89
b(California,)f(and)45 b(shall)f(not)g(be)g(used)g(for)h(advertising)e
(or)h(product)134 b(|)416 3188 y(\\)89 b(endorsement)43
b(purposes.)2061 b(/)460 3279 y
(-----------------------------------------)o(------)o(------)o(------)o
(------)o(----)0 3703 y Fg(B)n(.3.4)100 b(MD5)28 b(message)h(digest)g
(algor)q(ithm)0 3906 y Ft(The)20 b(source)f(code)h(for)f(the)i
Fm(md5)f Ft(module)f(contains)g(the)h(follo)n(wing)f(notice:)p
0 5549 3901 4 v 0 5649 a Fo(86)2584 b(Appendix)25 b(B)n(.)47
b(Histor)r(y)22 b(and)i(License)p eop end
%%Page: 87 93
TeXDict begin 87 92 bop 326 174 a Fj(Copyright)43 b(\(C\))i(1999,)f
(2002)g(Aladdin)f(Enterprises.)88 b(All)44 b(rights)g(reserved.)326
357 y(This)g(software)g(is)g(provided)g('as-is',)f(without)h(any)g
(express)g(or)g(implied)326 448 y(warranty.)88 b(In)45
b(no)f(event)g(will)g(the)h(authors)e(be)i(held)f(liable)g(for)g(any)g
(damages)326 540 y(arising)g(from)g(the)g(use)g(of)h(this)f(software.)
326 722 y(Permission)f(is)i(granted)e(to)i(anyone)f(to)g(use)g(this)g
(software)g(for)g(any)h(purpose,)326 814 y(including)e(commercial)g
(applications,)g(and)h(to)h(alter)f(it)g(and)h(redistribute)d(it)326
905 y(freely,)i(subject)f(to)i(the)f(following)f(restrictions:)326
1088 y(1.)h(The)h(origin)f(of)g(this)g(software)g(must)g(not)g(be)h
(misrepresented;)d(you)i(must)g(not)460 1179 y(claim)g(that)h(you)f
(wrote)g(the)g(original)g(software.)f(If)h(you)h(use)f(this)g(software)
460 1270 y(in)h(a)g(product,)e(an)i(acknowledgment)d(in)i(the)h
(product)e(documentation)g(would)h(be)460 1362 y(appreciated)f(but)i
(is)f(not)h(required.)326 1453 y(2.)f(Altered)g(source)g(versions)f
(must)i(be)f(plainly)g(marked)f(as)i(such,)f(and)g(must)g(not)h(be)460
1544 y(misrepresented)e(as)h(being)g(the)h(original)e(software.)326
1636 y(3.)h(This)h(notice)e(may)i(not)f(be)h(removed)e(or)i(altered)e
(from)h(any)h(source)f(distribution.)326 1818 y(L.)g(Peter)g(Deutsch)
326 1910 y(
[email protected])326 2092 y(Independent)f(implementation)f
(of)j(MD5)f(\(RFC)g(1321\).)326 2275 y(This)g(code)g(implements)f(the)i
(MD5)f(Algorithm)f(defined)h(in)g(RFC)h(1321,)f(whose)326
2366 y(text)g(is)h(available)e(at)236 2457 y
(
http://www.ietf.org/rfc/rfc1321.txt)326 2549 y(The)h(code)g(is)h
(derived)f(from)g(the)g(text)g(of)h(the)f(RFC,)g(including)f(the)i
(test)f(suite)326 2640 y(\(section)f(A.5\))i(but)f(excluding)f(the)i
(rest)f(of)g(Appendix)g(A.)89 b(It)44 b(does)h(not)f(include)326
2731 y(any)g(code)g(or)h(documentation)e(that)h(is)g(identified)f(in)i
(the)f(RFC)g(as)h(being)326 2823 y(copyrighted.)326 3005
y(The)f(original)g(and)g(principal)f(author)h(of)h(md5.h)f(is)g(L.)h
(Peter)f(Deutsch)326 3097 y(<
[email protected]>.)86 b(Other)44
b(authors)g(are)g(noted)g(in)h(the)f(change)g(history)326
3188 y(that)g(follows)g(\(in)g(reverse)g(chronological)e(order\):)326
3371 y(2002-04-13)h(lpd)h(Removed)g(support)g(for)g(non-ANSI)f
(compilers;)h(removed)236 3462 y(references)f(to)i(Ghostscript;)e
(clarified)g(derivation)g(from)h(RFC)h(1321;)236 3553
y(now)g(handles)e(byte)h(order)g(either)g(statically)f(or)i
(dynamically.)326 3645 y(1999-11-04)e(lpd)h(Edited)g(comments)g
(slightly)f(for)h(automatic)g(TOC)g(extraction.)326 3736
y(1999-10-18)f(lpd)h(Fixed)g(typo)h(in)f(header)g(comment)f(\(ansi2knr)
h(rather)g(than)g(md5\);)236 3827 y(added)g(conditionalization)e(for)i
(C++)h(compilation)e(from)h(Martin)236 3919 y(Purschke)g
(<
[email protected]>.)326 4010 y(1999-05-03)f(lpd)h(Original)g(version.)
0 4433 y Fg(B)n(.3.5)100 b(Asynchronous)30 b(soc)n(k)n(et)e(ser)s
(vices)0 4636 y Ft(The)20 b Fm(asynchat)f Ft(and)h Fm(asyncore)f
Ft(modules)g(contain)g(the)i(follo)n(wing)d(notice:)p
0 5549 3901 4 v 0 5649 a Fo(B)n(.3.)53 b(Licenses)22
b(and)i(Ac)n(kno)o(wledgements)g(f)n(or)e(Incor)r(por)o(ated)i(Softw)o
(are)1424 b(87)p eop end
%%Page: 88 94
TeXDict begin 88 93 bop 281 174 a Fj(Copyright)43 b(1996)i(by)f(Sam)g
(Rushing)1357 357 y(All)g(Rights)g(Reserved)281 540 y(Permission)f(to)i
(use,)f(copy,)g(modify,)g(and)g(distribute)f(this)h(software)g(and)281
631 y(its)g(documentation)f(for)h(any)h(purpose)e(and)i(without)e(fee)i
(is)f(hereby)281 722 y(granted,)g(provided)f(that)h(the)h(above)f
(copyright)f(notice)h(appear)f(in)i(all)281 814 y(copies)f(and)g(that)g
(both)h(that)f(copyright)f(notice)h(and)g(this)g(permission)281
905 y(notice)g(appear)g(in)g(supporting)f(documentation,)g(and)h(that)g
(the)h(name)f(of)g(Sam)281 996 y(Rushing)g(not)g(be)h(used)f(in)g
(advertising)f(or)i(publicity)e(pertaining)g(to)281 1088
y(distribution)g(of)h(the)h(software)e(without)h(specific,)f(written)h
(prior)281 1179 y(permission.)281 1362 y(SAM)g(RUSHING)g(DISCLAIMS)f
(ALL)i(WARRANTIES)e(WITH)h(REGARD)g(TO)g(THIS)g(SOFTWARE,)281
1453 y(INCLUDING)f(ALL)i(IMPLIED)e(WARRANTIES)h(OF)g(MERCHANTABILITY)e
(AND)j(FITNESS,)e(IN)281 1544 y(NO)i(EVENT)f(SHALL)g(SAM)g(RUSHING)g
(BE)g(LIABLE)g(FOR)g(ANY)g(SPECIAL,)g(INDIRECT)f(OR)281
1636 y(CONSEQUENTIAL)g(DAMAGES)g(OR)i(ANY)f(DAMAGES)g(WHATSOEVER)f
(RESULTING)g(FROM)h(LOSS)281 1727 y(OF)h(USE,)f(DATA)g(OR)g(PROFITS,)g
(WHETHER)g(IN)g(AN)g(ACTION)g(OF)h(CONTRACT,)281 1818
y(NEGLIGENCE)e(OR)i(OTHER)f(TORTIOUS)f(ACTION,)h(ARISING)f(OUT)i(OF)f
(OR)h(IN)281 1910 y(CONNECTION)e(WITH)h(THE)h(USE)f(OR)h(PERFORMANCE)d
(OF)j(THIS)f(SOFTWARE.)0 2334 y Fg(B)n(.3.6)100 b(Cookie)29
b(management)0 2537 y Ft(The)20 b Fm(Cookie)f Ft(module)g(contains)h
(the)g(follo)n(wing)f(notice:)281 2775 y Fj(Copyright)43
b(2000)i(by)f(Timothy)g(O'Malley)f(<
[email protected]>)954
2958 y(All)h(Rights)g(Reserved)281 3141 y(Permission)f(to)i(use,)f
(copy,)g(modify,)g(and)g(distribute)f(this)h(software)281
3232 y(and)g(its)h(documentation)d(for)j(any)f(purpose)g(and)g(without)
g(fee)g(is)g(hereby)281 3323 y(granted,)g(provided)f(that)h(the)h
(above)f(copyright)f(notice)h(appear)f(in)i(all)281 3415
y(copies)f(and)g(that)g(both)h(that)f(copyright)f(notice)h(and)g(this)g
(permission)281 3506 y(notice)g(appear)g(in)g(supporting)f
(documentation,)g(and)h(that)g(the)h(name)f(of)281 3597
y(Timothy)g(O'Malley)88 b(not)44 b(be)h(used)f(in)h(advertising)d(or)j
(publicity)281 3689 y(pertaining)e(to)i(distribution)e(of)h(the)g
(software)g(without)f(specific,)h(written)281 3780 y(prior)g
(permission.)281 3963 y(Timothy)g(O'Malley)f(DISCLAIMS)h(ALL)g
(WARRANTIES)f(WITH)h(REGARD)g(TO)g(THIS)281 4054 y(SOFTWARE,)f
(INCLUDING)h(ALL)g(IMPLIED)g(WARRANTIES)f(OF)h(MERCHANTABILITY)281
4145 y(AND)g(FITNESS,)g(IN)g(NO)h(EVENT)f(SHALL)g(Timothy)f(O'Malley)h
(BE)g(LIABLE)g(FOR)281 4237 y(ANY)g(SPECIAL,)g(INDIRECT)f(OR)i
(CONSEQUENTIAL)d(DAMAGES)i(OR)h(ANY)f(DAMAGES)281 4328
y(WHATSOEVER)f(RESULTING)h(FROM)g(LOSS)g(OF)g(USE,)g(DATA)h(OR)f
(PROFITS,)281 4419 y(WHETHER)g(IN)g(AN)h(ACTION)f(OF)g(CONTRACT,)f
(NEGLIGENCE)g(OR)i(OTHER)f(TORTIOUS)281 4511 y(ACTION,)g(ARISING)f(OUT)
i(OF)f(OR)h(IN)f(CONNECTION)f(WITH)h(THE)h(USE)f(OR)281
4602 y(PERFORMANCE)f(OF)i(THIS)f(SOFTWARE.)0 5026 y Fg(B)n(.3.7)100
b(Pro\002ling)0 5229 y Ft(The)20 b Fm(profile)f Ft(and)h
Fm(pstats)g Ft(modules)f(contain)g(the)h(follo)n(wing)f(notice:)p
0 5549 3901 4 v 0 5649 a Fo(88)2584 b(Appendix)25 b(B)n(.)47
b(Histor)r(y)22 b(and)i(License)p eop end
%%Page: 89 95
TeXDict begin 89 94 bop 281 174 a Fj(Copyright)43 b(1994,)h(by)h
(InfoSeek)e(Corporation,)g(all)h(rights)g(reserved.)281
266 y(Written)g(by)g(James)g(Roskind)281 448 y(Permission)f(to)i(use,)f
(copy,)g(modify,)g(and)g(distribute)f(this)h(Python)g(software)281
540 y(and)g(its)h(associated)e(documentation)g(for)h(any)g(purpose)g
(\(subject)f(to)i(the)281 631 y(restriction)e(in)i(the)f(following)f
(sentence\))g(without)h(fee)g(is)h(hereby)f(granted,)281
722 y(provided)g(that)g(the)g(above)g(copyright)f(notice)h(appears)g
(in)g(all)h(copies,)e(and)281 814 y(that)h(both)g(that)h(copyright)e
(notice)h(and)g(this)g(permission)f(notice)h(appear)g(in)281
905 y(supporting)f(documentation,)g(and)h(that)g(the)g(name)h(of)f
(InfoSeek)g(not)g(be)g(used)h(in)281 996 y(advertising)e(or)i
(publicity)e(pertaining)g(to)h(distribution)f(of)i(the)f(software)281
1088 y(without)g(specific,)f(written)h(prior)g(permission.)87
b(This)45 b(permission)e(is)281 1179 y(explicitly)g(restricted)g(to)i
(the)f(copying)g(and)g(modification)f(of)h(the)h(software)281
1270 y(to)g(remain)e(in)i(Python,)f(compiled)f(Python,)h(or)g(other)g
(languages)f(\(such)h(as)h(C\))281 1362 y(wherein)f(the)g(modified)g
(or)g(derived)g(code)g(is)g(exclusively)f(imported)h(into)g(a)281
1453 y(Python)g(module.)281 1636 y(INFOSEEK)g(CORPORATION)f(DISCLAIMS)g
(ALL)h(WARRANTIES)f(WITH)h(REGARD)g(TO)h(THIS)281 1727
y(SOFTWARE,)e(INCLUDING)h(ALL)g(IMPLIED)g(WARRANTIES)f(OF)h
(MERCHANTABILITY)f(AND)281 1818 y(FITNESS.)h(IN)g(NO)h(EVENT)f(SHALL)g
(INFOSEEK)f(CORPORATION)g(BE)h(LIABLE)g(FOR)h(ANY)281
1910 y(SPECIAL,)f(INDIRECT)f(OR)i(CONSEQUENTIAL)d(DAMAGES)i(OR)g(ANY)h
(DAMAGES)e(WHATSOEVER)281 2001 y(RESULTING)g(FROM)i(LOSS)f(OF)g(USE,)g
(DATA)h(OR)f(PROFITS,)f(WHETHER)h(IN)h(AN)f(ACTION)g(OF)281
2092 y(CONTRACT,)f(NEGLIGENCE)h(OR)g(OTHER)g(TORTIOUS)f(ACTION,)h
(ARISING)g(OUT)g(OF)h(OR)f(IN)281 2183 y(CONNECTION)f(WITH)h(THE)h(USE)
f(OR)h(PERFORMANCE)d(OF)j(THIS)f(SOFTWARE.)0 2607 y Fg(B)n(.3.8)100
b(Ex)m(ecution)29 b(tr)o(acing)0 2810 y Ft(The)20 b Fm(trace)g
Ft(module)e(contains)i(the)g(follo)n(wing)f(notice:)p
0 5549 3901 4 v 0 5649 a Fo(B)n(.3.)53 b(Licenses)22
b(and)i(Ac)n(kno)o(wledgements)g(f)n(or)e(Incor)r(por)o(ated)i(Softw)o
(are)1424 b(89)p eop end
%%Page: 90 96
TeXDict begin 90 95 bop 281 174 a Fj(portions)44 b(copyright)f(2001,)h
(Autonomous)f(Zones)h(Industries,)f(Inc.,)h(all)g(rights...)281
266 y(err...)89 b(reserved)43 b(and)i(offered)e(to)i(the)f(public)g
(under)g(the)g(terms)g(of)h(the)281 357 y(Python)f(2.2)g(license.)281
448 y(Author:)g(Zooko)g(O'Whielacronx)281 540 y(
http://zooko.com/)281
631 y(mailto:
[email protected])281 814 y(Copyright)f(2000,)h(Mojam)g
(Media,)g(Inc.,)g(all)g(rights)g(reserved.)281 905 y(Author:)g(Skip)g
(Montanaro)281 1088 y(Copyright)f(1999,)h(Bioreason,)g(Inc.,)g(all)g
(rights)g(reserved.)281 1179 y(Author:)g(Andrew)g(Dalke)281
1362 y(Copyright)f(1995-1997,)h(Automatrix,)f(Inc.,)g(all)i(rights)f
(reserved.)281 1453 y(Author:)g(Skip)g(Montanaro)281
1636 y(Copyright)f(1991-1995,)h(Stichting)f(Mathematisch)g(Centrum,)g
(all)h(rights)g(reserved.)281 1910 y(Permission)f(to)i(use,)f(copy,)g
(modify,)g(and)g(distribute)f(this)h(Python)g(software)f(and)281
2001 y(its)h(associated)g(documentation)e(for)j(any)f(purpose)f
(without)h(fee)g(is)h(hereby)281 2092 y(granted,)f(provided)f(that)h
(the)h(above)f(copyright)f(notice)h(appears)f(in)i(all)f(copies,)281
2183 y(and)g(that)h(both)f(that)g(copyright)f(notice)h(and)g(this)g
(permission)g(notice)f(appear)h(in)281 2275 y(supporting)f
(documentation,)g(and)h(that)g(the)g(name)h(of)f(neither)g(Automatrix,)
281 2366 y(Bioreason)f(or)i(Mojam)f(Media)g(be)g(used)h(in)f
(advertising)f(or)h(publicity)g(pertaining)f(to)281 2457
y(distribution)g(of)h(the)h(software)e(without)h(specific,)f(written)h
(prior)g(permission.)0 2882 y Fg(B)n(.3.9)100 b(UUencode)30
b(and)e(UUdecode)i(functions)0 3084 y Ft(The)20 b Fm(uu)g
Ft(module)f(contains)h(the)g(follo)n(wing)e(notice:)p
0 5549 3901 4 v 0 5649 a Fo(90)2584 b(Appendix)25 b(B)n(.)47
b(Histor)r(y)22 b(and)i(License)p eop end
%%Page: 91 97
TeXDict begin 91 96 bop 281 174 a Fj(Copyright)43 b(1994)i(by)f(Lance)g
(Ellinghouse)281 266 y(Cathedral)f(City,)h(California)g(Republic,)f
(United)h(States)f(of)i(America.)1312 357 y(All)g(Rights)e(Reserved)281
448 y(Permission)g(to)i(use,)f(copy,)g(modify,)g(and)g(distribute)f
(this)h(software)g(and)g(its)281 540 y(documentation)f(for)h(any)g
(purpose)g(and)g(without)g(fee)g(is)h(hereby)f(granted,)281
631 y(provided)g(that)g(the)g(above)g(copyright)f(notice)h(appear)g(in)
g(all)h(copies)f(and)g(that)281 722 y(both)g(that)g(copyright)g(notice)
g(and)g(this)g(permission)f(notice)h(appear)g(in)281
814 y(supporting)f(documentation,)g(and)h(that)g(the)g(name)h(of)f
(Lance)g(Ellinghouse)281 905 y(not)g(be)h(used)f(in)h(advertising)e(or)
h(publicity)f(pertaining)g(to)i(distribution)281 996
y(of)g(the)f(software)f(without)h(specific,)f(written)h(prior)g
(permission.)281 1088 y(LANCE)g(ELLINGHOUSE)f(DISCLAIMS)g(ALL)i
(WARRANTIES)e(WITH)h(REGARD)g(TO)281 1179 y(THIS)g(SOFTWARE,)g
(INCLUDING)f(ALL)h(IMPLIED)g(WARRANTIES)f(OF)h(MERCHANTABILITY)f(AND)
281 1270 y(FITNESS,)h(IN)g(NO)h(EVENT)f(SHALL)g(LANCE)g(ELLINGHOUSE)e
(CENTRUM)i(BE)h(LIABLE)281 1362 y(FOR)f(ANY)h(SPECIAL,)e(INDIRECT)h(OR)
g(CONSEQUENTIAL)f(DAMAGES)g(OR)i(ANY)f(DAMAGES)281 1453
y(WHATSOEVER)f(RESULTING)h(FROM)g(LOSS)g(OF)g(USE,)g(DATA)h(OR)f
(PROFITS,)g(WHETHER)f(IN)i(AN)281 1544 y(ACTION)f(OF)g(CONTRACT,)g
(NEGLIGENCE)f(OR)h(OTHER)g(TORTIOUS)g(ACTION,)f(ARISING)h(OUT)281
1636 y(OF)h(OR)f(IN)h(CONNECTION)e(WITH)h(THE)g(USE)h(OR)f(PERFORMANCE)
f(OF)h(THIS)h(SOFTWARE.)281 1818 y(Modified)f(by)g(Jack)g(Jansen,)g
(CWI,)g(July)g(1995:)281 1910 y(-)h(Use)f(binascii)g(module)f(to)i(do)f
(the)h(actual)e(line-by-line)g(conversion)371 2001 y(between)g(ascii)h
(and)h(binary.)e(This)i(results)e(in)i(a)f(1000-fold)g(speedup.)f(The)h
(C)371 2092 y(version)f(is)i(still)f(5)h(times)f(faster,)f(though.)281
2183 y(-)i(Arguments)e(more)h(compliant)g(with)g(python)f(standard)0
2608 y Fg(B)n(.3.10)101 b(XML)28 b(Remote)h(Procedure)h(Calls)0
2811 y Ft(The)20 b Fm(xmlrpclib)f Ft(module)g(contains)g(the)h(follo)n
(wing)f(notice:)p 0 5549 3901 4 v 0 5649 a Fo(B)n(.3.)53
b(Licenses)22 b(and)i(Ac)n(kno)o(wledgements)g(f)n(or)e(Incor)r(por)o
(ated)i(Softw)o(are)1424 b(91)p eop end
%%Page: 92 98
TeXDict begin 92 97 bop 460 174 a Fj(The)45 b(XML-RPC)e(client)h
(interface)g(is)281 357 y(Copyright)f(\(c\))i(1999-2002)e(by)i(Secret)e
(Labs)h(AB)281 448 y(Copyright)f(\(c\))i(1999-2002)e(by)i(Fredrik)e
(Lundh)281 631 y(By)i(obtaining,)e(using,)h(and/or)f(copying)h(this)g
(software)g(and/or)f(its)281 722 y(associated)g(documentation,)g(you)h
(agree)g(that)g(you)g(have)h(read,)f(understood,)281
814 y(and)g(will)h(comply)e(with)i(the)f(following)f(terms)h(and)g
(conditions:)281 996 y(Permission)f(to)i(use,)f(copy,)g(modify,)g(and)g
(distribute)f(this)h(software)g(and)281 1088 y(its)g(associated)g
(documentation)e(for)j(any)f(purpose)f(and)i(without)e(fee)i(is)281
1179 y(hereby)f(granted,)f(provided)h(that)g(the)g(above)g(copyright)g
(notice)f(appears)h(in)281 1270 y(all)g(copies,)g(and)g(that)h(both)f
(that)g(copyright)f(notice)h(and)g(this)g(permission)281
1362 y(notice)g(appear)g(in)g(supporting)f(documentation,)g(and)h(that)
g(the)h(name)f(of)281 1453 y(Secret)g(Labs)g(AB)h(or)f(the)g(author)g
(not)h(be)f(used)g(in)h(advertising)e(or)h(publicity)281
1544 y(pertaining)f(to)i(distribution)e(of)h(the)g(software)g(without)f
(specific,)h(written)281 1636 y(prior)g(permission.)281
1818 y(SECRET)g(LABS)g(AB)h(AND)f(THE)g(AUTHOR)g(DISCLAIMS)f(ALL)i
(WARRANTIES)e(WITH)h(REGARD)281 1910 y(TO)h(THIS)f(SOFTWARE,)f
(INCLUDING)g(ALL)i(IMPLIED)e(WARRANTIES)g(OF)i(MERCHANT-)281
2001 y(ABILITY)f(AND)g(FITNESS.)88 b(IN)45 b(NO)f(EVENT)g(SHALL)g
(SECRET)g(LABS)g(AB)h(OR)f(THE)h(AUTHOR)281 2092 y(BE)g(LIABLE)e(FOR)i
(ANY)f(SPECIAL,)g(INDIRECT)f(OR)h(CONSEQUENTIAL)f(DAMAGES)h(OR)g(ANY)
281 2183 y(DAMAGES)g(WHATSOEVER)f(RESULTING)g(FROM)h(LOSS)g(OF)h(USE,)f
(DATA)g(OR)h(PROFITS,)281 2275 y(WHETHER)f(IN)g(AN)h(ACTION)f(OF)g
(CONTRACT,)f(NEGLIGENCE)g(OR)i(OTHER)f(TORTIOUS)281 2366
y(ACTION,)g(ARISING)f(OUT)i(OF)f(OR)h(IN)f(CONNECTION)f(WITH)h(THE)h
(USE)f(OR)h(PERFORMANCE)281 2457 y(OF)g(THIS)f(SOFTWARE.)p
0 5549 3901 4 v 0 5649 a Fo(92)2584 b(Appendix)25 b(B)n(.)47
b(Histor)r(y)22 b(and)i(License)p eop end
%%Trailer
userdict /end-hook known{end-hook}if
%%EOF