Author: Wim Lavrijsen <[email protected]>
Branch: cppyy-packaging
Changeset: r94595:ba933501a318
Date: 2018-05-07 22:29 -0700
http://bitbucket.org/pypy/pypy/changeset/ba933501a318/
Log: add helper to extract outer namespace from a C++ name
diff --git a/pypy/module/_cppyy/helper.py b/pypy/module/_cppyy/helper.py
--- a/pypy/module/_cppyy/helper.py
+++ b/pypy/module/_cppyy/helper.py
@@ -59,6 +59,26 @@
name = name[:_find_qualifier_index(name)]
return name.strip(' ')
+def extract_namespace(name):
+ # find the namespace the named class lives in, take care of templates
+ tpl_open = 0
+ for pos in xrange(len(name)-1, 1, -1):
+ c = name[pos]
+
+ # count '<' and '>' to be able to skip template contents
+ if c == '>':
+ tpl_open += 1
+ elif c == '<':
+ tpl_open -= 1
+
+ # collect name up to "::"
+ elif tpl_open == 0 and c == ':' and name[pos-1] == ':':
+ # found the extend of the scope ... done
+ return name[0:pos-1]
+
+ # no namespace; assume outer scope
+ return ""
+
#- operator mappings --------------------------------------------------------
_operator_mappings = {}
diff --git a/pypy/module/_cppyy/test/test_helper.py
b/pypy/module/_cppyy/test/test_helper.py
--- a/pypy/module/_cppyy/test/test_helper.py
+++ b/pypy/module/_cppyy/test/test_helper.py
@@ -50,3 +50,14 @@
assert helper.map_operator_name(None, "func", 0, "") == "func"
assert helper.map_operator_name(None, "some_method", 0, "") ==
"some_method"
+
+
+def test_namespace_extraction():
+ assert helper.extract_namespace("vector") == ""
+ assert helper.extract_namespace("std::vector") == "std"
+ assert helper.extract_namespace("std::vector<double>") == "std"
+ assert helper.extract_namespace("std::vector<std::vector>") == "std"
+ assert helper.extract_namespace("vector<double>") == ""
+ assert helper.extract_namespace("vector<std::vector>") == ""
+ assert helper.extract_namespace("aap::noot::mies::zus") ==
"aap::noot::mies"
+
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit