Page MenuHomePhabricator

captcha.py issues due to change in `/` behaviour in python 3
Closed, ResolvedPublic

Description

I tried using captcha.py on python 3 and I get the following error:

multiprocessing.pool.RemoteTraceback: 
"""
Traceback (most recent call last):
  File "/usr/lib/python3.8/multiprocessing/pool.py", line 125, in worker
    result = (True, func(*args, **kwds))
  File "/usr/lib/python3.8/multiprocessing/pool.py", line 48, in mapstar
    return list(map(*args))
  File "captcha.py", line 205, in run_in_thread
    for i in range(count):
TypeError: 'float' object cannot be interpreted as an integer
"""

This is probably because python3 division has changed: "/" now does true divide not floor divide. We need to replace with "//" in some places to do floor divide.

Event Timeline

Aklapper renamed this task from captcha.py in FancyCaptcha doesn't support Python 3 to captcha.py in FancyCaptcha doesn't support Python 3: TypeError: 'float' object cannot be interpreted as an integer.Sep 18 2020, 7:40 AM
Aklapper added a project: Python3-Porting.

This is curious.. We did the python3 support back in T157888: Make captcha(-old)?.py python3 compatible, but obviously didn't necessarily test all paths through the code (and it's 3 years ago now)... Wikimedia production is still seemingly using 2.7 for "python", so probably haven't seen it there either

The only relevant divison I really see is in this block, which probably is where the count for the loop is coming from

	if count < threads:
		chunks = 1
		threads = 1
	else:
		chunks = (count / threads)

This is probably because python3 division has changed: "/" now does true divide not floor divide. We need to replace with "//" in some places to do floor divide.

Can you try that and see if it actually works? It's line 290 of captcha.py to be changed, I think

Also, what version of python, what paramters did you call the python (or more specifically, the php maintenance script) with? I guess the number of captchas and the threads parameters are probably the interesting ones here...

There are some other bits of division in the image generation, but your run hasn't got that far yet... Some use int() to wrap the divison

Change 628406 had a related patch set uploaded (by Reedy; owner: Reedy):
[mediawiki/extensions/ConfirmEdit@master] Use floor division (//) when calculating chunks

https://gerrit.wikimedia.org/r/628406

Ok, so using a chunk of 100, and 3 threads, I can replicate your reported error. Patch just uploaded does fix that. But it then does expose an error in another part of the code, though, this time, it is in "vendor" code in the PIL library

Traceback (most recent call last):
  File "/usr/lib/python3.8/multiprocessing/pool.py", line 125, in worker
    result = (True, func(*args, **kwds))
  File "/usr/lib/python3.8/multiprocessing/pool.py", line 48, in mapstar
    return list(map(*args))
  File "/var/www/wiki/mediawiki/extensions/ConfirmEdit/captcha.py", line 216, in run_in_thread
    gen_captcha(word, font, fontsize, os.path.join(output, filename))
  File "/var/www/wiki/mediawiki/extensions/ConfirmEdit/captcha.py", line 112, in gen_captcha
    noise = Image.new('L', nsize, bgcolor)
  File "/usr/lib/python3/dist-packages/PIL/Image.py", line 2544, in new
    return im._new(core.fill(mode, size, color))
TypeError: integer argument expected, got float

Line 112 of captcha.py is the second of these two lines

	nsize = (im.size[0] / nblock, im.size[1] / nblock)
	noise = Image.new('L', nsize, bgcolor)

So I guess we probably want something similar there too

Change 628372 had a related patch set uploaded (by Reedy; owner: Reedy):
[mediawiki/extensions/ConfirmEdit@REL1_35] Use floor division (//) when calculating chunks in python

https://gerrit.wikimedia.org/r/628372

Change 628373 had a related patch set uploaded (by Reedy; owner: Reedy):
[mediawiki/extensions/ConfirmEdit@REL1_34] Use floor division (//) when calculating chunks in python

https://gerrit.wikimedia.org/r/628373

Change 628374 had a related patch set uploaded (by Reedy; owner: Reedy):
[mediawiki/extensions/ConfirmEdit@REL1_31] Use floor division (//) when calculating chunks in python

https://gerrit.wikimedia.org/r/628374

Change 628373 merged by jenkins-bot:
[mediawiki/extensions/ConfirmEdit@REL1_34] Use floor division (//) when calculating chunks in python

https://gerrit.wikimedia.org/r/628373

Change 628372 merged by jenkins-bot:
[mediawiki/extensions/ConfirmEdit@REL1_35] Use floor division (//) when calculating chunks in python

https://gerrit.wikimedia.org/r/628372

Change 628374 merged by jenkins-bot:
[mediawiki/extensions/ConfirmEdit@REL1_31] Use floor division (//) when calculating chunks in python

https://gerrit.wikimedia.org/r/628374

Change 628406 merged by jenkins-bot:
[mediawiki/extensions/ConfirmEdit@master] Use floor division (//) when calculating chunks in python

https://gerrit.wikimedia.org/r/628406

Reedy renamed this task from captcha.py in FancyCaptcha doesn't support Python 3: TypeError: 'float' object cannot be interpreted as an integer to captcha.py issues due to change in / behaviour in python 3.Sep 18 2020, 6:17 PM
Tacsipacsi renamed this task from captcha.py issues due to change in / behaviour in python 3 to captcha.py issues due to change in `/` behaviour in python 3.Sep 20 2020, 12:31 AM
Tacsipacsi added a subscriber: Tacsipacsi.

Change 461505 had a related patch set uploaded (by Reedy; owner: Platonides):
[mediawiki/extensions/ConfirmEdit@master] Make captcha.py work under python 3

https://gerrit.wikimedia.org/r/461505

Change 636959 had a related patch set uploaded (by Reedy; owner: Platonides):
[mediawiki/extensions/ConfirmEdit@REL1_35] Make captcha.py work under python 3

https://gerrit.wikimedia.org/r/636959

Change 636960 had a related patch set uploaded (by Reedy; owner: Platonides):
[mediawiki/extensions/ConfirmEdit@REL1_31] Make captcha.py work under python 3

https://gerrit.wikimedia.org/r/636960

Change 636960 merged by jenkins-bot:
[mediawiki/extensions/ConfirmEdit@REL1_31] Make captcha.py work under python 3

https://gerrit.wikimedia.org/r/636960

Change 636959 merged by jenkins-bot:
[mediawiki/extensions/ConfirmEdit@REL1_35] Make captcha.py work under python 3

https://gerrit.wikimedia.org/r/636959

Change 461505 merged by jenkins-bot:
[mediawiki/extensions/ConfirmEdit@master] Make captcha.py work under python 3

https://gerrit.wikimedia.org/r/461505

Reedy claimed this task.

Should be all fixed now..