Skip to content

Commit 7817984

Browse files
committed
first use of libexpat
1 parent bd3f295 commit 7817984

File tree

7 files changed

+100
-7
lines changed

7 files changed

+100
-7
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ cscope.out
77
libmicropython.a
88
lua-5.3.5
99
lua-5.3.5.tar.gz
10+
elements
1011
shell_lua
1112
shell_py
1213
shell_upy

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,6 @@
44
[submodule "cpython"]
55
path = cpython
66
url = https://github.com/python/cpython.git
7+
[submodule "libexpat"]
8+
path = libexpat
9+
url = https://github.com/libexpat/libexpat.git

Makefile

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
MPTOP = micropython
22
LUATOP = lua-5.3.5
33
PYTOP = cpython
4+
EXPATTOP = libexpat
45

56
all: test
67

@@ -48,7 +49,20 @@ shell_lua: lua shell.o emb_lua.o
4849
emb_lua.o: lua emb_lua.cpp
4950
g++ -std=c++11 -c emb_lua.cpp -I$(LUATOP)/src
5051

51-
test: shell_py shell_upy shell_lua
52+
elements: expat
53+
gcc -std=c99 -c elements.c -I$(EXPATTOP)/expat/lib
54+
g++ -o elements elements.o -lexpat -L$(EXPATTOP)/expat/lib
55+
# ./elements < $(EXPATTOP)/testdata/largefiles/nes96.xml
56+
57+
expat: $(EXPATTOP)/expat $(EXPATTOP)/expat/lib/libexpat.la
58+
59+
$(EXPATTOP)/expat:
60+
git submodule update --init --recursive
61+
62+
$(EXPATTOP)/expat/lib/libexpat.la:
63+
(cd $(EXPATTOP)/expat; mkdir -p m4; ./buildconf.sh; ./configure --without-docbook; make)
64+
65+
test: shell_py shell_upy shell_lua elements
5266
(export PYTHONHOME=$(PYTOP) ; export PYTHONPATH=$(PYTOP)/Lib:$(PYTOP)/build/lib.linux-x86_64-3.8 ; $(PYTOP)/python tests.py)
5367

5468
test_threads:
@@ -60,13 +74,15 @@ cscope:
6074
cscope -R
6175

6276
clean:
63-
rm -rf shell_upy shell_lua shell_py
77+
rm -rf shell_upy shell_lua shell_py elements
6478
rm -rf *.o libmicropython.a build
6579
rm -rf __pycache__
6680
rm -rf cscope.out
6781
rm -rf $(LUATOP)
82+
rm -rf $(EXPATTOP)/expat/lib/libexpat.la
6883

6984
clean_all: clean
7085
rm -rf $(LUATOP).tar.gz
7186
git submodule deinit $(MPTOP)
7287
git submodule deinit $(PYTOP)
88+
git submodule deinit $(EXPATTOP)

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Some python tests to verify the stuff
77
Prerequisites for Ubuntu:
88

99
```
10-
sudo apt install build-essential unzip
10+
sudo apt install build-essential unzip libtool
1111
```
1212

1313
To download, build everything and run the tests:

elements.c

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include <stdio.h>
2+
#include <expat.h>
3+
4+
static void XMLCALL
5+
startElement(void *userData, const XML_Char *name, const XML_Char **atts)
6+
{
7+
int i;
8+
int *depthPtr = (int *)userData;
9+
(void)atts;
10+
11+
for (i = 0; i < *depthPtr; i++)
12+
putchar('\t');
13+
printf("%s\n", name);
14+
*depthPtr += 1;
15+
}
16+
17+
static void XMLCALL
18+
endElement(void *userData, const XML_Char *name)
19+
{
20+
int *depthPtr = (int *)userData;
21+
(void)name;
22+
23+
*depthPtr -= 1;
24+
}
25+
26+
int
27+
main(int argc, char *argv[])
28+
{
29+
char buf[BUFSIZ];
30+
XML_Parser parser = XML_ParserCreate(NULL);
31+
int done;
32+
int depth = 0;
33+
(void)argc;
34+
(void)argv;
35+
36+
XML_SetUserData(parser, &depth);
37+
XML_SetElementHandler(parser, startElement, endElement);
38+
do {
39+
size_t len = fread(buf, 1, sizeof(buf), stdin);
40+
done = len < sizeof(buf);
41+
if (XML_Parse(parser, buf, (int)len, done) == XML_STATUS_ERROR) {
42+
fprintf(stderr, "%s at line %lu\n",
43+
XML_ErrorString(XML_GetErrorCode(parser)),
44+
XML_GetCurrentLineNumber(parser));
45+
return 1;
46+
}
47+
} while (!done);
48+
XML_ParserFree(parser);
49+
return 0;
50+
}

libexpat

Submodule libexpat added at 39e487d

tests.py

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,38 @@
22
import unittest
33

44

5-
class Shell:
6-
def __init__(self, lang):
7-
prog = "./shell_" + lang
5+
class Prog:
6+
def __init__(self, *cmd):
87
self.shell = subprocess.Popen(
9-
prog, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
8+
cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
109

1110
def __getattr__(self, item):
1211
return getattr(self.shell, item)
1312

1413

14+
class Shell(Prog):
15+
def __init__(self, lang):
16+
super().__init__("./shell_" + lang)
17+
18+
19+
class TestElements(unittest.TestCase):
20+
def test_ok(self):
21+
prog = Prog("./elements")
22+
prog.stdin.write(b"<xml><tag></tag></xml>")
23+
stdoutdata, stderrdata = prog.communicate()
24+
self.assertEqual(prog.returncode, 0)
25+
self.assertIn(b"tag", stdoutdata)
26+
self.assertFalse(stderrdata)
27+
28+
def test_nok(self):
29+
prog = Prog("./elements")
30+
prog.stdin.write(b"<xml></xml-wrong>")
31+
stdoutdata, stderrdata = prog.communicate()
32+
self.assertNotEqual(prog.returncode, 0)
33+
self.assertIn(b"xml", stdoutdata)
34+
self.assertIn(b"mismatched tag at line 1", stderrdata)
35+
36+
1537
class TestPy(unittest.TestCase):
1638
def test_print(self):
1739
shell = Shell("py")

0 commit comments

Comments
 (0)