pytunia 0.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
pytunia/__init__.py ADDED
@@ -0,0 +1,34 @@
1
+ """
2
+ pytunia - Jasmin, the assembler for the Java Virtual Machine, in Python.
3
+
4
+ This package is a port of Jasmin 2.4 (Jonathan Meyer, Daniel Reynaud,
5
+ Iouri Kharon) from Java to Python. It reads Jasmin assembly code and
6
+ produces Java class files that are byte-for-byte identical to those
7
+ produced by the Java version.
8
+
9
+ import pytunia
10
+
11
+ # assemble a file and write the class file (returns its path)
12
+ path = pytunia.assemble("Hello.j", output_dir="classes")
13
+
14
+ # assemble source text in memory
15
+ class_name, data = pytunia.assemble_string(source)
16
+
17
+ Errors raise pytunia.AssemblyError; its attribute ``errors`` is a list of
18
+ Diagnostic objects, and str(error) is the error output of Jasmin.
19
+
20
+ For debugging, the original command line interface is available as
21
+ ``python3 -m pytunia [-d <outpath>] [-g] [-e <encoding>] <file> ...``.
22
+ """
23
+
24
+ from .api import assemble, assemble_string
25
+ from .classfile import ClassFile
26
+ from .diagnostics import AssemblyError, Diagnostic
27
+ from .main import VERSION as JASMIN_VERSION
28
+
29
+ # the version of pytunia (JASMIN_VERSION is the version of Jasmin that
30
+ # pytunia reproduces)
31
+ __version__ = "0.1"
32
+
33
+ __all__ = ["assemble", "assemble_string", "AssemblyError", "Diagnostic",
34
+ "ClassFile", "JASMIN_VERSION"]
pytunia/__main__.py ADDED
@@ -0,0 +1,7 @@
1
+ """Allows running Jasmin with ``python3 -m pytunia``."""
2
+
3
+ import sys
4
+
5
+ from .main import main
6
+
7
+ sys.exit(main())
pytunia/api.py ADDED
@@ -0,0 +1,106 @@
1
+ """
2
+ The library interface of the assembler.
3
+
4
+ import pytunia
5
+
6
+ # assemble a file and write the class file (returns its path)
7
+ path = pytunia.assemble("Hello.j", output_dir="classes")
8
+
9
+ # assemble source text in memory
10
+ class_name, data = pytunia.assemble_string(source)
11
+
12
+ Both functions raise pytunia.AssemblyError if the source contains errors.
13
+ They print nothing, and they can be called from several threads at once.
14
+ """
15
+
16
+ import os
17
+ import pathlib
18
+
19
+ from .classfile import ClassFile
20
+ from .diagnostics import AssemblyError
21
+ from .jas import JasError
22
+ from .javacompat import JavaException, from_java_string
23
+ from .main import class_file_location, _os_path
24
+ from .scanner import split_class_field
25
+
26
+
27
+ def _assemble(text, filename, display_name, line_numbers,
28
+ default_source=False):
29
+ """Assembles source text; returns the ClassFile object and the class
30
+ file. ``filename`` is used in the error messages and as the default
31
+ for the SourceFile attribute; ``display_name`` is used where Jasmin
32
+ shows the name given on the command line."""
33
+ cf = ClassFile()
34
+ try:
35
+ cf.read_jasmin(text, filename, line_numbers)
36
+ if cf.error_count() == 0:
37
+ if default_source and not cf.source_given:
38
+ _, simple_name = split_class_field(cf.get_class_name())
39
+ cf.source_name = simple_name + ".j"
40
+ return cf, cf.to_bytes()
41
+ except JasError as e:
42
+ cf.report_error("JAS Error: " + e.get_message(), e.num_tag)
43
+ except JavaException as e:
44
+ cf.report_error("%s: exception - <%s> %s."
45
+ % (display_name, e.java_class, e.get_message()))
46
+ raise AssemblyError(display_name, cf.diagnostics)
47
+
48
+
49
+ def assemble(source_file, output_dir=None, *, line_numbers=False,
50
+ encoding="utf-8"):
51
+ """Assembles the Jasmin file ``source_file`` and writes the class file.
52
+
53
+ The class file is placed below ``output_dir`` (default: the current
54
+ directory) in subdirectories for its package, e.g. the class
55
+ ``examples.Count`` goes to ``<output_dir>/examples/Count.class``. So
56
+ ``output_dir`` can be used as a class path. Directories are created
57
+ as needed.
58
+
59
+ If ``line_numbers`` is true, a line number table that refers to the
60
+ lines of the Jasmin source is generated (option -g of Jasmin).
61
+ ``encoding`` is the encoding of the source file.
62
+
63
+ Returns the path of the class file (a pathlib.Path). Raises
64
+ AssemblyError if the source contains errors and OSError (e.g.
65
+ FileNotFoundError) if a file cannot be read or written.
66
+
67
+ Note: file names in ``.attribute`` directives are relative to the
68
+ current directory, as in Jasmin.
69
+ """
70
+ fname = os.fsdecode(source_file)
71
+ with open(fname, 'rb') as f:
72
+ data = f.read()
73
+ text = data.decode(encoding, 'replace')
74
+ cf, code = _assemble(text, os.path.basename(fname), fname, line_numbers)
75
+ dest_path = None if output_dir is None else os.fsdecode(output_dir)
76
+ _, dest_dir, out_file = class_file_location(cf.get_class_name(),
77
+ dest_path)
78
+ if dest_dir is not None:
79
+ os.makedirs(_os_path(dest_dir), exist_ok=True)
80
+ path = pathlib.Path(_os_path(out_file))
81
+ path.write_bytes(code)
82
+ return path
83
+
84
+
85
+ def assemble_string(source, name=None, *, line_numbers=False):
86
+ """Assembles Jasmin source text in memory.
87
+
88
+ ``name`` is the name of the source file; it is used in error messages
89
+ and as the default for the SourceFile attribute of the class. If it
90
+ is omitted, error messages show "<string>" and the SourceFile
91
+ attribute is "<class name>.j" (unless the source has a .source
92
+ directive), as if the source had been read from a file named after
93
+ the class.
94
+
95
+ Returns the pair (class name, class file). The class name is in the
96
+ internal form of the JVM, e.g. "examples/Count"; the class file is a
97
+ bytes object. Raises AssemblyError if the source contains errors.
98
+ """
99
+ if name is None:
100
+ filename = display_name = "<string>"
101
+ else:
102
+ display_name = os.fsdecode(name)
103
+ filename = os.path.basename(display_name)
104
+ cf, code = _assemble(source, filename, display_name, line_numbers,
105
+ default_source=name is None)
106
+ return from_java_string(cf.get_class_name()), code