Typeerror: cannot convert 'int' object to byte

I am running Anaconda spyder 3.6, 64 bit and psychopy 1.90.1 via (pip install psychopy) on windows 10 64 bit. I am getting above mentioned error. I have tried with pyglet 1.2.2, 1.2.4 and 1.3.1, but still same error. I am putting the error trace back. Does anyone is having any idea about this problem?
Traceback (most recent call last):

'''
File "<ipython-input-10-1d8bff329d88>", line 2, in <module>
extra_info=exp_info, iti=1, t_inter_blocks=1, outdir=outfile)

 File "<ipython-input-4-942a2dbc6c26>", line 57, in run_experiment
win = visual.Window(fullscr=True, size=(1920, 1080), monitor='laptop') #TODO: handle in a different way the screen resolution

  File "C:\Users\ravikumar.mevada\Anaconda3\lib\site-packages\psychopy\visual\window.py", line 375, in __init__
self.backend = backends.getBackend(win=self, *args, **kwargs)

 File "C:\Users\ravikumar.mevada\Anaconda3\lib\site-packages\psychopy\visual\backends\__init__.py", line 32, in getBackend
return Backend(win, *args, **kwargs)

File "C:\Users\ravikumar.mevada\Anaconda3\lib\site-packages\psychopy\visual\backends\pygletbackend.py", line 223, in __init__
self._origGammaRamp = self.getGammaRamp()

File "C:\Users\ravikumar.mevada\Anaconda3\lib\site-packages\psychopy\visual\backends\pygletbackend.py", line 322, in getGammaRamp
return getGammaRamp(self.screenID, self.xDisplay)

File "C:\Users\ravikumar.mevada\Anaconda3\lib\site-packages\psychopy\visual\backends\pygletbackend.py", line 332, in screenID
_screenID = 0xFFFFFFFF & int.from_bytes(scrBytes, byteorder='little')

TypeError: cannot convert 'int' object to bytes

I get the same error. I haven’t been able to fix it. I tried win 32 and 64 bit, python 3.6.5.

Actually! I just updated pip and installed Psychopy 1.90.1, I had the previous version. This cleaned up some installs and it’s good now. :+1:

It’s been a while since the question has been asked. However, as it hasn’t been solved, and I got here by google and others will too, here is the solution:

scrBytes is of type ‘int’. So pyglet is trying to convert an int to an int, using a method that is designed to convert bytes to ‘int’. It will probably try to convert whatever you put in as argument to ‘byte’ before, if it isn’t already of that type. So it’s trying to get an int by converting an int to a byte to an int. Converting int to byte doesn’t seem to work, which is why it throws an error at you. It’s solved by just taking the int as is.

Using pyglet version 1.2.4, make line 399 of \psychopy\visual\backends\pygletbackends.py
this:
_screenID = 0xFFFFFFFF & scrBytes #int.from_bytes(scrBytes, byteorder=‘little’)
instead of this:
_screenID = 0xFFFFFFFF & int.from_bytes(scrBytes, byteorder=‘little’)

Hope that helps someone…
Cheers,
Lukas

1 Like

The problem still exists in psychopy v. 1.90.2. It originates in l. 334 of pygletbackend.py where a device handle for the current window is requested:

334: scrBytes = self.winHandle._dc

It is assumed that under Windows, this device handle is returned as Python’s bytes type. Probably this is true for some installations but not all.

Fortunately, the bugfix proposed by LukasNeugebauer still works, only that the code in question now starts in l. 335 of pygletbackend.py. However, assuming the original code was put in there for a reason and might be necessary on other Windows machines, I’d suggest the following fix.

Change ll. 335-336 of the original code of pygletbackend.py, which currently reads

335:  if constants.PY3:
336:      _screenID = 0xFFFFFFFF & int.from_bytes(scrBytes, byteorder='little')

to

335:  if constants.PY3:
336:      if isinstance(scrBytes, bytes):
337:          _screenID = 0xFFFFFFFF & int.from_bytes(scrBytes, byteorder='little')
338:      else:
339:          _screenID = 0xFFFFFFFF & scrBytes

That’ll do.

I’m not sure of the scope of this bug (does it just affect people running scripts from within Spyder/iPython on Windows?) but well done for chasing it down and suggesting a fix.

The best way to get it incorporated into the code base though would be to make a pull request to the PsychoPy repository on GitHub. Would that be possible?

I’m actually using Psychopy 1.90.2 and you’re absolutely right about the line number. Don’t know what went wrong there…
What version of pyglet are you using? This fix only worked for pyglet 1.2.4 and I didn’t manage to find the problem for other versions. Since I’m a neuroscientist and not a programmer and that was enough for what I needed, I didn’t dive too deep into it tbh…

While I generally agree with you that making fixes part of the code base is the way to go, this is a specific fix for pyglet 1.2.4 and this is generally not the version that is being recommended, I think. If someone finds out how to solve this issue more generally, that’d be worth a pull request.

1 Like