def rurandom_example(): r = rurandom.urandom(None, 100) print r # The call can be interrupted by a signal, you can define a callback # function for status checking r = rurandom.urandom(None, 100, signal_checker)
def urandom(space, n): """urandom(n) -> str Return a string of n random bytes suitable for cryptographic use. """ context = get(space).random_context try: return space.wrapbytes(rurandom.urandom(context, n)) except OSError, e: raise wrap_oserror(space, e)
def urandom(space, n): """urandom(n) -> str Return a string of n random bytes suitable for cryptographic use. """ context = get(space).random_context signal_checker = space.getexecutioncontext().checksignals try: return space.wrap(rurandom.urandom(context, n, signal_checker)) except OSError as e: raise wrap_oserror(space, e)
def urandom(space, n): """urandom(n) -> str Return a string of n random bytes suitable for cryptographic use. """ context = get(space).random_context try: # urandom() takes a final argument that should be a regular function, # not a bound method like 'getexecutioncontext().checksignals'. # Otherwise, we can't use it from several independent places. _sigcheck.space = space return space.newbytes(rurandom.urandom(context, n, _signal_checker)) except OSError as e: raise wrap_oserror(space, e)
def urandom(space, n): """urandom(n) -> str Return a string of n random bytes suitable for cryptographic use. """ context = get(space).random_context try: # urandom() takes a final argument that should be a regular function, # not a bound method like 'getexecutioncontext().checksignals'. # Otherwise, we can't use it from several independent places. _sigcheck.space = space return space.newbytes(rurandom.urandom(context, n, _signal_checker)) except OSError as e: # CPython raises NotImplementedError if /dev/urandom cannot be found. # To maximize compatibility, we should also raise NotImplementedError # and not OSError (although CPython also raises OSError in case it # could open /dev/urandom but there are further problems). raise wrap_oserror(space, e, w_exception_class=space.w_NotImplementedError)
def initialize_from_env(): # This uses the same algorithms as CPython 3.5. The environment # variable we read also defaults to "PYTHONHASHSEED". If needed, # a different RPython interpreter can patch the value of the # global variable 'env_var_name', or just patch the whole # initialize_from_env() function. value = os.environ.get(env_var_name) if value and value != "random": with rffi.scoped_view_charp(value) as ptr: with lltype.scoped_alloc(rffi.CCHARPP.TO, 1) as endptr: endptr[0] = ptr seed = strtoul(ptr, endptr, 10) full = endptr[0][0] == '\x00' seed = lltype.cast_primitive(lltype.Unsigned, seed) if not full or seed > r_uint(4294967295) or ( rposix.get_saved_errno() == errno.ERANGE and seed == lltype.cast_primitive(lltype.Unsigned, rffi.cast(rffi.ULONG, -1))): os.write( 2, "%s must be \"random\" or an integer " "in range [0; 4294967295]\n" % (env_var_name, )) os._exit(1) if not seed: # disable the randomized hash s = '\x00' * 16 else: s = lcg_urandom(seed) else: try: s = rurandom.urandom(random_ctx, 16) except Exception as e: os.write( 2, "%s: failed to get random numbers to initialize Python\n" % (str(e), )) os._exit(1) raise # makes the annotator happy select_random_seed(s)
def test_rurandom(): context = rurandom.init_urandom() s = rurandom.urandom(context, 5000) assert type(s) is str and len(s) == 5000 for x in [1, 11, 111, 222]: assert s.count(chr(x)) >= 1