[Pythonmac-SIG] [pyobjc] NSAutorelease support

Steven D. Majewski sdm7g@minsky.med.virginia.edu
Wed, 8 Nov 2000 15:12:33 -0500 (EST)


From reading the PyObjC(5.5) ChangeLog and the Python/Misc/HISTORY files
along with the source code, it looks like some of the AutoreleasePool
support was commented out when the support in Python itself for
Objective-C disappeared:  

  //  if (!PyObjC_Active)
  //  {
  //    PyThreadState *tstate = PyThreadState_Get();
  //
  //    PyObjC_Active = 1;
  //    tstate->interp->objc_autorelease_counter =OBJC_AUTORELEASE_FACTOR;
  //    tstate->interp->objc_autorelease_pool = [[NSAutoreleasePool alloc]init];
  //  }
  
After adding static declarations for PyObjC_Active and objc_autorelease_pool 
and putting back the [[NSAutoreleaseRool alloc]init] message, I can
now get it to do the following sort of stuff without pages of 'just
leaking' error messages:    

[localhost:~/Src/PyObjC] sdm% ./pyobjc
Python 2.0 (#3, 10/22/00, 12:04:10) 
[GCC Apple DevKit-based CPP 5.0] on Darwin1.2
Type "copyright", "credits" or "license" for more information.
>>> from ObjC import *
>>> dir()       
['__builtins__', '__doc__', '__name__', 'error', 'lookup_class',
'make_pointer', 'runtime']
>>> DC = lookup_class( 'NSCalendarDate' )
>>> DC
<Objective-C `NSCalendarDate' class at 9ab89d48>
>>> date = DC()
>>> date
<Objective-C `NSCalendarDate' instance at 28c070>
>>> print date
2000-11-08 14:26:53 -0500
>>> 
>>> date.monthOfYear()
11
>>> date.dayOfMonth()
8
>>> 


I don't yet know enough about what I'm doing here to tell if that's
a reasonable fix or an ugly leaking hack, but it does make pyobjc
at least experimentally usable on OSX. 
 I could not get pyobjc calls from Python to AutoreleasePool to 
work. Without that patch, I get 

>>> from ObjC import *
>>> P = lookup_class( 'NSAutoreleasePool' )
>>> pool = P()
Nov 08 14:58:11 pyobjc[465] *** Uncaught
exception: <NSInvalidArgumentException> *** -[NSAutoreleasePool
retain]: Cannot retain an autorelease pool


or 

>>> from ObjC import *
>>> POOL = lookup_class( 'NSAutoreleasePool' )
>>> pool = POOL.alloc()
Nov 08 14:59:04 pyobjc[466] *** _NSAutoreleaseNoPool(): Object 0x27c3a0 of
class NSCFString autoreleased with no pool in place - just leaking
Nov 08 14:59:04 pyobjc[466] *** _NSAutoreleaseNoPool(): Object 0x27eea0 of
class NSInvocation autoreleased with no pool in place - just leaking
Nov 08 14:59:04 pyobjc[466] *** _NSAutoreleaseNoPool(): Object 0x27f520 of
class NSCFString autoreleased with no pool in place - just leaking
Nov 08 14:59:04 pyobjc[466] *** _NSAutoreleaseNoPool(): Object 0x27f2c0 of
class NSCFString autoreleased with no pool in place - just leaking
Nov 08 14:59:04 pyobjc[466] *** _NSAutoreleaseNoPool(): Object 0x27fb00 of
class NSException autoreleased with no pool in place - just leaking
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
ObjC.error: *** -[NSAutoreleasePool retain]: Cannot retain an autorelease
pool
>>> pool.init()
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
NameError: There is no variable named 'pool'
>>> 


BTW:  Trying to do something "visual" with AppKit, I get: 

>>> NSApp = lookup_class( 'NSApplication' )
>>> NSApp.sharedApplication()
Nov 08 15:04:25 pyobjc[471] Warning - could not find theme file in System
directory. Trying home directory.
Bus error


-- Steve Majewski <sdm7g@Virginia.EDU>