arknave's blog

By arknave, history, 5 years ago, In English

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.

  • Vote: I like it
  • +48
  • Vote: I do not like it

»
5 years ago, # |
Rev. 3   Vote: I like it +64 Vote: I do not like it

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 years ago, # ^ |
      Vote: I like it +11 Vote: I do not like it

    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 years ago, # |
  Vote: I like it +18 Vote: I do not like it

I'll fix it in 2-3 days.

  • »
    »
    5 years ago, # ^ |
    Rev. 2   Vote: I like it 0 Vote: I do not like it

    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.