home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 2 / 2799 < prev    next >
Encoding:
Internet Message Format  |  1991-02-20  |  60.7 KB

  1. From: guido@cwi.nl (Guido van Rossum)
  2. Newsgroups: alt.sources
  3. Subject: Python 0.9.1 part 07/21
  4. Message-ID: <2969@charon.cwi.nl>
  5. Date: 19 Feb 91 17:41:43 GMT
  6.  
  7. : This is a shell archive.
  8. : Extract with 'sh this_file'.
  9. :
  10. : Extract part 01 first since it makes all directories
  11. echo 'Start of pack.out, part 07 out of 21:'
  12. if test -s 'demo/sgi/gl/mclock.py'
  13. then echo '*** I will not over-write existing file demo/sgi/gl/mclock.py'
  14. else
  15. echo 'x - demo/sgi/gl/mclock.py'
  16. sed 's/^X//' > 'demo/sgi/gl/mclock.py' << 'EOF'
  17. X#! /ufs/guido/bin/sgi/python
  18. X
  19. X#############################################################################
  20. X# NOTA BENE: Before installing, fix TZDIFF to reflect your local time zone! #
  21. X#############################################################################
  22. X
  23. X# "M Clock"
  24. X#
  25. X# An implementation in software of an original design by Rob Juda.
  26. X# Clock implementation: Guido van Rossum.
  27. X# Alarm and Gong features: Sape Mullender.
  28. X#
  29. X# XXX TO DO:
  30. X# find out local time zone difference automatically
  31. X# add a date indicator
  32. X# allow multiple alarms
  33. X# allow the menu to change more parameters
  34. X
  35. Ximport sys
  36. X
  37. Xfrom gl import *
  38. Xfrom GL import *
  39. Xfrom DEVICE import *
  40. Ximport time
  41. Ximport getopt
  42. Ximport string
  43. Ximport path
  44. Xfrom math import pi
  45. Ximport math
  46. Ximport posix
  47. X
  48. XFULLC = 3600        # Full circle in 1/10-ths of a degree
  49. XMIDN = 900        # Angle of the 12 o'clock position
  50. XR, G, B = 0, 1, 2    # Indices of colors in RGB list
  51. X
  52. XHOUR = 3600        # Number of seconds per hour
  53. XMINUTE = 60        # Number of seconds per minute
  54. X
  55. Xclass struct(): pass    # Class to define featureless structures
  56. XGl = struct()        # Object to hold writable global variables
  57. X
  58. X# Default constants (used in multiple places)
  59. X
  60. XSCREENBG = 127, 156, 191
  61. XNPARTS = 9
  62. XTITLE = 'M Clock'
  63. XTZDIFF = -1*HOUR    # <--- change this to reflect your local time zone
  64. X
  65. X# Default parameters
  66. X
  67. XGl.foreground = 0    # If set, run in the foreground
  68. XGl.fullscreen = 0    # If set, run on full screen
  69. XGl.tzdiff = TZDIFF    # Seconds west of Greenwich (winter time)
  70. XGl.nparts = NPARTS    # Number of parts each circle is divided in (>= 2)
  71. XGl.debug = 0        # If set, print debug output
  72. XGl.doublebuffer = 1    # If set, use double buffering
  73. XGl.update = 0        # Update interval; seconds hand is suppressed if > 1
  74. XGl.colorsubset = 0    # If set, display only a subset of the colors
  75. XGl.cyan = 0        # If set, display cyan overlay (big hand)
  76. XGl.magenta = 0        # If set, display magenta overlay (little hand)
  77. XGl.yellow = 0        # If set, display yellow overlay (fixed background)
  78. XGl.black = 0        # If set, display black overlay (hands)
  79. XGl.colormap = 0        # If set, use colormap mode instead of RGB mode
  80. XGl.warnings = 0        # If set, print warnings
  81. XGl.title = '- - '    # Window title (default set later)
  82. XGl.border = 1        # If set, use a window border (and title)
  83. XGl.bg = 0, 0, 0        # Background color R, G, B value
  84. XGl.iconic = 0        # Set in iconic state
  85. XGl.fg = 255, 0, 0    # Alarm background RGB (either normal or alarm)
  86. XGl.ox,Gl.oy = 0,0    # Window origin
  87. XGl.cx,Gl.cy = 0,0    # Window size
  88. XGl.alarm_set = 0    # Alarm on or off
  89. XGl.alarm_on = 0        # Alarm is ringing
  90. XGl.alarm_time = 0    # Alarm time in seconds after midnight
  91. XGl.alarm_hours = 0    # Alarm hour setting, 24 hour clock
  92. XGl.alarm_minutes = 0    # Alarm minutes setting
  93. XGl.alarm_rgb = 0,0,0    # Alarm display RGB colors
  94. XGl.alarm_cmd = ''    # Command to execute when alarm goes off
  95. XGl.mouse2down = 0    # Mouse button state
  96. XGl.mouse3down = 0    # Mouse button state
  97. XGl.gong_cmd = ''    # Command to execute when chimes go off
  98. XGl.gong_int = 3600    # Gong interval
  99. XGl.indices = R, G, B    # Colors (permuted when alarm is on)
  100. X
  101. Xdef main():
  102. X    #
  103. X    sys.stdout = sys.stderr        # All output is errors/warnings etc.
  104. X    #
  105. X    try:
  106. X        args = getoptions()
  107. X    except string.atoi_error, value:
  108. X        usage(string.atoi_error, value)
  109. X    except getopt.error, msg:
  110. X        usage(getopt.error, msg)
  111. X    #
  112. X    if args:
  113. X        realtime = 0
  114. X        hours = string.atoi(args[0])
  115. X        minutes = seconds = 0
  116. X        if args[1:]: minutes = string.atoi(args[1])
  117. X        if args[2:]: seconds = string.atoi(args[2])
  118. X        localtime = ((hours*60)+minutes)*60+seconds
  119. X    else:
  120. X        realtime = 1
  121. X    #
  122. X    if Gl.title = '- - ':
  123. X        if realtime:
  124. X            Gl.title = TITLE
  125. X        else:
  126. X            title = ''
  127. X            for arg in args: title = title + ' ' + arg
  128. X            Gl.title = title[1:]
  129. X            del title
  130. X    #
  131. X    wid = makewindow()
  132. X    Gl.ox,Gl.oy = getorigin()
  133. X    Gl.cx,Gl.cy = getsize()
  134. X    initmenu()
  135. X    clearall()
  136. X    #
  137. X    if not Gl.update:
  138. X        Gl.update = 60
  139. X    #
  140. X    if Gl.update <= 1:
  141. X        Gl.timernoise = 6
  142. X    else:
  143. X        Gl.timernoise = 60
  144. X    noise(TIMER0, Gl.timernoise)
  145. X    #
  146. X    qdevice(WINSHUT)
  147. X    qdevice(WINQUIT)
  148. X    qdevice(ESCKEY)
  149. X    if realtime:
  150. X        qdevice(TIMER0)
  151. X    qdevice(REDRAW)
  152. X    qdevice(WINFREEZE)
  153. X    qdevice(WINTHAW)
  154. X    qdevice(MENUBUTTON)    # MOUSE1
  155. X    qdevice(MOUSE3)        # Left button
  156. X    qdevice(MOUSE2)        # Middle button
  157. X    unqdevice(INPUTCHANGE)
  158. X    #
  159. X    lasttime = 0
  160. X    Gl.change = 1
  161. X    while 1:
  162. X        if realtime:
  163. X            localtime = time.time() - Gl.tzdiff
  164. X        if Gl.alarm_set:
  165. X            if localtime%(24*HOUR) = Gl.alarm_time:
  166. X                # Ring the alarm!
  167. X                if Gl.debug:
  168. X                    print 'Rrrringg!'
  169. X                Gl.alarm_on = 1
  170. X                if Gl.alarm_cmd <> '':
  171. X                    d = posix.system(Gl.alarm_cmd+' '+`Gl.alarm_time/3600`+' '+`(Gl.alarm_time/60)%60` + ' &')
  172. X                Gl.change = 1
  173. X                clearall()
  174. X        if Gl.alarm_on:
  175. X            if (localtime - Gl.alarm_time) % (24*HOUR) > 300:
  176. X                # More than 5 minutes away from alarm
  177. X                Gl.alarm_on = 0
  178. X                if Gl.debug:
  179. X                    print 'Alarm turned off'
  180. X                Gl.change = 1
  181. X                clearall()
  182. X                Gl.indices = R, G, B
  183. X            else:
  184. X                if localtime % 2 = 0:
  185. X                  # Permute color indices
  186. X                  Gl.indices = Gl.indices[2:] + Gl.indices[:2]
  187. X                  Gl.change = 1
  188. X        if Gl.gong_cmd <> '' and localtime%Gl.gong_int = 0:
  189. X            d = posix.system(Gl.gong_cmd+' '+`(localtime/3600)%24`+' '+`(localtime/60)%60` + ' &')
  190. X        if localtime/Gl.update <> lasttime/Gl.update:
  191. X            if Gl.debug: print 'new time'
  192. X            Gl.change = 1
  193. X        if Gl.change:
  194. X            if Gl.debug: print 'drawing'
  195. X            doit(localtime)
  196. X            lasttime = localtime
  197. X            Gl.change = 0
  198. X        dev, data = qread()
  199. X        if Gl.debug and dev <> TIMER0:
  200. X            print dev, data
  201. X        if dev = TIMER0:
  202. X            if Gl.debug > 1:
  203. X                print dev, data
  204. X        elif dev = MOUSE3:
  205. X            mousex = getvaluator(MOUSEX)
  206. X            mousey = getvaluator(MOUSEY)
  207. X            if mouseclick(3, data, mousex, mousey):
  208. X                Gl.change = 1
  209. X        elif dev = MOUSE2:
  210. X            mousex = getvaluator(MOUSEX)
  211. X            mousey = getvaluator(MOUSEY)
  212. X            if mouseclick(2, data, mousex, mousey):
  213. X                Gl.change = 1
  214. X        elif dev = MOUSEX:
  215. X            mousex = data
  216. X            if Gl.mouse2down:
  217. X                mouse2track(mousex, mousey)
  218. X            if Gl.mouse3down:
  219. X                mouse3track(mousex, mousey)
  220. X        elif dev = MOUSEY:
  221. X            mousey = data
  222. X            if Gl.mouse2down:
  223. X                mouse2track(mousex, mousey)
  224. X            if Gl.mouse3down:
  225. X                mouse3track(mousex, mousey)
  226. X        elif dev = REDRAW or dev = REDRAWICONIC:
  227. X            if Gl.debug:
  228. X                if dev = REDRAW: print 'REDRAW'
  229. X                else: print 'REDRAWICONIC'
  230. X            reshapeviewport()
  231. X            Gl.ox,Gl.oy = getorigin()
  232. X            Gl.cx,Gl.cy = getsize()
  233. X            Gl.change = 1
  234. X            clearall()
  235. X        elif dev = MENUBUTTON:
  236. X            if Gl.debug: print 'MENUBUTTON'
  237. X            handlemenu()
  238. X        elif dev = WINFREEZE:
  239. X            if Gl.debug: print 'WINFREEZE'
  240. X            Gl.iconic = 1
  241. X            noise(TIMER0, 60*60) # Redraw every 60 seconds only
  242. X        elif dev = WINTHAW:
  243. X            if Gl.debug: print 'WINTHAW'
  244. X            Gl.iconic = 0
  245. X            noise(TIMER0, Gl.timernoise)
  246. X            Gl.change = 1
  247. X        elif dev = ESCKEY or dev = WINSHUT or dev = WINQUIT:
  248. X            if Gl.debug: print 'Exit'
  249. X            sys.exit(0)
  250. X
  251. Xdef getoptions():
  252. X    optlist, args = getopt.getopt(sys.argv[1:], 'A:a:B:bc:dFfG:g:n:sT:t:u:wCMYK')
  253. X    for optname, optarg in optlist:
  254. X        if optname = '-A':
  255. X            Gl.fg = eval(optarg)    # Should be (r,g,b)
  256. X        elif optname = '-a':
  257. X            Gl.alarm_cmd = optarg
  258. X        elif optname = '-B':
  259. X            Gl.bg = eval(optarg)    # Should be (r,g,b)
  260. X        elif optname = '-b':
  261. X            Gl.border = 0
  262. X        elif optname = '-c':
  263. X            Gl.colormap = string.atoi(optarg)
  264. X        elif optname = '-d':
  265. X            Gl.debug = Gl.debug + 1
  266. X            Gl.warnings = 1
  267. X        elif optname = '-F':
  268. X            Gl.foreground = 1
  269. X        elif optname = '-f':
  270. X            Gl.fullscreen = 1
  271. X        elif optname = '-G':
  272. X            Gl.gong_int = 60*string.atoi(optarg)
  273. X        elif optname = '-g':
  274. X            Gl.gong_cmd = optarg
  275. X        elif optname = '-n':
  276. X            Gl.nparts = string.atoi(optarg)
  277. X        elif optname = '-s':
  278. X            Gl.doublebuffer = 0
  279. X        elif optname = '-T':
  280. X            Gl.title = optarg
  281. X        elif optname = '-t':
  282. X            Gl.tzdiff = string.atoi(optarg)
  283. X        elif optname = '-u':
  284. X            Gl.update = string.atoi(optarg)
  285. X        elif optname = '-w':
  286. X            Gl.warnings = 1
  287. X        elif optname = '-C':
  288. X            Gl.cyan = Gl.colorsubset = 1
  289. X        elif optname = '-M':
  290. X            Gl.magenta = Gl.colorsubset = 1
  291. X        elif optname = '-Y':
  292. X            Gl.yellow = Gl.colorsubset = 1
  293. X        elif optname = '-K':
  294. X            Gl.black = Gl.colorsubset = 1
  295. X        else:
  296. X            print 'Unsupported option', optname
  297. X    return args
  298. X
  299. Xdef usage(exc, msg):
  300. X    if sys.argv:
  301. X        progname = path.basename(sys.argv[0])
  302. X    else:
  303. X        progname = 'mclock'
  304. X    #
  305. X    print progname + ':',
  306. X    if exc = string.atoi_error:
  307. X        print 'non-numeric argument:',
  308. X    print msg
  309. X    #
  310. X    print 'usage:', progname, '[options] [hh [mm [ss]]]'
  311. X    #
  312. X    print '-A r,g,b  : alarm background red,green,blue [255,0,0]'
  313. X    print '-a cmd    : shell command executed when alarm goes off'
  314. X    print '-B r,g,b  : background red,green,blue [0,0,0]'
  315. X    print '            (-B SCREENBG uses the default screen background)'
  316. X    print '-b        : suppress window border and title'
  317. X    print '-c cmapid : select explicit colormap'
  318. X    print '-d        : more debug output (implies -F, -w)'
  319. X    print '-F        : run in foreground'
  320. X    print '-f        : use full screen'
  321. X    print '-G intrvl : interval between chimes in minutes [60]'
  322. X    print '-g cmd    : shell command executed when chimes go off'
  323. X    print '-s        : single buffer mode'
  324. X    print '-w        : print various warnings'
  325. X    print '-n nparts : number of parts [' + `NPARTS` + ']'
  326. X    print '-T title  : alternate window title [\'' + TITLE + '\']'
  327. X    print '-t tzdiff : time zone difference [' + `TZDIFF` + ']'
  328. X    print '-u update : update interval [60]'
  329. X    print '-CMYK     : Cyan, Magenta, Yellow or blacK overlay only'
  330. X    print 'if hh [mm [ss]] is specified, display that time statically'
  331. X    print 'on machines with < 12 bitplanes, -c and -s are forced on'
  332. X    #
  333. X    sys.exit(2)
  334. X
  335. Xdef doit(localtime):
  336. X    hands = makehands(localtime)
  337. X    list = makelist(hands)
  338. X    render(list, hands)
  339. X
  340. Xdef makehands(localtime):
  341. X    localtime = localtime % (12*HOUR)
  342. X    seconds_hand = MIDN + FULLC - (localtime*60) % FULLC
  343. X    big_hand = (MIDN + FULLC - (localtime%HOUR)) % FULLC
  344. X    little_hand = (MIDN + FULLC - ((localtime/12) % HOUR)) % FULLC
  345. X    return little_hand, big_hand, seconds_hand
  346. X
  347. Xdef makelist(little_hand, big_hand, seconds_hand):
  348. X    total = []
  349. X    if Gl.cyan or not Gl.colorsubset:
  350. X        total = total + makesublist(big_hand, Gl.indices[0])
  351. X    if Gl.magenta or not Gl.colorsubset:
  352. X        total = total + makesublist(little_hand, Gl.indices[1])
  353. X    if Gl.yellow or not Gl.colorsubset:
  354. X        total = total + makesublist(MIDN, Gl.indices[2])
  355. X    total.sort()
  356. X    return total
  357. X
  358. Xdef makesublist(first, icolor):
  359. X    list = []
  360. X    alpha = FULLC/Gl.nparts
  361. X    a = first - alpha/2
  362. X    for i in range(Gl.nparts):
  363. X        angle = (a + i*alpha + FULLC) % FULLC
  364. X        value = 255*(Gl.nparts-1-i)/(Gl.nparts-1)
  365. X        list.append(angle, icolor, value)
  366. X    list.sort()
  367. X    a, icolor, value = list[0]
  368. X    if a <> 0:
  369. X        a, icolor, value = list[len(list)-1]
  370. X        t = 0, icolor, value
  371. X        list.insert(0, t)
  372. X    return list
  373. X
  374. Xdef rgb_fg():
  375. X    return Gl.fg
  376. X    # Obsolete code:
  377. X    if Gl.alarm_on:
  378. X        return Gl.bg
  379. X    else:
  380. X        return Gl.fg
  381. X
  382. Xdef rgb_bg():
  383. X    return Gl.bg
  384. X    # Obsolete code:
  385. X    if Gl.alarm_on:
  386. X        return Gl.fg
  387. X    else:
  388. X        return Gl.bg
  389. X
  390. Xdef clearall():
  391. X    Gl.c3i(rgb_bg())
  392. X    clear()
  393. X    if Gl.doublebuffer:
  394. X        swapbuffers()
  395. X        clear()
  396. X
  397. Xdef draw_alarm(color):
  398. X    frontbuffer(TRUE)
  399. X    Gl.c3i(color)
  400. X    pushmatrix()
  401. X    rotate(-((Gl.alarm_time/12)%3600), 'z')
  402. X    bgnpolygon()
  403. X    v2f( 0.00,1.00)
  404. X    v2f( 0.04,1.05)
  405. X    v2f(-0.04,1.05)
  406. X    endpolygon()
  407. X    popmatrix()
  408. X    #
  409. X    pushmatrix()
  410. X    rotate(-((Gl.alarm_time)%3600), 'z')
  411. X    bgnpolygon()
  412. X    v2f( 0.00,1.05)
  413. X    v2f( 0.07,1.10)
  414. X    v2f(-0.07,1.10)
  415. X    endpolygon()
  416. X    popmatrix()
  417. X    #
  418. X    cmov2(-1.06, -1.06)
  419. X    charstr(string.rjust(`Gl.alarm_time/3600`,2))
  420. X    charstr(':')
  421. X    charstr(string.zfill((Gl.alarm_time/60)%60,2))
  422. X    frontbuffer(FALSE)
  423. X
  424. Xdef render(list, (little_hand, big_hand, seconds_hand)):
  425. X    #
  426. X    if Gl.colormap:
  427. X        resetindex()
  428. X    #
  429. X    if not list:
  430. X        Gl.c3i(255, 255, 255) # White
  431. X        circf(0.0, 0.0, 1.0)
  432. X    else:
  433. X        list.append(3600, 0, 255) # Sentinel
  434. X    #
  435. X    rgb = [255, 255, 255]
  436. X    a_prev = 0
  437. X    for a, icolor, value in list:
  438. X        if a <> a_prev:
  439. X            [r, g, b] = rgb
  440. X            if Gl.debug > 1:
  441. X                print rgb, a_prev, a
  442. X            Gl.c3i(r, g, b)
  443. X            arcf(0.0, 0.0, 1.0, a_prev, a)
  444. X        rgb[icolor] = value
  445. X        a_prev = a
  446. X    #
  447. X    if Gl.black or not Gl.colorsubset:
  448. X        #
  449. X        # Draw the hands -- in black
  450. X        #
  451. X        Gl.c3i(0, 0, 0)
  452. X        #
  453. X        if Gl.update = 1 and not Gl.iconic:
  454. X            # Seconds hand is only drawn if we update every second
  455. X            pushmatrix()
  456. X            rotate(seconds_hand, 'z')
  457. X            bgnline()
  458. X            v2f(0.0, 0.0)
  459. X            v2f(1.0, 0.0)
  460. X            endline()
  461. X            popmatrix()
  462. X        #
  463. X        pushmatrix()
  464. X        rotate(big_hand, 'z')
  465. X        rectf(0.0, -0.01, 0.97, 0.01)
  466. X        circf(0.0, 0.0, 0.01)
  467. X        circf(0.97, 0.0, 0.01)
  468. X        popmatrix()
  469. X        #
  470. X        pushmatrix()
  471. X        rotate(little_hand, 'z')
  472. X        rectf(0.04, -0.02, 0.63, 0.02)
  473. X        circf(0.04, 0.0, 0.02)
  474. X        circf(0.63, 0.0, 0.02)
  475. X        popmatrix()
  476. X        #
  477. X        # Draw the alarm time, if set or being set
  478. X        #
  479. X        if Gl.alarm_set:
  480. X            draw_alarm(rgb_fg())
  481. X    #
  482. X    if Gl.doublebuffer: swapbuffers()
  483. X
  484. Xdef makewindow():
  485. X    #
  486. X    if Gl.debug or Gl.foreground:
  487. X        foreground()
  488. X    #
  489. X    if Gl.fullscreen:
  490. X        # XXX Should find out true screen size using getgdesc()
  491. X        prefposition(0, 1279, 0, 1023)
  492. X    else:
  493. X        keepaspect(1, 1)
  494. X        minsize(64, 64)
  495. X    #
  496. X    if not Gl.border:
  497. X        noborder()
  498. X    wid = winopen(Gl.title)
  499. X    #
  500. X    if not Gl.fullscreen:
  501. X        keepaspect(1, 1)
  502. X        minsize(10, 10)
  503. X        maxsize(2000, 2000)
  504. X        iconsize(66, 66)
  505. X        winconstraints()
  506. X    #
  507. X    nplanes = getplanes()
  508. X    nmaps = getgdesc(GD_NMMAPS)
  509. X    if Gl.warnings:
  510. X        print nplanes, 'color planes,', nmaps, 'color maps'
  511. X    #
  512. X    if nplanes < 12 or Gl.colormap:
  513. X        if not Gl.colormap:
  514. X            Gl.colormap = nmaps - 1
  515. X            if Gl.warnings:
  516. X                print 'not enough color planes available',
  517. X                print 'for RGB mode; forcing colormap mode'
  518. X                print 'using color map number', Gl.colormap
  519. X        if not Gl.colorsubset:
  520. X            needed = 3
  521. X        else:
  522. X            needed = Gl.cyan + Gl.magenta + Gl.yellow
  523. X        needed = needed*Gl.nparts
  524. X        if Gl.bg <> (0, 0, 0):
  525. X            needed = needed+1
  526. X        if Gl.fg <> (0, 0, 0):
  527. X            needed = needed+1
  528. X        if Gl.doublebuffer:
  529. X            if needed > available(nplanes/2):
  530. X                Gl.doublebuffer = 0
  531. X                if Gl.warnings:
  532. X                    print 'not enough colors available',
  533. X                    print 'for double buffer mode;',
  534. X                    print 'forcing single buffer mode'
  535. X            else:
  536. X                nplanes = nplanes/2
  537. X        if needed > available(nplanes):
  538. X            # Do this warning always
  539. X            print 'still not enough colors available;',
  540. X            print 'parts will be left white'
  541. X            print '(needed', needed, 'but have only',
  542. X            print available(nplanes), 'colors available)'
  543. X    #
  544. X    if Gl.doublebuffer:
  545. X        doublebuffer()
  546. X        gconfig()
  547. X    #
  548. X    if Gl.colormap:
  549. X        Gl.c3i = pseudo_c3i
  550. X        fixcolormap()
  551. X    else:
  552. X        Gl.c3i = c3i
  553. X        RGBmode()
  554. X        gconfig()
  555. X    #
  556. X    if Gl.fullscreen:
  557. X        # XXX Should find out true screen size using getgdesc()
  558. X        ortho2(-1.1*1.280, 1.1*1.280, -1.1*1.024, 1.1*1.024)
  559. X    else:
  560. X        ortho2(-1.1, 1.1, -1.1, 1.1)
  561. X    #
  562. X    return wid
  563. X
  564. Xdef available(nplanes):
  565. X    return pow(2, nplanes) - 1    # Reserve one pixel for black
  566. X
  567. Xdef fixcolormap():
  568. X    multimap()
  569. X    gconfig()
  570. X    nplanes = getplanes()
  571. X    if Gl.warnings:
  572. X        print 'multimap mode has', nplanes, 'color planes'
  573. X    imap = Gl.colormap
  574. X    Gl.startindex = pow(2, nplanes) - 1
  575. X    Gl.stopindex = 1
  576. X    setmap(imap)
  577. X    mapcolor(0, 0, 0, 0) # Fixed entry for black
  578. X    if Gl.bg <> (0, 0, 0):
  579. X        r, g, b = Gl.bg
  580. X        mapcolor(1, r, g, b) # Fixed entry for Gl.bg
  581. X        Gl.stopindex = 2
  582. X    if Gl.fg <> (0, 0, 0):
  583. X        r, g, b = Gl.fg
  584. X        mapcolor(2, r, g, b) # Fixed entry for Gl.fg
  585. X        Gl.stopindex = 3
  586. X    Gl.overflow_seen = 0
  587. X    resetindex()
  588. X
  589. Xdef resetindex():
  590. X    Gl.index = Gl.startindex
  591. X
  592. Xr0g0b0 = (0, 0, 0)
  593. X
  594. Xdef pseudo_c3i(rgb):
  595. X    if rgb = r0g0b0:
  596. X        index = 0
  597. X    elif rgb = Gl.bg:
  598. X        index = 1
  599. X    elif rgb = Gl.fg:
  600. X        index = 2
  601. X    else:
  602. X        index = definecolor(rgb)
  603. X    color(index)
  604. X
  605. Xdef definecolor(rgb):
  606. X    index = Gl.index
  607. X    if index < Gl.stopindex:
  608. X        if Gl.debug: print 'definecolor hard case', rgb
  609. X        # First see if we already have this one...
  610. X        for index in range(Gl.stopindex, Gl.startindex+1):
  611. X            if rgb = getmcolor(index):
  612. X                if Gl.debug: print 'return', index
  613. X                return index
  614. X        # Don't clobber reserverd colormap entries
  615. X        if not Gl.overflow_seen:
  616. X            # Shouldn't happen any more, hence no Gl.warnings test
  617. X            print 'mclock: out of colormap entries'
  618. X            Gl.overflow_seen = 1
  619. X        return Gl.stopindex
  620. X    r, g, b = rgb
  621. X    if Gl.debug > 1: print 'mapcolor', (index, r, g, b)
  622. X    mapcolor(index, r, g, b)
  623. X    Gl.index = index - 1
  624. X    return index
  625. X
  626. X# Compute n**i
  627. Xdef pow(n, i):
  628. X    x = 1
  629. X    for j in range(i): x = x*n
  630. X    return x
  631. X
  632. Xdef mouseclick(mouse, updown, x, y):
  633. X    if updown = 1:
  634. X        # mouse button came down, start tracking
  635. X        if Gl.debug:
  636. X            print 'mouse', mouse, 'down at', x, y
  637. X        if mouse = 2:
  638. X            Gl.mouse2down = 1
  639. X            mouse2track(x, y)
  640. X        elif mouse = 3:
  641. X            Gl.mouse3down = 1
  642. X            mouse3track(x, y)
  643. X        else:
  644. X            print 'fatal error'
  645. X        qdevice(MOUSEX)
  646. X        qdevice(MOUSEY)
  647. X        return 0
  648. X    else:
  649. X        # mouse button came up, stop tracking
  650. X        if Gl.debug:
  651. X            print 'mouse', mouse, 'up at', x, y
  652. X        unqdevice(MOUSEX)
  653. X        unqdevice(MOUSEY)
  654. X        if mouse = 2:
  655. X            mouse2track(x, y)
  656. X            Gl.mouse2down = 0
  657. X        elif mouse = 3:
  658. X            mouse3track(x, y)
  659. X            Gl.mouse3down = 0
  660. X        else:
  661. X            print 'fatal error'
  662. X        Gl.alarm_set = 1
  663. X        return 1
  664. X
  665. Xdef mouse3track(x, y):
  666. X    # first compute polar coordinates from x and y
  667. X    cx, cy = Gl.ox + Gl.cx/2, Gl.oy + Gl.cy/2
  668. X    x, y = x - cx, y - cy
  669. X    if (x, y) = (0, 0): return    # would cause an exception
  670. X    minutes = int(30.5 + 30.0*math.atan2(float(-x), float(-y))/pi)
  671. X    if minutes = 60: minutes = 0
  672. X    a,b = Gl.alarm_minutes/15, minutes/15
  673. X    if (a,b) = (0,3):
  674. X        # Moved backward through 12 o'clock:
  675. X        Gl.alarm_hours = Gl.alarm_hours - 1
  676. X        if Gl.alarm_hours < 0: Gl.alarm_hours = Gl.alarm_hours + 24
  677. X    if (a,b) = (3,0):
  678. X        # Moved forward through 12 o'clock:
  679. X        Gl.alarm_hours = Gl.alarm_hours + 1
  680. X        if Gl.alarm_hours >= 24: Gl.alarm_hours = Gl.alarm_hours - 24
  681. X    Gl.alarm_minutes = minutes
  682. X    seconds = Gl.alarm_hours * HOUR + Gl.alarm_minutes * MINUTE
  683. X    if seconds <> Gl.alarm_time:
  684. X        draw_alarm(rgb_bg())
  685. X        Gl.alarm_time = seconds
  686. X        draw_alarm(rgb_fg())
  687. X
  688. Xdef mouse2track(x, y):
  689. X    # first compute polar coordinates from x and y
  690. X    cx, cy = Gl.ox + Gl.cx/2, Gl.oy + Gl.cy/2
  691. X    x, y = x - cx, y - cy
  692. X    if (x, y) = (0, 0): return    # would cause an exception
  693. X    hours = int(6.5 - float(Gl.alarm_minutes)/60.0 + 6.0*math.atan2(float(-x), float(-y))/pi)
  694. X    if hours = 12: hours = 0
  695. X    if (Gl.alarm_hours,hours) = (0,11):
  696. X        # Moved backward through midnight:
  697. X        Gl.alarm_hours = 23
  698. X    elif (Gl.alarm_hours,hours) = (12,11):
  699. X        # Moved backward through noon:
  700. X        Gl.alarm_hours = 11
  701. X    elif (Gl.alarm_hours,hours) = (11,0):
  702. X        # Moved forward through noon:
  703. X        Gl.alarm_hours = 12
  704. X    elif (Gl.alarm_hours,hours) = (23,0):
  705. X        # Moved forward through midnight:
  706. X        Gl.alarm_hours = 0
  707. X    elif Gl.alarm_hours < 12:
  708. X        Gl.alarm_hours = hours
  709. X    else:
  710. X        Gl.alarm_hours = hours + 12
  711. X    seconds = Gl.alarm_hours * HOUR + Gl.alarm_minutes * MINUTE
  712. X    if seconds <> Gl.alarm_time:
  713. X        draw_alarm(rgb_bg())
  714. X        Gl.alarm_time = seconds
  715. X        draw_alarm(rgb_fg())
  716. X
  717. Xdef initmenu():
  718. X    Gl.pup = pup = newpup()
  719. X    addtopup(pup, 'M Clock%t|Alarm On/Off|Seconds Hand On/Off|Quit', 0)
  720. X
  721. Xdef handlemenu():
  722. X    item = dopup(Gl.pup)
  723. X    if item = 1:
  724. X        # Toggle alarm
  725. X        if Gl.alarm_set:
  726. X            Gl.alarm_set = 0
  727. X            Gl.alarm_on = 0
  728. X        else:
  729. X            Gl.alarm_set = 1
  730. X        Gl.change = 1
  731. X        clearall()
  732. X    elif item = 2:
  733. X        # Toggle Seconds Hand
  734. X        if Gl.update = 1:
  735. X            Gl.update = 60
  736. X            Gl.timernoise = 60
  737. X        else:
  738. X            Gl.update = 1
  739. X            Gl.timernoise = 6
  740. X        Gl.change = 1
  741. X    elif item = 3:
  742. X        if Gl.debug: print 'Exit'
  743. X        sys.exit(0)
  744. X
  745. Xmain()
  746. EOF
  747. chmod +x 'demo/sgi/gl/mclock.py'
  748. fi
  749. if test -s 'src/ceval.c'
  750. then echo '*** I will not over-write existing file src/ceval.c'
  751. else
  752. echo 'x - src/ceval.c'
  753. sed 's/^X//' > 'src/ceval.c' << 'EOF'
  754. X/***********************************************************
  755. XCopyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The
  756. XNetherlands.
  757. X
  758. X                        All Rights Reserved
  759. X
  760. XPermission to use, copy, modify, and distribute this software and its 
  761. Xdocumentation for any purpose and without fee is hereby granted, 
  762. Xprovided that the above copyright notice appear in all copies and that
  763. Xboth that copyright notice and this permission notice appear in 
  764. Xsupporting documentation, and that the names of Stichting Mathematisch
  765. XCentrum or CWI not be used in advertising or publicity pertaining to
  766. Xdistribution of the software without specific, written prior permission.
  767. X
  768. XSTICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
  769. XTHIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  770. XFITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
  771. XFOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  772. XWHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  773. XACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  774. XOF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  775. X
  776. X******************************************************************/
  777. X
  778. X/* Execute compiled code */
  779. X
  780. X#include "allobjects.h"
  781. X
  782. X#include "import.h"
  783. X#include "sysmodule.h"
  784. X#include "compile.h"
  785. X#include "frameobject.h"
  786. X#include "ceval.h"
  787. X#include "opcode.h"
  788. X#include "bltinmodule.h"
  789. X#include "traceback.h"
  790. X
  791. X#ifndef NDEBUG
  792. X#define TRACE
  793. X#endif
  794. X
  795. X#ifdef TRACE
  796. Xstatic int
  797. Xprtrace(v, str)
  798. X    object *v;
  799. X    char *str;
  800. X{
  801. X    printf("%s ", str);
  802. X    printobject(v, stdout, 0);
  803. X    printf("\n");
  804. X}
  805. X#endif
  806. X
  807. Xstatic frameobject *current_frame;
  808. X
  809. Xobject *
  810. Xgetlocals()
  811. X{
  812. X    if (current_frame == NULL)
  813. X        return NULL;
  814. X    else
  815. X        return current_frame->f_locals;
  816. X}
  817. X
  818. Xobject *
  819. Xgetglobals()
  820. X{
  821. X    if (current_frame == NULL)
  822. X        return NULL;
  823. X    else
  824. X        return current_frame->f_globals;
  825. X}
  826. X
  827. Xvoid
  828. Xprinttraceback(fp)
  829. X    FILE *fp;
  830. X{
  831. X    object *v = tb_fetch();
  832. X    if (v != NULL) {
  833. X        fprintf(fp, "Stack backtrace (innermost last):\n");
  834. X        tb_print(v, fp);
  835. X        DECREF(v);
  836. X    }
  837. X}
  838. X
  839. X
  840. X/* XXX Mixing "print ...," and direct file I/O on stdin/stdout
  841. X   XXX has some bad consequences.  The needspace flag should
  842. X   XXX really be part of the file object. */
  843. X
  844. Xstatic int needspace;
  845. X
  846. Xvoid
  847. Xflushline()
  848. X{
  849. X    FILE *fp = sysgetfile("stdout", stdout);
  850. X    if (needspace) {
  851. X        fprintf(fp, "\n");
  852. X        needspace = 0;
  853. X    }
  854. X}
  855. X
  856. X
  857. X/* Test a value used as condition, e.g., in a for or if statement */
  858. X
  859. Xstatic int
  860. Xtestbool(v)
  861. X    object *v;
  862. X{
  863. X    if (is_intobject(v))
  864. X        return getintvalue(v) != 0;
  865. X    if (is_floatobject(v))
  866. X        return getfloatvalue(v) != 0.0;
  867. X    if (v->ob_type->tp_as_sequence != NULL)
  868. X        return (*v->ob_type->tp_as_sequence->sq_length)(v) != 0;
  869. X    if (v->ob_type->tp_as_mapping != NULL)
  870. X        return (*v->ob_type->tp_as_mapping->mp_length)(v) != 0;
  871. X    if (v == None)
  872. X        return 0;
  873. X    /* All other objects are 'true' */
  874. X    return 1;
  875. X}
  876. X
  877. Xstatic object *
  878. Xadd(v, w)
  879. X    object *v, *w;
  880. X{
  881. X    if (v->ob_type->tp_as_number != NULL)
  882. X        v = (*v->ob_type->tp_as_number->nb_add)(v, w);
  883. X    else if (v->ob_type->tp_as_sequence != NULL)
  884. X        v = (*v->ob_type->tp_as_sequence->sq_concat)(v, w);
  885. X    else {
  886. X        err_setstr(TypeError, "+ not supported by operands");
  887. X        return NULL;
  888. X    }
  889. X    return v;
  890. X}
  891. X
  892. Xstatic object *
  893. Xsub(v, w)
  894. X    object *v, *w;
  895. X{
  896. X    if (v->ob_type->tp_as_number != NULL)
  897. X        return (*v->ob_type->tp_as_number->nb_subtract)(v, w);
  898. X    err_setstr(TypeError, "bad operand type(s) for -");
  899. X    return NULL;
  900. X}
  901. X
  902. Xstatic object *
  903. Xmul(v, w)
  904. X    object *v, *w;
  905. X{
  906. X    typeobject *tp;
  907. X    if (is_intobject(v) && w->ob_type->tp_as_sequence != NULL) {
  908. X        /* int*sequence -- swap v and w */
  909. X        object *tmp = v;
  910. X        v = w;
  911. X        w = tmp;
  912. X    }
  913. X    tp = v->ob_type;
  914. X    if (tp->tp_as_number != NULL)
  915. X        return (*tp->tp_as_number->nb_multiply)(v, w);
  916. X    if (tp->tp_as_sequence != NULL) {
  917. X        if (!is_intobject(w)) {
  918. X            err_setstr(TypeError,
  919. X                "can't multiply sequence with non-int");
  920. X            return NULL;
  921. X        }
  922. X        if (tp->tp_as_sequence->sq_repeat == NULL) {
  923. X            err_setstr(TypeError, "sequence does not support *");
  924. X            return NULL;
  925. X        }
  926. X        return (*tp->tp_as_sequence->sq_repeat)
  927. X                        (v, (int)getintvalue(w));
  928. X    }
  929. X    err_setstr(TypeError, "bad operand type(s) for *");
  930. X    return NULL;
  931. X}
  932. X
  933. Xstatic object *
  934. Xdivide(v, w)
  935. X    object *v, *w;
  936. X{
  937. X    if (v->ob_type->tp_as_number != NULL)
  938. X        return (*v->ob_type->tp_as_number->nb_divide)(v, w);
  939. X    err_setstr(TypeError, "bad operand type(s) for /");
  940. X    return NULL;
  941. X}
  942. X
  943. Xstatic object *
  944. Xrem(v, w)
  945. X    object *v, *w;
  946. X{
  947. X    if (v->ob_type->tp_as_number != NULL)
  948. X        return (*v->ob_type->tp_as_number->nb_remainder)(v, w);
  949. X    err_setstr(TypeError, "bad operand type(s) for %");
  950. X    return NULL;
  951. X}
  952. X
  953. Xstatic object *
  954. Xneg(v)
  955. X    object *v;
  956. X{
  957. X    if (v->ob_type->tp_as_number != NULL)
  958. X        return (*v->ob_type->tp_as_number->nb_negative)(v);
  959. X    err_setstr(TypeError, "bad operand type(s) for unary -");
  960. X    return NULL;
  961. X}
  962. X
  963. Xstatic object *
  964. Xpos(v)
  965. X    object *v;
  966. X{
  967. X    if (v->ob_type->tp_as_number != NULL)
  968. X        return (*v->ob_type->tp_as_number->nb_positive)(v);
  969. X    err_setstr(TypeError, "bad operand type(s) for unary +");
  970. X    return NULL;
  971. X}
  972. X
  973. Xstatic object *
  974. Xnot(v)
  975. X    object *v;
  976. X{
  977. X    int outcome = testbool(v);
  978. X    object *w = outcome == 0 ? True : False;
  979. X    INCREF(w);
  980. X    return w;
  981. X}
  982. X
  983. Xstatic object *
  984. Xcall_builtin(func, arg)
  985. X    object *func;
  986. X    object *arg;
  987. X{
  988. X    if (is_methodobject(func)) {
  989. X        method meth = getmethod(func);
  990. X        object *self = getself(func);
  991. X        return (*meth)(self, arg);
  992. X    }
  993. X    if (is_classobject(func)) {
  994. X        if (arg != NULL) {
  995. X            err_setstr(TypeError,
  996. X                "classobject() allows no arguments");
  997. X            return NULL;
  998. X        }
  999. X        return newclassmemberobject(func);
  1000. X    }
  1001. X    err_setstr(TypeError, "call of non-function");
  1002. X    return NULL;
  1003. X}
  1004. X
  1005. Xstatic object *
  1006. Xcall_function(func, arg)
  1007. X    object *func;
  1008. X    object *arg;
  1009. X{
  1010. X    object *newarg = NULL;
  1011. X    object *newlocals, *newglobals;
  1012. X    object *co, *v;
  1013. X    
  1014. X    if (is_classmethodobject(func)) {
  1015. X        object *self = classmethodgetself(func);
  1016. X        func = classmethodgetfunc(func);
  1017. X        if (arg == NULL) {
  1018. X            arg = self;
  1019. X        }
  1020. X        else {
  1021. X            newarg = newtupleobject(2);
  1022. X            if (newarg == NULL)
  1023. X                return NULL;
  1024. X            INCREF(self);
  1025. X            INCREF(arg);
  1026. X            settupleitem(newarg, 0, self);
  1027. X            settupleitem(newarg, 1, arg);
  1028. X            arg = newarg;
  1029. X        }
  1030. X    }
  1031. X    else {
  1032. X        if (!is_funcobject(func)) {
  1033. X            err_setstr(TypeError, "call of non-function");
  1034. X            return NULL;
  1035. X        }
  1036. X    }
  1037. X    
  1038. X    co = getfunccode(func);
  1039. X    if (co == NULL) {
  1040. X        XDECREF(newarg);
  1041. X        return NULL;
  1042. X    }
  1043. X    if (!is_codeobject(co)) {
  1044. X        fprintf(stderr, "XXX Bad code\n");
  1045. X        abort();
  1046. X    }
  1047. X    newlocals = newdictobject();
  1048. X    if (newlocals == NULL) {
  1049. X        XDECREF(newarg);
  1050. X        return NULL;
  1051. X    }
  1052. X    
  1053. X    newglobals = getfuncglobals(func);
  1054. X    INCREF(newglobals);
  1055. X    
  1056. X    v = eval_code((codeobject *)co, newglobals, newlocals, arg);
  1057. X    
  1058. X    DECREF(newlocals);
  1059. X    DECREF(newglobals);
  1060. X    
  1061. X    XDECREF(newarg);
  1062. X    
  1063. X    return v;
  1064. X}
  1065. X
  1066. Xstatic object *
  1067. Xapply_subscript(v, w)
  1068. X    object *v, *w;
  1069. X{
  1070. X    typeobject *tp = v->ob_type;
  1071. X    if (tp->tp_as_sequence == NULL && tp->tp_as_mapping == NULL) {
  1072. X        err_setstr(TypeError, "unsubscriptable object");
  1073. X        return NULL;
  1074. X    }
  1075. X    if (tp->tp_as_sequence != NULL) {
  1076. X        int i;
  1077. X        if (!is_intobject(w)) {
  1078. X            err_setstr(TypeError, "sequence subscript not int");
  1079. X            return NULL;
  1080. X        }
  1081. X        i = getintvalue(w);
  1082. X        return (*tp->tp_as_sequence->sq_item)(v, i);
  1083. X    }
  1084. X    return (*tp->tp_as_mapping->mp_subscript)(v, w);
  1085. X}
  1086. X
  1087. Xstatic object *
  1088. Xloop_subscript(v, w)
  1089. X    object *v, *w;
  1090. X{
  1091. X    sequence_methods *sq = v->ob_type->tp_as_sequence;
  1092. X    int i, n;
  1093. X    if (sq == NULL) {
  1094. X        err_setstr(TypeError, "loop over non-sequence");
  1095. X        return NULL;
  1096. X    }
  1097. X    i = getintvalue(w);
  1098. X    n = (*sq->sq_length)(v);
  1099. X    if (i >= n)
  1100. X        return NULL; /* End of loop */
  1101. X    return (*sq->sq_item)(v, i);
  1102. X}
  1103. X
  1104. Xstatic int
  1105. Xslice_index(v, isize, pi)
  1106. X    object *v;
  1107. X    int isize;
  1108. X    int *pi;
  1109. X{
  1110. X    if (v != NULL) {
  1111. X        if (!is_intobject(v)) {
  1112. X            err_setstr(TypeError, "slice index must be int");
  1113. X            return -1;
  1114. X        }
  1115. X        *pi = getintvalue(v);
  1116. X        if (*pi < 0)
  1117. X            *pi += isize;
  1118. X    }
  1119. X    return 0;
  1120. X}
  1121. X
  1122. Xstatic object *
  1123. Xapply_slice(u, v, w) /* return u[v:w] */
  1124. X    object *u, *v, *w;
  1125. X{
  1126. X    typeobject *tp = u->ob_type;
  1127. X    int ilow, ihigh, isize;
  1128. X    if (tp->tp_as_sequence == NULL) {
  1129. X        err_setstr(TypeError, "only sequences can be sliced");
  1130. X        return NULL;
  1131. X    }
  1132. X    ilow = 0;
  1133. X    isize = ihigh = (*tp->tp_as_sequence->sq_length)(u);
  1134. X    if (slice_index(v, isize, &ilow) != 0)
  1135. X        return NULL;
  1136. X    if (slice_index(w, isize, &ihigh) != 0)
  1137. X        return NULL;
  1138. X    return (*tp->tp_as_sequence->sq_slice)(u, ilow, ihigh);
  1139. X}
  1140. X
  1141. Xstatic int
  1142. Xassign_subscript(w, key, v) /* w[key] = v */
  1143. X    object *w;
  1144. X    object *key;
  1145. X    object *v;
  1146. X{
  1147. X    typeobject *tp = w->ob_type;
  1148. X    sequence_methods *sq;
  1149. X    mapping_methods *mp;
  1150. X    int (*func)();
  1151. X    if ((sq = tp->tp_as_sequence) != NULL &&
  1152. X            (func = sq->sq_ass_item) != NULL) {
  1153. X        if (!is_intobject(key)) {
  1154. X            err_setstr(TypeError,
  1155. X                "sequence subscript must be integer");
  1156. X            return -1;
  1157. X        }
  1158. X        else
  1159. X            return (*func)(w, (int)getintvalue(key), v);
  1160. X    }
  1161. X    else if ((mp = tp->tp_as_mapping) != NULL &&
  1162. X            (func = mp->mp_ass_subscript) != NULL) {
  1163. X        return (*func)(w, key, v);
  1164. X    }
  1165. X    else {
  1166. X        err_setstr(TypeError,
  1167. X                "can't assign to this subscripted object");
  1168. X        return -1;
  1169. X    }
  1170. X}
  1171. X
  1172. Xstatic int
  1173. Xassign_slice(u, v, w, x) /* u[v:w] = x */
  1174. X    object *u, *v, *w, *x;
  1175. X{
  1176. X    sequence_methods *sq = u->ob_type->tp_as_sequence;
  1177. X    int ilow, ihigh, isize;
  1178. X    if (sq == NULL) {
  1179. X        err_setstr(TypeError, "assign to slice of non-sequence");
  1180. X        return -1;
  1181. X    }
  1182. X    if (sq == NULL || sq->sq_ass_slice == NULL) {
  1183. X        err_setstr(TypeError, "unassignable slice");
  1184. X        return -1;
  1185. X    }
  1186. X    ilow = 0;
  1187. X    isize = ihigh = (*sq->sq_length)(u);
  1188. X    if (slice_index(v, isize, &ilow) != 0)
  1189. X        return -1;
  1190. X    if (slice_index(w, isize, &ihigh) != 0)
  1191. X        return -1;
  1192. X    return (*sq->sq_ass_slice)(u, ilow, ihigh, x);
  1193. X}
  1194. X
  1195. Xstatic int
  1196. Xcmp_exception(err, v)
  1197. X    object *err, *v;
  1198. X{
  1199. X    if (is_tupleobject(v)) {
  1200. X        int i, n;
  1201. X        n = gettuplesize(v);
  1202. X        for (i = 0; i < n; i++) {
  1203. X            if (err == gettupleitem(v, i))
  1204. X                return 1;
  1205. X        }
  1206. X        return 0;
  1207. X    }
  1208. X    return err == v;
  1209. X}
  1210. X
  1211. Xstatic int
  1212. Xcmp_member(v, w)
  1213. X    object *v, *w;
  1214. X{
  1215. X    int i, n, cmp;
  1216. X    object *x;
  1217. X    sequence_methods *sq;
  1218. X    /* Special case for char in string */
  1219. X    if (is_stringobject(w)) {
  1220. X        register char *s, *end;
  1221. X        register char c;
  1222. X        if (!is_stringobject(v) || getstringsize(v) != 1) {
  1223. X            err_setstr(TypeError,
  1224. X                "string member test needs char left operand");
  1225. X            return -1;
  1226. X        }
  1227. X        c = getstringvalue(v)[0];
  1228. X        s = getstringvalue(w);
  1229. X        end = s + getstringsize(w);
  1230. X        while (s < end) {
  1231. X            if (c == *s++)
  1232. X                return 1;
  1233. X        }
  1234. X        return 0;
  1235. X    }
  1236. X    sq = w->ob_type->tp_as_sequence;
  1237. X    if (sq == NULL) {
  1238. X        err_setstr(TypeError,
  1239. X            "'in' or 'not in' needs sequence right argument");
  1240. X        return -1;
  1241. X    }
  1242. X    n = (*sq->sq_length)(w);
  1243. X    for (i = 0; i < n; i++) {
  1244. X        x = (*sq->sq_item)(w, i);
  1245. X        cmp = cmpobject(v, x);
  1246. X        XDECREF(x);
  1247. X        if (cmp == 0)
  1248. X            return 1;
  1249. X    }
  1250. X    return 0;
  1251. X}
  1252. X
  1253. Xstatic object *
  1254. Xcmp_outcome(op, v, w)
  1255. X    enum cmp_op op;
  1256. X    register object *v;
  1257. X    register object *w;
  1258. X{
  1259. X    register int cmp;
  1260. X    register int res = 0;
  1261. X    switch (op) {
  1262. X    case IS:
  1263. X    case IS_NOT:
  1264. X        res = (v == w);
  1265. X        if (op == IS_NOT)
  1266. X            res = !res;
  1267. X        break;
  1268. X    case IN:
  1269. X    case NOT_IN:
  1270. X        res = cmp_member(v, w);
  1271. X        if (res < 0)
  1272. X            return NULL;
  1273. X        if (op == NOT_IN)
  1274. X            res = !res;
  1275. X        break;
  1276. X    case EXC_MATCH:
  1277. X        res = cmp_exception(v, w);
  1278. X        break;
  1279. X    default:
  1280. X        cmp = cmpobject(v, w);
  1281. X        switch (op) {
  1282. X        case LT: res = cmp <  0; break;
  1283. X        case LE: res = cmp <= 0; break;
  1284. X        case EQ: res = cmp == 0; break;
  1285. X        case NE: res = cmp != 0; break;
  1286. X        case GT: res = cmp >  0; break;
  1287. X        case GE: res = cmp >= 0; break;
  1288. X        /* XXX no default? (res is initialized to 0 though) */
  1289. X        }
  1290. X    }
  1291. X    v = res ? True : False;
  1292. X    INCREF(v);
  1293. X    return v;
  1294. X}
  1295. X
  1296. Xstatic int
  1297. Ximport_from(locals, v, name)
  1298. X    object *locals;
  1299. X    object *v;
  1300. X    char *name;
  1301. X{
  1302. X    object *w, *x;
  1303. X    w = getmoduledict(v);
  1304. X    if (name[0] == '*') {
  1305. X        int i;
  1306. X        int n = getdictsize(w);
  1307. X        for (i = 0; i < n; i++) {
  1308. X            name = getdictkey(w, i);
  1309. X            if (name == NULL || name[0] == '_')
  1310. X                continue;
  1311. X            x = dictlookup(w, name);
  1312. X            if (x == NULL) {
  1313. X                /* XXX can't happen? */
  1314. X                err_setstr(NameError, name);
  1315. X                return -1;
  1316. X            }
  1317. X            if (dictinsert(locals, name, x) != 0)
  1318. X                return -1;
  1319. X        }
  1320. X        return 0;
  1321. X    }
  1322. X    else {
  1323. X        x = dictlookup(w, name);
  1324. X        if (x == NULL) {
  1325. X            err_setstr(NameError, name);
  1326. X            return -1;
  1327. X        }
  1328. X        else
  1329. X            return dictinsert(locals, name, x);
  1330. X    }
  1331. X}
  1332. X
  1333. Xstatic object *
  1334. Xbuild_class(v, w)
  1335. X    object *v; /* None or tuple containing base classes */
  1336. X    object *w; /* dictionary */
  1337. X{
  1338. X    if (is_tupleobject(v)) {
  1339. X        int i;
  1340. X        for (i = gettuplesize(v); --i >= 0; ) {
  1341. X            object *x = gettupleitem(v, i);
  1342. X            if (!is_classobject(x)) {
  1343. X                err_setstr(TypeError,
  1344. X                    "base is not a class object");
  1345. X                return NULL;
  1346. X            }
  1347. X        }
  1348. X    }
  1349. X    else {
  1350. X        v = NULL;
  1351. X    }
  1352. X    if (!is_dictobject(w)) {
  1353. X        err_setstr(SystemError, "build_class with non-dictionary");
  1354. X        return NULL;
  1355. X    }
  1356. X    return newclassobject(v, w);
  1357. X}
  1358. X
  1359. X
  1360. X/* Status code for main loop (reason for stack unwind) */
  1361. X
  1362. Xenum why_code {
  1363. X        WHY_NOT,    /* No error */
  1364. X        WHY_EXCEPTION,    /* Exception occurred */
  1365. X        WHY_RERAISE,    /* Exception re-raised by 'finally' */
  1366. X        WHY_RETURN,    /* 'return' statement */
  1367. X        WHY_BREAK    /* 'break' statement */
  1368. X};
  1369. X
  1370. X/* Interpreter main loop */
  1371. X
  1372. Xobject *
  1373. Xeval_code(co, globals, locals, arg)
  1374. X    codeobject *co;
  1375. X    object *globals;
  1376. X    object *locals;
  1377. X    object *arg;
  1378. X{
  1379. X    register unsigned char *next_instr;
  1380. X    register int opcode;    /* Current opcode */
  1381. X    register int oparg;    /* Current opcode argument, if any */
  1382. X    register object **stack_pointer;
  1383. X    register enum why_code why; /* Reason for block stack unwind */
  1384. X    register int err;    /* Error status -- nonzero if error */
  1385. X    register object *x;    /* Result object -- NULL if error */
  1386. X    register object *v;    /* Temporary objects popped off stack */
  1387. X    register object *w;
  1388. X    register object *u;
  1389. X    register object *t;
  1390. X    register frameobject *f; /* Current frame */
  1391. X    int lineno;        /* Current line number */
  1392. X    object *retval;        /* Return value iff why == WHY_RETURN */
  1393. X    char *name;        /* Name used by some instructions */
  1394. X    FILE *fp;        /* Used by print operations */
  1395. X#ifdef TRACE
  1396. X    int trace = dictlookup(globals, "__trace__") != NULL;
  1397. X#endif
  1398. X
  1399. X/* Code access macros */
  1400. X
  1401. X#define GETCONST(i)    Getconst(f, i)
  1402. X#define GETNAME(i)    Getname(f, i)
  1403. X#define FIRST_INSTR()    (GETUSTRINGVALUE(f->f_code->co_code))
  1404. X#define INSTR_OFFSET()    (next_instr - FIRST_INSTR())
  1405. X#define NEXTOP()    (*next_instr++)
  1406. X#define NEXTARG()    (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
  1407. X#define JUMPTO(x)    (next_instr = FIRST_INSTR() + (x))
  1408. X#define JUMPBY(x)    (next_instr += (x))
  1409. X
  1410. X/* Stack manipulation macros */
  1411. X
  1412. X#define STACK_LEVEL()    (stack_pointer - f->f_valuestack)
  1413. X#define EMPTY()        (STACK_LEVEL() == 0)
  1414. X#define TOP()        (stack_pointer[-1])
  1415. X#define BASIC_PUSH(v)    (*stack_pointer++ = (v))
  1416. X#define BASIC_POP()    (*--stack_pointer)
  1417. X
  1418. X#ifdef TRACE
  1419. X#define PUSH(v)        (BASIC_PUSH(v), trace && prtrace(TOP(), "push"))
  1420. X#define POP()        (trace && prtrace(TOP(), "pop"), BASIC_POP())
  1421. X#else
  1422. X#define PUSH(v)        BASIC_PUSH(v)
  1423. X#define POP()        BASIC_POP()
  1424. X#endif
  1425. X
  1426. X    f = newframeobject(
  1427. X            current_frame,        /*back*/
  1428. X            co,            /*code*/
  1429. X            globals,        /*globals*/
  1430. X            locals,            /*locals*/
  1431. X            50,            /*nvalues*/
  1432. X            20);            /*nblocks*/
  1433. X    if (f == NULL)
  1434. X        return NULL;
  1435. X    
  1436. X    current_frame = f;
  1437. X    
  1438. X    next_instr = GETUSTRINGVALUE(f->f_code->co_code);
  1439. X    
  1440. X    stack_pointer = f->f_valuestack;
  1441. X    
  1442. X    if (arg != NULL) {
  1443. X        INCREF(arg);
  1444. X        PUSH(arg);
  1445. X    }
  1446. X    
  1447. X    why = WHY_NOT;
  1448. X    err = 0;
  1449. X    x = None;    /* Not a reference, just anything non-NULL */
  1450. X    lineno = -1;
  1451. X    
  1452. X    for (;;) {
  1453. X        static ticker;
  1454. X        
  1455. X        /* Do periodic things */
  1456. X        
  1457. X        if (--ticker < 0) {
  1458. X            ticker = 100;
  1459. X            if (intrcheck()) {
  1460. X                err_set(KeyboardInterrupt);
  1461. X                why = WHY_EXCEPTION;
  1462. X                tb_here(f, INSTR_OFFSET(), lineno);
  1463. X                break;
  1464. X            }
  1465. X        }
  1466. X        
  1467. X        /* Extract opcode and argument */
  1468. X        
  1469. X        opcode = NEXTOP();
  1470. X        if (HAS_ARG(opcode))
  1471. X            oparg = NEXTARG();
  1472. X
  1473. X#ifdef TRACE
  1474. X        /* Instruction tracing */
  1475. X        
  1476. X        if (trace) {
  1477. X            if (HAS_ARG(opcode)) {
  1478. X                printf("%d: %d, %d\n",
  1479. X                    (int) (INSTR_OFFSET() - 3),
  1480. X                    opcode, oparg);
  1481. X            }
  1482. X            else {
  1483. X                printf("%d: %d\n",
  1484. X                    (int) (INSTR_OFFSET() - 1), opcode);
  1485. X            }
  1486. X        }
  1487. X#endif
  1488. X
  1489. X        /* Main switch on opcode */
  1490. X        
  1491. X        switch (opcode) {
  1492. X        
  1493. X        /* BEWARE!
  1494. X           It is essential that any operation that fails sets either
  1495. X           x to NULL, err to nonzero, or why to anything but WHY_NOT,
  1496. X           and that no operation that succeeds does this! */
  1497. X        
  1498. X        /* case STOP_CODE: this is an error! */
  1499. X        
  1500. X        case POP_TOP:
  1501. X            v = POP();
  1502. X            DECREF(v);
  1503. X            break;
  1504. X        
  1505. X        case ROT_TWO:
  1506. X            v = POP();
  1507. X            w = POP();
  1508. X            PUSH(v);
  1509. X            PUSH(w);
  1510. X            break;
  1511. X        
  1512. X        case ROT_THREE:
  1513. X            v = POP();
  1514. X            w = POP();
  1515. X            x = POP();
  1516. X            PUSH(v);
  1517. X            PUSH(x);
  1518. X            PUSH(w);
  1519. X            break;
  1520. X        
  1521. X        case DUP_TOP:
  1522. X            v = TOP();
  1523. X            INCREF(v);
  1524. X            PUSH(v);
  1525. X            break;
  1526. X        
  1527. X        case UNARY_POSITIVE:
  1528. X            v = POP();
  1529. X            x = pos(v);
  1530. X            DECREF(v);
  1531. X            PUSH(x);
  1532. X            break;
  1533. X        
  1534. X        case UNARY_NEGATIVE:
  1535. X            v = POP();
  1536. X            x = neg(v);
  1537. X            DECREF(v);
  1538. X            PUSH(x);
  1539. X            break;
  1540. X        
  1541. X        case UNARY_NOT:
  1542. X            v = POP();
  1543. X            x = not(v);
  1544. X            DECREF(v);
  1545. X            PUSH(x);
  1546. X            break;
  1547. X        
  1548. X        case UNARY_CONVERT:
  1549. X            v = POP();
  1550. X            x = reprobject(v);
  1551. X            DECREF(v);
  1552. X            PUSH(x);
  1553. X            break;
  1554. X        
  1555. X        case UNARY_CALL:
  1556. X            v = POP();
  1557. X            if (is_classmethodobject(v) || is_funcobject(v))
  1558. X                x = call_function(v, (object *)NULL);
  1559. X            else
  1560. X                x = call_builtin(v, (object *)NULL);
  1561. X            DECREF(v);
  1562. X            PUSH(x);
  1563. X            break;
  1564. X        
  1565. X        case BINARY_MULTIPLY:
  1566. X            w = POP();
  1567. X            v = POP();
  1568. X            x = mul(v, w);
  1569. X            DECREF(v);
  1570. X            DECREF(w);
  1571. X            PUSH(x);
  1572. X            break;
  1573. X        
  1574. X        case BINARY_DIVIDE:
  1575. X            w = POP();
  1576. X            v = POP();
  1577. X            x = divide(v, w);
  1578. X            DECREF(v);
  1579. X            DECREF(w);
  1580. X            PUSH(x);
  1581. X            break;
  1582. X        
  1583. X        case BINARY_MODULO:
  1584. X            w = POP();
  1585. X            v = POP();
  1586. X            x = rem(v, w);
  1587. X            DECREF(v);
  1588. X            DECREF(w);
  1589. X            PUSH(x);
  1590. X            break;
  1591. X        
  1592. X        case BINARY_ADD:
  1593. X            w = POP();
  1594. X            v = POP();
  1595. X            x = add(v, w);
  1596. X            DECREF(v);
  1597. X            DECREF(w);
  1598. X            PUSH(x);
  1599. X            break;
  1600. X        
  1601. X        case BINARY_SUBTRACT:
  1602. X            w = POP();
  1603. X            v = POP();
  1604. X            x = sub(v, w);
  1605. X            DECREF(v);
  1606. X            DECREF(w);
  1607. X            PUSH(x);
  1608. X            break;
  1609. X        
  1610. X        case BINARY_SUBSCR:
  1611. X            w = POP();
  1612. X            v = POP();
  1613. X            x = apply_subscript(v, w);
  1614. X            DECREF(v);
  1615. X            DECREF(w);
  1616. X            PUSH(x);
  1617. X            break;
  1618. X        
  1619. X        case BINARY_CALL:
  1620. X            w = POP();
  1621. X            v = POP();
  1622. X            if (is_classmethodobject(v) || is_funcobject(v))
  1623. X                x = call_function(v, w);
  1624. X            else
  1625. X                x = call_builtin(v, w);
  1626. X            DECREF(v);
  1627. X            DECREF(w);
  1628. X            PUSH(x);
  1629. X            break;
  1630. X        
  1631. X        case SLICE+0:
  1632. X        case SLICE+1:
  1633. X        case SLICE+2:
  1634. X        case SLICE+3:
  1635. X            if ((opcode-SLICE) & 2)
  1636. X                w = POP();
  1637. X            else
  1638. X                w = NULL;
  1639. X            if ((opcode-SLICE) & 1)
  1640. X                v = POP();
  1641. X            else
  1642. X                v = NULL;
  1643. X            u = POP();
  1644. X            x = apply_slice(u, v, w);
  1645. X            DECREF(u);
  1646. X            XDECREF(v);
  1647. X            XDECREF(w);
  1648. X            PUSH(x);
  1649. X            break;
  1650. X        
  1651. X        case STORE_SLICE+0:
  1652. X        case STORE_SLICE+1:
  1653. X        case STORE_SLICE+2:
  1654. X        case STORE_SLICE+3:
  1655. X            if ((opcode-STORE_SLICE) & 2)
  1656. X                w = POP();
  1657. X            else
  1658. X                w = NULL;
  1659. X            if ((opcode-STORE_SLICE) & 1)
  1660. X                v = POP();
  1661. X            else
  1662. X                v = NULL;
  1663. X            u = POP();
  1664. X            t = POP();
  1665. X            err = assign_slice(u, v, w, t); /* u[v:w] = t */
  1666. X            DECREF(t);
  1667. X            DECREF(u);
  1668. X            XDECREF(v);
  1669. X            XDECREF(w);
  1670. X            break;
  1671. X        
  1672. X        case DELETE_SLICE+0:
  1673. X        case DELETE_SLICE+1:
  1674. X        case DELETE_SLICE+2:
  1675. X        case DELETE_SLICE+3:
  1676. X            if ((opcode-DELETE_SLICE) & 2)
  1677. X                w = POP();
  1678. X            else
  1679. X                w = NULL;
  1680. X            if ((opcode-DELETE_SLICE) & 1)
  1681. X                v = POP();
  1682. X            else
  1683. X                v = NULL;
  1684. X            u = POP();
  1685. X            err = assign_slice(u, v, w, (object *)NULL);
  1686. X                            /* del u[v:w] */
  1687. X            DECREF(u);
  1688. X            XDECREF(v);
  1689. X            XDECREF(w);
  1690. X            break;
  1691. X        
  1692. X        case STORE_SUBSCR:
  1693. X            w = POP();
  1694. X            v = POP();
  1695. X            u = POP();
  1696. X            /* v[w] = u */
  1697. X            err = assign_subscript(v, w, u);
  1698. X            DECREF(u);
  1699. X            DECREF(v);
  1700. X            DECREF(w);
  1701. X            break;
  1702. X        
  1703. X        case DELETE_SUBSCR:
  1704. X            w = POP();
  1705. X            v = POP();
  1706. X            /* del v[w] */
  1707. X            err = assign_subscript(v, w, (object *)NULL);
  1708. X            DECREF(v);
  1709. X            DECREF(w);
  1710. X            break;
  1711. X        
  1712. X        case PRINT_EXPR:
  1713. X            v = POP();
  1714. X            fp = sysgetfile("stdout", stdout);
  1715. X            /* Print value except if procedure result */
  1716. X            if (v != None) {
  1717. X                flushline();
  1718. X                printobject(v, fp, 0);
  1719. X                fprintf(fp, "\n");
  1720. X            }
  1721. X            DECREF(v);
  1722. X            break;
  1723. X        
  1724. X        case PRINT_ITEM:
  1725. X            v = POP();
  1726. X            fp = sysgetfile("stdout", stdout);
  1727. X            if (needspace)
  1728. X                fprintf(fp, " ");
  1729. X            if (is_stringobject(v)) {
  1730. X                char *s = getstringvalue(v);
  1731. X                int len = getstringsize(v);
  1732. X                fwrite(s, 1, len, fp);
  1733. X                if (len > 0 && s[len-1] == '\n')
  1734. X                    needspace = 0;
  1735. X                else
  1736. X                    needspace = 1;
  1737. X            }
  1738. X            else {
  1739. X                printobject(v, fp, 0);
  1740. X                needspace = 1;
  1741. X            }
  1742. X            DECREF(v);
  1743. X            break;
  1744. X        
  1745. X        case PRINT_NEWLINE:
  1746. X            fp = sysgetfile("stdout", stdout);
  1747. X            fprintf(fp, "\n");
  1748. X            needspace = 0;
  1749. X            break;
  1750. X        
  1751. X        case BREAK_LOOP:
  1752. X            why = WHY_BREAK;
  1753. X            break;
  1754. X        
  1755. X        case RAISE_EXCEPTION:
  1756. X            v = POP();
  1757. X            w = POP();
  1758. X            if (!is_stringobject(w))
  1759. X                err_setstr(TypeError,
  1760. X                    "exceptions must be strings");
  1761. X            else
  1762. X                err_setval(w, v);
  1763. X            DECREF(v);
  1764. X            DECREF(w);
  1765. X            why = WHY_EXCEPTION;
  1766. X            break;
  1767. X        
  1768. X        case LOAD_LOCALS:
  1769. X            v = f->f_locals;
  1770. X            INCREF(v);
  1771. X            PUSH(v);
  1772. X            break;
  1773. X        
  1774. X        case RETURN_VALUE:
  1775. X            retval = POP();
  1776. X            why = WHY_RETURN;
  1777. X            break;
  1778. X        
  1779. X        case REQUIRE_ARGS:
  1780. X            if (EMPTY()) {
  1781. X                err_setstr(TypeError,
  1782. X                    "function expects argument(s)");
  1783. X                why = WHY_EXCEPTION;
  1784. X            }
  1785. X            break;
  1786. X        
  1787. X        case REFUSE_ARGS:
  1788. X            if (!EMPTY()) {
  1789. X                err_setstr(TypeError,
  1790. X                    "function expects no argument(s)");
  1791. X                why = WHY_EXCEPTION;
  1792. X            }
  1793. X            break;
  1794. X        
  1795. X        case BUILD_FUNCTION:
  1796. X            v = POP();
  1797. X            x = newfuncobject(v, f->f_globals);
  1798. X            DECREF(v);
  1799. X            PUSH(x);
  1800. X            break;
  1801. X        
  1802. X        case POP_BLOCK:
  1803. X            {
  1804. X                block *b = pop_block(f);
  1805. X                while (STACK_LEVEL() > b->b_level) {
  1806. X                    v = POP();
  1807. X                    DECREF(v);
  1808. X                }
  1809. X            }
  1810. X            break;
  1811. X        
  1812. X        case END_FINALLY:
  1813. X            v = POP();
  1814. X            if (is_intobject(v)) {
  1815. X                why = (enum why_code) getintvalue(v);
  1816. X                if (why == WHY_RETURN)
  1817. X                    retval = POP();
  1818. X            }
  1819. X            else if (is_stringobject(v)) {
  1820. X                w = POP();
  1821. X                err_setval(v, w);
  1822. X                DECREF(w);
  1823. X                w = POP();
  1824. X                tb_store(w);
  1825. X                DECREF(w);
  1826. X                why = WHY_RERAISE;
  1827. X            }
  1828. X            else if (v != None) {
  1829. X                err_setstr(SystemError,
  1830. X                    "'finally' pops bad exception");
  1831. X                why = WHY_EXCEPTION;
  1832. X            }
  1833. X            DECREF(v);
  1834. X            break;
  1835. X        
  1836. X        case BUILD_CLASS:
  1837. X            w = POP();
  1838. X            v = POP();
  1839. X            x = build_class(v, w);
  1840. X            PUSH(x);
  1841. X            DECREF(v);
  1842. X            DECREF(w);
  1843. X            break;
  1844. X        
  1845. X        case STORE_NAME:
  1846. X            name = GETNAME(oparg);
  1847. X            v = POP();
  1848. X            err = dictinsert(f->f_locals, name, v);
  1849. X            DECREF(v);
  1850. X            break;
  1851. X        
  1852. X        case DELETE_NAME:
  1853. X            name = GETNAME(oparg);
  1854. X            if ((err = dictremove(f->f_locals, name)) != 0)
  1855. X                err_setstr(NameError, name);
  1856. X            break;
  1857. X        
  1858. X        case UNPACK_TUPLE:
  1859. X            v = POP();
  1860. X            if (!is_tupleobject(v)) {
  1861. X                err_setstr(TypeError, "unpack non-tuple");
  1862. X                why = WHY_EXCEPTION;
  1863. X            }
  1864. X            else if (gettuplesize(v) != oparg) {
  1865. X                err_setstr(RuntimeError,
  1866. X                    "unpack tuple of wrong size");
  1867. X                why = WHY_EXCEPTION;
  1868. X            }
  1869. X            else {
  1870. X                for (; --oparg >= 0; ) {
  1871. X                    w = gettupleitem(v, oparg);
  1872. X                    INCREF(w);
  1873. X                    PUSH(w);
  1874. X                }
  1875. X            }
  1876. X            DECREF(v);
  1877. X            break;
  1878. X        
  1879. X        case UNPACK_LIST:
  1880. X            v = POP();
  1881. X            if (!is_listobject(v)) {
  1882. X                err_setstr(TypeError, "unpack non-list");
  1883. X                why = WHY_EXCEPTION;
  1884. X            }
  1885. X            else if (getlistsize(v) != oparg) {
  1886. X                err_setstr(RuntimeError,
  1887. X                    "unpack list of wrong size");
  1888. X                why = WHY_EXCEPTION;
  1889. X            }
  1890. X            else {
  1891. X                for (; --oparg >= 0; ) {
  1892. X                    w = getlistitem(v, oparg);
  1893. X                    INCREF(w);
  1894. X                    PUSH(w);
  1895. X                }
  1896. X            }
  1897. X            DECREF(v);
  1898. X            break;
  1899. X        
  1900. X        case STORE_ATTR:
  1901. X            name = GETNAME(oparg);
  1902. X            v = POP();
  1903. X            u = POP();
  1904. X            err = setattr(v, name, u); /* v.name = u */
  1905. X            DECREF(v);
  1906. X            DECREF(u);
  1907. X            break;
  1908. X        
  1909. X        case DELETE_ATTR:
  1910. X            name = GETNAME(oparg);
  1911. X            v = POP();
  1912. X            err = setattr(v, name, (object *)NULL);
  1913. X                            /* del v.name */
  1914. X            DECREF(v);
  1915. X            break;
  1916. X        
  1917. X        case LOAD_CONST:
  1918. X            x = GETCONST(oparg);
  1919. X            INCREF(x);
  1920. X            PUSH(x);
  1921. X            break;
  1922. X        
  1923. X        case LOAD_NAME:
  1924. X            name = GETNAME(oparg);
  1925. X            x = dictlookup(f->f_locals, name);
  1926. X            if (x == NULL) {
  1927. X                x = dictlookup(f->f_globals, name);
  1928. X                if (x == NULL)
  1929. X                    x = getbuiltin(name);
  1930. X            }
  1931. X            if (x == NULL)
  1932. X                err_setstr(NameError, name);
  1933. X            else
  1934. X                INCREF(x);
  1935. X            PUSH(x);
  1936. X            break;
  1937. X        
  1938. X        case BUILD_TUPLE:
  1939. X            x = newtupleobject(oparg);
  1940. X            if (x != NULL) {
  1941. X                for (; --oparg >= 0;) {
  1942. X                    w = POP();
  1943. X                    err = settupleitem(x, oparg, w);
  1944. X                    if (err != 0)
  1945. X                        break;
  1946. X                }
  1947. X                PUSH(x);
  1948. X            }
  1949. X            break;
  1950. X        
  1951. X        case BUILD_LIST:
  1952. X            x =  newlistobject(oparg);
  1953. X            if (x != NULL) {
  1954. X                for (; --oparg >= 0;) {
  1955. X                    w = POP();
  1956. X                    err = setlistitem(x, oparg, w);
  1957. X                    if (err != 0)
  1958. X                        break;
  1959. X                }
  1960. X                PUSH(x);
  1961. X            }
  1962. X            break;
  1963. X        
  1964. X        case BUILD_MAP:
  1965. X            x = newdictobject();
  1966. X            PUSH(x);
  1967. X            break;
  1968. X        
  1969. X        case LOAD_ATTR:
  1970. X            name = GETNAME(oparg);
  1971. X            v = POP();
  1972. X            x = getattr(v, name);
  1973. X            DECREF(v);
  1974. X            PUSH(x);
  1975. X            break;
  1976. X        
  1977. X        case COMPARE_OP:
  1978. X            w = POP();
  1979. X            v = POP();
  1980. X            x = cmp_outcome((enum cmp_op)oparg, v, w);
  1981. X            DECREF(v);
  1982. X            DECREF(w);
  1983. X            PUSH(x);
  1984. X            break;
  1985. X        
  1986. X        case IMPORT_NAME:
  1987. X            name = GETNAME(oparg);
  1988. X            x = import_module(name);
  1989. X            XINCREF(x);
  1990. X            PUSH(x);
  1991. X            break;
  1992. X        
  1993. X        case IMPORT_FROM:
  1994. X            name = GETNAME(oparg);
  1995. X            v = TOP();
  1996. X            err = import_from(f->f_locals, v, name);
  1997. X            break;
  1998. X        
  1999. X        case JUMP_FORWARD:
  2000. X            JUMPBY(oparg);
  2001. X            break;
  2002. X        
  2003. X        case JUMP_IF_FALSE:
  2004. X            if (!testbool(TOP()))
  2005. X                JUMPBY(oparg);
  2006. X            break;
  2007. X        
  2008. X        case JUMP_IF_TRUE:
  2009. X            if (testbool(TOP()))
  2010. X                JUMPBY(oparg);
  2011. X            break;
  2012. X        
  2013. X        case JUMP_ABSOLUTE:
  2014. X            JUMPTO(oparg);
  2015. X            break;
  2016. X        
  2017. X        case FOR_LOOP:
  2018. X            /* for v in s: ...
  2019. X               On entry: stack contains s, i.
  2020. X               On exit: stack contains s, i+1, s[i];
  2021. X               but if loop exhausted:
  2022. X                   s, i are popped, and we jump */
  2023. X            w = POP(); /* Loop index */
  2024. X            v = POP(); /* Sequence object */
  2025. X            u = loop_subscript(v, w);
  2026. X            if (u != NULL) {
  2027. X                PUSH(v);
  2028. X                x = newintobject(getintvalue(w)+1);
  2029. X                PUSH(x);
  2030. X                DECREF(w);
  2031. X                PUSH(u);
  2032. X            }
  2033. X            else {
  2034. X                DECREF(v);
  2035. X                DECREF(w);
  2036. X                /* A NULL can mean "s exhausted"
  2037. X                   but also an error: */
  2038. X                if (err_occurred())
  2039. X                    why = WHY_EXCEPTION;
  2040. X                else
  2041. X                    JUMPBY(oparg);
  2042. X            }
  2043. X            break;
  2044. X        
  2045. X        case SETUP_LOOP:
  2046. X        case SETUP_EXCEPT:
  2047. X        case SETUP_FINALLY:
  2048. X            setup_block(f, opcode, INSTR_OFFSET() + oparg,
  2049. X                        STACK_LEVEL());
  2050. X            break;
  2051. X        
  2052. X        case SET_LINENO:
  2053. X#ifdef TRACE
  2054. X            if (trace)
  2055. X                printf("--- Line %d ---\n", oparg);
  2056. X#endif
  2057. X            lineno = oparg;
  2058. X            break;
  2059. X        
  2060. X        default:
  2061. X            fprintf(stderr,
  2062. X                "XXX lineno: %d, opcode: %d\n",
  2063. X                lineno, opcode);
  2064. X            err_setstr(SystemError, "eval_code: unknown opcode");
  2065. X            why = WHY_EXCEPTION;
  2066. X            break;
  2067. X        
  2068. X        } /* switch */
  2069. X
  2070. X        
  2071. X        /* Quickly continue if no error occurred */
  2072. X        
  2073. X        if (why == WHY_NOT) {
  2074. X            if (err == 0 && x != NULL)
  2075. X                continue; /* Normal, fast path */
  2076. X            why = WHY_EXCEPTION;
  2077. X            x = None;
  2078. X            err = 0;
  2079. X        }
  2080. X
  2081. X#ifndef NDEBUG
  2082. X        /* Double-check exception status */
  2083. X        
  2084. X        if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
  2085. X            if (!err_occurred()) {
  2086. X                fprintf(stderr, "XXX ghost error\n");
  2087. X                err_setstr(SystemError, "ghost error");
  2088. X                why = WHY_EXCEPTION;
  2089. X            }
  2090. X        }
  2091. X        else {
  2092. X            if (err_occurred()) {
  2093. X                fprintf(stderr, "XXX undetected error\n");
  2094. X                why = WHY_EXCEPTION;
  2095. X            }
  2096. X        }
  2097. X#endif
  2098. X
  2099. X        /* Log traceback info if this is a real exception */
  2100. X        
  2101. X        if (why == WHY_EXCEPTION) {
  2102. X            int lasti = INSTR_OFFSET() - 1;
  2103. X            if (HAS_ARG(opcode))
  2104. X                lasti -= 2;
  2105. X            tb_here(f, lasti, lineno);
  2106. X        }
  2107. X        
  2108. X        /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
  2109. X        
  2110. X        if (why == WHY_RERAISE)
  2111. X            why = WHY_EXCEPTION;
  2112. X
  2113. X        /* Unwind stacks if a (pseudo) exception occurred */
  2114. X        
  2115. X        while (why != WHY_NOT && f->f_iblock > 0) {
  2116. X            block *b = pop_block(f);
  2117. X            while (STACK_LEVEL() > b->b_level) {
  2118. X                v = POP();
  2119. X                XDECREF(v);
  2120. X            }
  2121. X            if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
  2122. X                why = WHY_NOT;
  2123. X                JUMPTO(b->b_handler);
  2124. X                break;
  2125. X            }
  2126. X            if (b->b_type == SETUP_FINALLY ||
  2127. X                    b->b_type == SETUP_EXCEPT &&
  2128. X                    why == WHY_EXCEPTION) {
  2129. X                if (why == WHY_EXCEPTION) {
  2130. X                    object *exc, *val;
  2131. X                    err_get(&exc, &val);
  2132. X                    if (val == NULL) {
  2133. X                        val = None;
  2134. X                        INCREF(val);
  2135. X                    }
  2136. X                    v = tb_fetch();
  2137. X                    /* Make the raw exception data
  2138. X                       available to the handler,
  2139. X                       so a program can emulate the
  2140. X                       Python main loop.  Don't do
  2141. X                       this for 'finally'. */
  2142. X                    if (b->b_type == SETUP_EXCEPT) {
  2143. X#if 0 /* Oops, this breaks too many things */
  2144. X                        sysset("exc_traceback", v);
  2145. X#endif
  2146. X                        sysset("exc_value", val);
  2147. X                        sysset("exc_type", exc);
  2148. X                        err_clear();
  2149. X                    }
  2150. X                    PUSH(v);
  2151. X                    PUSH(val);
  2152. X                    PUSH(exc);
  2153. X                }
  2154. X                else {
  2155. X                    if (why == WHY_RETURN)
  2156. X                        PUSH(retval);
  2157. X                    v = newintobject((long)why);
  2158. X                    PUSH(v);
  2159. X                }
  2160. X                why = WHY_NOT;
  2161. X                JUMPTO(b->b_handler);
  2162. X                break;
  2163. X            }
  2164. X        } /* unwind stack */
  2165. X
  2166. X        /* End the loop if we still have an error (or return) */
  2167. X        
  2168. X        if (why != WHY_NOT)
  2169. X            break;
  2170. X        
  2171. X    } /* main loop */
  2172. X    
  2173. X    /* Pop remaining stack entries */
  2174. X    
  2175. X    while (!EMPTY()) {
  2176. X        v = POP();
  2177. X        XDECREF(v);
  2178. X    }
  2179. X    
  2180. X    /* Restore previous frame and release the current one */
  2181. X    
  2182. X    current_frame = f->f_back;
  2183. X    DECREF(f);
  2184. X    
  2185. X    if (why == WHY_RETURN)
  2186. X        return retval;
  2187. X    else
  2188. X        return NULL;
  2189. X}
  2190. EOF
  2191. fi
  2192. if test -s 'src/object.h'
  2193. then echo '*** I will not over-write existing file src/object.h'
  2194. else
  2195. echo 'x - src/object.h'
  2196. sed 's/^X//' > 'src/object.h' << 'EOF'
  2197. X/***********************************************************
  2198. XCopyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The
  2199. XNetherlands.
  2200. X
  2201. X                        All Rights Reserved
  2202. X
  2203. XPermission to use, copy, modify, and distribute this software and its 
  2204. Xdocumentation for any purpose and without fee is hereby granted, 
  2205. Xprovided that the above copyright notice appear in all copies and that
  2206. Xboth that copyright notice and this permission notice appear in 
  2207. Xsupporting documentation, and that the names of Stichting Mathematisch
  2208. XCentrum or CWI not be used in advertising or publicity pertaining to
  2209. Xdistribution of the software without specific, written prior permission.
  2210. X
  2211. XSTICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
  2212. XTHIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  2213. XFITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
  2214. XFOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  2215. XWHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  2216. XACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  2217. XOF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  2218. X
  2219. X******************************************************************/
  2220. X
  2221. X#define NDEBUG
  2222. X/* Object and type object interface */
  2223. X
  2224. X/*
  2225. X123456789-123456789-123456789-123456789-123456789-123456789-123456789-12
  2226. X
  2227. XObjects are structures allocated on the heap.  Special rules apply to
  2228. Xthe use of objects to ensure they are properly garbage-collected.
  2229. XObjects are never allocated statically or on the stack; they must be
  2230. Xaccessed through special macros and functions only.  (Type objects are
  2231. Xexceptions to the first rule; the standard types are represented by
  2232. Xstatically initialized type objects.)
  2233. X
  2234. XAn object has a 'reference count' that is increased or decreased when a
  2235. Xpointer to the object is copied or deleted; when the reference count
  2236. Xreaches zero there are no references to the object left and it can be
  2237. Xremoved from the heap.
  2238. X
  2239. XAn object has a 'type' that determines what it represents and what kind
  2240. Xof data it contains.  An object's type is fixed when it is created.
  2241. XTypes themselves are represented as objects; an object contains a
  2242. Xpointer to the corresponding type object.  The type itself has a type
  2243. Xpointer pointing to the object representing the type 'type', which
  2244. Xcontains a pointer to itself!).
  2245. X
  2246. XObjects do not float around in memory; once allocated an object keeps
  2247. Xthe same size and address.  Objects that must hold variable-size data
  2248. Xcan contain pointers to variable-size parts of the object.  Not all
  2249. Xobjects of the same type have the same size; but the size cannot change
  2250. Xafter allocation.  (These restrictions are made so a reference to an
  2251. Xobject can be simply a pointer -- moving an object would require
  2252. Xupdating all the pointers, and changing an object's size would require
  2253. Xmoving it if there was another object right next to it.)
  2254. X
  2255. XObjects are always accessed through pointers of the type 'object *'.
  2256. XThe type 'object' is a structure that only contains the reference count
  2257. Xand the type pointer.  The actual memory allocated for an object
  2258. Xcontains other data that can only be accessed after casting the pointer
  2259. Xto a pointer to a longer structure type.  This longer type must start
  2260. Xwith the reference count and type fields; the macro OB_HEAD should be
  2261. Xused for this (to accomodate for future changes).  The implementation
  2262. Xof a particular object type can cast the object pointer to the proper
  2263. Xtype and back.
  2264. X
  2265. XA standard interface exists for objects that contain an array of items
  2266. Xwhose size is determined when the object is allocated.
  2267. X
  2268. X123456789-123456789-123456789-123456789-123456789-123456789-123456789-12
  2269. X*/
  2270. X
  2271. X#ifndef NDEBUG
  2272. X
  2273. X/* Turn on heavy reference debugging */
  2274. X#define TRACE_REFS
  2275. X
  2276. X/* Turn on reference counting */
  2277. X#define REF_DEBUG
  2278. X
  2279. X#endif /* NDEBUG */
  2280. X
  2281. X#ifdef TRACE_REFS
  2282. X#define OB_HEAD \
  2283. X    struct _object *_ob_next, *_ob_prev; \
  2284. X    int ob_refcnt; \
  2285. X    struct _typeobject *ob_type;
  2286. X#define OB_HEAD_INIT(type) 0, 0, 1, type,
  2287. X#else
  2288. X#define OB_HEAD \
  2289. X    unsigned int ob_refcnt; \
  2290. X    struct _typeobject *ob_type;
  2291. X#define OB_HEAD_INIT(type) 1, type,
  2292. X#endif
  2293. X
  2294. X#define OB_VARHEAD \
  2295. X    OB_HEAD \
  2296. X    unsigned int ob_size; /* Number of items in variable part */
  2297. Xtypedef struct _object {
  2298. X    OB_HEAD
  2299. X} object;
  2300. X
  2301. Xtypedef struct {
  2302. X    OB_VARHEAD
  2303. X} varobject;
  2304. X
  2305. X
  2306. X/*
  2307. X123456789-123456789-123456789-123456789-123456789-123456789-123456789-12
  2308. X
  2309. XType objects contain a string containing the type name (to help somewhat
  2310. Xin debugging), the allocation parameters (see newobj() and newvarobj()),
  2311. Xand methods for accessing objects of the type.  Methods are optional,a
  2312. Xnil pointer meaning that particular kind of access is not available for
  2313. Xthis type.  The DECREF() macro uses the tp_dealloc method without
  2314. Xchecking for a nil pointer; it should always be implemented except if
  2315. Xthe implementation can guarantee that the reference count will never
  2316. Xreach zero (e.g., for type objects).
  2317. X
  2318. XNB: the methods for certain type groups are now contained in separate
  2319. Xmethod blocks.
  2320. X*/
  2321. X
  2322. Xtypedef struct {
  2323. X    object *(*nb_add) FPROTO((object *, object *));
  2324. X    object *(*nb_subtract) FPROTO((object *, object *));
  2325. X    object *(*nb_multiply) FPROTO((object *, object *));
  2326. X    object *(*nb_divide) FPROTO((object *, object *));
  2327. X    object *(*nb_remainder) FPROTO((object *, object *));
  2328. X    object *(*nb_power) FPROTO((object *, object *));
  2329. X    object *(*nb_negative) FPROTO((object *));
  2330. X    object *(*nb_positive) FPROTO((object *));
  2331. X} number_methods;
  2332. X
  2333. Xtypedef struct {
  2334. X    int (*sq_length) FPROTO((object *));
  2335. X    object *(*sq_concat) FPROTO((object *, object *));
  2336. X    object *(*sq_repeat) FPROTO((object *, int));
  2337. X    object *(*sq_item) FPROTO((object *, int));
  2338. X    object *(*sq_slice) FPROTO((object *, int, int));
  2339. X    int (*sq_ass_item) FPROTO((object *, int, object *));
  2340. X    int (*sq_ass_slice) FPROTO((object *, int, int, object *));
  2341. X} sequence_methods;
  2342. X
  2343. Xtypedef struct {
  2344. X    int (*mp_length) FPROTO((object *));
  2345. X    object *(*mp_subscript) FPROTO((object *, object *));
  2346. X    int (*mp_ass_subscript) FPROTO((object *, object *, object *));
  2347. X} mapping_methods;
  2348. X
  2349. Xtypedef struct _typeobject {
  2350. X    OB_VARHEAD
  2351. X    char *tp_name; /* For printing */
  2352. X    unsigned int tp_basicsize, tp_itemsize; /* For allocation */
  2353. X    
  2354. X    /* Methods to implement standard operations */
  2355. X    
  2356. X    void (*tp_dealloc) FPROTO((object *));
  2357. X    void (*tp_print) FPROTO((object *, FILE *, int));
  2358. X    object *(*tp_getattr) FPROTO((object *, char *));
  2359. X    int (*tp_setattr) FPROTO((object *, char *, object *));
  2360. X    int (*tp_compare) FPROTO((object *, object *));
  2361. X    object *(*tp_repr) FPROTO((object *));
  2362. X    
  2363. X    /* Method suites for standard classes */
  2364. X    
  2365. X    number_methods *tp_as_number;
  2366. X    sequence_methods *tp_as_sequence;
  2367. X    mapping_methods *tp_as_mapping;
  2368. X} typeobject;
  2369. X
  2370. Xextern typeobject Typetype; /* The type of type objects */
  2371. X
  2372. X#define is_typeobject(op) ((op)->ob_type == &Typetype)
  2373. X
  2374. X/* Generic operations on objects */
  2375. Xextern void printobject PROTO((object *, FILE *, int));
  2376. Xextern object * reprobject PROTO((object *));
  2377. Xextern int cmpobject PROTO((object *, object *));
  2378. Xextern object *getattr PROTO((object *, char *));
  2379. Xextern int setattr PROTO((object *, char *, object *));
  2380. X
  2381. X/* Flag bits for printing: */
  2382. X#define PRINT_RAW    1    /* No string quotes etc. */
  2383. X
  2384. X/*
  2385. X123456789-123456789-123456789-123456789-123456789-123456789-123456789-12
  2386. X
  2387. XThe macros INCREF(op) and DECREF(op) are used to increment or decrement
  2388. Xreference counts.  DECREF calls the object's deallocator function; for
  2389. Xobjects that don't contain references to other objects or heap memory
  2390. Xthis can be the standard function free().  Both macros can be used
  2391. Xwhereever a void expression is allowed.  The argument shouldn't be a
  2392. XNIL pointer.  The macro NEWREF(op) is used only to initialize reference
  2393. Xcounts to 1; it is defined here for convenience.
  2394. X
  2395. XWe assume that the reference count field can never overflow; this can
  2396. Xbe proven when the size of the field is the same as the pointer size
  2397. Xbut even with a 16-bit reference count field it is pretty unlikely so
  2398. Xwe ignore the possibility.  (If you are paranoid, make it a long.)
  2399. X
  2400. XType objects should never be deallocated; the type pointer in an object
  2401. Xis not considered to be a reference to the type object, to save
  2402. Xcomplications in the deallocation function.  (This is actually a
  2403. Xdecision that's up to the implementer of each new type so if you want,
  2404. Xyou can count such references to the type object.)
  2405. X
  2406. X*** WARNING*** The DECREF macro must have a side-effect-free argument
  2407. Xsince it may evaluate its argument multiple times.  (The alternative
  2408. Xwould be to mace it a proper function or assign it to a global temporary
  2409. Xvariable first, both of which are slower; and in a multi-threaded
  2410. Xenvironment the global variable trick is not safe.)
  2411. X*/
  2412. X
  2413. X#ifdef TRACE_REFS
  2414. X#ifndef REF_DEBUG
  2415. X#define REF_DEBUG
  2416. X#endif
  2417. X#endif
  2418. X
  2419. X#ifndef TRACE_REFS
  2420. X#define DELREF(op) (*(op)->ob_type->tp_dealloc)((object *)(op))
  2421. X#define UNREF(op) /*empty*/
  2422. X#endif
  2423. X
  2424. X#ifdef REF_DEBUG
  2425. Xextern long ref_total;
  2426. X#ifndef TRACE_REFS
  2427. X#define NEWREF(op) (ref_total++, (op)->ob_refcnt = 1)
  2428. X#endif
  2429. X#define INCREF(op) (ref_total++, (op)->ob_refcnt++)
  2430. X#define DECREF(op) \
  2431. X    if (--ref_total, --(op)->ob_refcnt > 0) \
  2432. X        ; \
  2433. X    else \
  2434. X        DELREF(op)
  2435. X#else
  2436. X#define NEWREF(op) ((op)->ob_refcnt = 1)
  2437. X#define INCREF(op) ((op)->ob_refcnt++)
  2438. X#define DECREF(op) \
  2439. X    if (--(op)->ob_refcnt > 0) \
  2440. X        ; \
  2441. X    else \
  2442. X        DELREF(op)
  2443. X#endif
  2444. X
  2445. X/* Macros to use in case the object pointer may be NULL: */
  2446. X
  2447. X#define XINCREF(op) if ((op) == NULL) ; else INCREF(op)
  2448. X#define XDECREF(op) if ((op) == NULL) ; else DECREF(op)
  2449. X
  2450. X/* Definition of NULL, so you don't have to include <stdio.h> */
  2451. X
  2452. X#ifndef NULL
  2453. X#define NULL 0
  2454. X#endif
  2455. X
  2456. X
  2457. X/*
  2458. XNoObject is an object of undefined type which can be used in contexts
  2459. Xwhere NULL (nil) is not suitable (since NULL often means 'error').
  2460. X
  2461. XDon't forget to apply INCREF() when returning this value!!!
  2462. X*/
  2463. X
  2464. Xextern object NoObject; /* Don't use this directly */
  2465. X
  2466. X#define None (&NoObject)
  2467. X
  2468. X
  2469. X/*
  2470. X123456789-123456789-123456789-123456789-123456789-123456789-123456789-12
  2471. X
  2472. XMore conventions
  2473. X================
  2474. X
  2475. XArgument Checking
  2476. X-----------------
  2477. X
  2478. XFunctions that take objects as arguments normally don't check for nil
  2479. Xarguments, but they do check the type of the argument, and return an
  2480. Xerror if the function doesn't apply to the type.
  2481. X
  2482. XFailure Modes
  2483. X-------------
  2484. X
  2485. XFunctions may fail for a variety of reasons, including running out of
  2486. Xmemory.  This is communicated to the caller in two ways: an error string
  2487. Xis set (see errors.h), and the function result differs: functions that
  2488. Xnormally return a pointer return NULL for failure, functions returning
  2489. Xan integer return -1 (which could be a legal return value too!), and
  2490. Xother functions return 0 for success and -1 for failure.
  2491. XCallers should always check for errors before using the result.
  2492. X
  2493. XReference Counts
  2494. X----------------
  2495. X
  2496. XIt takes a while to get used to the proper usage of reference counts.
  2497. X
  2498. XFunctions that create an object set the reference count to 1; such new
  2499. Xobjects must be stored somewhere or destroyed again with DECREF().
  2500. XFunctions that 'store' objects such as settupleitem() and dictinsert()
  2501. Xdon't increment the reference count of the object, since the most
  2502. Xfrequent use is to store a fresh object.  Functions that 'retrieve'
  2503. Xobjects such as gettupleitem() and dictlookup() also don't increment
  2504. Xthe reference count, since most frequently the object is only looked at
  2505. Xquickly.  Thus, to retrieve an object and store it again, the caller
  2506. Xmust call INCREF() explicitly.
  2507. X
  2508. XNOTE: functions that 'consume' a reference count like dictinsert() even
  2509. Xconsume the reference if the object wasn't stored, to simplify error
  2510. Xhandling.
  2511. X
  2512. XIt seems attractive to make other functions that take an object as
  2513. Xargument consume a reference count; however this may quickly get
  2514. Xconfusing (even the current practice is already confusing).  Consider
  2515. Xit carefully, it may safe lots of calls to INCREF() and DECREF() at
  2516. Xtimes.
  2517. X
  2518. X123456789-123456789-123456789-123456789-123456789-123456789-123456789-12
  2519. X*/
  2520. EOF
  2521. fi
  2522. echo 'Part 07 out of 21 of pack.out complete.'
  2523. exit 0
  2524.