Skip to content

Commit d135d64

Browse files
committed
Now properly expose PatternObject, doh
1 parent 7375cd6 commit d135d64

2 files changed

Lines changed: 78 additions & 9 deletions

File tree

src/org/python/core/BuiltinDocs.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4727,4 +4727,40 @@ public class BuiltinDocs {
47274727
public final static String traceback_tb_next_doc =
47284728
"";
47294729

4730+
public final static String sre_pattern_doc =
4731+
"Compiled regular expression objects";
4732+
4733+
public final static String sre_pattern_match_doc =
4734+
"match(string[, pos[, endpos]]) --> match object or None.\n" +
4735+
" Matches zero or more characters at the beginning of the string";
4736+
4737+
public final static String sre_pattern_findall_doc =
4738+
"findall(string[, pos[, endpos]]) --> list.\n" +
4739+
" Return a list of all non-overlapping matches of pattern in string.";
4740+
4741+
public final static String sre_pattern_finditer_doc =
4742+
"finditer(string[, pos[, endpos]]) --> iterator.\n" +
4743+
" Return an iterator over all non-overlapping matches for the \n" +
4744+
" RE pattern in string. For each match, the iterator returns a\n" +
4745+
" match object.";
4746+
4747+
public final static String sre_pattern_search_doc =
4748+
"search(string[, pos[, endpos]]) --> match object or None.\n" +
4749+
" Scan through string looking for a match, and return a corresponding\n" +
4750+
" match object instance. Return None if no position in the string matches.";
4751+
4752+
public final static String sre_pattern_split_doc =
4753+
"split(string[, maxsplit = 0]) --> list.\n" +
4754+
" Split string by the occurrences of pattern.";
4755+
4756+
public final static String sre_pattern_sub_doc =
4757+
"sub(repl, string[, count = 0]) --> newstring\n" +
4758+
" Return the string obtained by replacing the leftmost non-overlapping\n" +
4759+
" occurrences of pattern in string by the replacement repl.";
4760+
4761+
public final static String sre_pattern_subn_doc =
4762+
"subn(repl, string[, count = 0]) --> (newstring, number of subs)\n" +
4763+
" Return the tuple (new_string, number_of_subs_made) found by replacing\n" +
4764+
" the leftmost non-overlapping occurrences of pattern with the\n" +
4765+
" replacement repl.";
47304766
}

src/org/python/modules/sre/PatternObject.java

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@
1818

1919
import java.util.*;
2020
import org.python.core.*;
21+
import org.python.expose.ExposedGet;
22+
import org.python.expose.ExposedMethod;
2123
import org.python.expose.ExposedType;
2224

2325

24-
@ExposedType(name = "SRE_Pattern")
26+
@ExposedType(name = "SRE_Pattern", doc = BuiltinDocs.sre_pattern_doc)
2527
public class PatternObject extends PyObject implements Traverseproc {
2628
int[] code; /* link to the code string object */
2729
public PyString pattern; /* link to the pattern source (or None) */
@@ -45,7 +47,31 @@ public PatternObject(PyString pattern, int flags, int[] code,
4547
this.indexgroup = indexgroup;
4648
}
4749

48-
public MatchObject match(PyObject[] args, String[] kws) {
50+
@ExposedGet(name = "pattern")
51+
public PyString getPattern() {
52+
if (pattern == null) {
53+
return Py.EmptyString;
54+
}
55+
return pattern;
56+
}
57+
58+
@ExposedGet(name = "flags")
59+
public int getFlags() {
60+
return flags;
61+
}
62+
63+
@ExposedGet(name = "groups")
64+
public int getGroups() {
65+
return groups;
66+
}
67+
68+
@ExposedGet(name = "groupindex")
69+
public PyObject getGroupindex() {
70+
return groupindex;
71+
}
72+
73+
@ExposedMethod(doc = BuiltinDocs.sre_pattern_match_doc)
74+
public PyObject match(PyObject[] args, String[] kws) {
4975
ArgParser ap = new ArgParser("match", args, kws,
5076
"string", "pos", "endpos");
5177
PyString string = extractPyString(ap, 0);
@@ -56,10 +82,12 @@ public MatchObject match(PyObject[] args, String[] kws) {
5682
state.ptr = state.start;
5783
int status = state.SRE_MATCH(code, 0, 1);
5884

59-
return _pattern_new_match(state, string, status);
85+
MatchObject matchObject = _pattern_new_match(state, string, status);
86+
return matchObject != null ? matchObject : Py.None;
6087
}
61-
62-
public MatchObject search(PyObject[] args, String[] kws) {
88+
89+
@ExposedMethod(doc = BuiltinDocs.sre_pattern_search_doc)
90+
public PyObject search(PyObject[] args, String[] kws) {
6391
ArgParser ap = new ArgParser("search", args, kws,
6492
"string", "pos", "endpos");
6593
PyString string = extractPyString(ap, 0);
@@ -70,10 +98,12 @@ public MatchObject search(PyObject[] args, String[] kws) {
7098

7199
int status = state.SRE_SEARCH(code, 0);
72100

73-
return _pattern_new_match(state, string, status);
101+
MatchObject matchObject = _pattern_new_match(state, string, status);
102+
return matchObject != null ? matchObject : Py.None;
74103
}
75104

76105

106+
@ExposedMethod(doc = BuiltinDocs.sre_pattern_sub_doc)
77107
public PyObject sub(PyObject[] args, String[] kws) {
78108
ArgParser ap = new ArgParser("sub", args, kws,
79109
"repl", "string", "count");
@@ -84,7 +114,7 @@ public PyObject sub(PyObject[] args, String[] kws) {
84114
}
85115

86116

87-
117+
@ExposedMethod(doc = BuiltinDocs.sre_pattern_subn_doc)
88118
public PyObject subn(PyObject[] args, String[] kws) {
89119
ArgParser ap = new ArgParser("subn", args, kws,
90120
"repl", "string", "count");
@@ -184,6 +214,7 @@ private PyObject join_list(PyList list, PyString string) {
184214
return joiner.__getattr__("join").__call__(list);
185215
}
186216

217+
@ExposedMethod(doc = BuiltinDocs.sre_pattern_split_doc)
187218
public PyObject split(PyObject[] args, String[] kws) {
188219
ArgParser ap = new ArgParser("split", args, kws,
189220
"string", "maxsplit");
@@ -239,7 +270,7 @@ private PyObject call(String module, String function, PyObject[] args) {
239270
}
240271

241272

242-
273+
@ExposedMethod(doc = BuiltinDocs.sre_pattern_findall_doc)
243274
public PyObject findall(PyObject[] args, String[] kws) {
244275
ArgParser ap = new ArgParser("findall", args, kws,
245276
"string", "pos", "endpos");
@@ -291,12 +322,14 @@ public PyObject findall(PyObject[] args, String[] kws) {
291322
return new PyList(list);
292323
}
293324

325+
@ExposedMethod(doc = BuiltinDocs.sre_pattern_finditer_doc)
294326
public PyObject finditer(PyObject[] args, String[] kws) {
295327
ScannerObject scanner = scanner(args, kws);
296328
PyObject search = scanner.__findattr__("search");
297329
return new PyCallIter(search, Py.None);
298330
}
299-
331+
332+
@ExposedMethod()
300333
public ScannerObject scanner(PyObject[] args, String[] kws) {
301334
ArgParser ap = new ArgParser("scanner", args, kws,
302335
"pattern", "pos", "endpos");

0 commit comments

Comments
 (0)