Changeset: c16c97c177a0 for MonetDB
URL: http://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=c16c97c177a0
Added Files:
sql/backends/monet5/Tests/pyapi13.sql
sql/backends/monet5/Tests/pyapi13.stable.err
sql/backends/monet5/Tests/pyapi13.stable.out
Modified Files:
monetdb5/extras/pyapi/pyapi.c
sql/backends/monet5/Tests/All
Branch: pyapi
Log Message:
Fixed indenter for multiline strings, added indentation test case.
diffs (truncated from 414 to 300 lines):
diff --git a/monetdb5/extras/pyapi/pyapi.c b/monetdb5/extras/pyapi/pyapi.c
--- a/monetdb5/extras/pyapi/pyapi.c
+++ b/monetdb5/extras/pyapi/pyapi.c
@@ -1874,6 +1874,8 @@ char* FormatCode(char* code, char **args
size_t initial_spaces = 0;
size_t statement_size = 0;
bool seen_statement = false;
+ bool multiline_statement = false;
+ int multiline_quotes = 0;
char base_start[] = "def pyfun(";
char base_end[] = "):\n";
@@ -1915,6 +1917,19 @@ char* FormatCode(char* code, char **args
// We indent using spaces, four spaces per level
// We also erase empty lines
for(i = 0; i < length; i++) {
+ // handle multiline strings (strings that start with """)
+ if (code[i] == '\"') {
+ if (!multiline_statement) {
+ multiline_quotes++;
+ multiline_statement = multiline_quotes == 3;
+ } else {
+ multiline_quotes--;
+ multiline_statement = multiline_quotes != 0;
+ }
+ } else {
+ multiline_quotes = multiline_statement ? 3 : 0;
+ }
+
if (!seen_statement) {
// We have not seen a statement on this line yet
if (code[i] == '\n'){
@@ -1937,6 +1952,14 @@ char* FormatCode(char* code, char **args
// Statement ends here
bool placed = false;
size_t level = 0;
+
+ if (multiline_statement) {
+ //if we are in a multiline statement, we don't want to
mess with the indentation
+ size += statement_size;
+ initial_spaces = 0;
+ statement_size = 0;
+ continue;
+ }
// First put the indentation in the indentation table
if (indentation_count >= max_indentation) {
// If there is no room in the indentation arrays we will
extend them
@@ -2026,9 +2049,23 @@ char* FormatCode(char* code, char **args
// Now the second pass, actually construct the code
for(i = 0; i < length; i++) {
+ //handle multiline statements
+ if (code[i] == '\"') {
+ if (!multiline_statement) {
+ multiline_quotes++;
+ multiline_statement = multiline_quotes == 3;
+ } else {
+ multiline_quotes--;
+ multiline_statement = multiline_quotes != 0;
+ }
+ } else {
+ multiline_quotes = multiline_statement ? 3 : 0;
+ }
+
if (!seen_statement) {
+ if (multiline_statement) seen_statement = true; //if we are in a
multiline string, we simply want to copy everything (including indentation)
// We have not seen a statement on this line yet
- if (code[i] == '\n'){
+ else if (code[i] == '\n'){
// Empty line, skip to the next one
initial_spaces = 0;
} else if (code[i] == ' ') {
@@ -2074,6 +2111,7 @@ char* FormatCode(char* code, char **args
}
}
newcode[code_location] = '\0';
+ //printf("%s\n", newcode);
if (code_location >= size) {
// Something went wrong with our size computation, this also should
never happen
printf("WHAT HAPPENED\n");
diff --git a/sql/backends/monet5/Tests/All b/sql/backends/monet5/Tests/All
--- a/sql/backends/monet5/Tests/All
+++ b/sql/backends/monet5/Tests/All
@@ -11,6 +11,7 @@ HAVE_LIBPY?pyapi08
HAVE_LIBPY?pyapi10
HAVE_LIBPY?pyapi11
HAVE_LIBPY?pyapi12
+HAVE_LIBPY?pyapi13
#HAVE_LIBR?rapi00
#HAVE_LIBR?rapi01
diff --git a/sql/backends/monet5/Tests/pyapi13.sql
b/sql/backends/monet5/Tests/pyapi13.sql
new file mode 100644
--- /dev/null
+++ b/sql/backends/monet5/Tests/pyapi13.sql
@@ -0,0 +1,89 @@
+# Test whitespace
+START TRANSACTION;
+
+CREATE FUNCTION pyapi13_random_table_nulls(entries integer) returns table (i
integer, j integer)
+language P
+{
+ import random
+ random.seed(123)
+ results = [numpy.ma.masked_array(numpy.zeros(entries), 0),
numpy.ma.masked_array(numpy.zeros(entries), 0)]
+ for i in range(0,entries):
+ for j in range(0,2):
+ results[j][i] = random.randint(0,100)
+ if results[j][i] < 50:
+ results[j].mask[i] = True
+ return(results)
+};
+
+#"normal" indentation
+CREATE FUNCTION pyapi13_mult(i integer,j integer) returns integer
+language PYTHON_MAP
+{
+ return(i*j)
+};
+SELECT COUNT(pyapi13_mult(i,j)) FROM pyapi13_random_table_nulls(5000);
+DROP FUNCTION pyapi13_mult;
+
+#weird indentation
+CREATE FUNCTION pyapi13_mult(i integer,j integer) returns integer
+language PYTHON_MAP { return(i*j)
+};
+SELECT COUNT(pyapi13_mult(i,j)) FROM pyapi13_random_table_nulls(5000);
+DROP FUNCTION pyapi13_mult;
+
+#no new line
+CREATE FUNCTION pyapi13_mult(i integer,j integer) returns integer language
PYTHON_MAP { return(i*j) };
+SELECT COUNT(pyapi13_mult(i,j)) FROM pyapi13_random_table_nulls(5000);
+DROP FUNCTION pyapi13_mult;
+
+#\n in string
+CREATE FUNCTION pyapi13_mult(i integer,j integer) returns integer
+language PYTHON_MAP
+{
+x = "test\n\ntesttest\n"
+print(x)
+return(i*j)
+};
+SELECT COUNT(pyapi13_mult(i,j)) FROM pyapi13_random_table_nulls(5000);
+DROP FUNCTION pyapi13_mult;
+
+#Multiline statements
+CREATE FUNCTION pyapi13_mult(i integer,j integer) returns integer
+language PYTHON_MAP
+{
+x = """test123
+testtest
+""test2""
+hello world
+"""
+print(x)
+if len(x) > 10:
+ return(i*j)
+else:
+ return(i+j)
+};
+SELECT COUNT(pyapi13_mult(i,j)) FROM pyapi13_random_table_nulls(5000);
+DROP FUNCTION pyapi13_mult;
+
+#inconsistent indentation (mix spaces and tabs, weird indentation)
+CREATE FUNCTION pyapi13_mult(i integer,j integer) returns integer
+language PYTHON_MAP
+{
+ x = 5
+ y = 4
+ z = x + y
+ if z > x:
+ print("y is not negative!")
+ else:
+ print("y is negative!")
+ if x + y == z:
+ print("Addition in python is not inconsistent!")
+ return(i + j)
+ else:
+ return(i*j)
+};
+SELECT COUNT(pyapi13_mult(i,j)) FROM pyapi13_random_table_nulls(5000);
+DROP FUNCTION pyapi13_mult;
+
+DROP FUNCTION pyapi13_random_table_nulls;
+ROLLBACK;
diff --git a/sql/backends/monet5/Tests/pyapi13.stable.err
b/sql/backends/monet5/Tests/pyapi13.stable.err
new file mode 100644
--- /dev/null
+++ b/sql/backends/monet5/Tests/pyapi13.stable.err
@@ -0,0 +1,38 @@
+stderr of test 'pyapi13` in directory 'sql/backends/monet5` itself:
+
+
+# 18:27:33 >
+# 18:27:33 > "mserver5" "--debug=10" "--set" "gdk_nr_threads=0" "--set"
"mapi_open=true" "--set" "mapi_port=37004" "--set"
"mapi_usock=/var/tmp/mtest-1088/.s.monetdb.37004" "--set" "monet_prompt="
"--forcemito" "--set" "mal_listing=2"
"--dbpath=/home/mytherin/opt/var/mTests_sql_backends_monet5" "--set"
"mal_listing=0" "--set" "embedded_r=true" "--set" "embedded_py=true"
+# 18:27:33 >
+
+# builtin opt gdk_dbpath = /home/mytherin/opt/var/monetdb5/dbfarm/demo
+# builtin opt gdk_debug = 0
+# builtin opt gdk_vmtrim = no
+# builtin opt monet_prompt = >
+# builtin opt monet_daemon = no
+# builtin opt mapi_port = 50000
+# builtin opt mapi_open = false
+# builtin opt mapi_autosense = false
+# builtin opt sql_optimizer = default_pipe
+# builtin opt sql_debug = 0
+# cmdline opt gdk_nr_threads = 0
+# cmdline opt mapi_open = true
+# cmdline opt mapi_port = 37004
+# cmdline opt mapi_usock = /var/tmp/mtest-1088/.s.monetdb.37004
+# cmdline opt monet_prompt =
+# cmdline opt mal_listing = 2
+# cmdline opt gdk_dbpath = /home/mytherin/opt/var/mTests_sql_backends_monet5
+# cmdline opt mal_listing = 0
+# cmdline opt embedded_r = true
+# cmdline opt embedded_py = true
+# cmdline opt gdk_debug = 536870922
+
+# 18:27:33 >
+# 18:27:33 > "mclient" "-lsql" "-ftest" "-Eutf-8" "-i" "-e"
"--host=/var/tmp/mtest-1088" "--port=37004"
+# 18:27:33 >
+
+
+# 18:27:34 >
+# 18:27:34 > "Done."
+# 18:27:34 >
+
diff --git a/sql/backends/monet5/Tests/pyapi13.stable.out
b/sql/backends/monet5/Tests/pyapi13.stable.out
new file mode 100644
--- /dev/null
+++ b/sql/backends/monet5/Tests/pyapi13.stable.out
@@ -0,0 +1,181 @@
+stdout of test 'pyapi13` in directory 'sql/backends/monet5` itself:
+
+
+# 18:27:33 >
+# 18:27:33 > "mserver5" "--debug=10" "--set" "gdk_nr_threads=0" "--set"
"mapi_open=true" "--set" "mapi_port=37004" "--set"
"mapi_usock=/var/tmp/mtest-1088/.s.monetdb.37004" "--set" "monet_prompt="
"--forcemito" "--set" "mal_listing=2"
"--dbpath=/home/mytherin/opt/var/mTests_sql_backends_monet5" "--set"
"mal_listing=0" "--set" "embedded_r=true" "--set" "embedded_py=true"
+# 18:27:33 >
+
+# MonetDB 5 server v11.22.0
+# This is an unreleased version
+# Serving database 'mTests_sql_backends_monet5', using 8 threads
+# Compiled for x86_64-unknown-linux-gnu/64bit with 64bit OIDs and 128bit
integers dynamically linked
+# Found 7.684 GiB available main-memory.
+# Copyright (c) 1993-July 2008 CWI.
+# Copyright (c) August 2008-2015 MonetDB B.V., all rights reserved
+# Visit http://www.monetdb.org/ for further information
+# Listening for connection requests on mapi:monetdb://mytherin-N750JV:37004/
+# Listening for UNIX domain connection requests on
mapi:monetdb:///var/tmp/mtest-1088/.s.monetdb.37004
+# Start processing logs sql/sql_logs version 52200
+# Finished processing logs sql/sql_logs
+# MonetDB/SQL module loaded
+# MonetDB/Python module loaded
+# MonetDB/R module loaded
+
+Ready.
+# SQL catalog created, loading sql scripts once
+# loading sql script: 09_like.sql
+# loading sql script: 10_math.sql
+# loading sql script: 11_times.sql
+# loading sql script: 12_url.sql
+# loading sql script: 13_date.sql
+# loading sql script: 14_inet.sql
+# loading sql script: 15_querylog.sql
+# loading sql script: 16_tracelog.sql
+# loading sql script: 17_temporal.sql
+# loading sql script: 20_vacuum.sql
+# loading sql script: 21_dependency_functions.sql
+# loading sql script: 22_clients.sql
+# loading sql script: 23_skyserver.sql
+# loading sql script: 24_zorder.sql
+# loading sql script: 25_debug.sql
+# loading sql script: 26_sysmon.sql
+# loading sql script: 27_rejects.sql
+# loading sql script: 39_analytics.sql
+# loading sql script: 39_analytics_hge.sql
+# loading sql script: 40_json.sql
+# loading sql script: 40_json_hge.sql
+# loading sql script: 41_md5sum.sql
+# loading sql script: 45_uuid.sql
+# loading sql script: 51_sys_schema_extension.sql
+# loading sql script: 75_storagemodel.sql
+# loading sql script: 80_statistics.sql
+# loading sql script: 80_udf.sql
+# loading sql script: 80_udf_hge.sql
+# loading sql script: 90_generator.sql
+# loading sql script: 90_generator_hge.sql
+# loading sql script: 99_system.sql
+test
+
+testtest
+
+test123
+testtest
+""test2""
+hello world
+
+y is not negative!
+Addition in python is not inconsistent!
_______________________________________________
checkin-list mailing list
[email protected]
https://www.monetdb.org/mailman/listinfo/checkin-list