[Pythonmac-SIG] Re: [pyobjc] NSAutorelease support

Bill Bumgarner bbum@codefab.com
Wed, 8 Nov 2000 15:21:09 -0500


- autorelease pools

I would much rather see support for autorelease pools as a call to the ObjC module.

I.e.

ObjC.pushReleasePool()
ObjC.popReleasePool()

Implementation is left as an exercise to the reader-- keep in mind that you  
can't use standard ObjC collection classes because they all retain/release  
stuff!!!

The exception you are seeing makes sense-- the wrapping of the autrelease pool  
in the PyObject wrapper sends that release pool a -retain.

Actually, a better implementation would be to simply use -isKindOfClass: to  
test the object to see if it is a release pool or not and simply do NOT retain  
release pools... this would also lead to an implementation with much better  
thread support [which, btw, i have patches to make threading at least build on  
OSXS].

---

Appkit:

We will have to throw together a boostrapper that configures a mainBundle such  
that it has the appropriate property lists and such.... I think there was a  
discussion of doing wrapperless appkit apps on the osx-dev mailing list very  
recently.

b.bum



From: "Steven D. Majewski" <sdm7g@minsky.med.virginia.edu>
Date: 2000-11-08 15:12:33 -0500
To: python-list@python.org, pythonmac-sig@python.org
Subject: [pyobjc] NSAutorelease support
cc: Andrew Zeldis <azeldis@wesleyan.edu>, Bill Bumgarner <bbum@codefab.com>,
bobsavage@mac.com, jas@corpus-callosum.com, tony@metanet.com

 
>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>