Skip to content

Commit 8ee5d03

Browse files
Update imports to use direct namespace imports
WARNING: OLD STYLE IMPORTS DO NOT WORK The old style of importing the full module and using pb.* prefixes has been replaced with direct imports of the needed names. This makes the code more explicit about what is being used and removes the unnecessary pb prefix.
1 parent ccd2bb3 commit 8ee5d03

3 files changed

Lines changed: 28 additions & 26 deletions

File tree

README.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,25 @@ This is an LLVM IR generator for eBPF programs in Python. We use llvmlite to gen
1313
## Usage
1414
```python
1515
# pythonbpf_example.py
16-
import pythonbpf as pb
16+
from pythonbpf import bpf, map, bpfglobal, section, compile
1717
from pythonbpf.helpers import bpf_ktime_get_ns
1818
from pythonbpf.maps import HashMap
1919

2020
from ctypes import c_void_p, c_int64, c_int32, c_uint64
2121

22-
@pb.bpf
23-
@pb.map
22+
@bpf
23+
@map
2424
def last() -> HashMap:
2525
return HashMap(key_type=c_uint64, value_type=c_uint64, max_entries=1)
2626

27-
@pb.bpf
28-
@pb.section("tracepoint/syscalls/sys_enter_execve")
27+
@bpf
28+
@section("tracepoint/syscalls/sys_enter_execve")
2929
def hello(ctx: c_void_p) -> c_int32:
3030
print("entered")
3131
return c_int32(0)
3232

33-
@pb.bpf
34-
@pb.section("tracepoint/syscalls/sys_exit_execve")
33+
@bpf
34+
@section("tracepoint/syscalls/sys_exit_execve")
3535
def hello_again(ctx: c_void_p) -> c_int64:
3636
print("exited")
3737
key = 0
@@ -40,16 +40,16 @@ def hello_again(ctx: c_void_p) -> c_int64:
4040
ts = bpf_ktime_get_ns()
4141
return c_int64(0)
4242

43-
@pb.bpf
44-
@pb.bpfglobal
43+
@bpf
44+
@bpfglobal
4545
def LICENSE() -> str:
4646
return "GPL"
4747

4848
def some_normal_function():
4949
print("normal function")
5050

5151
# compiles and dumps object file in the same directory
52-
pb.compile()
52+
compile()
5353
```
5454
- Run `python pythonbpf_example.py` to get the compiled object file that can be then loaded into the kernel.
5555

examples/execve3.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
1-
import pythonbpf as pb
1+
from pythonbpf import bpf, map, section, bpfglobal, compile
22
from pythonbpf.helpers import bpf_ktime_get_ns
33
from pythonbpf.maps import HashMap
44

55
from ctypes import c_void_p, c_int64, c_int32, c_uint64
66

7-
@pb.bpf
8-
@pb.map
7+
@bpf
8+
@map
99
def last() -> HashMap:
1010
return HashMap(key_type=c_uint64, value_type=c_uint64, max_entries=1)
1111

1212

13-
@pb.bpf
14-
@pb.section("tracepoint/syscalls/sys_enter_execve")
13+
@bpf
14+
@section("tracepoint/syscalls/sys_enter_execve")
1515
def hello(ctx: c_void_p) -> c_int32:
1616
print("entered")
1717
print("multi constant support")
1818
return c_int32(0)
1919

2020

21-
@pb.bpf
22-
@pb.section("tracepoint/syscalls/sys_exit_execve")
21+
@bpf
22+
@section("tracepoint/syscalls/sys_exit_execve")
2323
def hello_again(ctx: c_void_p) -> c_int64:
2424
print("exited")
2525
key = 0
@@ -34,9 +34,9 @@ def hello_again(ctx: c_void_p) -> c_int64:
3434
return c_int64(0)
3535

3636

37-
@pb.bpf
38-
@pb.bpfglobal
37+
@bpf
38+
@bpfglobal
3939
def LICENSE() -> str:
4040
return "GPL"
4141

42-
pb.compile()
42+
compile()

pythonbpf/codegen.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
from .globals_pass import globals_processing
77
import os
88
import subprocess
9-
import pathlib
10-
import __main__
9+
import inspect
10+
from pathlib import Path
1111

1212
def find_bpf_chunks(tree):
1313
"""Find all functions decorated with @bpf in the AST."""
@@ -96,12 +96,14 @@ def compile_to_ir(filename: str, output: str):
9696
return output
9797

9898
def compile():
99-
main_file = pathlib.Path(__main__.__file__).resolve()
99+
# Look one level up the stack to the caller of this function
100+
caller_frame = inspect.stack()[1]
101+
caller_file = Path(caller_frame.filename).resolve()
100102

101-
ll_file = pathlib.Path("/tmp") / main_file.with_suffix(".ll").name
102-
o_file = main_file.with_suffix(".o")
103+
ll_file = Path("/tmp") / caller_file.with_suffix(".ll").name
104+
o_file = caller_file.with_suffix(".o")
103105

104-
compile_to_ir(str(main_file), str(ll_file))
106+
compile_to_ir(str(caller_file), str(ll_file))
105107

106108
subprocess.run([
107109
"llc", "-march=bpf", "-filetype=obj", "-O2",

0 commit comments

Comments
 (0)