Repository: psychopy/psychopy

Issue:
TypeError: unsupported operand type(s) for &: 'long' and 'LP_c_void' when using the pyglet backend
Under python 2.7 and psychopy 1.90.0 I get the following error:

```python-traceback
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
C:\Users\analuisa\gitrepos\cognitive_protocols\visual_protocols\retinotopy\retinoto.py in <module>()
    275         fixation_callback, pref_color = get_retino_fixation(number)
    276
--> 277         window = visual.Window(fullscr=False)
    278         show_init_screen(
    279             window, u"Chargement... \n", wait_key=None, with_fixation=False)

C:\Users\analuisa\Anaconda2\lib\site-packages\psychopy\visual\window.pyc in __init__(self, size, pos, color, colorSpace, rgb, dkl, lms, fullscr, allowGUI, monitor, bitsMode, winType, units, gamma, blendMode, screen, viewScale, viewPos, viewOri, waitBlanking, allowStencil, multiSample, numSamples, stereo, name, checkTiming, useFBO, useRetina, autoLog, *args, **kwargs)
    373
    374         # setup the context
--> 375         self.backend = backends.getBackend(win=self, *args, **kwargs)
    376         self.winHandle = self.backend.winHandle
    377         global GL

C:\Users\analuisa\Anaconda2\lib\site-packages\psychopy\visual\backends\__init__.pyc in getBackend(win, *args, **kwargs)
     30                              "there is no backend definition to match that "
     31                              "winType.".format(win.winType))
---> 32     return Backend(win, *args, **kwargs)
     33

C:\Users\analuisa\Anaconda2\lib\site-packages\psychopy\visual\backends\pygletbackend.pyc in __init__(self, win, *args, **kwargs)
    221         # store properties of the system
    222         self._driver = pyglet.gl.gl_info.get_renderer()
--> 223         self._origGammaRamp = self.getGammaRamp()
    224         self._rampSize = getGammaRampSize(self.screenID, self.xDisplay)
    225         self._TravisTesting = (os.environ.get('TRAVIS') == 'true')

C:\Users\analuisa\Anaconda2\lib\site-packages\psychopy\visual\backends\pygletbackend.pyc in getGammaRamp(self)
    320
    321     def getGammaRamp(self):
--> 322         return getGammaRamp(self.screenID, self.xDisplay)
    323
    324     @property

C:\Users\analuisa\Anaconda2\lib\site-packages\psychopy\visual\backends\pygletbackend.pyc in screenID(self)
    332                 _screenID = 0xFFFFFFFF & int.from_bytes(scrBytes, byteorder='little')
    333             else:
--> 334                 _screenID = 0xFFFFFFFF & scrBytes
    335         elif sys.platform == 'darwin':
    336             try:

TypeError: unsupported operand type(s) for &: 'long' and 'LP_c_void'
```

This protocol works fine with psychopy 1.85.3.



Discussion:
More info: this is under Windows 10 with pyglet 1.3.2.
Thanks for the info. Puzzling because the old code appears to be doing the same but didn't have a type error. I'll do my best to work it out though :-/

Are you sure that this version of pyglet worked with 1.85.3. I'm just wondering whether you might have updated both at the same time (and maybe something changed in pyglet)?
I'm also having trouble with this issue.  My setup is Windows 10 X64, Python 2.7.10 X86, PsychoPy 1.90.0 and Pyglet 1.3.0.

I read source code of pyglet and found that the content of `winHandle._dc` (=`scrBytes`) seems to be set in `pyglet.window.win32.Win32Window._create()`. In this method, return value of `_user32.GetDC()` is set to `_dc`.  `_user32.GetDC()` is actually `ctypes.windll.user32.GetDC()` as defined in pyglet/libs/win32/\_\_init\_\_.py.

Type of the return value of `_user32.GetDC()` are defined in lines 157 of pyglet/libs/win32/\_\_init\_\_.py as following.

```
# workaround for win 64-bit, see issue #664
_user32.GetDC.restype = c_void_p # HDC
```

Changing `restype` to `HDC`, PsychoPy can create pyglet window without error.
I think it difficult to fix this issue without editing pyglet codes.
FYI: replacing pyglet 1.3.0 with 1.2.4 seems to fix this issue in my setup.  In addition, pyglet 1.3.0 works fine with Windows10 x64, Python 3.6.1 x64 and the latest PsychoPy.

At the beginning, when I installed PsychoPy 1.90.0, pyglet was giving me the following error:

```python-traceback
NameError: name 'PUINT' is not defined
```
I have updated afterwards pyglet, according to what it is reported [here](https://bitbucket.org/pyglet/pyglet/issues/170/nameerror-name-puint-is-not-defined). I used the following command line:

```python-traceback
pip install --upgrade https://bitbucket.org/pyglet/pyglet/get/tip.zip
```

Then, it gave the error that I reported in the first instance of this thread. Afterwards, I downgraded my PsychoPy version to 1.85.3 and it solved the problem.
With  PsychoPy 1.90.1 / Pyglet 1.4 / Python 2.7 / Windows 7 32 bit I was having the same problem as described by alpinho and hsogo. Downgrading to Pyglet 1.24 solved the issue.
Wasn't this addressed via #1777 and #1787? Could you try again with latest master?
I tried the latest master with following setups.  Both work fine now.

- Windows 10 x64, Python 2.7.10 x86 and Pyglet 1.3.2
- Windows 10 x64, Python 3.6.1 x64 and Pyglet 1.3.0

Great, thanks! So this seems to be resolved.
Sorry, I noticed that the issue is not fixed in Python 2.7 x86 and Pyglet 1.3.2.
It seems I made a mistake in switching back and forth between Pyglet 1.2.4 and 1.3.2.
Do not downgrade pyglet - it is nice.

The error fixed with patch to pygletbackend.py : 338

```
338c338
<                 _screenID = 0xFFFFFFFF & scrBytes
---
>                 _screenID = scrBytes
```


Thanks @sherdim I didn't know that works. But there are other installations where it's needed, so I'll apply that using a try...except and hopefully everything will be happy!

Resolving patch:
diff --git a/psychopy/visual/backends/pygletbackend.py b/psychopy/visual/backends/pygletbackend.py
--- a/psychopy/visual/backends/pygletbackend.py
+++ b/psychopy/visual/backends/pygletbackend.py
@@ -335,7 +335,11 @@ def screenID(self):
             if constants.PY3:
                 _screenID = 0xFFFFFFFF & int.from_bytes(scrBytes, byteorder='little')
             else:
-                _screenID = 0xFFFFFFFF & scrBytes
+                try:
+                    _screenID = 0xFFFFFFFF & scrBytes
+                except TypeError:
+                    _screenID = scrBytes
+
         elif sys.platform == 'darwin':
             try:
                 _screenID = self.winHandle._screen.id  # pyglet1.2alpha1


Test patch: