[Pythonmac-SIG] dynload_next

Steven Majewski sdm7g@Virginia.EDU
Sat, 26 Jan 2002 22:24:04 -0500 (EST)


  This message is in MIME format.  The first part should be readable text,
  while the remaining parts are likely unreadable without MIME-aware tools.
  Send mail to mime@docserver.cac.washington.edu for more info.

---2138096204-1237206507-1012101844=:13414
Content-Type: TEXT/PLAIN; charset=US-ASCII



On Sat, 26 Jan 2002, Marcel Prastawa wrote:

> I've got dynload_next working with PyOpenGL!
>
> I used libdl's dlopen.c as a reference. The problem appears to be caused
> by the use of NSLookupAndBindSymbol(). I have switched to using
> NSLookupSymbolInModule() instead. I also commented out the quick return.
>
> I have attached the diff file. It is also available at:
> http://www.cs.unc.edu/~prastawa/macosx/dynload_next.diff
>
> Can any of you try it out and make sure it doesn't break anything?
> I haven't encountered any problems so far.
>

Marcel -- I kept getting error trying to apply the patch, but it
was just like one of the things I had already tried which wasn't
working for me.  Actually I tried two variations -- one the same
as yours and another also setting  NSLINKMODULE_OPTION_PRIVATE.
 version 1 didn't fix the problem for me.
 version 2 didn't work.

Did you change the link options to get it to work for you ?
( two-level-namespace ? )

(Also, I believe you used my NSLinkEditError patch, which I'm sorry to say
 was very sloppily done for debugging purposes. All those strcat()'s
 onto an existing constant string were a big mistake! )

I have, however, cleaned out some of the cruft and gotten a version
that works for me. Could a few of you folks who have had these
problems give this a try and tell me if it fixes things for you?


This one does set the PRIVATE option -- in the Python module API,
all of the exported functions go into the tables built and exported
by the init module, and the init function pointer is returned at the
end. So it really shouldn't need any global symbols to be bound
into the main program. It also seems to work for the Carbon modules.


My earlier attempt to use the PRIVATE option failed because the
second check for NSIsSymbolNameDefined() took not finding it as
a failure and bailed out. I had to cut that test out. That also
makes the first test (before the DEBUG) never pass, so the
warning doesn't even print for the 'import time, package.time'
test.

So far, this has worked for me on everything I've thrown at it.
If it works for everyone else, I'll permanenly pull out the
other non-functional code and submit it.

-- Steve Majewski


/* Support for dynamic loading of extension modules */

#include "Python.h"
#include "importdl.h"

#include <mach-o/dyld.h>

#define DEBUG 1
#define PRIVATE NSLINKMODULE_OPTION_PRIVATE
#define OTHER_OPTIONS  NSLINKMODULE_OPTION_BINDNOW|NSLINKMODULE_OPTION_RETURN_ON_ERROR
#define LINK_MODULE_OPTIONS  (PRIVATE | OTHER_OPTIONS)

const struct filedescr _PyImport_DynLoadFiletab[] = {
	{".so", "rb", C_EXTENSION},
	{"module.so", "rb", C_EXTENSION},
	{0, 0}
};

char errmessage[400];

dl_funcptr _PyImport_GetDynLoadFunc(const char *fqname, const char *shortname,
				    const char *pathname, FILE *fp)
{

	NSObjectFileImageReturnCode rc;
	NSObjectFileImage image;
	NSModule newModule;
	NSSymbol theSym;
	const char *errString;

	dl_funcptr p = NULL;
	char funcname[258];

	PyOS_snprintf(funcname, sizeof(funcname), "_init%.200s", shortname);

	if (NSIsSymbolNameDefined(funcname)) {
#ifdef DEBUG
	  fprintf(stderr,"[!warning] %s already defined.\n", funcname );
#endif
	}

	rc = NSCreateObjectFileImageFromFile(pathname, &image);
	switch(rc) {
	    default:
	    case NSObjectFileImageFailure:
	    case NSObjectFileImageFormat:
	    /* for these a message is printed on stderr by dyld */
		errString = "Can't create object file image";
		break;
	    case NSObjectFileImageSuccess:
		errString = NULL;
		break;
	    case NSObjectFileImageInappropriateFile:
		errString = "Inappropriate file type for dynamic loading";
		break;
	    case NSObjectFileImageArch:
		errString = "Wrong CPU type in object file";
		break;
	    case NSObjectFileImageAccess:
		errString = "Can't read object file (no access)";
		break;
	}
	if (errString == NULL) {
	  newModule = NSLinkModule(image, pathname,LINK_MODULE_OPTIONS);
	    if (!newModule) {
	      int errNo;
	      char *filename, *errinfo;
	      NSLinkEditErrors c;
	      errString = "Failure linking new module";
	      NSLinkEditError( &c, &errNo, &filename, &errinfo );
	      sprintf( errmessage, "%s. %s\n%s",
		       errString, filename, errinfo );
	      errString = errmessage;
	    }
	}
	if (errString != NULL) {
		PyErr_SetString(PyExc_ImportError, errString);
		return NULL;
	}
#ifdef NONONO
	if (!NSIsSymbolNameDefined(funcname)) {
		/* UnlinkModule() isn't implemented in current versions,
		   but calling it does no harm */
		NSUnLinkModule(newModule, FALSE);
		PyErr_Format(PyExc_ImportError,
			     "Loaded module does not contain symbol %.200s",
			     funcname);
		return NULL;
	}
#endif
	/*	theSym = NSLookupAndBindSymbol(funcname); */
	theSym = NSLookupSymbolInModule(newModule,funcname);
	p = (dl_funcptr)NSAddressOfSymbol(theSym);

	return p;
}


---2138096204-1237206507-1012101844=:13414
Content-Type: TEXT/PLAIN; charset=US-ASCII; name="dynload_next.c"
Content-Transfer-Encoding: BASE64
Content-ID: <Pine.OSX.4.43.0201262224040.13414@d-128-61-180.bootp.virginia.edu>
Content-Description: 
Content-Disposition: attachment; filename="dynload_next.c"

DQovKiBTdXBwb3J0IGZvciBkeW5hbWljIGxvYWRpbmcgb2YgZXh0ZW5zaW9u
IG1vZHVsZXMgKi8NCg0KI2luY2x1ZGUgIlB5dGhvbi5oIg0KI2luY2x1ZGUg
ImltcG9ydGRsLmgiDQoNCiNpbmNsdWRlIDxtYWNoLW8vZHlsZC5oPg0KDQoj
ZGVmaW5lIERFQlVHIDENCiNkZWZpbmUgUFJJVkFURSBOU0xJTktNT0RVTEVf
T1BUSU9OX1BSSVZBVEUNCiNkZWZpbmUgT1RIRVJfT1BUSU9OUyAgTlNMSU5L
TU9EVUxFX09QVElPTl9CSU5ETk9XfE5TTElOS01PRFVMRV9PUFRJT05fUkVU
VVJOX09OX0VSUk9SDQojZGVmaW5lIExJTktfTU9EVUxFX09QVElPTlMgIChQ
UklWQVRFIHwgT1RIRVJfT1BUSU9OUykNCg0KY29uc3Qgc3RydWN0IGZpbGVk
ZXNjciBfUHlJbXBvcnRfRHluTG9hZEZpbGV0YWJbXSA9IHsNCgl7Ii5zbyIs
ICJyYiIsIENfRVhURU5TSU9OfSwNCgl7Im1vZHVsZS5zbyIsICJyYiIsIENf
RVhURU5TSU9OfSwNCgl7MCwgMH0NCn07DQoNCmNoYXIgZXJybWVzc2FnZVs0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