pycforge 0.15.2__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.
Files changed (221) hide show
  1. pycforge/__init__.py +8 -0
  2. pycforge/__main__.py +2 -0
  3. pycforge/_version.py +1 -0
  4. pycforge/converter/__init__.py +0 -0
  5. pycforge/converter/analysis/__init__.py +5 -0
  6. pycforge/converter/analysis/functions.py +723 -0
  7. pycforge/converter/analysis/model.py +158 -0
  8. pycforge/converter/analysis/planning.py +464 -0
  9. pycforge/converter/analysis/stage.py +1976 -0
  10. pycforge/converter/analysis/symbols.py +237 -0
  11. pycforge/converter/analysis/validation.py +2166 -0
  12. pycforge/converter/c_output/__init__.py +2 -0
  13. pycforge/converter/c_output/conformance.py +297 -0
  14. pycforge/converter/c_output/renderer.py +220 -0
  15. pycforge/converter/conditional_regions/__init__.py +71 -0
  16. pycforge/converter/conditional_regions/analysis.py +481 -0
  17. pycforge/converter/conditional_regions/lowering.py +579 -0
  18. pycforge/converter/conditional_regions/model.py +243 -0
  19. pycforge/converter/conditional_regions/validation.py +690 -0
  20. pycforge/converter/containers/__init__.py +19 -0
  21. pycforge/converter/containers/analysis.py +542 -0
  22. pycforge/converter/containers/lowering.py +290 -0
  23. pycforge/converter/containers/model.py +146 -0
  24. pycforge/converter/contracts/__init__.py +0 -0
  25. pycforge/converter/contracts/configuration.py +178 -0
  26. pycforge/converter/contracts/identifiers.py +68 -0
  27. pycforge/converter/contracts/versions.py +71 -0
  28. pycforge/converter/core/__init__.py +0 -0
  29. pycforge/converter/core/artifact_io.py +87 -0
  30. pycforge/converter/core/cancellation.py +7 -0
  31. pycforge/converter/core/canonicalization.py +254 -0
  32. pycforge/converter/core/context.py +13 -0
  33. pycforge/converter/core/diagnostics.py +95 -0
  34. pycforge/converter/core/enums.py +23 -0
  35. pycforge/converter/core/fingerprint.py +22 -0
  36. pycforge/converter/core/pipeline.py +8 -0
  37. pycforge/converter/core/progress.py +25 -0
  38. pycforge/converter/core/request.py +50 -0
  39. pycforge/converter/core/resource_policy.py +31 -0
  40. pycforge/converter/core/result.py +23 -0
  41. pycforge/converter/core/serialization.py +36 -0
  42. pycforge/converter/core/stage.py +10 -0
  43. pycforge/converter/core/stage_artifact.py +80 -0
  44. pycforge/converter/core/stage_outcome.py +15 -0
  45. pycforge/converter/decision_trace/__init__.py +0 -0
  46. pycforge/converter/decision_trace/recorder.py +52 -0
  47. pycforge/converter/facade.py +459 -0
  48. pycforge/converter/frontend/__init__.py +4 -0
  49. pycforge/converter/frontend/normalizer.py +59 -0
  50. pycforge/converter/frontend/parser.py +20 -0
  51. pycforge/converter/frontend/source_document.py +93 -0
  52. pycforge/converter/frontend/stages.py +518 -0
  53. pycforge/converter/frontend/validation.py +76 -0
  54. pycforge/converter/io/__init__.py +0 -0
  55. pycforge/converter/io/atomic_writer.py +33 -0
  56. pycforge/converter/ir/__init__.py +1 -0
  57. pycforge/converter/ir/c_ir/__init__.py +4 -0
  58. pycforge/converter/ir/c_ir/builder.py +26 -0
  59. pycforge/converter/ir/c_ir/model.py +318 -0
  60. pycforge/converter/ir/c_ir/serialization.py +88 -0
  61. pycforge/converter/ir/c_ir/validation.py +834 -0
  62. pycforge/converter/ir/python_ir/__init__.py +17 -0
  63. pycforge/converter/ir/python_ir/nodes.py +86 -0
  64. pycforge/converter/keyword_calls/__init__.py +68 -0
  65. pycforge/converter/keyword_calls/analysis.py +458 -0
  66. pycforge/converter/keyword_calls/lowering.py +271 -0
  67. pycforge/converter/keyword_calls/model.py +304 -0
  68. pycforge/converter/keyword_calls/validation.py +802 -0
  69. pycforge/converter/keyword_only_calls/__init__.py +68 -0
  70. pycforge/converter/keyword_only_calls/analysis.py +640 -0
  71. pycforge/converter/keyword_only_calls/lowering.py +376 -0
  72. pycforge/converter/keyword_only_calls/model.py +328 -0
  73. pycforge/converter/keyword_only_calls/validation.py +1357 -0
  74. pycforge/converter/lowering.py +1000 -0
  75. pycforge/converter/modules/__init__.py +13 -0
  76. pycforge/converter/modules/analysis.py +1250 -0
  77. pycforge/converter/modules/lowering.py +200 -0
  78. pycforge/converter/modules/model.py +73 -0
  79. pycforge/converter/modules/stage.py +192 -0
  80. pycforge/converter/numeric_semantics/__init__.py +23 -0
  81. pycforge/converter/numeric_semantics/analysis.py +420 -0
  82. pycforge/converter/numeric_semantics/lowering.py +153 -0
  83. pycforge/converter/numeric_semantics/model.py +123 -0
  84. pycforge/converter/records/__init__.py +34 -0
  85. pycforge/converter/records/analysis.py +1394 -0
  86. pycforge/converter/records/lowering.py +393 -0
  87. pycforge/converter/records/model.py +319 -0
  88. pycforge/converter/rules/__init__.py +3 -0
  89. pycforge/converter/support_templates/__init__.py +49 -0
  90. pycforge/converter/support_templates/assembly.py +106 -0
  91. pycforge/converter/support_templates/factories.py +348 -0
  92. pycforge/converter/support_templates/model.py +159 -0
  93. pycforge/converter/support_templates/registry.py +388 -0
  94. pycforge/converter/telemetry/__init__.py +0 -0
  95. pycforge/converter/telemetry/sink.py +39 -0
  96. pycforge/ide/__init__.py +19 -0
  97. pycforge/ide/__main__.py +5 -0
  98. pycforge/ide/_worker_event_codec.py +276 -0
  99. pycforge/ide/_worker_protocol_json.py +197 -0
  100. pycforge/ide/_worker_protocol_types.py +90 -0
  101. pycforge/ide/_worker_request_codec.py +200 -0
  102. pycforge/ide/_worker_request_data.py +260 -0
  103. pycforge/ide/_worker_result_data.py +343 -0
  104. pycforge/ide/action_contract.py +469 -0
  105. pycforge/ide/action_validation.py +229 -0
  106. pycforge/ide/command_palette.py +223 -0
  107. pycforge/ide/controller.py +904 -0
  108. pycforge/ide/controller_conversion.py +546 -0
  109. pycforge/ide/controller_io.py +205 -0
  110. pycforge/ide/editor.py +951 -0
  111. pycforge/ide/editor_commands_qt.py +274 -0
  112. pycforge/ide/editor_lexical.py +43 -0
  113. pycforge/ide/editor_sidebars.py +236 -0
  114. pycforge/ide/editor_syntax.py +243 -0
  115. pycforge/ide/find_replace.py +535 -0
  116. pycforge/ide/icons.py +91 -0
  117. pycforge/ide/io_service.py +196 -0
  118. pycforge/ide/model.py +164 -0
  119. pycforge/ide/panels.py +903 -0
  120. pycforge/ide/positions.py +220 -0
  121. pycforge/ide/process_worker.py +130 -0
  122. pycforge/ide/qt.py +230 -0
  123. pycforge/ide/qt_actions.py +418 -0
  124. pycforge/ide/qt_close.py +169 -0
  125. pycforge/ide/qt_command_palette.py +297 -0
  126. pycforge/ide/qt_contract.py +220 -0
  127. pycforge/ide/qt_documents.py +590 -0
  128. pycforge/ide/qt_editor_buffers.py +113 -0
  129. pycforge/ide/qt_editor_surfaces.py +112 -0
  130. pycforge/ide/qt_menus.py +324 -0
  131. pycforge/ide/qt_projection.py +500 -0
  132. pycforge/ide/qt_shell.py +348 -0
  133. pycforge/ide/qt_shell_interactions.py +320 -0
  134. pycforge/ide/qt_state.py +393 -0
  135. pycforge/ide/qt_workspace_features.py +398 -0
  136. pycforge/ide/qt_workspace_navigation.py +208 -0
  137. pycforge/ide/qt_workspace_observers.py +173 -0
  138. pycforge/ide/qt_workspace_panels.py +545 -0
  139. pycforge/ide/qt_workspace_widgets.py +228 -0
  140. pycforge/ide/resources/icons/about.svg +1 -0
  141. pycforge/ide/resources/icons/add-document.svg +1 -0
  142. pycforge/ide/resources/icons/brand-mark.svg +1 -0
  143. pycforge/ide/resources/icons/bundle-search.svg +1 -0
  144. pycforge/ide/resources/icons/cancel.svg +1 -0
  145. pycforge/ide/resources/icons/check.svg +1 -0
  146. pycforge/ide/resources/icons/chevron-right.svg +1 -0
  147. pycforge/ide/resources/icons/close.svg +1 -0
  148. pycforge/ide/resources/icons/collapse-all.svg +1 -0
  149. pycforge/ide/resources/icons/command-palette.svg +1 -0
  150. pycforge/ide/resources/icons/convert.svg +1 -0
  151. pycforge/ide/resources/icons/copy.svg +1 -0
  152. pycforge/ide/resources/icons/cut.svg +1 -0
  153. pycforge/ide/resources/icons/decision-trace.svg +1 -0
  154. pycforge/ide/resources/icons/details.svg +1 -0
  155. pycforge/ide/resources/icons/diagnostics.svg +1 -0
  156. pycforge/ide/resources/icons/duplicate-line.svg +1 -0
  157. pycforge/ide/resources/icons/exit.svg +1 -0
  158. pycforge/ide/resources/icons/expand-all.svg +1 -0
  159. pycforge/ide/resources/icons/export.svg +1 -0
  160. pycforge/ide/resources/icons/find.svg +1 -0
  161. pycforge/ide/resources/icons/go-to-line.svg +1 -0
  162. pycforge/ide/resources/icons/go-to-output.svg +1 -0
  163. pycforge/ide/resources/icons/go-to-source.svg +1 -0
  164. pycforge/ide/resources/icons/history.svg +1 -0
  165. pycforge/ide/resources/icons/indent.svg +1 -0
  166. pycforge/ide/resources/icons/link-c.svg +1 -0
  167. pycforge/ide/resources/icons/mappings.svg +1 -0
  168. pycforge/ide/resources/icons/module.svg +1 -0
  169. pycforge/ide/resources/icons/move-down.svg +1 -0
  170. pycforge/ide/resources/icons/move-line-down.svg +1 -0
  171. pycforge/ide/resources/icons/move-line-up.svg +1 -0
  172. pycforge/ide/resources/icons/move-up.svg +1 -0
  173. pycforge/ide/resources/icons/next.svg +1 -0
  174. pycforge/ide/resources/icons/open.svg +1 -0
  175. pycforge/ide/resources/icons/outdent.svg +1 -0
  176. pycforge/ide/resources/icons/outline.svg +1 -0
  177. pycforge/ide/resources/icons/paste.svg +1 -0
  178. pycforge/ide/resources/icons/previous.svg +1 -0
  179. pycforge/ide/resources/icons/primary-module.svg +1 -0
  180. pycforge/ide/resources/icons/redo.svg +1 -0
  181. pycforge/ide/resources/icons/remove-document.svg +1 -0
  182. pycforge/ide/resources/icons/replace.svg +1 -0
  183. pycforge/ide/resources/icons/save-as.svg +1 -0
  184. pycforge/ide/resources/icons/save.svg +1 -0
  185. pycforge/ide/resources/icons/select-all.svg +1 -0
  186. pycforge/ide/resources/icons/settings.svg +1 -0
  187. pycforge/ide/resources/icons/show-c.svg +1 -0
  188. pycforge/ide/resources/icons/split-view.svg +1 -0
  189. pycforge/ide/resources/icons/summary.svg +1 -0
  190. pycforge/ide/resources/icons/telemetry.svg +1 -0
  191. pycforge/ide/resources/icons/toggle-comment.svg +1 -0
  192. pycforge/ide/resources/icons/toggle-fold.svg +1 -0
  193. pycforge/ide/resources/icons/undo.svg +1 -0
  194. pycforge/ide/resources/icons/whitespace.svg +1 -0
  195. pycforge/ide/revisions.py +400 -0
  196. pycforge/ide/search_service.py +280 -0
  197. pycforge/ide/session_history.py +150 -0
  198. pycforge/ide/source_editing.py +417 -0
  199. pycforge/ide/source_structure.py +558 -0
  200. pycforge/ide/source_structure_async.py +191 -0
  201. pycforge/ide/supervisor.py +503 -0
  202. pycforge/ide/theme.py +119 -0
  203. pycforge/ide/theme_stylesheet.py +598 -0
  204. pycforge/ide/theme_workspace_stylesheet.py +155 -0
  205. pycforge/ide/visual_tokens.py +142 -0
  206. pycforge/ide/worker_protocol.py +80 -0
  207. pycforge/ide/workspace_search.py +462 -0
  208. pycforge/ide/workspace_session.py +256 -0
  209. pycforge/laboratory/__init__.py +6 -0
  210. pycforge/laboratory/audits.py +2244 -0
  211. pycforge/laboratory/checkpoint_e.py +2685 -0
  212. pycforge/laboratory/cli.py +119 -0
  213. pycforge/laboratory/exit_codes.py +11 -0
  214. pycforge/laboratory/keyword_audit.py +661 -0
  215. pycforge/laboratory/keyword_only_audit.py +481 -0
  216. pycforge-0.15.2.dist-info/METADATA +209 -0
  217. pycforge-0.15.2.dist-info/RECORD +221 -0
  218. pycforge-0.15.2.dist-info/WHEEL +5 -0
  219. pycforge-0.15.2.dist-info/entry_points.txt +5 -0
  220. pycforge-0.15.2.dist-info/licenses/LICENSE +674 -0
  221. pycforge-0.15.2.dist-info/top_level.txt +1 -0
@@ -0,0 +1,518 @@
1
+ from __future__ import annotations
2
+
3
+ import ast
4
+ import tokenize
5
+ from types import MappingProxyType
6
+ from typing import Any
7
+
8
+ from pycforge.converter.contracts.configuration import supports_modules
9
+ from pycforge.converter.core.diagnostics import Diagnostic
10
+ from pycforge.converter.core.enums import Severity, StageTerminal
11
+ from pycforge.converter.core.fingerprint import fingerprint
12
+ from pycforge.converter.core.stage_artifact import StageArtifact
13
+ from pycforge.converter.core.stage_outcome import StageOutcome
14
+ from pycforge.converter.ir.python_ir import PythonIRBundle, PythonIRBundleDocument
15
+
16
+ from .normalizer import PythonNormalizer
17
+ from .parser import ParserVersionError, Python311ParserAdapter
18
+ from .source_document import SourceDocument
19
+ from .validation import validate_python_ir, validate_python_ir_bundle
20
+
21
+
22
+ def _artifact(kind: str, version: str, prior: StageArtifact, payload: dict[str, object]) -> StageArtifact:
23
+ serial = {key: list(value) if isinstance(value, tuple) else value for key, value in payload.items()}
24
+ artifact_fingerprint = fingerprint(
25
+ "stage-artifact",
26
+ {
27
+ "kind": kind,
28
+ "conversion_id": prior.conversion_id,
29
+ "parent": prior.artifact_fingerprint.value,
30
+ "payload": serial,
31
+ },
32
+ )
33
+ return StageArtifact(
34
+ kind,
35
+ version,
36
+ prior.conversion_id,
37
+ prior.artifact_fingerprint,
38
+ MappingProxyType(payload),
39
+ artifact_fingerprint,
40
+ )
41
+
42
+
43
+ def _module_request(services: Any) -> bool:
44
+ return supports_modules(services.context.canonical.request.rule_set_version)
45
+
46
+
47
+ def _inputs(services: Any):
48
+ bundle = services.context.canonical.request.source_bundle
49
+ return (bundle.primary,) + tuple(bundle.companions)
50
+
51
+
52
+ class SourceDocumentStage:
53
+ stage_id = "frontend.source_document"
54
+ input_schema = "initial/0.1"
55
+ output_schema = "source-bundle/0.2"
56
+
57
+ def run(self, artifact: StageArtifact, services: Any) -> StageOutcome:
58
+ if services.context.cancellation.is_canceled:
59
+ return _canceled(self.stage_id)
60
+ if not _module_request(services):
61
+ return self._legacy_run(artifact, services)
62
+
63
+ policy = services.context.canonical.request.resource_policy
64
+ documents: list[SourceDocument] = []
65
+ published: list[dict[str, object]] = []
66
+ total_lines = 0
67
+ total_tokens = 0
68
+ parser = Python311ParserAdapter()
69
+ for ordinal, source in enumerate(_inputs(services)):
70
+ if services.context.cancellation.is_canceled:
71
+ return _canceled(self.stage_id)
72
+ document = SourceDocument.create(source.logical_name, source.text)
73
+ total_lines += len(document.line_starts)
74
+ if total_lines > policy.max_source_lines:
75
+ return _rejected(
76
+ self.stage_id,
77
+ "PYC3510",
78
+ "SourceBundle exceeds the aggregate max_source_lines ceiling",
79
+ source_span=_point_span(document, 1, 0),
80
+ source_module_id=source.module_id,
81
+ source_logical_name=source.logical_name,
82
+ )
83
+ try:
84
+ document = parser.tokenize(document)
85
+ except (tokenize.TokenError, IndentationError) as exc:
86
+ return StageOutcome(
87
+ StageTerminal.REJECTED,
88
+ diagnostics=(
89
+ Diagnostic(
90
+ "PYC2002",
91
+ Severity.ERROR,
92
+ self.stage_id,
93
+ f"Tokenization failed: {exc}",
94
+ source_span=_tokenization_error_span(document, exc),
95
+ ),
96
+ ),
97
+ )
98
+ total_tokens += len(document.tokens)
99
+ if total_tokens > policy.max_tokens:
100
+ return _rejected(
101
+ self.stage_id,
102
+ "PYC3510",
103
+ "SourceBundle exceeds the aggregate max_tokens ceiling",
104
+ source_span=_point_span(document, 1, 0),
105
+ source_module_id=source.module_id,
106
+ source_logical_name=source.logical_name,
107
+ )
108
+ documents.append(document)
109
+ published.append(
110
+ {
111
+ "module_id": source.module_id,
112
+ "logical_name": source.logical_name,
113
+ "bundle_ordinal": ordinal,
114
+ "is_primary": ordinal == 0,
115
+ "document_id": document.document_id,
116
+ "source_document": document.to_dict(),
117
+ }
118
+ )
119
+
120
+ services.context.frontend_documents = tuple(documents)
121
+ order = tuple(artifact.payload.get("stage_order", ())) + (self.stage_id,)
122
+ source_bundle = {
123
+ "schema_version": "source-bundle/0.2",
124
+ "primary_module_id": published[0]["module_id"],
125
+ "documents": published,
126
+ "aggregate_source_lines": total_lines,
127
+ "aggregate_tokens": total_tokens,
128
+ }
129
+ return StageOutcome(
130
+ StageTerminal.COMPLETED,
131
+ _artifact(
132
+ "source_bundle",
133
+ "0.2",
134
+ artifact,
135
+ {"stage_order": order, "source_bundle": source_bundle},
136
+ ),
137
+ )
138
+
139
+ def _legacy_run(self, artifact: StageArtifact, services: Any) -> StageOutcome:
140
+ source = services.context.canonical.request.source_bundle.primary
141
+ document = SourceDocument.create(source.logical_name, source.text)
142
+ if len(document.line_starts) > services.context.canonical.request.resource_policy.max_source_lines:
143
+ return _rejected(self.stage_id, "PYC2007", "Source line count exceeds max_source_lines")
144
+ try:
145
+ document = Python311ParserAdapter().tokenize(document)
146
+ except (tokenize.TokenError, IndentationError) as exc:
147
+ return StageOutcome(
148
+ StageTerminal.REJECTED,
149
+ diagnostics=(
150
+ Diagnostic(
151
+ "PYC2002",
152
+ Severity.ERROR,
153
+ self.stage_id,
154
+ f"Tokenization failed: {exc}",
155
+ source_span=_tokenization_error_span(document, exc),
156
+ ),
157
+ ),
158
+ )
159
+ if len(document.tokens) > services.context.canonical.request.resource_policy.max_tokens:
160
+ return _rejected(self.stage_id, "PYC2003", "Token count exceeds max_tokens")
161
+ order = tuple(artifact.payload.get("stage_order", ())) + (self.stage_id,)
162
+ return StageOutcome(
163
+ StageTerminal.COMPLETED,
164
+ _artifact(
165
+ "source_document",
166
+ "0.3",
167
+ artifact,
168
+ {"stage_order": order, "source_document": document.to_dict()},
169
+ ),
170
+ )
171
+
172
+ def validate(self, artifact: StageArtifact, services: Any) -> tuple[bool, str]:
173
+ if _module_request(services):
174
+ bundle = artifact.payload.get("source_bundle", {})
175
+ return (
176
+ artifact.kind == "source_bundle"
177
+ and artifact.schema_version == "0.2"
178
+ and bundle.get("schema_version") == "source-bundle/0.2"
179
+ and bool(bundle.get("documents")),
180
+ "invalid source bundle artifact",
181
+ )
182
+ return (
183
+ artifact.kind == "source_document" and "source_document" in artifact.payload,
184
+ "invalid source document artifact",
185
+ )
186
+
187
+
188
+ class ParseStage:
189
+ stage_id = "frontend.parse"
190
+ input_schema = "source-bundle/0.2"
191
+ input_schemas = ("source-document/0.3", "source-bundle/0.2")
192
+ output_schema = "python-ast-bundle/0.4"
193
+
194
+ def run(self, artifact: StageArtifact, services: Any) -> StageOutcome:
195
+ if services.context.cancellation.is_canceled:
196
+ return _canceled(self.stage_id)
197
+ if not _module_request(services):
198
+ return self._legacy_run(artifact, services)
199
+
200
+ request = services.context.canonical.request
201
+ policy = request.resource_policy
202
+ documents = services.context.frontend_documents
203
+ if len(documents) != len(_inputs(services)):
204
+ return _internal(self.stage_id, "Source bundle scratch state is incomplete")
205
+ parser = Python311ParserAdapter()
206
+ trees: list[ast.Module] = []
207
+ counts: list[dict[str, object]] = []
208
+ total_nodes = 0
209
+ for ordinal, (source, document) in enumerate(zip(_inputs(services), documents)):
210
+ if services.context.cancellation.is_canceled:
211
+ return _canceled(self.stage_id)
212
+ assert isinstance(document, SourceDocument)
213
+ try:
214
+ tree = parser.parse(document, request.python_version)
215
+ except ParserVersionError as exc:
216
+ return _rejected(self.stage_id, "PYC2004", str(exc))
217
+ except SyntaxError as exc:
218
+ where = f"line {exc.lineno or 1}, column {exc.offset or 1}"
219
+ return StageOutcome(
220
+ StageTerminal.REJECTED,
221
+ diagnostics=(
222
+ Diagnostic(
223
+ "PYC2001",
224
+ Severity.ERROR,
225
+ self.stage_id,
226
+ f"Invalid Python syntax at {where}: {exc.msg}",
227
+ source_span=_syntax_error_span(document, exc),
228
+ ),
229
+ ),
230
+ )
231
+ if _ast_depth(tree) > policy.max_nesting_depth:
232
+ return _rejected(
233
+ self.stage_id,
234
+ "PYC2006",
235
+ f"AST nesting depth exceeds max_nesting_depth in module {source.module_id}",
236
+ source_span=_point_span(document, 1, 0),
237
+ source_module_id=source.module_id,
238
+ source_logical_name=source.logical_name,
239
+ )
240
+ count = sum(1 for _ in ast.walk(tree))
241
+ total_nodes += count
242
+ if total_nodes > policy.max_ast_nodes:
243
+ return _rejected(
244
+ self.stage_id,
245
+ "PYC3510",
246
+ "SourceBundle exceeds the aggregate max_ast_nodes ceiling",
247
+ source_span=_point_span(document, 1, 0),
248
+ source_module_id=source.module_id,
249
+ source_logical_name=source.logical_name,
250
+ )
251
+ trees.append(tree)
252
+ counts.append(
253
+ {
254
+ "module_id": source.module_id,
255
+ "document_id": document.document_id,
256
+ "bundle_ordinal": ordinal,
257
+ "ast_node_count": count,
258
+ }
259
+ )
260
+
261
+ services.context.frontend_trees = tuple(trees)
262
+ order = tuple(artifact.payload["stage_order"]) + (self.stage_id,)
263
+ payload = {
264
+ "stage_order": order,
265
+ "source_bundle": artifact.payload["source_bundle"],
266
+ "ast_documents": counts,
267
+ "aggregate_ast_nodes": total_nodes,
268
+ }
269
+ return StageOutcome(
270
+ StageTerminal.COMPLETED,
271
+ _artifact("python_ast_bundle", "0.4", artifact, payload),
272
+ )
273
+
274
+ def _legacy_run(self, artifact: StageArtifact, services: Any) -> StageOutcome:
275
+ source = services.context.canonical.request.source_bundle.primary
276
+ document = SourceDocument.create(source.logical_name, source.text)
277
+ try:
278
+ tree = Python311ParserAdapter().parse(
279
+ document,
280
+ services.context.canonical.request.python_version,
281
+ )
282
+ except ParserVersionError as exc:
283
+ return _rejected(self.stage_id, "PYC2004", str(exc))
284
+ except SyntaxError as exc:
285
+ where = f"line {exc.lineno or 1}, column {exc.offset or 1}"
286
+ return StageOutcome(
287
+ StageTerminal.REJECTED,
288
+ diagnostics=(
289
+ Diagnostic(
290
+ "PYC2001",
291
+ Severity.ERROR,
292
+ self.stage_id,
293
+ f"Invalid Python syntax at {where}: {exc.msg}",
294
+ source_span=_syntax_error_span(document, exc),
295
+ ),
296
+ ),
297
+ )
298
+ count = sum(1 for _ in ast.walk(tree))
299
+ policy = services.context.canonical.request.resource_policy
300
+ if _ast_depth(tree) > policy.max_nesting_depth:
301
+ return _rejected(self.stage_id, "PYC2006", "AST nesting depth exceeds max_nesting_depth")
302
+ if count > policy.max_ast_nodes:
303
+ return _rejected(self.stage_id, "PYC2005", "AST node count exceeds max_ast_nodes")
304
+ services.context.frontend_tree = tree
305
+ order = tuple(artifact.payload["stage_order"]) + (self.stage_id,)
306
+ return StageOutcome(
307
+ StageTerminal.COMPLETED,
308
+ _artifact(
309
+ "python_ast",
310
+ "0.3",
311
+ artifact,
312
+ {
313
+ "stage_order": order,
314
+ "ast_node_count": count,
315
+ "document_id": artifact.payload["source_document"]["document_id"],
316
+ },
317
+ ),
318
+ )
319
+
320
+ def validate(self, artifact: StageArtifact, services: Any) -> tuple[bool, str]:
321
+ if _module_request(services):
322
+ return (
323
+ artifact.kind == "python_ast_bundle"
324
+ and artifact.schema_version == "0.4"
325
+ and len(services.context.frontend_trees) == len(_inputs(services)),
326
+ "invalid parser bundle artifact",
327
+ )
328
+ return (
329
+ artifact.kind == "python_ast" and services.context.frontend_tree is not None,
330
+ "invalid parser artifact",
331
+ )
332
+
333
+
334
+ class NormalizeStage:
335
+ stage_id = "frontend.normalize"
336
+ input_schema = "python-ast-bundle/0.4"
337
+ input_schemas = ("python-ast/0.3", "python-ast-bundle/0.4")
338
+ output_schema = "python-ir-bundle/0.4"
339
+
340
+ def run(self, artifact: StageArtifact, services: Any) -> StageOutcome:
341
+ if services.context.cancellation.is_canceled:
342
+ return _canceled(self.stage_id)
343
+ if not _module_request(services):
344
+ return self._legacy_run(artifact, services)
345
+
346
+ documents = services.context.frontend_documents
347
+ trees = services.context.frontend_trees
348
+ sources = _inputs(services)
349
+ if len(documents) != len(trees) or len(trees) != len(sources):
350
+ return _internal(self.stage_id, "Parser bundle scratch state is incomplete")
351
+ normalized: list[PythonIRBundleDocument] = []
352
+ normalizer = PythonNormalizer()
353
+ try:
354
+ for ordinal, (source, document, tree) in enumerate(zip(sources, documents, trees)):
355
+ if services.context.cancellation.is_canceled:
356
+ return _canceled(self.stage_id)
357
+ assert isinstance(document, SourceDocument)
358
+ assert isinstance(tree, ast.Module)
359
+ module = normalizer.normalize(tree, document)
360
+ valid, message = validate_python_ir(module)
361
+ if not valid:
362
+ return _internal(self.stage_id, message, code="PYC9003")
363
+ normalized.append(
364
+ PythonIRBundleDocument(
365
+ module_id=source.module_id or "",
366
+ logical_name=source.logical_name,
367
+ bundle_ordinal=ordinal,
368
+ is_primary=ordinal == 0,
369
+ module=module,
370
+ )
371
+ )
372
+ except ValueError as exc:
373
+ return _internal(self.stage_id, f"Invalid parser source span: {exc}", code="PYC9004")
374
+
375
+ bundle = PythonIRBundle(
376
+ "python-ir-bundle/0.4",
377
+ normalized[0].module_id,
378
+ tuple(normalized),
379
+ )
380
+ valid, message = validate_python_ir_bundle(bundle)
381
+ if not valid:
382
+ return _internal(self.stage_id, message, code="PYC9003")
383
+ order = tuple(artifact.payload["stage_order"]) + (self.stage_id,)
384
+ payload = {
385
+ "stage_order": order,
386
+ "source_bundle": artifact.payload["source_bundle"],
387
+ "python_ir_bundle": bundle.to_dict(),
388
+ "aggregate_ast_nodes": artifact.payload["aggregate_ast_nodes"],
389
+ }
390
+ return StageOutcome(
391
+ StageTerminal.COMPLETED,
392
+ _artifact("python_ir_bundle", "0.4", artifact, payload),
393
+ )
394
+
395
+ def _legacy_run(self, artifact: StageArtifact, services: Any) -> StageOutcome:
396
+ source = services.context.canonical.request.source_bundle.primary
397
+ document = SourceDocument.create(source.logical_name, source.text)
398
+ try:
399
+ module = PythonNormalizer().normalize(services.context.frontend_tree, document)
400
+ except ValueError as exc:
401
+ return _internal(
402
+ self.stage_id,
403
+ f"Invalid parser source span: {exc}",
404
+ code="PYC9004",
405
+ )
406
+ valid, message = validate_python_ir(module)
407
+ if not valid:
408
+ return _internal(self.stage_id, message, code="PYC9003")
409
+ order = tuple(artifact.payload["stage_order"]) + (self.stage_id,)
410
+ return StageOutcome(
411
+ StageTerminal.COMPLETED,
412
+ _artifact(
413
+ "python_ir",
414
+ "0.3",
415
+ artifact,
416
+ {"stage_order": order, "python_ir": module.to_dict()},
417
+ ),
418
+ )
419
+
420
+ def validate(self, artifact: StageArtifact, services: Any) -> tuple[bool, str]:
421
+ if _module_request(services):
422
+ bundle = artifact.payload.get("python_ir_bundle", {})
423
+ return (
424
+ artifact.kind == "python_ir_bundle"
425
+ and artifact.schema_version == "0.4"
426
+ and bundle.get("schema_version") == "python-ir-bundle/0.4",
427
+ "invalid normalized Python IR bundle artifact",
428
+ )
429
+ return (
430
+ artifact.kind == "python_ir"
431
+ and artifact.payload.get("python_ir", {}).get("schema_version") == "python-ir/0.3",
432
+ "invalid normalized Python IR artifact",
433
+ )
434
+
435
+
436
+ def _ast_depth(root: ast.AST) -> int:
437
+ maximum = 0
438
+ stack = [(root, 1)]
439
+ while stack:
440
+ node, depth = stack.pop()
441
+ maximum = max(maximum, depth)
442
+ stack.extend((child, depth + 1) for child in ast.iter_child_nodes(node))
443
+ return maximum
444
+
445
+
446
+ def _canceled(stage_id: str) -> StageOutcome:
447
+ return StageOutcome(
448
+ StageTerminal.CANCELED,
449
+ diagnostics=(Diagnostic("PYC1901", Severity.ERROR, stage_id, "Conversion canceled"),),
450
+ )
451
+
452
+
453
+ def _rejected(
454
+ stage_id: str,
455
+ code: str,
456
+ message: str,
457
+ *,
458
+ source_span: dict[str, object] | None = None,
459
+ source_module_id: str | None = None,
460
+ source_logical_name: str | None = None,
461
+ ) -> StageOutcome:
462
+ return StageOutcome(
463
+ StageTerminal.REJECTED,
464
+ diagnostics=(
465
+ Diagnostic(
466
+ code,
467
+ Severity.ERROR,
468
+ stage_id,
469
+ message,
470
+ source_span=source_span,
471
+ source_module_id=source_module_id,
472
+ source_logical_name=source_logical_name,
473
+ ),
474
+ ),
475
+ )
476
+
477
+
478
+ def _internal(stage_id: str, message: str, *, code: str = "PYC9004") -> StageOutcome:
479
+ return StageOutcome(
480
+ StageTerminal.INTERNAL_FAILURE,
481
+ diagnostics=(Diagnostic(code, Severity.INTERNAL_ERROR, stage_id, message),),
482
+ )
483
+
484
+
485
+ def _point_span(document: SourceDocument, line: int, column: int) -> dict[str, object] | None:
486
+ try:
487
+ return document.span(line, column, line, column).to_dict()
488
+ except ValueError:
489
+ return None
490
+
491
+
492
+ def _tokenization_error_span(document: SourceDocument, exc: BaseException) -> dict[str, object] | None:
493
+ if (
494
+ isinstance(exc, tokenize.TokenError)
495
+ and len(exc.args) > 1
496
+ and isinstance(exc.args[1], tuple)
497
+ and len(exc.args[1]) == 2
498
+ ):
499
+ line, column = exc.args[1]
500
+ if isinstance(line, int) and isinstance(column, int):
501
+ return _point_span(document, line, column)
502
+ line = getattr(exc, "lineno", None)
503
+ offset = getattr(exc, "offset", None)
504
+ if isinstance(line, int) and isinstance(offset, int):
505
+ return _point_span(document, line, max(offset - 1, 0))
506
+ return None
507
+
508
+
509
+ def _syntax_error_span(document: SourceDocument, exc: SyntaxError) -> dict[str, object] | None:
510
+ start_line = exc.lineno or 1
511
+ start_column = max((exc.offset or 1) - 1, 0)
512
+ end_line = exc.end_lineno or start_line
513
+ end_offset = exc.end_offset if isinstance(exc.end_offset, int) and exc.end_offset > 0 else exc.offset
514
+ end_column = max((end_offset or 1) - 1, 0)
515
+ try:
516
+ return document.span(start_line, start_column, end_line, end_column).to_dict()
517
+ except ValueError:
518
+ return _point_span(document, start_line, start_column)
@@ -0,0 +1,76 @@
1
+ from __future__ import annotations
2
+ from pycforge.converter.ir.python_ir.nodes import (
3
+ PythonIRBundle,
4
+ PythonIRModule,
5
+ python_ir_reference_ids,
6
+ )
7
+
8
+ def validate_python_ir(
9
+ module: PythonIRModule,
10
+ *,
11
+ allowed_document_ids: frozenset[str] | None = None,
12
+ )->tuple[bool,str]:
13
+ if module.schema_version not in {"python-ir/0.3", "python-ir/0.4"}: return False,"unsupported Python IR schema"
14
+ permitted_documents = allowed_document_ids or frozenset((module.document_id,))
15
+ ids=[n.node_id for n in module.nodes]
16
+ if len(ids)!=len(set(ids)): return False,"duplicate Python IR node IDs"
17
+ id_set=set(ids)
18
+ if module.root_node_id not in id_set: return False,"missing Python IR root"
19
+ for node in module.nodes:
20
+ if not node.node_id.startswith(("py-","syn-")): return False,"invalid Python IR node identity domain"
21
+ if node.provenance.origin_kind not in {"source","synthetic"}: return False,"invalid Python IR provenance kind"
22
+ if node.provenance.origin_kind == "synthetic" and any(
23
+ origin_node_id not in id_set for origin_node_id in node.provenance.origin_node_ids
24
+ ):
25
+ return False,"synthetic Python IR provenance contains a dangling origin node"
26
+ if node.provenance.origin_kind == "source":
27
+ span=node.provenance.source_span
28
+ if span is not None:
29
+ if not isinstance(span,dict) or span.get("document_id") not in permitted_documents:return False,"source node has an invalid document span"
30
+ start,end=span.get("start",{}),span.get("end",{})
31
+ if any(not isinstance(pos.get(key),int) or pos.get(key)<0 for pos in (start,end) for key in ("line","column","offset")):
32
+ return False,"source node span contains invalid positions"
33
+ if end["offset"] < start["offset"]:
34
+ return False,"source node span is reversed"
35
+ field_names={name for name,_ in node.fields}
36
+ if field_names & {"generated_c","c_ir","c_type","target_representation"}:
37
+ return False,"normalized Python IR contains target-owned fields"
38
+ for field_name,value in node.fields:
39
+ if _contains_c_text(value): return False,"normalized Python IR contains generated C text"
40
+ for reference in python_ir_reference_ids(node.kind,field_name,value):
41
+ if reference not in id_set: return False,"normalized Python IR contains a dangling node reference"
42
+ return True,""
43
+
44
+
45
+ def validate_python_ir_bundle(bundle: PythonIRBundle) -> tuple[bool, str]:
46
+ if bundle.schema_version != "python-ir-bundle/0.4":
47
+ return False, "unsupported Python IR bundle schema"
48
+ if not bundle.documents or not 1 <= len(bundle.documents) <= 64:
49
+ return False, "Python IR bundle document count is outside the closed bound"
50
+ module_ids = [item.module_id for item in bundle.documents]
51
+ document_ids = [item.module.document_id for item in bundle.documents]
52
+ ordinals = [item.bundle_ordinal for item in bundle.documents]
53
+ if len(module_ids) != len(set(module_ids)):
54
+ return False, "duplicate module ID in Python IR bundle"
55
+ if len(document_ids) != len(set(document_ids)):
56
+ return False, "duplicate document ID in Python IR bundle"
57
+ if ordinals != list(range(len(bundle.documents))):
58
+ return False, "Python IR bundle ordinals are not contiguous source order"
59
+ if bundle.primary_module_id != bundle.documents[0].module_id or not bundle.documents[0].is_primary:
60
+ return False, "Python IR bundle primary identity is inconsistent"
61
+ if any(item.is_primary != (item.bundle_ordinal == 0) for item in bundle.documents):
62
+ return False, "Python IR bundle contains an invalid primary role"
63
+ all_node_ids: list[str] = []
64
+ for item in bundle.documents:
65
+ valid, message = validate_python_ir(item.module)
66
+ if not valid:
67
+ return False, message
68
+ all_node_ids.extend(node.node_id for node in item.module.nodes)
69
+ if len(all_node_ids) != len(set(all_node_ids)):
70
+ return False, "Python IR bundle contains duplicate cross-document node IDs"
71
+ return True, ""
72
+
73
+ def _contains_c_text(value:object)->bool:
74
+ # Structural guard against accidental target payload fields, not source strings.
75
+ if isinstance(value,tuple): return any(_contains_c_text(v) for v in value)
76
+ return False
File without changes
@@ -0,0 +1,33 @@
1
+ from __future__ import annotations
2
+ import os
3
+ from pathlib import Path
4
+ from tempfile import NamedTemporaryFile
5
+ from typing import Callable
6
+
7
+ class AtomicWriteError(OSError):
8
+ pass
9
+
10
+ class AtomicWriter:
11
+ def __init__(self, *, before_replace: Callable[[Path], None] | None = None) -> None:
12
+ self._before_replace = before_replace
13
+
14
+ def write_text(self, destination: Path, text: str) -> None:
15
+ destination = Path(destination)
16
+ temporary: Path | None = None
17
+ try:
18
+ destination.parent.mkdir(parents=True, exist_ok=True)
19
+ with NamedTemporaryFile("w", encoding="utf-8", newline="", dir=destination.parent, prefix=f".{destination.name}.", suffix=".tmp", delete=False) as handle:
20
+ temporary = Path(handle.name)
21
+ handle.write(text)
22
+ handle.flush()
23
+ os.fsync(handle.fileno())
24
+ if self._before_replace:
25
+ self._before_replace(temporary)
26
+ os.replace(temporary, destination)
27
+ temporary = None
28
+ except Exception as exc:
29
+ raise AtomicWriteError(f"atomic write failed for {destination}") from exc
30
+ finally:
31
+ if temporary is not None:
32
+ try: temporary.unlink()
33
+ except FileNotFoundError: pass
@@ -0,0 +1 @@
1
+ from .c_ir import *
@@ -0,0 +1,4 @@
1
+ from .model import *
2
+ from .builder import CTranslationUnitBuilder
3
+ from .validation import CIRValidationResult,validate_translation_unit
4
+ from .serialization import serialize_translation_unit
@@ -0,0 +1,26 @@
1
+ from __future__ import annotations
2
+ from dataclasses import dataclass, field
3
+ from .model import CExternalDeclaration,CInclude,CModuleManifestEntry,CProvenance,CTranslationUnit,LEGACY_SCHEMA_VERSION
4
+
5
+ @dataclass(slots=True)
6
+ class CTranslationUnitBuilder:
7
+ target_contract: str
8
+ node_id: str = "c-tu-0001"
9
+ provenance: CProvenance = CProvenance("synthetic")
10
+ schema_version: str = LEGACY_SCHEMA_VERSION
11
+ module_manifest: tuple[CModuleManifestEntry,...] = ()
12
+ module_order: tuple[str,...] = ()
13
+ module_dependencies: tuple[tuple[str,str],...] = ()
14
+ _includes: list[CInclude] = field(default_factory=list)
15
+ _declarations: list[CExternalDeclaration] = field(default_factory=list)
16
+ _sealed: bool = False
17
+ def add_include(self, include:CInclude)->None:
18
+ self._assert_open(); self._includes.append(include)
19
+ def add_declaration(self, declaration:CExternalDeclaration)->None:
20
+ self._assert_open(); self._declarations.append(declaration)
21
+ def build(self)->CTranslationUnit:
22
+ self._assert_open(); self._sealed=True
23
+ includes=tuple(sorted(self._includes,key=lambda x:(not x.system,x.header,x.node_id)))
24
+ return CTranslationUnit(self.schema_version,self.node_id,self.target_contract,includes,tuple(self._declarations),self.provenance,self.module_manifest,self.module_order,self.module_dependencies)
25
+ def _assert_open(self)->None:
26
+ if self._sealed: raise RuntimeError("C translation-unit builder is sealed")