[PATCH 1 of 3] rust: module policy with importrust

Yuya Nishihara yuya at tcha.org
Thu May 23 19:35:19 EDT 2019


On Thu, 23 May 2019 13:29:05 +0200, Georges Racinet wrote:
> # HG changeset patch
> # User Georges Racinet <georges.racinet at octobus.net>
> # Date 1558603154 -7200
> #      Thu May 23 11:19:14 2019 +0200
> # Node ID 36b4c4869531b3de0b101c941b77b94da85890b7
> # Parent  52beb1b8a64967bead7bc9e8a63c460430729b7a
> # EXP-Topic rust-modulepolicy
> rust: module policy with importrust

(not reviewed thoroughly)

> +def importrust(modname):
> +    """Import Rust module according to policy and availability.
> +
> +    If policy is non-Rust, this returns None
> +    If policy is Rust and the module is non available, returns None if policy
> +    is '-allow' else raise ImportError
> +    """
> +    if not policy.startswith('rust'):

Perhaps, it's b''.

> +        return None
> +    try:
> +        mod = _importfrom('rustext', modname)
> +    except ImportError:
> +        if policy.endswith('-allow'):
> +            return None
> +        raise
> +    return mod

How about making it return a cext module (or provided default) as a fallback?

  mayberustext = policy.importrust(r'parsers')
  # or
  mayberustext = policy.importrust('parers', parsers)

I think some "if rust is not None" can be removed with this trick, and
eventually the importrust() can be merged with the importmod().


More information about the Mercurial-devel mailing list