better regex, avoid Python keyword name collisions: WHERE = type == "btrfs" or a_pass == "1" v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Sat, 15 Dec 2018 01:02:49 +0100
branchv_0
changeset 18 6a178b86d048
parent 17 7a91400b84c2
child 19 5c7f0d21284e
better regex, avoid Python keyword name collisions: WHERE = type == "btrfs" or a_pass == "1" other usage: WHERE = type == "btrfs" or a_pass == "1"; r[0] = "TROLL!!!"
src/PythonHandler.h
--- a/src/PythonHandler.h	Sat Dec 15 00:44:04 2018 +0100
+++ b/src/PythonHandler.h	Sat Dec 15 01:02:49 2018 +0100
@@ -48,8 +48,8 @@
 
 class PythonHandler : public RelationalReaderStringHadler {
 private:
-	const wregex ATTRIBUTE_NAMES_ALLOWED = wregex(L"[a-zA-Z0-9_]");
-	const wregex ATTRIBUTE_NAMES_DISALLOWED = wregex(L"WHERE|and|or|[0-9_].*"); // TODO: more keywords
+	const wregex ATTRIBUTE_NAMES_ALLOWED = wregex(L"[a-zA-Z0-9_]+");
+	const wregex ATTRIBUTE_NAMES_DISALLOWED = wregex(L"WHERE|[0-9_].*|False|None|True|and|as|assert|break|class|continue|def|del|elif|else|except|finally|for|from|global|if|import|in|is|lambda|nonlocal|not|or|pass|raise|return|try|while|with|yield"); // Python keywords from: import keyword; keyword.kwlist
 
 	shared_ptr<writer::RelationalWriter> relationalWriter;
 
@@ -128,7 +128,8 @@
 					PyObject* pyValue = PyUnicode_FromString(convertor.to_bytes(currentRecord[i]).c_str());
 					PyList_SetItem(pyRecord, i, pyValue);
 					string_t attributeName = currentWriterMetadata[i].attributeName;
-					if (regex_match(attributeName, ATTRIBUTE_NAMES_ALLOWED) && !regex_match(attributeName, ATTRIBUTE_NAMES_DISALLOWED)) {
+					if (regex_match(attributeName, ATTRIBUTE_NAMES_ALLOWED)) {
+						if (regex_match(attributeName, ATTRIBUTE_NAMES_DISALLOWED)) attributeName = L"a_" + attributeName;
 						PyDict_SetItemString(pyDict, convertor.to_bytes(attributeName).c_str(), pyValue);
 					}
 				}