-6

I've poked around at the PyObjC innards quite a bit trying to figure this out. Is it possible to access Objective-C's hidden SEL _cmd method argument when writing a Python method? It's got to be generated at some point, but I'm not sure if that point is one at which my Python code can get to it.

I was mostly interested in this in order to be able to make an easy PyObjC NSLog text macro:

def meth_(self, arg):
    NSLog(u"%s called" % _cmd)

although I have found other ways to do this kind of logging in Python (see Jeremy's answer), so at this point it's become curiosity about the PyObjC bridge.

jscs
  • 63,694
  • 13
  • 151
  • 195

2 Answers2

3

Why not go about it by logging the name of the Python function, instead? Then you can apply a Python approach to getting the function name, like throwing an exception and munging around in the stack trace, as shown in this code snippet.

ETA: This code demonstrates the "better way" mentioned in the prior snippet as introduced in Python 2.1: using sys._getframe(). It is far simpler.

Jeremy W. Sherman
  • 35,901
  • 5
  • 77
  • 111
  • Indeed. I've seen these and made use of the second when necessary. Not exactly what I'm looking for, but I appreciate the answer. – jscs May 12 '11 at 22:18
-1

I am not familiar with Python. But in objective-C instead _cmd you can find method selector by:

const id* selfPtr = &self;
SEL sel = *(SEL*)(void*)(--selfPtr);

May be you can use same approach in Python.

Cy-4AH
  • 4,370
  • 2
  • 15
  • 22