Skip to content

Commit

Permalink
Add C enum for format
Browse files Browse the repository at this point in the history
  • Loading branch information
JelleZijlstra committed Sep 29, 2024
1 parent 80818c6 commit 4081f8f
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
7 changes: 7 additions & 0 deletions Include/internal/pycore_object.h
Original file line number Diff line number Diff line change
Expand Up @@ -879,6 +879,13 @@ PyAPI_DATA(int) _Py_SwappedOp[];

extern void _Py_GetConstant_Init(void);

enum _PyAnnotateFormat {
_Py_ANNOTATE_FORMAT_VALUE = 1,
_Py_ANNOTATE_FORMAT_VALUE_WITH_FAKE_GLOBALS = 2,
_Py_ANNOTATE_FORMAT_FORWARDREF = 3,
_Py_ANNOTATE_FORMAT_STRING = 4,
};

#ifdef __cplusplus
}
#endif
Expand Down
4 changes: 2 additions & 2 deletions Objects/typevarobject.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// TypeVar, TypeVarTuple, and ParamSpec
#include "Python.h"
#include "pycore_object.h" // _PyObject_GC_TRACK/UNTRACK
#include "pycore_object.h" // _PyObject_GC_TRACK/UNTRACK, PyAnnotateFormat
#include "pycore_typevarobject.h"
#include "pycore_unionobject.h" // _Py_union_type_or

Expand Down Expand Up @@ -168,7 +168,7 @@ constevaluator_call(PyObject *self, PyObject *args, PyObject *kwargs)
return NULL;
}
PyObject *value = ((constevaluatorobject *)self)->value;
if (format == 4) { // STRING
if (format == _Py_ANNOTATE_FORMAT_STRING) {
PyUnicodeWriter *writer = PyUnicodeWriter_Create(5); // cannot be <5
if (writer == NULL) {
return NULL;
Expand Down
6 changes: 3 additions & 3 deletions Python/codegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "pycore_instruction_sequence.h" // _PyInstructionSequence_NewLabel()
#include "pycore_intrinsics.h"
#include "pycore_long.h" // _PyLong_GetZero()
#include "pycore_object.h" // _Py_ANNOTATE_FORMAT_VALUE_WITH_FAKE_GLOBALS
#include "pycore_pystate.h" // _Py_GetConfig()
#include "pycore_symtable.h" // PySTEntryObject

Expand Down Expand Up @@ -655,9 +656,8 @@ codegen_setup_annotations_scope(compiler *c, location loc,
codegen_enter_scope(c, name, COMPILE_SCOPE_ANNOTATIONS,
key, loc.lineno, NULL, &umd));

// if .format > 2: raise NotImplementedError
_Py_DECLARE_STR(format, ".format");
PyObject *two = PyLong_FromLong(2);
// if .format > VALUE_WITH_FAKE_GLOBALS: raise NotImplementedError
PyObject *two = PyLong_FromLong(_Py_ANNOTATE_FORMAT_VALUE_WITH_FAKE_GLOBALS);
ADDOP_I(c, loc, LOAD_FAST, 0);
ADDOP_LOAD_CONST(c, loc, two);
ADDOP_I(c, loc, COMPARE_OP, (Py_GT << 5) | compare_masks[Py_GT]);
Expand Down

0 comments on commit 4081f8f

Please sign in to comment.