Python 2.6 Code in 3.2 verwenden

Python in C/C++ embedden, C-Module, ctypes, Cython, SWIG, SIP etc sind hier richtig.
Antworten
PythonFish
User
Beiträge: 26
Registriert: Mittwoch 17. August 2011, 07:37

Hallo,

ich bin gerade dabei Wii Remote Controller mit Hilfe der Native C++ Lib WiiYourself (http://wiiyourself.gl.tter.org/) mit dem Computer zu verbinden. WiiYourself hat einen Python Wrapper (Verwendung von Swig). Jedoch wurde dieser nur für Python 2.6 geschrieben.

Ich bin nun dabei zu versuchen diesen unter Python 3.2 zu verwenden. Dies habe ich auch geschafft (Konnte den Controller mit dem PC verbinden und die Events auslesen).

Jedoch gab es vorher 2,3 kleiner Probleme bei denen ich mir nicht sicher bin ob mein Workaround keine spätere Auswirkung haben wird.

1)

Code: Alles auswählen

// Python module footer code
%pythoncode %{
__all__ = []
def __cleanup_namespace():
  ''' Cleanup exported variables, to reduce bloat in the module.
      Removes _swigregister functions and undoes the promote-to-global
      modification which was applied earlier. '''
  class NullClass:
    pass
  for i in globals().copy():
    if i.endswith("_swigregister"):
      del globals()[i]
    elif '__' in i and not i.startswith('__') and not i.endswith('__'):
      # This name was promoted to global scope to help SWIG, as SWIG
      # cannot yet handle nested structs. The following code undoes the
      # promotion and puts the struct back to the proper place.
      path = i.split('__')
      # "super-mkdir" for objects
      for k in range(1,len(path)):
        par = '.'.join(path[:k])
        parobj = eval(par)
        if not hasattr(parobj,path[k]):
          setattr(parobj,path[k],NullClass)
      obj = globals()[i]
      setattr(parobj,path[k],obj)
      obj.__name__ = '.'.join(path)
    elif '__' not in i and not i.startswith('_') and i not in ['weakref', 'weakref_proxy']:
      __all__.append(i)
__cleanup_namespace()
del __cleanup_namespace

# export arrays from C
wiimote.ButtonNameFromBit = tuple([wiimote.GetButtonNameFromBit(i) for i in range(TOTAL_BUTTON_BITS)])
wiimote.ClassicButtonNameFromBit = tuple([wiimote.GetClassicButtonNameFromBit(i) for i in range(TOTAL_BUTTON_BITS)])
wiimote.FreqLookup = tuple([wiimote.GetFreqLookup(i) for i in range(TOTAL_FREQUENCIES)])
[b]del i[/b]
%}
Dies wird der wiimote.py ans Ende angehängt. Bei der Verwendung der wiimote-Lib wird nun folgender Fehler geworfen:

Code: Alles auswählen

  File "C:\Python32\lib\site-packages\wiimote.py", line 1389, in <module>
    del i
NameError: name 'i' is not defined
Wenn ich das richtig verstehe soll am Ende die Variable i gelöscht werden. Als workaround habe ich vor dem Bauen der Lib die Zeile del i auskommentiert. Was wäre hier die richtige Lösung?

2) Mein 2tes Problem tritt beim Aufruf der Connect-Funktion auf.

Fehlermeldung:

Code: Alles auswählen

Traceback (most recent call last):
  File "C:\script\WiiYourself!_1.15\Python\Demo\callbackdemo.py", line 22, in <module>
    while not remote.Connect(remote.FIRST_AVAILABLE):
  File "C:\Python32\lib\site-packages\wiimote.py", line 1214, in Connect
    return _wiimote.wiimote_Connect(self, *args)
NotImplementedError: Wrong number or type of arguments for overloaded function 'wiimote_Connect'.
  Possible C/C++ prototypes are:
    wiimote::Connect(unsigned int,bool)
    wiimote::Connect(unsigned int)
    wiimote::Connect()
Der Code an der Stelle sieht wie folgt aus:

Code: Alles auswählen

1208  def Connect(self, *args):
1209          """
1210          Connect(self, unsigned int wiimote_index = FIRST_AVAILABLE, bool force_hidwrites = False) -> bool
1211          Connect(self, unsigned int wiimote_index = FIRST_AVAILABLE) -> bool
1212          Connect(self) -> bool
1213          """
1214          return _wiimote.wiimote_Connect(self, *args)
Für meinen Test habe ich Zeile 1214 in return _wiimote.wiimote_Connect(self) geändert. Wenn ich das richtig verstehe wird dann wiimote_Connect() verwendet.
Dies fuktioniert zwar, jedoch würde ich gerne verstehen wie das krrekt gelöst wird.

Wäre sehr dankbar wenn hier jemand drüber schauen könnte
Dav1d
User
Beiträge: 1437
Registriert: Donnerstag 30. Juli 2009, 12:03
Kontaktdaten:

Zum 2., wieso hast du das geändert?
So wie ich das sehe ist wiimote::Connect eine überladene Methode:

Code: Alles auswählen

    wiimote::Connect(unsigned int,bool)
    wiimote::Connect(unsigned int)
    wiimote::Connect()
d.h. du kannst die Methode entweder mit keinem, einem Integer oder einem Integer und einem "bool" aufrufen. In Python musst du halt noch "self" mit übergeben. Also:

Code: Alles auswählen

    wiimote.Connect(self, 1123, True)
    wiimote::Connect(1123)
    wiimote::Connect(self)
//Edit: Oh, Ich hätte den Traceback komplett lesen sollen .. der NotImplementedError sieht so aus, als ob Swig das nicht handeln kann
the more they change the more they stay the same
Antworten