From bb285e43254356931582e999ba31a51b014f774e Mon Sep 17 00:00:00 2001 From: albert Date: Wed, 17 Aug 2016 17:57:53 +0200 Subject: [PATCH 1/2] Add `dylink` and Python underscore-import tests These tests try to illustrate Python's behavior on `import`ing or `from MODULE import *`ing a name that starts with an underscore (``_'') character, and check whether `dylink`'s corresponding functions `dy_import_module` and `dy_import_module_symbols` perform like their Python counterparts. The expected behavior is to * do import `module._underscorename`, but * don't add `_underscorename` via `from module import *` Alas, `dylink` doesn't conform currently: SeattleTestbed/seattlelib_v2#164 . --- tests/examplelib.py | 1 + ...t_seattlelib_dylinkunderscoreimportmodule.r2py | 8 ++++++++ ...lelib_dylinkunderscoreimportmodulesymbols.r2py | 15 +++++++++++++++ ...eattlelib_dylinkunderscorepythonfromximport.py | 14 ++++++++++++++ .../ut_seattlelib_dylinkunderscorepythonimport.py | 7 +++++++ 5 files changed, 45 insertions(+) create mode 100644 tests/ut_seattlelib_dylinkunderscoreimportmodule.r2py create mode 100644 tests/ut_seattlelib_dylinkunderscoreimportmodulesymbols.r2py create mode 100644 tests/ut_seattlelib_dylinkunderscorepythonfromximport.py create mode 100644 tests/ut_seattlelib_dylinkunderscorepythonimport.py diff --git a/tests/examplelib.py b/tests/examplelib.py index 4e01a98..64a7415 100644 --- a/tests/examplelib.py +++ b/tests/examplelib.py @@ -2,3 +2,4 @@ Example library used to demonstrate import-override rules. """ foo = "examplelib.py's foo" +_foo = "examplelib.py's underscore-foo" diff --git a/tests/ut_seattlelib_dylinkunderscoreimportmodule.r2py b/tests/ut_seattlelib_dylinkunderscoreimportmodule.r2py new file mode 100644 index 0000000..f14e52c --- /dev/null +++ b/tests/ut_seattlelib_dylinkunderscoreimportmodule.r2py @@ -0,0 +1,8 @@ +""" +Verify that `dylink`'s `dy_import_module` makes names with an underscore +prefix accessible. +""" +#pragma repy restrictions.test dylink.r2py +examplelib = dy_import_module("examplelib.py") + +examplelib._foo diff --git a/tests/ut_seattlelib_dylinkunderscoreimportmodulesymbols.r2py b/tests/ut_seattlelib_dylinkunderscoreimportmodulesymbols.r2py new file mode 100644 index 0000000..01adef0 --- /dev/null +++ b/tests/ut_seattlelib_dylinkunderscoreimportmodulesymbols.r2py @@ -0,0 +1,15 @@ +""" +Verify that `dylink`'s `dy_import_module_symbols` hides names with +an underscore prefix from the importer's namespace. +""" +#pragma repy restrictions.test dylink.r2py +dy_import_module_symbols("examplelib.py") + +try: + _foo +except NameError: + # The expected, correct exception. + pass +else: + log("Error! Expected examplelib's `_foo` to be hidden, like in Python!\n") + diff --git a/tests/ut_seattlelib_dylinkunderscorepythonfromximport.py b/tests/ut_seattlelib_dylinkunderscorepythonfromximport.py new file mode 100644 index 0000000..f45ab1a --- /dev/null +++ b/tests/ut_seattlelib_dylinkunderscorepythonfromximport.py @@ -0,0 +1,14 @@ +""" +Demonstrate that Python's `from MODULE import *` hides names with +an underscore prefix from the importer's namespace. +""" +from examplelib import * + +try: + _foo +except NameError: + # The expected, correct exception. + pass +else: + print "Error! Expected examplelib's `_foo` to be hidden!" + diff --git a/tests/ut_seattlelib_dylinkunderscorepythonimport.py b/tests/ut_seattlelib_dylinkunderscorepythonimport.py new file mode 100644 index 0000000..e711c87 --- /dev/null +++ b/tests/ut_seattlelib_dylinkunderscorepythonimport.py @@ -0,0 +1,7 @@ +""" +Demonstrate that Python's `import` makes names with an underscore +prefix accessible. +""" +import examplelib +examplelib._foo + From f8851a998b6e716a27ee84dd1dc2ed8623fd0909 Mon Sep 17 00:00:00 2001 From: albert Date: Fri, 26 Aug 2016 16:21:20 +0200 Subject: [PATCH 2/2] Fix #164, dylink import behavior for "_..." names This patch makes `dylink.r2py`'s import functions behave more like their Python counterparts. Specifically, * `dy_import_module_symbols` (akin to Python's `from MODULE import *`) now correctly *skips* names that start with an underscort ("_"), and * `dy_import_module` (akin to Python's `import MODULE`) now *does* correctly import these names. The new test cases supplied by the parent commit, aaaaalbert/seattlelib_v2@bb285e43254356931582e999ba31a51b014f774e , thus all pass now. --- dylink.r2py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/dylink.r2py b/dylink.r2py index da9e19f..96e5f4e 100644 --- a/dylink.r2py +++ b/dylink.r2py @@ -100,8 +100,8 @@ def _dy_bind_code(module, context): # Check each key, and bind those we should... for attr in context: - # Skip attributes starting with _ or part of the stock API - if attr.startswith("_") or attr in STOCK_API: + # Skip attributes that are part of the stock API + if attr in STOCK_API: continue # Bind now @@ -245,7 +245,8 @@ def dylink_import_global(module, module_context,new_callfunc="import"): # It is critical to avoid overwriting things like _context, callfunc, # and dy_import_module because these are specific to the containing # module - if symbolname not in SYMBOLS_TO_IGNORE_WHEN_FROM_X_IMPORT: + if (symbolname not in SYMBOLS_TO_IGNORE_WHEN_FROM_X_IMPORT and + not symbolname.startswith("_")): module_context[symbolname] = module_object._context[symbolname]