Блог пользователя arknave

Автор arknave, история, 5 лет назад, По-английски

Run the following code in the custom invocation tab with pypy3.6

import random

print('Hello world!')

This gives the following verdict:

Runtime error: exit code is 13131313
=====
Used: 373 ms, 10900 KB

Any ideas why? This also happens with pypy2.7, but not when using the CPython interpreters.

  • Проголосовать: нравится
  • +48
  • Проголосовать: не нравится

»
5 лет назад, # |
Rev. 3   Проголосовать: нравится +64 Проголосовать: не нравится

This has been an issue on codeforces for a long time. Know I messaged mike about this issue 7 months ago. For as long as I've been on codeforces, importing random on PyPy3 has never worked. Importing random on PyPy2 used to work, but the last update also broke PyPy2.

I've done some testing and I found that the issue comes from random importing the module hashlib which in turn imports the built in module _hashlib which causes the RTE. Since _hashlib is built in (not python code) I haven't been able to exactly pin point what causes the RTE. I have however come up with a work around.

Random only imports hashlib for one reason, and that is to allow random.seed to take in a string. So if you just don't care about seeding with strings, you could just make PyPy think that hashlib has already been imported, which avoids the crash.

import sys
sys.modules["hashlib"] = sys.sha512 = sys
import random
  • »
    »
    5 лет назад, # ^ |
      Проголосовать: нравится +11 Проголосовать: не нравится

    It was an interesting issue to debug way back then. It's good that a simple workaround exists, but actually fixing the issue would be better.

»
5 лет назад, # |
  Проголосовать: нравится +18 Проголосовать: не нравится

I'll fix it in 2-3 days.

  • »
    »
    5 лет назад, # ^ |
    Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

    It turns out that in recent versions _hashlib is not builtin anymore. So digging deeper you find that importing _cffi_ssl._stdssl.utility is what causes the crash.

    What's really interesting is that including the module from file manually like

    import sys,imp
    mymodule = imp.new_module('_cffi_ssl._stdssl.utility')
    f = open(r'C:\Programs\pypy3\lib_pypy\_cffi_ssl\_stdssl\utility.py')
    exec(f.read(), mymodule.__dict__)
    sys.modules['_cffi_ssl._stdssl.utility'] = mymodule
    
    import random
    random.seed('test')
    print(random.random())
    

    makes everything work. Not sure why this is, but I think this gets to the bottom of the issue.