Skip to content

Commit

Permalink
try unpretty=ast-tree,expanded
Browse files Browse the repository at this point in the history
  • Loading branch information
tschorr committed Sep 20, 2022
1 parent 04a1c14 commit 4336544
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 11 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ build: src/py_class/py_class_impl2.rs src/py_class/py_class_impl3.rs python27-sy

test: build
cargo test $(CARGO_FLAGS)
ifeq ($(NIGHTLY),1)
# unpretty=ast-tree,expanded output is only supported on nightly
python$(PY) tests/check_symbols.py
endif

doc: build
cargo doc --no-deps $(CARGO_FLAGS)
Expand Down
50 changes: 39 additions & 11 deletions tests/check_symbols.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env python
import re
import sysconfig
import subprocess
import os
Expand Down Expand Up @@ -56,20 +57,47 @@
for name in interesting_config_values:
cfgs += ['--cfg', 'py_sys_config="{}_{}"'.format(name, sysconfig.get_config_var(name))]

subprocess.call(cargo_cmd + ['--'] + cfgs)
output = subprocess.check_output(['nm', '-C', '-g', '../target/debug/libpython{}_sys.rlib'.format(3 if sys.version_info.major == 3 else 27)])
lines = output.decode('ascii').split('\n')

def match_braces(text):
stack = []
locs = dict()
for i, c in enumerate(asttree):
if c == '{':
stack.append(i)
elif c == '}':
try:
locs[stack.pop()] = i
except IndexError:
break
return locs


foreignsig = 'ForeignMod {'
foreign_sections = []
foreign_symbols = set()

for line in lines:
parts = line.split(' ')
if len(parts) > 1:
foreign_symbols.add(parts[-1])
output = subprocess.check_output(cargo_cmd + ['--', '-Z', 'unpretty=ast-tree,expanded'] + cfgs)
asttree = output.decode('ascii')
while asttree:
idx = asttree.find(foreignsig)
if idx < 0:
break
asttree = asttree[asttree.find(foreignsig):]
locs = match_braces(asttree)
if locs:
endpos = locs[len(foreignsig) - 1] + 1
foreign_sections.append(asttree[:endpos])
asttree = asttree[endpos:]

for section in foreign_sections:
lines = section.split('\n')
for idx in range(len(lines)):
line = lines[idx]
if ('kind: Fn(' in line) or ('kind: Static(' in line):
foreign_symbols.add(re.sub(r'\s*ident: (.*)#[0-9]*,', r'\1', lines[idx-1]))

print(lines[:25])
print(len(foreign_symbols))
assert 'PyList_Type' in foreign_symbols, "Failed getting statics from nm"
assert 'PyList_New' in foreign_symbols, "Failed getting functions from nm"
assert 'PyList_Type' in foreign_symbols, "Failed getting statics from rustc -Z unpretty=ast-tree,expanded"
assert 'PyList_New' in foreign_symbols, "Failed getting functions from rustc -Z unpretty=ast-tree,expanded"

names = sorted(foreign_symbols - so_symbols)
if names:
Expand Down

0 comments on commit 4336544

Please sign in to comment.